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