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