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>
28 #include <ICameraRecordingProxyListener.h>
29 #include <android/hardware/ICameraService.h>
30 #include <android/hardware/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 &::android::hardware::ICameraService::connect;
45 // construct a camera client from an existing camera remote
46 sp<Camera> Camera::create(const sp<::android::hardware::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,
75 int clientUid, int clientPid)
77 return CameraBaseT::connect(cameraId, clientPackageName, clientUid, clientPid);
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<::android::hardware::ICameraClient> cl = c;
88 status_t status = NO_ERROR;
89 const sp<::android::hardware::ICameraService>& cs = CameraBaseT::getCameraService();
93 ret = cs.get()->connectLegacy(cl, cameraId, halVersion, clientPackageName,
94 clientUid, /*out*/&(c->mCamera));
96 if (ret.isOk() && c->mCamera != nullptr) {
97 IInterface::asBinder(c->mCamera)->linkToDeath(c);
98 c->mStatus = NO_ERROR;
101 switch(ret.serviceSpecificErrorCode()) {
102 case hardware::ICameraService::ERROR_DISCONNECTED:
105 case hardware::ICameraService::ERROR_CAMERA_IN_USE:
108 case hardware::ICameraService::ERROR_INVALID_OPERATION:
111 case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE:
114 case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
117 case hardware::ICameraService::ERROR_DEPRECATED_HAL:
118 status = -EOPNOTSUPP;
120 case hardware::ICameraService::ERROR_DISABLED:
123 case hardware::ICameraService::ERROR_PERMISSION_DENIED:
124 status = PERMISSION_DENIED;
128 ALOGW("An error occurred while connecting to camera %d: %s", cameraId,
129 (cs != nullptr) ? "Service not available" : ret.toString8().string());
137 status_t Camera::reconnect()
140 sp <::android::hardware::ICamera> c = mCamera;
141 if (c == 0) return NO_INIT;
142 return c->connect(this);
145 status_t Camera::lock()
147 sp <::android::hardware::ICamera> c = mCamera;
148 if (c == 0) return NO_INIT;
152 status_t Camera::unlock()
154 sp <::android::hardware::ICamera> c = mCamera;
155 if (c == 0) return NO_INIT;
159 // pass the buffered IGraphicBufferProducer to the camera service
160 status_t Camera::setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
162 ALOGV("setPreviewTarget(%p)", bufferProducer.get());
163 sp <::android::hardware::ICamera> c = mCamera;
164 if (c == 0) return NO_INIT;
165 ALOGD_IF(bufferProducer == 0, "app passed NULL surface");
166 return c->setPreviewTarget(bufferProducer);
169 status_t Camera::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer)
171 ALOGV("setVideoTarget(%p)", bufferProducer.get());
172 sp <::android::hardware::ICamera> c = mCamera;
173 if (c == 0) return NO_INIT;
174 ALOGD_IF(bufferProducer == 0, "app passed NULL video surface");
175 return c->setVideoTarget(bufferProducer);
178 // start preview mode
179 status_t Camera::startPreview()
181 ALOGV("startPreview");
182 sp <::android::hardware::ICamera> c = mCamera;
183 if (c == 0) return NO_INIT;
184 return c->startPreview();
187 status_t Camera::setVideoBufferMode(int32_t videoBufferMode)
189 ALOGV("setVideoBufferMode: %d", videoBufferMode);
190 sp <::android::hardware::ICamera> c = mCamera;
191 if (c == 0) return NO_INIT;
192 return c->setVideoBufferMode(videoBufferMode);
195 // start recording mode, must call setPreviewTarget first
196 status_t Camera::startRecording()
198 ALOGV("startRecording");
199 sp <::android::hardware::ICamera> c = mCamera;
200 if (c == 0) return NO_INIT;
201 return c->startRecording();
205 void Camera::stopPreview()
207 ALOGV("stopPreview");
208 sp <::android::hardware::ICamera> c = mCamera;
213 // stop recording mode
214 void Camera::stopRecording()
216 ALOGV("stopRecording");
218 Mutex::Autolock _l(mLock);
219 mRecordingProxyListener.clear();
221 sp <::android::hardware::ICamera> c = mCamera;
226 // release a recording frame
227 void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
229 ALOGV("releaseRecordingFrame");
230 sp <::android::hardware::ICamera> c = mCamera;
232 c->releaseRecordingFrame(mem);
235 void Camera::releaseRecordingFrameHandle(native_handle_t* handle)
237 ALOGV("releaseRecordingFrameHandle");
238 sp <::android::hardware::ICamera> c = mCamera;
240 c->releaseRecordingFrameHandle(handle);
244 bool Camera::previewEnabled()
246 ALOGV("previewEnabled");
247 sp <::android::hardware::ICamera> c = mCamera;
248 if (c == 0) return false;
249 return c->previewEnabled();
252 // get recording state
253 bool Camera::recordingEnabled()
255 ALOGV("recordingEnabled");
256 sp <::android::hardware::ICamera> c = mCamera;
257 if (c == 0) return false;
258 return c->recordingEnabled();
261 status_t Camera::autoFocus()
264 sp <::android::hardware::ICamera> c = mCamera;
265 if (c == 0) return NO_INIT;
266 return c->autoFocus();
269 status_t Camera::cancelAutoFocus()
271 ALOGV("cancelAutoFocus");
272 sp <::android::hardware::ICamera> c = mCamera;
273 if (c == 0) return NO_INIT;
274 return c->cancelAutoFocus();
278 status_t Camera::takePicture(int msgType)
280 ALOGV("takePicture: 0x%x", msgType);
281 sp <::android::hardware::ICamera> c = mCamera;
282 if (c == 0) return NO_INIT;
283 return c->takePicture(msgType);
286 // set preview/capture parameters - key/value pairs
287 status_t Camera::setParameters(const String8& params)
289 ALOGV("setParameters");
290 sp <::android::hardware::ICamera> c = mCamera;
291 if (c == 0) return NO_INIT;
292 return c->setParameters(params);
295 // get preview/capture parameters - key/value pairs
296 String8 Camera::getParameters() const
298 ALOGV("getParameters");
300 sp <::android::hardware::ICamera> c = mCamera;
301 if (c != 0) params = mCamera->getParameters();
305 // send command to camera driver
306 status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
308 ALOGV("sendCommand");
309 sp <::android::hardware::ICamera> c = mCamera;
310 if (c == 0) return NO_INIT;
311 return c->sendCommand(cmd, arg1, arg2);
314 void Camera::setListener(const sp<CameraListener>& listener)
316 Mutex::Autolock _l(mLock);
317 mListener = listener;
320 void Camera::setRecordingProxyListener(const sp<ICameraRecordingProxyListener>& listener)
322 Mutex::Autolock _l(mLock);
323 mRecordingProxyListener = listener;
326 void Camera::setPreviewCallbackFlags(int flag)
328 ALOGV("setPreviewCallbackFlags");
329 sp <::android::hardware::ICamera> c = mCamera;
331 mCamera->setPreviewCallbackFlag(flag);
334 status_t Camera::setPreviewCallbackTarget(
335 const sp<IGraphicBufferProducer>& callbackProducer)
337 sp <::android::hardware::ICamera> c = mCamera;
338 if (c == 0) return NO_INIT;
339 return c->setPreviewCallbackTarget(callbackProducer);
342 // callback from camera service
343 void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
345 return CameraBaseT::notifyCallback(msgType, ext1, ext2);
348 // callback from camera service when frame or image is ready
349 void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
350 camera_frame_metadata_t *metadata)
352 sp<CameraListener> listener;
354 Mutex::Autolock _l(mLock);
355 listener = mListener;
357 if (listener != NULL) {
358 listener->postData(msgType, dataPtr, metadata);
362 // callback from camera service when timestamped frame is ready
363 void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
365 // If recording proxy listener is registered, forward the frame and return.
366 // The other listener (mListener) is ignored because the receiver needs to
367 // call releaseRecordingFrame.
368 sp<ICameraRecordingProxyListener> proxylistener;
370 Mutex::Autolock _l(mLock);
371 proxylistener = mRecordingProxyListener;
373 if (proxylistener != NULL) {
374 proxylistener->dataCallbackTimestamp(timestamp, msgType, dataPtr);
378 sp<CameraListener> listener;
380 Mutex::Autolock _l(mLock);
381 listener = mListener;
384 if (listener != NULL) {
385 listener->postDataTimestamp(timestamp, msgType, dataPtr);
387 ALOGW("No listener was set. Drop a recording frame.");
388 releaseRecordingFrame(dataPtr);
392 void Camera::recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle)
394 // If recording proxy listener is registered, forward the frame and return.
395 // The other listener (mListener) is ignored because the receiver needs to
396 // call releaseRecordingFrameHandle.
397 sp<ICameraRecordingProxyListener> proxylistener;
399 Mutex::Autolock _l(mLock);
400 proxylistener = mRecordingProxyListener;
402 if (proxylistener != NULL) {
403 proxylistener->recordingFrameHandleCallbackTimestamp(timestamp, handle);
407 sp<CameraListener> listener;
409 Mutex::Autolock _l(mLock);
410 listener = mListener;
413 if (listener != NULL) {
414 listener->postRecordingFrameHandleTimestamp(timestamp, handle);
416 ALOGW("No listener was set. Drop a recording frame.");
417 releaseRecordingFrameHandle(handle);
421 sp<ICameraRecordingProxy> Camera::getRecordingProxy() {
423 return new RecordingProxy(this);
426 status_t Camera::RecordingProxy::startRecording(const sp<ICameraRecordingProxyListener>& listener)
428 ALOGV("RecordingProxy::startRecording");
429 mCamera->setRecordingProxyListener(listener);
430 mCamera->reconnect();
431 return mCamera->startRecording();
434 void Camera::RecordingProxy::stopRecording()
436 ALOGV("RecordingProxy::stopRecording");
437 mCamera->stopRecording();
440 void Camera::RecordingProxy::releaseRecordingFrame(const sp<IMemory>& mem)
442 ALOGV("RecordingProxy::releaseRecordingFrame");
443 mCamera->releaseRecordingFrame(mem);
446 void Camera::RecordingProxy::releaseRecordingFrameHandle(native_handle_t* handle) {
447 ALOGV("RecordingProxy::releaseRecordingFrameHandle");
448 mCamera->releaseRecordingFrameHandle(handle);
451 Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera)
456 }; // namespace android