blob: 5a5570496a6eac81c3fd8dae592b01ab43ffe5f1 [file] [log] [blame]
Mathias Agopianccaa4142010-07-22 15:27:48 -07001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
Mathias Agopianccaa4142010-07-22 15:27:48 -07004**
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_TAG "CameraService"
19#include <utils/Log.h>
20
21#include <binder/IServiceManager.h>
22#include <binder/IPCThreadState.h>
23#include <utils/String16.h>
24#include <utils/Errors.h>
25#include <binder/MemoryBase.h>
26#include <binder/MemoryHeapBase.h>
27#include <camera/ICameraService.h>
28#include <surfaceflinger/ISurface.h>
29#include <ui/Overlay.h>
30
31#include <hardware/hardware.h>
32
33#include <media/mediaplayer.h>
34#include <media/AudioSystem.h>
35#include "CameraService.h"
36
37#include <cutils/atomic.h>
38
39namespace android {
40
41extern "C" {
42#include <stdio.h>
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <fcntl.h>
46#include <pthread.h>
47#include <signal.h>
48}
49
50// When you enable this, as well as DEBUG_REFS=1 and
51// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp, this will track all
52// references to the CameraService::Client in order to catch the case where the
53// client is being destroyed while a callback from the CameraHardwareInterface
54// is outstanding. This is a serious bug because if we make another call into
55// CameraHardwreInterface that itself triggers a callback, we will deadlock.
56
57#define DEBUG_CLIENT_REFERENCES 0
58
59#define PICTURE_TIMEOUT seconds(5)
60
61#define DEBUG_DUMP_PREVIEW_FRAME_TO_FILE 0 /* n-th frame to write */
62#define DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE 0
63#define DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE 0
64#define DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE 0
65
66#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
67static int debug_frame_cnt;
68#endif
69
70static int getCallingPid() {
71 return IPCThreadState::self()->getCallingPid();
72}
73
74// ----------------------------------------------------------------------------
75
76void CameraService::instantiate() {
77 defaultServiceManager()->addService(
78 String16("media.camera"), new CameraService());
79}
80
81// ----------------------------------------------------------------------------
82
83CameraService::CameraService() :
84 BnCameraService()
85{
86 LOGI("CameraService started: pid=%d", getpid());
87 mUsers = 0;
88}
89
90CameraService::~CameraService()
91{
92 if (mClient != 0) {
93 LOGE("mClient was still connected in destructor!");
94 }
95}
96
97sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient)
98{
99 int callingPid = getCallingPid();
100 LOGV("CameraService::connect E (pid %d, client %p)", callingPid,
101 cameraClient->asBinder().get());
102
103 Mutex::Autolock lock(mServiceLock);
104 sp<Client> client;
105 if (mClient != 0) {
106 sp<Client> currentClient = mClient.promote();
107 if (currentClient != 0) {
108 sp<ICameraClient> currentCameraClient(currentClient->getCameraClient());
109 if (cameraClient->asBinder() == currentCameraClient->asBinder()) {
110 // This is the same client reconnecting...
111 LOGV("CameraService::connect X (pid %d, same client %p) is reconnecting...",
112 callingPid, cameraClient->asBinder().get());
113 return currentClient;
114 } else {
115 // It's another client... reject it
116 LOGV("CameraService::connect X (pid %d, new client %p) rejected. "
117 "(old pid %d, old client %p)",
118 callingPid, cameraClient->asBinder().get(),
119 currentClient->mClientPid, currentCameraClient->asBinder().get());
120 if (kill(currentClient->mClientPid, 0) == -1 && errno == ESRCH) {
121 LOGV("The old client is dead!");
122 }
123 return client;
124 }
125 } else {
126 // can't promote, the previous client has died...
127 LOGV("New client (pid %d) connecting, old reference was dangling...",
128 callingPid);
129 mClient.clear();
130 }
131 }
132
133 if (mUsers > 0) {
134 LOGV("Still have client, rejected");
135 return client;
136 }
137
138 // create a new Client object
139 client = new Client(this, cameraClient, callingPid);
140 mClient = client;
141#if DEBUG_CLIENT_REFERENCES
142 // Enable tracking for this object, and track increments and decrements of
143 // the refcount.
144 client->trackMe(true, true);
145#endif
146 LOGV("CameraService::connect X");
147 return client;
148}
149
150void CameraService::removeClient(const sp<ICameraClient>& cameraClient)
151{
152 int callingPid = getCallingPid();
153
154 // Declare this outside the lock to make absolutely sure the
155 // destructor won't be called with the lock held.
156 sp<Client> client;
157
158 Mutex::Autolock lock(mServiceLock);
159
160 if (mClient == 0) {
161 // This happens when we have already disconnected.
162 LOGV("removeClient (pid %d): already disconnected", callingPid);
163 return;
164 }
165
166 // Promote mClient. It can fail if we are called from this path:
167 // Client::~Client() -> disconnect() -> removeClient().
168 client = mClient.promote();
169 if (client == 0) {
170 LOGV("removeClient (pid %d): no more strong reference", callingPid);
171 mClient.clear();
172 return;
173 }
174
175 if (cameraClient->asBinder() != client->getCameraClient()->asBinder()) {
176 // ugh! that's not our client!!
177 LOGW("removeClient (pid %d): mClient doesn't match!", callingPid);
178 } else {
179 // okay, good, forget about mClient
180 mClient.clear();
181 }
182
183 LOGV("removeClient (pid %d) done", callingPid);
184}
185
186// The reason we need this count is a new CameraService::connect() request may
187// come in while the previous Client's destructor has not been run or is still
188// running. If the last strong reference of the previous Client is gone but
189// destructor has not been run, we should not allow the new Client to be created
190// because we need to wait for the previous Client to tear down the hardware
191// first.
192void CameraService::incUsers() {
193 android_atomic_inc(&mUsers);
194}
195
196void CameraService::decUsers() {
197 android_atomic_dec(&mUsers);
198}
199
200static sp<MediaPlayer> newMediaPlayer(const char *file)
201{
202 sp<MediaPlayer> mp = new MediaPlayer();
203 if (mp->setDataSource(file, NULL /* headers */) == NO_ERROR) {
204 mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
205 mp->prepare();
206 } else {
207 mp.clear();
208 LOGE("Failed to load CameraService sounds.");
209 }
210 return mp;
211}
212
213CameraService::Client::Client(const sp<CameraService>& cameraService,
214 const sp<ICameraClient>& cameraClient, pid_t clientPid)
215{
216 int callingPid = getCallingPid();
217 LOGV("Client::Client E (pid %d)", callingPid);
218 mCameraService = cameraService;
219 mCameraClient = cameraClient;
220 mClientPid = clientPid;
221 mHardware = openCameraHardware();
222 mUseOverlay = mHardware->useOverlay();
223
224 mHardware->setCallbacks(notifyCallback,
225 dataCallback,
226 dataCallbackTimestamp,
227 mCameraService.get());
228
229 // Enable zoom, error, and focus messages by default
230 mHardware->enableMsgType(CAMERA_MSG_ERROR |
231 CAMERA_MSG_ZOOM |
232 CAMERA_MSG_FOCUS);
233
234 mMediaPlayerClick = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
235 mMediaPlayerBeep = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
236 mOverlayW = 0;
237 mOverlayH = 0;
238
239 // Callback is disabled by default
240 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
241 mOrientation = 0;
242 cameraService->incUsers();
243 LOGV("Client::Client X (pid %d)", callingPid);
244}
245
246status_t CameraService::Client::checkPid()
247{
248 int callingPid = getCallingPid();
249 if (mClientPid == callingPid) return NO_ERROR;
250 LOGW("Attempt to use locked camera (client %p) from different process "
251 " (old pid %d, new pid %d)",
252 getCameraClient()->asBinder().get(), mClientPid, callingPid);
253 return -EBUSY;
254}
255
256status_t CameraService::Client::lock()
257{
258 int callingPid = getCallingPid();
259 LOGV("lock from pid %d (mClientPid %d)", callingPid, mClientPid);
260 Mutex::Autolock _l(mLock);
261 // lock camera to this client if the the camera is unlocked
262 if (mClientPid == 0) {
263 mClientPid = callingPid;
264 return NO_ERROR;
265 }
266 // returns NO_ERROR if the client already owns the camera, -EBUSY otherwise
267 return checkPid();
268}
269
270status_t CameraService::Client::unlock()
271{
272 int callingPid = getCallingPid();
273 LOGV("unlock from pid %d (mClientPid %d)", callingPid, mClientPid);
274 Mutex::Autolock _l(mLock);
275 // allow anyone to use camera
276 status_t result = checkPid();
277 if (result == NO_ERROR) {
278 mClientPid = 0;
279 LOGV("clear mCameraClient (pid %d)", callingPid);
280 // we need to remove the reference so that when app goes
281 // away, the reference count goes to 0.
282 mCameraClient.clear();
283 }
284 return result;
285}
286
287status_t CameraService::Client::connect(const sp<ICameraClient>& client)
288{
289 int callingPid = getCallingPid();
290
291 // connect a new process to the camera
292 LOGV("Client::connect E (pid %d, client %p)", callingPid, client->asBinder().get());
293
294 // I hate this hack, but things get really ugly when the media recorder
295 // service is handing back the camera to the app. The ICameraClient
296 // destructor will be called during the same IPC, making it look like
297 // the remote client is trying to disconnect. This hack temporarily
298 // sets the mClientPid to an invalid pid to prevent the hardware from
299 // being torn down.
300 {
301
302 // hold a reference to the old client or we will deadlock if the client is
303 // in the same process and we hold the lock when we remove the reference
304 sp<ICameraClient> oldClient;
305 {
306 Mutex::Autolock _l(mLock);
307 if (mClientPid != 0 && checkPid() != NO_ERROR) {
308 LOGW("Tried to connect to locked camera (old pid %d, new pid %d)",
309 mClientPid, callingPid);
310 return -EBUSY;
311 }
312 oldClient = mCameraClient;
313
314 // did the client actually change?
315 if ((mCameraClient != NULL) && (client->asBinder() == mCameraClient->asBinder())) {
316 LOGV("Connect to the same client");
317 return NO_ERROR;
318 }
319
320 mCameraClient = client;
321 mClientPid = -1;
322 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
323 LOGV("Connect to the new client (pid %d, client %p)",
324 callingPid, mCameraClient->asBinder().get());
325 }
326
327 }
328 // the old client destructor is called when oldClient goes out of scope
329 // now we set the new PID to lock the interface again
330 mClientPid = callingPid;
331
332 return NO_ERROR;
333}
334
335#if HAVE_ANDROID_OS
336static void *unregister_surface(void *arg)
337{
338 ISurface *surface = (ISurface *)arg;
339 surface->unregisterBuffers();
340 IPCThreadState::self()->flushCommands();
341 return NULL;
342}
343#endif
344
345CameraService::Client::~Client()
346{
347 int callingPid = getCallingPid();
348
349 // tear down client
350 LOGV("Client::~Client E (pid %d, client %p)",
351 callingPid, getCameraClient()->asBinder().get());
352 if (mSurface != 0 && !mUseOverlay) {
353#if HAVE_ANDROID_OS
354 pthread_t thr;
355 // We unregister the buffers in a different thread because binder does
356 // not let us make sychronous transactions in a binder destructor (that
357 // is, upon our reaching a refcount of zero.)
358 pthread_create(&thr, NULL,
359 unregister_surface,
360 mSurface.get());
361 pthread_join(thr, NULL);
362#else
363 mSurface->unregisterBuffers();
364#endif
365 }
366
367 if (mMediaPlayerBeep.get() != NULL) {
368 mMediaPlayerBeep->disconnect();
369 mMediaPlayerBeep.clear();
370 }
371 if (mMediaPlayerClick.get() != NULL) {
372 mMediaPlayerClick->disconnect();
373 mMediaPlayerClick.clear();
374 }
375
376 // make sure we tear down the hardware
377 mClientPid = callingPid;
378 disconnect();
379 LOGV("Client::~Client X (pid %d)", mClientPid);
380}
381
382void CameraService::Client::disconnect()
383{
384 int callingPid = getCallingPid();
385
386 LOGV("Client::disconnect() E (pid %d client %p)",
387 callingPid, getCameraClient()->asBinder().get());
388
389 Mutex::Autolock lock(mLock);
390 if (mClientPid <= 0) {
391 LOGV("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
392 return;
393 }
394 if (checkPid() != NO_ERROR) {
395 LOGV("Different client - don't disconnect");
396 return;
397 }
398
399 // Make sure disconnect() is done once and once only, whether it is called
400 // from the user directly, or called by the destructor.
401 if (mHardware == 0) return;
402
403 LOGV("hardware teardown");
404 // Before destroying mHardware, we must make sure it's in the
405 // idle state.
406 mHardware->stopPreview();
407 // Cancel all picture callbacks.
408 mHardware->disableMsgType(CAMERA_MSG_SHUTTER |
409 CAMERA_MSG_POSTVIEW_FRAME |
410 CAMERA_MSG_RAW_IMAGE |
411 CAMERA_MSG_COMPRESSED_IMAGE);
412 mHardware->cancelPicture();
413 // Turn off remaining messages.
414 mHardware->disableMsgType(CAMERA_MSG_ALL_MSGS);
415 // Release the hardware resources.
416 mHardware->release();
417 // Release the held overlay resources.
418 if (mUseOverlay)
419 {
420 mOverlayRef = 0;
421 }
422 mHardware.clear();
423
424 mCameraService->removeClient(mCameraClient);
425 mCameraService->decUsers();
426
427 LOGV("Client::disconnect() X (pid %d)", callingPid);
428}
429
430// pass the buffered ISurface to the camera service
431status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface)
432{
433 LOGV("setPreviewDisplay(%p) (pid %d)",
434 ((surface == NULL) ? NULL : surface.get()), getCallingPid());
435 Mutex::Autolock lock(mLock);
436 status_t result = checkPid();
437 if (result != NO_ERROR) return result;
438
439 Mutex::Autolock surfaceLock(mSurfaceLock);
440 result = NO_ERROR;
441 // asBinder() is safe on NULL (returns NULL)
442 if (surface->asBinder() != mSurface->asBinder()) {
443 if (mSurface != 0) {
444 LOGV("clearing old preview surface %p", mSurface.get());
445 if ( !mUseOverlay)
446 {
447 mSurface->unregisterBuffers();
448 }
449 else
450 {
451 // Force the destruction of any previous overlay
452 sp<Overlay> dummy;
453 mHardware->setOverlay( dummy );
454 }
455 }
456 mSurface = surface;
457 mOverlayRef = 0;
458 // If preview has been already started, set overlay or register preview
459 // buffers now.
460 if (mHardware->previewEnabled()) {
461 if (mUseOverlay) {
462 result = setOverlay();
463 } else if (mSurface != 0) {
464 result = registerPreviewBuffers();
465 }
466 }
467 }
468 return result;
469}
470
471// set the preview callback flag to affect how the received frames from
472// preview are handled.
473void CameraService::Client::setPreviewCallbackFlag(int callback_flag)
474{
475 LOGV("setPreviewCallbackFlag (pid %d)", getCallingPid());
476 Mutex::Autolock lock(mLock);
477 if (checkPid() != NO_ERROR) return;
478 mPreviewCallbackFlag = callback_flag;
479
480 if(mUseOverlay) {
481 if(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)
482 mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
483 else
484 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
485 }
486}
487
488// start preview mode
489status_t CameraService::Client::startCameraMode(camera_mode mode)
490{
491 int callingPid = getCallingPid();
492
493 LOGV("startCameraMode(%d) (pid %d)", mode, callingPid);
494
495 /* we cannot call into mHardware with mLock held because
496 * mHardware has callbacks onto us which acquire this lock
497 */
498
499 Mutex::Autolock lock(mLock);
500 status_t result = checkPid();
501 if (result != NO_ERROR) return result;
502
503 if (mHardware == 0) {
504 LOGE("mHardware is NULL, returning.");
505 return INVALID_OPERATION;
506 }
507
508 switch(mode) {
509 case CAMERA_RECORDING_MODE:
510 if (mSurface == 0) {
511 LOGE("setPreviewDisplay must be called before startRecordingMode.");
512 return INVALID_OPERATION;
513 }
514 return startRecordingMode();
515
516 default: // CAMERA_PREVIEW_MODE
517 if (mSurface == 0) {
518 LOGV("mSurface is not set yet.");
519 }
520 return startPreviewMode();
521 }
522}
523
524status_t CameraService::Client::startRecordingMode()
525{
526 LOGV("startRecordingMode (pid %d)", getCallingPid());
527
528 status_t ret = UNKNOWN_ERROR;
529
530 // if preview has not been started, start preview first
531 if (!mHardware->previewEnabled()) {
532 ret = startPreviewMode();
533 if (ret != NO_ERROR) {
534 return ret;
535 }
536 }
537
538 // if recording has been enabled, nothing needs to be done
539 if (mHardware->recordingEnabled()) {
540 return NO_ERROR;
541 }
542
543 // start recording mode
544 ret = mHardware->startRecording();
545 if (ret != NO_ERROR) {
546 LOGE("mHardware->startRecording() failed with status %d", ret);
547 }
548 return ret;
549}
550
551status_t CameraService::Client::setOverlay()
552{
553 LOGV("setOverlay");
554 int w, h;
555 CameraParameters params(mHardware->getParameters());
556 params.getPreviewSize(&w, &h);
557
558 if ( w != mOverlayW || h != mOverlayH )
559 {
560 // Force the destruction of any previous overlay
561 sp<Overlay> dummy;
562 mHardware->setOverlay( dummy );
563 mOverlayRef = 0;
564 }
565
566 status_t ret = NO_ERROR;
567 if (mSurface != 0) {
568 if (mOverlayRef.get() == NULL) {
569
570 // FIXME:
571 // Surfaceflinger may hold onto the previous overlay reference for some
572 // time after we try to destroy it. retry a few times. In the future, we
573 // should make the destroy call block, or possibly specify that we can
574 // wait in the createOverlay call if the previous overlay is in the
575 // process of being destroyed.
576 for (int retry = 0; retry < 50; ++retry) {
577 mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT,
578 mOrientation);
579 if (mOverlayRef != NULL) break;
580 LOGW("Overlay create failed - retrying");
581 usleep(20000);
582 }
583 if ( mOverlayRef.get() == NULL )
584 {
585 LOGE("Overlay Creation Failed!");
586 return -EINVAL;
587 }
588 ret = mHardware->setOverlay(new Overlay(mOverlayRef));
589 }
590 } else {
591 ret = mHardware->setOverlay(NULL);
592 }
593 if (ret != NO_ERROR) {
594 LOGE("mHardware->setOverlay() failed with status %d\n", ret);
595 }
596
597 mOverlayW = w;
598 mOverlayH = h;
599
600 return ret;
601}
602
603status_t CameraService::Client::registerPreviewBuffers()
604{
605 int w, h;
606 CameraParameters params(mHardware->getParameters());
607 params.getPreviewSize(&w, &h);
608
609 // don't use a hardcoded format here
610 ISurface::BufferHeap buffers(w, h, w, h,
611 HAL_PIXEL_FORMAT_YCrCb_420_SP,
612 mOrientation,
613 0,
614 mHardware->getPreviewHeap());
615
616 status_t ret = mSurface->registerBuffers(buffers);
617 if (ret != NO_ERROR) {
618 LOGE("registerBuffers failed with status %d", ret);
619 }
620 return ret;
621}
622
623status_t CameraService::Client::startPreviewMode()
624{
625 LOGV("startPreviewMode (pid %d)", getCallingPid());
626
627 // if preview has been enabled, nothing needs to be done
628 if (mHardware->previewEnabled()) {
629 return NO_ERROR;
630 }
631
632 // start preview mode
633#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
634 debug_frame_cnt = 0;
635#endif
636 status_t ret = NO_ERROR;
637
638 if (mUseOverlay) {
639 // If preview display has been set, set overlay now.
640 if (mSurface != 0) {
641 ret = setOverlay();
642 }
643 if (ret != NO_ERROR) return ret;
644 ret = mHardware->startPreview();
645 } else {
646 mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
647 ret = mHardware->startPreview();
648 if (ret != NO_ERROR) return ret;
649 // If preview display has been set, register preview buffers now.
650 if (mSurface != 0) {
651 // Unregister here because the surface registered with raw heap.
652 mSurface->unregisterBuffers();
653 ret = registerPreviewBuffers();
654 }
655 }
656 return ret;
657}
658
659status_t CameraService::Client::startPreview()
660{
661 LOGV("startPreview (pid %d)", getCallingPid());
662
663 return startCameraMode(CAMERA_PREVIEW_MODE);
664}
665
666status_t CameraService::Client::startRecording()
667{
668 LOGV("startRecording (pid %d)", getCallingPid());
669
670 if (mMediaPlayerBeep.get() != NULL) {
671 // do not play record jingle if stream volume is 0
672 // (typically because ringer mode is silent).
673 int index;
674 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
675 if (index != 0) {
676 mMediaPlayerBeep->seekTo(0);
677 mMediaPlayerBeep->start();
678 }
679 }
680
681 mHardware->enableMsgType(CAMERA_MSG_VIDEO_FRAME);
682
683 return startCameraMode(CAMERA_RECORDING_MODE);
684}
685
686// stop preview mode
687void CameraService::Client::stopPreview()
688{
689 LOGV("stopPreview (pid %d)", getCallingPid());
690
691 // hold main lock during state transition
692 {
693 Mutex::Autolock lock(mLock);
694 if (checkPid() != NO_ERROR) return;
695
696 if (mHardware == 0) {
697 LOGE("mHardware is NULL, returning.");
698 return;
699 }
700
701 mHardware->stopPreview();
702 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
703 LOGV("stopPreview(), hardware stopped OK");
704
705 if (mSurface != 0 && !mUseOverlay) {
706 mSurface->unregisterBuffers();
707 }
708 }
709
710 // hold preview buffer lock
711 {
712 Mutex::Autolock lock(mPreviewLock);
713 mPreviewBuffer.clear();
714 }
715}
716
717// stop recording mode
718void CameraService::Client::stopRecording()
719{
720 LOGV("stopRecording (pid %d)", getCallingPid());
721
722 // hold main lock during state transition
723 {
724 Mutex::Autolock lock(mLock);
725 if (checkPid() != NO_ERROR) return;
726
727 if (mHardware == 0) {
728 LOGE("mHardware is NULL, returning.");
729 return;
730 }
731
732 if (mMediaPlayerBeep.get() != NULL) {
733 mMediaPlayerBeep->seekTo(0);
734 mMediaPlayerBeep->start();
735 }
736
737 mHardware->stopRecording();
738 mHardware->disableMsgType(CAMERA_MSG_VIDEO_FRAME);
739 LOGV("stopRecording(), hardware stopped OK");
740 }
741
742 // hold preview buffer lock
743 {
744 Mutex::Autolock lock(mPreviewLock);
745 mPreviewBuffer.clear();
746 }
747}
748
749// release a recording frame
750void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem)
751{
752 Mutex::Autolock lock(mLock);
753 if (checkPid() != NO_ERROR) return;
754
755 if (mHardware == 0) {
756 LOGE("mHardware is NULL, returning.");
757 return;
758 }
759
760 mHardware->releaseRecordingFrame(mem);
761}
762
763bool CameraService::Client::previewEnabled()
764{
765 Mutex::Autolock lock(mLock);
766 if (mHardware == 0) return false;
767 return mHardware->previewEnabled();
768}
769
770bool CameraService::Client::recordingEnabled()
771{
772 Mutex::Autolock lock(mLock);
773 if (mHardware == 0) return false;
774 return mHardware->recordingEnabled();
775}
776
777// Safely retrieves a strong pointer to the client during a hardware callback.
778sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user)
779{
780 sp<Client> client = 0;
781 CameraService *service = static_cast<CameraService*>(user);
782 if (service != NULL) {
783 Mutex::Autolock ourLock(service->mServiceLock);
784 if (service->mClient != 0) {
785 client = service->mClient.promote();
786 if (client == 0) {
787 LOGE("getClientFromCookie: client appears to have died");
788 service->mClient.clear();
789 }
790 } else {
791 LOGE("getClientFromCookie: got callback but client was NULL");
792 }
793 }
794 return client;
795}
796
797
798#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \
799 DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \
800 DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
801static void dump_to_file(const char *fname,
802 uint8_t *buf, uint32_t size)
803{
804 int nw, cnt = 0;
805 uint32_t written = 0;
806
807 LOGV("opening file [%s]\n", fname);
808 int fd = open(fname, O_RDWR | O_CREAT);
809 if (fd < 0) {
810 LOGE("failed to create file [%s]: %s", fname, strerror(errno));
811 return;
812 }
813
814 LOGV("writing %d bytes to file [%s]\n", size, fname);
815 while (written < size) {
816 nw = ::write(fd,
817 buf + written,
818 size - written);
819 if (nw < 0) {
820 LOGE("failed to write to file [%s]: %s",
821 fname, strerror(errno));
822 break;
823 }
824 written += nw;
825 cnt++;
826 }
827 LOGV("done writing %d bytes to file [%s] in %d passes\n",
828 size, fname, cnt);
829 ::close(fd);
830}
831#endif
832
833status_t CameraService::Client::autoFocus()
834{
835 LOGV("autoFocus (pid %d)", getCallingPid());
836
837 Mutex::Autolock lock(mLock);
838 status_t result = checkPid();
839 if (result != NO_ERROR) return result;
840
841 if (mHardware == 0) {
842 LOGE("mHardware is NULL, returning.");
843 return INVALID_OPERATION;
844 }
845
846 return mHardware->autoFocus();
847}
848
849status_t CameraService::Client::cancelAutoFocus()
850{
851 LOGV("cancelAutoFocus (pid %d)", getCallingPid());
852
853 Mutex::Autolock lock(mLock);
854 status_t result = checkPid();
855 if (result != NO_ERROR) return result;
856
857 if (mHardware == 0) {
858 LOGE("mHardware is NULL, returning.");
859 return INVALID_OPERATION;
860 }
861
862 return mHardware->cancelAutoFocus();
863}
864
865// take a picture - image is returned in callback
866status_t CameraService::Client::takePicture()
867{
868 LOGV("takePicture (pid %d)", getCallingPid());
869
870 Mutex::Autolock lock(mLock);
871 status_t result = checkPid();
872 if (result != NO_ERROR) return result;
873
874 if (mHardware == 0) {
875 LOGE("mHardware is NULL, returning.");
876 return INVALID_OPERATION;
877 }
878
879 mHardware->enableMsgType(CAMERA_MSG_SHUTTER |
880 CAMERA_MSG_POSTVIEW_FRAME |
881 CAMERA_MSG_RAW_IMAGE |
882 CAMERA_MSG_COMPRESSED_IMAGE);
883
884 return mHardware->takePicture();
885}
886
887// snapshot taken
888void CameraService::Client::handleShutter(
889 image_rect_type *size // The width and height of yuv picture for
890 // registerBuffer. If this is NULL, use the picture
891 // size from parameters.
892)
893{
894 // Play shutter sound.
895 if (mMediaPlayerClick.get() != NULL) {
896 // do not play shutter sound if stream volume is 0
897 // (typically because ringer mode is silent).
898 int index;
899 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
900 if (index != 0) {
901 mMediaPlayerClick->seekTo(0);
902 mMediaPlayerClick->start();
903 }
904 }
905
906 // Screen goes black after the buffer is unregistered.
907 if (mSurface != 0 && !mUseOverlay) {
908 mSurface->unregisterBuffers();
909 }
910
911 sp<ICameraClient> c = mCameraClient;
912 if (c != NULL) {
913 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
914 }
915 mHardware->disableMsgType(CAMERA_MSG_SHUTTER);
916
917 // It takes some time before yuvPicture callback to be called.
918 // Register the buffer for raw image here to reduce latency.
919 if (mSurface != 0 && !mUseOverlay) {
920 int w, h;
921 CameraParameters params(mHardware->getParameters());
922 if (size == NULL) {
923 params.getPictureSize(&w, &h);
924 } else {
925 w = size->width;
926 h = size->height;
927 w &= ~1;
928 h &= ~1;
929 LOGV("Snapshot image width=%d, height=%d", w, h);
930 }
931 // FIXME: don't use hardcoded format constants here
932 ISurface::BufferHeap buffers(w, h, w, h,
933 HAL_PIXEL_FORMAT_YCrCb_420_SP, mOrientation, 0,
934 mHardware->getRawHeap());
935
936 mSurface->registerBuffers(buffers);
937 }
938}
939
940// preview callback - frame buffer update
941void CameraService::Client::handlePreviewData(const sp<IMemory>& mem)
942{
943 ssize_t offset;
944 size_t size;
945 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
946
947#if DEBUG_HEAP_LEAKS && 0 // debugging
948 if (gWeakHeap == NULL) {
949 if (gWeakHeap != heap) {
950 LOGV("SETTING PREVIEW HEAP");
951 heap->trackMe(true, true);
952 gWeakHeap = heap;
953 }
954 }
955#endif
956#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
957 {
958 if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) {
959 dump_to_file("/data/preview.yuv",
960 (uint8_t *)heap->base() + offset, size);
961 }
962 }
963#endif
964
965 if (!mUseOverlay)
966 {
967 Mutex::Autolock surfaceLock(mSurfaceLock);
968 if (mSurface != NULL) {
969 mSurface->postBuffer(offset);
970 }
971 }
972
973 // local copy of the callback flags
974 int flags = mPreviewCallbackFlag;
975
976 // is callback enabled?
977 if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
978 // If the enable bit is off, the copy-out and one-shot bits are ignored
979 LOGV("frame callback is diabled");
980 return;
981 }
982
983 // hold a strong pointer to the client
984 sp<ICameraClient> c = mCameraClient;
985
986 // clear callback flags if no client or one-shot mode
987 if ((c == NULL) || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
988 LOGV("Disable preview callback");
989 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
990 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
991 FRAME_CALLBACK_FLAG_ENABLE_MASK);
992 // TODO: Shouldn't we use this API for non-overlay hardware as well?
993 if (mUseOverlay)
994 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
995 }
996
997 // Is the received frame copied out or not?
998 if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
999 LOGV("frame is copied");
1000 copyFrameAndPostCopiedFrame(c, heap, offset, size);
1001 } else {
1002 LOGV("frame is forwarded");
1003 c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
1004 }
1005}
1006
1007// picture callback - postview image ready
1008void CameraService::Client::handlePostview(const sp<IMemory>& mem)
1009{
1010#if DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE // for testing pursposes only
1011 {
1012 ssize_t offset;
1013 size_t size;
1014 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1015 dump_to_file("/data/postview.yuv",
1016 (uint8_t *)heap->base() + offset, size);
1017 }
1018#endif
1019
1020 sp<ICameraClient> c = mCameraClient;
1021 if (c != NULL) {
1022 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
1023 }
1024 mHardware->disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1025}
1026
1027// picture callback - raw image ready
1028void CameraService::Client::handleRawPicture(const sp<IMemory>& mem)
1029{
1030 ssize_t offset;
1031 size_t size;
1032 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1033#if DEBUG_HEAP_LEAKS && 0 // debugging
1034 gWeakHeap = heap; // debugging
1035#endif
1036
1037 //LOGV("handleRawPicture(%d, %d)", offset, size);
1038#if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only
1039 dump_to_file("/data/photo.yuv",
1040 (uint8_t *)heap->base() + offset, size);
1041#endif
1042
1043 // Put the YUV version of the snapshot in the preview display.
1044 if (mSurface != 0 && !mUseOverlay) {
1045 mSurface->postBuffer(offset);
1046 }
1047
1048 sp<ICameraClient> c = mCameraClient;
1049 if (c != NULL) {
1050 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
1051 }
1052 mHardware->disableMsgType(CAMERA_MSG_RAW_IMAGE);
1053}
1054
1055// picture callback - compressed picture ready
1056void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem)
1057{
1058#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only
1059 {
1060 ssize_t offset;
1061 size_t size;
1062 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1063 dump_to_file("/data/photo.jpg",
1064 (uint8_t *)heap->base() + offset, size);
1065 }
1066#endif
1067
1068 sp<ICameraClient> c = mCameraClient;
1069 if (c != NULL) {
1070 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
1071 }
1072 mHardware->disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1073}
1074
1075void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user)
1076{
1077 LOGV("notifyCallback(%d)", msgType);
1078
1079 sp<Client> client = getClientFromCookie(user);
1080 if (client == 0) {
1081 return;
1082 }
1083
1084 switch (msgType) {
1085 case CAMERA_MSG_SHUTTER:
1086 // ext1 is the dimension of the yuv picture.
1087 client->handleShutter((image_rect_type *)ext1);
1088 break;
1089 default:
1090 sp<ICameraClient> c = client->mCameraClient;
1091 if (c != NULL) {
1092 c->notifyCallback(msgType, ext1, ext2);
1093 }
1094 break;
1095 }
1096
1097#if DEBUG_CLIENT_REFERENCES
1098 if (client->getStrongCount() == 1) {
1099 LOGE("++++++++++++++++ (NOTIFY CALLBACK) THIS WILL CAUSE A LOCKUP!");
1100 client->printRefs();
1101 }
1102#endif
1103}
1104
1105void CameraService::Client::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user)
1106{
1107 LOGV("dataCallback(%d)", msgType);
1108
1109 sp<Client> client = getClientFromCookie(user);
1110 if (client == 0) {
1111 return;
1112 }
1113
1114 sp<ICameraClient> c = client->mCameraClient;
1115 if (dataPtr == NULL) {
1116 LOGE("Null data returned in data callback");
1117 if (c != NULL) {
1118 c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1119 c->dataCallback(msgType, NULL);
1120 }
1121 return;
1122 }
1123
1124 switch (msgType) {
1125 case CAMERA_MSG_PREVIEW_FRAME:
1126 client->handlePreviewData(dataPtr);
1127 break;
1128 case CAMERA_MSG_POSTVIEW_FRAME:
1129 client->handlePostview(dataPtr);
1130 break;
1131 case CAMERA_MSG_RAW_IMAGE:
1132 client->handleRawPicture(dataPtr);
1133 break;
1134 case CAMERA_MSG_COMPRESSED_IMAGE:
1135 client->handleCompressedPicture(dataPtr);
1136 break;
1137 default:
1138 if (c != NULL) {
1139 c->dataCallback(msgType, dataPtr);
1140 }
1141 break;
1142 }
1143
1144#if DEBUG_CLIENT_REFERENCES
1145 if (client->getStrongCount() == 1) {
1146 LOGE("++++++++++++++++ (DATA CALLBACK) THIS WILL CAUSE A LOCKUP!");
1147 client->printRefs();
1148 }
1149#endif
1150}
1151
1152void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType,
1153 const sp<IMemory>& dataPtr, void* user)
1154{
1155 LOGV("dataCallbackTimestamp(%d)", msgType);
1156
1157 sp<Client> client = getClientFromCookie(user);
1158 if (client == 0) {
1159 return;
1160 }
1161 sp<ICameraClient> c = client->mCameraClient;
1162
1163 if (dataPtr == NULL) {
1164 LOGE("Null data returned in data with timestamp callback");
1165 if (c != NULL) {
1166 c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1167 c->dataCallbackTimestamp(0, msgType, NULL);
1168 }
1169 return;
1170 }
1171
1172 if (c != NULL) {
1173 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1174 }
1175
1176#if DEBUG_CLIENT_REFERENCES
1177 if (client->getStrongCount() == 1) {
1178 LOGE("++++++++++++++++ (DATA CALLBACK TIMESTAMP) THIS WILL CAUSE A LOCKUP!");
1179 client->printRefs();
1180 }
1181#endif
1182}
1183
1184// set preview/capture parameters - key/value pairs
1185status_t CameraService::Client::setParameters(const String8& params)
1186{
1187 LOGV("setParameters(%s)", params.string());
1188
1189 Mutex::Autolock lock(mLock);
1190 status_t result = checkPid();
1191 if (result != NO_ERROR) return result;
1192
1193 if (mHardware == 0) {
1194 LOGE("mHardware is NULL, returning.");
1195 return INVALID_OPERATION;
1196 }
1197
1198 CameraParameters p(params);
1199
1200 return mHardware->setParameters(p);
1201}
1202
1203// get preview/capture parameters - key/value pairs
1204String8 CameraService::Client::getParameters() const
1205{
1206 Mutex::Autolock lock(mLock);
1207
1208 if (mHardware == 0) {
1209 LOGE("mHardware is NULL, returning.");
1210 return String8();
1211 }
1212
1213 String8 params(mHardware->getParameters().flatten());
1214 LOGV("getParameters(%s)", params.string());
1215 return params;
1216}
1217
1218status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
1219{
1220 LOGV("sendCommand (pid %d)", getCallingPid());
1221 Mutex::Autolock lock(mLock);
1222 status_t result = checkPid();
1223 if (result != NO_ERROR) return result;
1224
1225 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
1226 // The orientation cannot be set during preview.
1227 if (mHardware->previewEnabled()) {
1228 return INVALID_OPERATION;
1229 }
1230 switch (arg1) {
1231 case 0:
1232 mOrientation = ISurface::BufferHeap::ROT_0;
1233 break;
1234 case 90:
1235 mOrientation = ISurface::BufferHeap::ROT_90;
1236 break;
1237 case 180:
1238 mOrientation = ISurface::BufferHeap::ROT_180;
1239 break;
1240 case 270:
1241 mOrientation = ISurface::BufferHeap::ROT_270;
1242 break;
1243 default:
1244 return BAD_VALUE;
1245 }
1246 return OK;
1247 }
1248
1249 if (mHardware == 0) {
1250 LOGE("mHardware is NULL, returning.");
1251 return INVALID_OPERATION;
1252 }
1253
1254 return mHardware->sendCommand(cmd, arg1, arg2);
1255}
1256
1257void CameraService::Client::copyFrameAndPostCopiedFrame(const sp<ICameraClient>& client,
1258 const sp<IMemoryHeap>& heap, size_t offset, size_t size)
1259{
1260 LOGV("copyFrameAndPostCopiedFrame");
1261 // It is necessary to copy out of pmem before sending this to
1262 // the callback. For efficiency, reuse the same MemoryHeapBase
1263 // provided it's big enough. Don't allocate the memory or
1264 // perform the copy if there's no callback.
1265
1266 // hold the preview lock while we grab a reference to the preview buffer
1267 sp<MemoryHeapBase> previewBuffer;
1268 {
1269 Mutex::Autolock lock(mPreviewLock);
1270 if (mPreviewBuffer == 0) {
1271 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1272 } else if (size > mPreviewBuffer->virtualSize()) {
1273 mPreviewBuffer.clear();
1274 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1275 }
1276 if (mPreviewBuffer == 0) {
1277 LOGE("failed to allocate space for preview buffer");
1278 return;
1279 }
1280 previewBuffer = mPreviewBuffer;
1281 }
1282 memcpy(previewBuffer->base(),
1283 (uint8_t *)heap->base() + offset, size);
1284
1285 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1286 if (frame == 0) {
1287 LOGE("failed to allocate space for frame callback");
1288 return;
1289 }
1290 client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
1291}
1292
1293static const int kDumpLockRetries = 50;
1294static const int kDumpLockSleep = 60000;
1295
1296static bool tryLock(Mutex& mutex)
1297{
1298 bool locked = false;
1299 for (int i = 0; i < kDumpLockRetries; ++i) {
1300 if (mutex.tryLock() == NO_ERROR) {
1301 locked = true;
1302 break;
1303 }
1304 usleep(kDumpLockSleep);
1305 }
1306 return locked;
1307}
1308
1309status_t CameraService::dump(int fd, const Vector<String16>& args)
1310{
1311 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1312
1313 const size_t SIZE = 256;
1314 char buffer[SIZE];
1315 String8 result;
1316 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1317 snprintf(buffer, SIZE, "Permission Denial: "
1318 "can't dump CameraService from pid=%d, uid=%d\n",
1319 getCallingPid(),
1320 IPCThreadState::self()->getCallingUid());
1321 result.append(buffer);
1322 write(fd, result.string(), result.size());
1323 } else {
1324 bool locked = tryLock(mServiceLock);
1325 // failed to lock - CameraService is probably deadlocked
1326 if (!locked) {
1327 String8 result(kDeadlockedString);
1328 write(fd, result.string(), result.size());
1329 }
1330
1331 if (mClient != 0) {
1332 sp<Client> currentClient = mClient.promote();
1333 sprintf(buffer, "Client (%p) PID: %d\n",
1334 currentClient->getCameraClient()->asBinder().get(),
1335 currentClient->mClientPid);
1336 result.append(buffer);
1337 write(fd, result.string(), result.size());
1338 currentClient->mHardware->dump(fd, args);
1339 } else {
1340 result.append("No camera client yet.\n");
1341 write(fd, result.string(), result.size());
1342 }
1343
1344 if (locked) mServiceLock.unlock();
1345 }
1346 return NO_ERROR;
1347}
1348
1349
1350status_t CameraService::onTransact(
1351 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1352{
1353 // permission checks...
1354 switch (code) {
1355 case BnCameraService::CONNECT:
1356 IPCThreadState* ipc = IPCThreadState::self();
1357 const int pid = ipc->getCallingPid();
1358 const int self_pid = getpid();
1359 if (pid != self_pid) {
1360 // we're called from a different process, do the real check
1361 if (!checkCallingPermission(
1362 String16("android.permission.CAMERA")))
1363 {
1364 const int uid = ipc->getCallingUid();
1365 LOGE("Permission Denial: "
1366 "can't use the camera pid=%d, uid=%d", pid, uid);
1367 return PERMISSION_DENIED;
1368 }
1369 }
1370 break;
1371 }
1372
1373 status_t err = BnCameraService::onTransact(code, data, reply, flags);
1374
1375#if DEBUG_HEAP_LEAKS
1376 LOGV("+++ onTransact err %d code %d", err, code);
1377
1378 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
1379 // the 'service' command interrogates this binder for its name, and then supplies it
1380 // even for the debugging commands. that means we need to check for it here, using
1381 // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to
1382 // BnSurfaceComposer before falling through to this code).
1383
1384 LOGV("+++ onTransact code %d", code);
1385
1386 CHECK_INTERFACE(ICameraService, data, reply);
1387
1388 switch(code) {
1389 case 1000:
1390 {
1391 if (gWeakHeap != 0) {
1392 sp<IMemoryHeap> h = gWeakHeap.promote();
1393 IMemoryHeap *p = gWeakHeap.unsafe_get();
1394 LOGV("CHECKING WEAK REFERENCE %p (%p)", h.get(), p);
1395 if (h != 0)
1396 h->printRefs();
1397 bool attempt_to_delete = data.readInt32() == 1;
1398 if (attempt_to_delete) {
1399 // NOT SAFE!
1400 LOGV("DELETING WEAK REFERENCE %p (%p)", h.get(), p);
1401 if (p) delete p;
1402 }
1403 return NO_ERROR;
1404 }
1405 }
1406 break;
1407 default:
1408 break;
1409 }
1410 }
1411#endif // DEBUG_HEAP_LEAKS
1412
1413 return err;
1414}
1415
1416}; // namespace android