blob: b103861533fc209f9137efc9b47b213c1e4e9003 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2**
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08003** Copyright (C) 2008, The Android Open Source Project
4** Copyright (C) 2008 HTC Inc.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07005**
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
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080019//#define LOG_NDEBUG 0
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070020#define LOG_TAG "CameraService"
21#include <utils/Log.h>
22
23#include <utils/IServiceManager.h>
24#include <utils/IPCThreadState.h>
25#include <utils/String16.h>
26#include <utils/Errors.h>
27#include <utils/MemoryBase.h>
28#include <utils/MemoryHeapBase.h>
29#include <ui/ICameraService.h>
30
31#include "CameraService.h"
32
33namespace android {
34
35extern "C" {
36#include <stdio.h>
37#include <sys/types.h>
38#include <sys/stat.h>
39#include <fcntl.h>
40#include <pthread.h>
41}
42
43// When you enable this, as well as DEBUG_REFS=1 and
44// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp, this will track all
45// references to the CameraService::Client in order to catch the case where the
46// client is being destroyed while a callback from the CameraHardwareInterface
47// is outstanding. This is a serious bug because if we make another call into
48// CameraHardwreInterface that itself triggers a callback, we will deadlock.
49
50#define DEBUG_CLIENT_REFERENCES 0
51
52#define PICTURE_TIMEOUT seconds(5)
53
54#define DEBUG_DUMP_PREVIEW_FRAME_TO_FILE 0 /* n-th frame to write */
55#define DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE 0
56#define DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE 0
57
58#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
59static int debug_frame_cnt;
60#endif
61
62// ----------------------------------------------------------------------------
63
64void CameraService::instantiate() {
65 defaultServiceManager()->addService(
66 String16("media.camera"), new CameraService());
67}
68
69// ----------------------------------------------------------------------------
70
71CameraService::CameraService() :
72 BnCameraService()
73{
74 LOGI("CameraService started: pid=%d", getpid());
75}
76
77CameraService::~CameraService()
78{
79 if (mClient != 0) {
80 LOGE("mClient was still connected in destructor!");
81 }
82}
83
84sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient)
85{
86 LOGD("Connect E from ICameraClient %p", cameraClient->asBinder().get());
87
88 Mutex::Autolock lock(mLock);
89 if (mClient != 0) {
90 sp<Client> currentClient = mClient.promote();
91 if (currentClient != 0) {
92 sp<ICameraClient> currentCameraClient(currentClient->getCameraClient());
93 if (cameraClient->asBinder() == currentCameraClient->asBinder()) {
94 // this is the same client reconnecting...
95 LOGD("Connect X same client is reconnecting...");
96 return currentClient;
97 } else {
98 // it's another client... boot the previous one...
99 LOGD("new client connecting, booting the old one...");
100 mClient.clear();
101 }
102 } else {
103 // can't promote, the previous client has died...
104 LOGD("new client connecting, old reference was dangling...");
105 mClient.clear();
106 }
107 }
108
109 // create a new Client object
The Android Open Source Project27629322009-01-09 17:51:23 -0800110 sp<Client> client = new Client(this, cameraClient, IPCThreadState::self()->getCallingPid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 mClient = client;
112#if DEBUG_CLIENT_REFERENCES
113 // Enable tracking for this object, and track increments and decrements of
114 // the refcount.
115 client->trackMe(true, true);
116#endif
117 LOGD("Connect X");
118 return client;
119}
120
121void CameraService::removeClient(const sp<ICameraClient>& cameraClient)
122{
123 // declar this outside the lock to make absolutely sure the
124 // destructor won't be called with the lock held.
125 sp<Client> client;
126
127 Mutex::Autolock lock(mLock);
128
129 if (mClient == 0) {
130 // This happens when we have already disconnected.
131 LOGV("mClient is null.");
132 return;
133 }
134
135 // Promote mClient. It should never fail because we're called from
136 // a binder call, so someone has to have a strong reference.
137 client = mClient.promote();
138 if (client == 0) {
139 LOGW("can't get a strong reference on mClient!");
140 mClient.clear();
141 return;
142 }
143
144 if (cameraClient->asBinder() != client->getCameraClient()->asBinder()) {
145 // ugh! that's not our client!!
146 LOGW("removeClient() called, but mClient doesn't match!");
147 } else {
148 // okay, good, forget about mClient
149 mClient.clear();
150 }
151}
152
153CameraService::Client::Client(const sp<CameraService>& cameraService,
The Android Open Source Project27629322009-01-09 17:51:23 -0800154 const sp<ICameraClient>& cameraClient, pid_t clientPid)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700155{
156 LOGD("Client E constructor");
The Android Open Source Project27629322009-01-09 17:51:23 -0800157 mCameraService = cameraService;
158 mCameraClient = cameraClient;
159 mClientPid = clientPid;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 mHardware = openCameraHardware();
Rebecca Schultz Zavin288482b2009-02-13 17:28:15 -0800161 mUseOverlay = mHardware->useOverlay();
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800162
163 // Callback is disabled by default
164 mFrameCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 LOGD("Client X constructor");
166}
167
The Android Open Source Project27629322009-01-09 17:51:23 -0800168status_t CameraService::Client::checkPid()
169{
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800170 if (mClientPid == IPCThreadState::self()->getCallingPid()) return NO_ERROR;
171 LOGW("Attempt to use locked camera from different process");
172 return -EBUSY;
The Android Open Source Project27629322009-01-09 17:51:23 -0800173}
174
175status_t CameraService::Client::lock()
176{
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800177 // lock camera to this client if the the camera is unlocked
178 if (mClientPid == 0) {
179 mClientPid = IPCThreadState::self()->getCallingPid();
180 return NO_ERROR;
181 }
182 // returns NO_ERROR if the client already owns the camera, -EBUSY otherwise
183 return checkPid();
The Android Open Source Project27629322009-01-09 17:51:23 -0800184}
185
186status_t CameraService::Client::unlock()
187{
188 // allow anyone to use camera
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800189 LOGV("unlock");
The Android Open Source Project27629322009-01-09 17:51:23 -0800190 status_t result = checkPid();
191 if (result == NO_ERROR) mClientPid = 0;
192 return result;
193}
194
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800195status_t CameraService::Client::connect(const sp<ICameraClient>& client)
196{
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800197 // connect a new process to the camera
198 LOGV("connect");
199
200 // hold a reference to the old client or we will deadlock if the client is
201 // in the same process and we hold the lock when we remove the reference
202 sp<ICameraClient> oldClient;
203 {
204 Mutex::Autolock _l(mLock);
205 if (mClientPid != 0) {
206 LOGW("Tried to connect to locked camera");
207 return -EBUSY;
208 }
209 oldClient = mCameraClient;
210
211 // did the client actually change?
212 if (client->asBinder() == mCameraClient->asBinder()) return NO_ERROR;
213
214 LOGV("connect new process to existing camera client");
215 mCameraClient = client;
216 mClientPid = IPCThreadState::self()->getCallingPid();
217 mFrameCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
218 }
219
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800220 return NO_ERROR;
221}
222
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223#if HAVE_ANDROID_OS
224static void *unregister_surface(void *arg)
225{
226 ISurface *surface = (ISurface *)arg;
227 surface->unregisterBuffers();
228 IPCThreadState::self()->flushCommands();
229 return NULL;
230}
231#endif
232
233CameraService::Client::~Client()
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800234{
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800235 // tear down client
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 LOGD("Client E destructor");
Rebecca Schultz Zavin288482b2009-02-13 17:28:15 -0800237 if (mSurface != 0 && !mUseOverlay) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238#if HAVE_ANDROID_OS
239 pthread_t thr;
240 // We unregister the buffers in a different thread because binder does
241 // not let us make sychronous transactions in a binder destructor (that
242 // is, upon our reaching a refcount of zero.)
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800243 pthread_create(&thr, NULL,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244 unregister_surface,
245 mSurface.get());
246 pthread_join(thr, NULL);
247#else
248 mSurface->unregisterBuffers();
249#endif
250 }
251
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800252 // make sure we tear down the hardware
253 mClientPid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 disconnect();
255 LOGD("Client X destructor");
256}
257
258void CameraService::Client::disconnect()
259{
260 LOGD("Client E disconnect");
261 Mutex::Autolock lock(mLock);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800262 if (mClientPid == 0) {
263 LOGV("camera is unlocked, don't tear down hardware");
264 return;
265 }
The Android Open Source Project27629322009-01-09 17:51:23 -0800266 if (checkPid() != NO_ERROR) return;
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800267
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 mCameraService->removeClient(mCameraClient);
269 if (mHardware != 0) {
270 // Before destroying mHardware, we must make sure it's in the
271 // idle state.
272 mHardware->stopPreview();
273 // Cancel all picture callbacks.
274 mHardware->cancelPicture(true, true, true);
275 // Release the hardware resources.
276 mHardware->release();
277 }
278 mHardware.clear();
279 LOGD("Client X disconnect");
280}
281
282// pass the buffered ISurface to the camera service
283status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface)
284{
285 LOGD("setPreviewDisplay(%p)", surface.get());
286 Mutex::Autolock lock(mLock);
The Android Open Source Project27629322009-01-09 17:51:23 -0800287 status_t result = checkPid();
288 if (result != NO_ERROR) return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289 Mutex::Autolock surfaceLock(mSurfaceLock);
290 // asBinder() is safe on NULL (returns NULL)
291 if (surface->asBinder() != mSurface->asBinder()) {
Rebecca Schultz Zavin288482b2009-02-13 17:28:15 -0800292 if (mSurface != 0 && !mUseOverlay) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700293 LOGD("clearing old preview surface %p", mSurface.get());
294 mSurface->unregisterBuffers();
295 }
296 mSurface = surface;
297 }
298 return NO_ERROR;
299}
300
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800301// set the frame callback flag to affect how the received frames from
302// preview are handled.
303void CameraService::Client::setFrameCallbackFlag(int frame_callback_flag)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304{
305 Mutex::Autolock lock(mLock);
The Android Open Source Project27629322009-01-09 17:51:23 -0800306 if (checkPid() != NO_ERROR) return;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800307 mFrameCallbackFlag = frame_callback_flag;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700308}
309
310// start preview mode, must call setPreviewDisplay first
311status_t CameraService::Client::startPreview()
312{
313 LOGD("startPreview()");
314
315 /* we cannot call into mHardware with mLock held because
316 * mHardware has callbacks onto us which acquire this lock
317 */
318
319 Mutex::Autolock lock(mLock);
The Android Open Source Project27629322009-01-09 17:51:23 -0800320 status_t result = checkPid();
321 if (result != NO_ERROR) return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700322
323 if (mHardware == 0) {
324 LOGE("mHardware is NULL, returning.");
325 return INVALID_OPERATION;
326 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 if (mSurface == 0) {
329 LOGE("setPreviewDisplay must be called before startPreview!");
330 return INVALID_OPERATION;
331 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800332
The Android Open Source Project27629322009-01-09 17:51:23 -0800333 // do nothing if preview is already started
334 if (mHardware->previewEnabled()) return NO_ERROR;
335
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336 // XXX: This needs to be improved. remove all hardcoded stuff
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 int w, h;
339 CameraParameters params(mHardware->getParameters());
340 params.getPreviewSize(&w, &h);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800341
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
343 debug_frame_cnt = 0;
344#endif
Rebecca Schultz Zavin288482b2009-02-13 17:28:15 -0800345 status_t ret;
346 if (mUseOverlay) {
347 const char *format = params.getPreviewFormat();
348 int fmt;
349 LOGD("Use Overlays");
350 if (!strcmp(format, "yuv422i"))
351 fmt = OVERLAY_FORMAT_YCbCr_422_I;
352 else if (!strcmp(format, "rgb565"))
353 fmt = OVERLAY_FORMAT_RGB_565;
354 else {
355 LOGE("Invalid preview format for overlays");
356 return -EINVAL;
357 }
358 sp<OverlayRef> ref = mSurface->createOverlay(w, h, fmt);
359 ret = mHardware->setOverlay(new Overlay(ref));
360 if (ret != NO_ERROR) {
361 LOGE("mHardware->setOverlay() failed with status %d\n", ret);
362 return ret;
363 }
364 ret = mHardware->startPreview(NULL, mCameraService.get());
365 if (ret != NO_ERROR)
366 LOGE("mHardware->startPreview() failed with status %d\n", ret);
367 } else {
368 LOGD("Don't use Overlays");
369 ret = mHardware->startPreview(previewCallback,
370 mCameraService.get());
371 if (ret == NO_ERROR) {
372 mSurface->unregisterBuffers();
373 mSurface->registerBuffers(w, h, w, h, PIXEL_FORMAT_YCbCr_420_SP,
374 mHardware->getPreviewHeap());
375 }
376 else LOGE("mHardware->startPreview() failed with status %d\n", ret);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700377 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800378
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700379 return ret;
380}
381
382// stop preview mode
383void CameraService::Client::stopPreview()
384{
385 LOGD("stopPreview()");
386
387 Mutex::Autolock lock(mLock);
The Android Open Source Project27629322009-01-09 17:51:23 -0800388 if (checkPid() != NO_ERROR) return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700389
390 if (mHardware == 0) {
391 LOGE("mHardware is NULL, returning.");
392 return;
393 }
394
395 mHardware->stopPreview();
396 LOGD("stopPreview(), hardware stopped OK");
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800397
Rebecca Schultz Zavin288482b2009-02-13 17:28:15 -0800398 if (mSurface != 0 && !mUseOverlay) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700399 mSurface->unregisterBuffers();
400 }
401 mPreviewBuffer.clear();
402}
403
The Android Open Source Project27629322009-01-09 17:51:23 -0800404bool CameraService::Client::previewEnabled()
405{
406 Mutex::Autolock lock(mLock);
407 if (mHardware == 0) return false;
408 return mHardware->previewEnabled();
409}
410
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700411// Safely retrieves a strong pointer to the client during a hardware callback.
412sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user)
413{
414 sp<Client> client = 0;
415 CameraService *service = static_cast<CameraService*>(user);
416 if (service != NULL) {
417 Mutex::Autolock ourLock(service->mLock);
418 if (service->mClient != 0) {
419 client = service->mClient.promote();
420 if (client == 0) {
421 LOGE("getClientFromCookie: client appears to have died");
422 service->mClient.clear();
423 }
424 } else {
425 LOGE("getClientFromCookie: got callback but client was NULL");
426 }
427 }
428 return client;
429}
430
431
432#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \
433 DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \
434 DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
435static void dump_to_file(const char *fname,
436 uint8_t *buf, uint32_t size)
437{
438 int nw, cnt = 0;
439 uint32_t written = 0;
440
441 LOGD("opening file [%s]\n", fname);
442 int fd = open(fname, O_RDWR | O_CREAT);
443 if (fd < 0) {
444 LOGE("failed to create file [%s]: %s", fname, strerror(errno));
445 return;
446 }
447
448 LOGD("writing %d bytes to file [%s]\n", size, fname);
449 while (written < size) {
450 nw = ::write(fd,
451 buf + written,
452 size - written);
453 if (nw < 0) {
454 LOGE("failed to write to file [%s]: %s",
455 fname, strerror(errno));
456 break;
457 }
458 written += nw;
459 cnt++;
460 }
461 LOGD("done writing %d bytes to file [%s] in %d passes\n",
462 size, fname, cnt);
463 ::close(fd);
464}
465#endif
466
467// preview callback - frame buffer update
468void CameraService::Client::previewCallback(const sp<IMemory>& mem, void* user)
469{
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800470 LOGV("previewCallback()");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700471 sp<Client> client = getClientFromCookie(user);
472 if (client == 0) {
473 return;
474 }
475
476#if DEBUG_HEAP_LEAKS && 0 // debugging
477 if (gWeakHeap == NULL) {
478 ssize_t offset;
479 size_t size;
480 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
481 if (gWeakHeap != heap) {
482 LOGD("SETTING PREVIEW HEAP");
483 heap->trackMe(true, true);
484 gWeakHeap = heap;
485 }
486 }
487#endif
488
489#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
490 {
491 if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) {
492 ssize_t offset;
493 size_t size;
494 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
495 dump_to_file("/data/preview.yuv",
496 (uint8_t *)heap->base() + offset, size);
497 }
498 }
499#endif
500
501 // The strong pointer guarantees the client will exist, but no lock is held.
502 client->postFrame(mem);
503
504#if DEBUG_CLIENT_REFERENCES
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800505 //**** if the client's refcount is 1, then we are about to destroy it here,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700506 // which is bad--print all refcounts.
507 if (client->getStrongCount() == 1) {
508 LOGE("++++++++++++++++ (PREVIEW) THIS WILL CAUSE A LOCKUP!");
509 client->printRefs();
510 }
511#endif
512}
513
514// take a picture - image is returned in callback
515status_t CameraService::Client::autoFocus()
516{
517 LOGV("autoFocus");
518
519 Mutex::Autolock lock(mLock);
The Android Open Source Project27629322009-01-09 17:51:23 -0800520 status_t result = checkPid();
521 if (result != NO_ERROR) return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700522
523 if (mHardware == 0) {
524 LOGE("mHardware is NULL, returning.");
525 return INVALID_OPERATION;
526 }
527
528 return mHardware->autoFocus(autoFocusCallback,
529 mCameraService.get());
530}
531
532// take a picture - image is returned in callback
533status_t CameraService::Client::takePicture()
534{
535 LOGD("takePicture");
536
537 Mutex::Autolock lock(mLock);
The Android Open Source Project27629322009-01-09 17:51:23 -0800538 status_t result = checkPid();
539 if (result != NO_ERROR) return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700540
541 if (mHardware == 0) {
542 LOGE("mHardware is NULL, returning.");
543 return INVALID_OPERATION;
544 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800545
Rebecca Schultz Zavin288482b2009-02-13 17:28:15 -0800546 if (mSurface != NULL && !mUseOverlay)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700547 mSurface->unregisterBuffers();
548
549 return mHardware->takePicture(shutterCallback,
550 yuvPictureCallback,
551 jpegPictureCallback,
552 mCameraService.get());
553}
554
555// picture callback - snapshot taken
556void CameraService::Client::shutterCallback(void *user)
557{
558 sp<Client> client = getClientFromCookie(user);
559 if (client == 0) {
560 return;
561 }
562
563 client->postShutter();
564}
565
566// picture callback - raw image ready
567void CameraService::Client::yuvPictureCallback(const sp<IMemory>& mem,
568 void *user)
569{
570 sp<Client> client = getClientFromCookie(user);
571 if (client == 0) {
572 return;
573 }
574 if (mem == NULL) {
575 client->postRaw(NULL);
576 client->postError(UNKNOWN_ERROR);
577 return;
578 }
579
580 ssize_t offset;
581 size_t size;
582 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
583#if DEBUG_HEAP_LEAKS && 0 // debugging
584 gWeakHeap = heap; // debugging
585#endif
586
587 //LOGV("yuvPictureCallback(%d, %d, %p)", offset, size, user);
588#if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only
589 dump_to_file("/data/photo.yuv",
590 (uint8_t *)heap->base() + offset, size);
591#endif
592
593 // Put the YUV version of the snapshot in the preview display.
594 int w, h;
595 CameraParameters params(client->mHardware->getParameters());
596 params.getPictureSize(&w, &h);
597
598// Mutex::Autolock clientLock(client->mLock);
Rebecca Schultz Zavin288482b2009-02-13 17:28:15 -0800599 if (client->mSurface != 0 && !client->mUseOverlay) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600 client->mSurface->unregisterBuffers();
601 client->mSurface->registerBuffers(w,h,w,h,
602 PIXEL_FORMAT_YCbCr_420_SP, heap);
603 client->mSurface->postBuffer(offset);
604 }
605
606 client->postRaw(mem);
607
608#if DEBUG_CLIENT_REFERENCES
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800609 //**** if the client's refcount is 1, then we are about to destroy it here,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700610 // which is bad--print all refcounts.
611 if (client->getStrongCount() == 1) {
612 LOGE("++++++++++++++++ (RAW) THIS WILL CAUSE A LOCKUP!");
613 client->printRefs();
614 }
615#endif
616}
617
618// picture callback - jpeg ready
619void CameraService::Client::jpegPictureCallback(const sp<IMemory>& mem, void *user)
620{
621 sp<Client> client = getClientFromCookie(user);
622 if (client == 0) {
623 return;
624 }
625 if (mem == NULL) {
626 client->postJpeg(NULL);
627 client->postError(UNKNOWN_ERROR);
628 return;
629 }
630
631 /** We absolutely CANNOT call into user code with a lock held **/
632
633#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only
634 {
635 ssize_t offset;
636 size_t size;
637 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
638 dump_to_file("/data/photo.jpg",
639 (uint8_t *)heap->base() + offset, size);
640 }
641#endif
642
643 client->postJpeg(mem);
644
645#if DEBUG_CLIENT_REFERENCES
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800646 //**** if the client's refcount is 1, then we are about to destroy it here,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700647 // which is bad--print all refcounts.
648 if (client->getStrongCount() == 1) {
649 LOGE("++++++++++++++++ (JPEG) THIS WILL CAUSE A LOCKUP!");
650 client->printRefs();
651 }
652#endif
653}
654
655void CameraService::Client::autoFocusCallback(bool focused, void *user)
656{
657 LOGV("autoFocusCallback");
658
659 sp<Client> client = getClientFromCookie(user);
660 if (client == 0) {
661 return;
662 }
663
664 client->postAutoFocus(focused);
665
666#if DEBUG_CLIENT_REFERENCES
667 if (client->getStrongCount() == 1) {
668 LOGE("++++++++++++++++ (AUTOFOCUS) THIS WILL CAUSE A LOCKUP!");
669 client->printRefs();
670 }
671#endif
672}
673
674// set preview/capture parameters - key/value pairs
675status_t CameraService::Client::setParameters(const String8& params)
676{
677 LOGD("setParameters(%s)", params.string());
678
679 Mutex::Autolock lock(mLock);
The Android Open Source Project27629322009-01-09 17:51:23 -0800680 status_t result = checkPid();
681 if (result != NO_ERROR) return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700682
683 if (mHardware == 0) {
684 LOGE("mHardware is NULL, returning.");
685 return INVALID_OPERATION;
686 }
687
688 CameraParameters p(params);
689 mHardware->setParameters(p);
690 return NO_ERROR;
691}
692
693// get preview/capture parameters - key/value pairs
694String8 CameraService::Client::getParameters() const
695{
696 LOGD("getParameters");
697
698 Mutex::Autolock lock(mLock);
699
700 if (mHardware == 0) {
701 LOGE("mHardware is NULL, returning.");
702 return String8();
703 }
704
705 return mHardware->getParameters().flatten();
706}
707
708void CameraService::Client::postAutoFocus(bool focused)
709{
710 LOGV("postAutoFocus");
711 mCameraClient->autoFocusCallback(focused);
712}
713
714void CameraService::Client::postShutter()
715{
716 mCameraClient->shutterCallback();
717}
718
719void CameraService::Client::postRaw(const sp<IMemory>& mem)
720{
721 LOGD("postRaw");
722 mCameraClient->rawCallback(mem);
723}
724
725void CameraService::Client::postJpeg(const sp<IMemory>& mem)
726{
727 LOGD("postJpeg");
728 mCameraClient->jpegCallback(mem);
729}
730
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800731void CameraService::Client::copyFrameAndPostCopiedFrame(sp<IMemoryHeap> heap, size_t offset, size_t size)
732{
733 LOGV("copyFrameAndPostCopiedFrame");
734 // It is necessary to copy out of pmem before sending this to
735 // the callback. For efficiency, reuse the same MemoryHeapBase
736 // provided it's big enough. Don't allocate the memory or
737 // perform the copy if there's no callback.
738 if (mPreviewBuffer == 0) {
739 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
740 } else if (size > mPreviewBuffer->virtualSize()) {
741 mPreviewBuffer.clear();
742 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
743 if (mPreviewBuffer == 0) {
744 LOGE("failed to allocate space for preview buffer");
745 return;
746 }
747 }
748 memcpy(mPreviewBuffer->base(),
749 (uint8_t *)heap->base() + offset, size);
750
751 sp<MemoryBase> frame = new MemoryBase(mPreviewBuffer, 0, size);
752 if (frame == 0) {
753 LOGE("failed to allocate space for frame callback");
754 return;
755 }
756 mCameraClient->frameCallback(frame);
757}
758
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700759void CameraService::Client::postFrame(const sp<IMemory>& mem)
760{
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800761 LOGV("postFrame");
762 if (mem == 0) {
763 LOGW("mem is a null pointer");
764 return;
765 }
766
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700767 ssize_t offset;
768 size_t size;
769 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700770 {
771 Mutex::Autolock surfaceLock(mSurfaceLock);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800772 if (mSurface != NULL) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700773 mSurface->postBuffer(offset);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700774 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700775 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800776
777 // Is the callback enabled or not?
778 if (!(mFrameCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
779 // If the enable bit is off, the copy-out and one-shot bits are ignored
780 LOGV("frame callback is diabled");
781 return;
782 }
783
784 // Is the received frame copied out or not?
785 if (mFrameCallbackFlag & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
786 LOGV("frame is copied out");
787 copyFrameAndPostCopiedFrame(heap, offset, size);
788 } else {
789 LOGV("frame is directly sent out without copying");
790 mCameraClient->frameCallback(mem);
791 }
792
793 // Is this is one-shot only?
794 if (mFrameCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK) {
795 LOGV("One-shot only, thus clear the bits and disable frame callback");
796 mFrameCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
797 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
798 FRAME_CALLBACK_FLAG_ENABLE_MASK);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700799 }
800}
801
802void CameraService::Client::postError(status_t error) {
803 mCameraClient->errorCallback(error);
804}
805
806status_t CameraService::dump(int fd, const Vector<String16>& args)
807{
808 const size_t SIZE = 256;
809 char buffer[SIZE];
810 String8 result;
811 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
812 snprintf(buffer, SIZE, "Permission Denial: "
813 "can't dump CameraService from pid=%d, uid=%d\n",
814 IPCThreadState::self()->getCallingPid(),
815 IPCThreadState::self()->getCallingUid());
816 result.append(buffer);
817 write(fd, result.string(), result.size());
818 } else {
819 AutoMutex lock(&mLock);
820 if (mClient != 0) {
821 sp<Client> currentClient = mClient.promote();
822 currentClient->mHardware->dump(fd, args);
823 } else {
824 result.append("No camera client yet.\n");
825 write(fd, result.string(), result.size());
826 }
827 }
828 return NO_ERROR;
829}
830
831
832#if DEBUG_HEAP_LEAKS
833
834#define CHECK_INTERFACE(interface, data, reply) \
835 do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \
836 LOGW("Call incorrectly routed to " #interface); \
837 return PERMISSION_DENIED; \
838 } } while (0)
839
840status_t CameraService::onTransact(
841 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
842{
843 // permission checks...
844 switch (code) {
845 case BnCameraService::CONNECT:
846 IPCThreadState* ipc = IPCThreadState::self();
847 const int pid = ipc->getCallingPid();
848 const int self_pid = getpid();
849 if (pid != self_pid) {
850 // we're called from a different process, do the real check
851 if (!checkCallingPermission(
852 String16("android.permission.CAMERA")))
853 {
854 const int uid = ipc->getCallingUid();
855 LOGE("Permission Denial: "
856 "can't use the camera pid=%d, uid=%d", pid, uid);
857 return PERMISSION_DENIED;
858 }
859 }
860 break;
861 }
862
863 status_t err = BnCameraService::onTransact(code, data, reply, flags);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800864
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700865 LOGD("+++ onTransact err %d code %d", err, code);
866
867 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
868 // the 'service' command interrogates this binder for its name, and then supplies it
869 // even for the debugging commands. that means we need to check for it here, using
870 // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to
871 // BnSurfaceComposer before falling through to this code).
872
873 LOGD("+++ onTransact code %d", code);
874
875 CHECK_INTERFACE(ICameraService, data, reply);
876
877 switch(code) {
878 case 1000:
879 {
880 if (gWeakHeap != 0) {
881 sp<IMemoryHeap> h = gWeakHeap.promote();
882 IMemoryHeap *p = gWeakHeap.unsafe_get();
883 LOGD("CHECKING WEAK REFERENCE %p (%p)", h.get(), p);
884 if (h != 0)
885 h->printRefs();
886 bool attempt_to_delete = data.readInt32() == 1;
887 if (attempt_to_delete) {
888 // NOT SAFE!
889 LOGD("DELETING WEAK REFERENCE %p (%p)", h.get(), p);
890 if (p) delete p;
891 }
892 return NO_ERROR;
893 }
894 }
895 break;
896 default:
897 break;
898 }
899 }
900 return err;
901}
902
903#endif // DEBUG_HEAP_LEAKS
904
905}; // namespace android