1/*
2**
3** Copyright (C) 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "CameraService"
19
20#include <stdio.h>
21#include <sys/types.h>
22#include <pthread.h>
23
24#include <binder/IPCThreadState.h>
25#include <binder/IServiceManager.h>
26#include <binder/MemoryBase.h>
27#include <binder/MemoryHeapBase.h>
28#include <cutils/atomic.h>
29#include <hardware/hardware.h>
30#include <media/AudioSystem.h>
31#include <media/mediaplayer.h>
32#include <surfaceflinger/ISurface.h>
33#include <ui/Overlay.h>
34#include <utils/Errors.h>
35#include <utils/Log.h>
36#include <utils/String16.h>
37
38#include "CameraService.h"
39
40namespace android {
41
42// ----------------------------------------------------------------------------
43// Logging support -- this is for debugging only
44// Use "adb shell dumpsys media.camera -v 1" to change it.
45static volatile int32_t gLogLevel = 0;
46
47#define LOG1(...) LOGD_IF(gLogLevel >= 1, __VA_ARGS__);
48#define LOG2(...) LOGD_IF(gLogLevel >= 2, __VA_ARGS__);
49
50static void setLogLevel(int level) {
51    android_atomic_write(level, &gLogLevel);
52}
53
54// ----------------------------------------------------------------------------
55
56static int getCallingPid() {
57    return IPCThreadState::self()->getCallingPid();
58}
59
60static int getCallingUid() {
61    return IPCThreadState::self()->getCallingUid();
62}
63
64// ----------------------------------------------------------------------------
65
66// This is ugly and only safe if we never re-create the CameraService, but
67// should be ok for now.
68static CameraService *gCameraService;
69
70CameraService::CameraService()
71:mSoundRef(0)
72{
73    LOGI("CameraService started (pid=%d)", getpid());
74
75    mNumberOfCameras = HAL_getNumberOfCameras();
76    if (mNumberOfCameras > MAX_CAMERAS) {
77        LOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
78             mNumberOfCameras, MAX_CAMERAS);
79        mNumberOfCameras = MAX_CAMERAS;
80    }
81
82    for (int i = 0; i < mNumberOfCameras; i++) {
83        setCameraFree(i);
84    }
85
86    gCameraService = this;
87}
88
89CameraService::~CameraService() {
90    for (int i = 0; i < mNumberOfCameras; i++) {
91        if (mBusy[i]) {
92            LOGE("camera %d is still in use in destructor!", i);
93        }
94    }
95
96    gCameraService = NULL;
97}
98
99int32_t CameraService::getNumberOfCameras() {
100    return mNumberOfCameras;
101}
102
103status_t CameraService::getCameraInfo(int cameraId,
104                                      struct CameraInfo* cameraInfo) {
105    if (cameraId < 0 || cameraId >= mNumberOfCameras) {
106        return BAD_VALUE;
107    }
108
109    HAL_getCameraInfo(cameraId, cameraInfo);
110    return OK;
111}
112
113sp<ICamera> CameraService::connect(
114        const sp<ICameraClient>& cameraClient, int cameraId) {
115    int callingPid = getCallingPid();
116    LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId);
117
118    sp<Client> client;
119    if (cameraId < 0 || cameraId >= mNumberOfCameras) {
120        LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
121            callingPid, cameraId);
122        return NULL;
123    }
124
125    Mutex::Autolock lock(mServiceLock);
126    if (mClient[cameraId] != 0) {
127        client = mClient[cameraId].promote();
128        if (client != 0) {
129            if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
130                LOG1("CameraService::connect X (pid %d) (the same client)",
131                    callingPid);
132                return client;
133            } else {
134                LOGW("CameraService::connect X (pid %d) rejected (existing client).",
135                    callingPid);
136                return NULL;
137            }
138        }
139        mClient[cameraId].clear();
140    }
141
142    if (mBusy[cameraId]) {
143        LOGW("CameraService::connect X (pid %d) rejected"
144             " (camera %d is still busy).", callingPid, cameraId);
145        return NULL;
146    }
147
148    sp<CameraHardwareInterface> hardware = HAL_openCameraHardware(cameraId);
149    if (hardware == NULL) {
150        LOGE("Fail to open camera hardware (id=%d)", cameraId);
151        return NULL;
152    }
153    CameraInfo info;
154    HAL_getCameraInfo(cameraId, &info);
155    client = new Client(this, cameraClient, hardware, cameraId, info.facing,
156                        callingPid);
157    mClient[cameraId] = client;
158    LOG1("CameraService::connect X");
159    return client;
160}
161
162void CameraService::removeClient(const sp<ICameraClient>& cameraClient) {
163    int callingPid = getCallingPid();
164    LOG1("CameraService::removeClient E (pid %d)", callingPid);
165
166    for (int i = 0; i < mNumberOfCameras; i++) {
167        // Declare this before the lock to make absolutely sure the
168        // destructor won't be called with the lock held.
169        sp<Client> client;
170
171        Mutex::Autolock lock(mServiceLock);
172
173        // This happens when we have already disconnected (or this is
174        // just another unused camera).
175        if (mClient[i] == 0) continue;
176
177        // Promote mClient. It can fail if we are called from this path:
178        // Client::~Client() -> disconnect() -> removeClient().
179        client = mClient[i].promote();
180
181        if (client == 0) {
182            mClient[i].clear();
183            continue;
184        }
185
186        if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
187            // Found our camera, clear and leave.
188            LOG1("removeClient: clear camera %d", i);
189            mClient[i].clear();
190            break;
191        }
192    }
193
194    LOG1("CameraService::removeClient X (pid %d)", callingPid);
195}
196
197sp<CameraService::Client> CameraService::getClientById(int cameraId) {
198    if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
199    return mClient[cameraId].promote();
200}
201
202status_t CameraService::onTransact(
203    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
204    // Permission checks
205    switch (code) {
206        case BnCameraService::CONNECT:
207            const int pid = getCallingPid();
208            const int self_pid = getpid();
209            if (pid != self_pid) {
210                // we're called from a different process, do the real check
211                if (!checkCallingPermission(
212                        String16("android.permission.CAMERA"))) {
213                    const int uid = getCallingUid();
214                    LOGE("Permission Denial: "
215                         "can't use the camera pid=%d, uid=%d", pid, uid);
216                    return PERMISSION_DENIED;
217                }
218            }
219            break;
220    }
221
222    return BnCameraService::onTransact(code, data, reply, flags);
223}
224
225// The reason we need this busy bit is a new CameraService::connect() request
226// may come in while the previous Client's destructor has not been run or is
227// still running. If the last strong reference of the previous Client is gone
228// but the destructor has not been finished, we should not allow the new Client
229// to be created because we need to wait for the previous Client to tear down
230// the hardware first.
231void CameraService::setCameraBusy(int cameraId) {
232    android_atomic_write(1, &mBusy[cameraId]);
233}
234
235void CameraService::setCameraFree(int cameraId) {
236    android_atomic_write(0, &mBusy[cameraId]);
237}
238
239// We share the media players for shutter and recording sound for all clients.
240// A reference count is kept to determine when we will actually release the
241// media players.
242
243static MediaPlayer* newMediaPlayer(const char *file) {
244    MediaPlayer* mp = new MediaPlayer();
245    if (mp->setDataSource(file, NULL) == NO_ERROR) {
246        mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
247        mp->prepare();
248    } else {
249        LOGE("Failed to load CameraService sounds: %s", file);
250        return NULL;
251    }
252    return mp;
253}
254
255void CameraService::loadSound() {
256    Mutex::Autolock lock(mSoundLock);
257    LOG1("CameraService::loadSound ref=%d", mSoundRef);
258    if (mSoundRef++) return;
259
260    mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
261    mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
262}
263
264void CameraService::releaseSound() {
265    Mutex::Autolock lock(mSoundLock);
266    LOG1("CameraService::releaseSound ref=%d", mSoundRef);
267    if (--mSoundRef) return;
268
269    for (int i = 0; i < NUM_SOUNDS; i++) {
270        if (mSoundPlayer[i] != 0) {
271            mSoundPlayer[i]->disconnect();
272            mSoundPlayer[i].clear();
273        }
274    }
275}
276
277void CameraService::playSound(sound_kind kind) {
278    LOG1("playSound(%d)", kind);
279    Mutex::Autolock lock(mSoundLock);
280    sp<MediaPlayer> player = mSoundPlayer[kind];
281    if (player != 0) {
282        // do not play the sound if stream volume is 0
283        // (typically because ringer mode is silent).
284        int index;
285        AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
286        if (index != 0) {
287            player->seekTo(0);
288            player->start();
289        }
290    }
291}
292
293// ----------------------------------------------------------------------------
294
295CameraService::Client::Client(const sp<CameraService>& cameraService,
296        const sp<ICameraClient>& cameraClient,
297        const sp<CameraHardwareInterface>& hardware,
298        int cameraId, int cameraFacing, int clientPid) {
299    int callingPid = getCallingPid();
300    LOG1("Client::Client E (pid %d)", callingPid);
301
302    mCameraService = cameraService;
303    mCameraClient = cameraClient;
304    mHardware = hardware;
305    mCameraId = cameraId;
306    mCameraFacing = cameraFacing;
307    mClientPid = clientPid;
308    mUseOverlay = mHardware->useOverlay();
309    mMsgEnabled = 0;
310
311    mHardware->setCallbacks(notifyCallback,
312                            dataCallback,
313                            dataCallbackTimestamp,
314                            (void *)cameraId);
315
316    // Enable zoom, error, and focus messages by default
317    enableMsgType(CAMERA_MSG_ERROR |
318                  CAMERA_MSG_ZOOM |
319                  CAMERA_MSG_FOCUS);
320    mOverlayW = 0;
321    mOverlayH = 0;
322
323    // Callback is disabled by default
324    mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
325    mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
326    mOrientationChanged = false;
327    cameraService->setCameraBusy(cameraId);
328    cameraService->loadSound();
329    LOG1("Client::Client X (pid %d)", callingPid);
330}
331
332static void *unregister_surface(void *arg) {
333    ISurface *surface = (ISurface *)arg;
334    surface->unregisterBuffers();
335    IPCThreadState::self()->flushCommands();
336    return NULL;
337}
338
339// tear down the client
340CameraService::Client::~Client() {
341    int callingPid = getCallingPid();
342    LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
343
344    if (mSurface != 0 && !mUseOverlay) {
345        pthread_t thr;
346        // We unregister the buffers in a different thread because binder does
347        // not let us make sychronous transactions in a binder destructor (that
348        // is, upon our reaching a refcount of zero.)
349        pthread_create(&thr,
350                       NULL,  // attr
351                       unregister_surface,
352                       mSurface.get());
353        pthread_join(thr, NULL);
354    }
355
356    // set mClientPid to let disconnet() tear down the hardware
357    mClientPid = callingPid;
358    disconnect();
359    mCameraService->releaseSound();
360    LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
361}
362
363// ----------------------------------------------------------------------------
364
365status_t CameraService::Client::checkPid() const {
366    int callingPid = getCallingPid();
367    if (callingPid == mClientPid) return NO_ERROR;
368
369    LOGW("attempt to use a locked camera from a different process"
370         " (old pid %d, new pid %d)", mClientPid, callingPid);
371    return EBUSY;
372}
373
374status_t CameraService::Client::checkPidAndHardware() const {
375    status_t result = checkPid();
376    if (result != NO_ERROR) return result;
377    if (mHardware == 0) {
378        LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
379        return INVALID_OPERATION;
380    }
381    return NO_ERROR;
382}
383
384status_t CameraService::Client::lock() {
385    int callingPid = getCallingPid();
386    LOG1("lock (pid %d)", callingPid);
387    Mutex::Autolock lock(mLock);
388
389    // lock camera to this client if the the camera is unlocked
390    if (mClientPid == 0) {
391        mClientPid = callingPid;
392        return NO_ERROR;
393    }
394
395    // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
396    return checkPid();
397}
398
399status_t CameraService::Client::unlock() {
400    int callingPid = getCallingPid();
401    LOG1("unlock (pid %d)", callingPid);
402    Mutex::Autolock lock(mLock);
403
404    // allow anyone to use camera (after they lock the camera)
405    status_t result = checkPid();
406    if (result == NO_ERROR) {
407        mClientPid = 0;
408        LOG1("clear mCameraClient (pid %d)", callingPid);
409        // we need to remove the reference to ICameraClient so that when the app
410        // goes away, the reference count goes to 0.
411        mCameraClient.clear();
412    }
413    return result;
414}
415
416// connect a new client to the camera
417status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
418    int callingPid = getCallingPid();
419    LOG1("connect E (pid %d)", callingPid);
420    Mutex::Autolock lock(mLock);
421
422    if (mClientPid != 0 && checkPid() != NO_ERROR) {
423        LOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
424                mClientPid, callingPid);
425        return EBUSY;
426    }
427
428    if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
429        LOG1("Connect to the same client");
430        return NO_ERROR;
431    }
432
433    mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
434    mClientPid = callingPid;
435    mCameraClient = client;
436
437    LOG1("connect X (pid %d)", callingPid);
438    return NO_ERROR;
439}
440
441void CameraService::Client::disconnect() {
442    int callingPid = getCallingPid();
443    LOG1("disconnect E (pid %d)", callingPid);
444    Mutex::Autolock lock(mLock);
445
446    if (checkPid() != NO_ERROR) {
447        LOGW("different client - don't disconnect");
448        return;
449    }
450
451    if (mClientPid <= 0) {
452        LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
453        return;
454    }
455
456    // Make sure disconnect() is done once and once only, whether it is called
457    // from the user directly, or called by the destructor.
458    if (mHardware == 0) return;
459
460    LOG1("hardware teardown");
461    // Before destroying mHardware, we must make sure it's in the
462    // idle state.
463    // Turn off all messages.
464    disableMsgType(CAMERA_MSG_ALL_MSGS);
465    mHardware->stopPreview();
466    mHardware->cancelPicture();
467    // Release the hardware resources.
468    mHardware->release();
469    // Release the held overlay resources.
470    if (mUseOverlay) {
471        mOverlayRef = 0;
472    }
473    mHardware.clear();
474
475    mCameraService->removeClient(mCameraClient);
476    mCameraService->setCameraFree(mCameraId);
477
478    LOG1("disconnect X (pid %d)", callingPid);
479}
480
481// ----------------------------------------------------------------------------
482
483// set the ISurface that the preview will use
484status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface) {
485    LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
486    Mutex::Autolock lock(mLock);
487    status_t result = checkPidAndHardware();
488    if (result != NO_ERROR) return result;
489
490    result = NO_ERROR;
491
492    // return if no change in surface.
493    // asBinder() is safe on NULL (returns NULL)
494    if (surface->asBinder() == mSurface->asBinder()) {
495        return result;
496    }
497
498    if (mSurface != 0) {
499        LOG1("clearing old preview surface %p", mSurface.get());
500        if (mUseOverlay) {
501            // Force the destruction of any previous overlay
502            sp<Overlay> dummy;
503            mHardware->setOverlay(dummy);
504            mOverlayRef = 0;
505        } else {
506            mSurface->unregisterBuffers();
507        }
508    }
509    mSurface = surface;
510    mOverlayRef = 0;
511    // If preview has been already started, set overlay or register preview
512    // buffers now.
513    if (mHardware->previewEnabled()) {
514        if (mUseOverlay) {
515            result = setOverlay();
516        } else if (mSurface != 0) {
517            result = registerPreviewBuffers();
518        }
519    }
520
521    return result;
522}
523
524status_t CameraService::Client::registerPreviewBuffers() {
525    int w, h;
526    CameraParameters params(mHardware->getParameters());
527    params.getPreviewSize(&w, &h);
528
529    // FIXME: don't use a hardcoded format here.
530    ISurface::BufferHeap buffers(w, h, w, h,
531                                 HAL_PIXEL_FORMAT_YCrCb_420_SP,
532                                 mOrientation,
533                                 0,
534                                 mHardware->getPreviewHeap());
535
536    status_t result = mSurface->registerBuffers(buffers);
537    if (result != NO_ERROR) {
538        LOGE("registerBuffers failed with status %d", result);
539    }
540    return result;
541}
542
543status_t CameraService::Client::setOverlay() {
544    int w, h;
545    CameraParameters params(mHardware->getParameters());
546    params.getPreviewSize(&w, &h);
547
548    if (w != mOverlayW || h != mOverlayH || mOrientationChanged) {
549        // Force the destruction of any previous overlay
550        sp<Overlay> dummy;
551        mHardware->setOverlay(dummy);
552        mOverlayRef = 0;
553        mOrientationChanged = false;
554    }
555
556    status_t result = NO_ERROR;
557    if (mSurface == 0) {
558        result = mHardware->setOverlay(NULL);
559    } else {
560        if (mOverlayRef == 0) {
561            // FIXME:
562            // Surfaceflinger may hold onto the previous overlay reference for some
563            // time after we try to destroy it. retry a few times. In the future, we
564            // should make the destroy call block, or possibly specify that we can
565            // wait in the createOverlay call if the previous overlay is in the
566            // process of being destroyed.
567            for (int retry = 0; retry < 50; ++retry) {
568                mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT,
569                                                      mOrientation);
570                if (mOverlayRef != 0) break;
571                LOGW("Overlay create failed - retrying");
572                usleep(20000);
573            }
574            if (mOverlayRef == 0) {
575                LOGE("Overlay Creation Failed!");
576                return -EINVAL;
577            }
578            result = mHardware->setOverlay(new Overlay(mOverlayRef));
579        }
580    }
581    if (result != NO_ERROR) {
582        LOGE("mHardware->setOverlay() failed with status %d\n", result);
583        return result;
584    }
585
586    mOverlayW = w;
587    mOverlayH = h;
588
589    return result;
590}
591
592// set the preview callback flag to affect how the received frames from
593// preview are handled.
594void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
595    LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
596    Mutex::Autolock lock(mLock);
597    if (checkPidAndHardware() != NO_ERROR) return;
598
599    mPreviewCallbackFlag = callback_flag;
600
601    // If we don't use overlay, we always need the preview frame for display.
602    // If we do use overlay, we only need the preview frame if the user
603    // wants the data.
604    if (mUseOverlay) {
605        if(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK) {
606            enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
607        } else {
608            disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
609        }
610    }
611}
612
613// start preview mode
614status_t CameraService::Client::startPreview() {
615    LOG1("startPreview (pid %d)", getCallingPid());
616    return startCameraMode(CAMERA_PREVIEW_MODE);
617}
618
619// start recording mode
620status_t CameraService::Client::startRecording() {
621    LOG1("startRecording (pid %d)", getCallingPid());
622    return startCameraMode(CAMERA_RECORDING_MODE);
623}
624
625// start preview or recording
626status_t CameraService::Client::startCameraMode(camera_mode mode) {
627    LOG1("startCameraMode(%d)", mode);
628    Mutex::Autolock lock(mLock);
629    status_t result = checkPidAndHardware();
630    if (result != NO_ERROR) return result;
631
632    switch(mode) {
633        case CAMERA_PREVIEW_MODE:
634            if (mSurface == 0) {
635                LOG1("mSurface is not set yet.");
636                // still able to start preview in this case.
637            }
638            return startPreviewMode();
639        case CAMERA_RECORDING_MODE:
640            if (mSurface == 0) {
641                LOGE("mSurface must be set before startRecordingMode.");
642                return INVALID_OPERATION;
643            }
644            return startRecordingMode();
645        default:
646            return UNKNOWN_ERROR;
647    }
648}
649
650status_t CameraService::Client::startPreviewMode() {
651    LOG1("startPreviewMode");
652    status_t result = NO_ERROR;
653
654    // if preview has been enabled, nothing needs to be done
655    if (mHardware->previewEnabled()) {
656        return NO_ERROR;
657    }
658
659    if (mUseOverlay) {
660        // If preview display has been set, set overlay now.
661        if (mSurface != 0) {
662            result = setOverlay();
663        }
664        if (result != NO_ERROR) return result;
665        result = mHardware->startPreview();
666    } else {
667        enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
668        result = mHardware->startPreview();
669        if (result != NO_ERROR) return result;
670        // If preview display has been set, register preview buffers now.
671        if (mSurface != 0) {
672           // Unregister here because the surface may be previously registered
673           // with the raw (snapshot) heap.
674           mSurface->unregisterBuffers();
675           result = registerPreviewBuffers();
676        }
677    }
678    return result;
679}
680
681status_t CameraService::Client::startRecordingMode() {
682    LOG1("startRecordingMode");
683    status_t result = NO_ERROR;
684
685    // if recording has been enabled, nothing needs to be done
686    if (mHardware->recordingEnabled()) {
687        return NO_ERROR;
688    }
689
690    // if preview has not been started, start preview first
691    if (!mHardware->previewEnabled()) {
692        result = startPreviewMode();
693        if (result != NO_ERROR) {
694            return result;
695        }
696    }
697
698    // start recording mode
699    enableMsgType(CAMERA_MSG_VIDEO_FRAME);
700    mCameraService->playSound(SOUND_RECORDING);
701    result = mHardware->startRecording();
702    if (result != NO_ERROR) {
703        LOGE("mHardware->startRecording() failed with status %d", result);
704    }
705    return result;
706}
707
708// stop preview mode
709void CameraService::Client::stopPreview() {
710    LOG1("stopPreview (pid %d)", getCallingPid());
711    Mutex::Autolock lock(mLock);
712    if (checkPidAndHardware() != NO_ERROR) return;
713
714    disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
715    mHardware->stopPreview();
716
717    if (mSurface != 0 && !mUseOverlay) {
718        mSurface->unregisterBuffers();
719    }
720
721    mPreviewBuffer.clear();
722}
723
724// stop recording mode
725void CameraService::Client::stopRecording() {
726    LOG1("stopRecording (pid %d)", getCallingPid());
727    Mutex::Autolock lock(mLock);
728    if (checkPidAndHardware() != NO_ERROR) return;
729
730    mCameraService->playSound(SOUND_RECORDING);
731    disableMsgType(CAMERA_MSG_VIDEO_FRAME);
732    mHardware->stopRecording();
733
734    mPreviewBuffer.clear();
735}
736
737// release a recording frame
738void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
739    Mutex::Autolock lock(mLock);
740    if (checkPidAndHardware() != NO_ERROR) return;
741    mHardware->releaseRecordingFrame(mem);
742}
743
744bool CameraService::Client::previewEnabled() {
745    LOG1("previewEnabled (pid %d)", getCallingPid());
746
747    Mutex::Autolock lock(mLock);
748    if (checkPidAndHardware() != NO_ERROR) return false;
749    return mHardware->previewEnabled();
750}
751
752bool CameraService::Client::recordingEnabled() {
753    LOG1("recordingEnabled (pid %d)", getCallingPid());
754
755    Mutex::Autolock lock(mLock);
756    if (checkPidAndHardware() != NO_ERROR) return false;
757    return mHardware->recordingEnabled();
758}
759
760status_t CameraService::Client::autoFocus() {
761    LOG1("autoFocus (pid %d)", getCallingPid());
762
763    Mutex::Autolock lock(mLock);
764    status_t result = checkPidAndHardware();
765    if (result != NO_ERROR) return result;
766
767    return mHardware->autoFocus();
768}
769
770status_t CameraService::Client::cancelAutoFocus() {
771    LOG1("cancelAutoFocus (pid %d)", getCallingPid());
772
773    Mutex::Autolock lock(mLock);
774    status_t result = checkPidAndHardware();
775    if (result != NO_ERROR) return result;
776
777    return mHardware->cancelAutoFocus();
778}
779
780// take a picture - image is returned in callback
781status_t CameraService::Client::takePicture() {
782    LOG1("takePicture (pid %d)", getCallingPid());
783
784    Mutex::Autolock lock(mLock);
785    status_t result = checkPidAndHardware();
786    if (result != NO_ERROR) return result;
787
788    enableMsgType(CAMERA_MSG_SHUTTER |
789                  CAMERA_MSG_POSTVIEW_FRAME |
790                  CAMERA_MSG_RAW_IMAGE |
791                  CAMERA_MSG_COMPRESSED_IMAGE);
792
793    return mHardware->takePicture();
794}
795
796// set preview/capture parameters - key/value pairs
797status_t CameraService::Client::setParameters(const String8& params) {
798    LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
799
800    Mutex::Autolock lock(mLock);
801    status_t result = checkPidAndHardware();
802    if (result != NO_ERROR) return result;
803
804    CameraParameters p(params);
805    return mHardware->setParameters(p);
806}
807
808// get preview/capture parameters - key/value pairs
809String8 CameraService::Client::getParameters() const {
810    Mutex::Autolock lock(mLock);
811    if (checkPidAndHardware() != NO_ERROR) return String8();
812
813    String8 params(mHardware->getParameters().flatten());
814    LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
815    return params;
816}
817
818status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
819    LOG1("sendCommand (pid %d)", getCallingPid());
820    int orientation;
821    Mutex::Autolock lock(mLock);
822    status_t result = checkPidAndHardware();
823    if (result != NO_ERROR) return result;
824
825    if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
826        // The orientation cannot be set during preview.
827        if (mHardware->previewEnabled()) {
828            return INVALID_OPERATION;
829        }
830        // Mirror the preview if the camera is front-facing.
831        orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
832        if (orientation == -1) return BAD_VALUE;
833
834        if (mOrientation != orientation) {
835            mOrientation = orientation;
836            if (mOverlayRef != 0) mOrientationChanged = true;
837        }
838        return OK;
839    }
840
841    return mHardware->sendCommand(cmd, arg1, arg2);
842}
843
844// ----------------------------------------------------------------------------
845
846void CameraService::Client::enableMsgType(int32_t msgType) {
847    android_atomic_or(msgType, &mMsgEnabled);
848    mHardware->enableMsgType(msgType);
849}
850
851void CameraService::Client::disableMsgType(int32_t msgType) {
852    android_atomic_and(~msgType, &mMsgEnabled);
853    mHardware->disableMsgType(msgType);
854}
855
856#define CHECK_MESSAGE_INTERVAL 10 // 10ms
857bool CameraService::Client::lockIfMessageWanted(int32_t msgType) {
858    int sleepCount = 0;
859    while (mMsgEnabled & msgType) {
860        if (mLock.tryLock() == NO_ERROR) {
861            if (sleepCount > 0) {
862                LOG1("lockIfMessageWanted(%d): waited for %d ms",
863                    msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
864            }
865            return true;
866        }
867        if (sleepCount++ == 0) {
868            LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
869        }
870        usleep(CHECK_MESSAGE_INTERVAL * 1000);
871    }
872    LOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
873    return false;
874}
875
876// ----------------------------------------------------------------------------
877
878// Converts from a raw pointer to the client to a strong pointer during a
879// hardware callback. This requires the callbacks only happen when the client
880// is still alive.
881sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
882    sp<Client> client = gCameraService->getClientById((int) user);
883
884    // This could happen if the Client is in the process of shutting down (the
885    // last strong reference is gone, but the destructor hasn't finished
886    // stopping the hardware).
887    if (client == 0) return NULL;
888
889    // The checks below are not necessary and are for debugging only.
890    if (client->mCameraService.get() != gCameraService) {
891        LOGE("mismatch service!");
892        return NULL;
893    }
894
895    if (client->mHardware == 0) {
896        LOGE("mHardware == 0: callback after disconnect()?");
897        return NULL;
898    }
899
900    return client;
901}
902
903// Callback messages can be dispatched to internal handlers or pass to our
904// client's callback functions, depending on the message type.
905//
906// notifyCallback:
907//      CAMERA_MSG_SHUTTER              handleShutter
908//      (others)                        c->notifyCallback
909// dataCallback:
910//      CAMERA_MSG_PREVIEW_FRAME        handlePreviewData
911//      CAMERA_MSG_POSTVIEW_FRAME       handlePostview
912//      CAMERA_MSG_RAW_IMAGE            handleRawPicture
913//      CAMERA_MSG_COMPRESSED_IMAGE     handleCompressedPicture
914//      (others)                        c->dataCallback
915// dataCallbackTimestamp
916//      (others)                        c->dataCallbackTimestamp
917//
918// NOTE: the *Callback functions grab mLock of the client before passing
919// control to handle* functions. So the handle* functions must release the
920// lock before calling the ICameraClient's callbacks, so those callbacks can
921// invoke methods in the Client class again (For example, the preview frame
922// callback may want to releaseRecordingFrame). The handle* functions must
923// release the lock after all accesses to member variables, so it must be
924// handled very carefully.
925
926void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,
927        int32_t ext2, void* user) {
928    LOG2("notifyCallback(%d)", msgType);
929
930    sp<Client> client = getClientFromCookie(user);
931    if (client == 0) return;
932    if (!client->lockIfMessageWanted(msgType)) return;
933
934    switch (msgType) {
935        case CAMERA_MSG_SHUTTER:
936            // ext1 is the dimension of the yuv picture.
937            client->handleShutter((image_rect_type *)ext1);
938            break;
939        default:
940            client->handleGenericNotify(msgType, ext1, ext2);
941            break;
942    }
943}
944
945void CameraService::Client::dataCallback(int32_t msgType,
946        const sp<IMemory>& dataPtr, void* user) {
947    LOG2("dataCallback(%d)", msgType);
948
949    sp<Client> client = getClientFromCookie(user);
950    if (client == 0) return;
951    if (!client->lockIfMessageWanted(msgType)) return;
952
953    if (dataPtr == 0) {
954        LOGE("Null data returned in data callback");
955        client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
956        return;
957    }
958
959    switch (msgType) {
960        case CAMERA_MSG_PREVIEW_FRAME:
961            client->handlePreviewData(dataPtr);
962            break;
963        case CAMERA_MSG_POSTVIEW_FRAME:
964            client->handlePostview(dataPtr);
965            break;
966        case CAMERA_MSG_RAW_IMAGE:
967            client->handleRawPicture(dataPtr);
968            break;
969        case CAMERA_MSG_COMPRESSED_IMAGE:
970            client->handleCompressedPicture(dataPtr);
971            break;
972        default:
973            client->handleGenericData(msgType, dataPtr);
974            break;
975    }
976}
977
978void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp,
979        int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
980    LOG2("dataCallbackTimestamp(%d)", msgType);
981
982    sp<Client> client = getClientFromCookie(user);
983    if (client == 0) return;
984    if (!client->lockIfMessageWanted(msgType)) return;
985
986    if (dataPtr == 0) {
987        LOGE("Null data returned in data with timestamp callback");
988        client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
989        return;
990    }
991
992    client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
993}
994
995// snapshot taken callback
996// "size" is the width and height of yuv picture for registerBuffer.
997// If it is NULL, use the picture size from parameters.
998void CameraService::Client::handleShutter(image_rect_type *size) {
999    mCameraService->playSound(SOUND_SHUTTER);
1000
1001    // Screen goes black after the buffer is unregistered.
1002    if (mSurface != 0 && !mUseOverlay) {
1003        mSurface->unregisterBuffers();
1004    }
1005
1006    sp<ICameraClient> c = mCameraClient;
1007    if (c != 0) {
1008        mLock.unlock();
1009        c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
1010        if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
1011    }
1012    disableMsgType(CAMERA_MSG_SHUTTER);
1013
1014    // It takes some time before yuvPicture callback to be called.
1015    // Register the buffer for raw image here to reduce latency.
1016    if (mSurface != 0 && !mUseOverlay) {
1017        int w, h;
1018        CameraParameters params(mHardware->getParameters());
1019        if (size == NULL) {
1020            params.getPictureSize(&w, &h);
1021        } else {
1022            w = size->width;
1023            h = size->height;
1024            w &= ~1;
1025            h &= ~1;
1026            LOG1("Snapshot image width=%d, height=%d", w, h);
1027        }
1028        // FIXME: don't use hardcoded format constants here
1029        ISurface::BufferHeap buffers(w, h, w, h,
1030            HAL_PIXEL_FORMAT_YCrCb_420_SP, mOrientation, 0,
1031            mHardware->getRawHeap());
1032
1033        mSurface->registerBuffers(buffers);
1034        IPCThreadState::self()->flushCommands();
1035    }
1036
1037    mLock.unlock();
1038}
1039
1040// preview callback - frame buffer update
1041void CameraService::Client::handlePreviewData(const sp<IMemory>& mem) {
1042    ssize_t offset;
1043    size_t size;
1044    sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1045
1046    if (!mUseOverlay) {
1047        if (mSurface != 0) {
1048            mSurface->postBuffer(offset);
1049        }
1050    }
1051
1052    // local copy of the callback flags
1053    int flags = mPreviewCallbackFlag;
1054
1055    // is callback enabled?
1056    if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
1057        // If the enable bit is off, the copy-out and one-shot bits are ignored
1058        LOG2("frame callback is disabled");
1059        mLock.unlock();
1060        return;
1061    }
1062
1063    // hold a strong pointer to the client
1064    sp<ICameraClient> c = mCameraClient;
1065
1066    // clear callback flags if no client or one-shot mode
1067    if (c == 0 || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
1068        LOG2("Disable preview callback");
1069        mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1070                                  FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1071                                  FRAME_CALLBACK_FLAG_ENABLE_MASK);
1072        if (mUseOverlay) {
1073            disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
1074        }
1075    }
1076
1077    if (c != 0) {
1078        // Is the received frame copied out or not?
1079        if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1080            LOG2("frame is copied");
1081            copyFrameAndPostCopiedFrame(c, heap, offset, size);
1082        } else {
1083            LOG2("frame is forwarded");
1084            mLock.unlock();
1085            c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
1086        }
1087    } else {
1088        mLock.unlock();
1089    }
1090}
1091
1092// picture callback - postview image ready
1093void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
1094    disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1095
1096    sp<ICameraClient> c = mCameraClient;
1097    mLock.unlock();
1098    if (c != 0) {
1099        c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
1100    }
1101}
1102
1103// picture callback - raw image ready
1104void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
1105    disableMsgType(CAMERA_MSG_RAW_IMAGE);
1106
1107    ssize_t offset;
1108    size_t size;
1109    sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1110
1111    // Put the YUV version of the snapshot in the preview display.
1112    if (mSurface != 0 && !mUseOverlay) {
1113        mSurface->postBuffer(offset);
1114    }
1115
1116    sp<ICameraClient> c = mCameraClient;
1117    mLock.unlock();
1118    if (c != 0) {
1119        c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
1120    }
1121}
1122
1123// picture callback - compressed picture ready
1124void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
1125    disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1126
1127    sp<ICameraClient> c = mCameraClient;
1128    mLock.unlock();
1129    if (c != 0) {
1130        c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
1131    }
1132}
1133
1134
1135void CameraService::Client::handleGenericNotify(int32_t msgType,
1136    int32_t ext1, int32_t ext2) {
1137    sp<ICameraClient> c = mCameraClient;
1138    mLock.unlock();
1139    if (c != 0) {
1140        c->notifyCallback(msgType, ext1, ext2);
1141    }
1142}
1143
1144void CameraService::Client::handleGenericData(int32_t msgType,
1145    const sp<IMemory>& dataPtr) {
1146    sp<ICameraClient> c = mCameraClient;
1147    mLock.unlock();
1148    if (c != 0) {
1149        c->dataCallback(msgType, dataPtr);
1150    }
1151}
1152
1153void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
1154    int32_t msgType, const sp<IMemory>& dataPtr) {
1155    sp<ICameraClient> c = mCameraClient;
1156    mLock.unlock();
1157    if (c != 0) {
1158        c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1159    }
1160}
1161
1162void CameraService::Client::copyFrameAndPostCopiedFrame(
1163        const sp<ICameraClient>& client, const sp<IMemoryHeap>& heap,
1164        size_t offset, size_t size) {
1165    LOG2("copyFrameAndPostCopiedFrame");
1166    // It is necessary to copy out of pmem before sending this to
1167    // the callback. For efficiency, reuse the same MemoryHeapBase
1168    // provided it's big enough. Don't allocate the memory or
1169    // perform the copy if there's no callback.
1170    // hold the preview lock while we grab a reference to the preview buffer
1171    sp<MemoryHeapBase> previewBuffer;
1172
1173    if (mPreviewBuffer == 0) {
1174        mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1175    } else if (size > mPreviewBuffer->virtualSize()) {
1176        mPreviewBuffer.clear();
1177        mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1178    }
1179    if (mPreviewBuffer == 0) {
1180        LOGE("failed to allocate space for preview buffer");
1181        mLock.unlock();
1182        return;
1183    }
1184    previewBuffer = mPreviewBuffer;
1185
1186    memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
1187
1188    sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1189    if (frame == 0) {
1190        LOGE("failed to allocate space for frame callback");
1191        mLock.unlock();
1192        return;
1193    }
1194
1195    mLock.unlock();
1196    client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
1197}
1198
1199int CameraService::Client::getOrientation(int degrees, bool mirror) {
1200    if (!mirror) {
1201        if (degrees == 0) return 0;
1202        else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1203        else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1204        else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1205    } else {  // Do mirror (horizontal flip)
1206        if (degrees == 0) {           // FLIP_H and ROT_0
1207            return HAL_TRANSFORM_FLIP_H;
1208        } else if (degrees == 90) {   // FLIP_H and ROT_90
1209            return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1210        } else if (degrees == 180) {  // FLIP_H and ROT_180
1211            return HAL_TRANSFORM_FLIP_V;
1212        } else if (degrees == 270) {  // FLIP_H and ROT_270
1213            return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1214        }
1215    }
1216    LOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1217    return -1;
1218}
1219
1220
1221// ----------------------------------------------------------------------------
1222
1223static const int kDumpLockRetries = 50;
1224static const int kDumpLockSleep = 60000;
1225
1226static bool tryLock(Mutex& mutex)
1227{
1228    bool locked = false;
1229    for (int i = 0; i < kDumpLockRetries; ++i) {
1230        if (mutex.tryLock() == NO_ERROR) {
1231            locked = true;
1232            break;
1233        }
1234        usleep(kDumpLockSleep);
1235    }
1236    return locked;
1237}
1238
1239status_t CameraService::dump(int fd, const Vector<String16>& args) {
1240    static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1241
1242    const size_t SIZE = 256;
1243    char buffer[SIZE];
1244    String8 result;
1245    if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1246        snprintf(buffer, SIZE, "Permission Denial: "
1247                "can't dump CameraService from pid=%d, uid=%d\n",
1248                getCallingPid(),
1249                getCallingUid());
1250        result.append(buffer);
1251        write(fd, result.string(), result.size());
1252    } else {
1253        bool locked = tryLock(mServiceLock);
1254        // failed to lock - CameraService is probably deadlocked
1255        if (!locked) {
1256            String8 result(kDeadlockedString);
1257            write(fd, result.string(), result.size());
1258        }
1259
1260        bool hasClient = false;
1261        for (int i = 0; i < mNumberOfCameras; i++) {
1262            sp<Client> client = mClient[i].promote();
1263            if (client == 0) continue;
1264            hasClient = true;
1265            sprintf(buffer, "Client[%d] (%p) PID: %d\n",
1266                    i,
1267                    client->getCameraClient()->asBinder().get(),
1268                    client->mClientPid);
1269            result.append(buffer);
1270            write(fd, result.string(), result.size());
1271            client->mHardware->dump(fd, args);
1272        }
1273        if (!hasClient) {
1274            result.append("No camera client yet.\n");
1275            write(fd, result.string(), result.size());
1276        }
1277
1278        if (locked) mServiceLock.unlock();
1279
1280        // change logging level
1281        int n = args.size();
1282        for (int i = 0; i + 1 < n; i++) {
1283            if (args[i] == String16("-v")) {
1284                String8 levelStr(args[i+1]);
1285                int level = atoi(levelStr.string());
1286                sprintf(buffer, "Set Log Level to %d", level);
1287                result.append(buffer);
1288                setLogLevel(level);
1289            }
1290        }
1291    }
1292    return NO_ERROR;
1293}
1294
1295}; // namespace android
1296