xref: /aosp_15_r20/frameworks/av/camera/Camera.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2 **
3 ** Copyright (C) 2008, The Android Open Source Project
4 **
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>
22 #include <binder/IPCThreadState.h>
23 #include <binder/IServiceManager.h>
24 #include <binder/IMemory.h>
25 
26 #include <Camera.h>
27 #include <android/hardware/ICameraService.h>
28 #include <android/hardware/ICamera.h>
29 
30 #include <gui/IGraphicBufferProducer.h>
31 #include <gui/Surface.h>
32 
33 namespace android {
34 
Camera(int cameraId)35 Camera::Camera(int cameraId)
36     : CameraBase(cameraId)
37 {
38 }
39 
40 CameraTraits<Camera>::TCamConnectService CameraTraits<Camera>::fnConnectService =
41         &::android::hardware::ICameraService::connect;
42 
43 // construct a camera client from an existing camera remote
create(const sp<::android::hardware::ICamera> & camera)44 sp<Camera> Camera::create(const sp<::android::hardware::ICamera>& camera)
45 {
46      ALOGV("create");
47      if (camera == 0) {
48          ALOGE("camera remote is a NULL pointer");
49          return 0;
50      }
51 
52     sp<Camera> c = new Camera(-1);
53     if (camera->connect(c) == NO_ERROR) {
54         c->mStatus = NO_ERROR;
55         c->mCamera = camera;
56         IInterface::asBinder(camera)->linkToDeath(c);
57         return c;
58     }
59     return 0;
60 }
61 
~Camera()62 Camera::~Camera()
63 {
64     // We don't need to call disconnect() here because if the CameraService
65     // thinks we are the owner of the hardware, it will hold a (strong)
66     // reference to us, and we can't possibly be here. We also don't want to
67     // call disconnect() here if we are in the same process as mediaserver,
68     // because we may be invoked by CameraService::Client::connect() and will
69     // deadlock if we call any method of ICamera here.
70 }
71 
connect(int cameraId,int targetSdkVersion,int rotationOverride,bool forceSlowJpegMode,const AttributionSourceState & clientAttribution,int32_t devicePolicy)72 sp<Camera> Camera::connect(int cameraId, int targetSdkVersion, int rotationOverride,
73         bool forceSlowJpegMode, const AttributionSourceState& clientAttribution,
74         int32_t devicePolicy)
75 {
76     return CameraBaseT::connect(cameraId, targetSdkVersion, rotationOverride,
77             forceSlowJpegMode, clientAttribution, devicePolicy);
78 }
79 
reconnect()80 status_t Camera::reconnect()
81 {
82     ALOGV("reconnect");
83     sp <::android::hardware::ICamera> c = mCamera;
84     if (c == 0) return NO_INIT;
85     return c->connect(this);
86 }
87 
lock()88 status_t Camera::lock()
89 {
90     sp <::android::hardware::ICamera> c = mCamera;
91     if (c == 0) return NO_INIT;
92     return c->lock();
93 }
94 
unlock()95 status_t Camera::unlock()
96 {
97     sp <::android::hardware::ICamera> c = mCamera;
98     if (c == 0) return NO_INIT;
99     return c->unlock();
100 }
101 
102 // pass the Surface to the camera service
setPreviewTarget(const sp<SurfaceType> & target)103 status_t Camera::setPreviewTarget(const sp<SurfaceType>& target) {
104     ALOGV("setPreviewTarget(%p)", target.get());
105     sp<::android::hardware::ICamera> c = mCamera;
106     if (c == 0) return NO_INIT;
107     ALOGD_IF(target == 0, "app passed NULL surface");
108     return c->setPreviewTarget(target);
109 }
110 
setVideoTarget(const sp<SurfaceType> & target)111 status_t Camera::setVideoTarget(const sp<SurfaceType>& target) {
112     ALOGV("setVideoTarget(%p)", target.get());
113     sp<::android::hardware::ICamera> c = mCamera;
114     if (c == 0) return NO_INIT;
115     ALOGD_IF(target == 0, "app passed NULL video surface");
116     return c->setVideoTarget(target);
117 }
118 
119 // start preview mode
startPreview()120 status_t Camera::startPreview()
121 {
122     ALOGV("startPreview");
123     sp <::android::hardware::ICamera> c = mCamera;
124     if (c == 0) return NO_INIT;
125     return c->startPreview();
126 }
127 
setVideoBufferMode(int32_t videoBufferMode)128 status_t Camera::setVideoBufferMode(int32_t videoBufferMode)
129 {
130     ALOGV("setVideoBufferMode: %d", videoBufferMode);
131     sp <::android::hardware::ICamera> c = mCamera;
132     if (c == 0) return NO_INIT;
133     return c->setVideoBufferMode(videoBufferMode);
134 }
135 
136 // start recording mode, must call setPreviewTarget first
startRecording()137 status_t Camera::startRecording()
138 {
139     ALOGV("startRecording");
140     sp <::android::hardware::ICamera> c = mCamera;
141     if (c == 0) return NO_INIT;
142     return c->startRecording();
143 }
144 
145 // stop preview mode
stopPreview()146 void Camera::stopPreview()
147 {
148     ALOGV("stopPreview");
149     sp <::android::hardware::ICamera> c = mCamera;
150     if (c == 0) return;
151     c->stopPreview();
152 }
153 
154 // stop recording mode
stopRecording()155 void Camera::stopRecording()
156 {
157     ALOGV("stopRecording");
158     sp <::android::hardware::ICamera> c = mCamera;
159     if (c == 0) return;
160     c->stopRecording();
161 }
162 
163 // release a recording frame
releaseRecordingFrame(const sp<IMemory> & mem)164 void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
165 {
166     ALOGV("releaseRecordingFrame");
167     sp <::android::hardware::ICamera> c = mCamera;
168     if (c == 0) return;
169     c->releaseRecordingFrame(mem);
170 }
171 
releaseRecordingFrameHandle(native_handle_t * handle)172 void Camera::releaseRecordingFrameHandle(native_handle_t* handle)
173 {
174     ALOGV("releaseRecordingFrameHandle");
175     sp <::android::hardware::ICamera> c = mCamera;
176     if (c == 0) return;
177     c->releaseRecordingFrameHandle(handle);
178 }
179 
releaseRecordingFrameHandleBatch(const std::vector<native_handle_t * > handles)180 void Camera::releaseRecordingFrameHandleBatch(
181         const std::vector<native_handle_t*> handles) {
182     ALOGV("releaseRecordingFrameHandleBatch");
183     sp <::android::hardware::ICamera> c = mCamera;
184     if (c == 0) return;
185     c->releaseRecordingFrameHandleBatch(handles);
186 }
187 
188 // get preview state
previewEnabled()189 bool Camera::previewEnabled()
190 {
191     ALOGV("previewEnabled");
192     sp <::android::hardware::ICamera> c = mCamera;
193     if (c == 0) return false;
194     return c->previewEnabled();
195 }
196 
197 // get recording state
recordingEnabled()198 bool Camera::recordingEnabled()
199 {
200     ALOGV("recordingEnabled");
201     sp <::android::hardware::ICamera> c = mCamera;
202     if (c == 0) return false;
203     return c->recordingEnabled();
204 }
205 
autoFocus()206 status_t Camera::autoFocus()
207 {
208     ALOGV("autoFocus");
209     sp <::android::hardware::ICamera> c = mCamera;
210     if (c == 0) return NO_INIT;
211     return c->autoFocus();
212 }
213 
cancelAutoFocus()214 status_t Camera::cancelAutoFocus()
215 {
216     ALOGV("cancelAutoFocus");
217     sp <::android::hardware::ICamera> c = mCamera;
218     if (c == 0) return NO_INIT;
219     return c->cancelAutoFocus();
220 }
221 
222 // take a picture
takePicture(int msgType)223 status_t Camera::takePicture(int msgType)
224 {
225     ALOGV("takePicture: 0x%x", msgType);
226     sp <::android::hardware::ICamera> c = mCamera;
227     if (c == 0) return NO_INIT;
228     return c->takePicture(msgType);
229 }
230 
231 // set preview/capture parameters - key/value pairs
setParameters(const String8 & params)232 status_t Camera::setParameters(const String8& params)
233 {
234     ALOGV("setParameters");
235     sp <::android::hardware::ICamera> c = mCamera;
236     if (c == 0) return NO_INIT;
237     return c->setParameters(params);
238 }
239 
240 // get preview/capture parameters - key/value pairs
getParameters() const241 String8 Camera::getParameters() const
242 {
243     ALOGV("getParameters");
244     String8 params;
245     sp <::android::hardware::ICamera> c = mCamera;
246     if (c != 0) params = c->getParameters();
247     return params;
248 }
249 
250 // send command to camera driver
sendCommand(int32_t cmd,int32_t arg1,int32_t arg2)251 status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
252 {
253     ALOGV("sendCommand");
254     sp <::android::hardware::ICamera> c = mCamera;
255     if (c == 0) return NO_INIT;
256     return c->sendCommand(cmd, arg1, arg2);
257 }
258 
setListener(const sp<CameraListener> & listener)259 void Camera::setListener(const sp<CameraListener>& listener)
260 {
261     Mutex::Autolock _l(mLock);
262     mListener = listener;
263 }
264 
setPreviewCallbackFlags(int flag)265 void Camera::setPreviewCallbackFlags(int flag)
266 {
267     ALOGV("setPreviewCallbackFlags");
268     sp <::android::hardware::ICamera> c = mCamera;
269     if (c == 0) return;
270     c->setPreviewCallbackFlag(flag);
271 }
272 
setPreviewCallbackTarget(const sp<SurfaceType> & target)273 status_t Camera::setPreviewCallbackTarget(const sp<SurfaceType>& target) {
274     sp<::android::hardware::ICamera> c = mCamera;
275     if (c == 0) return NO_INIT;
276     return c->setPreviewCallbackTarget(target);
277 }
278 
setAudioRestriction(int32_t mode)279 status_t Camera::setAudioRestriction(int32_t mode)
280 {
281     sp <::android::hardware::ICamera> c = mCamera;
282     if (c == 0) return NO_INIT;
283     return c->setAudioRestriction(mode);
284 }
285 
getGlobalAudioRestriction()286 int32_t Camera::getGlobalAudioRestriction()
287 {
288     sp <::android::hardware::ICamera> c = mCamera;
289     if (c == 0) return NO_INIT;
290     return c->getGlobalAudioRestriction();
291 }
292 
293 // callback from camera service
notifyCallback(int32_t msgType,int32_t ext1,int32_t ext2)294 void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
295 {
296     return CameraBaseT::notifyCallback(msgType, ext1, ext2);
297 }
298 
299 // callback from camera service when frame or image is ready
dataCallback(int32_t msgType,const sp<IMemory> & dataPtr,camera_frame_metadata_t * metadata)300 void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
301                           camera_frame_metadata_t *metadata)
302 {
303     sp<CameraListener> listener;
304     {
305         Mutex::Autolock _l(mLock);
306         listener = mListener;
307     }
308     if (listener != NULL) {
309         listener->postData(msgType, dataPtr, metadata);
310     }
311 }
312 
313 // callback from camera service when timestamped frame is ready
dataCallbackTimestamp(nsecs_t timestamp,int32_t msgType,const sp<IMemory> & dataPtr)314 void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
315 {
316     sp<CameraListener> listener;
317     {
318         Mutex::Autolock _l(mLock);
319         listener = mListener;
320     }
321 
322     if (listener != NULL) {
323         listener->postDataTimestamp(timestamp, msgType, dataPtr);
324     } else {
325         ALOGW("No listener was set. Drop a recording frame.");
326         releaseRecordingFrame(dataPtr);
327     }
328 }
329 
recordingFrameHandleCallbackTimestamp(nsecs_t timestamp,native_handle_t * handle)330 void Camera::recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle)
331 {
332     sp<CameraListener> listener;
333     {
334         Mutex::Autolock _l(mLock);
335         listener = mListener;
336     }
337 
338     if (listener != NULL) {
339         listener->postRecordingFrameHandleTimestamp(timestamp, handle);
340     } else {
341         ALOGW("No listener was set. Drop a recording frame.");
342         releaseRecordingFrameHandle(handle);
343     }
344 }
345 
recordingFrameHandleCallbackTimestampBatch(const std::vector<nsecs_t> & timestamps,const std::vector<native_handle_t * > & handles)346 void Camera::recordingFrameHandleCallbackTimestampBatch(
347         const std::vector<nsecs_t>& timestamps,
348         const std::vector<native_handle_t*>& handles)
349 {
350     sp<CameraListener> listener;
351     {
352         Mutex::Autolock _l(mLock);
353         listener = mListener;
354     }
355 
356     if (listener != NULL) {
357         listener->postRecordingFrameHandleTimestampBatch(timestamps, handles);
358     } else {
359         ALOGW("No listener was set. Drop a batch of recording frames.");
360         releaseRecordingFrameHandleBatch(handles);
361     }
362 }
363 
getRecordingProxy()364 sp<ICameraRecordingProxy> Camera::getRecordingProxy() {
365     ALOGV("getProxy");
366     return new RecordingProxy(this);
367 }
368 
startRecording()369 status_t Camera::RecordingProxy::startRecording()
370 {
371     ALOGV("RecordingProxy::startRecording");
372     mCamera->reconnect();
373     return mCamera->startRecording();
374 }
375 
stopRecording()376 void Camera::RecordingProxy::stopRecording()
377 {
378     ALOGV("RecordingProxy::stopRecording");
379     mCamera->stopRecording();
380 }
381 
RecordingProxy(const sp<Camera> & camera)382 Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera)
383 {
384     mCamera = camera;
385 }
386 
387 }; // namespace android
388