• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)73 sp<Camera> Camera::connect(int cameraId, const String16& clientPackageName,
74         int clientUid, int clientPid, int targetSdkVersion)
75 {
76     return CameraBaseT::connect(cameraId, clientPackageName, clientUid,
77             clientPid, targetSdkVersion);
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 buffered IGraphicBufferProducer to the camera service
setPreviewTarget(const sp<IGraphicBufferProducer> & bufferProducer)103 status_t Camera::setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
104 {
105     ALOGV("setPreviewTarget(%p)", bufferProducer.get());
106     sp <::android::hardware::ICamera> c = mCamera;
107     if (c == 0) return NO_INIT;
108     ALOGD_IF(bufferProducer == 0, "app passed NULL surface");
109     return c->setPreviewTarget(bufferProducer);
110 }
111 
setVideoTarget(const sp<IGraphicBufferProducer> & bufferProducer)112 status_t Camera::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer)
113 {
114     ALOGV("setVideoTarget(%p)", bufferProducer.get());
115     sp <::android::hardware::ICamera> c = mCamera;
116     if (c == 0) return NO_INIT;
117     ALOGD_IF(bufferProducer == 0, "app passed NULL video surface");
118     return c->setVideoTarget(bufferProducer);
119 }
120 
121 // start preview mode
startPreview()122 status_t Camera::startPreview()
123 {
124     ALOGV("startPreview");
125     sp <::android::hardware::ICamera> c = mCamera;
126     if (c == 0) return NO_INIT;
127     return c->startPreview();
128 }
129 
setVideoBufferMode(int32_t videoBufferMode)130 status_t Camera::setVideoBufferMode(int32_t videoBufferMode)
131 {
132     ALOGV("setVideoBufferMode: %d", videoBufferMode);
133     sp <::android::hardware::ICamera> c = mCamera;
134     if (c == 0) return NO_INIT;
135     return c->setVideoBufferMode(videoBufferMode);
136 }
137 
138 // start recording mode, must call setPreviewTarget first
startRecording()139 status_t Camera::startRecording()
140 {
141     ALOGV("startRecording");
142     sp <::android::hardware::ICamera> c = mCamera;
143     if (c == 0) return NO_INIT;
144     return c->startRecording();
145 }
146 
147 // stop preview mode
stopPreview()148 void Camera::stopPreview()
149 {
150     ALOGV("stopPreview");
151     sp <::android::hardware::ICamera> c = mCamera;
152     if (c == 0) return;
153     c->stopPreview();
154 }
155 
156 // stop recording mode
stopRecording()157 void Camera::stopRecording()
158 {
159     ALOGV("stopRecording");
160     sp <::android::hardware::ICamera> c = mCamera;
161     if (c == 0) return;
162     c->stopRecording();
163 }
164 
165 // release a recording frame
releaseRecordingFrame(const sp<IMemory> & mem)166 void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
167 {
168     ALOGV("releaseRecordingFrame");
169     sp <::android::hardware::ICamera> c = mCamera;
170     if (c == 0) return;
171     c->releaseRecordingFrame(mem);
172 }
173 
releaseRecordingFrameHandle(native_handle_t * handle)174 void 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 
releaseRecordingFrameHandleBatch(const std::vector<native_handle_t * > handles)182 void 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 
190 // get preview state
previewEnabled()191 bool Camera::previewEnabled()
192 {
193     ALOGV("previewEnabled");
194     sp <::android::hardware::ICamera> c = mCamera;
195     if (c == 0) return false;
196     return c->previewEnabled();
197 }
198 
199 // get recording state
recordingEnabled()200 bool Camera::recordingEnabled()
201 {
202     ALOGV("recordingEnabled");
203     sp <::android::hardware::ICamera> c = mCamera;
204     if (c == 0) return false;
205     return c->recordingEnabled();
206 }
207 
autoFocus()208 status_t Camera::autoFocus()
209 {
210     ALOGV("autoFocus");
211     sp <::android::hardware::ICamera> c = mCamera;
212     if (c == 0) return NO_INIT;
213     return c->autoFocus();
214 }
215 
cancelAutoFocus()216 status_t Camera::cancelAutoFocus()
217 {
218     ALOGV("cancelAutoFocus");
219     sp <::android::hardware::ICamera> c = mCamera;
220     if (c == 0) return NO_INIT;
221     return c->cancelAutoFocus();
222 }
223 
224 // take a picture
takePicture(int msgType)225 status_t Camera::takePicture(int msgType)
226 {
227     ALOGV("takePicture: 0x%x", msgType);
228     sp <::android::hardware::ICamera> c = mCamera;
229     if (c == 0) return NO_INIT;
230     return c->takePicture(msgType);
231 }
232 
233 // set preview/capture parameters - key/value pairs
setParameters(const String8 & params)234 status_t Camera::setParameters(const String8& params)
235 {
236     ALOGV("setParameters");
237     sp <::android::hardware::ICamera> c = mCamera;
238     if (c == 0) return NO_INIT;
239     return c->setParameters(params);
240 }
241 
242 // get preview/capture parameters - key/value pairs
getParameters() const243 String8 Camera::getParameters() const
244 {
245     ALOGV("getParameters");
246     String8 params;
247     sp <::android::hardware::ICamera> c = mCamera;
248     if (c != 0) params = mCamera->getParameters();
249     return params;
250 }
251 
252 // send command to camera driver
sendCommand(int32_t cmd,int32_t arg1,int32_t arg2)253 status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
254 {
255     ALOGV("sendCommand");
256     sp <::android::hardware::ICamera> c = mCamera;
257     if (c == 0) return NO_INIT;
258     return c->sendCommand(cmd, arg1, arg2);
259 }
260 
setListener(const sp<CameraListener> & listener)261 void Camera::setListener(const sp<CameraListener>& listener)
262 {
263     Mutex::Autolock _l(mLock);
264     mListener = listener;
265 }
266 
setPreviewCallbackFlags(int flag)267 void Camera::setPreviewCallbackFlags(int flag)
268 {
269     ALOGV("setPreviewCallbackFlags");
270     sp <::android::hardware::ICamera> c = mCamera;
271     if (c == 0) return;
272     mCamera->setPreviewCallbackFlag(flag);
273 }
274 
setPreviewCallbackTarget(const sp<IGraphicBufferProducer> & callbackProducer)275 status_t Camera::setPreviewCallbackTarget(
276         const sp<IGraphicBufferProducer>& callbackProducer)
277 {
278     sp <::android::hardware::ICamera> c = mCamera;
279     if (c == 0) return NO_INIT;
280     return c->setPreviewCallbackTarget(callbackProducer);
281 }
282 
setAudioRestriction(int32_t mode)283 status_t Camera::setAudioRestriction(int32_t mode)
284 {
285     sp <::android::hardware::ICamera> c = mCamera;
286     if (c == 0) return NO_INIT;
287     return c->setAudioRestriction(mode);
288 }
289 
getGlobalAudioRestriction()290 int32_t Camera::getGlobalAudioRestriction()
291 {
292     sp <::android::hardware::ICamera> c = mCamera;
293     if (c == 0) return NO_INIT;
294     return c->getGlobalAudioRestriction();
295 }
296 
297 // callback from camera service
notifyCallback(int32_t msgType,int32_t ext1,int32_t ext2)298 void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
299 {
300     return CameraBaseT::notifyCallback(msgType, ext1, ext2);
301 }
302 
303 // callback from camera service when frame or image is ready
dataCallback(int32_t msgType,const sp<IMemory> & dataPtr,camera_frame_metadata_t * metadata)304 void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
305                           camera_frame_metadata_t *metadata)
306 {
307     sp<CameraListener> listener;
308     {
309         Mutex::Autolock _l(mLock);
310         listener = mListener;
311     }
312     if (listener != NULL) {
313         listener->postData(msgType, dataPtr, metadata);
314     }
315 }
316 
317 // callback from camera service when timestamped frame is ready
dataCallbackTimestamp(nsecs_t timestamp,int32_t msgType,const sp<IMemory> & dataPtr)318 void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
319 {
320     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 {
329         ALOGW("No listener was set. Drop a recording frame.");
330         releaseRecordingFrame(dataPtr);
331     }
332 }
333 
recordingFrameHandleCallbackTimestamp(nsecs_t timestamp,native_handle_t * handle)334 void Camera::recordingFrameHandleCallbackTimestamp(nsecs_t timestamp, native_handle_t* handle)
335 {
336     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 
recordingFrameHandleCallbackTimestampBatch(const std::vector<nsecs_t> & timestamps,const std::vector<native_handle_t * > & handles)350 void Camera::recordingFrameHandleCallbackTimestampBatch(
351         const std::vector<nsecs_t>& timestamps,
352         const std::vector<native_handle_t*>& handles)
353 {
354     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 
getRecordingProxy()368 sp<ICameraRecordingProxy> Camera::getRecordingProxy() {
369     ALOGV("getProxy");
370     return new RecordingProxy(this);
371 }
372 
startRecording()373 status_t Camera::RecordingProxy::startRecording()
374 {
375     ALOGV("RecordingProxy::startRecording");
376     mCamera->reconnect();
377     return mCamera->startRecording();
378 }
379 
stopRecording()380 void Camera::RecordingProxy::stopRecording()
381 {
382     ALOGV("RecordingProxy::stopRecording");
383     mCamera->stopRecording();
384 }
385 
RecordingProxy(const sp<Camera> & camera)386 Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera)
387 {
388     mCamera = camera;
389 }
390 
391 }; // namespace android
392