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