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 camera->asBinder()->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 c->mCamera->asBinder()->linkToDeath(c);
97 c->mStatus = NO_ERROR;
100 ALOGW("An error occurred while connecting to camera: %d", cameraId);
106 status_t Camera::reconnect()
109 sp <ICamera> c = mCamera;
110 if (c == 0) return NO_INIT;
111 return c->connect(this);
114 status_t Camera::lock()
116 sp <ICamera> c = mCamera;
117 if (c == 0) return NO_INIT;
121 status_t Camera::unlock()
123 sp <ICamera> c = mCamera;
124 if (c == 0) return NO_INIT;
128 // pass the buffered IGraphicBufferProducer to the camera service
129 status_t Camera::setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
131 ALOGV("setPreviewTarget(%p)", bufferProducer.get());
132 sp <ICamera> c = mCamera;
133 if (c == 0) return NO_INIT;
134 ALOGD_IF(bufferProducer == 0, "app passed NULL surface");
135 return c->setPreviewTarget(bufferProducer);
138 // start preview mode
139 status_t Camera::startPreview()
141 ALOGV("startPreview");
142 sp <ICamera> c = mCamera;
143 if (c == 0) return NO_INIT;
144 return c->startPreview();
147 status_t Camera::storeMetaDataInBuffers(bool enabled)
149 ALOGV("storeMetaDataInBuffers: %s",
150 enabled? "true": "false");
151 sp <ICamera> c = mCamera;
152 if (c == 0) return NO_INIT;
153 return c->storeMetaDataInBuffers(enabled);
156 // start recording mode, must call setPreviewTarget first
157 status_t Camera::startRecording()
159 ALOGV("startRecording");
160 sp <ICamera> c = mCamera;
161 if (c == 0) return NO_INIT;
162 return c->startRecording();
166 void Camera::stopPreview()
168 ALOGV("stopPreview");
169 sp <ICamera> c = mCamera;
174 // stop recording mode
175 void Camera::stopRecording()
177 ALOGV("stopRecording");
179 Mutex::Autolock _l(mLock);
180 mRecordingProxyListener.clear();
182 sp <ICamera> c = mCamera;
187 // release a recording frame
188 void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
190 ALOGV("releaseRecordingFrame");
191 sp <ICamera> c = mCamera;
193 c->releaseRecordingFrame(mem);
197 bool Camera::previewEnabled()
199 ALOGV("previewEnabled");
200 sp <ICamera> c = mCamera;
201 if (c == 0) return false;
202 return c->previewEnabled();
205 // get recording state
206 bool Camera::recordingEnabled()
208 ALOGV("recordingEnabled");
209 sp <ICamera> c = mCamera;
210 if (c == 0) return false;
211 return c->recordingEnabled();
214 status_t Camera::autoFocus()
217 sp <ICamera> c = mCamera;
218 if (c == 0) return NO_INIT;
219 return c->autoFocus();
222 status_t Camera::cancelAutoFocus()
224 ALOGV("cancelAutoFocus");
225 sp <ICamera> c = mCamera;
226 if (c == 0) return NO_INIT;
227 return c->cancelAutoFocus();
231 status_t Camera::takePicture(int msgType)
233 ALOGV("takePicture: 0x%x", msgType);
234 sp <ICamera> c = mCamera;
235 if (c == 0) return NO_INIT;
236 return c->takePicture(msgType);
239 // set preview/capture parameters - key/value pairs
240 status_t Camera::setParameters(const String8& params)
242 ALOGV("setParameters");
243 sp <ICamera> c = mCamera;
244 if (c == 0) return NO_INIT;
245 return c->setParameters(params);
248 // get preview/capture parameters - key/value pairs
249 String8 Camera::getParameters() const
251 ALOGV("getParameters");
253 sp <ICamera> c = mCamera;
254 if (c != 0) params = mCamera->getParameters();
258 // send command to camera driver
259 status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
261 ALOGV("sendCommand");
262 sp <ICamera> c = mCamera;
263 if (c == 0) return NO_INIT;
264 return c->sendCommand(cmd, arg1, arg2);
267 void Camera::setListener(const sp<CameraListener>& listener)
269 Mutex::Autolock _l(mLock);
270 mListener = listener;
273 void Camera::setRecordingProxyListener(const sp<ICameraRecordingProxyListener>& listener)
275 Mutex::Autolock _l(mLock);
276 mRecordingProxyListener = listener;
279 void Camera::setPreviewCallbackFlags(int flag)
281 ALOGV("setPreviewCallbackFlags");
282 sp <ICamera> c = mCamera;
284 mCamera->setPreviewCallbackFlag(flag);
287 status_t Camera::setPreviewCallbackTarget(
288 const sp<IGraphicBufferProducer>& callbackProducer)
290 sp <ICamera> c = mCamera;
291 if (c == 0) return NO_INIT;
292 return c->setPreviewCallbackTarget(callbackProducer);
295 // callback from camera service
296 void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
298 return CameraBaseT::notifyCallback(msgType, ext1, ext2);
301 // callback from camera service when frame or image is ready
302 void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
303 camera_frame_metadata_t *metadata)
305 sp<CameraListener> listener;
307 Mutex::Autolock _l(mLock);
308 listener = mListener;
310 if (listener != NULL) {
311 listener->postData(msgType, dataPtr, metadata);
315 // callback from camera service when timestamped frame is ready
316 void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
318 // If recording proxy listener is registered, forward the frame and return.
319 // The other listener (mListener) is ignored because the receiver needs to
320 // call releaseRecordingFrame.
321 sp<ICameraRecordingProxyListener> proxylistener;
323 Mutex::Autolock _l(mLock);
324 proxylistener = mRecordingProxyListener;
326 if (proxylistener != NULL) {
327 proxylistener->dataCallbackTimestamp(timestamp, msgType, dataPtr);
331 sp<CameraListener> listener;
333 Mutex::Autolock _l(mLock);
334 listener = mListener;
337 if (listener != NULL) {
338 listener->postDataTimestamp(timestamp, msgType, dataPtr);
340 ALOGW("No listener was set. Drop a recording frame.");
341 releaseRecordingFrame(dataPtr);
345 sp<ICameraRecordingProxy> Camera::getRecordingProxy() {
347 return new RecordingProxy(this);
350 status_t Camera::RecordingProxy::startRecording(const sp<ICameraRecordingProxyListener>& listener)
352 ALOGV("RecordingProxy::startRecording");
353 mCamera->setRecordingProxyListener(listener);
354 mCamera->reconnect();
355 return mCamera->startRecording();
358 void Camera::RecordingProxy::stopRecording()
360 ALOGV("RecordingProxy::stopRecording");
361 mCamera->stopRecording();
364 void Camera::RecordingProxy::releaseRecordingFrame(const sp<IMemory>& mem)
366 ALOGV("RecordingProxy::releaseRecordingFrame");
367 mCamera->releaseRecordingFrame(mem);
370 Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera)
375 }; // namespace android