CameraService.cpp revision 3068d73c6c7e1f44523b1466b903a9c82408b258
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#include <system/camera_metadata.h> 43#include <system/camera.h> 44 45#include "CameraService.h" 46#include "api1/CameraClient.h" 47#include "api1/Camera2Client.h" 48#include "api_pro/ProCamera2Client.h" 49#include "api2/CameraDeviceClient.h" 50#include "utils/CameraTraces.h" 51#include "CameraDeviceFactory.h" 52 53namespace android { 54 55// ---------------------------------------------------------------------------- 56// Logging support -- this is for debugging only 57// Use "adb shell dumpsys media.camera -v 1" to change it. 58volatile int32_t gLogLevel = 0; 59 60#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); 61#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); 62 63static void setLogLevel(int level) { 64 android_atomic_write(level, &gLogLevel); 65} 66 67// ---------------------------------------------------------------------------- 68 69static int getCallingPid() { 70 return IPCThreadState::self()->getCallingPid(); 71} 72 73static int getCallingUid() { 74 return IPCThreadState::self()->getCallingUid(); 75} 76 77extern "C" { 78static void camera_device_status_change( 79 const struct camera_module_callbacks* callbacks, 80 int camera_id, 81 int new_status) { 82 sp<CameraService> cs = const_cast<CameraService*>( 83 static_cast<const CameraService*>(callbacks)); 84 85 cs->onDeviceStatusChanged( 86 camera_id, 87 new_status); 88} 89 90static void torch_mode_status_change( 91 const struct camera_module_callbacks* callbacks, 92 const char* camera_id, 93 int new_status) { 94 if (!callbacks || !camera_id) { 95 ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__, 96 callbacks, camera_id); 97 } 98 sp<CameraService> cs = const_cast<CameraService*>( 99 static_cast<const CameraService*>(callbacks)); 100 101 ICameraServiceListener::TorchStatus status; 102 switch (new_status) { 103 case TORCH_MODE_STATUS_AVAILABLE: 104 status = ICameraServiceListener::TORCH_STATUS_AVAILABLE; 105 break; 106 case TORCH_MODE_STATUS_RESOURCE_BUSY: 107 status = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; 108 break; 109 case TORCH_MODE_STATUS_OFF: 110 status = ICameraServiceListener::TORCH_STATUS_OFF; 111 break; 112 default: 113 ALOGE("Unknown torch status %d", new_status); 114 return; 115 } 116 117 cs->onTorchStatusChanged( 118 String16(camera_id), 119 status); 120} 121} // extern "C" 122 123// ---------------------------------------------------------------------------- 124 125// This is ugly and only safe if we never re-create the CameraService, but 126// should be ok for now. 127static CameraService *gCameraService; 128 129CameraService::CameraService() 130 :mSoundRef(0), mModule(0), mFlashlight(0) 131{ 132 ALOGI("CameraService started (pid=%d)", getpid()); 133 gCameraService = this; 134 135 for (size_t i = 0; i < MAX_CAMERAS; ++i) { 136 mStatusList[i] = ICameraServiceListener::STATUS_PRESENT; 137 } 138 139 this->camera_device_status_change = android::camera_device_status_change; 140 this->torch_mode_status_change = android::torch_mode_status_change; 141 142} 143 144void CameraService::onFirstRef() 145{ 146 LOG1("CameraService::onFirstRef"); 147 148 BnCameraService::onFirstRef(); 149 150 camera_module_t *rawModule; 151 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID, 152 (const hw_module_t **)&rawModule) < 0) { 153 ALOGE("Could not load camera HAL module"); 154 mNumberOfCameras = 0; 155 } 156 else { 157 mModule = new CameraModule(rawModule); 158 mFlashlight = new CameraFlashlight(*mModule, *this); 159 160 const hw_module_t *common = mModule->getRawModule(); 161 ALOGI("Loaded \"%s\" camera module", common->name); 162 mNumberOfCameras = mModule->getNumberOfCameras(); 163 if (mNumberOfCameras > MAX_CAMERAS) { 164 ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).", 165 mNumberOfCameras, MAX_CAMERAS); 166 mNumberOfCameras = MAX_CAMERAS; 167 } 168 for (int i = 0; i < mNumberOfCameras; i++) { 169 setCameraFree(i); 170 171 String16 cameraName = String16(String8::format("%d", i)); 172 if (mFlashlight->hasFlashUnit(cameraName)) { 173 mTorchStatusMap.add(cameraName, 174 ICameraServiceListener::TORCH_STATUS_AVAILABLE); 175 } 176 } 177 178 if (common->module_api_version >= CAMERA_MODULE_API_VERSION_2_1) { 179 mModule->setCallbacks(this); 180 } 181 182 VendorTagDescriptor::clearGlobalVendorTagDescriptor(); 183 184 if (common->module_api_version >= CAMERA_MODULE_API_VERSION_2_2) { 185 setUpVendorTags(); 186 } 187 188 CameraDeviceFactory::registerService(this); 189 } 190} 191 192CameraService::~CameraService() { 193 for (int i = 0; i < mNumberOfCameras; i++) { 194 if (mBusy[i]) { 195 ALOGE("camera %d is still in use in destructor!", i); 196 } 197 } 198 199 if (mModule) { 200 delete mModule; 201 } 202 VendorTagDescriptor::clearGlobalVendorTagDescriptor(); 203 gCameraService = NULL; 204} 205 206void CameraService::onDeviceStatusChanged(int cameraId, 207 int newStatus) 208{ 209 ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__, 210 cameraId, newStatus); 211 212 if (cameraId < 0 || cameraId >= MAX_CAMERAS) { 213 ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId); 214 return; 215 } 216 217 if ((int)getStatus(cameraId) == newStatus) { 218 ALOGE("%s: State transition to the same status 0x%x not allowed", 219 __FUNCTION__, (uint32_t)newStatus); 220 return; 221 } 222 223 /* don't do this in updateStatus 224 since it is also called from connect and we could get into a deadlock */ 225 if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) { 226 Vector<sp<BasicClient> > clientsToDisconnect; 227 { 228 Mutex::Autolock al(mServiceLock); 229 230 /* Remove cached parameters from shim cache */ 231 mShimParams.removeItem(cameraId); 232 233 /* Find all clients that we need to disconnect */ 234 sp<BasicClient> client = mClient[cameraId].promote(); 235 if (client.get() != NULL) { 236 clientsToDisconnect.push_back(client); 237 } 238 239 int i = cameraId; 240 for (size_t j = 0; j < mProClientList[i].size(); ++j) { 241 sp<ProClient> cl = mProClientList[i][j].promote(); 242 if (cl != NULL) { 243 clientsToDisconnect.push_back(cl); 244 } 245 } 246 } 247 248 /* now disconnect them. don't hold the lock 249 or we can get into a deadlock */ 250 251 for (size_t i = 0; i < clientsToDisconnect.size(); ++i) { 252 sp<BasicClient> client = clientsToDisconnect[i]; 253 254 client->disconnect(); 255 /** 256 * The remote app will no longer be able to call methods on the 257 * client since the client PID will be reset to 0 258 */ 259 } 260 261 ALOGV("%s: After unplug, disconnected %zu clients", 262 __FUNCTION__, clientsToDisconnect.size()); 263 } 264 265 updateStatus( 266 static_cast<ICameraServiceListener::Status>(newStatus), cameraId); 267 268} 269 270void CameraService::onTorchStatusChanged(const String16& cameraId, 271 ICameraServiceListener::TorchStatus newStatus) { 272 Mutex::Autolock al(mTorchStatusMutex); 273 onTorchStatusChangedLocked(cameraId, newStatus); 274} 275 276void CameraService::onTorchStatusChangedLocked(const String16& cameraId, 277 ICameraServiceListener::TorchStatus newStatus) { 278 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", 279 __FUNCTION__, cameraId.string(), newStatus); 280 281 if (getTorchStatusLocked(cameraId) == newStatus) { 282 ALOGE("%s: Torch state transition to the same status 0x%x not allowed", 283 __FUNCTION__, (uint32_t)newStatus); 284 return; 285 } 286 287 status_t res = setTorchStatusLocked(cameraId, newStatus); 288 if (res) { 289 ALOGE("%s: Failed to set the torch status", __FUNCTION__, 290 (uint32_t)newStatus); 291 return; 292 } 293 294 Vector<sp<ICameraServiceListener> >::const_iterator it; 295 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { 296 (*it)->onTorchStatusChanged(newStatus, cameraId); 297 } 298} 299 300 301int32_t CameraService::getNumberOfCameras() { 302 return mNumberOfCameras; 303} 304 305status_t CameraService::getCameraInfo(int cameraId, 306 struct CameraInfo* cameraInfo) { 307 if (!mModule) { 308 return -ENODEV; 309 } 310 311 if (cameraId < 0 || cameraId >= mNumberOfCameras) { 312 return BAD_VALUE; 313 } 314 315 struct camera_info info; 316 status_t rc = filterGetInfoErrorCode( 317 mModule->getCameraInfo(cameraId, &info)); 318 cameraInfo->facing = info.facing; 319 cameraInfo->orientation = info.orientation; 320 return rc; 321} 322 323 324status_t CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) { 325 status_t ret = OK; 326 struct CameraInfo info; 327 if ((ret = getCameraInfo(cameraId, &info)) != OK) { 328 return ret; 329 } 330 331 CameraMetadata shimInfo; 332 int32_t orientation = static_cast<int32_t>(info.orientation); 333 if ((ret = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) { 334 return ret; 335 } 336 337 uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ? 338 ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK; 339 if ((ret = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) { 340 return ret; 341 } 342 343 CameraParameters shimParams; 344 if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) { 345 // Error logged by callee 346 return ret; 347 } 348 349 Vector<Size> sizes; 350 Vector<Size> jpegSizes; 351 Vector<int32_t> formats; 352 const char* supportedPreviewFormats; 353 { 354 shimParams.getSupportedPreviewSizes(/*out*/sizes); 355 shimParams.getSupportedPreviewFormats(/*out*/formats); 356 shimParams.getSupportedPictureSizes(/*out*/jpegSizes); 357 } 358 359 // Always include IMPLEMENTATION_DEFINED 360 formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED); 361 362 const size_t INTS_PER_CONFIG = 4; 363 364 // Build available stream configurations metadata 365 size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG; 366 367 Vector<int32_t> streamConfigs; 368 streamConfigs.setCapacity(streamConfigSize); 369 370 for (size_t i = 0; i < formats.size(); ++i) { 371 for (size_t j = 0; j < sizes.size(); ++j) { 372 streamConfigs.add(formats[i]); 373 streamConfigs.add(sizes[j].width); 374 streamConfigs.add(sizes[j].height); 375 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); 376 } 377 } 378 379 for (size_t i = 0; i < jpegSizes.size(); ++i) { 380 streamConfigs.add(HAL_PIXEL_FORMAT_BLOB); 381 streamConfigs.add(jpegSizes[i].width); 382 streamConfigs.add(jpegSizes[i].height); 383 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); 384 } 385 386 if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, 387 streamConfigs.array(), streamConfigSize)) != OK) { 388 return ret; 389 } 390 391 int64_t fakeMinFrames[0]; 392 // TODO: Fixme, don't fake min frame durations. 393 if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, 394 fakeMinFrames, 0)) != OK) { 395 return ret; 396 } 397 398 int64_t fakeStalls[0]; 399 // TODO: Fixme, don't fake stall durations. 400 if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, 401 fakeStalls, 0)) != OK) { 402 return ret; 403 } 404 405 *cameraInfo = shimInfo; 406 return OK; 407} 408 409status_t CameraService::getCameraCharacteristics(int cameraId, 410 CameraMetadata* cameraInfo) { 411 if (!cameraInfo) { 412 ALOGE("%s: cameraInfo is NULL", __FUNCTION__); 413 return BAD_VALUE; 414 } 415 416 if (!mModule) { 417 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__); 418 return -ENODEV; 419 } 420 421 if (cameraId < 0 || cameraId >= mNumberOfCameras) { 422 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId); 423 return BAD_VALUE; 424 } 425 426 int facing; 427 status_t ret = OK; 428 if (mModule->getRawModule()->module_api_version < CAMERA_MODULE_API_VERSION_2_0 || 429 getDeviceVersion(cameraId, &facing) <= CAMERA_DEVICE_API_VERSION_2_1 ) { 430 /** 431 * Backwards compatibility mode for old HALs: 432 * - Convert CameraInfo into static CameraMetadata properties. 433 * - Retrieve cached CameraParameters for this camera. If none exist, 434 * attempt to open CameraClient and retrieve the CameraParameters. 435 * - Convert cached CameraParameters into static CameraMetadata 436 * properties. 437 */ 438 ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__); 439 440 if ((ret = generateShimMetadata(cameraId, cameraInfo)) != OK) { 441 return ret; 442 } 443 444 } else { 445 /** 446 * Normal HAL 2.1+ codepath. 447 */ 448 struct camera_info info; 449 ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info)); 450 *cameraInfo = info.static_camera_characteristics; 451 } 452 453 return ret; 454} 455 456status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) { 457 if (!mModule) { 458 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__); 459 return -ENODEV; 460 } 461 462 desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); 463 return OK; 464} 465 466int CameraService::getDeviceVersion(int cameraId, int* facing) { 467 struct camera_info info; 468 if (mModule->getCameraInfo(cameraId, &info) != OK) { 469 return -1; 470 } 471 472 int deviceVersion; 473 if (mModule->getRawModule()->module_api_version >= CAMERA_MODULE_API_VERSION_2_0) { 474 deviceVersion = info.device_version; 475 } else { 476 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; 477 } 478 479 if (facing) { 480 *facing = info.facing; 481 } 482 483 return deviceVersion; 484} 485 486status_t CameraService::filterOpenErrorCode(status_t err) { 487 switch(err) { 488 case NO_ERROR: 489 case -EBUSY: 490 case -EINVAL: 491 case -EUSERS: 492 return err; 493 default: 494 break; 495 } 496 return -ENODEV; 497} 498 499status_t CameraService::filterGetInfoErrorCode(status_t err) { 500 switch(err) { 501 case NO_ERROR: 502 case -EINVAL: 503 return err; 504 default: 505 break; 506 } 507 return -ENODEV; 508} 509 510bool CameraService::setUpVendorTags() { 511 vendor_tag_ops_t vOps = vendor_tag_ops_t(); 512 513 // Check if vendor operations have been implemented 514 if (!mModule->isVendorTagDefined()) { 515 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__); 516 return false; 517 } 518 519 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops"); 520 mModule->getVendorTagOps(&vOps); 521 ATRACE_END(); 522 523 // Ensure all vendor operations are present 524 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL || 525 vOps.get_section_name == NULL || vOps.get_tag_name == NULL || 526 vOps.get_tag_type == NULL) { 527 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions." 528 , __FUNCTION__); 529 return false; 530 } 531 532 // Read all vendor tag definitions into a descriptor 533 sp<VendorTagDescriptor> desc; 534 status_t res; 535 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc)) 536 != OK) { 537 ALOGE("%s: Could not generate descriptor from vendor tag operations," 538 "received error %s (%d). Camera clients will not be able to use" 539 "vendor tags", __FUNCTION__, strerror(res), res); 540 return false; 541 } 542 543 // Set the global descriptor to use with camera metadata 544 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc); 545 return true; 546} 547 548status_t CameraService::initializeShimMetadata(int cameraId) { 549 int pid = getCallingPid(); 550 int uid = getCallingUid(); 551 status_t ret = validateConnect(cameraId, uid); 552 if (ret != OK) { 553 // Error already logged by callee 554 return ret; 555 } 556 557 bool needsNewClient = false; 558 sp<Client> client; 559 560 String16 internalPackageName("media"); 561 { // Scope for service lock 562 Mutex::Autolock lock(mServiceLock); 563 if (mClient[cameraId] != NULL) { 564 client = static_cast<Client*>(mClient[cameraId].promote().get()); 565 } 566 if (client == NULL) { 567 needsNewClient = true; 568 ret = connectHelperLocked(/*out*/client, 569 /*cameraClient*/NULL, // Empty binder callbacks 570 cameraId, 571 internalPackageName, 572 uid, 573 pid); 574 575 if (ret != OK) { 576 // Error already logged by callee 577 return ret; 578 } 579 } 580 581 if (client == NULL) { 582 ALOGE("%s: Could not connect to client camera device.", __FUNCTION__); 583 return BAD_VALUE; 584 } 585 586 String8 rawParams = client->getParameters(); 587 CameraParameters params(rawParams); 588 mShimParams.add(cameraId, params); 589 } 590 591 // Close client if one was opened solely for this call 592 if (needsNewClient) { 593 client->disconnect(); 594 } 595 return OK; 596} 597 598status_t CameraService::getLegacyParametersLazy(int cameraId, 599 /*out*/ 600 CameraParameters* parameters) { 601 602 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); 603 604 status_t ret = 0; 605 606 if (parameters == NULL) { 607 ALOGE("%s: parameters must not be null", __FUNCTION__); 608 return BAD_VALUE; 609 } 610 611 ssize_t index = -1; 612 { // Scope for service lock 613 Mutex::Autolock lock(mServiceLock); 614 index = mShimParams.indexOfKey(cameraId); 615 // Release service lock so initializeShimMetadata can be called correctly. 616 617 if (index >= 0) { 618 *parameters = mShimParams[index]; 619 } 620 } 621 622 if (index < 0) { 623 int64_t token = IPCThreadState::self()->clearCallingIdentity(); 624 ret = initializeShimMetadata(cameraId); 625 IPCThreadState::self()->restoreCallingIdentity(token); 626 if (ret != OK) { 627 // Error already logged by callee 628 return ret; 629 } 630 631 { // Scope for service lock 632 Mutex::Autolock lock(mServiceLock); 633 index = mShimParams.indexOfKey(cameraId); 634 635 LOG_ALWAYS_FATAL_IF(index < 0, "index should have been initialized"); 636 637 *parameters = mShimParams[index]; 638 } 639 } 640 641 return OK; 642} 643 644status_t CameraService::validateConnect(int cameraId, 645 /*inout*/ 646 int& clientUid) const { 647 648 int callingPid = getCallingPid(); 649 650 if (clientUid == USE_CALLING_UID) { 651 clientUid = getCallingUid(); 652 } else { 653 // We only trust our own process to forward client UIDs 654 if (callingPid != getpid()) { 655 ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)", 656 callingPid); 657 return PERMISSION_DENIED; 658 } 659 } 660 661 if (!mModule) { 662 ALOGE("Camera HAL module not loaded"); 663 return -ENODEV; 664 } 665 666 if (cameraId < 0 || cameraId >= mNumberOfCameras) { 667 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).", 668 callingPid, cameraId); 669 return -ENODEV; 670 } 671 672 char value[PROPERTY_VALUE_MAX]; 673 property_get("sys.secpolicy.camera.disabled", value, "0"); 674 if (strcmp(value, "1") == 0) { 675 // Camera is disabled by DevicePolicyManager. 676 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid); 677 return -EACCES; 678 } 679 680 ICameraServiceListener::Status currentStatus = getStatus(cameraId); 681 if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) { 682 ALOGI("Camera is not plugged in," 683 " connect X (pid %d) rejected", callingPid); 684 return -ENODEV; 685 } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) { 686 ALOGI("Camera is enumerating," 687 " connect X (pid %d) rejected", callingPid); 688 return -EBUSY; 689 } 690 // Else don't check for STATUS_NOT_AVAILABLE. 691 // -- It's done implicitly in canConnectUnsafe /w the mBusy array 692 693 return OK; 694} 695 696bool CameraService::canConnectUnsafe(int cameraId, 697 const String16& clientPackageName, 698 const sp<IBinder>& remoteCallback, 699 sp<BasicClient> &client) { 700 String8 clientName8(clientPackageName); 701 int callingPid = getCallingPid(); 702 703 if (mClient[cameraId] != 0) { 704 client = mClient[cameraId].promote(); 705 if (client != 0) { 706 if (remoteCallback == client->getRemote()) { 707 LOG1("CameraService::connect X (pid %d) (the same client)", 708 callingPid); 709 return true; 710 } else { 711 // TODOSC: need to support 1 regular client, 712 // multiple shared clients here 713 ALOGW("CameraService::connect X (pid %d) rejected" 714 " (existing client).", callingPid); 715 return false; 716 } 717 } 718 mClient[cameraId].clear(); 719 } 720 721 /* 722 mBusy is set to false as the last step of the Client destructor, 723 after which it is guaranteed that the Client destructor has finished ( 724 including any inherited destructors) 725 726 We only need this for a Client subclasses since we don't allow 727 multiple Clents to be opened concurrently, but multiple BasicClient 728 would be fine 729 */ 730 if (mBusy[cameraId]) { 731 ALOGW("CameraService::connect X (pid %d, \"%s\") rejected" 732 " (camera %d is still busy).", callingPid, 733 clientName8.string(), cameraId); 734 return false; 735 } 736 737 return true; 738} 739 740status_t CameraService::connectHelperLocked( 741 /*out*/ 742 sp<Client>& client, 743 /*in*/ 744 const sp<ICameraClient>& cameraClient, 745 int cameraId, 746 const String16& clientPackageName, 747 int clientUid, 748 int callingPid, 749 int halVersion, 750 bool legacyMode) { 751 752 // give flashlight a chance to close devices if necessary. 753 mFlashlight->prepareDeviceOpen(); 754 755 int facing = -1; 756 int deviceVersion = getDeviceVersion(cameraId, &facing); 757 758 if (halVersion < 0 || halVersion == deviceVersion) { 759 // Default path: HAL version is unspecified by caller, create CameraClient 760 // based on device version reported by the HAL. 761 switch(deviceVersion) { 762 case CAMERA_DEVICE_API_VERSION_1_0: 763 client = new CameraClient(this, cameraClient, 764 clientPackageName, cameraId, 765 facing, callingPid, clientUid, getpid(), legacyMode); 766 break; 767 case CAMERA_DEVICE_API_VERSION_2_0: 768 case CAMERA_DEVICE_API_VERSION_2_1: 769 case CAMERA_DEVICE_API_VERSION_3_0: 770 case CAMERA_DEVICE_API_VERSION_3_1: 771 case CAMERA_DEVICE_API_VERSION_3_2: 772 client = new Camera2Client(this, cameraClient, 773 clientPackageName, cameraId, 774 facing, callingPid, clientUid, getpid(), legacyMode); 775 break; 776 case -1: 777 ALOGE("Invalid camera id %d", cameraId); 778 return BAD_VALUE; 779 default: 780 ALOGE("Unknown camera device HAL version: %d", deviceVersion); 781 return INVALID_OPERATION; 782 } 783 } else { 784 // A particular HAL version is requested by caller. Create CameraClient 785 // based on the requested HAL version. 786 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 && 787 halVersion == CAMERA_DEVICE_API_VERSION_1_0) { 788 // Only support higher HAL version device opened as HAL1.0 device. 789 client = new CameraClient(this, cameraClient, 790 clientPackageName, cameraId, 791 facing, callingPid, clientUid, getpid(), legacyMode); 792 } else { 793 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet. 794 ALOGE("Invalid camera HAL version %x: HAL %x device can only be" 795 " opened as HAL %x device", halVersion, deviceVersion, 796 CAMERA_DEVICE_API_VERSION_1_0); 797 return INVALID_OPERATION; 798 } 799 } 800 801 status_t status = connectFinishUnsafe(client, client->getRemote()); 802 if (status != OK) { 803 // this is probably not recoverable.. maybe the client can try again 804 return status; 805 } 806 807 mClient[cameraId] = client; 808 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, 809 getpid()); 810 811 return OK; 812} 813 814status_t CameraService::connect( 815 const sp<ICameraClient>& cameraClient, 816 int cameraId, 817 const String16& clientPackageName, 818 int clientUid, 819 /*out*/ 820 sp<ICamera>& device) { 821 822 String8 clientName8(clientPackageName); 823 int callingPid = getCallingPid(); 824 825 LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid, 826 clientName8.string(), cameraId); 827 828 status_t status = validateConnect(cameraId, /*inout*/clientUid); 829 if (status != OK) { 830 return status; 831 } 832 833 834 sp<Client> client; 835 { 836 Mutex::Autolock lock(mServiceLock); 837 sp<BasicClient> clientTmp; 838 if (!canConnectUnsafe(cameraId, clientPackageName, 839 IInterface::asBinder(cameraClient), 840 /*out*/clientTmp)) { 841 return -EBUSY; 842 } else if (client.get() != NULL) { 843 device = static_cast<Client*>(clientTmp.get()); 844 return OK; 845 } 846 847 status = connectHelperLocked(/*out*/client, 848 cameraClient, 849 cameraId, 850 clientPackageName, 851 clientUid, 852 callingPid); 853 if (status != OK) { 854 return status; 855 } 856 857 } 858 // important: release the mutex here so the client can call back 859 // into the service from its destructor (can be at the end of the call) 860 861 device = client; 862 return OK; 863} 864 865status_t CameraService::connectLegacy( 866 const sp<ICameraClient>& cameraClient, 867 int cameraId, int halVersion, 868 const String16& clientPackageName, 869 int clientUid, 870 /*out*/ 871 sp<ICamera>& device) { 872 873 int apiVersion = mModule->getRawModule()->module_api_version; 874 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED && 875 apiVersion < CAMERA_MODULE_API_VERSION_2_3) { 876 /* 877 * Either the HAL version is unspecified in which case this just creates 878 * a camera client selected by the latest device version, or 879 * it's a particular version in which case the HAL must supported 880 * the open_legacy call 881 */ 882 ALOGE("%s: camera HAL module version %x doesn't support connecting to legacy HAL devices!", 883 __FUNCTION__, apiVersion); 884 return INVALID_OPERATION; 885 } 886 887 String8 clientName8(clientPackageName); 888 int callingPid = getCallingPid(); 889 890 LOG1("CameraService::connect legacy E (pid %d \"%s\", id %d)", callingPid, 891 clientName8.string(), cameraId); 892 893 status_t status = validateConnect(cameraId, /*inout*/clientUid); 894 if (status != OK) { 895 return status; 896 } 897 898 sp<Client> client; 899 { 900 Mutex::Autolock lock(mServiceLock); 901 sp<BasicClient> clientTmp; 902 if (!canConnectUnsafe(cameraId, clientPackageName, 903 IInterface::asBinder(cameraClient), 904 /*out*/clientTmp)) { 905 return -EBUSY; 906 } else if (client.get() != NULL) { 907 device = static_cast<Client*>(clientTmp.get()); 908 return OK; 909 } 910 911 status = connectHelperLocked(/*out*/client, 912 cameraClient, 913 cameraId, 914 clientPackageName, 915 clientUid, 916 callingPid, 917 halVersion, 918 /*legacyMode*/true); 919 if (status != OK) { 920 return status; 921 } 922 923 } 924 // important: release the mutex here so the client can call back 925 // into the service from its destructor (can be at the end of the call) 926 927 device = client; 928 return OK; 929} 930 931status_t CameraService::setTorchMode(const String16& cameraId, bool enabled, 932 const sp<IBinder>& clientBinder) { 933 if (enabled && clientBinder == NULL) { 934 ALOGE("%s: torch client binder is NULL", __FUNCTION__); 935 return -ENOSYS; 936 } 937 938 Mutex::Autolock al(mTorchStatusMutex); 939 status_t res = mFlashlight->setTorchMode(cameraId, enabled); 940 if (res) { 941 ALOGE("%s: setting torch mode of camera %s to %d failed", __FUNCTION__, 942 cameraId.string(), enabled); 943 return res; 944 } 945 946 // update the link to client's death 947 ssize_t index = mTorchClientMap.indexOfKey(cameraId); 948 if (enabled) { 949 if (index == NAME_NOT_FOUND) { 950 mTorchClientMap.add(cameraId, clientBinder); 951 } else { 952 const sp<IBinder> oldBinder = mTorchClientMap.valueAt(index); 953 oldBinder->unlinkToDeath(this); 954 955 mTorchClientMap.replaceValueAt(index, clientBinder); 956 } 957 clientBinder->linkToDeath(this); 958 } else if (index != NAME_NOT_FOUND) { 959 sp<IBinder> oldBinder = mTorchClientMap.valueAt(index); 960 oldBinder->unlinkToDeath(this); 961 } 962 963 // notify the listeners the change. 964 ICameraServiceListener::TorchStatus status = enabled ? 965 ICameraServiceListener::TORCH_STATUS_ON : 966 ICameraServiceListener::TORCH_STATUS_OFF; 967 onTorchStatusChangedLocked(cameraId, status); 968 969 return OK; 970} 971 972status_t CameraService::connectFinishUnsafe(const sp<BasicClient>& client, 973 const sp<IBinder>& remoteCallback) { 974 status_t status = client->initialize(mModule); 975 if (status != OK) { 976 ALOGE("%s: Could not initialize client from HAL module.", __FUNCTION__); 977 return status; 978 } 979 if (remoteCallback != NULL) { 980 remoteCallback->linkToDeath(this); 981 } 982 983 return OK; 984} 985 986status_t CameraService::connectPro( 987 const sp<IProCameraCallbacks>& cameraCb, 988 int cameraId, 989 const String16& clientPackageName, 990 int clientUid, 991 /*out*/ 992 sp<IProCameraUser>& device) 993{ 994 if (cameraCb == 0) { 995 ALOGE("%s: Callback must not be null", __FUNCTION__); 996 return BAD_VALUE; 997 } 998 999 String8 clientName8(clientPackageName); 1000 int callingPid = getCallingPid(); 1001 1002 LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid, 1003 clientName8.string(), cameraId); 1004 status_t status = validateConnect(cameraId, /*inout*/clientUid); 1005 if (status != OK) { 1006 return status; 1007 } 1008 1009 sp<ProClient> client; 1010 { 1011 Mutex::Autolock lock(mServiceLock); 1012 { 1013 sp<BasicClient> client; 1014 if (!canConnectUnsafe(cameraId, clientPackageName, 1015 IInterface::asBinder(cameraCb), 1016 /*out*/client)) { 1017 return -EBUSY; 1018 } 1019 } 1020 1021 int facing = -1; 1022 int deviceVersion = getDeviceVersion(cameraId, &facing); 1023 1024 switch(deviceVersion) { 1025 case CAMERA_DEVICE_API_VERSION_1_0: 1026 ALOGE("Camera id %d uses HALv1, doesn't support ProCamera", 1027 cameraId); 1028 return -EOPNOTSUPP; 1029 break; 1030 case CAMERA_DEVICE_API_VERSION_2_0: 1031 case CAMERA_DEVICE_API_VERSION_2_1: 1032 case CAMERA_DEVICE_API_VERSION_3_0: 1033 case CAMERA_DEVICE_API_VERSION_3_1: 1034 case CAMERA_DEVICE_API_VERSION_3_2: 1035 client = new ProCamera2Client(this, cameraCb, clientPackageName, 1036 cameraId, facing, callingPid, clientUid, getpid()); 1037 break; 1038 case -1: 1039 ALOGE("Invalid camera id %d", cameraId); 1040 return BAD_VALUE; 1041 default: 1042 ALOGE("Unknown camera device HAL version: %d", deviceVersion); 1043 return INVALID_OPERATION; 1044 } 1045 1046 status_t status = connectFinishUnsafe(client, client->getRemote()); 1047 if (status != OK) { 1048 return status; 1049 } 1050 1051 mProClientList[cameraId].push(client); 1052 1053 LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId, 1054 getpid()); 1055 } 1056 // important: release the mutex here so the client can call back 1057 // into the service from its destructor (can be at the end of the call) 1058 device = client; 1059 return OK; 1060} 1061 1062status_t CameraService::connectDevice( 1063 const sp<ICameraDeviceCallbacks>& cameraCb, 1064 int cameraId, 1065 const String16& clientPackageName, 1066 int clientUid, 1067 /*out*/ 1068 sp<ICameraDeviceUser>& device) 1069{ 1070 1071 String8 clientName8(clientPackageName); 1072 int callingPid = getCallingPid(); 1073 1074 LOG1("CameraService::connectDevice E (pid %d \"%s\", id %d)", callingPid, 1075 clientName8.string(), cameraId); 1076 1077 status_t status = validateConnect(cameraId, /*inout*/clientUid); 1078 if (status != OK) { 1079 return status; 1080 } 1081 1082 sp<CameraDeviceClient> client; 1083 { 1084 Mutex::Autolock lock(mServiceLock); 1085 { 1086 sp<BasicClient> client; 1087 if (!canConnectUnsafe(cameraId, clientPackageName, 1088 IInterface::asBinder(cameraCb), 1089 /*out*/client)) { 1090 return -EBUSY; 1091 } 1092 } 1093 1094 int facing = -1; 1095 int deviceVersion = getDeviceVersion(cameraId, &facing); 1096 1097 // give flashlight a chance to close devices if necessary. 1098 mFlashlight->prepareDeviceOpen(); 1099 1100 switch(deviceVersion) { 1101 case CAMERA_DEVICE_API_VERSION_1_0: 1102 ALOGW("Camera using old HAL version: %d", deviceVersion); 1103 return -EOPNOTSUPP; 1104 // TODO: don't allow 2.0 Only allow 2.1 and higher 1105 case CAMERA_DEVICE_API_VERSION_2_0: 1106 case CAMERA_DEVICE_API_VERSION_2_1: 1107 case CAMERA_DEVICE_API_VERSION_3_0: 1108 case CAMERA_DEVICE_API_VERSION_3_1: 1109 case CAMERA_DEVICE_API_VERSION_3_2: 1110 client = new CameraDeviceClient(this, cameraCb, clientPackageName, 1111 cameraId, facing, callingPid, clientUid, getpid()); 1112 break; 1113 case -1: 1114 ALOGE("Invalid camera id %d", cameraId); 1115 return BAD_VALUE; 1116 default: 1117 ALOGE("Unknown camera device HAL version: %d", deviceVersion); 1118 return INVALID_OPERATION; 1119 } 1120 1121 status_t status = connectFinishUnsafe(client, client->getRemote()); 1122 if (status != OK) { 1123 // this is probably not recoverable.. maybe the client can try again 1124 return status; 1125 } 1126 1127 LOG1("CameraService::connectDevice X (id %d, this pid is %d)", cameraId, 1128 getpid()); 1129 1130 mClient[cameraId] = client; 1131 } 1132 // important: release the mutex here so the client can call back 1133 // into the service from its destructor (can be at the end of the call) 1134 1135 device = client; 1136 return OK; 1137} 1138 1139 1140status_t CameraService::addListener( 1141 const sp<ICameraServiceListener>& listener) { 1142 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); 1143 1144 if (listener == 0) { 1145 ALOGE("%s: Listener must not be null", __FUNCTION__); 1146 return BAD_VALUE; 1147 } 1148 1149 Mutex::Autolock lock(mServiceLock); 1150 1151 Vector<sp<ICameraServiceListener> >::iterator it, end; 1152 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { 1153 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) { 1154 ALOGW("%s: Tried to add listener %p which was already subscribed", 1155 __FUNCTION__, listener.get()); 1156 return ALREADY_EXISTS; 1157 } 1158 } 1159 1160 mListenerList.push_back(listener); 1161 1162 /* Immediately signal current status to this listener only */ 1163 { 1164 Mutex::Autolock m(mStatusMutex) ; 1165 int numCams = getNumberOfCameras(); 1166 for (int i = 0; i < numCams; ++i) { 1167 listener->onStatusChanged(mStatusList[i], i); 1168 } 1169 } 1170 1171 /* Immediately signal current torch status to this listener only */ 1172 { 1173 Mutex::Autolock al(mTorchStatusMutex); 1174 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { 1175 listener->onTorchStatusChanged(mTorchStatusMap.valueAt(i), 1176 mTorchStatusMap.keyAt(i)); 1177 } 1178 1179 } 1180 1181 return OK; 1182} 1183status_t CameraService::removeListener( 1184 const sp<ICameraServiceListener>& listener) { 1185 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); 1186 1187 if (listener == 0) { 1188 ALOGE("%s: Listener must not be null", __FUNCTION__); 1189 return BAD_VALUE; 1190 } 1191 1192 Mutex::Autolock lock(mServiceLock); 1193 1194 Vector<sp<ICameraServiceListener> >::iterator it; 1195 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { 1196 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) { 1197 mListenerList.erase(it); 1198 return OK; 1199 } 1200 } 1201 1202 ALOGW("%s: Tried to remove a listener %p which was not subscribed", 1203 __FUNCTION__, listener.get()); 1204 1205 return BAD_VALUE; 1206} 1207 1208status_t CameraService::getLegacyParameters( 1209 int cameraId, 1210 /*out*/ 1211 String16* parameters) { 1212 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); 1213 1214 if (parameters == NULL) { 1215 ALOGE("%s: parameters must not be null", __FUNCTION__); 1216 return BAD_VALUE; 1217 } 1218 1219 status_t ret = 0; 1220 1221 CameraParameters shimParams; 1222 if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) { 1223 // Error logged by caller 1224 return ret; 1225 } 1226 1227 String8 shimParamsString8 = shimParams.flatten(); 1228 String16 shimParamsString16 = String16(shimParamsString8); 1229 1230 *parameters = shimParamsString16; 1231 1232 return OK; 1233} 1234 1235status_t CameraService::supportsCameraApi(int cameraId, int apiVersion) { 1236 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); 1237 1238 switch (apiVersion) { 1239 case API_VERSION_1: 1240 case API_VERSION_2: 1241 break; 1242 default: 1243 ALOGE("%s: Bad API version %d", __FUNCTION__, apiVersion); 1244 return BAD_VALUE; 1245 } 1246 1247 int facing = -1; 1248 int deviceVersion = getDeviceVersion(cameraId, &facing); 1249 1250 switch(deviceVersion) { 1251 case CAMERA_DEVICE_API_VERSION_1_0: 1252 case CAMERA_DEVICE_API_VERSION_2_0: 1253 case CAMERA_DEVICE_API_VERSION_2_1: 1254 case CAMERA_DEVICE_API_VERSION_3_0: 1255 case CAMERA_DEVICE_API_VERSION_3_1: 1256 if (apiVersion == API_VERSION_2) { 1257 ALOGV("%s: Camera id %d uses HAL prior to HAL3.2, doesn't support api2 without shim", 1258 __FUNCTION__, cameraId); 1259 return -EOPNOTSUPP; 1260 } else { // if (apiVersion == API_VERSION_1) { 1261 ALOGV("%s: Camera id %d uses older HAL before 3.2, but api1 is always supported", 1262 __FUNCTION__, cameraId); 1263 return OK; 1264 } 1265 case CAMERA_DEVICE_API_VERSION_3_2: 1266 ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly", 1267 __FUNCTION__, cameraId); 1268 return OK; 1269 case -1: 1270 ALOGE("%s: Invalid camera id %d", __FUNCTION__, cameraId); 1271 return BAD_VALUE; 1272 default: 1273 ALOGE("%s: Unknown camera device HAL version: %d", __FUNCTION__, deviceVersion); 1274 return INVALID_OPERATION; 1275 } 1276 1277 return OK; 1278} 1279 1280void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) { 1281 int callingPid = getCallingPid(); 1282 LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid); 1283 1284 // Declare this before the lock to make absolutely sure the 1285 // destructor won't be called with the lock held. 1286 Mutex::Autolock lock(mServiceLock); 1287 1288 int outIndex; 1289 sp<BasicClient> client = findClientUnsafe(remoteBinder, outIndex); 1290 1291 if (client != 0) { 1292 // Found our camera, clear and leave. 1293 LOG1("removeClient: clear camera %d", outIndex); 1294 1295 sp<IBinder> remote = client->getRemote(); 1296 if (remote != NULL) { 1297 remote->unlinkToDeath(this); 1298 } 1299 1300 mClient[outIndex].clear(); 1301 } else { 1302 1303 sp<ProClient> clientPro = findProClientUnsafe(remoteBinder); 1304 1305 if (clientPro != NULL) { 1306 // Found our camera, clear and leave. 1307 LOG1("removeClient: clear pro %p", clientPro.get()); 1308 1309 IInterface::asBinder(clientPro->getRemoteCallback())->unlinkToDeath(this); 1310 } 1311 } 1312 1313 LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid); 1314} 1315 1316sp<CameraService::ProClient> CameraService::findProClientUnsafe( 1317 const wp<IBinder>& cameraCallbacksRemote) 1318{ 1319 sp<ProClient> clientPro; 1320 1321 for (int i = 0; i < mNumberOfCameras; ++i) { 1322 Vector<size_t> removeIdx; 1323 1324 for (size_t j = 0; j < mProClientList[i].size(); ++j) { 1325 wp<ProClient> cl = mProClientList[i][j]; 1326 1327 sp<ProClient> clStrong = cl.promote(); 1328 if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) { 1329 clientPro = clStrong; 1330 break; 1331 } else if (clStrong == NULL) { 1332 // mark to clean up dead ptr 1333 removeIdx.push(j); 1334 } 1335 } 1336 1337 // remove stale ptrs (in reverse so the indices dont change) 1338 for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) { 1339 mProClientList[i].removeAt(removeIdx[j]); 1340 } 1341 1342 } 1343 1344 return clientPro; 1345} 1346 1347sp<CameraService::BasicClient> CameraService::findClientUnsafe( 1348 const wp<IBinder>& cameraClient, int& outIndex) { 1349 sp<BasicClient> client; 1350 1351 for (int i = 0; i < mNumberOfCameras; i++) { 1352 1353 // This happens when we have already disconnected (or this is 1354 // just another unused camera). 1355 if (mClient[i] == 0) continue; 1356 1357 // Promote mClient. It can fail if we are called from this path: 1358 // Client::~Client() -> disconnect() -> removeClientByRemote(). 1359 client = mClient[i].promote(); 1360 1361 // Clean up stale client entry 1362 if (client == NULL) { 1363 mClient[i].clear(); 1364 continue; 1365 } 1366 1367 if (cameraClient == client->getRemote()) { 1368 // Found our camera 1369 outIndex = i; 1370 return client; 1371 } 1372 } 1373 1374 outIndex = -1; 1375 return NULL; 1376} 1377 1378CameraService::BasicClient* CameraService::getClientByIdUnsafe(int cameraId) { 1379 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; 1380 return mClient[cameraId].unsafe_get(); 1381} 1382 1383Mutex* CameraService::getClientLockById(int cameraId) { 1384 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; 1385 return &mClientLock[cameraId]; 1386} 1387 1388sp<CameraService::BasicClient> CameraService::getClientByRemote( 1389 const wp<IBinder>& cameraClient) { 1390 1391 // Declare this before the lock to make absolutely sure the 1392 // destructor won't be called with the lock held. 1393 sp<BasicClient> client; 1394 1395 Mutex::Autolock lock(mServiceLock); 1396 1397 int outIndex; 1398 client = findClientUnsafe(cameraClient, outIndex); 1399 1400 return client; 1401} 1402 1403status_t CameraService::onTransact( 1404 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { 1405 // Permission checks 1406 switch (code) { 1407 case BnCameraService::CONNECT: 1408 case BnCameraService::CONNECT_PRO: 1409 case BnCameraService::CONNECT_DEVICE: 1410 case BnCameraService::CONNECT_LEGACY: 1411 const int pid = getCallingPid(); 1412 const int self_pid = getpid(); 1413 if (pid != self_pid) { 1414 // we're called from a different process, do the real check 1415 if (!checkCallingPermission( 1416 String16("android.permission.CAMERA"))) { 1417 const int uid = getCallingUid(); 1418 ALOGE("Permission Denial: " 1419 "can't use the camera pid=%d, uid=%d", pid, uid); 1420 return PERMISSION_DENIED; 1421 } 1422 } 1423 break; 1424 } 1425 1426 return BnCameraService::onTransact(code, data, reply, flags); 1427} 1428 1429// The reason we need this busy bit is a new CameraService::connect() request 1430// may come in while the previous Client's destructor has not been run or is 1431// still running. If the last strong reference of the previous Client is gone 1432// but the destructor has not been finished, we should not allow the new Client 1433// to be created because we need to wait for the previous Client to tear down 1434// the hardware first. 1435void CameraService::setCameraBusy(int cameraId) { 1436 android_atomic_write(1, &mBusy[cameraId]); 1437 1438 ALOGV("setCameraBusy cameraId=%d", cameraId); 1439} 1440 1441void CameraService::setCameraFree(int cameraId) { 1442 android_atomic_write(0, &mBusy[cameraId]); 1443 1444 ALOGV("setCameraFree cameraId=%d", cameraId); 1445} 1446 1447// We share the media players for shutter and recording sound for all clients. 1448// A reference count is kept to determine when we will actually release the 1449// media players. 1450 1451MediaPlayer* CameraService::newMediaPlayer(const char *file) { 1452 MediaPlayer* mp = new MediaPlayer(); 1453 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) { 1454 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); 1455 mp->prepare(); 1456 } else { 1457 ALOGE("Failed to load CameraService sounds: %s", file); 1458 return NULL; 1459 } 1460 return mp; 1461} 1462 1463void CameraService::loadSound() { 1464 Mutex::Autolock lock(mSoundLock); 1465 LOG1("CameraService::loadSound ref=%d", mSoundRef); 1466 if (mSoundRef++) return; 1467 1468 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); 1469 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); 1470} 1471 1472void CameraService::releaseSound() { 1473 Mutex::Autolock lock(mSoundLock); 1474 LOG1("CameraService::releaseSound ref=%d", mSoundRef); 1475 if (--mSoundRef) return; 1476 1477 for (int i = 0; i < NUM_SOUNDS; i++) { 1478 if (mSoundPlayer[i] != 0) { 1479 mSoundPlayer[i]->disconnect(); 1480 mSoundPlayer[i].clear(); 1481 } 1482 } 1483} 1484 1485void CameraService::playSound(sound_kind kind) { 1486 LOG1("playSound(%d)", kind); 1487 Mutex::Autolock lock(mSoundLock); 1488 sp<MediaPlayer> player = mSoundPlayer[kind]; 1489 if (player != 0) { 1490 player->seekTo(0); 1491 player->start(); 1492 } 1493} 1494 1495// ---------------------------------------------------------------------------- 1496 1497CameraService::Client::Client(const sp<CameraService>& cameraService, 1498 const sp<ICameraClient>& cameraClient, 1499 const String16& clientPackageName, 1500 int cameraId, int cameraFacing, 1501 int clientPid, uid_t clientUid, 1502 int servicePid) : 1503 CameraService::BasicClient(cameraService, 1504 IInterface::asBinder(cameraClient), 1505 clientPackageName, 1506 cameraId, cameraFacing, 1507 clientPid, clientUid, 1508 servicePid) 1509{ 1510 int callingPid = getCallingPid(); 1511 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId); 1512 1513 mRemoteCallback = cameraClient; 1514 1515 cameraService->setCameraBusy(cameraId); 1516 cameraService->loadSound(); 1517 1518 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId); 1519} 1520 1521// tear down the client 1522CameraService::Client::~Client() { 1523 ALOGV("~Client"); 1524 mDestructionStarted = true; 1525 1526 mCameraService->releaseSound(); 1527 // unconditionally disconnect. function is idempotent 1528 Client::disconnect(); 1529} 1530 1531CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, 1532 const sp<IBinder>& remoteCallback, 1533 const String16& clientPackageName, 1534 int cameraId, int cameraFacing, 1535 int clientPid, uid_t clientUid, 1536 int servicePid): 1537 mClientPackageName(clientPackageName) 1538{ 1539 mCameraService = cameraService; 1540 mRemoteBinder = remoteCallback; 1541 mCameraId = cameraId; 1542 mCameraFacing = cameraFacing; 1543 mClientPid = clientPid; 1544 mClientUid = clientUid; 1545 mServicePid = servicePid; 1546 mOpsActive = false; 1547 mDestructionStarted = false; 1548} 1549 1550CameraService::BasicClient::~BasicClient() { 1551 ALOGV("~BasicClient"); 1552 mDestructionStarted = true; 1553} 1554 1555void CameraService::BasicClient::disconnect() { 1556 ALOGV("BasicClient::disconnect"); 1557 mCameraService->removeClientByRemote(mRemoteBinder); 1558 1559 finishCameraOps(); 1560 // client shouldn't be able to call into us anymore 1561 mClientPid = 0; 1562} 1563 1564status_t CameraService::BasicClient::startCameraOps() { 1565 int32_t res; 1566 // Notify app ops that the camera is not available 1567 mOpsCallback = new OpsCallback(this); 1568 1569 { 1570 ALOGV("%s: Start camera ops, package name = %s, client UID = %d", 1571 __FUNCTION__, String8(mClientPackageName).string(), mClientUid); 1572 } 1573 1574 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA, 1575 mClientPackageName, mOpsCallback); 1576 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA, 1577 mClientUid, mClientPackageName); 1578 1579 if (res != AppOpsManager::MODE_ALLOWED) { 1580 ALOGI("Camera %d: Access for \"%s\" has been revoked", 1581 mCameraId, String8(mClientPackageName).string()); 1582 return PERMISSION_DENIED; 1583 } 1584 1585 mOpsActive = true; 1586 1587 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE 1588 mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, 1589 mCameraId); 1590 1591 return OK; 1592} 1593 1594status_t CameraService::BasicClient::finishCameraOps() { 1595 // Check if startCameraOps succeeded, and if so, finish the camera op 1596 if (mOpsActive) { 1597 // Notify app ops that the camera is available again 1598 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid, 1599 mClientPackageName); 1600 mOpsActive = false; 1601 1602 // Notify device availability listeners that this camera is available 1603 // again 1604 1605 StatusVector rejectSourceStates; 1606 rejectSourceStates.push_back(ICameraServiceListener::STATUS_NOT_PRESENT); 1607 rejectSourceStates.push_back(ICameraServiceListener::STATUS_ENUMERATING); 1608 1609 // Transition to PRESENT if the camera is not in either of above 2 1610 // states 1611 mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT, 1612 mCameraId, 1613 &rejectSourceStates); 1614 1615 } 1616 // Always stop watching, even if no camera op is active 1617 if (mOpsCallback != NULL) { 1618 mAppOpsManager.stopWatchingMode(mOpsCallback); 1619 } 1620 mOpsCallback.clear(); 1621 1622 return OK; 1623} 1624 1625void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) { 1626 String8 name(packageName); 1627 String8 myName(mClientPackageName); 1628 1629 if (op != AppOpsManager::OP_CAMERA) { 1630 ALOGW("Unexpected app ops notification received: %d", op); 1631 return; 1632 } 1633 1634 int32_t res; 1635 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA, 1636 mClientUid, mClientPackageName); 1637 ALOGV("checkOp returns: %d, %s ", res, 1638 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : 1639 res == AppOpsManager::MODE_IGNORED ? "IGNORED" : 1640 res == AppOpsManager::MODE_ERRORED ? "ERRORED" : 1641 "UNKNOWN"); 1642 1643 if (res != AppOpsManager::MODE_ALLOWED) { 1644 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId, 1645 myName.string()); 1646 // Reset the client PID to allow server-initiated disconnect, 1647 // and to prevent further calls by client. 1648 mClientPid = getCallingPid(); 1649 CaptureResultExtras resultExtras; // a dummy result (invalid) 1650 notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras); 1651 disconnect(); 1652 } 1653} 1654 1655// ---------------------------------------------------------------------------- 1656 1657Mutex* CameraService::Client::getClientLockFromCookie(void* user) { 1658 return gCameraService->getClientLockById((int)(intptr_t) user); 1659} 1660 1661// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should 1662// be acquired for this to be safe 1663CameraService::Client* CameraService::Client::getClientFromCookie(void* user) { 1664 BasicClient *basicClient = gCameraService->getClientByIdUnsafe((int)(intptr_t) user); 1665 // OK: only CameraClient calls this, and they already cast anyway. 1666 Client* client = static_cast<Client*>(basicClient); 1667 1668 // This could happen if the Client is in the process of shutting down (the 1669 // last strong reference is gone, but the destructor hasn't finished 1670 // stopping the hardware). 1671 if (client == NULL) return NULL; 1672 1673 // destruction already started, so should not be accessed 1674 if (client->mDestructionStarted) return NULL; 1675 1676 return client; 1677} 1678 1679void CameraService::Client::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, 1680 const CaptureResultExtras& resultExtras) { 1681 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); 1682} 1683 1684// NOTE: function is idempotent 1685void CameraService::Client::disconnect() { 1686 ALOGV("Client::disconnect"); 1687 BasicClient::disconnect(); 1688 mCameraService->setCameraFree(mCameraId); 1689} 1690 1691CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): 1692 mClient(client) { 1693} 1694 1695void CameraService::Client::OpsCallback::opChanged(int32_t op, 1696 const String16& packageName) { 1697 sp<BasicClient> client = mClient.promote(); 1698 if (client != NULL) { 1699 client->opChanged(op, packageName); 1700 } 1701} 1702 1703// ---------------------------------------------------------------------------- 1704// IProCamera 1705// ---------------------------------------------------------------------------- 1706 1707CameraService::ProClient::ProClient(const sp<CameraService>& cameraService, 1708 const sp<IProCameraCallbacks>& remoteCallback, 1709 const String16& clientPackageName, 1710 int cameraId, 1711 int cameraFacing, 1712 int clientPid, 1713 uid_t clientUid, 1714 int servicePid) 1715 : CameraService::BasicClient(cameraService, IInterface::asBinder(remoteCallback), 1716 clientPackageName, cameraId, cameraFacing, 1717 clientPid, clientUid, servicePid) 1718{ 1719 mRemoteCallback = remoteCallback; 1720} 1721 1722CameraService::ProClient::~ProClient() { 1723} 1724 1725void CameraService::ProClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, 1726 const CaptureResultExtras& resultExtras) { 1727 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); 1728} 1729 1730// ---------------------------------------------------------------------------- 1731 1732static const int kDumpLockRetries = 50; 1733static const int kDumpLockSleep = 60000; 1734 1735static bool tryLock(Mutex& mutex) 1736{ 1737 bool locked = false; 1738 for (int i = 0; i < kDumpLockRetries; ++i) { 1739 if (mutex.tryLock() == NO_ERROR) { 1740 locked = true; 1741 break; 1742 } 1743 usleep(kDumpLockSleep); 1744 } 1745 return locked; 1746} 1747 1748status_t CameraService::dump(int fd, const Vector<String16>& args) { 1749 String8 result; 1750 if (checkCallingPermission(String16("android.permission.DUMP")) == false) { 1751 result.appendFormat("Permission Denial: " 1752 "can't dump CameraService from pid=%d, uid=%d\n", 1753 getCallingPid(), 1754 getCallingUid()); 1755 write(fd, result.string(), result.size()); 1756 } else { 1757 bool locked = tryLock(mServiceLock); 1758 // failed to lock - CameraService is probably deadlocked 1759 if (!locked) { 1760 result.append("CameraService may be deadlocked\n"); 1761 write(fd, result.string(), result.size()); 1762 } 1763 1764 bool hasClient = false; 1765 if (!mModule) { 1766 result = String8::format("No camera module available!\n"); 1767 write(fd, result.string(), result.size()); 1768 if (locked) mServiceLock.unlock(); 1769 return NO_ERROR; 1770 } 1771 1772 const hw_module_t* common = mModule->getRawModule(); 1773 result = String8::format("Camera module HAL API version: 0x%x\n", common->hal_api_version); 1774 result.appendFormat("Camera module API version: 0x%x\n", common->module_api_version); 1775 result.appendFormat("Camera module name: %s\n", common->name); 1776 result.appendFormat("Camera module author: %s\n", common->author); 1777 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras); 1778 1779 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); 1780 if (desc == NULL) { 1781 result.appendFormat("Vendor tags left unimplemented.\n"); 1782 } else { 1783 result.appendFormat("Vendor tag definitions:\n"); 1784 } 1785 1786 write(fd, result.string(), result.size()); 1787 1788 if (desc != NULL) { 1789 desc->dump(fd, /*verbosity*/2, /*indentation*/4); 1790 } 1791 1792 for (int i = 0; i < mNumberOfCameras; i++) { 1793 result = String8::format("Camera %d static information:\n", i); 1794 camera_info info; 1795 1796 status_t rc = mModule->getCameraInfo(i, &info); 1797 if (rc != OK) { 1798 result.appendFormat(" Error reading static information!\n"); 1799 write(fd, result.string(), result.size()); 1800 } else { 1801 result.appendFormat(" Facing: %s\n", 1802 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT"); 1803 result.appendFormat(" Orientation: %d\n", info.orientation); 1804 int deviceVersion; 1805 if (common->module_api_version < CAMERA_MODULE_API_VERSION_2_0) { 1806 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; 1807 } else { 1808 deviceVersion = info.device_version; 1809 } 1810 result.appendFormat(" Device version: 0x%x\n", deviceVersion); 1811 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) { 1812 result.appendFormat(" Device static metadata:\n"); 1813 write(fd, result.string(), result.size()); 1814 dump_indented_camera_metadata(info.static_camera_characteristics, 1815 fd, /*verbosity*/2, /*indentation*/4); 1816 } else { 1817 write(fd, result.string(), result.size()); 1818 } 1819 } 1820 1821 sp<BasicClient> client = mClient[i].promote(); 1822 if (client == 0) { 1823 result = String8::format(" Device is closed, no client instance\n"); 1824 write(fd, result.string(), result.size()); 1825 continue; 1826 } 1827 hasClient = true; 1828 result = String8::format(" Device is open. Client instance dump:\n"); 1829 write(fd, result.string(), result.size()); 1830 client->dump(fd, args); 1831 } 1832 if (!hasClient) { 1833 result = String8::format("\nNo active camera clients yet.\n"); 1834 write(fd, result.string(), result.size()); 1835 } 1836 1837 if (locked) mServiceLock.unlock(); 1838 1839 // Dump camera traces if there were any 1840 write(fd, "\n", 1); 1841 camera3::CameraTraces::dump(fd, args); 1842 1843 // change logging level 1844 int n = args.size(); 1845 for (int i = 0; i + 1 < n; i++) { 1846 String16 verboseOption("-v"); 1847 if (args[i] == verboseOption) { 1848 String8 levelStr(args[i+1]); 1849 int level = atoi(levelStr.string()); 1850 result = String8::format("\nSetting log level to %d.\n", level); 1851 setLogLevel(level); 1852 write(fd, result.string(), result.size()); 1853 } 1854 } 1855 1856 } 1857 return NO_ERROR; 1858} 1859 1860void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { 1861 Mutex::Autolock al(mTorchStatusMutex); 1862 for (size_t i = 0; i < mTorchClientMap.size(); i++) { 1863 if (mTorchClientMap[i] == who) { 1864 // turn off the torch mode that was turned on by dead client 1865 String16 cameraId = mTorchClientMap.keyAt(i); 1866 mFlashlight->setTorchMode(cameraId, false); 1867 mTorchClientMap.removeItemsAt(i); 1868 1869 // notify torch mode was turned off 1870 onTorchStatusChangedLocked(cameraId, 1871 ICameraServiceListener::TORCH_STATUS_OFF); 1872 break; 1873 } 1874 } 1875} 1876 1877/*virtual*/void CameraService::binderDied( 1878 const wp<IBinder> &who) { 1879 1880 /** 1881 * While tempting to promote the wp<IBinder> into a sp, 1882 * it's actually not supported by the binder driver 1883 */ 1884 1885 ALOGV("java clients' binder died"); 1886 1887 // check torch client 1888 handleTorchClientBinderDied(who); 1889 1890 // check camera device client 1891 sp<BasicClient> cameraClient = getClientByRemote(who); 1892 1893 if (cameraClient == 0) { 1894 ALOGV("java clients' binder death already cleaned up (normal case)"); 1895 return; 1896 } 1897 1898 ALOGW("Disconnecting camera client %p since the binder for it " 1899 "died (this pid %d)", cameraClient.get(), getCallingPid()); 1900 1901 cameraClient->disconnect(); 1902 1903} 1904 1905void CameraService::updateStatus(ICameraServiceListener::Status status, 1906 int32_t cameraId, 1907 const StatusVector *rejectSourceStates) { 1908 // do not lock mServiceLock here or can get into a deadlock from 1909 // connect() -> ProClient::disconnect -> updateStatus 1910 Mutex::Autolock lock(mStatusMutex); 1911 1912 ICameraServiceListener::Status oldStatus = mStatusList[cameraId]; 1913 1914 mStatusList[cameraId] = status; 1915 1916 if (oldStatus != status) { 1917 ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x", 1918 __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status); 1919 1920 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT && 1921 (status != ICameraServiceListener::STATUS_PRESENT && 1922 status != ICameraServiceListener::STATUS_ENUMERATING)) { 1923 1924 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT" 1925 " or ENUMERATING", __FUNCTION__); 1926 mStatusList[cameraId] = oldStatus; 1927 return; 1928 } 1929 1930 if (rejectSourceStates != NULL) { 1931 const StatusVector &rejectList = *rejectSourceStates; 1932 StatusVector::const_iterator it = rejectList.begin(); 1933 1934 /** 1935 * Sometimes we want to conditionally do a transition. 1936 * For example if a client disconnects, we want to go to PRESENT 1937 * only if we weren't already in NOT_PRESENT or ENUMERATING. 1938 */ 1939 for (; it != rejectList.end(); ++it) { 1940 if (oldStatus == *it) { 1941 ALOGV("%s: Rejecting status transition for Camera ID %d, " 1942 " since the source state was was in one of the bad " 1943 " states.", __FUNCTION__, cameraId); 1944 mStatusList[cameraId] = oldStatus; 1945 return; 1946 } 1947 } 1948 } 1949 1950 /** 1951 * ProClients lose their exclusive lock. 1952 * - Done before the CameraClient can initialize the HAL device, 1953 * since we want to be able to close it before they get to initialize 1954 */ 1955 if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) { 1956 Vector<wp<ProClient> > proClients(mProClientList[cameraId]); 1957 Vector<wp<ProClient> >::const_iterator it; 1958 1959 for (it = proClients.begin(); it != proClients.end(); ++it) { 1960 sp<ProClient> proCl = it->promote(); 1961 if (proCl.get() != NULL) { 1962 proCl->onExclusiveLockStolen(); 1963 } 1964 } 1965 } 1966 1967 Vector<sp<ICameraServiceListener> >::const_iterator it; 1968 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { 1969 (*it)->onStatusChanged(status, cameraId); 1970 } 1971 } 1972} 1973 1974ICameraServiceListener::Status CameraService::getStatus(int cameraId) const { 1975 if (cameraId < 0 || cameraId >= MAX_CAMERAS) { 1976 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId); 1977 return ICameraServiceListener::STATUS_UNKNOWN; 1978 } 1979 1980 Mutex::Autolock al(mStatusMutex); 1981 return mStatusList[cameraId]; 1982} 1983 1984ICameraServiceListener::TorchStatus CameraService::getTorchStatusLocked( 1985 const String16& cameraId) const { 1986 ssize_t index = mTorchStatusMap.indexOfKey(cameraId); 1987 if (index == NAME_NOT_FOUND) { 1988 return ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; 1989 } 1990 1991 return mTorchStatusMap.valueAt(index); 1992} 1993 1994status_t CameraService::setTorchStatusLocked(const String16& cameraId, 1995 ICameraServiceListener::TorchStatus status) { 1996 ssize_t index = mTorchStatusMap.indexOfKey(cameraId); 1997 if (index == NAME_NOT_FOUND) { 1998 return BAD_VALUE; 1999 } 2000 ICameraServiceListener::TorchStatus& item = 2001 mTorchStatusMap.editValueAt(index); 2002 item = status; 2003 2004 return OK; 2005} 2006 2007}; // namespace android 2008