blob: ed4f3b82d83056a7b2d3e02fa673a38a06fe3099 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
4** Copyright (C) 2008 HTC Inc.
5**
6** Licensed under the Apache License, Version 2.0 (the "License");
7** you may not use this file except in compliance with the License.
8** You may obtain a copy of the License at
9**
10** http://www.apache.org/licenses/LICENSE-2.0
11**
12** Unless required by applicable law or agreed to in writing, software
13** distributed under the License is distributed on an "AS IS" BASIS,
14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15** See the License for the specific language governing permissions and
16** limitations under the License.
17*/
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "Camera"
21#include <utils/Log.h>
22#include <utils/IServiceManager.h>
23#include <utils/threads.h>
24#include <utils/IMemory.h>
25#include <ui/Surface.h>
26#include <ui/Camera.h>
27#include <ui/ICameraService.h>
28
29namespace android {
30
31// client singleton for camera service binder interface
32Mutex Camera::mLock;
33sp<ICameraService> Camera::mCameraService;
34sp<Camera::DeathNotifier> Camera::mDeathNotifier;
35
36// establish binder interface to camera service
37const sp<ICameraService>& Camera::getCameraService()
38{
39 Mutex::Autolock _l(mLock);
40 if (mCameraService.get() == 0) {
41 sp<IServiceManager> sm = defaultServiceManager();
42 sp<IBinder> binder;
43 do {
44 binder = sm->getService(String16("media.camera"));
45 if (binder != 0)
46 break;
47 LOGW("CameraService not published, waiting...");
48 usleep(500000); // 0.5 s
49 } while(true);
50 if (mDeathNotifier == NULL) {
51 mDeathNotifier = new DeathNotifier();
52 }
53 binder->linkToDeath(mDeathNotifier);
54 mCameraService = interface_cast<ICameraService>(binder);
55 }
56 LOGE_IF(mCameraService==0, "no CameraService!?");
57 return mCameraService;
58}
59
60// ---------------------------------------------------------------------------
61
62Camera::Camera()
63{
64 init();
65}
66
James Dongd2dc9ac2009-04-27 12:01:59 -070067// construct a camera client from an existing camera remote
James Dongf1a53142009-04-23 14:07:23 -070068sp<Camera> Camera::create(const sp<ICamera>& camera)
69{
James Dongd2dc9ac2009-04-27 12:01:59 -070070 LOGV("create");
71 if (camera == 0) {
72 LOGE("camera remote is a NULL pointer");
73 return 0;
74 }
75
James Dongf1a53142009-04-23 14:07:23 -070076 sp<Camera> c = new Camera();
James Dongf1a53142009-04-23 14:07:23 -070077 if (camera->connect(c) == NO_ERROR) {
78 c->mStatus = NO_ERROR;
79 c->mCamera = camera;
80 camera->asBinder()->linkToDeath(c);
81 }
82 return c;
83}
84
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085void Camera::init()
86{
87 mStatus = UNKNOWN_ERROR;
88 mShutterCallback = 0;
89 mShutterCallbackCookie = 0;
90 mRawCallback = 0;
91 mRawCallbackCookie = 0;
92 mJpegCallback = 0;
93 mJpegCallbackCookie = 0;
94 mPreviewCallback = 0;
95 mPreviewCallbackCookie = 0;
96 mRecordingCallback = 0;
97 mRecordingCallbackCookie = 0;
98 mErrorCallback = 0;
99 mErrorCallbackCookie = 0;
100 mAutoFocusCallback = 0;
101 mAutoFocusCallbackCookie = 0;
102}
103
104Camera::~Camera()
105{
106 disconnect();
107}
108
109sp<Camera> Camera::connect()
110{
111 LOGV("connect");
112 sp<Camera> c = new Camera();
113 const sp<ICameraService>& cs = getCameraService();
114 if (cs != 0) {
115 c->mCamera = cs->connect(c);
116 }
117 if (c->mCamera != 0) {
118 c->mCamera->asBinder()->linkToDeath(c);
119 c->mStatus = NO_ERROR;
120 } else {
121 c.clear();
122 }
123 return c;
124}
125
126void Camera::disconnect()
127{
128 LOGV("disconnect");
129 if (mCamera != 0) {
130 mErrorCallback = 0;
131 mCamera->disconnect();
132 mCamera = 0;
133 }
134}
135
136status_t Camera::reconnect()
137{
138 LOGV("reconnect");
139 sp <ICamera> c = mCamera;
140 if (c == 0) return NO_INIT;
141 return c->connect(this);
142}
143
144sp<ICamera> Camera::remote()
145{
146 return mCamera;
147}
148
149status_t Camera::lock()
150{
151 sp <ICamera> c = mCamera;
152 if (c == 0) return NO_INIT;
153 return c->lock();
154}
155
156status_t Camera::unlock()
157{
158 sp <ICamera> c = mCamera;
159 if (c == 0) return NO_INIT;
160 return c->unlock();
161}
162
163// pass the buffered ISurface to the camera service
164status_t Camera::setPreviewDisplay(const sp<Surface>& surface)
165{
166 LOGV("setPreviewDisplay");
167 if (surface == 0) {
168 LOGE("app passed NULL surface");
169 return NO_INIT;
170 }
171 sp <ICamera> c = mCamera;
172 if (c == 0) return NO_INIT;
173 return c->setPreviewDisplay(surface->getISurface());
174}
175
176status_t Camera::setPreviewDisplay(const sp<ISurface>& surface)
177{
178 LOGV("setPreviewDisplay");
179 if (surface == 0) {
180 LOGE("app passed NULL surface");
181 return NO_INIT;
182 }
183 sp <ICamera> c = mCamera;
184 if (c == 0) return NO_INIT;
185 return c->setPreviewDisplay(surface);
186}
187
188
189// start preview mode, must call setPreviewDisplay first
190status_t Camera::startPreview()
191{
192 LOGV("startPreview");
193 sp <ICamera> c = mCamera;
194 if (c == 0) return NO_INIT;
195 return c->startPreview();
196}
197
198// start recording mode, must call setPreviewDisplay first
199status_t Camera::startRecording()
200{
201 LOGV("startRecording");
202 sp <ICamera> c = mCamera;
203 if (c == 0) return NO_INIT;
204 return c->startRecording();
205}
206
207// stop preview mode
208void Camera::stopPreview()
209{
210 LOGV("stopPreview");
211 sp <ICamera> c = mCamera;
212 if (c == 0) return;
213 c->stopPreview();
214}
215
216// stop recording mode
217void Camera::stopRecording()
218{
219 LOGV("stopRecording");
220 sp <ICamera> c = mCamera;
221 if (c == 0) return;
222 c->stopRecording();
223}
224
225// release a recording frame
226void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
227{
228 LOGV("releaseRecordingFrame");
229 sp <ICamera> c = mCamera;
230 if (c == 0) return;
231 c->releaseRecordingFrame(mem);
232}
233
234// get preview state
235bool Camera::previewEnabled()
236{
237 LOGV("previewEnabled");
238 sp <ICamera> c = mCamera;
239 if (c == 0) return false;
240 return c->previewEnabled();
241}
242
243// get recording state
244bool Camera::recordingEnabled()
245{
246 LOGV("recordingEnabled");
247 sp <ICamera> c = mCamera;
248 if (c == 0) return false;
249 return c->recordingEnabled();
250}
251
252status_t Camera::autoFocus()
253{
254 LOGV("autoFocus");
255 sp <ICamera> c = mCamera;
256 if (c == 0) return NO_INIT;
257 return c->autoFocus();
258}
259
260// take a picture
261status_t Camera::takePicture()
262{
263 LOGV("takePicture");
264 sp <ICamera> c = mCamera;
265 if (c == 0) return NO_INIT;
266 return c->takePicture();
267}
268
269// set preview/capture parameters - key/value pairs
270status_t Camera::setParameters(const String8& params)
271{
272 LOGV("setParameters");
273 sp <ICamera> c = mCamera;
274 if (c == 0) return NO_INIT;
275 return c->setParameters(params);
276}
277
278// get preview/capture parameters - key/value pairs
279String8 Camera::getParameters() const
280{
281 LOGV("getParameters");
282 String8 params;
283 sp <ICamera> c = mCamera;
284 if (c != 0) params = mCamera->getParameters();
285 return params;
286}
287
288void Camera::setAutoFocusCallback(autofocus_callback cb, void *cookie)
289{
290 LOGV("setAutoFocusCallback");
291 mAutoFocusCallback = cb;
292 mAutoFocusCallbackCookie = cookie;
293}
294
295void Camera::setShutterCallback(shutter_callback cb, void *cookie)
296{
297 LOGV("setShutterCallback");
298 mShutterCallback = cb;
299 mShutterCallbackCookie = cookie;
300}
301
302void Camera::setRawCallback(frame_callback cb, void *cookie)
303{
304 LOGV("setRawCallback");
305 mRawCallback = cb;
306 mRawCallbackCookie = cookie;
307}
308
309void Camera::setJpegCallback(frame_callback cb, void *cookie)
310{
311 LOGV("setJpegCallback");
312 mJpegCallback = cb;
313 mJpegCallbackCookie = cookie;
314}
315
316void Camera::setPreviewCallback(frame_callback cb, void *cookie, int flag)
317{
318 LOGV("setPreviewCallback");
319 mPreviewCallback = cb;
320 mPreviewCallbackCookie = cookie;
321 sp <ICamera> c = mCamera;
322 if (c == 0) return;
323 mCamera->setPreviewCallbackFlag(flag);
324}
325
326void Camera::setRecordingCallback(frame_callback cb, void *cookie)
327{
328 LOGV("setRecordingCallback");
329 mRecordingCallback = cb;
330 mRecordingCallbackCookie = cookie;
331}
332
333void Camera::setErrorCallback(error_callback cb, void *cookie)
334{
335 LOGV("setErrorCallback");
336 mErrorCallback = cb;
337 mErrorCallbackCookie = cookie;
338}
339
340void Camera::autoFocusCallback(bool focused)
341{
342 LOGV("autoFocusCallback");
343 if (mAutoFocusCallback) {
344 mAutoFocusCallback(focused, mAutoFocusCallbackCookie);
345 }
346}
347
348void Camera::shutterCallback()
349{
350 LOGV("shutterCallback");
351 if (mShutterCallback) {
352 mShutterCallback(mShutterCallbackCookie);
353 }
354}
355
356void Camera::rawCallback(const sp<IMemory>& picture)
357{
358 LOGV("rawCallback");
359 if (mRawCallback) {
360 mRawCallback(picture, mRawCallbackCookie);
361 }
362}
363
364// callback from camera service when image is ready
365void Camera::jpegCallback(const sp<IMemory>& picture)
366{
367 LOGV("jpegCallback");
368 if (mJpegCallback) {
369 mJpegCallback(picture, mJpegCallbackCookie);
370 }
371}
372
373// callback from camera service when preview frame is ready
374void Camera::previewCallback(const sp<IMemory>& frame)
375{
376 LOGV("frameCallback");
377 if (mPreviewCallback) {
378 mPreviewCallback(frame, mPreviewCallbackCookie);
379 }
380}
381
382// callback from camera service when a recording frame is ready
383void Camera::recordingCallback(const sp<IMemory>& frame)
384{
385 LOGV("recordingCallback");
386 if (mRecordingCallback) {
387 mRecordingCallback(frame, mRecordingCallbackCookie);
388 }
389}
390
391// callback from camera service when an error occurs in preview or takePicture
392void Camera::errorCallback(status_t error)
393{
394 LOGV("errorCallback");
395 if (mErrorCallback) {
396 mErrorCallback(error, mErrorCallbackCookie);
397 }
398}
399
Dave Sparks9b352332009-05-07 12:25:25 -0700400// callback from camera service
401void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
402{
403 LOGV("notifyCallback");
404}
405
406// callback from camera service when image is ready
407void Camera::dataCallback(int32_t msgType, const sp<IMemory>& frame)
408{
409 LOGV("dataCallback");
410}
411
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412void Camera::binderDied(const wp<IBinder>& who) {
413 LOGW("ICamera died");
414 if (mErrorCallback) {
415 mErrorCallback(DEAD_OBJECT, mErrorCallbackCookie);
416 }
417}
418
419void Camera::DeathNotifier::binderDied(const wp<IBinder>& who) {
420 LOGV("binderDied");
421 Mutex::Autolock _l(Camera::mLock);
422 Camera::mCameraService.clear();
423 LOGW("Camera server died!");
424}
425
426}; // namespace android
427