blob: f4cf6674f0eb1506c06bcf67ef795002cddbeecb [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
Jayant Chowdhary5216b212019-07-17 09:26:23 -070057enum SystemCameraKind {
58 /**
59 * These camera devices are visible to all apps and system components alike
60 */
61 PUBLIC = 0,
62
63 /**
64 * These camera devices are visible only to processes having the
65 * android.permission.SYSTEM_CAMERA permission. They are not exposed to 3P
66 * apps.
67 */
68 SYSTEM_ONLY_CAMERA,
69
70 /**
71 * These camera devices are visible only to HAL clients (that try to connect
72 * on a hwbinder thread).
73 */
74 HIDDEN_SECURE_CAMERA
75};
76
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080077/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080078 * A manager for all camera providers available on an Android device.
79 *
80 * Responsible for enumerating providers and the individual camera devices
81 * they export, both at startup and as providers and devices are added/removed.
82 *
83 * Provides methods for requesting information about individual devices and for
84 * opening them for active use.
85 *
86 */
87class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification {
88public:
89
90 ~CameraProviderManager();
91
92 // Tiny proxy for the static methods in a HIDL interface that communicate with the hardware
93 // service manager, to be replacable in unit tests with a fake.
94 struct ServiceInteractionProxy {
95 virtual bool registerForNotifications(
96 const std::string &serviceName,
97 const sp<hidl::manager::V1_0::IServiceNotification>
98 &notification) = 0;
99 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
100 const std::string &serviceName) = 0;
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700101 virtual hardware::hidl_vec<hardware::hidl_string> listServices() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800102 virtual ~ServiceInteractionProxy() {}
103 };
104
105 // Standard use case - call into the normal generated static methods which invoke
106 // the real hardware service manager
107 struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
108 virtual bool registerForNotifications(
109 const std::string &serviceName,
110 const sp<hidl::manager::V1_0::IServiceNotification>
111 &notification) override {
112 return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
113 serviceName, notification);
114 }
115 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
116 const std::string &serviceName) override {
117 return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
118 }
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700119
120 virtual hardware::hidl_vec<hardware::hidl_string> listServices() override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800121 };
122
123 /**
124 * Listener interface for device/torch status changes
125 */
126 struct StatusListener : virtual public RefBase {
127 ~StatusListener() {}
128
129 virtual void onDeviceStatusChanged(const String8 &cameraId,
130 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
131 virtual void onTorchStatusChanged(const String8 &cameraId,
132 hardware::camera::common::V1_0::TorchModeStatus newStatus) = 0;
Emilian Peevaee727d2017-05-04 16:35:48 +0100133 virtual void onNewProviderRegistered() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800134 };
135
136 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700137 * Represents the mode a camera device is currently in
138 */
139 enum class DeviceMode {
140 TORCH,
141 CAMERA
142 };
143
144 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800145 * Initialize the manager and give it a status listener; optionally accepts a service
146 * interaction proxy.
147 *
148 * The default proxy communicates via the hardware service manager; alternate proxies can be
149 * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
150 */
151 status_t initialize(wp<StatusListener> listener,
152 ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);
153
154 /**
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700155 * Retrieve the total number of available cameras.
156 * This value may change dynamically as cameras are added or removed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800157 */
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700158 std::pair<int, int> getCameraCount() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800159
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800160 std::vector<std::string> getCameraDeviceIds() const;
161
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800162 /**
Emilian Peevf53f66e2017-04-11 14:29:43 +0100163 * Retrieve the number of API1 compatible cameras; these are internal and
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800164 * backwards-compatible. This is the set of cameras that will be
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800165 * accessible via the old camera API.
166 * The return value may change dynamically due to external camera hotplug.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800167 */
Emilian Peevf53f66e2017-04-11 14:29:43 +0100168 std::vector<std::string> getAPI1CompatibleCameraDeviceIds() const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700169
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800170 /**
171 * Return true if a device with a given ID and major version exists
172 */
173 bool isValidDevice(const std::string &id, uint16_t majorVersion) const;
174
175 /**
176 * Return true if a device with a given ID has a flash unit. Returns false
177 * for devices that are unknown.
178 */
179 bool hasFlashUnit(const std::string &id) const;
180
181 /**
182 * Return the resource cost of this camera device
183 */
184 status_t getResourceCost(const std::string &id,
185 hardware::camera::common::V1_0::CameraResourceCost* cost) const;
186
187 /**
188 * Return the old camera API camera info
189 */
190 status_t getCameraInfo(const std::string &id,
191 hardware::CameraInfo* info) const;
192
193 /**
194 * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does
195 * not have a v3 or newer HAL version.
196 */
197 status_t getCameraCharacteristics(const std::string &id,
198 CameraMetadata* characteristics) const;
199
200 /**
Emilian Peev35ae8262018-11-08 13:11:32 +0000201 * Check for device support of specific stream combination.
202 */
203 status_t isSessionConfigurationSupported(const std::string& id,
204 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
205 bool *status /*out*/) const;
206
207 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800208 * Return the highest supported device interface version for this ID
209 */
210 status_t getHighestSupportedVersion(const std::string &id,
211 hardware::hidl_version *v);
212
213 /**
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700214 * Check if a given camera device support setTorchMode API.
215 */
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700216 bool supportSetTorchMode(const std::string &id) const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700217
218 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800219 * Turn on or off the flashlight on a given camera device.
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700220 * May fail if the device does not support this API, is in active use, or if the device
221 * doesn't exist, etc.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800222 */
223 status_t setTorchMode(const std::string &id, bool enabled);
224
225 /**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800226 * Setup vendor tags for all registered providers
227 */
228 status_t setUpVendorTags();
229
230 /**
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800231 * Inform registered providers about a device state change, such as folding or unfolding
232 */
233 status_t notifyDeviceStateChange(
234 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> newState);
235
236 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800237 * Open an active session to a camera device.
238 *
239 * This fully powers on the camera device hardware, and returns a handle to a
240 * session to be used for hardware configuration and operation.
241 */
242 status_t openSession(const std::string &id,
243 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
244 /*out*/
245 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session);
246
247 status_t openSession(const std::string &id,
248 const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback,
249 /*out*/
250 sp<hardware::camera::device::V1_0::ICameraDevice> *session);
251
252 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700253 * Save the ICameraProvider while it is being used by a camera or torch client
254 */
255 void saveRef(DeviceMode usageType, const std::string &cameraId,
256 sp<hardware::camera::provider::V2_4::ICameraProvider> provider);
257
258 /**
259 * Notify that the camera or torch is no longer being used by a camera client
260 */
261 void removeRef(DeviceMode usageType, const std::string &cameraId);
262
263 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800264 * IServiceNotification::onRegistration
265 * Invoked by the hardware service manager when a new camera provider is registered
266 */
267 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
268 const hardware::hidl_string& name,
269 bool preexisting) override;
270
271 /**
272 * Dump out information about available providers and devices
273 */
274 status_t dump(int fd, const Vector<String16>& args);
275
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800276 /**
277 * Conversion methods between HAL Status and status_t and strings
278 */
279 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
280 static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
281
Emilian Peev71c73a22017-03-21 16:35:51 +0000282 /*
283 * Return provider type for a specific device.
284 */
285 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id,
286 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
287 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
288
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700289 /*
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700290 * Check if a camera is a logical camera. And if yes, return
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700291 * the physical camera ids.
292 */
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700293 bool isLogicalCamera(const std::string& id, std::vector<std::string>* physicalCameraIds);
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700294
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700295 SystemCameraKind getSystemCameraKind(const std::string& id) const;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700296 bool isHiddenPhysicalCamera(const std::string& cameraId);
Emilian Peev538c90e2018-12-17 18:03:19 +0000297
298 static const float kDepthARTolerance;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800299private:
300 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
301 mutable std::mutex mInterfaceMutex;
302
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800303 // the status listener update callbacks will lock mStatusMutex
304 mutable std::mutex mStatusListenerMutex;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800305 wp<StatusListener> mListener;
306 ServiceInteractionProxy* mServiceProxy;
307
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800308 // Current overall Android device physical status
309 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
310
Shuzhen Wang6ba8eb22018-07-08 13:10:44 -0700311 // mProviderLifecycleLock is locked during onRegistration and removeProvider
312 mutable std::mutex mProviderLifecycleLock;
313
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800314 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
315
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700316 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
317 // ICameraProvider alive while it is in use by the camera with the given ID for camera
318 // capabilities
319 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
320 mCameraProviderByCameraId;
321
322 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
323 // ICameraProvider alive while it is in use by the camera with the given ID for torch
324 // capabilities
325 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
326 mTorchProviderByCameraId;
327
328 // Lock for accessing mCameraProviderByCameraId and mTorchProviderByCameraId
329 std::mutex mProviderInterfaceMapLock;
330
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700331 struct ProviderInfo :
332 virtual public hardware::camera::provider::V2_4::ICameraProviderCallback,
333 virtual public hardware::hidl_death_recipient
334 {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800335 const std::string mProviderName;
Emilian Peev71c73a22017-03-21 16:35:51 +0000336 const metadata_vendor_id_t mProviderTagid;
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800337 int mMinorVersion;
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800338 sp<VendorTagDescriptor> mVendorTagDescriptor;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700339 bool mSetTorchModeSupported;
340 bool mIsRemote;
341
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800342 // Current overall Android device physical status
343 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
344
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700345 // This pointer is used to keep a reference to the ICameraProvider that was last accessed.
346 wp<hardware::camera::provider::V2_4::ICameraProvider> mActiveInterface;
347
348 sp<hardware::camera::provider::V2_4::ICameraProvider> mSavedInterface;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800349
350 ProviderInfo(const std::string &providerName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800351 CameraProviderManager *manager);
352 ~ProviderInfo();
353
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800354 status_t initialize(sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
355 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
356 currentDeviceState);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700357
358 const sp<hardware::camera::provider::V2_4::ICameraProvider> startProviderInterface();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800359
360 const std::string& getType() const;
361
362 status_t addDevice(const std::string& name,
363 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
364 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
365 /*out*/ std::string *parsedId = nullptr);
366
367 status_t dump(int fd, const Vector<String16>& args) const;
368
369 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
370 virtual hardware::Return<void> cameraDeviceStatusChange(
371 const hardware::hidl_string& cameraDeviceName,
372 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
373 virtual hardware::Return<void> torchModeStatusChange(
374 const hardware::hidl_string& cameraDeviceName,
375 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
376
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700377 // hidl_death_recipient interface - this locks the parent mInterfaceMutex
378 virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
379
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800380 /**
381 * Setup vendor tags for this provider
382 */
383 status_t setUpVendorTags();
384
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800385 /**
386 * Notify provider about top-level device physical state changes
387 */
388 status_t notifyDeviceStateChange(
389 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
390 newDeviceState);
391
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800392 // Basic device information, common to all camera devices
393 struct DeviceInfo {
394 const std::string mName; // Full instance name
395 const std::string mId; // ID section of full name
396 const hardware::hidl_version mVersion;
Emilian Peev71c73a22017-03-21 16:35:51 +0000397 const metadata_vendor_id_t mProviderTagid;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700398 bool mIsLogicalCamera;
399 std::vector<std::string> mPhysicalIds;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700400 hardware::CameraInfo mInfo;
401 sp<IBase> mSavedInterface;
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700402 SystemCameraKind mSystemCameraKind = SystemCameraKind::PUBLIC;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800403
404 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
405
406 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
407
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700408 sp<ProviderInfo> mParentProvider;
409
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800410 bool hasFlashUnit() const { return mHasFlashUnit; }
411 virtual status_t setTorchMode(bool enabled) = 0;
412 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100413 virtual bool isAPI1Compatible() const = 0;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700414 virtual status_t dumpState(int fd) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800415 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
416 (void) characteristics;
417 return INVALID_OPERATION;
418 }
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700419 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
420 CameraMetadata *characteristics) const {
421 (void) physicalCameraId;
422 (void) characteristics;
423 return INVALID_OPERATION;
424 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800425
Emilian Peev35ae8262018-11-08 13:11:32 +0000426 virtual status_t isSessionConfigurationSupported(
427 const hardware::camera::device::V3_4::StreamConfiguration &/*configuration*/,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700428 bool * /*status*/) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000429 return INVALID_OPERATION;
430 }
431
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700432 template<class InterfaceT>
433 sp<InterfaceT> startDeviceInterface();
434
Emilian Peev71c73a22017-03-21 16:35:51 +0000435 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
436 const std::string &id, const hardware::hidl_version& version,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700437 const std::vector<std::string>& publicCameraIds,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700438 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
439 sp<ProviderInfo> parentProvider) :
Emilian Peev71c73a22017-03-21 16:35:51 +0000440 mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700441 mIsLogicalCamera(false), mResourceCost(resourceCost),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800442 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700443 mParentProvider(parentProvider), mHasFlashUnit(false),
444 mPublicCameraIds(publicCameraIds) {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800445 virtual ~DeviceInfo();
446 protected:
447 bool mHasFlashUnit;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700448 const std::vector<std::string>& mPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800449
450 template<class InterfaceT>
451 static status_t setTorchMode(InterfaceT& interface, bool enabled);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700452
453 template<class InterfaceT>
454 status_t setTorchModeForDevice(bool enabled) {
455 // Don't save the ICameraProvider interface here because we assume that this was
456 // called from CameraProviderManager::setTorchMode(), which does save it.
457 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
458 return DeviceInfo::setTorchMode(interface, enabled);
459 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800460 };
461 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800462 std::unordered_set<std::string> mUniqueCameraIds;
Yin-Chia Yehe8e9e192017-03-16 15:23:51 -0700463 int mUniqueDeviceCount;
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700464 std::vector<std::string> mUniqueAPI1CompatibleCameraIds;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700465 // The initial public camera IDs published by the camera provider.
466 // Currently logical multi-camera is not supported for hot-plug camera.
467 // And we use this list to keep track of initial public camera IDs
468 // advertised by the provider, and to distinguish against "hidden"
469 // physical camera IDs.
470 std::vector<std::string> mProviderPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800471
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800472 // HALv1-specific camera fields, including the actual device interface
473 struct DeviceInfo1 : public DeviceInfo {
474 typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800475
476 virtual status_t setTorchMode(bool enabled) override;
477 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100478 //In case of Device1Info assume that we are always API1 compatible
479 virtual bool isAPI1Compatible() const override { return true; }
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700480 virtual status_t dumpState(int fd) override;
Emilian Peev71c73a22017-03-21 16:35:51 +0000481 DeviceInfo1(const std::string& name, const metadata_vendor_id_t tagId,
482 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800483 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700484 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700485 const std::vector<std::string>& publicCameraIds,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800486 sp<InterfaceT> interface);
487 virtual ~DeviceInfo1();
488 private:
489 CameraParameters2 mDefaultParameters;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700490 status_t cacheCameraInfo(sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800491 };
492
493 // HALv3-specific camera fields, including the actual device interface
494 struct DeviceInfo3 : public DeviceInfo {
495 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800496
497 virtual status_t setTorchMode(bool enabled) override;
498 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100499 virtual bool isAPI1Compatible() const override;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700500 virtual status_t dumpState(int fd) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800501 virtual status_t getCameraCharacteristics(
502 CameraMetadata *characteristics) const override;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700503 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
504 CameraMetadata *characteristics) const override;
Emilian Peev35ae8262018-11-08 13:11:32 +0000505 virtual status_t isSessionConfigurationSupported(
506 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
507 bool *status /*out*/)
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700508 override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800509
Emilian Peev71c73a22017-03-21 16:35:51 +0000510 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
511 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800512 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700513 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700514 const std::vector<std::string>& publicCameraIds, sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800515 virtual ~DeviceInfo3();
516 private:
517 CameraMetadata mCameraCharacteristics;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700518 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700519 void queryPhysicalCameraIds();
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700520 SystemCameraKind getSystemCameraKind();
Shuzhen Wang268a1362018-10-16 16:32:59 -0700521 status_t fixupMonochromeTags();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000522 status_t addDynamicDepthTags();
523 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag,
524 android_pixel_format_t format,
525 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/);
526 void getSupportedDurations( const CameraMetadata& ch, uint32_t tag,
527 android_pixel_format_t format,
528 const std::vector<std::tuple<size_t, size_t>>& sizes,
529 std::vector<int64_t> *durations/*out*/);
530 void getSupportedDynamicDepthDurations(const std::vector<int64_t>& depthDurations,
531 const std::vector<int64_t>& blobDurations,
532 std::vector<int64_t> *dynamicDepthDurations /*out*/);
Emilian Peevcbf174b2019-01-25 14:38:59 -0800533 static bool isDepthPhotoLibraryPresent();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000534 static void getSupportedDynamicDepthSizes(
535 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
536 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
537 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
538 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/);
Shuzhen Wang268a1362018-10-16 16:32:59 -0700539 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys,
540 uint32_t keyTag);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800541 status_t fillHeicStreamCombinations(std::vector<int32_t>* outputs,
542 std::vector<int64_t>* durations,
543 std::vector<int64_t>* stallDurations,
544 const camera_metadata_entry& halStreamConfigs,
545 const camera_metadata_entry& halStreamDurations);
546 status_t deriveHeicTags();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800547 };
548
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800549 private:
550 std::string mType;
551 uint32_t mId;
552
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700553 std::mutex mLock;
554
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800555 CameraProviderManager *mManager;
556
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800557 bool mInitialized = false;
558
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800559 // Templated method to instantiate the right kind of DeviceInfo and call the
560 // right CameraProvider getCameraDeviceInterface_* method.
561 template<class DeviceInfoT>
562 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
Emilian Peev71c73a22017-03-21 16:35:51 +0000563 const metadata_vendor_id_t tagId, const std::string &id,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700564 uint16_t minorVersion);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800565
566 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
567 template<class InterfaceT>
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700568 sp<InterfaceT> startDeviceInterface(const std::string &name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800569
570 // Parse provider instance name for type and id
571 static status_t parseProviderName(const std::string& name,
572 std::string *type, uint32_t *id);
573
574 // Parse device instance name for device version, type, and id.
575 static status_t parseDeviceName(const std::string& name,
576 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
Emilian Peev71c73a22017-03-21 16:35:51 +0000577
578 // Generate vendor tag id
579 static metadata_vendor_id_t generateVendorTagId(const std::string &name);
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100580
581 void removeDevice(std::string id);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800582 };
583
584 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
585 // and the calling code doesn't mutate the list of providers or their lists of devices.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800586 // Finds the first device of the given ID that falls within the requested version range
587 // minVersion <= deviceVersion < maxVersion
588 // No guarantees on the order of traversal
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800589 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800590 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
591 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800592
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700593 status_t addProviderLocked(const std::string& newProvider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700594
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800595 status_t removeProvider(const std::string& provider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700596 sp<StatusListener> getStatusListener() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800597
598 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
599
600 std::vector<sp<ProviderInfo>> mProviders;
601
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700602 void addProviderToMap(
603 const std::string &cameraId,
604 sp<hardware::camera::provider::V2_4::ICameraProvider> provider,
605 bool isTorchUsage);
606 void removeCameraIdFromMap(
607 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> &map,
608 const std::string &cameraId);
609
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800610 static const char* deviceStatusToString(
611 const hardware::camera::common::V1_0::CameraDeviceStatus&);
612 static const char* torchStatusToString(
613 const hardware::camera::common::V1_0::TorchModeStatus&);
614
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700615 status_t getCameraCharacteristicsLocked(const std::string &id,
616 CameraMetadata* characteristics) const;
617 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700618
619 SystemCameraKind getSystemCameraKindLocked(const std::string& id) const;
620
621 void collectDeviceIdsLocked(const std::vector<std::string> deviceIds,
622 std::vector<std::string>& normalDeviceIds,
623 std::vector<std::string>& systemCameraDeviceIds) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800624};
625
626} // namespace android
627
628#endif