blob: 9bf31342eea0528c0c6859140708f2a30b494428 [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
27#include <camera/Camera.h>
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080028#include <camera/ICameraRecordingProxyListener.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080029#include <camera/ICameraService.h>
Igor Murashkinc073ba52013-02-26 14:32:34 -080030#include <camera/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 =
43 &ICameraService::connect;
44
Mathias Agopian3cf61352010-02-09 17:46:37 -080045// construct a camera client from an existing camera remote
46sp<Camera> Camera::create(const sp<ICamera>& camera)
47{
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,
75 int clientUid)
Mathias Agopian3cf61352010-02-09 17:46:37 -080076{
Svetoslav Ganov280405a2015-05-12 02:19:27 +000077 return CameraBaseT::connect(cameraId, clientPackageName, clientUid);
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);
87 sp<ICameraClient> cl = c;
88 status_t status = NO_ERROR;
89 const sp<ICameraService>& cs = CameraBaseT::getCameraService();
90
91 if (cs != 0) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +000092 status = cs.get()->connectLegacy(cl, cameraId, halVersion, clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -070093 clientUid, /*out*/c->mCamera);
94 }
95 if (status == OK && c->mCamera != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -080096 IInterface::asBinder(c->mCamera)->linkToDeath(c);
Zhijun Heb10cdad2014-06-16 16:38:35 -070097 c->mStatus = NO_ERROR;
98 camera = c;
99 } else {
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -0700100 ALOGW("An error occurred while connecting to camera %d: %d (%s)",
101 cameraId, status, strerror(-status));
Zhijun Heb10cdad2014-06-16 16:38:35 -0700102 c.clear();
103 }
104 return status;
105}
106
Mathias Agopian3cf61352010-02-09 17:46:37 -0800107status_t Camera::reconnect()
108{
Steve Block3856b092011-10-20 11:56:00 +0100109 ALOGV("reconnect");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800110 sp <ICamera> c = mCamera;
111 if (c == 0) return NO_INIT;
112 return c->connect(this);
113}
114
Mathias Agopian3cf61352010-02-09 17:46:37 -0800115status_t Camera::lock()
116{
117 sp <ICamera> c = mCamera;
118 if (c == 0) return NO_INIT;
119 return c->lock();
120}
121
122status_t Camera::unlock()
123{
124 sp <ICamera> c = mCamera;
125 if (c == 0) return NO_INIT;
126 return c->unlock();
127}
128
Andy McFadden8ba01022012-12-18 09:46:54 -0800129// pass the buffered IGraphicBufferProducer to the camera service
Eino-Ville Talvala4b820b02013-08-21 14:39:05 -0700130status_t Camera::setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800131{
Eino-Ville Talvala4b820b02013-08-21 14:39:05 -0700132 ALOGV("setPreviewTarget(%p)", bufferProducer.get());
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800133 sp <ICamera> c = mCamera;
134 if (c == 0) return NO_INIT;
Mathias Agopian99617ad2013-03-12 18:42:23 -0700135 ALOGD_IF(bufferProducer == 0, "app passed NULL surface");
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700136 return c->setPreviewTarget(bufferProducer);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800137}
138
Mathias Agopian3cf61352010-02-09 17:46:37 -0800139// start preview mode
140status_t Camera::startPreview()
141{
Steve Block3856b092011-10-20 11:56:00 +0100142 ALOGV("startPreview");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800143 sp <ICamera> c = mCamera;
144 if (c == 0) return NO_INIT;
145 return c->startPreview();
146}
147
James Donge2ad6732010-10-18 20:42:51 -0700148status_t Camera::storeMetaDataInBuffers(bool enabled)
149{
Steve Block3856b092011-10-20 11:56:00 +0100150 ALOGV("storeMetaDataInBuffers: %s",
James Donge2ad6732010-10-18 20:42:51 -0700151 enabled? "true": "false");
152 sp <ICamera> c = mCamera;
153 if (c == 0) return NO_INIT;
154 return c->storeMetaDataInBuffers(enabled);
155}
156
Eino-Ville Talvala4b820b02013-08-21 14:39:05 -0700157// start recording mode, must call setPreviewTarget first
Mathias Agopian3cf61352010-02-09 17:46:37 -0800158status_t Camera::startRecording()
159{
Steve Block3856b092011-10-20 11:56:00 +0100160 ALOGV("startRecording");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800161 sp <ICamera> c = mCamera;
162 if (c == 0) return NO_INIT;
163 return c->startRecording();
164}
165
166// stop preview mode
167void Camera::stopPreview()
168{
Steve Block3856b092011-10-20 11:56:00 +0100169 ALOGV("stopPreview");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800170 sp <ICamera> c = mCamera;
171 if (c == 0) return;
172 c->stopPreview();
173}
174
175// stop recording mode
176void Camera::stopRecording()
177{
Steve Block3856b092011-10-20 11:56:00 +0100178 ALOGV("stopRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800179 {
180 Mutex::Autolock _l(mLock);
181 mRecordingProxyListener.clear();
182 }
Mathias Agopian3cf61352010-02-09 17:46:37 -0800183 sp <ICamera> c = mCamera;
184 if (c == 0) return;
185 c->stopRecording();
186}
187
188// release a recording frame
189void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
190{
Steve Block3856b092011-10-20 11:56:00 +0100191 ALOGV("releaseRecordingFrame");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800192 sp <ICamera> c = mCamera;
193 if (c == 0) return;
194 c->releaseRecordingFrame(mem);
195}
196
197// get preview state
198bool Camera::previewEnabled()
199{
Steve Block3856b092011-10-20 11:56:00 +0100200 ALOGV("previewEnabled");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800201 sp <ICamera> c = mCamera;
202 if (c == 0) return false;
203 return c->previewEnabled();
204}
205
206// get recording state
207bool Camera::recordingEnabled()
208{
Steve Block3856b092011-10-20 11:56:00 +0100209 ALOGV("recordingEnabled");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800210 sp <ICamera> c = mCamera;
211 if (c == 0) return false;
212 return c->recordingEnabled();
213}
214
215status_t Camera::autoFocus()
216{
Steve Block3856b092011-10-20 11:56:00 +0100217 ALOGV("autoFocus");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800218 sp <ICamera> c = mCamera;
219 if (c == 0) return NO_INIT;
220 return c->autoFocus();
221}
222
223status_t Camera::cancelAutoFocus()
224{
Steve Block3856b092011-10-20 11:56:00 +0100225 ALOGV("cancelAutoFocus");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800226 sp <ICamera> c = mCamera;
227 if (c == 0) return NO_INIT;
228 return c->cancelAutoFocus();
229}
230
231// take a picture
James Donge468ac52011-02-17 16:38:06 -0800232status_t Camera::takePicture(int msgType)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800233{
Steve Block3856b092011-10-20 11:56:00 +0100234 ALOGV("takePicture: 0x%x", msgType);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800235 sp <ICamera> c = mCamera;
236 if (c == 0) return NO_INIT;
James Donge468ac52011-02-17 16:38:06 -0800237 return c->takePicture(msgType);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800238}
239
240// set preview/capture parameters - key/value pairs
241status_t Camera::setParameters(const String8& params)
242{
Steve Block3856b092011-10-20 11:56:00 +0100243 ALOGV("setParameters");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800244 sp <ICamera> c = mCamera;
245 if (c == 0) return NO_INIT;
246 return c->setParameters(params);
247}
248
249// get preview/capture parameters - key/value pairs
250String8 Camera::getParameters() const
251{
Steve Block3856b092011-10-20 11:56:00 +0100252 ALOGV("getParameters");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800253 String8 params;
254 sp <ICamera> c = mCamera;
255 if (c != 0) params = mCamera->getParameters();
256 return params;
257}
258
259// send command to camera driver
260status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
261{
Steve Block3856b092011-10-20 11:56:00 +0100262 ALOGV("sendCommand");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800263 sp <ICamera> c = mCamera;
264 if (c == 0) return NO_INIT;
265 return c->sendCommand(cmd, arg1, arg2);
266}
267
268void Camera::setListener(const sp<CameraListener>& listener)
269{
270 Mutex::Autolock _l(mLock);
271 mListener = listener;
272}
273
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800274void Camera::setRecordingProxyListener(const sp<ICameraRecordingProxyListener>& listener)
275{
276 Mutex::Autolock _l(mLock);
277 mRecordingProxyListener = listener;
278}
279
Mathias Agopian3cf61352010-02-09 17:46:37 -0800280void Camera::setPreviewCallbackFlags(int flag)
281{
Steve Block3856b092011-10-20 11:56:00 +0100282 ALOGV("setPreviewCallbackFlags");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800283 sp <ICamera> c = mCamera;
284 if (c == 0) return;
285 mCamera->setPreviewCallbackFlag(flag);
286}
287
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700288status_t Camera::setPreviewCallbackTarget(
289 const sp<IGraphicBufferProducer>& callbackProducer)
290{
291 sp <ICamera> c = mCamera;
292 if (c == 0) return NO_INIT;
293 return c->setPreviewCallbackTarget(callbackProducer);
294}
295
Mathias Agopian3cf61352010-02-09 17:46:37 -0800296// callback from camera service
297void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
298{
Igor Murashkinc073ba52013-02-26 14:32:34 -0800299 return CameraBaseT::notifyCallback(msgType, ext1, ext2);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800300}
301
302// callback from camera service when frame or image is ready
Wu-cheng Li57c86182011-07-30 05:00:37 +0800303void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
304 camera_frame_metadata_t *metadata)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800305{
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -0800306 sp<CameraListener> listener;
307 {
308 Mutex::Autolock _l(mLock);
309 listener = mListener;
310 }
311 if (listener != NULL) {
312 listener->postData(msgType, dataPtr, metadata);
313 }
Mathias Agopian3cf61352010-02-09 17:46:37 -0800314}
315
316// callback from camera service when timestamped frame is ready
317void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
318{
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800319 // 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;
323 {
324 Mutex::Autolock _l(mLock);
325 proxylistener = mRecordingProxyListener;
326 }
327 if (proxylistener != NULL) {
328 proxylistener->dataCallbackTimestamp(timestamp, msgType, dataPtr);
329 return;
330 }
331
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -0800332 sp<CameraListener> listener;
333 {
334 Mutex::Autolock _l(mLock);
335 listener = mListener;
336 }
337
338 if (listener != NULL) {
339 listener->postDataTimestamp(timestamp, msgType, dataPtr);
340 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +0000341 ALOGW("No listener was set. Drop a recording frame.");
James Dongc42478e2010-11-15 10:38:37 -0800342 releaseRecordingFrame(dataPtr);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800343 }
344}
345
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800346sp<ICameraRecordingProxy> Camera::getRecordingProxy() {
Steve Block3856b092011-10-20 11:56:00 +0100347 ALOGV("getProxy");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800348 return new RecordingProxy(this);
349}
350
351status_t Camera::RecordingProxy::startRecording(const sp<ICameraRecordingProxyListener>& listener)
352{
Steve Block3856b092011-10-20 11:56:00 +0100353 ALOGV("RecordingProxy::startRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800354 mCamera->setRecordingProxyListener(listener);
355 mCamera->reconnect();
356 return mCamera->startRecording();
357}
358
359void Camera::RecordingProxy::stopRecording()
360{
Steve Block3856b092011-10-20 11:56:00 +0100361 ALOGV("RecordingProxy::stopRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800362 mCamera->stopRecording();
363}
364
365void Camera::RecordingProxy::releaseRecordingFrame(const sp<IMemory>& mem)
366{
Steve Block3856b092011-10-20 11:56:00 +0100367 ALOGV("RecordingProxy::releaseRecordingFrame");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800368 mCamera->releaseRecordingFrame(mem);
369}
370
371Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera)
372{
373 mCamera = camera;
374}
375
Mathias Agopian3cf61352010-02-09 17:46:37 -0800376}; // namespace android