1 /* 2 * Copyright (C) 2016 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 #ifndef ANDROID_SERVERS_CAMERA_CAMERAPROVIDER_H 18 #define ANDROID_SERVERS_CAMERA_CAMERAPROVIDER_H 19 20 #include <vector> 21 #include <unordered_map> 22 #include <unordered_set> 23 #include <set> 24 #include <string> 25 #include <mutex> 26 27 #include <camera/camera2/ConcurrentCamera.h> 28 #include <camera/CameraParameters2.h> 29 #include <camera/CameraMetadata.h> 30 #include <camera/CameraBase.h> 31 #include <utils/Condition.h> 32 #include <utils/Errors.h> 33 #include <android/hardware/ICameraService.h> 34 #include <utils/IPCTransport.h> 35 #include <utils/SessionConfigurationUtils.h> 36 #include <aidl/android/hardware/camera/provider/ICameraProvider.h> 37 #include <android/hardware/camera/common/1.0/types.h> 38 #include <android/hardware/camera/provider/2.5/ICameraProvider.h> 39 #include <android/hardware/camera/provider/2.6/ICameraProviderCallback.h> 40 #include <android/hardware/camera/provider/2.6/ICameraProvider.h> 41 #include <android/hardware/camera/provider/2.7/ICameraProvider.h> 42 #include <android/hardware/camera/device/3.7/types.h> 43 #include <android/hidl/manager/1.0/IServiceNotification.h> 44 #include <binder/IServiceManager.h> 45 #include <camera/VendorTagDescriptor.h> 46 47 namespace android { 48 49 using hardware::camera2::utils::CameraIdAndSessionConfiguration; 50 51 enum class CameraDeviceStatus : uint32_t { 52 NOT_PRESENT = 0, 53 PRESENT = 1, 54 ENUMERATING = 2 55 }; 56 57 enum class TorchModeStatus : uint32_t { 58 NOT_AVAILABLE = 0, 59 AVAILABLE_OFF = 1, 60 AVAILABLE_ON = 2 61 }; 62 63 struct CameraResourceCost { 64 uint32_t resourceCost; 65 std::vector<std::string> conflictingDevices; 66 }; 67 68 enum SystemCameraKind { 69 /** 70 * These camera devices are visible to all apps and system components alike 71 */ 72 PUBLIC = 0, 73 74 /** 75 * These camera devices are visible only to processes having the 76 * android.permission.SYSTEM_CAMERA permission. They are not exposed to 3P 77 * apps. 78 */ 79 SYSTEM_ONLY_CAMERA, 80 81 /** 82 * These camera devices are visible only to HAL clients (that try to connect 83 * on a hwbinder thread). 84 */ 85 HIDDEN_SECURE_CAMERA 86 }; 87 88 #define CAMERA_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0) 89 #define CAMERA_DEVICE_API_VERSION_1_2 HARDWARE_DEVICE_API_VERSION(1, 2) 90 #define CAMERA_DEVICE_API_VERSION_1_3 HARDWARE_DEVICE_API_VERSION(1, 3) 91 #define CAMERA_DEVICE_API_VERSION_1_4 HARDWARE_DEVICE_API_VERSION(1, 4) 92 #define CAMERA_DEVICE_API_VERSION_3_0 HARDWARE_DEVICE_API_VERSION(3, 0) 93 #define CAMERA_DEVICE_API_VERSION_3_1 HARDWARE_DEVICE_API_VERSION(3, 1) 94 #define CAMERA_DEVICE_API_VERSION_3_2 HARDWARE_DEVICE_API_VERSION(3, 2) 95 #define CAMERA_DEVICE_API_VERSION_3_3 HARDWARE_DEVICE_API_VERSION(3, 3) 96 #define CAMERA_DEVICE_API_VERSION_3_4 HARDWARE_DEVICE_API_VERSION(3, 4) 97 #define CAMERA_DEVICE_API_VERSION_3_5 HARDWARE_DEVICE_API_VERSION(3, 5) 98 #define CAMERA_DEVICE_API_VERSION_3_6 HARDWARE_DEVICE_API_VERSION(3, 6) 99 #define CAMERA_DEVICE_API_VERSION_3_7 HARDWARE_DEVICE_API_VERSION(3, 7) 100 101 /** 102 * The vendor tag descriptor class that takes HIDL/AIDL vendor tag information as 103 * input. Not part of VendorTagDescriptor class because that class is used 104 * in AIDL generated sources which don't have access to AIDL / HIDL headers. 105 */ 106 class IdlVendorTagDescriptor : public VendorTagDescriptor { 107 public: 108 /** 109 * Create a VendorTagDescriptor object from the HIDL/AIDL VendorTagSection 110 * vector. 111 * 112 * Returns OK on success, or a negative error code. 113 */ 114 template <class VendorTagSectionVectorType, class VendorTagSectionType> 115 static status_t createDescriptorFromIdl( 116 const VendorTagSectionVectorType& vts, 117 /*out*/ 118 sp<VendorTagDescriptor>& descriptor); 119 }; 120 121 /** 122 * A manager for all camera providers available on an Android device. 123 * 124 * Responsible for enumerating providers and the individual camera devices 125 * they export, both at startup and as providers and devices are added/removed. 126 * 127 * Provides methods for requesting information about individual devices and for 128 * opening them for active use. 129 * 130 */ 131 class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification, 132 public virtual IServiceManager::LocalRegistrationCallback { 133 public: 134 // needs to be made friend strict since HidlProviderInfo needs to inherit 135 // from CameraProviderManager::ProviderInfo which isn't a public member. 136 friend struct HidlProviderInfo; 137 friend struct AidlProviderInfo; 138 ~CameraProviderManager(); 139 140 // Tiny proxy for the static methods in a HIDL interface that communicate with the hardware 141 // service manager, to be replacable in unit tests with a fake. 142 struct HidlServiceInteractionProxy { 143 virtual bool registerForNotifications( 144 const std::string &serviceName, 145 const sp<hidl::manager::V1_0::IServiceNotification> 146 ¬ification) = 0; 147 // Will not wait for service to start if it's not already running 148 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService( 149 const std::string &serviceName) = 0; 150 // Will block for service if it exists but isn't running 151 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService( 152 const std::string &serviceName) = 0; 153 virtual hardware::hidl_vec<hardware::hidl_string> listServices() = 0; ~HidlServiceInteractionProxyHidlServiceInteractionProxy154 virtual ~HidlServiceInteractionProxy() {} 155 }; 156 157 // Standard use case - call into the normal generated static methods which invoke 158 // the real hardware service manager 159 struct HidlServiceInteractionProxyImpl : public HidlServiceInteractionProxy { registerForNotificationsHidlServiceInteractionProxyImpl160 virtual bool registerForNotifications( 161 const std::string &serviceName, 162 const sp<hidl::manager::V1_0::IServiceNotification> 163 ¬ification) override { 164 return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications( 165 serviceName, notification); 166 } tryGetServiceHidlServiceInteractionProxyImpl167 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService( 168 const std::string &serviceName) override { 169 return hardware::camera::provider::V2_4::ICameraProvider::tryGetService(serviceName); 170 } getServiceHidlServiceInteractionProxyImpl171 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService( 172 const std::string &serviceName) override { 173 return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName); 174 } 175 176 virtual hardware::hidl_vec<hardware::hidl_string> listServices() override; 177 }; 178 179 // Proxy to inject fake services in test. 180 class AidlServiceInteractionProxy { 181 public: 182 // Returns the Aidl service with the given serviceName. Will wait indefinitely 183 // for the service to come up if not running. 184 virtual std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider> 185 getService(const std::string& serviceName) = 0; 186 187 // Attempts to get an already running AIDL service of the given serviceName. 188 // Returns nullptr immediately if service is not running. 189 virtual std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider> 190 tryGetService(const std::string& serviceName) = 0; 191 192 virtual ~AidlServiceInteractionProxy() = default; 193 }; 194 195 // Standard use case - call into the normal static methods which invoke 196 // the real service manager 197 class AidlServiceInteractionProxyImpl : public AidlServiceInteractionProxy { 198 public: 199 virtual std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider> 200 getService(const std::string& serviceName) override; 201 202 virtual std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider> 203 tryGetService(const std::string& serviceName) override; 204 }; 205 206 /** 207 * Listener interface for device/torch status changes 208 */ 209 struct StatusListener : virtual public RefBase { ~StatusListenerStatusListener210 ~StatusListener() {} 211 212 virtual void onDeviceStatusChanged(const std::string &cameraId, 213 CameraDeviceStatus newStatus) = 0; 214 virtual void onDeviceStatusChanged(const std::string &cameraId, 215 const std::string &physicalCameraId, 216 CameraDeviceStatus newStatus) = 0; 217 virtual void onTorchStatusChanged(const std::string &cameraId, 218 TorchModeStatus newStatus, 219 SystemCameraKind kind) = 0; 220 virtual void onTorchStatusChanged(const std::string &cameraId, 221 TorchModeStatus newStatus) = 0; 222 virtual void onNewProviderRegistered() = 0; 223 }; 224 225 /** 226 * Represents the mode a camera device is currently in 227 */ 228 enum class DeviceMode { 229 TORCH, 230 CAMERA 231 }; 232 233 /** 234 * Initialize the manager and give it a status listener; optionally accepts a service 235 * interaction proxy. 236 * 237 * The default proxy communicates via the hardware service manager; alternate proxies can be 238 * used for testing. The lifetime of the proxy must exceed the lifetime of the manager. 239 */ 240 status_t initialize(wp<StatusListener> listener, 241 HidlServiceInteractionProxy* hidlProxy = &sHidlServiceInteractionProxy, 242 AidlServiceInteractionProxy* aidlProxy = &sAidlServiceInteractionProxy); 243 244 status_t getCameraIdIPCTransport(const std::string &id, 245 IPCTransport *providerTransport) const; 246 247 /** 248 * Retrieve the total number of available cameras. 249 * This value may change dynamically as cameras are added or removed. 250 */ 251 std::pair<int, int> getCameraCount() const; 252 253 /** 254 * Upon the function return, if unavailablePhysicalIds is not nullptr, it 255 * will contain all of the unavailable physical camera Ids represented in 256 * the form of: 257 * {[logicalCamera, {physicalCamera1, physicalCamera2, ...}], ...}. 258 */ 259 std::vector<std::string> getCameraDeviceIds(std::unordered_map< 260 std::string, std::set<std::string>>* unavailablePhysicalIds = nullptr) const; 261 262 /** 263 * Retrieve the number of API1 compatible cameras; these are internal and 264 * backwards-compatible. This is the set of cameras that will be 265 * accessible via the old camera API. 266 * The return value may change dynamically due to external camera hotplug. 267 */ 268 std::vector<std::string> getAPI1CompatibleCameraDeviceIds() const; 269 270 /** 271 * Return true if a device with a given ID has a flash unit. Returns false 272 * for devices that are unknown. 273 */ 274 bool hasFlashUnit(const std::string &id) const; 275 276 /** 277 * Return true if the camera device has native zoom ratio support. 278 */ 279 bool supportNativeZoomRatio(const std::string &id) const; 280 281 /** 282 * Return true if the camera device has no composite Jpeg/R support. 283 */ 284 bool isCompositeJpegRDisabled(const std::string &id) const; 285 286 /** 287 * Return the resource cost of this camera device 288 */ 289 status_t getResourceCost(const std::string &id, 290 CameraResourceCost* cost) const; 291 292 /** 293 * Return the old camera API camera info 294 */ 295 status_t getCameraInfo(const std::string &id, 296 int rotationOverride, int *portraitRotation, 297 hardware::CameraInfo* info) const; 298 299 /** 300 * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does 301 * not have a v3 or newer HAL version. 302 */ 303 status_t getCameraCharacteristics(const std::string &id, 304 bool overrideForPerfClass, CameraMetadata* characteristics, 305 int rotationOverride) const; 306 307 status_t isConcurrentSessionConfigurationSupported( 308 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration> 309 &cameraIdsAndSessionConfigs, 310 const std::set<std::string>& perfClassPrimaryCameraIds, 311 int targetSdkVersion, bool *isSupported); 312 313 std::vector<std::unordered_set<std::string>> getConcurrentCameraIds() const; 314 315 /** 316 * Create a default capture request metadata for a camera and a specific 317 * template. 318 */ 319 status_t createDefaultRequest(const std::string& id, 320 camera3::camera_request_template_t templateId, 321 hardware::camera2::impl::CameraMetadataNative* request) const; 322 /** 323 * Check for device support of specific stream combination. 324 */ 325 status_t isSessionConfigurationSupported(const std::string& id, 326 const SessionConfiguration &configuration, 327 bool overrideForPerfClass, bool checkSessionParams, 328 bool *status /*out*/) const; 329 330 /** 331 * Get session characteristics for a particular session. 332 */ 333 status_t getSessionCharacteristics(const std::string& id, 334 const SessionConfiguration &configuration, 335 bool overrideForPerfClass, 336 int rotationOverride, 337 CameraMetadata* sessionCharacteristics /*out*/) const; 338 339 /** 340 * Return the highest supported device interface version for this ID 341 */ 342 status_t getHighestSupportedVersion(const std::string &id, 343 hardware::hidl_version *v, IPCTransport *transport); 344 345 /** 346 * Check if a given camera device support setTorchMode API. 347 */ 348 bool supportSetTorchMode(const std::string &id) const; 349 350 /** 351 * Check if torch strength update should be skipped or not. 352 */ 353 bool shouldSkipTorchStrengthUpdate(const std::string &id, int32_t torchStrength) const; 354 355 /** 356 * Return the default torch strength level if the torch strength control 357 * feature is supported. 358 */ 359 int32_t getTorchDefaultStrengthLevel(const std::string &id) const; 360 361 /** 362 * Turn on or off the flashlight on a given camera device. 363 * May fail if the device does not support this API, is in active use, or if the device 364 * doesn't exist, etc. 365 */ 366 status_t setTorchMode(const std::string &id, bool enabled); 367 368 /** 369 * Change the brightness level of the flash unit associated with the cameraId and 370 * set it to the value in torchStrength. 371 * If the torch is OFF and torchStrength > 0, the torch will be turned ON with the 372 * specified strength level. If the torch is ON, only the brightness level will be 373 * changed. 374 * 375 * This operation will fail if the device does not have flash unit, has flash unit 376 * but does not support this API, torchStrength is invalid or if the device doesn't 377 * exist etc. 378 */ 379 status_t turnOnTorchWithStrengthLevel(const std::string &id, int32_t torchStrength); 380 381 /** 382 * Return the torch strength level of this camera device. 383 */ 384 status_t getTorchStrengthLevel(const std::string &id, int32_t* torchStrength); 385 386 /** 387 * Setup vendor tags for all registered providers 388 */ 389 status_t setUpVendorTags(); 390 391 /** 392 * Inform registered providers about a device state change, such as folding or unfolding 393 */ 394 status_t notifyDeviceStateChange(int64_t newState); 395 396 status_t openAidlSession(const std::string &id, 397 const std::shared_ptr< 398 aidl::android::hardware::camera::device::ICameraDeviceCallback>& callback, 399 /*out*/ 400 std::shared_ptr<aidl::android::hardware::camera::device::ICameraDeviceSession> *session); 401 402 status_t openAidlInjectionSession(const std::string &id, 403 const std::shared_ptr< 404 aidl::android::hardware::camera::device::ICameraDeviceCallback>& callback, 405 /*out*/ 406 std::shared_ptr<aidl::android::hardware::camera::device::ICameraInjectionSession> *session); 407 408 /** 409 * Open an active session to a camera device. 410 * 411 * This fully powers on the camera device hardware, and returns a handle to a 412 * session to be used for hardware configuration and operation. 413 */ 414 status_t openHidlSession(const std::string &id, 415 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback, 416 /*out*/ 417 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session); 418 419 /** 420 * Notify that the camera or torch is no longer being used by a camera client 421 */ 422 void removeRef(DeviceMode usageType, const std::string &cameraId); 423 424 /** 425 * IServiceNotification::onRegistration 426 * Invoked by the hardware service manager when a new camera provider is registered 427 */ 428 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName, 429 const hardware::hidl_string& name, 430 bool preexisting) override; 431 432 // LocalRegistrationCallback::onServiceRegistration 433 virtual void onServiceRegistration(const String16& name, const sp<IBinder> &binder) override; 434 435 /* 436 * Return list of provider pid 437 */ 438 std::set<pid_t> getProviderPids(); 439 440 /** 441 * Dump out information about available providers and devices 442 */ 443 status_t dump(int fd, const Vector<String16>& args); 444 445 /** 446 * Conversion methods between HAL Status and status_t and strings 447 */ 448 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s); 449 static const char* statusToString(const hardware::camera::common::V1_0::Status& s); 450 451 /* 452 * Return provider type for a specific device. 453 */ 454 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id) const; 455 456 /* 457 * Check if a camera is a logical camera. And if yes, return 458 * the physical camera ids. 459 */ 460 bool isLogicalCamera(const std::string& id, std::vector<std::string>* physicalCameraIds); 461 462 status_t getSystemCameraKind(const std::string& id, SystemCameraKind *kind) const; 463 bool isHiddenPhysicalCamera(const std::string& cameraId) const; 464 465 status_t filterSmallJpegSizes(const std::string& cameraId); 466 467 status_t notifyUsbDeviceEvent(int32_t eventId, const std::string &usbDeviceId); 468 469 static bool isConcurrentDynamicRangeCaptureSupported(const CameraMetadata& deviceInfo, 470 int64_t profile, int64_t concurrentProfile); 471 472 static const float kDepthARTolerance; 473 static const bool kFrameworkJpegRDisabled; 474 static const bool kFrameworkHeicUltraHDRDisabled; 475 static const bool kFrameworkHeicAllowSWCodecs; 476 477 private: 478 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use 479 mutable std::mutex mInterfaceMutex; 480 481 wp<StatusListener> mListener; 482 HidlServiceInteractionProxy* mHidlServiceProxy; 483 AidlServiceInteractionProxy* mAidlServiceProxy; 484 485 // Current overall Android device physical status 486 int64_t mDeviceState; 487 488 // mProviderLifecycleLock is locked during onRegistration and removeProvider 489 mutable std::mutex mProviderLifecycleLock; 490 491 static HidlServiceInteractionProxyImpl sHidlServiceInteractionProxy; 492 static AidlServiceInteractionProxyImpl sAidlServiceInteractionProxy; 493 494 struct HalCameraProvider { 495 // Empty parent struct for storing either aidl / hidl camera provider reference HalCameraProviderHalCameraProvider496 HalCameraProvider(const char *descriptor) : mDescriptor(descriptor) { }; ~HalCameraProviderHalCameraProvider497 virtual ~HalCameraProvider() {}; 498 std::string mDescriptor; 499 }; 500 501 struct HidlHalCameraProvider : public HalCameraProvider { HidlHalCameraProviderHidlHalCameraProvider502 HidlHalCameraProvider( 503 const sp<hardware::camera::provider::V2_4::ICameraProvider> &provider, 504 const char *descriptor) : 505 HalCameraProvider(descriptor), mCameraProvider(provider) { }; 506 private: 507 sp<hardware::camera::provider::V2_4::ICameraProvider> mCameraProvider; 508 }; 509 510 struct AidlHalCameraProvider : public HalCameraProvider { AidlHalCameraProviderAidlHalCameraProvider511 AidlHalCameraProvider( 512 const std::shared_ptr< 513 aidl::android::hardware::camera::provider::ICameraProvider> &provider, 514 const char *descriptor) : 515 HalCameraProvider(descriptor), mCameraProvider(provider) { }; 516 private: 517 std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider> mCameraProvider; 518 }; 519 520 521 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the 522 // ICameraProvider alive while it is in use by the camera with the given ID for camera 523 // capabilities 524 std::unordered_map<std::string, std::shared_ptr<HalCameraProvider>> 525 mCameraProviderByCameraId; 526 527 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the 528 // ICameraProvider alive while it is in use by the camera with the given ID for torch 529 // capabilities 530 std::unordered_map<std::string, std::shared_ptr<HalCameraProvider>> 531 mTorchProviderByCameraId; 532 533 // Lock for accessing mCameraProviderByCameraId and mTorchProviderByCameraId 534 std::mutex mProviderInterfaceMapLock; 535 struct ProviderInfo : public virtual RefBase { 536 friend struct HidlProviderInfo; 537 friend struct AidlProviderInfo; 538 const std::string mProviderName; 539 const std::string mProviderInstance; 540 const metadata_vendor_id_t mProviderTagid; 541 int32_t mMinorVersion; 542 sp<VendorTagDescriptor> mVendorTagDescriptor; 543 bool mSetTorchModeSupported; 544 bool mIsRemote; 545 546 ProviderInfo(const std::string &providerName, const std::string &providerInstance, 547 CameraProviderManager *manager); 548 ~ProviderInfo(); 549 550 virtual IPCTransport getIPCTransport() const = 0; 551 552 const std::string& getType() const; 553 554 status_t dump(int fd, const Vector<String16>& args) const; 555 556 void initializeProviderInfoCommon(const std::vector<std::string> &devices); 557 /** 558 * Setup vendor tags for this provider 559 */ 560 virtual status_t setUpVendorTags() = 0; 561 562 /** 563 * Notify provider about top-level device physical state changes 564 * 565 * Note that 'mInterfaceMutex' should not be held when calling this method. 566 * It is possible for camera providers to add/remove devices and try to 567 * acquire it. 568 */ 569 virtual status_t notifyDeviceStateChange(int64_t newDeviceState) = 0; 570 571 virtual bool successfullyStartedProviderInterface() = 0; 572 573 virtual int64_t getDeviceState() = 0; 574 575 std::vector<std::unordered_set<std::string>> getConcurrentCameraIdCombinations(); 576 577 /** 578 * Notify 'DeviceInfo' instanced about top-level device physical state changes 579 * 580 * Note that 'mInterfaceMutex' should be held when calling this method. 581 */ 582 void notifyDeviceInfoStateChangeLocked(int64_t newDeviceState); 583 584 /** 585 * Query the camera provider for concurrent stream configuration support 586 */ 587 virtual status_t isConcurrentSessionConfigurationSupported( 588 const std::vector<CameraIdAndSessionConfiguration> &cameraIdsAndSessionConfigs, 589 const std::set<std::string>& perfClassPrimaryCameraIds, 590 int targetSdkVersion, bool *isSupported) = 0; 591 592 /** 593 * Remove all devices associated with this provider and notify listeners 594 * with NOT_PRESENT state. 595 */ 596 void removeAllDevices(); 597 598 /** 599 * Provider is an external lazy HAL 600 */ 601 bool isExternalLazyHAL() const; 602 603 // Basic device information, common to all camera devices 604 struct DeviceInfo { 605 const std::string mName; // Full instance name 606 const std::string mId; // ID section of full name 607 //Both hidl and aidl DeviceInfos. Aidl deviceInfos get {3, 8} to 608 //start off. 609 const hardware::hidl_version mVersion; 610 const metadata_vendor_id_t mProviderTagid; 611 bool mIsLogicalCamera; 612 std::vector<std::string> mPhysicalIds; 613 hardware::CameraInfo mInfo; 614 SystemCameraKind mSystemCameraKind = SystemCameraKind::PUBLIC; 615 616 const CameraResourceCost mResourceCost; 617 618 CameraDeviceStatus mStatus; 619 620 wp<ProviderInfo> mParentProvider; 621 // Torch strength default, maximum levels if the torch strength control 622 // feature is supported. 623 int32_t mTorchStrengthLevel; 624 int32_t mTorchMaximumStrengthLevel; 625 int32_t mTorchDefaultStrengthLevel; 626 627 // Wait for lazy HALs to confirm device availability 628 static const nsecs_t kDeviceAvailableTimeout = 2000e6; // 2000 ms 629 Mutex mDeviceAvailableLock; 630 Condition mDeviceAvailableSignal; 631 bool mIsDeviceAvailable = true; 632 hasFlashUnitProviderInfo::DeviceInfo633 bool hasFlashUnit() const { return mHasFlashUnit; } supportNativeZoomRatioProviderInfo::DeviceInfo634 bool supportNativeZoomRatio() const { return mSupportNativeZoomRatio; } isCompositeJpegRDisabledProviderInfo::DeviceInfo635 bool isCompositeJpegRDisabled() const { return mCompositeJpegRDisabled; } isCompositeHeicUltraHDRDisabledProviderInfo::DeviceInfo636 bool isCompositeHeicUltraHDRDisabled() const { return mCompositeHeicUltraHDRDisabled; } 637 virtual status_t setTorchMode(bool enabled) = 0; 638 virtual status_t turnOnTorchWithStrengthLevel(int32_t torchStrength) = 0; 639 virtual status_t getTorchStrengthLevel(int32_t *torchStrength) = 0; 640 virtual status_t getCameraInfo( 641 int rotationOverride, 642 int *portraitRotation, 643 hardware::CameraInfo *info) const = 0; 644 virtual bool isAPI1Compatible() const = 0; 645 virtual status_t dumpState(int fd) = 0; getCameraCharacteristicsProviderInfo::DeviceInfo646 virtual status_t getCameraCharacteristics( 647 [[maybe_unused]] bool overrideForPerfClass, 648 [[maybe_unused]] CameraMetadata *characteristics, 649 [[maybe_unused]] int rotationOverride) { 650 return INVALID_OPERATION; 651 } getPhysicalCameraCharacteristicsProviderInfo::DeviceInfo652 virtual status_t getPhysicalCameraCharacteristics( 653 [[maybe_unused]] const std::string& physicalCameraId, 654 [[maybe_unused]] CameraMetadata *characteristics) const { 655 return INVALID_OPERATION; 656 } 657 isSessionConfigurationSupportedProviderInfo::DeviceInfo658 virtual status_t isSessionConfigurationSupported( 659 const SessionConfiguration &/*configuration*/, 660 bool /*overrideForPerfClass*/, 661 camera3::metadataGetter /*getMetadata*/, 662 bool /*checkSessionParams*/, 663 bool * /*status*/) { 664 return INVALID_OPERATION; 665 } 666 getSessionCharacteristicsProviderInfo::DeviceInfo667 virtual status_t getSessionCharacteristics( 668 const SessionConfiguration &/*configuration*/, 669 bool /*overrideForPerfClass*/, 670 camera3::metadataGetter /*getMetadata*/, CameraMetadata* /*outChars*/) { 671 return INVALID_OPERATION; 672 } 673 674 virtual status_t filterSmallJpegSizes() = 0; notifyDeviceStateChangeProviderInfo::DeviceInfo675 virtual void notifyDeviceStateChange(int64_t /*newState*/) {} createDefaultRequestProviderInfo::DeviceInfo676 virtual status_t createDefaultRequest( 677 camera3::camera_request_template_t /*templateId*/, 678 CameraMetadata* /*metadata*/) { 679 return INVALID_OPERATION; 680 } 681 DeviceInfoProviderInfo::DeviceInfo682 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId, 683 const std::string &id, const hardware::hidl_version& version, 684 const std::vector<std::string>& publicCameraIds, 685 const CameraResourceCost& resourceCost, 686 sp<ProviderInfo> parentProvider) : 687 mName(name), mId(id), mVersion(version), mProviderTagid(tagId), 688 mIsLogicalCamera(false), mResourceCost(resourceCost), 689 mStatus(CameraDeviceStatus::PRESENT), 690 mParentProvider(parentProvider), mTorchStrengthLevel(0), 691 mTorchMaximumStrengthLevel(0), mTorchDefaultStrengthLevel(0), 692 mHasFlashUnit(false), mSupportNativeZoomRatio(false), 693 mPublicCameraIds(publicCameraIds), mCompositeJpegRDisabled(false), 694 mCompositeHeicUltraHDRDisabled(false) {} ~DeviceInfoProviderInfo::DeviceInfo695 virtual ~DeviceInfo() {} 696 protected: 697 698 bool mHasFlashUnit; // const after constructor 699 bool mSupportNativeZoomRatio; // const after constructor 700 const std::vector<std::string>& mPublicCameraIds; 701 bool mCompositeJpegRDisabled, mCompositeHeicUltraHDRDisabled; 702 }; 703 std::vector<std::unique_ptr<DeviceInfo>> mDevices; 704 std::unordered_set<std::string> mUniqueCameraIds; 705 std::unordered_map<std::string, std::set<std::string>> mUnavailablePhysicalCameras; 706 int mUniqueDeviceCount; 707 std::vector<std::string> mUniqueAPI1CompatibleCameraIds; 708 // The initial public camera IDs published by the camera provider. 709 // Currently logical multi-camera is not supported for hot-plug camera. 710 // And we use this list to keep track of initial public camera IDs 711 // advertised by the provider, and to distinguish against "hidden" 712 // physical camera IDs. 713 std::vector<std::string> mProviderPublicCameraIds; 714 715 // HALv3-specific camera fields, including the actual device interface 716 struct DeviceInfo3 : public DeviceInfo { 717 718 virtual status_t setTorchMode(bool enabled) = 0; 719 virtual status_t turnOnTorchWithStrengthLevel(int32_t torchStrength) = 0; 720 virtual status_t getTorchStrengthLevel(int32_t *torchStrength) = 0; 721 virtual status_t getCameraInfo( 722 int rotationOverride, 723 int *portraitRotation, 724 hardware::CameraInfo *info) const override; 725 virtual bool isAPI1Compatible() const override; 726 virtual status_t dumpState(int fd) = 0; 727 virtual status_t getCameraCharacteristics( 728 bool overrideForPerfClass, 729 CameraMetadata *characteristics, 730 int rotationOverride) override; 731 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId, 732 CameraMetadata *characteristics) const override; 733 virtual status_t filterSmallJpegSizes() override; 734 virtual void notifyDeviceStateChange( 735 int64_t newState) override; 736 737 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId, 738 const std::string &id, uint16_t minorVersion, 739 const CameraResourceCost& resourceCost, 740 sp<ProviderInfo> parentProvider, 741 const std::vector<std::string>& publicCameraIds); ~DeviceInfo3ProviderInfo::DeviceInfo3742 virtual ~DeviceInfo3() {}; 743 protected: 744 // Modified by derived transport specific (hidl / aidl) class 745 CameraMetadata mCameraCharacteristics; 746 // Map device states to sensor orientations 747 std::unordered_map<int64_t, int32_t> mDeviceStateOrientationMap; 748 // A copy of mCameraCharacteristics without performance class 749 // override 750 std::unique_ptr<CameraMetadata> mCameraCharNoPCOverride; 751 // Only contains characteristics for hidden physical cameras, 752 // not for public physical cameras. 753 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics; 754 // Value filled in from addSessionConfigQueryVersionTag. 755 // Cached to make lookups faster 756 int mSessionConfigQueryVersion = 0; 757 758 void queryPhysicalCameraIds(); 759 SystemCameraKind getSystemCameraKind(); 760 status_t fixupMonochromeTags(); 761 status_t fixupTorchStrengthTags(); 762 status_t fixupManualFlashStrengthControlTags(CameraMetadata& ch); 763 status_t addDynamicDepthTags(bool maxResolution = false); 764 status_t deriveHeicTags(bool maxResolution = false); 765 status_t deriveJpegRTags(bool maxResolution = false); 766 status_t deriveHeicUltraHDRTags(bool maxResolution = false); 767 status_t deriveBlobDurationEntries( 768 const CameraMetadata& c, bool maxResolution, 769 const std::vector<std::tuple<size_t, size_t>>& filteredSizes, 770 std::vector<int64_t>* filteredStallDurations /*out*/, 771 std::vector<int64_t>* filteredMinDurations /*out*/); 772 status_t insertStreamConfigTags(int32_t sizeTag, int32_t minFrameDurationTag, 773 int32_t stallDurationTag, 774 const std::vector<int32_t>& sizeEntries, 775 const std::vector<int64_t>& minFrameDurationEntries, 776 const std::vector<int64_t>& stallDurationEntries, 777 CameraMetadata* c /*out*/); 778 status_t addRotateCropTags(); 779 status_t addAutoframingTags(); 780 status_t addPreCorrectionActiveArraySize(); 781 status_t addReadoutTimestampTag(bool readoutTimestampSupported = true); 782 status_t addColorCorrectionAvailableModesTag(CameraMetadata& ch); 783 status_t addAePriorityModeTags(); 784 status_t addSessionConfigQueryVersionTag(); 785 status_t addSharedSessionConfigurationTags(); 786 bool isAutomotiveDevice(); 787 788 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag, 789 android_pixel_format_t format, 790 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/); 791 static void getSupportedDurations( const CameraMetadata& ch, uint32_t tag, 792 android_pixel_format_t format, 793 const std::vector<std::tuple<size_t, size_t>>& sizes, 794 std::vector<int64_t> *durations/*out*/); 795 static void getSupportedDynamicDepthDurations( 796 const std::vector<int64_t>& depthDurations, 797 const std::vector<int64_t>& blobDurations, 798 std::vector<int64_t> *dynamicDepthDurations /*out*/); 799 static void getSupportedDynamicDepthSizes( 800 const std::vector<std::tuple<size_t, size_t>>& blobSizes, 801 const std::vector<std::tuple<size_t, size_t>>& depthSizes, 802 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/, 803 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/); 804 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys, 805 uint32_t keyTag); 806 status_t fillHeicStreamCombinations(std::vector<int32_t>* outputs, 807 std::vector<int64_t>* durations, 808 std::vector<int64_t>* stallDurations, 809 const camera_metadata_entry& halStreamConfigs, 810 const camera_metadata_entry& halStreamDurations); 811 }; 812 protected: 813 std::string mType; 814 uint32_t mId; 815 816 std::mutex mLock; 817 818 CameraProviderManager *mManager; 819 820 struct CameraStatusInfoT { 821 bool isPhysicalCameraStatus = false; 822 std::string cameraId; 823 std::string physicalCameraId; 824 CameraDeviceStatus status; CameraStatusInfoTProviderInfo::CameraStatusInfoT825 CameraStatusInfoT(bool isForPhysicalCamera, const std::string& id, 826 const std::string& physicalId, 827 CameraDeviceStatus s) : 828 isPhysicalCameraStatus(isForPhysicalCamera), cameraId(id), 829 physicalCameraId(physicalId), status(s) {} 830 }; 831 832 // Lock to synchronize between initialize() and camera status callbacks 833 std::mutex mInitLock; 834 bool mInitialized = false; 835 std::vector<CameraStatusInfoT> mCachedStatus; 836 // End of scope for mInitLock 837 838 std::unique_ptr<ProviderInfo::DeviceInfo> 839 virtual initializeDeviceInfo( 840 const std::string &name, const metadata_vendor_id_t tagId, 841 const std::string &id, uint16_t minorVersion) = 0; 842 843 virtual status_t reCacheConcurrentStreamingCameraIdsLocked() = 0; 844 845 std::vector<std::unordered_set<std::string>> mConcurrentCameraIdCombinations; 846 847 // Parse provider instance name for type and id 848 static status_t parseProviderName(const std::string& name, 849 std::string *type, uint32_t *id); 850 851 // Parse device instance name for device version, type, and id. 852 static status_t parseDeviceName(const std::string& name, 853 uint16_t *major, uint16_t *minor, std::string *type, std::string *id); 854 855 // Generate vendor tag id 856 static metadata_vendor_id_t generateVendorTagId(const std::string &name); 857 858 status_t addDevice( 859 const std::string& name, CameraDeviceStatus initialStatus, 860 /*out*/ std::string* parsedId); 861 862 void cameraDeviceStatusChangeInternal(const std::string& cameraDeviceName, 863 CameraDeviceStatus newStatus); 864 865 status_t cameraDeviceStatusChangeLocked( 866 std::string* id, const std::string& cameraDeviceName, 867 CameraDeviceStatus newStatus); 868 869 void physicalCameraDeviceStatusChangeInternal(const std::string& cameraDeviceName, 870 const std::string& physicalCameraDeviceName, 871 CameraDeviceStatus newStatus); 872 873 status_t physicalCameraDeviceStatusChangeLocked( 874 std::string* id, std::string* physicalId, 875 const std::string& cameraDeviceName, 876 const std::string& physicalCameraDeviceName, 877 CameraDeviceStatus newStatus); 878 879 void torchModeStatusChangeInternal(const std::string& cameraDeviceName, 880 TorchModeStatus newStatus); 881 882 void removeDevice(const std::string &id); 883 884 }; 885 886 template <class ProviderInfoType, class HalCameraProviderType> 887 status_t setTorchModeT(sp<ProviderInfo> &parentProvider, 888 std::shared_ptr<HalCameraProvider> *halCameraProvider); 889 890 // Try to get hidl provider services declared. Expects mInterfaceMutex to be 891 // locked. Also registers for hidl provider service notifications. 892 status_t tryToInitAndAddHidlProvidersLocked(HidlServiceInteractionProxy *hidlProxy); 893 894 // Try to get aidl provider services declared. Expects mInterfaceMutex to be 895 // locked. Also registers for aidl provider service notifications. 896 status_t tryToAddAidlProvidersLocked(); 897 898 /** 899 * Save the ICameraProvider while it is being used by a camera or torch client 900 */ 901 void saveRef(DeviceMode usageType, const std::string &cameraId, 902 std::shared_ptr<HalCameraProvider> provider); 903 904 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held 905 // and the calling code doesn't mutate the list of providers or their lists of devices. 906 // No guarantees on the order of traversal 907 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id) const; 908 909 bool isCompositeJpegRDisabledLocked(const std::string &id) const; 910 911 // Map external providers to USB devices in order to handle USB hotplug 912 // events for lazy HALs 913 std::pair<std::vector<std::string>, sp<ProviderInfo>> 914 mExternalUsbDevicesForProvider; 915 sp<ProviderInfo> startExternalLazyProvider() const; 916 917 status_t addHidlProviderLocked(const std::string& newProvider, bool preexisting = false); 918 919 status_t addAidlProviderLocked(const std::string& newProvider); 920 921 status_t tryToInitializeHidlProviderLocked(const std::string& providerName, 922 const sp<ProviderInfo>& providerInfo); 923 924 status_t tryToInitializeAidlProviderLocked(const std::string& providerName, 925 const sp<ProviderInfo>& providerInfo); 926 927 bool isLogicalCameraLocked(const std::string& id, std::vector<std::string>* physicalCameraIds); 928 929 // No method corresponding to the same provider / member belonging to the 930 // same provider should be used after this method is called since it'll lead 931 // to invalid memory access (especially since this is called by ProviderInfo methods on hal 932 // service death). 933 status_t removeProvider(const std::string& provider); 934 sp<StatusListener> getStatusListener() const; 935 936 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion, 937 IPCTransport transport) const; 938 939 size_t mProviderInstanceId = 0; 940 std::vector<sp<ProviderInfo>> mProviders; 941 // Provider names of AIDL providers with retrieved binders. 942 std::set<std::string> mAidlProviderWithBinders; 943 944 std::mutex mProviderPidMapLock; 945 std::map<std::string, pid_t> mProviderPidMap; 946 947 static const char* deviceStatusToString( 948 const hardware::camera::common::V1_0::CameraDeviceStatus&); 949 static const char* torchStatusToString( 950 const hardware::camera::common::V1_0::TorchModeStatus&); 951 952 status_t getCameraCharacteristicsLocked(const std::string &id, bool overrideForPerfClass, 953 CameraMetadata* characteristics, int rotationOverride) const; 954 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const; 955 956 status_t getSystemCameraKindLocked(const std::string& id, SystemCameraKind *kind) const; 957 std::pair<bool, ProviderInfo::DeviceInfo *> isHiddenPhysicalCameraInternal( 958 const std::string& cameraId) const; 959 960 void collectDeviceIdsLocked(const std::vector<std::string> deviceIds, 961 std::vector<std::string>& normalDeviceIds, 962 std::vector<std::string>& systemCameraDeviceIds) const; 963 964 status_t usbDeviceDetached(const std::string &usbDeviceId); 965 966 static bool isVirtualCameraHalEnabled(); 967 }; 968 969 } // namespace android 970 971 #endif 972