3 ** Copyright (C) 2008, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 //#define LOG_NDEBUG 0
19 #define LOG_TAG "Camera"
20 #include <utils/Log.h>
21 #include <utils/threads.h>
22 #include <utils/String16.h>
23 #include <binder/IPCThreadState.h>
24 #include <binder/IServiceManager.h>
25 #include <binder/IMemory.h>
27 #include <camera/Camera.h>
28 #include <camera/ICameraRecordingProxyListener.h>
29 #include <camera/ICameraService.h>
30 #include <camera/ICamera.h>
32 #include <gui/IGraphicBufferProducer.h>
33 #include <gui/Surface.h>
37 Camera::Camera(int cameraId)
38 : CameraBase(cameraId)
42 CameraTraits<Camera>::TCamConnectService CameraTraits<Camera>::fnConnectService =
43 &ICameraService::connect;
45 // construct a camera client from an existing camera remote
46 sp<Camera> Camera::create(const sp<ICamera>& camera)
50 ALOGE("camera remote is a NULL pointer");
54 sp<Camera> c = new Camera(-1);
55 if (camera->connect(c) == NO_ERROR) {
56 c->mStatus = NO_ERROR;
58 IInterface::asBinder(camera)->linkToDeath(c);
66 // We don't need to call disconnect() here because if the CameraService
67 // thinks we are the owner of the hardware, it will hold a (strong)
68 // reference to us, and we can't possibly be here. We also don't want to
69 // call disconnect() here if we are in the same process as mediaserver,
70 // because we may be invoked by CameraService::Client::connect() and will
71 // deadlock if we call any method of ICamera here.
74 sp<Camera> Camera::connect(int cameraId, const String16& clientPackageName,
77 return CameraBaseT::connect(cameraId, clientPackageName, clientUid);
80 status_t Camera::connectLegacy(int cameraId, int halVersion,
81 const String16& clientPackageName,
85 ALOGV("%s: connect legacy camera device", __FUNCTION__);
86 sp<Camera> c = new Camera(cameraId);
87 sp<ICameraClient> cl = c;
88 status_t status = NO_ERROR;
89 const sp<ICameraService>& cs = CameraBaseT::getCameraService();
92 status = cs.get()->connectLegacy(cl, cameraId, halVersion, clientPackageName,
93 clientUid, /*out*/c->mCamera);
95 if (status == OK && c->mCamera != 0) {
96 IInterface::asBinder(c->mCamera)->linkToDeath(c);
97 c->mStatus = NO_ERROR;
100 ALOGW("An error occurred while connecting to camera %d: %d (%s)",
101 cameraId, status, strerror(-status));
107 status_t Camera::reconnect()
110 sp <ICamera> c = mCamera;
111 if (c == 0) return NO_INIT;
112 return c->connect(this);
115 status_t Camera::lock()
117 sp <ICamera> c = mCamera;
118 if (c == 0) return NO_INIT;
122 status_t Camera::unlock()
124 sp <ICamera> c = mCamera;
125 if (c == 0) return NO_INIT;
129 // pass the buffered IGraphicBufferProducer to the camera service
130 status_t Camera::setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
132 ALOGV("setPreviewTarget(%p)", bufferProducer.get());
133 sp <ICamera> c = mCamera;
134 if (c == 0) return NO_INIT;
135 ALOGD_IF(bufferProducer == 0, "app passed NULL surface");
136 return c->setPreviewTarget(bufferProducer);
139 // start preview mode
140 status_t Camera::startPreview()
142 ALOGV("startPreview");
143 sp <ICamera> c = mCamera;
144 if (c == 0) return NO_INIT;
145 return c->startPreview();
148 status_t Camera::storeMetaDataInBuffers(bool enabled)
150 ALOGV("storeMetaDataInBuffers: %s",
151 enabled? "true": "false");
152 sp <ICamera> c = mCamera;
153 if (c == 0) return NO_INIT;
154 return c->storeMetaDataInBuffers(enabled);
157 // start recording mode, must call setPreviewTarget first
158 status_t Camera::startRecording()
160 ALOGV("startRecording");
161 sp <ICamera> c = mCamera;
162 if (c == 0) return NO_INIT;
163 return c->startRecording();
167 void Camera::stopPreview()
169 ALOGV("stopPreview");
170 sp <ICamera> c = mCamera;
175 // stop recording mode
176 void Camera::stopRecording()
178 ALOGV("stopRecording");
180 Mutex::Autolock _l(mLock);
181 mRecordingProxyListener.clear();
183 sp <ICamera> c = mCamera;
188 // release a recording frame
189 void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
191 ALOGV("releaseRecordingFrame");
192 sp <ICamera> c = mCamera;
194 c->releaseRecordingFrame(mem);
198 bool Camera::previewEnabled()
200 ALOGV("previewEnabled");
201 sp <ICamera> c = mCamera;
202 if (c == 0) return false;
203 return c->previewEnabled();
206 // get recording state
207 bool Camera::recordingEnabled()
209 ALOGV("recordingEnabled");
210 sp <ICamera> c = mCamera;
211 if (c == 0) return false;
212 return c->recordingEnabled();
215 status_t Camera::autoFocus()
218 sp <ICamera> c = mCamera;
219 if (c == 0) return NO_INIT;
220 return c->autoFocus();
223 status_t Camera::cancelAutoFocus()
225 ALOGV("cancelAutoFocus");
226 sp <ICamera> c = mCamera;
227 if (c == 0) return NO_INIT;
228 return c->cancelAutoFocus();
232 status_t Camera::takePicture(int msgType)
234 ALOGV("takePicture: 0x%x", msgType);
235 sp <ICamera> c = mCamera;
236 if (c == 0) return NO_INIT;
237 return c->takePicture(msgType);
240 // set preview/capture parameters - key/value pairs
241 status_t Camera::setParameters(const String8& params)
243 ALOGV("setParameters");
244 sp <ICamera> c = mCamera;
245 if (c == 0) return NO_INIT;
246 return c->setParameters(params);
249 // get preview/capture parameters - key/value pairs
250 String8 Camera::getParameters() const
252 ALOGV("getParameters");
254 sp <ICamera> c = mCamera;
255 if (c != 0) params = mCamera->getParameters();
259 // send command to camera driver
260 status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
262 ALOGV("sendCommand");
263 sp <ICamera> c = mCamera;
264 if (c == 0) return NO_INIT;
265 return c->sendCommand(cmd, arg1, arg2);
268 void Camera::setListener(const sp<CameraListener>& listener)
270 Mutex::Autolock _l(mLock);
271 mListener = listener;
274 void Camera::setRecordingProxyListener(const sp<ICameraRecordingProxyListener>& listener)
276 Mutex::Autolock _l(mLock);
277 mRecordingProxyListener = listener;
280 void Camera::setPreviewCallbackFlags(int flag)
282 ALOGV("setPreviewCallbackFlags");
283 sp <ICamera> c = mCamera;
285 mCamera->setPreviewCallbackFlag(flag);
288 status_t Camera::setPreviewCallbackTarget(
289 const sp<IGraphicBufferProducer>& callbackProducer)
291 sp <ICamera> c = mCamera;
292 if (c == 0) return NO_INIT;
293 return c->setPreviewCallbackTarget(callbackProducer);
296 // callback from camera service
297 void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
299 return CameraBaseT::notifyCallback(msgType, ext1, ext2);
302 // callback from camera service when frame or image is ready
303 void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
304 camera_frame_metadata_t *metadata)
306 sp<CameraListener> listener;
308 Mutex::Autolock _l(mLock);
309 listener = mListener;
311 if (listener != NULL) {
312 listener->postData(msgType, dataPtr, metadata);
316 // callback from camera service when timestamped frame is ready
317 void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
319 // If recording proxy listener is registered, forward the frame and return.
320 // The other listener (mListener) is ignored because the receiver needs to
321 // call releaseRecordingFrame.
322 sp<ICameraRecordingProxyListener> proxylistener;
324 Mutex::Autolock _l(mLock);
325 proxylistener = mRecordingProxyListener;
327 if (proxylistener != NULL) {
328 proxylistener->dataCallbackTimestamp(timestamp, msgType, dataPtr);
332 sp<CameraListener> listener;
334 Mutex::Autolock _l(mLock);
335 listener = mListener;
338 if (listener != NULL) {
339 listener->postDataTimestamp(timestamp, msgType, dataPtr);
341 ALOGW("No listener was set. Drop a recording frame.");
342 releaseRecordingFrame(dataPtr);
346 sp<ICameraRecordingProxy> Camera::getRecordingProxy() {
348 return new RecordingProxy(this);
351 status_t Camera::RecordingProxy::startRecording(const sp<ICameraRecordingProxyListener>& listener)
353 ALOGV("RecordingProxy::startRecording");
354 mCamera->setRecordingProxyListener(listener);
355 mCamera->reconnect();
356 return mCamera->startRecording();
359 void Camera::RecordingProxy::stopRecording()
361 ALOGV("RecordingProxy::stopRecording");
362 mCamera->stopRecording();
365 void Camera::RecordingProxy::releaseRecordingFrame(const sp<IMemory>& mem)
367 ALOGV("RecordingProxy::releaseRecordingFrame");
368 mCamera->releaseRecordingFrame(mem);
371 Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera)
376 }; // namespace android