blob: c52e581ed396cffc7b50e4cf20d8448b2bccf77b [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>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080022#include <utils/String16.h>
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080023#include <binder/IPCThreadState.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080024#include <binder/IServiceManager.h>
25#include <binder/IMemory.h>
26
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080027#include <Camera.h>
28#include <ICameraRecordingProxyListener.h>
29#include <android/hardware/ICameraService.h>
30#include <android/hardware/ICamera.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080031
Andy McFadden8ba01022012-12-18 09:46:54 -080032#include <gui/IGraphicBufferProducer.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080033#include <gui/Surface.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080034
35namespace android {
36
Igor Murashkinc073ba52013-02-26 14:32:34 -080037Camera::Camera(int cameraId)
38 : CameraBase(cameraId)
Mathias Agopian3cf61352010-02-09 17:46:37 -080039{
Mathias Agopian3cf61352010-02-09 17:46:37 -080040}
41
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070042CameraTraits<Camera>::TCamConnectService CameraTraits<Camera>::fnConnectService =
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080043 &::android::hardware::ICameraService::connect;
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070044
Mathias Agopian3cf61352010-02-09 17:46:37 -080045// construct a camera client from an existing camera remote
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046sp<Camera> Camera::create(const sp<::android::hardware::ICamera>& camera)
Mathias Agopian3cf61352010-02-09 17:46:37 -080047{
Steve Block3856b092011-10-20 11:56:00 +010048 ALOGV("create");
Mathias Agopian3cf61352010-02-09 17:46:37 -080049 if (camera == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000050 ALOGE("camera remote is a NULL pointer");
Mathias Agopian3cf61352010-02-09 17:46:37 -080051 return 0;
52 }
53
Igor Murashkinc073ba52013-02-26 14:32:34 -080054 sp<Camera> c = new Camera(-1);
Mathias Agopian3cf61352010-02-09 17:46:37 -080055 if (camera->connect(c) == NO_ERROR) {
56 c->mStatus = NO_ERROR;
57 c->mCamera = camera;
Marco Nelissen06b46062014-11-14 07:58:25 -080058 IInterface::asBinder(camera)->linkToDeath(c);
Wu-cheng Li627baac2011-01-04 20:00:55 +080059 return c;
Mathias Agopian3cf61352010-02-09 17:46:37 -080060 }
Wu-cheng Li627baac2011-01-04 20:00:55 +080061 return 0;
Mathias Agopian3cf61352010-02-09 17:46:37 -080062}
63
Mathias Agopian3cf61352010-02-09 17:46:37 -080064Camera::~Camera()
65{
Chih-Chung Changd06618e2010-05-13 15:14:24 +080066 // 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.
Mathias Agopian3cf61352010-02-09 17:46:37 -080072}
73
Svetoslav Ganov280405a2015-05-12 02:19:27 +000074sp<Camera> Camera::connect(int cameraId, const String16& clientPackageName,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080075 int clientUid, int clientPid)
Mathias Agopian3cf61352010-02-09 17:46:37 -080076{
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080077 return CameraBaseT::connect(cameraId, clientPackageName, clientUid, clientPid);
Mathias Agopian3cf61352010-02-09 17:46:37 -080078}
79
Zhijun Heb10cdad2014-06-16 16:38:35 -070080status_t Camera::connectLegacy(int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +000081 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -070082 int clientUid,
83 sp<Camera>& camera)
84{
85 ALOGV("%s: connect legacy camera device", __FUNCTION__);
86 sp<Camera> c = new Camera(cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080087 sp<::android::hardware::ICameraClient> cl = c;
Zhijun Heb10cdad2014-06-16 16:38:35 -070088 status_t status = NO_ERROR;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080089 const sp<::android::hardware::ICameraService>& cs = CameraBaseT::getCameraService();
Zhijun Heb10cdad2014-06-16 16:38:35 -070090
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080091 binder::Status ret;
92 if (cs != nullptr) {
93 ret = cs.get()->connectLegacy(cl, cameraId, halVersion, clientPackageName,
94 clientUid, /*out*/&(c->mCamera));
Zhijun Heb10cdad2014-06-16 16:38:35 -070095 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080096 if (ret.isOk() && c->mCamera != nullptr) {
Marco Nelissen06b46062014-11-14 07:58:25 -080097 IInterface::asBinder(c->mCamera)->linkToDeath(c);
Zhijun Heb10cdad2014-06-16 16:38:35 -070098 c->mStatus = NO_ERROR;
99 camera = c;
100 } else {
Eino-Ville Talvala3525dd92016-03-20 18:04:09 -0700101 switch(ret.serviceSpecificErrorCode()) {
102 case hardware::ICameraService::ERROR_DISCONNECTED:
103 status = -ENODEV;
104 break;
105 case hardware::ICameraService::ERROR_CAMERA_IN_USE:
106 status = -EBUSY;
107 break;
108 case hardware::ICameraService::ERROR_INVALID_OPERATION:
109 status = -EINVAL;
110 break;
111 case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE:
112 status = -EUSERS;
113 break;
114 case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
115 status = BAD_VALUE;
116 break;
117 case hardware::ICameraService::ERROR_DEPRECATED_HAL:
118 status = -EOPNOTSUPP;
119 break;
120 case hardware::ICameraService::ERROR_DISABLED:
121 status = -EACCES;
122 break;
123 case hardware::ICameraService::ERROR_PERMISSION_DENIED:
124 status = PERMISSION_DENIED;
125 break;
126 default:
127 status = -EINVAL;
128 ALOGW("An error occurred while connecting to camera %d: %s", cameraId,
129 (cs != nullptr) ? "Service not available" : ret.toString8().string());
130 break;
131 }
Zhijun Heb10cdad2014-06-16 16:38:35 -0700132 c.clear();
133 }
134 return status;
135}
136
Mathias Agopian3cf61352010-02-09 17:46:37 -0800137status_t Camera::reconnect()
138{
Steve Block3856b092011-10-20 11:56:00 +0100139 ALOGV("reconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800140 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800141 if (c == 0) return NO_INIT;
142 return c->connect(this);
143}
144
Mathias Agopian3cf61352010-02-09 17:46:37 -0800145status_t Camera::lock()
146{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800147 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800148 if (c == 0) return NO_INIT;
149 return c->lock();
150}
151
152status_t Camera::unlock()
153{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800154 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800155 if (c == 0) return NO_INIT;
156 return c->unlock();
157}
158
Andy McFadden8ba01022012-12-18 09:46:54 -0800159// pass the buffered IGraphicBufferProducer to the camera service
Eino-Ville Talvala4b820b02013-08-21 14:39:05 -0700160status_t Camera::setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800161{
Eino-Ville Talvala4b820b02013-08-21 14:39:05 -0700162 ALOGV("setPreviewTarget(%p)", bufferProducer.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800163 sp <::android::hardware::ICamera> c = mCamera;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800164 if (c == 0) return NO_INIT;
Mathias Agopian99617ad2013-03-12 18:42:23 -0700165 ALOGD_IF(bufferProducer == 0, "app passed NULL surface");
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700166 return c->setPreviewTarget(bufferProducer);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800167}
168
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800169status_t Camera::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer)
170{
171 ALOGV("setVideoTarget(%p)", bufferProducer.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800172 sp <::android::hardware::ICamera> c = mCamera;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800173 if (c == 0) return NO_INIT;
174 ALOGD_IF(bufferProducer == 0, "app passed NULL video surface");
175 return c->setVideoTarget(bufferProducer);
176}
177
Mathias Agopian3cf61352010-02-09 17:46:37 -0800178// start preview mode
179status_t Camera::startPreview()
180{
Steve Block3856b092011-10-20 11:56:00 +0100181 ALOGV("startPreview");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800182 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800183 if (c == 0) return NO_INIT;
184 return c->startPreview();
185}
186
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800187status_t Camera::setVideoBufferMode(int32_t videoBufferMode)
James Donge2ad6732010-10-18 20:42:51 -0700188{
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800189 ALOGV("setVideoBufferMode: %d", videoBufferMode);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800190 sp <::android::hardware::ICamera> c = mCamera;
James Donge2ad6732010-10-18 20:42:51 -0700191 if (c == 0) return NO_INIT;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800192 return c->setVideoBufferMode(videoBufferMode);
James Donge2ad6732010-10-18 20:42:51 -0700193}
194
Eino-Ville Talvala4b820b02013-08-21 14:39:05 -0700195// start recording mode, must call setPreviewTarget first
Mathias Agopian3cf61352010-02-09 17:46:37 -0800196status_t Camera::startRecording()
197{
Steve Block3856b092011-10-20 11:56:00 +0100198 ALOGV("startRecording");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800199 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800200 if (c == 0) return NO_INIT;
201 return c->startRecording();
202}
203
204// stop preview mode
205void Camera::stopPreview()
206{
Steve Block3856b092011-10-20 11:56:00 +0100207 ALOGV("stopPreview");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800208 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800209 if (c == 0) return;
210 c->stopPreview();
211}
212
213// stop recording mode
214void Camera::stopRecording()
215{
Steve Block3856b092011-10-20 11:56:00 +0100216 ALOGV("stopRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800217 {
218 Mutex::Autolock _l(mLock);
219 mRecordingProxyListener.clear();
220 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800221 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800222 if (c == 0) return;
223 c->stopRecording();
224}
225
226// release a recording frame
227void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
228{
Steve Block3856b092011-10-20 11:56:00 +0100229 ALOGV("releaseRecordingFrame");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800230 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800231 if (c == 0) return;
232 c->releaseRecordingFrame(mem);
233}
234
235// get preview state
236bool Camera::previewEnabled()
237{
Steve Block3856b092011-10-20 11:56:00 +0100238 ALOGV("previewEnabled");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800239 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800240 if (c == 0) return false;
241 return c->previewEnabled();
242}
243
244// get recording state
245bool Camera::recordingEnabled()
246{
Steve Block3856b092011-10-20 11:56:00 +0100247 ALOGV("recordingEnabled");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800248 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800249 if (c == 0) return false;
250 return c->recordingEnabled();
251}
252
253status_t Camera::autoFocus()
254{
Steve Block3856b092011-10-20 11:56:00 +0100255 ALOGV("autoFocus");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800256 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800257 if (c == 0) return NO_INIT;
258 return c->autoFocus();
259}
260
261status_t Camera::cancelAutoFocus()
262{
Steve Block3856b092011-10-20 11:56:00 +0100263 ALOGV("cancelAutoFocus");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800264 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800265 if (c == 0) return NO_INIT;
266 return c->cancelAutoFocus();
267}
268
269// take a picture
James Donge468ac52011-02-17 16:38:06 -0800270status_t Camera::takePicture(int msgType)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800271{
Steve Block3856b092011-10-20 11:56:00 +0100272 ALOGV("takePicture: 0x%x", msgType);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800273 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800274 if (c == 0) return NO_INIT;
James Donge468ac52011-02-17 16:38:06 -0800275 return c->takePicture(msgType);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800276}
277
278// set preview/capture parameters - key/value pairs
279status_t Camera::setParameters(const String8& params)
280{
Steve Block3856b092011-10-20 11:56:00 +0100281 ALOGV("setParameters");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800282 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800283 if (c == 0) return NO_INIT;
284 return c->setParameters(params);
285}
286
287// get preview/capture parameters - key/value pairs
288String8 Camera::getParameters() const
289{
Steve Block3856b092011-10-20 11:56:00 +0100290 ALOGV("getParameters");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800291 String8 params;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800292 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800293 if (c != 0) params = mCamera->getParameters();
294 return params;
295}
296
297// send command to camera driver
298status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
299{
Steve Block3856b092011-10-20 11:56:00 +0100300 ALOGV("sendCommand");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800301 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800302 if (c == 0) return NO_INIT;
303 return c->sendCommand(cmd, arg1, arg2);
304}
305
306void Camera::setListener(const sp<CameraListener>& listener)
307{
308 Mutex::Autolock _l(mLock);
309 mListener = listener;
310}
311
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800312void Camera::setRecordingProxyListener(const sp<ICameraRecordingProxyListener>& listener)
313{
314 Mutex::Autolock _l(mLock);
315 mRecordingProxyListener = listener;
316}
317
Mathias Agopian3cf61352010-02-09 17:46:37 -0800318void Camera::setPreviewCallbackFlags(int flag)
319{
Steve Block3856b092011-10-20 11:56:00 +0100320 ALOGV("setPreviewCallbackFlags");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800321 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800322 if (c == 0) return;
323 mCamera->setPreviewCallbackFlag(flag);
324}
325
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700326status_t Camera::setPreviewCallbackTarget(
327 const sp<IGraphicBufferProducer>& callbackProducer)
328{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800329 sp <::android::hardware::ICamera> c = mCamera;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700330 if (c == 0) return NO_INIT;
331 return c->setPreviewCallbackTarget(callbackProducer);
332}
333
Mathias Agopian3cf61352010-02-09 17:46:37 -0800334// callback from camera service
335void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
336{
Igor Murashkinc073ba52013-02-26 14:32:34 -0800337 return CameraBaseT::notifyCallback(msgType, ext1, ext2);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800338}
339
340// callback from camera service when frame or image is ready
Wu-cheng Li57c86182011-07-30 05:00:37 +0800341void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
342 camera_frame_metadata_t *metadata)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800343{
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -0800344 sp<CameraListener> listener;
345 {
346 Mutex::Autolock _l(mLock);
347 listener = mListener;
348 }
349 if (listener != NULL) {
350 listener->postData(msgType, dataPtr, metadata);
351 }
Mathias Agopian3cf61352010-02-09 17:46:37 -0800352}
353
354// callback from camera service when timestamped frame is ready
355void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
356{
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800357 // If recording proxy listener is registered, forward the frame and return.
358 // The other listener (mListener) is ignored because the receiver needs to
359 // call releaseRecordingFrame.
360 sp<ICameraRecordingProxyListener> proxylistener;
361 {
362 Mutex::Autolock _l(mLock);
363 proxylistener = mRecordingProxyListener;
364 }
365 if (proxylistener != NULL) {
366 proxylistener->dataCallbackTimestamp(timestamp, msgType, dataPtr);
367 return;
368 }
369
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -0800370 sp<CameraListener> listener;
371 {
372 Mutex::Autolock _l(mLock);
373 listener = mListener;
374 }
375
376 if (listener != NULL) {
377 listener->postDataTimestamp(timestamp, msgType, dataPtr);
378 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +0000379 ALOGW("No listener was set. Drop a recording frame.");
James Dongc42478e2010-11-15 10:38:37 -0800380 releaseRecordingFrame(dataPtr);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800381 }
382}
383
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800384sp<ICameraRecordingProxy> Camera::getRecordingProxy() {
Steve Block3856b092011-10-20 11:56:00 +0100385 ALOGV("getProxy");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800386 return new RecordingProxy(this);
387}
388
389status_t Camera::RecordingProxy::startRecording(const sp<ICameraRecordingProxyListener>& listener)
390{
Steve Block3856b092011-10-20 11:56:00 +0100391 ALOGV("RecordingProxy::startRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800392 mCamera->setRecordingProxyListener(listener);
393 mCamera->reconnect();
394 return mCamera->startRecording();
395}
396
397void Camera::RecordingProxy::stopRecording()
398{
Steve Block3856b092011-10-20 11:56:00 +0100399 ALOGV("RecordingProxy::stopRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800400 mCamera->stopRecording();
401}
402
403void Camera::RecordingProxy::releaseRecordingFrame(const sp<IMemory>& mem)
404{
Steve Block3856b092011-10-20 11:56:00 +0100405 ALOGV("RecordingProxy::releaseRecordingFrame");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800406 mCamera->releaseRecordingFrame(mem);
407}
408
409Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera)
410{
411 mCamera = camera;
412}
413
Mathias Agopian3cf61352010-02-09 17:46:37 -0800414}; // namespace android