blob: f7d194e63faa3a9feb262b8577a8fbeb3d584ed7 [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>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080028#include <android/hardware/ICameraService.h>
29#include <android/hardware/ICamera.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080030
Andy McFadden8ba01022012-12-18 09:46:54 -080031#include <gui/IGraphicBufferProducer.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080032#include <gui/Surface.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080033
34namespace android {
35
Igor Murashkinc073ba52013-02-26 14:32:34 -080036Camera::Camera(int cameraId)
37 : CameraBase(cameraId)
Mathias Agopian3cf61352010-02-09 17:46:37 -080038{
Mathias Agopian3cf61352010-02-09 17:46:37 -080039}
40
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070041CameraTraits<Camera>::TCamConnectService CameraTraits<Camera>::fnConnectService =
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080042 &::android::hardware::ICameraService::connect;
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070043
Mathias Agopian3cf61352010-02-09 17:46:37 -080044// construct a camera client from an existing camera remote
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080045sp<Camera> Camera::create(const sp<::android::hardware::ICamera>& camera)
Mathias Agopian3cf61352010-02-09 17:46:37 -080046{
Steve Block3856b092011-10-20 11:56:00 +010047 ALOGV("create");
Mathias Agopian3cf61352010-02-09 17:46:37 -080048 if (camera == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000049 ALOGE("camera remote is a NULL pointer");
Mathias Agopian3cf61352010-02-09 17:46:37 -080050 return 0;
51 }
52
Igor Murashkinc073ba52013-02-26 14:32:34 -080053 sp<Camera> c = new Camera(-1);
Mathias Agopian3cf61352010-02-09 17:46:37 -080054 if (camera->connect(c) == NO_ERROR) {
55 c->mStatus = NO_ERROR;
56 c->mCamera = camera;
Marco Nelissen06b46062014-11-14 07:58:25 -080057 IInterface::asBinder(camera)->linkToDeath(c);
Wu-cheng Li627baac2011-01-04 20:00:55 +080058 return c;
Mathias Agopian3cf61352010-02-09 17:46:37 -080059 }
Wu-cheng Li627baac2011-01-04 20:00:55 +080060 return 0;
Mathias Agopian3cf61352010-02-09 17:46:37 -080061}
62
Mathias Agopian3cf61352010-02-09 17:46:37 -080063Camera::~Camera()
64{
Chih-Chung Changd06618e2010-05-13 15:14:24 +080065 // We don't need to call disconnect() here because if the CameraService
66 // thinks we are the owner of the hardware, it will hold a (strong)
67 // reference to us, and we can't possibly be here. We also don't want to
68 // call disconnect() here if we are in the same process as mediaserver,
69 // because we may be invoked by CameraService::Client::connect() and will
70 // deadlock if we call any method of ICamera here.
Mathias Agopian3cf61352010-02-09 17:46:37 -080071}
72
Svetoslav Ganov280405a2015-05-12 02:19:27 +000073sp<Camera> Camera::connect(int cameraId, const String16& clientPackageName,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080074 int clientUid, int clientPid)
Mathias Agopian3cf61352010-02-09 17:46:37 -080075{
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080076 return CameraBaseT::connect(cameraId, clientPackageName, clientUid, clientPid);
Mathias Agopian3cf61352010-02-09 17:46:37 -080077}
78
79status_t Camera::reconnect()
80{
Steve Block3856b092011-10-20 11:56:00 +010081 ALOGV("reconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080082 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -080083 if (c == 0) return NO_INIT;
84 return c->connect(this);
85}
86
Mathias Agopian3cf61352010-02-09 17:46:37 -080087status_t Camera::lock()
88{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080089 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -080090 if (c == 0) return NO_INIT;
91 return c->lock();
92}
93
94status_t Camera::unlock()
95{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080096 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -080097 if (c == 0) return NO_INIT;
98 return c->unlock();
99}
100
Andy McFadden8ba01022012-12-18 09:46:54 -0800101// pass the buffered IGraphicBufferProducer to the camera service
Eino-Ville Talvala4b820b02013-08-21 14:39:05 -0700102status_t Camera::setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800103{
Eino-Ville Talvala4b820b02013-08-21 14:39:05 -0700104 ALOGV("setPreviewTarget(%p)", bufferProducer.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800105 sp <::android::hardware::ICamera> c = mCamera;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800106 if (c == 0) return NO_INIT;
Mathias Agopian99617ad2013-03-12 18:42:23 -0700107 ALOGD_IF(bufferProducer == 0, "app passed NULL surface");
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700108 return c->setPreviewTarget(bufferProducer);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800109}
110
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800111status_t Camera::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer)
112{
113 ALOGV("setVideoTarget(%p)", bufferProducer.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800114 sp <::android::hardware::ICamera> c = mCamera;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800115 if (c == 0) return NO_INIT;
116 ALOGD_IF(bufferProducer == 0, "app passed NULL video surface");
117 return c->setVideoTarget(bufferProducer);
118}
119
Mathias Agopian3cf61352010-02-09 17:46:37 -0800120// start preview mode
121status_t Camera::startPreview()
122{
Steve Block3856b092011-10-20 11:56:00 +0100123 ALOGV("startPreview");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800124 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800125 if (c == 0) return NO_INIT;
126 return c->startPreview();
127}
128
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800129status_t Camera::setVideoBufferMode(int32_t videoBufferMode)
James Donge2ad6732010-10-18 20:42:51 -0700130{
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800131 ALOGV("setVideoBufferMode: %d", videoBufferMode);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800132 sp <::android::hardware::ICamera> c = mCamera;
James Donge2ad6732010-10-18 20:42:51 -0700133 if (c == 0) return NO_INIT;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800134 return c->setVideoBufferMode(videoBufferMode);
James Donge2ad6732010-10-18 20:42:51 -0700135}
136
Eino-Ville Talvala4b820b02013-08-21 14:39:05 -0700137// start recording mode, must call setPreviewTarget first
Mathias Agopian3cf61352010-02-09 17:46:37 -0800138status_t Camera::startRecording()
139{
Steve Block3856b092011-10-20 11:56:00 +0100140 ALOGV("startRecording");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800141 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800142 if (c == 0) return NO_INIT;
143 return c->startRecording();
144}
145
146// stop preview mode
147void Camera::stopPreview()
148{
Steve Block3856b092011-10-20 11:56:00 +0100149 ALOGV("stopPreview");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800150 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800151 if (c == 0) return;
152 c->stopPreview();
153}
154
155// stop recording mode
156void Camera::stopRecording()
157{
Steve Block3856b092011-10-20 11:56:00 +0100158 ALOGV("stopRecording");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800159 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800160 if (c == 0) return;
161 c->stopRecording();
162}
163
164// release a recording frame
165void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
166{
Steve Block3856b092011-10-20 11:56:00 +0100167 ALOGV("releaseRecordingFrame");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800168 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800169 if (c == 0) return;
170 c->releaseRecordingFrame(mem);
171}
172
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700173void Camera::releaseRecordingFrameHandle(native_handle_t* handle)
174{
175 ALOGV("releaseRecordingFrameHandle");
176 sp <::android::hardware::ICamera> c = mCamera;
177 if (c == 0) return;
178 c->releaseRecordingFrameHandle(handle);
179}
180
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700181void Camera::releaseRecordingFrameHandleBatch(
182 const std::vector<native_handle_t*> handles) {
183 ALOGV("releaseRecordingFrameHandleBatch");
184 sp <::android::hardware::ICamera> c = mCamera;
185 if (c == 0) return;
186 c->releaseRecordingFrameHandleBatch(handles);
187}
188
Mathias Agopian3cf61352010-02-09 17:46:37 -0800189// get preview state
190bool Camera::previewEnabled()
191{
Steve Block3856b092011-10-20 11:56:00 +0100192 ALOGV("previewEnabled");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800193 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800194 if (c == 0) return false;
195 return c->previewEnabled();
196}
197
198// get recording state
199bool Camera::recordingEnabled()
200{
Steve Block3856b092011-10-20 11:56:00 +0100201 ALOGV("recordingEnabled");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800202 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800203 if (c == 0) return false;
204 return c->recordingEnabled();
205}
206
207status_t Camera::autoFocus()
208{
Steve Block3856b092011-10-20 11:56:00 +0100209 ALOGV("autoFocus");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800210 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800211 if (c == 0) return NO_INIT;
212 return c->autoFocus();
213}
214
215status_t Camera::cancelAutoFocus()
216{
Steve Block3856b092011-10-20 11:56:00 +0100217 ALOGV("cancelAutoFocus");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800218 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800219 if (c == 0) return NO_INIT;
220 return c->cancelAutoFocus();
221}
222
223// take a picture
James Donge468ac52011-02-17 16:38:06 -0800224status_t Camera::takePicture(int msgType)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800225{
Steve Block3856b092011-10-20 11:56:00 +0100226 ALOGV("takePicture: 0x%x", msgType);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800227 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800228 if (c == 0) return NO_INIT;
James Donge468ac52011-02-17 16:38:06 -0800229 return c->takePicture(msgType);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800230}
231
232// set preview/capture parameters - key/value pairs
233status_t Camera::setParameters(const String8& params)
234{
Steve Block3856b092011-10-20 11:56:00 +0100235 ALOGV("setParameters");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800236 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800237 if (c == 0) return NO_INIT;
238 return c->setParameters(params);
239}
240
241// get preview/capture parameters - key/value pairs
242String8 Camera::getParameters() const
243{
Steve Block3856b092011-10-20 11:56:00 +0100244 ALOGV("getParameters");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800245 String8 params;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800246 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800247 if (c != 0) params = mCamera->getParameters();
248 return params;
249}
250
251// send command to camera driver
252status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
253{
Steve Block3856b092011-10-20 11:56:00 +0100254 ALOGV("sendCommand");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800255 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800256 if (c == 0) return NO_INIT;
257 return c->sendCommand(cmd, arg1, arg2);
258}
259
260void Camera::setListener(const sp<CameraListener>& listener)
261{
262 Mutex::Autolock _l(mLock);
263 mListener = listener;
264}
265
266void Camera::setPreviewCallbackFlags(int flag)
267{
Steve Block3856b092011-10-20 11:56:00 +0100268 ALOGV("setPreviewCallbackFlags");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800269 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800270 if (c == 0) return;
271 mCamera->setPreviewCallbackFlag(flag);
272}
273
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700274status_t Camera::setPreviewCallbackTarget(
275 const sp<IGraphicBufferProducer>& callbackProducer)
276{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800277 sp <::android::hardware::ICamera> c = mCamera;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700278 if (c == 0) return NO_INIT;
279 return c->setPreviewCallbackTarget(callbackProducer);
280}
281
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -0700282status_t Camera::setAudioRestriction(int32_t mode)
Yin-Chia Yehdba03232019-08-19 15:54:28 -0700283{
284 sp <::android::hardware::ICamera> c = mCamera;
285 if (c == 0) return NO_INIT;
286 return c->setAudioRestriction(mode);
287}
288
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -0700289int32_t Camera::getGlobalAudioRestriction()
290{
291 sp <::android::hardware::ICamera> c = mCamera;
292 if (c == 0) return NO_INIT;
293 return c->getGlobalAudioRestriction();
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{
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -0800319 sp<CameraListener> listener;
320 {
321 Mutex::Autolock _l(mLock);
322 listener = mListener;
323 }
324
325 if (listener != NULL) {
326 listener->postDataTimestamp(timestamp, msgType, dataPtr);
327 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +0000328 ALOGW("No listener was set. Drop a recording frame.");
James Dongc42478e2010-11-15 10:38:37 -0800329 releaseRecordingFrame(dataPtr);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800330 }
331}
332
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700333void Camera::recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle)
334{
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700335 sp<CameraListener> listener;
336 {
337 Mutex::Autolock _l(mLock);
338 listener = mListener;
339 }
340
341 if (listener != NULL) {
342 listener->postRecordingFrameHandleTimestamp(timestamp, handle);
343 } else {
344 ALOGW("No listener was set. Drop a recording frame.");
345 releaseRecordingFrameHandle(handle);
346 }
347}
348
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700349void Camera::recordingFrameHandleCallbackTimestampBatch(
350 const std::vector<nsecs_t>& timestamps,
351 const std::vector<native_handle_t*>& handles)
352{
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700353 sp<CameraListener> listener;
354 {
355 Mutex::Autolock _l(mLock);
356 listener = mListener;
357 }
358
359 if (listener != NULL) {
360 listener->postRecordingFrameHandleTimestampBatch(timestamps, handles);
361 } else {
362 ALOGW("No listener was set. Drop a batch of recording frames.");
363 releaseRecordingFrameHandleBatch(handles);
364 }
365}
366
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800367sp<ICameraRecordingProxy> Camera::getRecordingProxy() {
Steve Block3856b092011-10-20 11:56:00 +0100368 ALOGV("getProxy");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800369 return new RecordingProxy(this);
370}
371
Eino-Ville Talvalab8ed8ef2020-06-22 16:59:48 -0700372status_t Camera::RecordingProxy::startRecording()
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800373{
Steve Block3856b092011-10-20 11:56:00 +0100374 ALOGV("RecordingProxy::startRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800375 mCamera->reconnect();
376 return mCamera->startRecording();
377}
378
379void Camera::RecordingProxy::stopRecording()
380{
Steve Block3856b092011-10-20 11:56:00 +0100381 ALOGV("RecordingProxy::stopRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800382 mCamera->stopRecording();
383}
384
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800385Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera)
386{
387 mCamera = camera;
388}
389
Mathias Agopian3cf61352010-02-09 17:46:37 -0800390}; // namespace android