CameraService.cpp revision d1176ef16677b6c94fb893edb6a864cdccc0b190
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraService"
18//#define LOG_NDEBUG 0
19
20#include <stdio.h>
21#include <string.h>
22#include <sys/types.h>
23#include <pthread.h>
24
25#include <binder/AppOpsManager.h>
26#include <binder/IPCThreadState.h>
27#include <binder/IServiceManager.h>
28#include <binder/MemoryBase.h>
29#include <binder/MemoryHeapBase.h>
30#include <cutils/atomic.h>
31#include <cutils/properties.h>
32#include <gui/Surface.h>
33#include <hardware/hardware.h>
34#include <media/AudioSystem.h>
35#include <media/IMediaHTTPService.h>
36#include <media/mediaplayer.h>
37#include <utils/Errors.h>
38#include <utils/Log.h>
39#include <utils/String16.h>
40#include <utils/Trace.h>
41#include <system/camera_vendor_tags.h>
42
43#include "CameraService.h"
44#include "api1/CameraClient.h"
45#include "api1/Camera2Client.h"
46#include "api_pro/ProCamera2Client.h"
47#include "api2/CameraDeviceClient.h"
48#include "utils/CameraTraces.h"
49#include "CameraDeviceFactory.h"
50
51namespace android {
52
53// ----------------------------------------------------------------------------
54// Logging support -- this is for debugging only
55// Use "adb shell dumpsys media.camera -v 1" to change it.
56volatile int32_t gLogLevel = 0;
57
58#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
59#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
60
61static void setLogLevel(int level) {
62    android_atomic_write(level, &gLogLevel);
63}
64
65// ----------------------------------------------------------------------------
66
67static int getCallingPid() {
68    return IPCThreadState::self()->getCallingPid();
69}
70
71static int getCallingUid() {
72    return IPCThreadState::self()->getCallingUid();
73}
74
75extern "C" {
76static void camera_device_status_change(
77        const struct camera_module_callbacks* callbacks,
78        int camera_id,
79        int new_status) {
80    sp<CameraService> cs = const_cast<CameraService*>(
81                                static_cast<const CameraService*>(callbacks));
82
83    cs->onDeviceStatusChanged(
84        camera_id,
85        new_status);
86}
87} // extern "C"
88
89// ----------------------------------------------------------------------------
90
91// This is ugly and only safe if we never re-create the CameraService, but
92// should be ok for now.
93static CameraService *gCameraService;
94
95CameraService::CameraService()
96    :mSoundRef(0), mModule(0)
97{
98    ALOGI("CameraService started (pid=%d)", getpid());
99    gCameraService = this;
100
101    for (size_t i = 0; i < MAX_CAMERAS; ++i) {
102        mStatusList[i] = ICameraServiceListener::STATUS_PRESENT;
103    }
104
105    this->camera_device_status_change = android::camera_device_status_change;
106}
107
108void CameraService::onFirstRef()
109{
110    LOG1("CameraService::onFirstRef");
111
112    BnCameraService::onFirstRef();
113
114    if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
115                (const hw_module_t **)&mModule) < 0) {
116        ALOGE("Could not load camera HAL module");
117        mNumberOfCameras = 0;
118    }
119    else {
120        ALOGI("Loaded \"%s\" camera module", mModule->common.name);
121        mNumberOfCameras = mModule->get_number_of_cameras();
122        if (mNumberOfCameras > MAX_CAMERAS) {
123            ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
124                    mNumberOfCameras, MAX_CAMERAS);
125            mNumberOfCameras = MAX_CAMERAS;
126        }
127        for (int i = 0; i < mNumberOfCameras; i++) {
128            setCameraFree(i);
129        }
130
131        if (mModule->common.module_api_version >=
132                CAMERA_MODULE_API_VERSION_2_1) {
133            mModule->set_callbacks(this);
134        }
135
136        VendorTagDescriptor::clearGlobalVendorTagDescriptor();
137
138        if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_2) {
139            setUpVendorTags();
140        }
141
142        CameraDeviceFactory::registerService(this);
143    }
144}
145
146CameraService::~CameraService() {
147    for (int i = 0; i < mNumberOfCameras; i++) {
148        if (mBusy[i]) {
149            ALOGE("camera %d is still in use in destructor!", i);
150        }
151    }
152
153    VendorTagDescriptor::clearGlobalVendorTagDescriptor();
154    gCameraService = NULL;
155}
156
157void CameraService::onDeviceStatusChanged(int cameraId,
158                                          int newStatus)
159{
160    ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__,
161          cameraId, newStatus);
162
163    if (cameraId < 0 || cameraId >= MAX_CAMERAS) {
164        ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId);
165        return;
166    }
167
168    if ((int)getStatus(cameraId) == newStatus) {
169        ALOGE("%s: State transition to the same status 0x%x not allowed",
170              __FUNCTION__, (uint32_t)newStatus);
171        return;
172    }
173
174    /* don't do this in updateStatus
175       since it is also called from connect and we could get into a deadlock */
176    if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) {
177        Vector<sp<BasicClient> > clientsToDisconnect;
178        {
179           Mutex::Autolock al(mServiceLock);
180
181           /* Find all clients that we need to disconnect */
182           sp<BasicClient> client = mClient[cameraId].promote();
183           if (client.get() != NULL) {
184               clientsToDisconnect.push_back(client);
185           }
186
187           int i = cameraId;
188           for (size_t j = 0; j < mProClientList[i].size(); ++j) {
189               sp<ProClient> cl = mProClientList[i][j].promote();
190               if (cl != NULL) {
191                   clientsToDisconnect.push_back(cl);
192               }
193           }
194        }
195
196        /* now disconnect them. don't hold the lock
197           or we can get into a deadlock */
198
199        for (size_t i = 0; i < clientsToDisconnect.size(); ++i) {
200            sp<BasicClient> client = clientsToDisconnect[i];
201
202            client->disconnect();
203            /**
204             * The remote app will no longer be able to call methods on the
205             * client since the client PID will be reset to 0
206             */
207        }
208
209        ALOGV("%s: After unplug, disconnected %d clients",
210              __FUNCTION__, clientsToDisconnect.size());
211    }
212
213    updateStatus(
214            static_cast<ICameraServiceListener::Status>(newStatus), cameraId);
215
216}
217
218int32_t CameraService::getNumberOfCameras() {
219    return mNumberOfCameras;
220}
221
222status_t CameraService::getCameraInfo(int cameraId,
223                                      struct CameraInfo* cameraInfo) {
224    if (!mModule) {
225        return -ENODEV;
226    }
227
228    if (cameraId < 0 || cameraId >= mNumberOfCameras) {
229        return BAD_VALUE;
230    }
231
232    struct camera_info info;
233    status_t rc = mModule->get_camera_info(cameraId, &info);
234    cameraInfo->facing = info.facing;
235    cameraInfo->orientation = info.orientation;
236    return rc;
237}
238
239status_t CameraService::getCameraCharacteristics(int cameraId,
240                                                CameraMetadata* cameraInfo) {
241    if (!cameraInfo) {
242        ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
243        return BAD_VALUE;
244    }
245
246    if (!mModule) {
247        ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
248        return -ENODEV;
249    }
250
251    if (mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_0) {
252        // TODO: Remove this check once HAL1 shim is in place.
253        ALOGE("%s: Only HAL module version V2 or higher supports static metadata", __FUNCTION__);
254        return BAD_VALUE;
255    }
256
257    if (cameraId < 0 || cameraId >= mNumberOfCameras) {
258        ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId);
259        return BAD_VALUE;
260    }
261
262    int facing;
263    if (getDeviceVersion(cameraId, &facing) == CAMERA_DEVICE_API_VERSION_1_0) {
264        // TODO: Remove this check once HAL1 shim is in place.
265        ALOGE("%s: HAL1 doesn't support static metadata yet", __FUNCTION__);
266        return BAD_VALUE;
267    }
268
269    if (getDeviceVersion(cameraId, &facing) <= CAMERA_DEVICE_API_VERSION_2_1) {
270        // Disable HAL2.x support for camera2 API for now.
271        ALOGW("%s: HAL2.x doesn't support getCameraCharacteristics for now", __FUNCTION__);
272        return BAD_VALUE;
273    }
274
275    struct camera_info info;
276    status_t ret = mModule->get_camera_info(cameraId, &info);
277    *cameraInfo = info.static_camera_characteristics;
278
279    return ret;
280}
281
282status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) {
283    if (!mModule) {
284        ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
285        return -ENODEV;
286    }
287
288    if (mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_2) {
289        // TODO: Remove this check once HAL1 shim is in place.
290        ALOGE("%s: Only HAL module version V2.2 or higher supports vendor tags", __FUNCTION__);
291        return -EOPNOTSUPP;
292    }
293
294    desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
295    return OK;
296}
297
298int CameraService::getDeviceVersion(int cameraId, int* facing) {
299    struct camera_info info;
300    if (mModule->get_camera_info(cameraId, &info) != OK) {
301        return -1;
302    }
303
304    int deviceVersion;
305    if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) {
306        deviceVersion = info.device_version;
307    } else {
308        deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
309    }
310
311    if (facing) {
312        *facing = info.facing;
313    }
314
315    return deviceVersion;
316}
317
318bool CameraService::isValidCameraId(int cameraId) {
319    int facing;
320    int deviceVersion = getDeviceVersion(cameraId, &facing);
321
322    switch(deviceVersion) {
323      case CAMERA_DEVICE_API_VERSION_1_0:
324      case CAMERA_DEVICE_API_VERSION_2_0:
325      case CAMERA_DEVICE_API_VERSION_2_1:
326      case CAMERA_DEVICE_API_VERSION_3_0:
327        return true;
328      default:
329        return false;
330    }
331
332    return false;
333}
334
335bool CameraService::setUpVendorTags() {
336    vendor_tag_ops_t vOps = vendor_tag_ops_t();
337
338    // Check if vendor operations have been implemented
339    if (mModule->get_vendor_tag_ops == NULL) {
340        ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
341        return false;
342    }
343
344    ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
345    mModule->get_vendor_tag_ops(&vOps);
346    ATRACE_END();
347
348    // Ensure all vendor operations are present
349    if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
350            vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
351            vOps.get_tag_type == NULL) {
352        ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
353               , __FUNCTION__);
354        return false;
355    }
356
357    // Read all vendor tag definitions into a descriptor
358    sp<VendorTagDescriptor> desc;
359    status_t res;
360    if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
361            != OK) {
362        ALOGE("%s: Could not generate descriptor from vendor tag operations,"
363              "received error %s (%d). Camera clients will not be able to use"
364              "vendor tags", __FUNCTION__, strerror(res), res);
365        return false;
366    }
367
368    // Set the global descriptor to use with camera metadata
369    VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
370    return true;
371}
372
373status_t CameraService::validateConnect(int cameraId,
374                                    /*inout*/
375                                    int& clientUid) const {
376
377    int callingPid = getCallingPid();
378
379    if (clientUid == USE_CALLING_UID) {
380        clientUid = getCallingUid();
381    } else {
382        // We only trust our own process to forward client UIDs
383        if (callingPid != getpid()) {
384            ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)",
385                    callingPid);
386            return PERMISSION_DENIED;
387        }
388    }
389
390    if (!mModule) {
391        ALOGE("Camera HAL module not loaded");
392        return -ENODEV;
393    }
394
395    if (cameraId < 0 || cameraId >= mNumberOfCameras) {
396        ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
397            callingPid, cameraId);
398        return -ENODEV;
399    }
400
401    char value[PROPERTY_VALUE_MAX];
402    property_get("sys.secpolicy.camera.disabled", value, "0");
403    if (strcmp(value, "1") == 0) {
404        // Camera is disabled by DevicePolicyManager.
405        ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
406        return -EACCES;
407    }
408
409    ICameraServiceListener::Status currentStatus = getStatus(cameraId);
410    if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
411        ALOGI("Camera is not plugged in,"
412               " connect X (pid %d) rejected", callingPid);
413        return -ENODEV;
414    } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) {
415        ALOGI("Camera is enumerating,"
416               " connect X (pid %d) rejected", callingPid);
417        return -EBUSY;
418    }
419    // Else don't check for STATUS_NOT_AVAILABLE.
420    //  -- It's done implicitly in canConnectUnsafe /w the mBusy array
421
422    return OK;
423}
424
425bool CameraService::canConnectUnsafe(int cameraId,
426                                     const String16& clientPackageName,
427                                     const sp<IBinder>& remoteCallback,
428                                     sp<BasicClient> &client) {
429    String8 clientName8(clientPackageName);
430    int callingPid = getCallingPid();
431
432    if (mClient[cameraId] != 0) {
433        client = mClient[cameraId].promote();
434        if (client != 0) {
435            if (remoteCallback == client->getRemote()) {
436                LOG1("CameraService::connect X (pid %d) (the same client)",
437                     callingPid);
438                return true;
439            } else {
440                // TODOSC: need to support 1 regular client,
441                // multiple shared clients here
442                ALOGW("CameraService::connect X (pid %d) rejected"
443                      " (existing client).", callingPid);
444                return false;
445            }
446        }
447        mClient[cameraId].clear();
448    }
449
450    /*
451    mBusy is set to false as the last step of the Client destructor,
452    after which it is guaranteed that the Client destructor has finished (
453    including any inherited destructors)
454
455    We only need this for a Client subclasses since we don't allow
456    multiple Clents to be opened concurrently, but multiple BasicClient
457    would be fine
458    */
459    if (mBusy[cameraId]) {
460        ALOGW("CameraService::connect X (pid %d, \"%s\") rejected"
461                " (camera %d is still busy).", callingPid,
462                clientName8.string(), cameraId);
463        return false;
464    }
465
466    return true;
467}
468
469status_t CameraService::connect(
470        const sp<ICameraClient>& cameraClient,
471        int cameraId,
472        const String16& clientPackageName,
473        int clientUid,
474        /*out*/
475        sp<ICamera>& device) {
476
477    String8 clientName8(clientPackageName);
478    int callingPid = getCallingPid();
479
480    LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid,
481            clientName8.string(), cameraId);
482
483    status_t status = validateConnect(cameraId, /*inout*/clientUid);
484    if (status != OK) {
485        return status;
486    }
487
488
489    sp<Client> client;
490    {
491        Mutex::Autolock lock(mServiceLock);
492        sp<BasicClient> clientTmp;
493        if (!canConnectUnsafe(cameraId, clientPackageName,
494                              cameraClient->asBinder(),
495                              /*out*/clientTmp)) {
496            return -EBUSY;
497        } else if (client.get() != NULL) {
498            device = static_cast<Client*>(clientTmp.get());
499            return OK;
500        }
501
502        int facing = -1;
503        int deviceVersion = getDeviceVersion(cameraId, &facing);
504
505        // If there are other non-exclusive users of the camera,
506        //  this will tear them down before we can reuse the camera
507        if (isValidCameraId(cameraId)) {
508            // transition from PRESENT -> NOT_AVAILABLE
509            updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
510                         cameraId);
511        }
512
513        switch(deviceVersion) {
514          case CAMERA_DEVICE_API_VERSION_1_0:
515            client = new CameraClient(this, cameraClient,
516                    clientPackageName, cameraId,
517                    facing, callingPid, clientUid, getpid());
518            break;
519          case CAMERA_DEVICE_API_VERSION_2_0:
520          case CAMERA_DEVICE_API_VERSION_2_1:
521          case CAMERA_DEVICE_API_VERSION_3_0:
522            client = new Camera2Client(this, cameraClient,
523                    clientPackageName, cameraId,
524                    facing, callingPid, clientUid, getpid(),
525                    deviceVersion);
526            break;
527          case -1:
528            ALOGE("Invalid camera id %d", cameraId);
529            return BAD_VALUE;
530          default:
531            ALOGE("Unknown camera device HAL version: %d", deviceVersion);
532            return INVALID_OPERATION;
533        }
534
535        status_t status = connectFinishUnsafe(client, client->getRemote());
536        if (status != OK) {
537            // this is probably not recoverable.. maybe the client can try again
538            // OK: we can only get here if we were originally in PRESENT state
539            updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId);
540            return status;
541        }
542
543        mClient[cameraId] = client;
544        LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId,
545             getpid());
546    }
547    // important: release the mutex here so the client can call back
548    //    into the service from its destructor (can be at the end of the call)
549
550    device = client;
551    return OK;
552}
553
554status_t CameraService::connectFinishUnsafe(const sp<BasicClient>& client,
555                                            const sp<IBinder>& remoteCallback) {
556    status_t status = client->initialize(mModule);
557    if (status != OK) {
558        return status;
559    }
560
561    remoteCallback->linkToDeath(this);
562
563    return OK;
564}
565
566status_t CameraService::connectPro(
567                                        const sp<IProCameraCallbacks>& cameraCb,
568                                        int cameraId,
569                                        const String16& clientPackageName,
570                                        int clientUid,
571                                        /*out*/
572                                        sp<IProCameraUser>& device)
573{
574    String8 clientName8(clientPackageName);
575    int callingPid = getCallingPid();
576
577    LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid,
578            clientName8.string(), cameraId);
579    status_t status = validateConnect(cameraId, /*inout*/clientUid);
580    if (status != OK) {
581        return status;
582    }
583
584    sp<ProClient> client;
585    {
586        Mutex::Autolock lock(mServiceLock);
587        {
588            sp<BasicClient> client;
589            if (!canConnectUnsafe(cameraId, clientPackageName,
590                                  cameraCb->asBinder(),
591                                  /*out*/client)) {
592                return -EBUSY;
593            }
594        }
595
596        int facing = -1;
597        int deviceVersion = getDeviceVersion(cameraId, &facing);
598
599        switch(deviceVersion) {
600          case CAMERA_DEVICE_API_VERSION_1_0:
601            ALOGE("Camera id %d uses HALv1, doesn't support ProCamera",
602                  cameraId);
603            return -EOPNOTSUPP;
604            break;
605          case CAMERA_DEVICE_API_VERSION_2_0:
606          case CAMERA_DEVICE_API_VERSION_2_1:
607          case CAMERA_DEVICE_API_VERSION_3_0:
608            client = new ProCamera2Client(this, cameraCb, String16(),
609                    cameraId, facing, callingPid, USE_CALLING_UID, getpid());
610            break;
611          case -1:
612            ALOGE("Invalid camera id %d", cameraId);
613            return BAD_VALUE;
614          default:
615            ALOGE("Unknown camera device HAL version: %d", deviceVersion);
616            return INVALID_OPERATION;
617        }
618
619        status_t status = connectFinishUnsafe(client, client->getRemote());
620        if (status != OK) {
621            return status;
622        }
623
624        mProClientList[cameraId].push(client);
625
626        LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId,
627                getpid());
628    }
629    // important: release the mutex here so the client can call back
630    //    into the service from its destructor (can be at the end of the call)
631    device = client;
632    return OK;
633}
634
635status_t CameraService::connectDevice(
636        const sp<ICameraDeviceCallbacks>& cameraCb,
637        int cameraId,
638        const String16& clientPackageName,
639        int clientUid,
640        /*out*/
641        sp<ICameraDeviceUser>& device)
642{
643
644    String8 clientName8(clientPackageName);
645    int callingPid = getCallingPid();
646
647    LOG1("CameraService::connectDevice E (pid %d \"%s\", id %d)", callingPid,
648            clientName8.string(), cameraId);
649
650    status_t status = validateConnect(cameraId, /*inout*/clientUid);
651    if (status != OK) {
652        return status;
653    }
654
655    sp<CameraDeviceClient> client;
656    {
657        Mutex::Autolock lock(mServiceLock);
658        {
659            sp<BasicClient> client;
660            if (!canConnectUnsafe(cameraId, clientPackageName,
661                                  cameraCb->asBinder(),
662                                  /*out*/client)) {
663                return -EBUSY;
664            }
665        }
666
667        int facing = -1;
668        int deviceVersion = getDeviceVersion(cameraId, &facing);
669
670        // If there are other non-exclusive users of the camera,
671        //  this will tear them down before we can reuse the camera
672        if (isValidCameraId(cameraId)) {
673            // transition from PRESENT -> NOT_AVAILABLE
674            updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
675                         cameraId);
676        }
677
678        switch(deviceVersion) {
679          case CAMERA_DEVICE_API_VERSION_1_0:
680            ALOGW("Camera using old HAL version: %d", deviceVersion);
681            return -EOPNOTSUPP;
682           // TODO: don't allow 2.0  Only allow 2.1 and higher
683          case CAMERA_DEVICE_API_VERSION_2_0:
684          case CAMERA_DEVICE_API_VERSION_2_1:
685          case CAMERA_DEVICE_API_VERSION_3_0:
686            client = new CameraDeviceClient(this, cameraCb, String16(),
687                    cameraId, facing, callingPid, USE_CALLING_UID, getpid());
688            break;
689          case -1:
690            ALOGE("Invalid camera id %d", cameraId);
691            return BAD_VALUE;
692          default:
693            ALOGE("Unknown camera device HAL version: %d", deviceVersion);
694            return INVALID_OPERATION;
695        }
696
697        status_t status = connectFinishUnsafe(client, client->getRemote());
698        if (status != OK) {
699            // this is probably not recoverable.. maybe the client can try again
700            // OK: we can only get here if we were originally in PRESENT state
701            updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId);
702            return status;
703        }
704
705        LOG1("CameraService::connectDevice X (id %d, this pid is %d)", cameraId,
706                getpid());
707
708        mClient[cameraId] = client;
709    }
710    // important: release the mutex here so the client can call back
711    //    into the service from its destructor (can be at the end of the call)
712
713    device = client;
714    return OK;
715}
716
717
718status_t CameraService::addListener(
719                                const sp<ICameraServiceListener>& listener) {
720    ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
721
722    Mutex::Autolock lock(mServiceLock);
723
724    Vector<sp<ICameraServiceListener> >::iterator it, end;
725    for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
726        if ((*it)->asBinder() == listener->asBinder()) {
727            ALOGW("%s: Tried to add listener %p which was already subscribed",
728                  __FUNCTION__, listener.get());
729            return ALREADY_EXISTS;
730        }
731    }
732
733    mListenerList.push_back(listener);
734
735    /* Immediately signal current status to this listener only */
736    {
737        Mutex::Autolock m(mStatusMutex) ;
738        int numCams = getNumberOfCameras();
739        for (int i = 0; i < numCams; ++i) {
740            listener->onStatusChanged(mStatusList[i], i);
741        }
742    }
743
744    return OK;
745}
746status_t CameraService::removeListener(
747                                const sp<ICameraServiceListener>& listener) {
748    ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
749
750    Mutex::Autolock lock(mServiceLock);
751
752    Vector<sp<ICameraServiceListener> >::iterator it;
753    for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
754        if ((*it)->asBinder() == listener->asBinder()) {
755            mListenerList.erase(it);
756            return OK;
757        }
758    }
759
760    ALOGW("%s: Tried to remove a listener %p which was not subscribed",
761          __FUNCTION__, listener.get());
762
763    return BAD_VALUE;
764}
765
766void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) {
767    int callingPid = getCallingPid();
768    LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid);
769
770    // Declare this before the lock to make absolutely sure the
771    // destructor won't be called with the lock held.
772    Mutex::Autolock lock(mServiceLock);
773
774    int outIndex;
775    sp<BasicClient> client = findClientUnsafe(remoteBinder, outIndex);
776
777    if (client != 0) {
778        // Found our camera, clear and leave.
779        LOG1("removeClient: clear camera %d", outIndex);
780        mClient[outIndex].clear();
781
782        client->getRemote()->unlinkToDeath(this);
783    } else {
784
785        sp<ProClient> clientPro = findProClientUnsafe(remoteBinder);
786
787        if (clientPro != NULL) {
788            // Found our camera, clear and leave.
789            LOG1("removeClient: clear pro %p", clientPro.get());
790
791            clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this);
792        }
793    }
794
795    LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid);
796}
797
798sp<CameraService::ProClient> CameraService::findProClientUnsafe(
799                        const wp<IBinder>& cameraCallbacksRemote)
800{
801    sp<ProClient> clientPro;
802
803    for (int i = 0; i < mNumberOfCameras; ++i) {
804        Vector<size_t> removeIdx;
805
806        for (size_t j = 0; j < mProClientList[i].size(); ++j) {
807            wp<ProClient> cl = mProClientList[i][j];
808
809            sp<ProClient> clStrong = cl.promote();
810            if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) {
811                clientPro = clStrong;
812                break;
813            } else if (clStrong == NULL) {
814                // mark to clean up dead ptr
815                removeIdx.push(j);
816            }
817        }
818
819        // remove stale ptrs (in reverse so the indices dont change)
820        for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) {
821            mProClientList[i].removeAt(removeIdx[j]);
822        }
823
824    }
825
826    return clientPro;
827}
828
829sp<CameraService::BasicClient> CameraService::findClientUnsafe(
830                        const wp<IBinder>& cameraClient, int& outIndex) {
831    sp<BasicClient> client;
832
833    for (int i = 0; i < mNumberOfCameras; i++) {
834
835        // This happens when we have already disconnected (or this is
836        // just another unused camera).
837        if (mClient[i] == 0) continue;
838
839        // Promote mClient. It can fail if we are called from this path:
840        // Client::~Client() -> disconnect() -> removeClientByRemote().
841        client = mClient[i].promote();
842
843        // Clean up stale client entry
844        if (client == NULL) {
845            mClient[i].clear();
846            continue;
847        }
848
849        if (cameraClient == client->getRemote()) {
850            // Found our camera
851            outIndex = i;
852            return client;
853        }
854    }
855
856    outIndex = -1;
857    return NULL;
858}
859
860CameraService::BasicClient* CameraService::getClientByIdUnsafe(int cameraId) {
861    if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
862    return mClient[cameraId].unsafe_get();
863}
864
865Mutex* CameraService::getClientLockById(int cameraId) {
866    if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
867    return &mClientLock[cameraId];
868}
869
870sp<CameraService::BasicClient> CameraService::getClientByRemote(
871                                const wp<IBinder>& cameraClient) {
872
873    // Declare this before the lock to make absolutely sure the
874    // destructor won't be called with the lock held.
875    sp<BasicClient> client;
876
877    Mutex::Autolock lock(mServiceLock);
878
879    int outIndex;
880    client = findClientUnsafe(cameraClient, outIndex);
881
882    return client;
883}
884
885status_t CameraService::onTransact(
886    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
887    // Permission checks
888    switch (code) {
889        case BnCameraService::CONNECT:
890        case BnCameraService::CONNECT_PRO:
891            const int pid = getCallingPid();
892            const int self_pid = getpid();
893            if (pid != self_pid) {
894                // we're called from a different process, do the real check
895                if (!checkCallingPermission(
896                        String16("android.permission.CAMERA"))) {
897                    const int uid = getCallingUid();
898                    ALOGE("Permission Denial: "
899                         "can't use the camera pid=%d, uid=%d", pid, uid);
900                    return PERMISSION_DENIED;
901                }
902            }
903            break;
904    }
905
906    return BnCameraService::onTransact(code, data, reply, flags);
907}
908
909// The reason we need this busy bit is a new CameraService::connect() request
910// may come in while the previous Client's destructor has not been run or is
911// still running. If the last strong reference of the previous Client is gone
912// but the destructor has not been finished, we should not allow the new Client
913// to be created because we need to wait for the previous Client to tear down
914// the hardware first.
915void CameraService::setCameraBusy(int cameraId) {
916    android_atomic_write(1, &mBusy[cameraId]);
917
918    ALOGV("setCameraBusy cameraId=%d", cameraId);
919}
920
921void CameraService::setCameraFree(int cameraId) {
922    android_atomic_write(0, &mBusy[cameraId]);
923
924    ALOGV("setCameraFree cameraId=%d", cameraId);
925}
926
927// We share the media players for shutter and recording sound for all clients.
928// A reference count is kept to determine when we will actually release the
929// media players.
930
931MediaPlayer* CameraService::newMediaPlayer(const char *file) {
932    MediaPlayer* mp = new MediaPlayer();
933    if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
934        mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
935        mp->prepare();
936    } else {
937        ALOGE("Failed to load CameraService sounds: %s", file);
938        return NULL;
939    }
940    return mp;
941}
942
943void CameraService::loadSound() {
944    Mutex::Autolock lock(mSoundLock);
945    LOG1("CameraService::loadSound ref=%d", mSoundRef);
946    if (mSoundRef++) return;
947
948    mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
949    mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
950}
951
952void CameraService::releaseSound() {
953    Mutex::Autolock lock(mSoundLock);
954    LOG1("CameraService::releaseSound ref=%d", mSoundRef);
955    if (--mSoundRef) return;
956
957    for (int i = 0; i < NUM_SOUNDS; i++) {
958        if (mSoundPlayer[i] != 0) {
959            mSoundPlayer[i]->disconnect();
960            mSoundPlayer[i].clear();
961        }
962    }
963}
964
965void CameraService::playSound(sound_kind kind) {
966    LOG1("playSound(%d)", kind);
967    Mutex::Autolock lock(mSoundLock);
968    sp<MediaPlayer> player = mSoundPlayer[kind];
969    if (player != 0) {
970        player->seekTo(0);
971        player->start();
972    }
973}
974
975// ----------------------------------------------------------------------------
976
977CameraService::Client::Client(const sp<CameraService>& cameraService,
978        const sp<ICameraClient>& cameraClient,
979        const String16& clientPackageName,
980        int cameraId, int cameraFacing,
981        int clientPid, uid_t clientUid,
982        int servicePid) :
983        CameraService::BasicClient(cameraService, cameraClient->asBinder(),
984                clientPackageName,
985                cameraId, cameraFacing,
986                clientPid, clientUid,
987                servicePid)
988{
989    int callingPid = getCallingPid();
990    LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
991
992    mRemoteCallback = cameraClient;
993
994    cameraService->setCameraBusy(cameraId);
995    cameraService->loadSound();
996
997    LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
998}
999
1000// tear down the client
1001CameraService::Client::~Client() {
1002    ALOGV("~Client");
1003    mDestructionStarted = true;
1004
1005    mCameraService->releaseSound();
1006    // unconditionally disconnect. function is idempotent
1007    Client::disconnect();
1008}
1009
1010CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
1011        const sp<IBinder>& remoteCallback,
1012        const String16& clientPackageName,
1013        int cameraId, int cameraFacing,
1014        int clientPid, uid_t clientUid,
1015        int servicePid):
1016        mClientPackageName(clientPackageName)
1017{
1018    mCameraService = cameraService;
1019    mRemoteBinder = remoteCallback;
1020    mCameraId = cameraId;
1021    mCameraFacing = cameraFacing;
1022    mClientPid = clientPid;
1023    mClientUid = clientUid;
1024    mServicePid = servicePid;
1025    mOpsActive = false;
1026    mDestructionStarted = false;
1027}
1028
1029CameraService::BasicClient::~BasicClient() {
1030    ALOGV("~BasicClient");
1031    mDestructionStarted = true;
1032}
1033
1034void CameraService::BasicClient::disconnect() {
1035    ALOGV("BasicClient::disconnect");
1036    mCameraService->removeClientByRemote(mRemoteBinder);
1037    // client shouldn't be able to call into us anymore
1038    mClientPid = 0;
1039}
1040
1041status_t CameraService::BasicClient::startCameraOps() {
1042    int32_t res;
1043
1044    mOpsCallback = new OpsCallback(this);
1045
1046    {
1047        ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
1048              __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
1049    }
1050
1051    mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
1052            mClientPackageName, mOpsCallback);
1053    res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
1054            mClientUid, mClientPackageName);
1055
1056    if (res != AppOpsManager::MODE_ALLOWED) {
1057        ALOGI("Camera %d: Access for \"%s\" has been revoked",
1058                mCameraId, String8(mClientPackageName).string());
1059        return PERMISSION_DENIED;
1060    }
1061    mOpsActive = true;
1062    return OK;
1063}
1064
1065status_t CameraService::BasicClient::finishCameraOps() {
1066    if (mOpsActive) {
1067        mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
1068                mClientPackageName);
1069        mOpsActive = false;
1070    }
1071    mAppOpsManager.stopWatchingMode(mOpsCallback);
1072    mOpsCallback.clear();
1073
1074    return OK;
1075}
1076
1077void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
1078    String8 name(packageName);
1079    String8 myName(mClientPackageName);
1080
1081    if (op != AppOpsManager::OP_CAMERA) {
1082        ALOGW("Unexpected app ops notification received: %d", op);
1083        return;
1084    }
1085
1086    int32_t res;
1087    res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
1088            mClientUid, mClientPackageName);
1089    ALOGV("checkOp returns: %d, %s ", res,
1090            res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
1091            res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
1092            res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
1093            "UNKNOWN");
1094
1095    if (res != AppOpsManager::MODE_ALLOWED) {
1096        ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
1097                myName.string());
1098        // Reset the client PID to allow server-initiated disconnect,
1099        // and to prevent further calls by client.
1100        mClientPid = getCallingPid();
1101        notifyError();
1102        disconnect();
1103    }
1104}
1105
1106// ----------------------------------------------------------------------------
1107
1108Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
1109    return gCameraService->getClientLockById((int)(intptr_t) user);
1110}
1111
1112// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
1113// be acquired for this to be safe
1114CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
1115    BasicClient *basicClient = gCameraService->getClientByIdUnsafe((int)(intptr_t) user);
1116    // OK: only CameraClient calls this, and they already cast anyway.
1117    Client* client = static_cast<Client*>(basicClient);
1118
1119    // This could happen if the Client is in the process of shutting down (the
1120    // last strong reference is gone, but the destructor hasn't finished
1121    // stopping the hardware).
1122    if (client == NULL) return NULL;
1123
1124    // destruction already started, so should not be accessed
1125    if (client->mDestructionStarted) return NULL;
1126
1127    return client;
1128}
1129
1130void CameraService::Client::notifyError() {
1131    mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
1132}
1133
1134// NOTE: function is idempotent
1135void CameraService::Client::disconnect() {
1136    ALOGV("Client::disconnect");
1137    BasicClient::disconnect();
1138    mCameraService->setCameraFree(mCameraId);
1139
1140    StatusVector rejectSourceStates;
1141    rejectSourceStates.push_back(ICameraServiceListener::STATUS_NOT_PRESENT);
1142    rejectSourceStates.push_back(ICameraServiceListener::STATUS_ENUMERATING);
1143
1144    // Transition to PRESENT if the camera is not in either of above 2 states
1145    mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT,
1146                                 mCameraId,
1147                                 &rejectSourceStates);
1148}
1149
1150CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
1151        mClient(client) {
1152}
1153
1154void CameraService::Client::OpsCallback::opChanged(int32_t op,
1155        const String16& packageName) {
1156    sp<BasicClient> client = mClient.promote();
1157    if (client != NULL) {
1158        client->opChanged(op, packageName);
1159    }
1160}
1161
1162// ----------------------------------------------------------------------------
1163//                  IProCamera
1164// ----------------------------------------------------------------------------
1165
1166CameraService::ProClient::ProClient(const sp<CameraService>& cameraService,
1167        const sp<IProCameraCallbacks>& remoteCallback,
1168        const String16& clientPackageName,
1169        int cameraId,
1170        int cameraFacing,
1171        int clientPid,
1172        uid_t clientUid,
1173        int servicePid)
1174        : CameraService::BasicClient(cameraService, remoteCallback->asBinder(),
1175                clientPackageName, cameraId, cameraFacing,
1176                clientPid,  clientUid, servicePid)
1177{
1178    mRemoteCallback = remoteCallback;
1179}
1180
1181CameraService::ProClient::~ProClient() {
1182}
1183
1184void CameraService::ProClient::notifyError() {
1185    mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
1186}
1187
1188// ----------------------------------------------------------------------------
1189
1190static const int kDumpLockRetries = 50;
1191static const int kDumpLockSleep = 60000;
1192
1193static bool tryLock(Mutex& mutex)
1194{
1195    bool locked = false;
1196    for (int i = 0; i < kDumpLockRetries; ++i) {
1197        if (mutex.tryLock() == NO_ERROR) {
1198            locked = true;
1199            break;
1200        }
1201        usleep(kDumpLockSleep);
1202    }
1203    return locked;
1204}
1205
1206status_t CameraService::dump(int fd, const Vector<String16>& args) {
1207    String8 result;
1208    if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1209        result.appendFormat("Permission Denial: "
1210                "can't dump CameraService from pid=%d, uid=%d\n",
1211                getCallingPid(),
1212                getCallingUid());
1213        write(fd, result.string(), result.size());
1214    } else {
1215        bool locked = tryLock(mServiceLock);
1216        // failed to lock - CameraService is probably deadlocked
1217        if (!locked) {
1218            result.append("CameraService may be deadlocked\n");
1219            write(fd, result.string(), result.size());
1220        }
1221
1222        bool hasClient = false;
1223        if (!mModule) {
1224            result = String8::format("No camera module available!\n");
1225            write(fd, result.string(), result.size());
1226            return NO_ERROR;
1227        }
1228
1229        result = String8::format("Camera module HAL API version: 0x%x\n",
1230                mModule->common.hal_api_version);
1231        result.appendFormat("Camera module API version: 0x%x\n",
1232                mModule->common.module_api_version);
1233        result.appendFormat("Camera module name: %s\n",
1234                mModule->common.name);
1235        result.appendFormat("Camera module author: %s\n",
1236                mModule->common.author);
1237        result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
1238        write(fd, result.string(), result.size());
1239        for (int i = 0; i < mNumberOfCameras; i++) {
1240            result = String8::format("Camera %d static information:\n", i);
1241            camera_info info;
1242
1243            status_t rc = mModule->get_camera_info(i, &info);
1244            if (rc != OK) {
1245                result.appendFormat("  Error reading static information!\n");
1246                write(fd, result.string(), result.size());
1247            } else {
1248                result.appendFormat("  Facing: %s\n",
1249                        info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
1250                result.appendFormat("  Orientation: %d\n", info.orientation);
1251                int deviceVersion;
1252                if (mModule->common.module_api_version <
1253                        CAMERA_MODULE_API_VERSION_2_0) {
1254                    deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
1255                } else {
1256                    deviceVersion = info.device_version;
1257                }
1258                result.appendFormat("  Device version: 0x%x\n", deviceVersion);
1259                if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
1260                    result.appendFormat("  Device static metadata:\n");
1261                    write(fd, result.string(), result.size());
1262                    dump_indented_camera_metadata(info.static_camera_characteristics,
1263                            fd, 2, 4);
1264                } else {
1265                    write(fd, result.string(), result.size());
1266                }
1267            }
1268
1269            sp<BasicClient> client = mClient[i].promote();
1270            if (client == 0) {
1271                result = String8::format("  Device is closed, no client instance\n");
1272                write(fd, result.string(), result.size());
1273                continue;
1274            }
1275            hasClient = true;
1276            result = String8::format("  Device is open. Client instance dump:\n");
1277            write(fd, result.string(), result.size());
1278            client->dump(fd, args);
1279        }
1280        if (!hasClient) {
1281            result = String8::format("\nNo active camera clients yet.\n");
1282            write(fd, result.string(), result.size());
1283        }
1284
1285        if (locked) mServiceLock.unlock();
1286
1287        // Dump camera traces if there were any
1288        write(fd, "\n", 1);
1289        camera3::CameraTraces::dump(fd, args);
1290
1291        // change logging level
1292        int n = args.size();
1293        for (int i = 0; i + 1 < n; i++) {
1294            String16 verboseOption("-v");
1295            if (args[i] == verboseOption) {
1296                String8 levelStr(args[i+1]);
1297                int level = atoi(levelStr.string());
1298                result = String8::format("\nSetting log level to %d.\n", level);
1299                setLogLevel(level);
1300                write(fd, result.string(), result.size());
1301            }
1302        }
1303
1304    }
1305    return NO_ERROR;
1306}
1307
1308/*virtual*/void CameraService::binderDied(
1309    const wp<IBinder> &who) {
1310
1311    /**
1312      * While tempting to promote the wp<IBinder> into a sp,
1313      * it's actually not supported by the binder driver
1314      */
1315
1316    ALOGV("java clients' binder died");
1317
1318    sp<BasicClient> cameraClient = getClientByRemote(who);
1319
1320    if (cameraClient == 0) {
1321        ALOGV("java clients' binder death already cleaned up (normal case)");
1322        return;
1323    }
1324
1325    ALOGW("Disconnecting camera client %p since the binder for it "
1326          "died (this pid %d)", cameraClient.get(), getCallingPid());
1327
1328    cameraClient->disconnect();
1329
1330}
1331
1332void CameraService::updateStatus(ICameraServiceListener::Status status,
1333                                 int32_t cameraId,
1334                                 const StatusVector *rejectSourceStates) {
1335    // do not lock mServiceLock here or can get into a deadlock from
1336    //  connect() -> ProClient::disconnect -> updateStatus
1337    Mutex::Autolock lock(mStatusMutex);
1338
1339    ICameraServiceListener::Status oldStatus = mStatusList[cameraId];
1340
1341    mStatusList[cameraId] = status;
1342
1343    if (oldStatus != status) {
1344        ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x",
1345              __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status);
1346
1347        if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT &&
1348            (status != ICameraServiceListener::STATUS_PRESENT &&
1349             status != ICameraServiceListener::STATUS_ENUMERATING)) {
1350
1351            ALOGW("%s: From NOT_PRESENT can only transition into PRESENT"
1352                  " or ENUMERATING", __FUNCTION__);
1353            mStatusList[cameraId] = oldStatus;
1354            return;
1355        }
1356
1357        if (rejectSourceStates != NULL) {
1358            const StatusVector &rejectList = *rejectSourceStates;
1359            StatusVector::const_iterator it = rejectList.begin();
1360
1361            /**
1362             * Sometimes we want to conditionally do a transition.
1363             * For example if a client disconnects, we want to go to PRESENT
1364             * only if we weren't already in NOT_PRESENT or ENUMERATING.
1365             */
1366            for (; it != rejectList.end(); ++it) {
1367                if (oldStatus == *it) {
1368                    ALOGV("%s: Rejecting status transition for Camera ID %d, "
1369                          " since the source state was was in one of the bad "
1370                          " states.", __FUNCTION__, cameraId);
1371                    mStatusList[cameraId] = oldStatus;
1372                    return;
1373                }
1374            }
1375        }
1376
1377        /**
1378          * ProClients lose their exclusive lock.
1379          * - Done before the CameraClient can initialize the HAL device,
1380          *   since we want to be able to close it before they get to initialize
1381          */
1382        if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
1383            Vector<wp<ProClient> > proClients(mProClientList[cameraId]);
1384            Vector<wp<ProClient> >::const_iterator it;
1385
1386            for (it = proClients.begin(); it != proClients.end(); ++it) {
1387                sp<ProClient> proCl = it->promote();
1388                if (proCl.get() != NULL) {
1389                    proCl->onExclusiveLockStolen();
1390                }
1391            }
1392        }
1393
1394        Vector<sp<ICameraServiceListener> >::const_iterator it;
1395        for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1396            (*it)->onStatusChanged(status, cameraId);
1397        }
1398    }
1399}
1400
1401ICameraServiceListener::Status CameraService::getStatus(int cameraId) const {
1402    if (cameraId < 0 || cameraId >= MAX_CAMERAS) {
1403        ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
1404        return ICameraServiceListener::STATUS_UNKNOWN;
1405    }
1406
1407    Mutex::Autolock al(mStatusMutex);
1408    return mStatusList[cameraId];
1409}
1410
1411}; // namespace android
1412