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 #include <future> 27 28 #include <camera/camera2/ConcurrentCamera.h> 29 #include <camera/CameraParameters2.h> 30 #include <camera/CameraMetadata.h> 31 #include <camera/CameraBase.h> 32 #include <utils/Errors.h> 33 #include <android/hardware/camera/common/1.0/types.h> 34 #include <android/hardware/camera/provider/2.5/ICameraProvider.h> 35 #include <android/hardware/camera/provider/2.6/ICameraProviderCallback.h> 36 #include <android/hardware/camera/provider/2.6/ICameraProvider.h> 37 #include <android/hardware/camera/provider/2.7/ICameraProvider.h> 38 #include <android/hardware/camera/device/3.7/types.h> 39 #include <android/hidl/manager/1.0/IServiceNotification.h> 40 #include <camera/VendorTagDescriptor.h> 41 42 namespace android { 43 44 /** 45 * The vendor tag descriptor class that takes HIDL vendor tag information as 46 * input. Not part of VendorTagDescriptor class because that class is used 47 * in AIDL generated sources which don't have access to HIDL headers. 48 */ 49 class HidlVendorTagDescriptor : public VendorTagDescriptor { 50 public: 51 /** 52 * Create a VendorTagDescriptor object from the HIDL VendorTagSection 53 * vector. 54 * 55 * Returns OK on success, or a negative error code. 56 */ 57 static status_t createDescriptorFromHidl( 58 const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts, 59 /*out*/ 60 sp<VendorTagDescriptor>& descriptor); 61 }; 62 63 enum SystemCameraKind { 64 /** 65 * These camera devices are visible to all apps and system components alike 66 */ 67 PUBLIC = 0, 68 69 /** 70 * These camera devices are visible only to processes having the 71 * android.permission.SYSTEM_CAMERA permission. They are not exposed to 3P 72 * apps. 73 */ 74 SYSTEM_ONLY_CAMERA, 75 76 /** 77 * These camera devices are visible only to HAL clients (that try to connect 78 * on a hwbinder thread). 79 */ 80 HIDDEN_SECURE_CAMERA 81 }; 82 83 #define CAMERA_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0) 84 #define CAMERA_DEVICE_API_VERSION_3_0 HARDWARE_DEVICE_API_VERSION(3, 0) 85 #define CAMERA_DEVICE_API_VERSION_3_1 HARDWARE_DEVICE_API_VERSION(3, 1) 86 #define CAMERA_DEVICE_API_VERSION_3_2 HARDWARE_DEVICE_API_VERSION(3, 2) 87 #define CAMERA_DEVICE_API_VERSION_3_3 HARDWARE_DEVICE_API_VERSION(3, 3) 88 #define CAMERA_DEVICE_API_VERSION_3_4 HARDWARE_DEVICE_API_VERSION(3, 4) 89 #define CAMERA_DEVICE_API_VERSION_3_5 HARDWARE_DEVICE_API_VERSION(3, 5) 90 #define CAMERA_DEVICE_API_VERSION_3_6 HARDWARE_DEVICE_API_VERSION(3, 6) 91 #define CAMERA_DEVICE_API_VERSION_3_7 HARDWARE_DEVICE_API_VERSION(3, 7) 92 93 /** 94 * A manager for all camera providers available on an Android device. 95 * 96 * Responsible for enumerating providers and the individual camera devices 97 * they export, both at startup and as providers and devices are added/removed. 98 * 99 * Provides methods for requesting information about individual devices and for 100 * opening them for active use. 101 * 102 */ 103 class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification { 104 public: 105 106 ~CameraProviderManager(); 107 108 // Tiny proxy for the static methods in a HIDL interface that communicate with the hardware 109 // service manager, to be replacable in unit tests with a fake. 110 struct ServiceInteractionProxy { 111 virtual bool registerForNotifications( 112 const std::string &serviceName, 113 const sp<hidl::manager::V1_0::IServiceNotification> 114 ¬ification) = 0; 115 // Will not wait for service to start if it's not already running 116 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService( 117 const std::string &serviceName) = 0; 118 // Will block for service if it exists but isn't running 119 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService( 120 const std::string &serviceName) = 0; 121 virtual hardware::hidl_vec<hardware::hidl_string> listServices() = 0; ~ServiceInteractionProxyServiceInteractionProxy122 virtual ~ServiceInteractionProxy() {} 123 }; 124 125 // Standard use case - call into the normal generated static methods which invoke 126 // the real hardware service manager 127 struct HardwareServiceInteractionProxy : public ServiceInteractionProxy { registerForNotificationsHardwareServiceInteractionProxy128 virtual bool registerForNotifications( 129 const std::string &serviceName, 130 const sp<hidl::manager::V1_0::IServiceNotification> 131 ¬ification) override { 132 return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications( 133 serviceName, notification); 134 } tryGetServiceHardwareServiceInteractionProxy135 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService( 136 const std::string &serviceName) override { 137 return hardware::camera::provider::V2_4::ICameraProvider::tryGetService(serviceName); 138 } getServiceHardwareServiceInteractionProxy139 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService( 140 const std::string &serviceName) override { 141 return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName); 142 } 143 144 virtual hardware::hidl_vec<hardware::hidl_string> listServices() override; 145 }; 146 147 /** 148 * Listener interface for device/torch status changes 149 */ 150 struct StatusListener : virtual public RefBase { ~StatusListenerStatusListener151 ~StatusListener() {} 152 153 virtual void onDeviceStatusChanged(const String8 &cameraId, 154 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0; 155 virtual void onDeviceStatusChanged(const String8 &cameraId, 156 const String8 &physicalCameraId, 157 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0; 158 virtual void onTorchStatusChanged(const String8 &cameraId, 159 hardware::camera::common::V1_0::TorchModeStatus newStatus) = 0; 160 virtual void onNewProviderRegistered() = 0; 161 }; 162 163 /** 164 * Represents the mode a camera device is currently in 165 */ 166 enum class DeviceMode { 167 TORCH, 168 CAMERA 169 }; 170 171 /** 172 * Initialize the manager and give it a status listener; optionally accepts a service 173 * interaction proxy. 174 * 175 * The default proxy communicates via the hardware service manager; alternate proxies can be 176 * used for testing. The lifetime of the proxy must exceed the lifetime of the manager. 177 */ 178 status_t initialize(wp<StatusListener> listener, 179 ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy); 180 181 /** 182 * Retrieve the total number of available cameras. 183 * This value may change dynamically as cameras are added or removed. 184 */ 185 std::pair<int, int> getCameraCount() const; 186 187 std::vector<std::string> getCameraDeviceIds() const; 188 189 /** 190 * Retrieve the number of API1 compatible cameras; these are internal and 191 * backwards-compatible. This is the set of cameras that will be 192 * accessible via the old camera API. 193 * The return value may change dynamically due to external camera hotplug. 194 */ 195 std::vector<std::string> getAPI1CompatibleCameraDeviceIds() const; 196 197 /** 198 * Return true if a device with a given ID and major version exists 199 */ 200 bool isValidDevice(const std::string &id, uint16_t majorVersion) const; 201 202 /** 203 * Return true if a device with a given ID has a flash unit. Returns false 204 * for devices that are unknown. 205 */ 206 bool hasFlashUnit(const std::string &id) const; 207 208 /** 209 * Return true if the camera device has native zoom ratio support. 210 */ 211 bool supportNativeZoomRatio(const std::string &id) const; 212 213 /** 214 * Return the resource cost of this camera device 215 */ 216 status_t getResourceCost(const std::string &id, 217 hardware::camera::common::V1_0::CameraResourceCost* cost) const; 218 219 /** 220 * Return the old camera API camera info 221 */ 222 status_t getCameraInfo(const std::string &id, 223 hardware::CameraInfo* info) const; 224 225 /** 226 * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does 227 * not have a v3 or newer HAL version. 228 */ 229 status_t getCameraCharacteristics(const std::string &id, 230 bool overrideForPerfClass, CameraMetadata* characteristics) const; 231 232 status_t isConcurrentSessionConfigurationSupported( 233 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration> 234 &cameraIdsAndSessionConfigs, 235 const std::set<std::string>& perfClassPrimaryCameraIds, 236 int targetSdkVersion, bool *isSupported); 237 238 std::vector<std::unordered_set<std::string>> getConcurrentCameraIds() const; 239 /** 240 * Check for device support of specific stream combination. 241 */ 242 status_t isSessionConfigurationSupported(const std::string& id, 243 const hardware::camera::device::V3_7::StreamConfiguration &configuration, 244 bool *status /*out*/) const; 245 246 /** 247 * Return the highest supported device interface version for this ID 248 */ 249 status_t getHighestSupportedVersion(const std::string &id, 250 hardware::hidl_version *v); 251 252 /** 253 * Check if a given camera device support setTorchMode API. 254 */ 255 bool supportSetTorchMode(const std::string &id) const; 256 257 /** 258 * Turn on or off the flashlight on a given camera device. 259 * May fail if the device does not support this API, is in active use, or if the device 260 * doesn't exist, etc. 261 */ 262 status_t setTorchMode(const std::string &id, bool enabled); 263 264 /** 265 * Setup vendor tags for all registered providers 266 */ 267 status_t setUpVendorTags(); 268 269 /** 270 * Inform registered providers about a device state change, such as folding or unfolding 271 */ 272 status_t notifyDeviceStateChange( 273 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> newState); 274 275 /** 276 * Open an active session to a camera device. 277 * 278 * This fully powers on the camera device hardware, and returns a handle to a 279 * session to be used for hardware configuration and operation. 280 */ 281 status_t openSession(const std::string &id, 282 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback, 283 /*out*/ 284 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session); 285 286 /** 287 * Save the ICameraProvider while it is being used by a camera or torch client 288 */ 289 void saveRef(DeviceMode usageType, const std::string &cameraId, 290 sp<hardware::camera::provider::V2_4::ICameraProvider> provider); 291 292 /** 293 * Notify that the camera or torch is no longer being used by a camera client 294 */ 295 void removeRef(DeviceMode usageType, const std::string &cameraId); 296 297 /** 298 * IServiceNotification::onRegistration 299 * Invoked by the hardware service manager when a new camera provider is registered 300 */ 301 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName, 302 const hardware::hidl_string& name, 303 bool preexisting) override; 304 305 /** 306 * Dump out information about available providers and devices 307 */ 308 status_t dump(int fd, const Vector<String16>& args); 309 310 /** 311 * Conversion methods between HAL Status and status_t and strings 312 */ 313 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s); 314 static const char* statusToString(const hardware::camera::common::V1_0::Status& s); 315 316 /* 317 * Return provider type for a specific device. 318 */ 319 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id, 320 hardware::hidl_version minVersion = hardware::hidl_version{0,0}, 321 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const; 322 323 /* 324 * Check if a camera is a logical camera. And if yes, return 325 * the physical camera ids. 326 */ 327 bool isLogicalCamera(const std::string& id, std::vector<std::string>* physicalCameraIds); 328 329 status_t getSystemCameraKind(const std::string& id, SystemCameraKind *kind) const; 330 bool isHiddenPhysicalCamera(const std::string& cameraId) const; 331 332 status_t filterSmallJpegSizes(const std::string& cameraId); 333 334 static const float kDepthARTolerance; 335 private: 336 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use 337 mutable std::mutex mInterfaceMutex; 338 339 // the status listener update callbacks will lock mStatusMutex 340 mutable std::mutex mStatusListenerMutex; 341 wp<StatusListener> mListener; 342 ServiceInteractionProxy* mServiceProxy; 343 344 // Current overall Android device physical status 345 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState; 346 347 // mProviderLifecycleLock is locked during onRegistration and removeProvider 348 mutable std::mutex mProviderLifecycleLock; 349 350 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy; 351 352 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the 353 // ICameraProvider alive while it is in use by the camera with the given ID for camera 354 // capabilities 355 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> 356 mCameraProviderByCameraId; 357 358 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the 359 // ICameraProvider alive while it is in use by the camera with the given ID for torch 360 // capabilities 361 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> 362 mTorchProviderByCameraId; 363 364 // Lock for accessing mCameraProviderByCameraId and mTorchProviderByCameraId 365 std::mutex mProviderInterfaceMapLock; 366 367 struct ProviderInfo : 368 virtual public hardware::camera::provider::V2_6::ICameraProviderCallback, 369 virtual public hardware::hidl_death_recipient 370 { 371 const std::string mProviderName; 372 const std::string mProviderInstance; 373 const metadata_vendor_id_t mProviderTagid; 374 int mMinorVersion; 375 sp<VendorTagDescriptor> mVendorTagDescriptor; 376 bool mSetTorchModeSupported; 377 bool mIsRemote; 378 379 // Current overall Android device physical status 380 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState; 381 382 // This pointer is used to keep a reference to the ICameraProvider that was last accessed. 383 wp<hardware::camera::provider::V2_4::ICameraProvider> mActiveInterface; 384 385 sp<hardware::camera::provider::V2_4::ICameraProvider> mSavedInterface; 386 387 ProviderInfo(const std::string &providerName, const std::string &providerInstance, 388 CameraProviderManager *manager); 389 ~ProviderInfo(); 390 391 status_t initialize(sp<hardware::camera::provider::V2_4::ICameraProvider>& interface, 392 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> 393 currentDeviceState); 394 395 const sp<hardware::camera::provider::V2_4::ICameraProvider> startProviderInterface(); 396 397 const std::string& getType() const; 398 399 status_t addDevice(const std::string& name, 400 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus = 401 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT, 402 /*out*/ std::string *parsedId = nullptr); 403 404 status_t dump(int fd, const Vector<String16>& args) const; 405 406 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex 407 hardware::Return<void> cameraDeviceStatusChange( 408 const hardware::hidl_string& cameraDeviceName, 409 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override; 410 hardware::Return<void> torchModeStatusChange( 411 const hardware::hidl_string& cameraDeviceName, 412 hardware::camera::common::V1_0::TorchModeStatus newStatus) override; 413 hardware::Return<void> physicalCameraDeviceStatusChange( 414 const hardware::hidl_string& cameraDeviceName, 415 const hardware::hidl_string& physicalCameraDeviceName, 416 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override; 417 418 status_t cameraDeviceStatusChangeLocked( 419 std::string* id, const hardware::hidl_string& cameraDeviceName, 420 hardware::camera::common::V1_0::CameraDeviceStatus newStatus); 421 status_t physicalCameraDeviceStatusChangeLocked( 422 std::string* id, std::string* physicalId, 423 const hardware::hidl_string& cameraDeviceName, 424 const hardware::hidl_string& physicalCameraDeviceName, 425 hardware::camera::common::V1_0::CameraDeviceStatus newStatus); 426 427 // hidl_death_recipient interface - this locks the parent mInterfaceMutex 428 virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override; 429 430 /** 431 * Setup vendor tags for this provider 432 */ 433 status_t setUpVendorTags(); 434 435 /** 436 * Notify provider about top-level device physical state changes 437 */ 438 status_t notifyDeviceStateChange( 439 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> 440 newDeviceState); 441 442 std::vector<std::unordered_set<std::string>> getConcurrentCameraIdCombinations(); 443 444 /** 445 * Query the camera provider for concurrent stream configuration support 446 */ 447 status_t isConcurrentSessionConfigurationSupported( 448 const hardware::hidl_vec< 449 hardware::camera::provider::V2_7::CameraIdAndStreamCombination> 450 &halCameraIdsAndStreamCombinations, 451 bool *isSupported); 452 453 // Basic device information, common to all camera devices 454 struct DeviceInfo { 455 const std::string mName; // Full instance name 456 const std::string mId; // ID section of full name 457 const hardware::hidl_version mVersion; 458 const metadata_vendor_id_t mProviderTagid; 459 bool mIsLogicalCamera; 460 std::vector<std::string> mPhysicalIds; 461 hardware::CameraInfo mInfo; 462 sp<IBase> mSavedInterface; 463 SystemCameraKind mSystemCameraKind = SystemCameraKind::PUBLIC; 464 465 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost; 466 467 hardware::camera::common::V1_0::CameraDeviceStatus mStatus; 468 469 wp<ProviderInfo> mParentProvider; 470 hasFlashUnitProviderInfo::DeviceInfo471 bool hasFlashUnit() const { return mHasFlashUnit; } supportNativeZoomRatioProviderInfo::DeviceInfo472 bool supportNativeZoomRatio() const { return mSupportNativeZoomRatio; } 473 virtual status_t setTorchMode(bool enabled) = 0; 474 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0; 475 virtual bool isAPI1Compatible() const = 0; 476 virtual status_t dumpState(int fd) = 0; getCameraCharacteristicsProviderInfo::DeviceInfo477 virtual status_t getCameraCharacteristics(bool overrideForPerfClass, 478 CameraMetadata *characteristics) const { 479 (void) overrideForPerfClass; 480 (void) characteristics; 481 return INVALID_OPERATION; 482 } getPhysicalCameraCharacteristicsProviderInfo::DeviceInfo483 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId, 484 CameraMetadata *characteristics) const { 485 (void) physicalCameraId; 486 (void) characteristics; 487 return INVALID_OPERATION; 488 } 489 isSessionConfigurationSupportedProviderInfo::DeviceInfo490 virtual status_t isSessionConfigurationSupported( 491 const hardware::camera::device::V3_7::StreamConfiguration &/*configuration*/, 492 bool * /*status*/) { 493 return INVALID_OPERATION; 494 } 495 virtual status_t filterSmallJpegSizes() = 0; 496 497 template<class InterfaceT> 498 sp<InterfaceT> startDeviceInterface(); 499 DeviceInfoProviderInfo::DeviceInfo500 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId, 501 const std::string &id, const hardware::hidl_version& version, 502 const std::vector<std::string>& publicCameraIds, 503 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost, 504 sp<ProviderInfo> parentProvider) : 505 mName(name), mId(id), mVersion(version), mProviderTagid(tagId), 506 mIsLogicalCamera(false), mResourceCost(resourceCost), 507 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT), 508 mParentProvider(parentProvider), mHasFlashUnit(false), 509 mSupportNativeZoomRatio(false), mPublicCameraIds(publicCameraIds) {} 510 virtual ~DeviceInfo(); 511 protected: 512 bool mHasFlashUnit; // const after constructor 513 bool mSupportNativeZoomRatio; // const after constructor 514 const std::vector<std::string>& mPublicCameraIds; 515 516 template<class InterfaceT> 517 static status_t setTorchMode(InterfaceT& interface, bool enabled); 518 519 template<class InterfaceT> setTorchModeForDeviceProviderInfo::DeviceInfo520 status_t setTorchModeForDevice(bool enabled) { 521 // Don't save the ICameraProvider interface here because we assume that this was 522 // called from CameraProviderManager::setTorchMode(), which does save it. 523 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>(); 524 return DeviceInfo::setTorchMode(interface, enabled); 525 } 526 }; 527 std::vector<std::unique_ptr<DeviceInfo>> mDevices; 528 std::unordered_set<std::string> mUniqueCameraIds; 529 int mUniqueDeviceCount; 530 std::vector<std::string> mUniqueAPI1CompatibleCameraIds; 531 // The initial public camera IDs published by the camera provider. 532 // Currently logical multi-camera is not supported for hot-plug camera. 533 // And we use this list to keep track of initial public camera IDs 534 // advertised by the provider, and to distinguish against "hidden" 535 // physical camera IDs. 536 std::vector<std::string> mProviderPublicCameraIds; 537 538 // HALv3-specific camera fields, including the actual device interface 539 struct DeviceInfo3 : public DeviceInfo { 540 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT; 541 542 virtual status_t setTorchMode(bool enabled) override; 543 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override; 544 virtual bool isAPI1Compatible() const override; 545 virtual status_t dumpState(int fd) override; 546 virtual status_t getCameraCharacteristics( 547 bool overrideForPerfClass, 548 CameraMetadata *characteristics) const override; 549 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId, 550 CameraMetadata *characteristics) const override; 551 virtual status_t isSessionConfigurationSupported( 552 const hardware::camera::device::V3_7::StreamConfiguration &configuration, 553 bool *status /*out*/) 554 override; 555 virtual status_t filterSmallJpegSizes() override; 556 557 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId, 558 const std::string &id, uint16_t minorVersion, 559 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost, 560 sp<ProviderInfo> parentProvider, 561 const std::vector<std::string>& publicCameraIds, sp<InterfaceT> interface); 562 virtual ~DeviceInfo3(); 563 private: 564 CameraMetadata mCameraCharacteristics; 565 // A copy of mCameraCharacteristics without performance class 566 // override 567 std::unique_ptr<CameraMetadata> mCameraCharNoPCOverride; 568 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics; 569 void queryPhysicalCameraIds(); 570 SystemCameraKind getSystemCameraKind(); 571 status_t fixupMonochromeTags(); 572 status_t addDynamicDepthTags(bool maxResolution = false); 573 status_t deriveHeicTags(bool maxResolution = false); 574 status_t addRotateCropTags(); 575 status_t addPreCorrectionActiveArraySize(); 576 577 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag, 578 android_pixel_format_t format, 579 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/); 580 static void getSupportedDurations( const CameraMetadata& ch, uint32_t tag, 581 android_pixel_format_t format, 582 const std::vector<std::tuple<size_t, size_t>>& sizes, 583 std::vector<int64_t> *durations/*out*/); 584 static void getSupportedDynamicDepthDurations( 585 const std::vector<int64_t>& depthDurations, 586 const std::vector<int64_t>& blobDurations, 587 std::vector<int64_t> *dynamicDepthDurations /*out*/); 588 static void getSupportedDynamicDepthSizes( 589 const std::vector<std::tuple<size_t, size_t>>& blobSizes, 590 const std::vector<std::tuple<size_t, size_t>>& depthSizes, 591 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/, 592 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/); 593 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys, 594 uint32_t keyTag); 595 status_t fillHeicStreamCombinations(std::vector<int32_t>* outputs, 596 std::vector<int64_t>* durations, 597 std::vector<int64_t>* stallDurations, 598 const camera_metadata_entry& halStreamConfigs, 599 const camera_metadata_entry& halStreamDurations); 600 }; 601 602 private: 603 std::string mType; 604 uint32_t mId; 605 606 std::mutex mLock; 607 608 CameraProviderManager *mManager; 609 610 struct CameraStatusInfoT { 611 bool isPhysicalCameraStatus = false; 612 hardware::hidl_string cameraId; 613 hardware::hidl_string physicalCameraId; 614 hardware::camera::common::V1_0::CameraDeviceStatus status; CameraStatusInfoTProviderInfo::CameraStatusInfoT615 CameraStatusInfoT(bool isForPhysicalCamera, const hardware::hidl_string& id, 616 const hardware::hidl_string& physicalId, 617 hardware::camera::common::V1_0::CameraDeviceStatus s) : 618 isPhysicalCameraStatus(isForPhysicalCamera), cameraId(id), 619 physicalCameraId(physicalId), status(s) {} 620 }; 621 622 // Lock to synchronize between initialize() and camera status callbacks 623 std::mutex mInitLock; 624 bool mInitialized = false; 625 std::vector<CameraStatusInfoT> mCachedStatus; 626 // End of scope for mInitLock 627 628 std::future<void> mInitialStatusCallbackFuture; 629 void notifyInitialStatusChange(sp<StatusListener> listener, 630 std::unique_ptr<std::vector<CameraStatusInfoT>> cachedStatus); 631 632 std::vector<std::unordered_set<std::string>> mConcurrentCameraIdCombinations; 633 634 // Templated method to instantiate the right kind of DeviceInfo and call the 635 // right CameraProvider getCameraDeviceInterface_* method. 636 template<class DeviceInfoT> 637 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name, 638 const metadata_vendor_id_t tagId, const std::string &id, 639 uint16_t minorVersion); 640 641 // Helper for initializeDeviceInfo to use the right CameraProvider get method. 642 template<class InterfaceT> 643 sp<InterfaceT> startDeviceInterface(const std::string &name); 644 645 // Parse provider instance name for type and id 646 static status_t parseProviderName(const std::string& name, 647 std::string *type, uint32_t *id); 648 649 // Parse device instance name for device version, type, and id. 650 static status_t parseDeviceName(const std::string& name, 651 uint16_t *major, uint16_t *minor, std::string *type, std::string *id); 652 653 // Generate vendor tag id 654 static metadata_vendor_id_t generateVendorTagId(const std::string &name); 655 656 void removeDevice(std::string id); 657 658 // Expects to have mLock locked 659 status_t reCacheConcurrentStreamingCameraIdsLocked(); 660 // Expects to have mLock locked 661 status_t getConcurrentCameraIdsInternalLocked( 662 sp<hardware::camera::provider::V2_6::ICameraProvider> &interface2_6); 663 }; 664 665 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held 666 // and the calling code doesn't mutate the list of providers or their lists of devices. 667 // Finds the first device of the given ID that falls within the requested version range 668 // minVersion <= deviceVersion < maxVersion 669 // No guarantees on the order of traversal 670 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id, 671 hardware::hidl_version minVersion = hardware::hidl_version{0,0}, 672 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const; 673 674 status_t addProviderLocked(const std::string& newProvider, bool preexisting = false); 675 676 status_t tryToInitializeProviderLocked(const std::string& providerName, 677 const sp<ProviderInfo>& providerInfo); 678 679 bool isLogicalCameraLocked(const std::string& id, std::vector<std::string>* physicalCameraIds); 680 681 status_t removeProvider(const std::string& provider); 682 sp<StatusListener> getStatusListener() const; 683 684 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const; 685 686 size_t mProviderInstanceId = 0; 687 std::vector<sp<ProviderInfo>> mProviders; 688 689 void addProviderToMap( 690 const std::string &cameraId, 691 sp<hardware::camera::provider::V2_4::ICameraProvider> provider, 692 bool isTorchUsage); 693 void removeCameraIdFromMap( 694 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> &map, 695 const std::string &cameraId); 696 697 static const char* deviceStatusToString( 698 const hardware::camera::common::V1_0::CameraDeviceStatus&); 699 static const char* torchStatusToString( 700 const hardware::camera::common::V1_0::TorchModeStatus&); 701 702 status_t getCameraCharacteristicsLocked(const std::string &id, bool overrideForPerfClass, 703 CameraMetadata* characteristics) const; 704 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const; 705 706 status_t getSystemCameraKindLocked(const std::string& id, SystemCameraKind *kind) const; 707 std::pair<bool, ProviderInfo::DeviceInfo *> isHiddenPhysicalCameraInternal(const std::string& cameraId) const; 708 709 void collectDeviceIdsLocked(const std::vector<std::string> deviceIds, 710 std::vector<std::string>& normalDeviceIds, 711 std::vector<std::string>& systemCameraDeviceIds) const; 712 713 status_t convertToHALStreamCombinationAndCameraIdsLocked( 714 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration> 715 &cameraIdsAndSessionConfigs, 716 const std::set<std::string>& perfClassPrimaryCameraIds, 717 int targetSdkVersion, 718 hardware::hidl_vec<hardware::camera::provider::V2_7::CameraIdAndStreamCombination> 719 *halCameraIdsAndStreamCombinations, 720 bool *earlyExit); 721 }; 722 723 } // namespace android 724 725 #endif 726