• 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 <set>
22 #include <string>
23 #include <mutex>
24 
25 #include <camera/CameraParameters2.h>
26 #include <camera/CameraMetadata.h>
27 #include <camera/CameraBase.h>
28 #include <utils/Errors.h>
29 #include <android/hardware/camera/common/1.0/types.h>
30 #include <android/hardware/camera/provider/2.4/ICameraProvider.h>
31 //#include <android/hardware/camera/provider/2.4/ICameraProviderCallbacks.h>
32 #include <android/hidl/manager/1.0/IServiceNotification.h>
33 #include <camera/VendorTagDescriptor.h>
34 
35 namespace android {
36 
37 /**
38  * The vendor tag descriptor class that takes HIDL vendor tag information as
39  * input. Not part of VendorTagDescriptor class because that class is used
40  * in AIDL generated sources which don't have access to HIDL headers.
41  */
42 class HidlVendorTagDescriptor : public VendorTagDescriptor {
43 public:
44     /**
45      * Create a VendorTagDescriptor object from the HIDL VendorTagSection
46      * vector.
47      *
48      * Returns OK on success, or a negative error code.
49      */
50     static status_t createDescriptorFromHidl(
51             const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts,
52             /*out*/
53             sp<VendorTagDescriptor>& descriptor);
54 };
55 
56 /**
57  * A manager for all camera providers available on an Android device.
58  *
59  * Responsible for enumerating providers and the individual camera devices
60  * they export, both at startup and as providers and devices are added/removed.
61  *
62  * Provides methods for requesting information about individual devices and for
63  * opening them for active use.
64  *
65  */
66 class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification {
67 public:
68 
69     ~CameraProviderManager();
70 
71     // Tiny proxy for the static methods in a HIDL interface that communicate with the hardware
72     // service manager, to be replacable in unit tests with a fake.
73     struct ServiceInteractionProxy {
74         virtual bool registerForNotifications(
75                 const std::string &serviceName,
76                 const sp<hidl::manager::V1_0::IServiceNotification>
77                 &notification) = 0;
78         virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
79                 const std::string &serviceName) = 0;
~ServiceInteractionProxyServiceInteractionProxy80         virtual ~ServiceInteractionProxy() {}
81     };
82 
83     // Standard use case - call into the normal generated static methods which invoke
84     // the real hardware service manager
85     struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
registerForNotificationsHardwareServiceInteractionProxy86         virtual bool registerForNotifications(
87                 const std::string &serviceName,
88                 const sp<hidl::manager::V1_0::IServiceNotification>
89                 &notification) override {
90             return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
91                     serviceName, notification);
92         }
getServiceHardwareServiceInteractionProxy93         virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
94                 const std::string &serviceName) override {
95             return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
96         }
97     };
98 
99     /**
100      * Listener interface for device/torch status changes
101      */
102     struct StatusListener : virtual public RefBase {
~StatusListenerStatusListener103         ~StatusListener() {}
104 
105         virtual void onDeviceStatusChanged(const String8 &cameraId,
106                 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
107         virtual void onTorchStatusChanged(const String8 &cameraId,
108                 hardware::camera::common::V1_0::TorchModeStatus newStatus) = 0;
109         virtual void onNewProviderRegistered() = 0;
110     };
111 
112     /**
113      * Initialize the manager and give it a status listener; optionally accepts a service
114      * interaction proxy.
115      *
116      * The default proxy communicates via the hardware service manager; alternate proxies can be
117      * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
118      */
119     status_t initialize(wp<StatusListener> listener,
120             ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);
121 
122     /**
123      * Retrieve the total number of available cameras. This value may change dynamically as cameras
124      * are added or removed.
125      */
126     int getCameraCount() const;
127 
128     /**
129      * Retrieve the number of API1 compatible cameras; these are internal and
130      * backwards-compatible. This is the set of cameras that will be
131      * accessible via the old camera API, with IDs in range of
132      * [0, getAPI1CompatibleCameraCount()-1]. This value is not expected to change dynamically.
133      */
134     int getAPI1CompatibleCameraCount() const;
135 
136     std::vector<std::string> getCameraDeviceIds() const;
137 
138     std::vector<std::string> getAPI1CompatibleCameraDeviceIds() const;
139 
140     /**
141      * Return true if a device with a given ID and major version exists
142      */
143     bool isValidDevice(const std::string &id, uint16_t majorVersion) const;
144 
145     /**
146      * Return true if a device with a given ID has a flash unit. Returns false
147      * for devices that are unknown.
148      */
149     bool hasFlashUnit(const std::string &id) const;
150 
151     /**
152      * Return the resource cost of this camera device
153      */
154     status_t getResourceCost(const std::string &id,
155             hardware::camera::common::V1_0::CameraResourceCost* cost) const;
156 
157     /**
158      * Return the old camera API camera info
159      */
160     status_t getCameraInfo(const std::string &id,
161             hardware::CameraInfo* info) const;
162 
163     /**
164      * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does
165      * not have a v3 or newer HAL version.
166      */
167     status_t getCameraCharacteristics(const std::string &id,
168             CameraMetadata* characteristics) const;
169 
170     /**
171      * Return the highest supported device interface version for this ID
172      */
173     status_t getHighestSupportedVersion(const std::string &id,
174             hardware::hidl_version *v);
175 
176     /**
177      * Check if a given camera device support setTorchMode API.
178      */
179     bool supportSetTorchMode(const std::string &id);
180 
181     /**
182      * Turn on or off the flashlight on a given camera device.
183      * May fail if the device does not support this API, is in active use, or if the device
184      * doesn't exist, etc.
185      */
186     status_t setTorchMode(const std::string &id, bool enabled);
187 
188     /**
189      * Setup vendor tags for all registered providers
190      */
191     status_t setUpVendorTags();
192 
193     /**
194      * Open an active session to a camera device.
195      *
196      * This fully powers on the camera device hardware, and returns a handle to a
197      * session to be used for hardware configuration and operation.
198      */
199     status_t openSession(const std::string &id,
200             const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
201             /*out*/
202             sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session);
203 
204     status_t openSession(const std::string &id,
205             const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback,
206             /*out*/
207             sp<hardware::camera::device::V1_0::ICameraDevice> *session);
208 
209     /**
210      * IServiceNotification::onRegistration
211      * Invoked by the hardware service manager when a new camera provider is registered
212      */
213     virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
214             const hardware::hidl_string& name,
215             bool preexisting) override;
216 
217     /**
218      * Dump out information about available providers and devices
219      */
220     status_t dump(int fd, const Vector<String16>& args);
221 
222     /**
223      * Conversion methods between HAL Status and status_t and strings
224      */
225     static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
226     static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
227 
228     /*
229      * Return provider type for a specific device.
230      */
231     metadata_vendor_id_t getProviderTagIdLocked(const std::string& id,
232             hardware::hidl_version minVersion = hardware::hidl_version{0,0},
233             hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
234 
235 private:
236     // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
237     mutable std::mutex mInterfaceMutex;
238 
239     // the status listener update callbacks will lock mStatusMutex
240     mutable std::mutex mStatusListenerMutex;
241     wp<StatusListener> mListener;
242     ServiceInteractionProxy* mServiceProxy;
243 
244     static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
245 
246     struct ProviderInfo :
247             virtual public hardware::camera::provider::V2_4::ICameraProviderCallback,
248             virtual public hardware::hidl_death_recipient
249     {
250         const std::string mProviderName;
251         const sp<hardware::camera::provider::V2_4::ICameraProvider> mInterface;
252         const metadata_vendor_id_t mProviderTagid;
253 
254         ProviderInfo(const std::string &providerName,
255                 sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
256                 CameraProviderManager *manager);
257         ~ProviderInfo();
258 
259         status_t initialize();
260 
261         const std::string& getType() const;
262 
263         status_t addDevice(const std::string& name,
264                 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
265                 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
266                 /*out*/ std::string *parsedId = nullptr);
267 
268         status_t dump(int fd, const Vector<String16>& args) const;
269 
270         // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
271         virtual hardware::Return<void> cameraDeviceStatusChange(
272                 const hardware::hidl_string& cameraDeviceName,
273                 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
274         virtual hardware::Return<void> torchModeStatusChange(
275                 const hardware::hidl_string& cameraDeviceName,
276                 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
277 
278         // hidl_death_recipient interface - this locks the parent mInterfaceMutex
279         virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
280 
281         // Basic device information, common to all camera devices
282         struct DeviceInfo {
283             const std::string mName;  // Full instance name
284             const std::string mId;    // ID section of full name
285             const hardware::hidl_version mVersion;
286             const metadata_vendor_id_t mProviderTagid;
287 
288             const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
289 
290             hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
291 
hasFlashUnitProviderInfo::DeviceInfo292             bool hasFlashUnit() const { return mHasFlashUnit; }
293             virtual status_t setTorchMode(bool enabled) = 0;
294             virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
295             virtual bool isAPI1Compatible() const = 0;
getCameraCharacteristicsProviderInfo::DeviceInfo296             virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
297                 (void) characteristics;
298                 return INVALID_OPERATION;
299             }
300 
DeviceInfoProviderInfo::DeviceInfo301             DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
302                     const std::string &id, const hardware::hidl_version& version,
303                     const hardware::camera::common::V1_0::CameraResourceCost& resourceCost) :
304                     mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
305                     mResourceCost(resourceCost),
306                     mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
307                     mHasFlashUnit(false) {}
308             virtual ~DeviceInfo();
309         protected:
310             bool mHasFlashUnit;
311 
312             template<class InterfaceT>
313             static status_t setTorchMode(InterfaceT& interface, bool enabled);
314         };
315         std::vector<std::unique_ptr<DeviceInfo>> mDevices;
316         std::set<std::string> mUniqueCameraIds;
317         int mUniqueDeviceCount;
318         std::set<std::string> mUniqueAPI1CompatibleCameraIds;
319 
320         // HALv1-specific camera fields, including the actual device interface
321         struct DeviceInfo1 : public DeviceInfo {
322             typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT;
323             const sp<InterfaceT> mInterface;
324 
325             virtual status_t setTorchMode(bool enabled) override;
326             virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
327             //In case of Device1Info assume that we are always API1 compatible
isAPI1CompatibleProviderInfo::DeviceInfo1328             virtual bool isAPI1Compatible() const override { return true; }
329             DeviceInfo1(const std::string& name, const metadata_vendor_id_t tagId,
330                     const std::string &id, uint16_t minorVersion,
331                     const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
332                     sp<InterfaceT> interface);
333             virtual ~DeviceInfo1();
334         private:
335             CameraParameters2 mDefaultParameters;
336         };
337 
338         // HALv3-specific camera fields, including the actual device interface
339         struct DeviceInfo3 : public DeviceInfo {
340             typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
341             const sp<InterfaceT> mInterface;
342 
343             virtual status_t setTorchMode(bool enabled) override;
344             virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
345             virtual bool isAPI1Compatible() const override;
346             virtual status_t getCameraCharacteristics(
347                     CameraMetadata *characteristics) const override;
348 
349             DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
350                     const std::string &id, uint16_t minorVersion,
351                     const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
352                     sp<InterfaceT> interface);
353             virtual ~DeviceInfo3();
354         private:
355             CameraMetadata mCameraCharacteristics;
356         };
357 
358     private:
359         std::string mType;
360         uint32_t mId;
361 
362         std::mutex mLock;
363 
364         CameraProviderManager *mManager;
365 
366         // Templated method to instantiate the right kind of DeviceInfo and call the
367         // right CameraProvider getCameraDeviceInterface_* method.
368         template<class DeviceInfoT>
369         std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
370                 const metadata_vendor_id_t tagId, const std::string &id,
371                 uint16_t minorVersion) const;
372 
373         // Helper for initializeDeviceInfo to use the right CameraProvider get method.
374         template<class InterfaceT>
375         sp<InterfaceT> getDeviceInterface(const std::string &name) const;
376 
377         // Parse provider instance name for type and id
378         static status_t parseProviderName(const std::string& name,
379                 std::string *type, uint32_t *id);
380 
381         // Parse device instance name for device version, type, and id.
382         static status_t parseDeviceName(const std::string& name,
383                 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
384 
385         // Generate vendor tag id
386         static metadata_vendor_id_t generateVendorTagId(const std::string &name);
387     };
388 
389     // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
390     // and the calling code doesn't mutate the list of providers or their lists of devices.
391     // Finds the first device of the given ID that falls within the requested version range
392     //   minVersion <= deviceVersion < maxVersion
393     // No guarantees on the order of traversal
394     ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
395             hardware::hidl_version minVersion = hardware::hidl_version{0,0},
396             hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
397 
398     status_t addProviderLocked(const std::string& newProvider, bool expected = true);
399 
400     status_t removeProvider(const std::string& provider);
401     sp<StatusListener> getStatusListener() const;
402 
403     bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
404 
405     std::vector<sp<ProviderInfo>> mProviders;
406 
407     static const char* deviceStatusToString(
408         const hardware::camera::common::V1_0::CameraDeviceStatus&);
409     static const char* torchStatusToString(
410         const hardware::camera::common::V1_0::TorchModeStatus&);
411 
412 };
413 
414 } // namespace android
415 
416 #endif
417