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