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