blob: d43cb0bb434fb19c0f181e93ac034c4f4b68a05d [file] [log] [blame]
Mathias Agopian3cf61352010-02-09 17:46:37 -08001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
Mathias Agopian3cf61352010-02-09 17:46:37 -08004**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18//#define LOG_NDEBUG 0
19#define LOG_TAG "Camera"
20#include <utils/Log.h>
21#include <utils/threads.h>
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080022#include <binder/IPCThreadState.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080023#include <binder/IServiceManager.h>
24#include <binder/IMemory.h>
25
26#include <camera/Camera.h>
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080027#include <camera/ICameraRecordingProxyListener.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080028#include <camera/ICameraService.h>
29
Mathias Agopiandf712ea2012-02-25 18:48:35 -080030#include <gui/ISurfaceTexture.h>
31#include <gui/Surface.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080032
33namespace android {
34
35// client singleton for camera service binder interface
36Mutex Camera::mLock;
37sp<ICameraService> Camera::mCameraService;
38sp<Camera::DeathNotifier> Camera::mDeathNotifier;
39
40// establish binder interface to camera service
41const sp<ICameraService>& Camera::getCameraService()
42{
43 Mutex::Autolock _l(mLock);
44 if (mCameraService.get() == 0) {
45 sp<IServiceManager> sm = defaultServiceManager();
46 sp<IBinder> binder;
47 do {
48 binder = sm->getService(String16("media.camera"));
49 if (binder != 0)
50 break;
Steve Block5ff1dd52012-01-05 23:22:43 +000051 ALOGW("CameraService not published, waiting...");
Mathias Agopian3cf61352010-02-09 17:46:37 -080052 usleep(500000); // 0.5 s
53 } while(true);
54 if (mDeathNotifier == NULL) {
55 mDeathNotifier = new DeathNotifier();
56 }
57 binder->linkToDeath(mDeathNotifier);
58 mCameraService = interface_cast<ICameraService>(binder);
59 }
Steve Block29357bc2012-01-06 19:20:56 +000060 ALOGE_IF(mCameraService==0, "no CameraService!?");
Mathias Agopian3cf61352010-02-09 17:46:37 -080061 return mCameraService;
62}
63
64// ---------------------------------------------------------------------------
65
66Camera::Camera()
67{
68 init();
69}
70
71// construct a camera client from an existing camera remote
72sp<Camera> Camera::create(const sp<ICamera>& camera)
73{
Steve Block3856b092011-10-20 11:56:00 +010074 ALOGV("create");
Mathias Agopian3cf61352010-02-09 17:46:37 -080075 if (camera == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000076 ALOGE("camera remote is a NULL pointer");
Mathias Agopian3cf61352010-02-09 17:46:37 -080077 return 0;
78 }
79
80 sp<Camera> c = new Camera();
81 if (camera->connect(c) == NO_ERROR) {
82 c->mStatus = NO_ERROR;
83 c->mCamera = camera;
84 camera->asBinder()->linkToDeath(c);
Wu-cheng Li627baac2011-01-04 20:00:55 +080085 return c;
Mathias Agopian3cf61352010-02-09 17:46:37 -080086 }
Wu-cheng Li627baac2011-01-04 20:00:55 +080087 return 0;
Mathias Agopian3cf61352010-02-09 17:46:37 -080088}
89
90void Camera::init()
91{
92 mStatus = UNKNOWN_ERROR;
93}
94
95Camera::~Camera()
96{
Chih-Chung Changd06618e2010-05-13 15:14:24 +080097 // We don't need to call disconnect() here because if the CameraService
98 // thinks we are the owner of the hardware, it will hold a (strong)
99 // reference to us, and we can't possibly be here. We also don't want to
100 // call disconnect() here if we are in the same process as mediaserver,
101 // because we may be invoked by CameraService::Client::connect() and will
102 // deadlock if we call any method of ICamera here.
Mathias Agopian3cf61352010-02-09 17:46:37 -0800103}
104
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800105int32_t Camera::getNumberOfCameras()
106{
107 const sp<ICameraService>& cs = getCameraService();
108 if (cs == 0) return 0;
109 return cs->getNumberOfCameras();
110}
111
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800112status_t Camera::getCameraInfo(int cameraId,
113 struct CameraInfo* cameraInfo) {
114 const sp<ICameraService>& cs = getCameraService();
115 if (cs == 0) return UNKNOWN_ERROR;
116 return cs->getCameraInfo(cameraId, cameraInfo);
117}
118
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800119sp<Camera> Camera::connect(int cameraId)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800120{
Steve Block3856b092011-10-20 11:56:00 +0100121 ALOGV("connect");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800122 sp<Camera> c = new Camera();
123 const sp<ICameraService>& cs = getCameraService();
124 if (cs != 0) {
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800125 c->mCamera = cs->connect(c, cameraId);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800126 }
127 if (c->mCamera != 0) {
128 c->mCamera->asBinder()->linkToDeath(c);
129 c->mStatus = NO_ERROR;
130 } else {
131 c.clear();
132 }
133 return c;
134}
135
136void Camera::disconnect()
137{
Steve Block3856b092011-10-20 11:56:00 +0100138 ALOGV("disconnect");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800139 if (mCamera != 0) {
140 mCamera->disconnect();
Chih-Chung Changf8ed70a2010-03-24 16:38:02 -0700141 mCamera->asBinder()->unlinkToDeath(this);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800142 mCamera = 0;
143 }
144}
145
146status_t Camera::reconnect()
147{
Steve Block3856b092011-10-20 11:56:00 +0100148 ALOGV("reconnect");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800149 sp <ICamera> c = mCamera;
150 if (c == 0) return NO_INIT;
151 return c->connect(this);
152}
153
154sp<ICamera> Camera::remote()
155{
156 return mCamera;
157}
158
159status_t Camera::lock()
160{
161 sp <ICamera> c = mCamera;
162 if (c == 0) return NO_INIT;
163 return c->lock();
164}
165
166status_t Camera::unlock()
167{
168 sp <ICamera> c = mCamera;
169 if (c == 0) return NO_INIT;
170 return c->unlock();
171}
172
Jamie Gennis4b791682010-08-10 16:37:53 -0700173// pass the buffered Surface to the camera service
Mathias Agopian3cf61352010-02-09 17:46:37 -0800174status_t Camera::setPreviewDisplay(const sp<Surface>& surface)
175{
Steve Block3856b092011-10-20 11:56:00 +0100176 ALOGV("setPreviewDisplay(%p)", surface.get());
Mathias Agopian3cf61352010-02-09 17:46:37 -0800177 sp <ICamera> c = mCamera;
178 if (c == 0) return NO_INIT;
179 if (surface != 0) {
Jamie Gennis4b791682010-08-10 16:37:53 -0700180 return c->setPreviewDisplay(surface);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800181 } else {
Steve Blockb8a80522011-12-20 16:23:08 +0000182 ALOGD("app passed NULL surface");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800183 return c->setPreviewDisplay(0);
184 }
185}
186
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800187// pass the buffered ISurfaceTexture to the camera service
188status_t Camera::setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture)
189{
Steve Block3856b092011-10-20 11:56:00 +0100190 ALOGV("setPreviewTexture(%p)", surfaceTexture.get());
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800191 sp <ICamera> c = mCamera;
192 if (c == 0) return NO_INIT;
193 if (surfaceTexture != 0) {
194 return c->setPreviewTexture(surfaceTexture);
195 } else {
Steve Blockb8a80522011-12-20 16:23:08 +0000196 ALOGD("app passed NULL surface");
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800197 return c->setPreviewTexture(0);
198 }
199}
200
Mathias Agopian3cf61352010-02-09 17:46:37 -0800201// start preview mode
202status_t Camera::startPreview()
203{
Steve Block3856b092011-10-20 11:56:00 +0100204 ALOGV("startPreview");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800205 sp <ICamera> c = mCamera;
206 if (c == 0) return NO_INIT;
207 return c->startPreview();
208}
209
James Donge2ad6732010-10-18 20:42:51 -0700210status_t Camera::storeMetaDataInBuffers(bool enabled)
211{
Steve Block3856b092011-10-20 11:56:00 +0100212 ALOGV("storeMetaDataInBuffers: %s",
James Donge2ad6732010-10-18 20:42:51 -0700213 enabled? "true": "false");
214 sp <ICamera> c = mCamera;
215 if (c == 0) return NO_INIT;
216 return c->storeMetaDataInBuffers(enabled);
217}
218
Mathias Agopian3cf61352010-02-09 17:46:37 -0800219// start recording mode, must call setPreviewDisplay first
220status_t Camera::startRecording()
221{
Steve Block3856b092011-10-20 11:56:00 +0100222 ALOGV("startRecording");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800223 sp <ICamera> c = mCamera;
224 if (c == 0) return NO_INIT;
225 return c->startRecording();
226}
227
228// stop preview mode
229void Camera::stopPreview()
230{
Steve Block3856b092011-10-20 11:56:00 +0100231 ALOGV("stopPreview");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800232 sp <ICamera> c = mCamera;
233 if (c == 0) return;
234 c->stopPreview();
235}
236
237// stop recording mode
238void Camera::stopRecording()
239{
Steve Block3856b092011-10-20 11:56:00 +0100240 ALOGV("stopRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800241 {
242 Mutex::Autolock _l(mLock);
243 mRecordingProxyListener.clear();
244 }
Mathias Agopian3cf61352010-02-09 17:46:37 -0800245 sp <ICamera> c = mCamera;
246 if (c == 0) return;
247 c->stopRecording();
248}
249
250// release a recording frame
251void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
252{
Steve Block3856b092011-10-20 11:56:00 +0100253 ALOGV("releaseRecordingFrame");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800254 sp <ICamera> c = mCamera;
255 if (c == 0) return;
256 c->releaseRecordingFrame(mem);
257}
258
259// get preview state
260bool Camera::previewEnabled()
261{
Steve Block3856b092011-10-20 11:56:00 +0100262 ALOGV("previewEnabled");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800263 sp <ICamera> c = mCamera;
264 if (c == 0) return false;
265 return c->previewEnabled();
266}
267
268// get recording state
269bool Camera::recordingEnabled()
270{
Steve Block3856b092011-10-20 11:56:00 +0100271 ALOGV("recordingEnabled");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800272 sp <ICamera> c = mCamera;
273 if (c == 0) return false;
274 return c->recordingEnabled();
275}
276
277status_t Camera::autoFocus()
278{
Steve Block3856b092011-10-20 11:56:00 +0100279 ALOGV("autoFocus");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800280 sp <ICamera> c = mCamera;
281 if (c == 0) return NO_INIT;
282 return c->autoFocus();
283}
284
285status_t Camera::cancelAutoFocus()
286{
Steve Block3856b092011-10-20 11:56:00 +0100287 ALOGV("cancelAutoFocus");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800288 sp <ICamera> c = mCamera;
289 if (c == 0) return NO_INIT;
290 return c->cancelAutoFocus();
291}
292
293// take a picture
James Donge468ac52011-02-17 16:38:06 -0800294status_t Camera::takePicture(int msgType)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800295{
Steve Block3856b092011-10-20 11:56:00 +0100296 ALOGV("takePicture: 0x%x", msgType);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800297 sp <ICamera> c = mCamera;
298 if (c == 0) return NO_INIT;
James Donge468ac52011-02-17 16:38:06 -0800299 return c->takePicture(msgType);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800300}
301
302// set preview/capture parameters - key/value pairs
303status_t Camera::setParameters(const String8& params)
304{
Steve Block3856b092011-10-20 11:56:00 +0100305 ALOGV("setParameters");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800306 sp <ICamera> c = mCamera;
307 if (c == 0) return NO_INIT;
308 return c->setParameters(params);
309}
310
311// get preview/capture parameters - key/value pairs
312String8 Camera::getParameters() const
313{
Steve Block3856b092011-10-20 11:56:00 +0100314 ALOGV("getParameters");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800315 String8 params;
316 sp <ICamera> c = mCamera;
317 if (c != 0) params = mCamera->getParameters();
318 return params;
319}
320
321// send command to camera driver
322status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
323{
Steve Block3856b092011-10-20 11:56:00 +0100324 ALOGV("sendCommand");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800325 sp <ICamera> c = mCamera;
326 if (c == 0) return NO_INIT;
327 return c->sendCommand(cmd, arg1, arg2);
328}
329
330void Camera::setListener(const sp<CameraListener>& listener)
331{
332 Mutex::Autolock _l(mLock);
333 mListener = listener;
334}
335
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800336void Camera::setRecordingProxyListener(const sp<ICameraRecordingProxyListener>& listener)
337{
338 Mutex::Autolock _l(mLock);
339 mRecordingProxyListener = listener;
340}
341
Mathias Agopian3cf61352010-02-09 17:46:37 -0800342void Camera::setPreviewCallbackFlags(int flag)
343{
Steve Block3856b092011-10-20 11:56:00 +0100344 ALOGV("setPreviewCallbackFlags");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800345 sp <ICamera> c = mCamera;
346 if (c == 0) return;
347 mCamera->setPreviewCallbackFlag(flag);
348}
349
350// callback from camera service
351void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
352{
353 sp<CameraListener> listener;
354 {
355 Mutex::Autolock _l(mLock);
356 listener = mListener;
357 }
358 if (listener != NULL) {
359 listener->notify(msgType, ext1, ext2);
360 }
361}
362
363// callback from camera service when frame or image is ready
Wu-cheng Li57c86182011-07-30 05:00:37 +0800364void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
365 camera_frame_metadata_t *metadata)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800366{
367 sp<CameraListener> listener;
368 {
369 Mutex::Autolock _l(mLock);
370 listener = mListener;
371 }
372 if (listener != NULL) {
Wu-cheng Li57c86182011-07-30 05:00:37 +0800373 listener->postData(msgType, dataPtr, metadata);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800374 }
375}
376
377// callback from camera service when timestamped frame is ready
378void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
379{
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800380 // If recording proxy listener is registered, forward the frame and return.
381 // The other listener (mListener) is ignored because the receiver needs to
382 // call releaseRecordingFrame.
383 sp<ICameraRecordingProxyListener> proxylistener;
384 {
385 Mutex::Autolock _l(mLock);
386 proxylistener = mRecordingProxyListener;
387 }
388 if (proxylistener != NULL) {
389 proxylistener->dataCallbackTimestamp(timestamp, msgType, dataPtr);
390 return;
391 }
392
Mathias Agopian3cf61352010-02-09 17:46:37 -0800393 sp<CameraListener> listener;
394 {
395 Mutex::Autolock _l(mLock);
396 listener = mListener;
397 }
398 if (listener != NULL) {
399 listener->postDataTimestamp(timestamp, msgType, dataPtr);
James Dongc42478e2010-11-15 10:38:37 -0800400 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +0000401 ALOGW("No listener was set. Drop a recording frame.");
James Dongc42478e2010-11-15 10:38:37 -0800402 releaseRecordingFrame(dataPtr);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800403 }
404}
405
406void Camera::binderDied(const wp<IBinder>& who) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000407 ALOGW("ICamera died");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800408 notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, 0);
409}
410
411void Camera::DeathNotifier::binderDied(const wp<IBinder>& who) {
Steve Block3856b092011-10-20 11:56:00 +0100412 ALOGV("binderDied");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800413 Mutex::Autolock _l(Camera::mLock);
414 Camera::mCameraService.clear();
Steve Block5ff1dd52012-01-05 23:22:43 +0000415 ALOGW("Camera server died!");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800416}
417
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800418sp<ICameraRecordingProxy> Camera::getRecordingProxy() {
Steve Block3856b092011-10-20 11:56:00 +0100419 ALOGV("getProxy");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800420 return new RecordingProxy(this);
421}
422
423status_t Camera::RecordingProxy::startRecording(const sp<ICameraRecordingProxyListener>& listener)
424{
Steve Block3856b092011-10-20 11:56:00 +0100425 ALOGV("RecordingProxy::startRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800426 mCamera->setRecordingProxyListener(listener);
427 mCamera->reconnect();
428 return mCamera->startRecording();
429}
430
431void Camera::RecordingProxy::stopRecording()
432{
Steve Block3856b092011-10-20 11:56:00 +0100433 ALOGV("RecordingProxy::stopRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800434 mCamera->stopRecording();
435}
436
437void Camera::RecordingProxy::releaseRecordingFrame(const sp<IMemory>& mem)
438{
Steve Block3856b092011-10-20 11:56:00 +0100439 ALOGV("RecordingProxy::releaseRecordingFrame");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800440 mCamera->releaseRecordingFrame(mem);
441}
442
443Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera)
444{
445 mCamera = camera;
446}
447
Mathias Agopian3cf61352010-02-09 17:46:37 -0800448}; // namespace android