CameraService.cpp revision 32ce52b01dab863f2b4749e1c69f619cf820486f
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 %zu 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 ALOGW("%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 if (listener == 0) { 723 ALOGE("%s: Listener must not be null", __FUNCTION__); 724 return BAD_VALUE; 725 } 726 727 Mutex::Autolock lock(mServiceLock); 728 729 Vector<sp<ICameraServiceListener> >::iterator it, end; 730 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { 731 if ((*it)->asBinder() == listener->asBinder()) { 732 ALOGW("%s: Tried to add listener %p which was already subscribed", 733 __FUNCTION__, listener.get()); 734 return ALREADY_EXISTS; 735 } 736 } 737 738 mListenerList.push_back(listener); 739 740 /* Immediately signal current status to this listener only */ 741 { 742 Mutex::Autolock m(mStatusMutex) ; 743 int numCams = getNumberOfCameras(); 744 for (int i = 0; i < numCams; ++i) { 745 listener->onStatusChanged(mStatusList[i], i); 746 } 747 } 748 749 return OK; 750} 751status_t CameraService::removeListener( 752 const sp<ICameraServiceListener>& listener) { 753 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); 754 755 if (listener == 0) { 756 ALOGE("%s: Listener must not be null", __FUNCTION__); 757 return BAD_VALUE; 758 } 759 760 Mutex::Autolock lock(mServiceLock); 761 762 Vector<sp<ICameraServiceListener> >::iterator it; 763 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { 764 if ((*it)->asBinder() == listener->asBinder()) { 765 mListenerList.erase(it); 766 return OK; 767 } 768 } 769 770 ALOGW("%s: Tried to remove a listener %p which was not subscribed", 771 __FUNCTION__, listener.get()); 772 773 return BAD_VALUE; 774} 775 776void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) { 777 int callingPid = getCallingPid(); 778 LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid); 779 780 // Declare this before the lock to make absolutely sure the 781 // destructor won't be called with the lock held. 782 Mutex::Autolock lock(mServiceLock); 783 784 int outIndex; 785 sp<BasicClient> client = findClientUnsafe(remoteBinder, outIndex); 786 787 if (client != 0) { 788 // Found our camera, clear and leave. 789 LOG1("removeClient: clear camera %d", outIndex); 790 mClient[outIndex].clear(); 791 792 client->getRemote()->unlinkToDeath(this); 793 } else { 794 795 sp<ProClient> clientPro = findProClientUnsafe(remoteBinder); 796 797 if (clientPro != NULL) { 798 // Found our camera, clear and leave. 799 LOG1("removeClient: clear pro %p", clientPro.get()); 800 801 clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this); 802 } 803 } 804 805 LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid); 806} 807 808sp<CameraService::ProClient> CameraService::findProClientUnsafe( 809 const wp<IBinder>& cameraCallbacksRemote) 810{ 811 sp<ProClient> clientPro; 812 813 for (int i = 0; i < mNumberOfCameras; ++i) { 814 Vector<size_t> removeIdx; 815 816 for (size_t j = 0; j < mProClientList[i].size(); ++j) { 817 wp<ProClient> cl = mProClientList[i][j]; 818 819 sp<ProClient> clStrong = cl.promote(); 820 if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) { 821 clientPro = clStrong; 822 break; 823 } else if (clStrong == NULL) { 824 // mark to clean up dead ptr 825 removeIdx.push(j); 826 } 827 } 828 829 // remove stale ptrs (in reverse so the indices dont change) 830 for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) { 831 mProClientList[i].removeAt(removeIdx[j]); 832 } 833 834 } 835 836 return clientPro; 837} 838 839sp<CameraService::BasicClient> CameraService::findClientUnsafe( 840 const wp<IBinder>& cameraClient, int& outIndex) { 841 sp<BasicClient> client; 842 843 for (int i = 0; i < mNumberOfCameras; i++) { 844 845 // This happens when we have already disconnected (or this is 846 // just another unused camera). 847 if (mClient[i] == 0) continue; 848 849 // Promote mClient. It can fail if we are called from this path: 850 // Client::~Client() -> disconnect() -> removeClientByRemote(). 851 client = mClient[i].promote(); 852 853 // Clean up stale client entry 854 if (client == NULL) { 855 mClient[i].clear(); 856 continue; 857 } 858 859 if (cameraClient == client->getRemote()) { 860 // Found our camera 861 outIndex = i; 862 return client; 863 } 864 } 865 866 outIndex = -1; 867 return NULL; 868} 869 870CameraService::BasicClient* CameraService::getClientByIdUnsafe(int cameraId) { 871 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; 872 return mClient[cameraId].unsafe_get(); 873} 874 875Mutex* CameraService::getClientLockById(int cameraId) { 876 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; 877 return &mClientLock[cameraId]; 878} 879 880sp<CameraService::BasicClient> CameraService::getClientByRemote( 881 const wp<IBinder>& cameraClient) { 882 883 // Declare this before the lock to make absolutely sure the 884 // destructor won't be called with the lock held. 885 sp<BasicClient> client; 886 887 Mutex::Autolock lock(mServiceLock); 888 889 int outIndex; 890 client = findClientUnsafe(cameraClient, outIndex); 891 892 return client; 893} 894 895status_t CameraService::onTransact( 896 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { 897 // Permission checks 898 switch (code) { 899 case BnCameraService::CONNECT: 900 case BnCameraService::CONNECT_PRO: 901 const int pid = getCallingPid(); 902 const int self_pid = getpid(); 903 if (pid != self_pid) { 904 // we're called from a different process, do the real check 905 if (!checkCallingPermission( 906 String16("android.permission.CAMERA"))) { 907 const int uid = getCallingUid(); 908 ALOGE("Permission Denial: " 909 "can't use the camera pid=%d, uid=%d", pid, uid); 910 return PERMISSION_DENIED; 911 } 912 } 913 break; 914 } 915 916 return BnCameraService::onTransact(code, data, reply, flags); 917} 918 919// The reason we need this busy bit is a new CameraService::connect() request 920// may come in while the previous Client's destructor has not been run or is 921// still running. If the last strong reference of the previous Client is gone 922// but the destructor has not been finished, we should not allow the new Client 923// to be created because we need to wait for the previous Client to tear down 924// the hardware first. 925void CameraService::setCameraBusy(int cameraId) { 926 android_atomic_write(1, &mBusy[cameraId]); 927 928 ALOGV("setCameraBusy cameraId=%d", cameraId); 929} 930 931void CameraService::setCameraFree(int cameraId) { 932 android_atomic_write(0, &mBusy[cameraId]); 933 934 ALOGV("setCameraFree cameraId=%d", cameraId); 935} 936 937// We share the media players for shutter and recording sound for all clients. 938// A reference count is kept to determine when we will actually release the 939// media players. 940 941MediaPlayer* CameraService::newMediaPlayer(const char *file) { 942 MediaPlayer* mp = new MediaPlayer(); 943 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) { 944 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); 945 mp->prepare(); 946 } else { 947 ALOGE("Failed to load CameraService sounds: %s", file); 948 return NULL; 949 } 950 return mp; 951} 952 953void CameraService::loadSound() { 954 Mutex::Autolock lock(mSoundLock); 955 LOG1("CameraService::loadSound ref=%d", mSoundRef); 956 if (mSoundRef++) return; 957 958 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); 959 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); 960} 961 962void CameraService::releaseSound() { 963 Mutex::Autolock lock(mSoundLock); 964 LOG1("CameraService::releaseSound ref=%d", mSoundRef); 965 if (--mSoundRef) return; 966 967 for (int i = 0; i < NUM_SOUNDS; i++) { 968 if (mSoundPlayer[i] != 0) { 969 mSoundPlayer[i]->disconnect(); 970 mSoundPlayer[i].clear(); 971 } 972 } 973} 974 975void CameraService::playSound(sound_kind kind) { 976 LOG1("playSound(%d)", kind); 977 Mutex::Autolock lock(mSoundLock); 978 sp<MediaPlayer> player = mSoundPlayer[kind]; 979 if (player != 0) { 980 player->seekTo(0); 981 player->start(); 982 } 983} 984 985// ---------------------------------------------------------------------------- 986 987CameraService::Client::Client(const sp<CameraService>& cameraService, 988 const sp<ICameraClient>& cameraClient, 989 const String16& clientPackageName, 990 int cameraId, int cameraFacing, 991 int clientPid, uid_t clientUid, 992 int servicePid) : 993 CameraService::BasicClient(cameraService, cameraClient->asBinder(), 994 clientPackageName, 995 cameraId, cameraFacing, 996 clientPid, clientUid, 997 servicePid) 998{ 999 int callingPid = getCallingPid(); 1000 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId); 1001 1002 mRemoteCallback = cameraClient; 1003 1004 cameraService->setCameraBusy(cameraId); 1005 cameraService->loadSound(); 1006 1007 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId); 1008} 1009 1010// tear down the client 1011CameraService::Client::~Client() { 1012 ALOGV("~Client"); 1013 mDestructionStarted = true; 1014 1015 mCameraService->releaseSound(); 1016 // unconditionally disconnect. function is idempotent 1017 Client::disconnect(); 1018} 1019 1020CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, 1021 const sp<IBinder>& remoteCallback, 1022 const String16& clientPackageName, 1023 int cameraId, int cameraFacing, 1024 int clientPid, uid_t clientUid, 1025 int servicePid): 1026 mClientPackageName(clientPackageName) 1027{ 1028 mCameraService = cameraService; 1029 mRemoteBinder = remoteCallback; 1030 mCameraId = cameraId; 1031 mCameraFacing = cameraFacing; 1032 mClientPid = clientPid; 1033 mClientUid = clientUid; 1034 mServicePid = servicePid; 1035 mOpsActive = false; 1036 mDestructionStarted = false; 1037} 1038 1039CameraService::BasicClient::~BasicClient() { 1040 ALOGV("~BasicClient"); 1041 mDestructionStarted = true; 1042} 1043 1044void CameraService::BasicClient::disconnect() { 1045 ALOGV("BasicClient::disconnect"); 1046 mCameraService->removeClientByRemote(mRemoteBinder); 1047 // client shouldn't be able to call into us anymore 1048 mClientPid = 0; 1049} 1050 1051status_t CameraService::BasicClient::startCameraOps() { 1052 int32_t res; 1053 1054 mOpsCallback = new OpsCallback(this); 1055 1056 { 1057 ALOGV("%s: Start camera ops, package name = %s, client UID = %d", 1058 __FUNCTION__, String8(mClientPackageName).string(), mClientUid); 1059 } 1060 1061 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA, 1062 mClientPackageName, mOpsCallback); 1063 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA, 1064 mClientUid, mClientPackageName); 1065 1066 if (res != AppOpsManager::MODE_ALLOWED) { 1067 ALOGI("Camera %d: Access for \"%s\" has been revoked", 1068 mCameraId, String8(mClientPackageName).string()); 1069 return PERMISSION_DENIED; 1070 } 1071 mOpsActive = true; 1072 return OK; 1073} 1074 1075status_t CameraService::BasicClient::finishCameraOps() { 1076 if (mOpsActive) { 1077 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid, 1078 mClientPackageName); 1079 mOpsActive = false; 1080 } 1081 mAppOpsManager.stopWatchingMode(mOpsCallback); 1082 mOpsCallback.clear(); 1083 1084 return OK; 1085} 1086 1087void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) { 1088 String8 name(packageName); 1089 String8 myName(mClientPackageName); 1090 1091 if (op != AppOpsManager::OP_CAMERA) { 1092 ALOGW("Unexpected app ops notification received: %d", op); 1093 return; 1094 } 1095 1096 int32_t res; 1097 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA, 1098 mClientUid, mClientPackageName); 1099 ALOGV("checkOp returns: %d, %s ", res, 1100 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : 1101 res == AppOpsManager::MODE_IGNORED ? "IGNORED" : 1102 res == AppOpsManager::MODE_ERRORED ? "ERRORED" : 1103 "UNKNOWN"); 1104 1105 if (res != AppOpsManager::MODE_ALLOWED) { 1106 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId, 1107 myName.string()); 1108 // Reset the client PID to allow server-initiated disconnect, 1109 // and to prevent further calls by client. 1110 mClientPid = getCallingPid(); 1111 notifyError(); 1112 disconnect(); 1113 } 1114} 1115 1116// ---------------------------------------------------------------------------- 1117 1118Mutex* CameraService::Client::getClientLockFromCookie(void* user) { 1119 return gCameraService->getClientLockById((int)(intptr_t) user); 1120} 1121 1122// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should 1123// be acquired for this to be safe 1124CameraService::Client* CameraService::Client::getClientFromCookie(void* user) { 1125 BasicClient *basicClient = gCameraService->getClientByIdUnsafe((int)(intptr_t) user); 1126 // OK: only CameraClient calls this, and they already cast anyway. 1127 Client* client = static_cast<Client*>(basicClient); 1128 1129 // This could happen if the Client is in the process of shutting down (the 1130 // last strong reference is gone, but the destructor hasn't finished 1131 // stopping the hardware). 1132 if (client == NULL) return NULL; 1133 1134 // destruction already started, so should not be accessed 1135 if (client->mDestructionStarted) return NULL; 1136 1137 return client; 1138} 1139 1140void CameraService::Client::notifyError() { 1141 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); 1142} 1143 1144// NOTE: function is idempotent 1145void CameraService::Client::disconnect() { 1146 ALOGV("Client::disconnect"); 1147 BasicClient::disconnect(); 1148 mCameraService->setCameraFree(mCameraId); 1149 1150 StatusVector rejectSourceStates; 1151 rejectSourceStates.push_back(ICameraServiceListener::STATUS_NOT_PRESENT); 1152 rejectSourceStates.push_back(ICameraServiceListener::STATUS_ENUMERATING); 1153 1154 // Transition to PRESENT if the camera is not in either of above 2 states 1155 mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT, 1156 mCameraId, 1157 &rejectSourceStates); 1158} 1159 1160CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): 1161 mClient(client) { 1162} 1163 1164void CameraService::Client::OpsCallback::opChanged(int32_t op, 1165 const String16& packageName) { 1166 sp<BasicClient> client = mClient.promote(); 1167 if (client != NULL) { 1168 client->opChanged(op, packageName); 1169 } 1170} 1171 1172// ---------------------------------------------------------------------------- 1173// IProCamera 1174// ---------------------------------------------------------------------------- 1175 1176CameraService::ProClient::ProClient(const sp<CameraService>& cameraService, 1177 const sp<IProCameraCallbacks>& remoteCallback, 1178 const String16& clientPackageName, 1179 int cameraId, 1180 int cameraFacing, 1181 int clientPid, 1182 uid_t clientUid, 1183 int servicePid) 1184 : CameraService::BasicClient(cameraService, remoteCallback->asBinder(), 1185 clientPackageName, cameraId, cameraFacing, 1186 clientPid, clientUid, servicePid) 1187{ 1188 mRemoteCallback = remoteCallback; 1189} 1190 1191CameraService::ProClient::~ProClient() { 1192} 1193 1194void CameraService::ProClient::notifyError() { 1195 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); 1196} 1197 1198// ---------------------------------------------------------------------------- 1199 1200static const int kDumpLockRetries = 50; 1201static const int kDumpLockSleep = 60000; 1202 1203static bool tryLock(Mutex& mutex) 1204{ 1205 bool locked = false; 1206 for (int i = 0; i < kDumpLockRetries; ++i) { 1207 if (mutex.tryLock() == NO_ERROR) { 1208 locked = true; 1209 break; 1210 } 1211 usleep(kDumpLockSleep); 1212 } 1213 return locked; 1214} 1215 1216status_t CameraService::dump(int fd, const Vector<String16>& args) { 1217 String8 result; 1218 if (checkCallingPermission(String16("android.permission.DUMP")) == false) { 1219 result.appendFormat("Permission Denial: " 1220 "can't dump CameraService from pid=%d, uid=%d\n", 1221 getCallingPid(), 1222 getCallingUid()); 1223 write(fd, result.string(), result.size()); 1224 } else { 1225 bool locked = tryLock(mServiceLock); 1226 // failed to lock - CameraService is probably deadlocked 1227 if (!locked) { 1228 result.append("CameraService may be deadlocked\n"); 1229 write(fd, result.string(), result.size()); 1230 } 1231 1232 bool hasClient = false; 1233 if (!mModule) { 1234 result = String8::format("No camera module available!\n"); 1235 write(fd, result.string(), result.size()); 1236 return NO_ERROR; 1237 } 1238 1239 result = String8::format("Camera module HAL API version: 0x%x\n", 1240 mModule->common.hal_api_version); 1241 result.appendFormat("Camera module API version: 0x%x\n", 1242 mModule->common.module_api_version); 1243 result.appendFormat("Camera module name: %s\n", 1244 mModule->common.name); 1245 result.appendFormat("Camera module author: %s\n", 1246 mModule->common.author); 1247 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras); 1248 write(fd, result.string(), result.size()); 1249 for (int i = 0; i < mNumberOfCameras; i++) { 1250 result = String8::format("Camera %d static information:\n", i); 1251 camera_info info; 1252 1253 status_t rc = mModule->get_camera_info(i, &info); 1254 if (rc != OK) { 1255 result.appendFormat(" Error reading static information!\n"); 1256 write(fd, result.string(), result.size()); 1257 } else { 1258 result.appendFormat(" Facing: %s\n", 1259 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT"); 1260 result.appendFormat(" Orientation: %d\n", info.orientation); 1261 int deviceVersion; 1262 if (mModule->common.module_api_version < 1263 CAMERA_MODULE_API_VERSION_2_0) { 1264 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; 1265 } else { 1266 deviceVersion = info.device_version; 1267 } 1268 result.appendFormat(" Device version: 0x%x\n", deviceVersion); 1269 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) { 1270 result.appendFormat(" Device static metadata:\n"); 1271 write(fd, result.string(), result.size()); 1272 dump_indented_camera_metadata(info.static_camera_characteristics, 1273 fd, 2, 4); 1274 } else { 1275 write(fd, result.string(), result.size()); 1276 } 1277 } 1278 1279 sp<BasicClient> client = mClient[i].promote(); 1280 if (client == 0) { 1281 result = String8::format(" Device is closed, no client instance\n"); 1282 write(fd, result.string(), result.size()); 1283 continue; 1284 } 1285 hasClient = true; 1286 result = String8::format(" Device is open. Client instance dump:\n"); 1287 write(fd, result.string(), result.size()); 1288 client->dump(fd, args); 1289 } 1290 if (!hasClient) { 1291 result = String8::format("\nNo active camera clients yet.\n"); 1292 write(fd, result.string(), result.size()); 1293 } 1294 1295 if (locked) mServiceLock.unlock(); 1296 1297 // Dump camera traces if there were any 1298 write(fd, "\n", 1); 1299 camera3::CameraTraces::dump(fd, args); 1300 1301 // change logging level 1302 int n = args.size(); 1303 for (int i = 0; i + 1 < n; i++) { 1304 String16 verboseOption("-v"); 1305 if (args[i] == verboseOption) { 1306 String8 levelStr(args[i+1]); 1307 int level = atoi(levelStr.string()); 1308 result = String8::format("\nSetting log level to %d.\n", level); 1309 setLogLevel(level); 1310 write(fd, result.string(), result.size()); 1311 } 1312 } 1313 1314 } 1315 return NO_ERROR; 1316} 1317 1318/*virtual*/void CameraService::binderDied( 1319 const wp<IBinder> &who) { 1320 1321 /** 1322 * While tempting to promote the wp<IBinder> into a sp, 1323 * it's actually not supported by the binder driver 1324 */ 1325 1326 ALOGV("java clients' binder died"); 1327 1328 sp<BasicClient> cameraClient = getClientByRemote(who); 1329 1330 if (cameraClient == 0) { 1331 ALOGV("java clients' binder death already cleaned up (normal case)"); 1332 return; 1333 } 1334 1335 ALOGW("Disconnecting camera client %p since the binder for it " 1336 "died (this pid %d)", cameraClient.get(), getCallingPid()); 1337 1338 cameraClient->disconnect(); 1339 1340} 1341 1342void CameraService::updateStatus(ICameraServiceListener::Status status, 1343 int32_t cameraId, 1344 const StatusVector *rejectSourceStates) { 1345 // do not lock mServiceLock here or can get into a deadlock from 1346 // connect() -> ProClient::disconnect -> updateStatus 1347 Mutex::Autolock lock(mStatusMutex); 1348 1349 ICameraServiceListener::Status oldStatus = mStatusList[cameraId]; 1350 1351 mStatusList[cameraId] = status; 1352 1353 if (oldStatus != status) { 1354 ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x", 1355 __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status); 1356 1357 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT && 1358 (status != ICameraServiceListener::STATUS_PRESENT && 1359 status != ICameraServiceListener::STATUS_ENUMERATING)) { 1360 1361 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT" 1362 " or ENUMERATING", __FUNCTION__); 1363 mStatusList[cameraId] = oldStatus; 1364 return; 1365 } 1366 1367 if (rejectSourceStates != NULL) { 1368 const StatusVector &rejectList = *rejectSourceStates; 1369 StatusVector::const_iterator it = rejectList.begin(); 1370 1371 /** 1372 * Sometimes we want to conditionally do a transition. 1373 * For example if a client disconnects, we want to go to PRESENT 1374 * only if we weren't already in NOT_PRESENT or ENUMERATING. 1375 */ 1376 for (; it != rejectList.end(); ++it) { 1377 if (oldStatus == *it) { 1378 ALOGV("%s: Rejecting status transition for Camera ID %d, " 1379 " since the source state was was in one of the bad " 1380 " states.", __FUNCTION__, cameraId); 1381 mStatusList[cameraId] = oldStatus; 1382 return; 1383 } 1384 } 1385 } 1386 1387 /** 1388 * ProClients lose their exclusive lock. 1389 * - Done before the CameraClient can initialize the HAL device, 1390 * since we want to be able to close it before they get to initialize 1391 */ 1392 if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) { 1393 Vector<wp<ProClient> > proClients(mProClientList[cameraId]); 1394 Vector<wp<ProClient> >::const_iterator it; 1395 1396 for (it = proClients.begin(); it != proClients.end(); ++it) { 1397 sp<ProClient> proCl = it->promote(); 1398 if (proCl.get() != NULL) { 1399 proCl->onExclusiveLockStolen(); 1400 } 1401 } 1402 } 1403 1404 Vector<sp<ICameraServiceListener> >::const_iterator it; 1405 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { 1406 (*it)->onStatusChanged(status, cameraId); 1407 } 1408 } 1409} 1410 1411ICameraServiceListener::Status CameraService::getStatus(int cameraId) const { 1412 if (cameraId < 0 || cameraId >= MAX_CAMERAS) { 1413 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId); 1414 return ICameraServiceListener::STATUS_UNKNOWN; 1415 } 1416 1417 Mutex::Autolock al(mStatusMutex); 1418 return mStatusList[cameraId]; 1419} 1420 1421}; // namespace android 1422