blob: 604dbb8b17d456349afb634b019b319abb0f7529 [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,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070074 int clientUid, int clientPid, int targetSdkVersion)
Mathias Agopian3cf61352010-02-09 17:46:37 -080075{
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070076 return CameraBaseT::connect(cameraId, clientPackageName, clientUid,
77 clientPid, targetSdkVersion);
Mathias Agopian3cf61352010-02-09 17:46:37 -080078}
79
80status_t Camera::reconnect()
81{
Steve Block3856b092011-10-20 11:56:00 +010082 ALOGV("reconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080083 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -080084 if (c == 0) return NO_INIT;
85 return c->connect(this);
86}
87
Mathias Agopian3cf61352010-02-09 17:46:37 -080088status_t Camera::lock()
89{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080090 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -080091 if (c == 0) return NO_INIT;
92 return c->lock();
93}
94
95status_t Camera::unlock()
96{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080097 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -080098 if (c == 0) return NO_INIT;
99 return c->unlock();
100}
101
Andy McFadden8ba01022012-12-18 09:46:54 -0800102// pass the buffered IGraphicBufferProducer to the camera service
Eino-Ville Talvala4b820b02013-08-21 14:39:05 -0700103status_t Camera::setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800104{
Eino-Ville Talvala4b820b02013-08-21 14:39:05 -0700105 ALOGV("setPreviewTarget(%p)", bufferProducer.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800106 sp <::android::hardware::ICamera> c = mCamera;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800107 if (c == 0) return NO_INIT;
Mathias Agopian99617ad2013-03-12 18:42:23 -0700108 ALOGD_IF(bufferProducer == 0, "app passed NULL surface");
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700109 return c->setPreviewTarget(bufferProducer);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800110}
111
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800112status_t Camera::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer)
113{
114 ALOGV("setVideoTarget(%p)", bufferProducer.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800115 sp <::android::hardware::ICamera> c = mCamera;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800116 if (c == 0) return NO_INIT;
117 ALOGD_IF(bufferProducer == 0, "app passed NULL video surface");
118 return c->setVideoTarget(bufferProducer);
119}
120
Mathias Agopian3cf61352010-02-09 17:46:37 -0800121// start preview mode
122status_t Camera::startPreview()
123{
Steve Block3856b092011-10-20 11:56:00 +0100124 ALOGV("startPreview");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800125 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800126 if (c == 0) return NO_INIT;
127 return c->startPreview();
128}
129
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800130status_t Camera::setVideoBufferMode(int32_t videoBufferMode)
James Donge2ad6732010-10-18 20:42:51 -0700131{
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800132 ALOGV("setVideoBufferMode: %d", videoBufferMode);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800133 sp <::android::hardware::ICamera> c = mCamera;
James Donge2ad6732010-10-18 20:42:51 -0700134 if (c == 0) return NO_INIT;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800135 return c->setVideoBufferMode(videoBufferMode);
James Donge2ad6732010-10-18 20:42:51 -0700136}
137
Eino-Ville Talvala4b820b02013-08-21 14:39:05 -0700138// start recording mode, must call setPreviewTarget first
Mathias Agopian3cf61352010-02-09 17:46:37 -0800139status_t Camera::startRecording()
140{
Steve Block3856b092011-10-20 11:56:00 +0100141 ALOGV("startRecording");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800142 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800143 if (c == 0) return NO_INIT;
144 return c->startRecording();
145}
146
147// stop preview mode
148void Camera::stopPreview()
149{
Steve Block3856b092011-10-20 11:56:00 +0100150 ALOGV("stopPreview");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800151 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800152 if (c == 0) return;
153 c->stopPreview();
154}
155
156// stop recording mode
157void Camera::stopRecording()
158{
Steve Block3856b092011-10-20 11:56:00 +0100159 ALOGV("stopRecording");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800160 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800161 if (c == 0) return;
162 c->stopRecording();
163}
164
165// release a recording frame
166void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
167{
Steve Block3856b092011-10-20 11:56:00 +0100168 ALOGV("releaseRecordingFrame");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800169 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800170 if (c == 0) return;
171 c->releaseRecordingFrame(mem);
172}
173
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700174void Camera::releaseRecordingFrameHandle(native_handle_t* handle)
175{
176 ALOGV("releaseRecordingFrameHandle");
177 sp <::android::hardware::ICamera> c = mCamera;
178 if (c == 0) return;
179 c->releaseRecordingFrameHandle(handle);
180}
181
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700182void Camera::releaseRecordingFrameHandleBatch(
183 const std::vector<native_handle_t*> handles) {
184 ALOGV("releaseRecordingFrameHandleBatch");
185 sp <::android::hardware::ICamera> c = mCamera;
186 if (c == 0) return;
187 c->releaseRecordingFrameHandleBatch(handles);
188}
189
Mathias Agopian3cf61352010-02-09 17:46:37 -0800190// get preview state
191bool Camera::previewEnabled()
192{
Steve Block3856b092011-10-20 11:56:00 +0100193 ALOGV("previewEnabled");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800194 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800195 if (c == 0) return false;
196 return c->previewEnabled();
197}
198
199// get recording state
200bool Camera::recordingEnabled()
201{
Steve Block3856b092011-10-20 11:56:00 +0100202 ALOGV("recordingEnabled");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800203 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800204 if (c == 0) return false;
205 return c->recordingEnabled();
206}
207
208status_t Camera::autoFocus()
209{
Steve Block3856b092011-10-20 11:56:00 +0100210 ALOGV("autoFocus");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800211 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800212 if (c == 0) return NO_INIT;
213 return c->autoFocus();
214}
215
216status_t Camera::cancelAutoFocus()
217{
Steve Block3856b092011-10-20 11:56:00 +0100218 ALOGV("cancelAutoFocus");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800219 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800220 if (c == 0) return NO_INIT;
221 return c->cancelAutoFocus();
222}
223
224// take a picture
James Donge468ac52011-02-17 16:38:06 -0800225status_t Camera::takePicture(int msgType)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800226{
Steve Block3856b092011-10-20 11:56:00 +0100227 ALOGV("takePicture: 0x%x", msgType);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800228 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800229 if (c == 0) return NO_INIT;
James Donge468ac52011-02-17 16:38:06 -0800230 return c->takePicture(msgType);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800231}
232
233// set preview/capture parameters - key/value pairs
234status_t Camera::setParameters(const String8& params)
235{
Steve Block3856b092011-10-20 11:56:00 +0100236 ALOGV("setParameters");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800237 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800238 if (c == 0) return NO_INIT;
239 return c->setParameters(params);
240}
241
242// get preview/capture parameters - key/value pairs
243String8 Camera::getParameters() const
244{
Steve Block3856b092011-10-20 11:56:00 +0100245 ALOGV("getParameters");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800246 String8 params;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800247 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800248 if (c != 0) params = mCamera->getParameters();
249 return params;
250}
251
252// send command to camera driver
253status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
254{
Steve Block3856b092011-10-20 11:56:00 +0100255 ALOGV("sendCommand");
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->sendCommand(cmd, arg1, arg2);
259}
260
261void Camera::setListener(const sp<CameraListener>& listener)
262{
263 Mutex::Autolock _l(mLock);
264 mListener = listener;
265}
266
267void Camera::setPreviewCallbackFlags(int flag)
268{
Steve Block3856b092011-10-20 11:56:00 +0100269 ALOGV("setPreviewCallbackFlags");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800270 sp <::android::hardware::ICamera> c = mCamera;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800271 if (c == 0) return;
272 mCamera->setPreviewCallbackFlag(flag);
273}
274
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700275status_t Camera::setPreviewCallbackTarget(
276 const sp<IGraphicBufferProducer>& callbackProducer)
277{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800278 sp <::android::hardware::ICamera> c = mCamera;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700279 if (c == 0) return NO_INIT;
280 return c->setPreviewCallbackTarget(callbackProducer);
281}
282
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -0700283status_t Camera::setAudioRestriction(int32_t mode)
Yin-Chia Yehdba03232019-08-19 15:54:28 -0700284{
285 sp <::android::hardware::ICamera> c = mCamera;
286 if (c == 0) return NO_INIT;
287 return c->setAudioRestriction(mode);
288}
289
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -0700290int32_t Camera::getGlobalAudioRestriction()
291{
292 sp <::android::hardware::ICamera> c = mCamera;
293 if (c == 0) return NO_INIT;
294 return c->getGlobalAudioRestriction();
295}
296
Mathias Agopian3cf61352010-02-09 17:46:37 -0800297// callback from camera service
298void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
299{
Igor Murashkinc073ba52013-02-26 14:32:34 -0800300 return CameraBaseT::notifyCallback(msgType, ext1, ext2);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800301}
302
303// callback from camera service when frame or image is ready
Wu-cheng Li57c86182011-07-30 05:00:37 +0800304void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
305 camera_frame_metadata_t *metadata)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800306{
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -0800307 sp<CameraListener> listener;
308 {
309 Mutex::Autolock _l(mLock);
310 listener = mListener;
311 }
312 if (listener != NULL) {
313 listener->postData(msgType, dataPtr, metadata);
314 }
Mathias Agopian3cf61352010-02-09 17:46:37 -0800315}
316
317// callback from camera service when timestamped frame is ready
318void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
319{
Igor Murashkinfa4cf9d2013-03-04 16:14:23 -0800320 sp<CameraListener> listener;
321 {
322 Mutex::Autolock _l(mLock);
323 listener = mListener;
324 }
325
326 if (listener != NULL) {
327 listener->postDataTimestamp(timestamp, msgType, dataPtr);
328 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +0000329 ALOGW("No listener was set. Drop a recording frame.");
James Dongc42478e2010-11-15 10:38:37 -0800330 releaseRecordingFrame(dataPtr);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800331 }
332}
333
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700334void Camera::recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle)
335{
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700336 sp<CameraListener> listener;
337 {
338 Mutex::Autolock _l(mLock);
339 listener = mListener;
340 }
341
342 if (listener != NULL) {
343 listener->postRecordingFrameHandleTimestamp(timestamp, handle);
344 } else {
345 ALOGW("No listener was set. Drop a recording frame.");
346 releaseRecordingFrameHandle(handle);
347 }
348}
349
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700350void Camera::recordingFrameHandleCallbackTimestampBatch(
351 const std::vector<nsecs_t>& timestamps,
352 const std::vector<native_handle_t*>& handles)
353{
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700354 sp<CameraListener> listener;
355 {
356 Mutex::Autolock _l(mLock);
357 listener = mListener;
358 }
359
360 if (listener != NULL) {
361 listener->postRecordingFrameHandleTimestampBatch(timestamps, handles);
362 } else {
363 ALOGW("No listener was set. Drop a batch of recording frames.");
364 releaseRecordingFrameHandleBatch(handles);
365 }
366}
367
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800368sp<ICameraRecordingProxy> Camera::getRecordingProxy() {
Steve Block3856b092011-10-20 11:56:00 +0100369 ALOGV("getProxy");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800370 return new RecordingProxy(this);
371}
372
Eino-Ville Talvalab8ed8ef2020-06-22 16:59:48 -0700373status_t Camera::RecordingProxy::startRecording()
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800374{
Steve Block3856b092011-10-20 11:56:00 +0100375 ALOGV("RecordingProxy::startRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800376 mCamera->reconnect();
377 return mCamera->startRecording();
378}
379
380void Camera::RecordingProxy::stopRecording()
381{
Steve Block3856b092011-10-20 11:56:00 +0100382 ALOGV("RecordingProxy::stopRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800383 mCamera->stopRecording();
384}
385
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800386Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera)
387{
388 mCamera = camera;
389}
390
Mathias Agopian3cf61352010-02-09 17:46:37 -0800391}; // namespace android