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