blob: 954c0d997f5be8359f97ab9b4dfce806364d4a02 [file] [log] [blame]
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001/*
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>
Peter Kalauskasa29c1352018-10-10 12:05:42 -070021#include <unordered_map>
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080022#include <unordered_set>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080023#include <string>
24#include <mutex>
25
26#include <camera/CameraParameters2.h>
27#include <camera/CameraMetadata.h>
28#include <camera/CameraBase.h>
29#include <utils/Errors.h>
30#include <android/hardware/camera/common/1.0/types.h>
Eino-Ville Talvala63f36112018-12-06 14:57:03 -080031#include <android/hardware/camera/provider/2.5/ICameraProvider.h>
Emilian Peev35ae8262018-11-08 13:11:32 +000032#include <android/hardware/camera/device/3.4/ICameraDeviceSession.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080033#include <android/hidl/manager/1.0/IServiceNotification.h>
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080034#include <camera/VendorTagDescriptor.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080035
36namespace android {
37
38/**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080039 * The vendor tag descriptor class that takes HIDL vendor tag information as
40 * input. Not part of VendorTagDescriptor class because that class is used
41 * in AIDL generated sources which don't have access to HIDL headers.
42 */
43class HidlVendorTagDescriptor : public VendorTagDescriptor {
44public:
45 /**
46 * Create a VendorTagDescriptor object from the HIDL VendorTagSection
47 * vector.
48 *
49 * Returns OK on success, or a negative error code.
50 */
51 static status_t createDescriptorFromHidl(
52 const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts,
53 /*out*/
54 sp<VendorTagDescriptor>& descriptor);
55};
56
57/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080058 * A manager for all camera providers available on an Android device.
59 *
60 * Responsible for enumerating providers and the individual camera devices
61 * they export, both at startup and as providers and devices are added/removed.
62 *
63 * Provides methods for requesting information about individual devices and for
64 * opening them for active use.
65 *
66 */
67class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification {
68public:
69
70 ~CameraProviderManager();
71
72 // Tiny proxy for the static methods in a HIDL interface that communicate with the hardware
73 // service manager, to be replacable in unit tests with a fake.
74 struct ServiceInteractionProxy {
75 virtual bool registerForNotifications(
76 const std::string &serviceName,
77 const sp<hidl::manager::V1_0::IServiceNotification>
78 &notification) = 0;
79 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
80 const std::string &serviceName) = 0;
Yin-Chia Yeh26c79972019-06-25 10:53:03 -070081 virtual hardware::hidl_vec<hardware::hidl_string> listServices() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080082 virtual ~ServiceInteractionProxy() {}
83 };
84
85 // Standard use case - call into the normal generated static methods which invoke
86 // the real hardware service manager
87 struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
88 virtual bool registerForNotifications(
89 const std::string &serviceName,
90 const sp<hidl::manager::V1_0::IServiceNotification>
91 &notification) override {
92 return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
93 serviceName, notification);
94 }
95 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
96 const std::string &serviceName) override {
97 return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
98 }
Yin-Chia Yeh26c79972019-06-25 10:53:03 -070099
100 virtual hardware::hidl_vec<hardware::hidl_string> listServices() override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800101 };
102
103 /**
104 * Listener interface for device/torch status changes
105 */
106 struct StatusListener : virtual public RefBase {
107 ~StatusListener() {}
108
109 virtual void onDeviceStatusChanged(const String8 &cameraId,
110 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
111 virtual void onTorchStatusChanged(const String8 &cameraId,
112 hardware::camera::common::V1_0::TorchModeStatus newStatus) = 0;
Emilian Peevaee727d2017-05-04 16:35:48 +0100113 virtual void onNewProviderRegistered() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800114 };
115
116 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700117 * Represents the mode a camera device is currently in
118 */
119 enum class DeviceMode {
120 TORCH,
121 CAMERA
122 };
123
124 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800125 * Initialize the manager and give it a status listener; optionally accepts a service
126 * interaction proxy.
127 *
128 * The default proxy communicates via the hardware service manager; alternate proxies can be
129 * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
130 */
131 status_t initialize(wp<StatusListener> listener,
132 ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);
133
134 /**
135 * Retrieve the total number of available cameras. This value may change dynamically as cameras
136 * are added or removed.
137 */
138 int getCameraCount() const;
139
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800140 std::vector<std::string> getCameraDeviceIds() const;
141
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800142 /**
Emilian Peevf53f66e2017-04-11 14:29:43 +0100143 * Retrieve the number of API1 compatible cameras; these are internal and
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800144 * backwards-compatible. This is the set of cameras that will be
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800145 * accessible via the old camera API.
146 * The return value may change dynamically due to external camera hotplug.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800147 */
Emilian Peevf53f66e2017-04-11 14:29:43 +0100148 std::vector<std::string> getAPI1CompatibleCameraDeviceIds() const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700149
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800150 /**
151 * Return true if a device with a given ID and major version exists
152 */
153 bool isValidDevice(const std::string &id, uint16_t majorVersion) const;
154
155 /**
156 * Return true if a device with a given ID has a flash unit. Returns false
157 * for devices that are unknown.
158 */
159 bool hasFlashUnit(const std::string &id) const;
160
161 /**
162 * Return the resource cost of this camera device
163 */
164 status_t getResourceCost(const std::string &id,
165 hardware::camera::common::V1_0::CameraResourceCost* cost) const;
166
167 /**
168 * Return the old camera API camera info
169 */
170 status_t getCameraInfo(const std::string &id,
171 hardware::CameraInfo* info) const;
172
173 /**
174 * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does
175 * not have a v3 or newer HAL version.
176 */
177 status_t getCameraCharacteristics(const std::string &id,
178 CameraMetadata* characteristics) const;
179
180 /**
Emilian Peev35ae8262018-11-08 13:11:32 +0000181 * Check for device support of specific stream combination.
182 */
183 status_t isSessionConfigurationSupported(const std::string& id,
184 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
185 bool *status /*out*/) const;
186
187 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800188 * Return the highest supported device interface version for this ID
189 */
190 status_t getHighestSupportedVersion(const std::string &id,
191 hardware::hidl_version *v);
192
193 /**
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700194 * Check if a given camera device support setTorchMode API.
195 */
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700196 bool supportSetTorchMode(const std::string &id) const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700197
198 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800199 * Turn on or off the flashlight on a given camera device.
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700200 * May fail if the device does not support this API, is in active use, or if the device
201 * doesn't exist, etc.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800202 */
203 status_t setTorchMode(const std::string &id, bool enabled);
204
205 /**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800206 * Setup vendor tags for all registered providers
207 */
208 status_t setUpVendorTags();
209
210 /**
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800211 * Inform registered providers about a device state change, such as folding or unfolding
212 */
213 status_t notifyDeviceStateChange(
214 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> newState);
215
216 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800217 * Open an active session to a camera device.
218 *
219 * This fully powers on the camera device hardware, and returns a handle to a
220 * session to be used for hardware configuration and operation.
221 */
222 status_t openSession(const std::string &id,
223 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
224 /*out*/
225 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session);
226
227 status_t openSession(const std::string &id,
228 const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback,
229 /*out*/
230 sp<hardware::camera::device::V1_0::ICameraDevice> *session);
231
232 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700233 * Save the ICameraProvider while it is being used by a camera or torch client
234 */
235 void saveRef(DeviceMode usageType, const std::string &cameraId,
236 sp<hardware::camera::provider::V2_4::ICameraProvider> provider);
237
238 /**
239 * Notify that the camera or torch is no longer being used by a camera client
240 */
241 void removeRef(DeviceMode usageType, const std::string &cameraId);
242
243 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800244 * IServiceNotification::onRegistration
245 * Invoked by the hardware service manager when a new camera provider is registered
246 */
247 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
248 const hardware::hidl_string& name,
249 bool preexisting) override;
250
251 /**
252 * Dump out information about available providers and devices
253 */
254 status_t dump(int fd, const Vector<String16>& args);
255
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800256 /**
257 * Conversion methods between HAL Status and status_t and strings
258 */
259 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
260 static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
261
Emilian Peev71c73a22017-03-21 16:35:51 +0000262 /*
263 * Return provider type for a specific device.
264 */
265 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id,
266 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
267 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
268
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700269 /*
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700270 * Check if a camera is a logical camera. And if yes, return
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700271 * the physical camera ids.
272 */
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700273 bool isLogicalCamera(const std::string& id, std::vector<std::string>* physicalCameraIds);
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700274
Jayant Chowdharycf935402019-09-18 20:14:02 -0700275 bool isPublicallyHiddenSecureCamera(const std::string& id) const;
Jayant Chowdhary31706c82019-10-21 14:51:56 -0700276
Jayant Chowdharyfd39db82019-10-07 15:26:41 -0700277 bool isHiddenPhysicalCamera(const std::string& cameraId) const;
Emilian Peev538c90e2018-12-17 18:03:19 +0000278
279 static const float kDepthARTolerance;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800280private:
281 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
282 mutable std::mutex mInterfaceMutex;
283
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800284 // the status listener update callbacks will lock mStatusMutex
285 mutable std::mutex mStatusListenerMutex;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800286 wp<StatusListener> mListener;
287 ServiceInteractionProxy* mServiceProxy;
288
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800289 // Current overall Android device physical status
290 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
291
Shuzhen Wang6ba8eb22018-07-08 13:10:44 -0700292 // mProviderLifecycleLock is locked during onRegistration and removeProvider
293 mutable std::mutex mProviderLifecycleLock;
294
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800295 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
296
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700297 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
298 // ICameraProvider alive while it is in use by the camera with the given ID for camera
299 // capabilities
300 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
301 mCameraProviderByCameraId;
302
303 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
304 // ICameraProvider alive while it is in use by the camera with the given ID for torch
305 // capabilities
306 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
307 mTorchProviderByCameraId;
308
309 // Lock for accessing mCameraProviderByCameraId and mTorchProviderByCameraId
310 std::mutex mProviderInterfaceMapLock;
311
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700312 struct ProviderInfo :
313 virtual public hardware::camera::provider::V2_4::ICameraProviderCallback,
314 virtual public hardware::hidl_death_recipient
315 {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800316 const std::string mProviderName;
Emilian Peev71c73a22017-03-21 16:35:51 +0000317 const metadata_vendor_id_t mProviderTagid;
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800318 int mMinorVersion;
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800319 sp<VendorTagDescriptor> mVendorTagDescriptor;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700320 bool mSetTorchModeSupported;
321 bool mIsRemote;
322
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800323 // Current overall Android device physical status
324 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
325
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700326 // This pointer is used to keep a reference to the ICameraProvider that was last accessed.
327 wp<hardware::camera::provider::V2_4::ICameraProvider> mActiveInterface;
328
329 sp<hardware::camera::provider::V2_4::ICameraProvider> mSavedInterface;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800330
331 ProviderInfo(const std::string &providerName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800332 CameraProviderManager *manager);
333 ~ProviderInfo();
334
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800335 status_t initialize(sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
336 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
337 currentDeviceState);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700338
339 const sp<hardware::camera::provider::V2_4::ICameraProvider> startProviderInterface();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800340
341 const std::string& getType() const;
342
343 status_t addDevice(const std::string& name,
344 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
345 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
346 /*out*/ std::string *parsedId = nullptr);
347
348 status_t dump(int fd, const Vector<String16>& args) const;
349
350 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
351 virtual hardware::Return<void> cameraDeviceStatusChange(
352 const hardware::hidl_string& cameraDeviceName,
353 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
354 virtual hardware::Return<void> torchModeStatusChange(
355 const hardware::hidl_string& cameraDeviceName,
356 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
357
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700358 // hidl_death_recipient interface - this locks the parent mInterfaceMutex
359 virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
360
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800361 /**
362 * Setup vendor tags for this provider
363 */
364 status_t setUpVendorTags();
365
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800366 /**
367 * Notify provider about top-level device physical state changes
368 */
369 status_t notifyDeviceStateChange(
370 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
371 newDeviceState);
372
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800373 // Basic device information, common to all camera devices
374 struct DeviceInfo {
375 const std::string mName; // Full instance name
376 const std::string mId; // ID section of full name
377 const hardware::hidl_version mVersion;
Emilian Peev71c73a22017-03-21 16:35:51 +0000378 const metadata_vendor_id_t mProviderTagid;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700379 bool mIsLogicalCamera;
380 std::vector<std::string> mPhysicalIds;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700381 hardware::CameraInfo mInfo;
382 sp<IBase> mSavedInterface;
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -0800383 bool mIsPublicallyHiddenSecureCamera = false;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800384
385 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
386
387 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
388
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700389 sp<ProviderInfo> mParentProvider;
390
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800391 bool hasFlashUnit() const { return mHasFlashUnit; }
392 virtual status_t setTorchMode(bool enabled) = 0;
393 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100394 virtual bool isAPI1Compatible() const = 0;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700395 virtual status_t dumpState(int fd) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800396 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
397 (void) characteristics;
398 return INVALID_OPERATION;
399 }
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700400 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
401 CameraMetadata *characteristics) const {
402 (void) physicalCameraId;
403 (void) characteristics;
404 return INVALID_OPERATION;
405 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800406
Emilian Peev35ae8262018-11-08 13:11:32 +0000407 virtual status_t isSessionConfigurationSupported(
408 const hardware::camera::device::V3_4::StreamConfiguration &/*configuration*/,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700409 bool * /*status*/) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000410 return INVALID_OPERATION;
411 }
412
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700413 template<class InterfaceT>
414 sp<InterfaceT> startDeviceInterface();
415
Emilian Peev71c73a22017-03-21 16:35:51 +0000416 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
417 const std::string &id, const hardware::hidl_version& version,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700418 const std::vector<std::string>& publicCameraIds,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700419 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
420 sp<ProviderInfo> parentProvider) :
Emilian Peev71c73a22017-03-21 16:35:51 +0000421 mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700422 mIsLogicalCamera(false), mResourceCost(resourceCost),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800423 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700424 mParentProvider(parentProvider), mHasFlashUnit(false),
425 mPublicCameraIds(publicCameraIds) {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800426 virtual ~DeviceInfo();
427 protected:
428 bool mHasFlashUnit;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700429 const std::vector<std::string>& mPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800430
431 template<class InterfaceT>
432 static status_t setTorchMode(InterfaceT& interface, bool enabled);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700433
434 template<class InterfaceT>
435 status_t setTorchModeForDevice(bool enabled) {
436 // Don't save the ICameraProvider interface here because we assume that this was
437 // called from CameraProviderManager::setTorchMode(), which does save it.
438 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
439 return DeviceInfo::setTorchMode(interface, enabled);
440 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800441 };
442 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800443 std::unordered_set<std::string> mUniqueCameraIds;
Yin-Chia Yehe8e9e192017-03-16 15:23:51 -0700444 int mUniqueDeviceCount;
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700445 std::vector<std::string> mUniqueAPI1CompatibleCameraIds;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700446 // The initial public camera IDs published by the camera provider.
447 // Currently logical multi-camera is not supported for hot-plug camera.
448 // And we use this list to keep track of initial public camera IDs
449 // advertised by the provider, and to distinguish against "hidden"
450 // physical camera IDs.
451 std::vector<std::string> mProviderPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800452
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800453 // HALv1-specific camera fields, including the actual device interface
454 struct DeviceInfo1 : public DeviceInfo {
455 typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800456
457 virtual status_t setTorchMode(bool enabled) override;
458 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100459 //In case of Device1Info assume that we are always API1 compatible
460 virtual bool isAPI1Compatible() const override { return true; }
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700461 virtual status_t dumpState(int fd) override;
Emilian Peev71c73a22017-03-21 16:35:51 +0000462 DeviceInfo1(const std::string& name, const metadata_vendor_id_t tagId,
463 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800464 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700465 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700466 const std::vector<std::string>& publicCameraIds,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800467 sp<InterfaceT> interface);
468 virtual ~DeviceInfo1();
469 private:
470 CameraParameters2 mDefaultParameters;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700471 status_t cacheCameraInfo(sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800472 };
473
474 // HALv3-specific camera fields, including the actual device interface
475 struct DeviceInfo3 : public DeviceInfo {
476 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800477
478 virtual status_t setTorchMode(bool enabled) override;
479 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100480 virtual bool isAPI1Compatible() const override;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700481 virtual status_t dumpState(int fd) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800482 virtual status_t getCameraCharacteristics(
483 CameraMetadata *characteristics) const override;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700484 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
485 CameraMetadata *characteristics) const override;
Emilian Peev35ae8262018-11-08 13:11:32 +0000486 virtual status_t isSessionConfigurationSupported(
487 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
488 bool *status /*out*/)
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700489 override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800490
Emilian Peev71c73a22017-03-21 16:35:51 +0000491 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
492 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800493 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700494 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700495 const std::vector<std::string>& publicCameraIds, sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800496 virtual ~DeviceInfo3();
497 private:
498 CameraMetadata mCameraCharacteristics;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700499 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700500 void queryPhysicalCameraIds();
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -0800501 bool isPublicallyHiddenSecureCamera();
Shuzhen Wang268a1362018-10-16 16:32:59 -0700502 status_t fixupMonochromeTags();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000503 status_t addDynamicDepthTags();
504 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag,
505 android_pixel_format_t format,
506 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/);
507 void getSupportedDurations( const CameraMetadata& ch, uint32_t tag,
508 android_pixel_format_t format,
509 const std::vector<std::tuple<size_t, size_t>>& sizes,
510 std::vector<int64_t> *durations/*out*/);
511 void getSupportedDynamicDepthDurations(const std::vector<int64_t>& depthDurations,
512 const std::vector<int64_t>& blobDurations,
513 std::vector<int64_t> *dynamicDepthDurations /*out*/);
Emilian Peevcbf174b2019-01-25 14:38:59 -0800514 static bool isDepthPhotoLibraryPresent();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000515 static void getSupportedDynamicDepthSizes(
516 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
517 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
518 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
519 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/);
Shuzhen Wang268a1362018-10-16 16:32:59 -0700520 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys,
521 uint32_t keyTag);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800522 status_t fillHeicStreamCombinations(std::vector<int32_t>* outputs,
523 std::vector<int64_t>* durations,
524 std::vector<int64_t>* stallDurations,
525 const camera_metadata_entry& halStreamConfigs,
526 const camera_metadata_entry& halStreamDurations);
527 status_t deriveHeicTags();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800528 };
529
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800530 private:
531 std::string mType;
532 uint32_t mId;
533
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700534 std::mutex mLock;
535
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800536 CameraProviderManager *mManager;
537
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800538 bool mInitialized = false;
539
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800540 // Templated method to instantiate the right kind of DeviceInfo and call the
541 // right CameraProvider getCameraDeviceInterface_* method.
542 template<class DeviceInfoT>
543 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
Emilian Peev71c73a22017-03-21 16:35:51 +0000544 const metadata_vendor_id_t tagId, const std::string &id,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700545 uint16_t minorVersion);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800546
547 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
548 template<class InterfaceT>
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700549 sp<InterfaceT> startDeviceInterface(const std::string &name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800550
551 // Parse provider instance name for type and id
552 static status_t parseProviderName(const std::string& name,
553 std::string *type, uint32_t *id);
554
555 // Parse device instance name for device version, type, and id.
556 static status_t parseDeviceName(const std::string& name,
557 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
Emilian Peev71c73a22017-03-21 16:35:51 +0000558
559 // Generate vendor tag id
560 static metadata_vendor_id_t generateVendorTagId(const std::string &name);
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100561
562 void removeDevice(std::string id);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800563 };
564
565 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
566 // and the calling code doesn't mutate the list of providers or their lists of devices.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800567 // Finds the first device of the given ID that falls within the requested version range
568 // minVersion <= deviceVersion < maxVersion
569 // No guarantees on the order of traversal
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800570 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800571 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
572 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800573
Yin-Chia Yeh26c79972019-06-25 10:53:03 -0700574 status_t addProviderLocked(const std::string& newProvider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700575
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800576 status_t removeProvider(const std::string& provider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700577 sp<StatusListener> getStatusListener() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800578
579 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
580
581 std::vector<sp<ProviderInfo>> mProviders;
582
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700583 void addProviderToMap(
584 const std::string &cameraId,
585 sp<hardware::camera::provider::V2_4::ICameraProvider> provider,
586 bool isTorchUsage);
587 void removeCameraIdFromMap(
588 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> &map,
589 const std::string &cameraId);
590
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800591 static const char* deviceStatusToString(
592 const hardware::camera::common::V1_0::CameraDeviceStatus&);
593 static const char* torchStatusToString(
594 const hardware::camera::common::V1_0::TorchModeStatus&);
595
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700596 status_t getCameraCharacteristicsLocked(const std::string &id,
597 CameraMetadata* characteristics) const;
Jayant Chowdharycf935402019-09-18 20:14:02 -0700598
599 bool isPublicallyHiddenSecureCameraLocked(const std::string& id) const;
600
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700601 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const;
Jayant Chowdharyfd39db82019-10-07 15:26:41 -0700602
603 bool isPublicallyHiddenSecureCameraLocked(const std::string& id);
604
605 std::pair<bool, CameraProviderManager::ProviderInfo::DeviceInfo *>
606 isHiddenPhysicalCameraInternal(const std::string& cameraId) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800607};
608
609} // namespace android
610
611#endif