blob: 49a93cc3cd255272ea397087ac13bcd34a443c7d [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>
Shuzhen Wang43858162020-01-10 13:42:15 -080032#include <android/hardware/camera/provider/2.6/ICameraProviderCallback.h>
Emilian Peev35ae8262018-11-08 13:11:32 +000033#include <android/hardware/camera/device/3.4/ICameraDeviceSession.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080034#include <android/hidl/manager/1.0/IServiceNotification.h>
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080035#include <camera/VendorTagDescriptor.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080036
37namespace android {
38
39/**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080040 * The vendor tag descriptor class that takes HIDL vendor tag information as
41 * input. Not part of VendorTagDescriptor class because that class is used
42 * in AIDL generated sources which don't have access to HIDL headers.
43 */
44class HidlVendorTagDescriptor : public VendorTagDescriptor {
45public:
46 /**
47 * Create a VendorTagDescriptor object from the HIDL VendorTagSection
48 * vector.
49 *
50 * Returns OK on success, or a negative error code.
51 */
52 static status_t createDescriptorFromHidl(
53 const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts,
54 /*out*/
55 sp<VendorTagDescriptor>& descriptor);
56};
57
Jayant Chowdhary5216b212019-07-17 09:26:23 -070058enum SystemCameraKind {
59 /**
60 * These camera devices are visible to all apps and system components alike
61 */
62 PUBLIC = 0,
63
64 /**
65 * These camera devices are visible only to processes having the
66 * android.permission.SYSTEM_CAMERA permission. They are not exposed to 3P
67 * apps.
68 */
69 SYSTEM_ONLY_CAMERA,
70
71 /**
72 * These camera devices are visible only to HAL clients (that try to connect
73 * on a hwbinder thread).
74 */
75 HIDDEN_SECURE_CAMERA
76};
77
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080078/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080079 * A manager for all camera providers available on an Android device.
80 *
81 * Responsible for enumerating providers and the individual camera devices
82 * they export, both at startup and as providers and devices are added/removed.
83 *
84 * Provides methods for requesting information about individual devices and for
85 * opening them for active use.
86 *
87 */
88class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification {
89public:
90
91 ~CameraProviderManager();
92
93 // Tiny proxy for the static methods in a HIDL interface that communicate with the hardware
94 // service manager, to be replacable in unit tests with a fake.
95 struct ServiceInteractionProxy {
96 virtual bool registerForNotifications(
97 const std::string &serviceName,
98 const sp<hidl::manager::V1_0::IServiceNotification>
99 &notification) = 0;
Eino-Ville Talvalaec960602019-10-15 11:46:16 -0700100 // Will not wait for service to start if it's not already running
101 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService(
102 const std::string &serviceName) = 0;
103 // Will block for service if it exists but isn't running
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800104 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
105 const std::string &serviceName) = 0;
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700106 virtual hardware::hidl_vec<hardware::hidl_string> listServices() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800107 virtual ~ServiceInteractionProxy() {}
108 };
109
110 // Standard use case - call into the normal generated static methods which invoke
111 // the real hardware service manager
112 struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
113 virtual bool registerForNotifications(
114 const std::string &serviceName,
115 const sp<hidl::manager::V1_0::IServiceNotification>
116 &notification) override {
117 return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
118 serviceName, notification);
119 }
Eino-Ville Talvalaec960602019-10-15 11:46:16 -0700120 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService(
121 const std::string &serviceName) override {
122 return hardware::camera::provider::V2_4::ICameraProvider::tryGetService(serviceName);
123 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800124 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
125 const std::string &serviceName) override {
126 return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
127 }
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700128
129 virtual hardware::hidl_vec<hardware::hidl_string> listServices() override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800130 };
131
132 /**
133 * Listener interface for device/torch status changes
134 */
135 struct StatusListener : virtual public RefBase {
136 ~StatusListener() {}
137
138 virtual void onDeviceStatusChanged(const String8 &cameraId,
139 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
Shuzhen Wang43858162020-01-10 13:42:15 -0800140 virtual void onDeviceStatusChanged(const String8 &cameraId,
141 const String8 &physicalCameraId,
142 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800143 virtual void onTorchStatusChanged(const String8 &cameraId,
144 hardware::camera::common::V1_0::TorchModeStatus newStatus) = 0;
Emilian Peevaee727d2017-05-04 16:35:48 +0100145 virtual void onNewProviderRegistered() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800146 };
147
148 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700149 * Represents the mode a camera device is currently in
150 */
151 enum class DeviceMode {
152 TORCH,
153 CAMERA
154 };
155
156 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800157 * Initialize the manager and give it a status listener; optionally accepts a service
158 * interaction proxy.
159 *
160 * The default proxy communicates via the hardware service manager; alternate proxies can be
161 * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
162 */
163 status_t initialize(wp<StatusListener> listener,
164 ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);
165
166 /**
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700167 * Retrieve the total number of available cameras.
168 * This value may change dynamically as cameras are added or removed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800169 */
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700170 std::pair<int, int> getCameraCount() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800171
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800172 std::vector<std::string> getCameraDeviceIds() const;
173
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800174 /**
Emilian Peevf53f66e2017-04-11 14:29:43 +0100175 * Retrieve the number of API1 compatible cameras; these are internal and
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800176 * backwards-compatible. This is the set of cameras that will be
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800177 * accessible via the old camera API.
178 * The return value may change dynamically due to external camera hotplug.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800179 */
Emilian Peevf53f66e2017-04-11 14:29:43 +0100180 std::vector<std::string> getAPI1CompatibleCameraDeviceIds() const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700181
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800182 /**
183 * Return true if a device with a given ID and major version exists
184 */
185 bool isValidDevice(const std::string &id, uint16_t majorVersion) const;
186
187 /**
188 * Return true if a device with a given ID has a flash unit. Returns false
189 * for devices that are unknown.
190 */
191 bool hasFlashUnit(const std::string &id) const;
192
193 /**
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800194 * Return true if the camera device has native zoom ratio support.
195 */
196 bool supportNativeZoomRatio(const std::string &id) const;
197
198 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800199 * Return the resource cost of this camera device
200 */
201 status_t getResourceCost(const std::string &id,
202 hardware::camera::common::V1_0::CameraResourceCost* cost) const;
203
204 /**
205 * Return the old camera API camera info
206 */
207 status_t getCameraInfo(const std::string &id,
208 hardware::CameraInfo* info) const;
209
210 /**
211 * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does
212 * not have a v3 or newer HAL version.
213 */
214 status_t getCameraCharacteristics(const std::string &id,
215 CameraMetadata* characteristics) const;
216
217 /**
Emilian Peev35ae8262018-11-08 13:11:32 +0000218 * Check for device support of specific stream combination.
219 */
220 status_t isSessionConfigurationSupported(const std::string& id,
221 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
222 bool *status /*out*/) const;
223
224 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800225 * Return the highest supported device interface version for this ID
226 */
227 status_t getHighestSupportedVersion(const std::string &id,
228 hardware::hidl_version *v);
229
230 /**
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700231 * Check if a given camera device support setTorchMode API.
232 */
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700233 bool supportSetTorchMode(const std::string &id) const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700234
235 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800236 * Turn on or off the flashlight on a given camera device.
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700237 * May fail if the device does not support this API, is in active use, or if the device
238 * doesn't exist, etc.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800239 */
240 status_t setTorchMode(const std::string &id, bool enabled);
241
242 /**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800243 * Setup vendor tags for all registered providers
244 */
245 status_t setUpVendorTags();
246
247 /**
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800248 * Inform registered providers about a device state change, such as folding or unfolding
249 */
250 status_t notifyDeviceStateChange(
251 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> newState);
252
253 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800254 * Open an active session to a camera device.
255 *
256 * This fully powers on the camera device hardware, and returns a handle to a
257 * session to be used for hardware configuration and operation.
258 */
259 status_t openSession(const std::string &id,
260 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
261 /*out*/
262 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session);
263
264 status_t openSession(const std::string &id,
265 const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback,
266 /*out*/
267 sp<hardware::camera::device::V1_0::ICameraDevice> *session);
268
269 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700270 * Save the ICameraProvider while it is being used by a camera or torch client
271 */
272 void saveRef(DeviceMode usageType, const std::string &cameraId,
273 sp<hardware::camera::provider::V2_4::ICameraProvider> provider);
274
275 /**
276 * Notify that the camera or torch is no longer being used by a camera client
277 */
278 void removeRef(DeviceMode usageType, const std::string &cameraId);
279
280 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800281 * IServiceNotification::onRegistration
282 * Invoked by the hardware service manager when a new camera provider is registered
283 */
284 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
285 const hardware::hidl_string& name,
286 bool preexisting) override;
287
288 /**
289 * Dump out information about available providers and devices
290 */
291 status_t dump(int fd, const Vector<String16>& args);
292
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800293 /**
294 * Conversion methods between HAL Status and status_t and strings
295 */
296 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
297 static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
298
Emilian Peev71c73a22017-03-21 16:35:51 +0000299 /*
300 * Return provider type for a specific device.
301 */
302 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id,
303 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
304 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
305
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700306 /*
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700307 * Check if a camera is a logical camera. And if yes, return
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700308 * the physical camera ids.
309 */
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700310 bool isLogicalCamera(const std::string& id, std::vector<std::string>* physicalCameraIds);
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700311
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700312 status_t getSystemCameraKind(const std::string& id, SystemCameraKind *kind) const;
313 bool isHiddenPhysicalCamera(const std::string& cameraId) const;
Emilian Peev538c90e2018-12-17 18:03:19 +0000314
315 static const float kDepthARTolerance;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800316private:
317 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
318 mutable std::mutex mInterfaceMutex;
319
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800320 // the status listener update callbacks will lock mStatusMutex
321 mutable std::mutex mStatusListenerMutex;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800322 wp<StatusListener> mListener;
323 ServiceInteractionProxy* mServiceProxy;
324
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800325 // Current overall Android device physical status
326 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
327
Shuzhen Wang6ba8eb22018-07-08 13:10:44 -0700328 // mProviderLifecycleLock is locked during onRegistration and removeProvider
329 mutable std::mutex mProviderLifecycleLock;
330
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800331 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
332
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700333 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
334 // ICameraProvider alive while it is in use by the camera with the given ID for camera
335 // capabilities
336 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
337 mCameraProviderByCameraId;
338
339 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
340 // ICameraProvider alive while it is in use by the camera with the given ID for torch
341 // capabilities
342 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
343 mTorchProviderByCameraId;
344
345 // Lock for accessing mCameraProviderByCameraId and mTorchProviderByCameraId
346 std::mutex mProviderInterfaceMapLock;
347
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700348 struct ProviderInfo :
Shuzhen Wang43858162020-01-10 13:42:15 -0800349 virtual public hardware::camera::provider::V2_6::ICameraProviderCallback,
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700350 virtual public hardware::hidl_death_recipient
351 {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800352 const std::string mProviderName;
Emilian Peev71c73a22017-03-21 16:35:51 +0000353 const metadata_vendor_id_t mProviderTagid;
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800354 int mMinorVersion;
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800355 sp<VendorTagDescriptor> mVendorTagDescriptor;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700356 bool mSetTorchModeSupported;
357 bool mIsRemote;
358
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800359 // Current overall Android device physical status
360 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
361
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700362 // This pointer is used to keep a reference to the ICameraProvider that was last accessed.
363 wp<hardware::camera::provider::V2_4::ICameraProvider> mActiveInterface;
364
365 sp<hardware::camera::provider::V2_4::ICameraProvider> mSavedInterface;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800366
367 ProviderInfo(const std::string &providerName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800368 CameraProviderManager *manager);
369 ~ProviderInfo();
370
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800371 status_t initialize(sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
372 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
373 currentDeviceState);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700374
375 const sp<hardware::camera::provider::V2_4::ICameraProvider> startProviderInterface();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800376
377 const std::string& getType() const;
378
379 status_t addDevice(const std::string& name,
380 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
381 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
382 /*out*/ std::string *parsedId = nullptr);
383
384 status_t dump(int fd, const Vector<String16>& args) const;
385
386 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
Shuzhen Wang43858162020-01-10 13:42:15 -0800387 hardware::Return<void> cameraDeviceStatusChange(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800388 const hardware::hidl_string& cameraDeviceName,
389 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
Shuzhen Wang43858162020-01-10 13:42:15 -0800390 hardware::Return<void> torchModeStatusChange(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800391 const hardware::hidl_string& cameraDeviceName,
392 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
Shuzhen Wang43858162020-01-10 13:42:15 -0800393 hardware::Return<void> physicalCameraDeviceStatusChange(
394 const hardware::hidl_string& cameraDeviceName,
395 const hardware::hidl_string& physicalCameraDeviceName,
396 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800397
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700398 // hidl_death_recipient interface - this locks the parent mInterfaceMutex
399 virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
400
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800401 /**
402 * Setup vendor tags for this provider
403 */
404 status_t setUpVendorTags();
405
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800406 /**
407 * Notify provider about top-level device physical state changes
408 */
409 status_t notifyDeviceStateChange(
410 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
411 newDeviceState);
412
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800413 // Basic device information, common to all camera devices
414 struct DeviceInfo {
415 const std::string mName; // Full instance name
416 const std::string mId; // ID section of full name
417 const hardware::hidl_version mVersion;
Emilian Peev71c73a22017-03-21 16:35:51 +0000418 const metadata_vendor_id_t mProviderTagid;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700419 bool mIsLogicalCamera;
420 std::vector<std::string> mPhysicalIds;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700421 hardware::CameraInfo mInfo;
422 sp<IBase> mSavedInterface;
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700423 SystemCameraKind mSystemCameraKind = SystemCameraKind::PUBLIC;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800424
425 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
426
427 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
Shuzhen Wang43858162020-01-10 13:42:15 -0800428 std::map<std::string, hardware::camera::common::V1_0::CameraDeviceStatus>
429 mPhysicalStatus;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800430
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700431 sp<ProviderInfo> mParentProvider;
432
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800433 bool hasFlashUnit() const { return mHasFlashUnit; }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800434 bool supportNativeZoomRatio() const { return mSupportNativeZoomRatio; }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800435 virtual status_t setTorchMode(bool enabled) = 0;
436 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100437 virtual bool isAPI1Compatible() const = 0;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700438 virtual status_t dumpState(int fd) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800439 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
440 (void) characteristics;
441 return INVALID_OPERATION;
442 }
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700443 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
444 CameraMetadata *characteristics) const {
445 (void) physicalCameraId;
446 (void) characteristics;
447 return INVALID_OPERATION;
448 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800449
Emilian Peev35ae8262018-11-08 13:11:32 +0000450 virtual status_t isSessionConfigurationSupported(
451 const hardware::camera::device::V3_4::StreamConfiguration &/*configuration*/,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700452 bool * /*status*/) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000453 return INVALID_OPERATION;
454 }
455
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700456 template<class InterfaceT>
457 sp<InterfaceT> startDeviceInterface();
458
Emilian Peev71c73a22017-03-21 16:35:51 +0000459 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
460 const std::string &id, const hardware::hidl_version& version,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700461 const std::vector<std::string>& publicCameraIds,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700462 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
463 sp<ProviderInfo> parentProvider) :
Emilian Peev71c73a22017-03-21 16:35:51 +0000464 mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700465 mIsLogicalCamera(false), mResourceCost(resourceCost),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800466 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700467 mParentProvider(parentProvider), mHasFlashUnit(false),
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800468 mSupportNativeZoomRatio(false), mPublicCameraIds(publicCameraIds) {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800469 virtual ~DeviceInfo();
470 protected:
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800471 bool mHasFlashUnit; // const after constructor
472 bool mSupportNativeZoomRatio; // const after constructor
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700473 const std::vector<std::string>& mPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800474
475 template<class InterfaceT>
476 static status_t setTorchMode(InterfaceT& interface, bool enabled);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700477
478 template<class InterfaceT>
479 status_t setTorchModeForDevice(bool enabled) {
480 // Don't save the ICameraProvider interface here because we assume that this was
481 // called from CameraProviderManager::setTorchMode(), which does save it.
482 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
483 return DeviceInfo::setTorchMode(interface, enabled);
484 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800485 };
486 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800487 std::unordered_set<std::string> mUniqueCameraIds;
Yin-Chia Yehe8e9e192017-03-16 15:23:51 -0700488 int mUniqueDeviceCount;
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700489 std::vector<std::string> mUniqueAPI1CompatibleCameraIds;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700490 // The initial public camera IDs published by the camera provider.
491 // Currently logical multi-camera is not supported for hot-plug camera.
492 // And we use this list to keep track of initial public camera IDs
493 // advertised by the provider, and to distinguish against "hidden"
494 // physical camera IDs.
495 std::vector<std::string> mProviderPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800496
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800497 // HALv1-specific camera fields, including the actual device interface
498 struct DeviceInfo1 : public DeviceInfo {
499 typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800500
501 virtual status_t setTorchMode(bool enabled) override;
502 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100503 //In case of Device1Info assume that we are always API1 compatible
504 virtual bool isAPI1Compatible() const override { return true; }
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700505 virtual status_t dumpState(int fd) override;
Emilian Peev71c73a22017-03-21 16:35:51 +0000506 DeviceInfo1(const std::string& name, const metadata_vendor_id_t tagId,
507 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800508 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700509 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700510 const std::vector<std::string>& publicCameraIds,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800511 sp<InterfaceT> interface);
512 virtual ~DeviceInfo1();
513 private:
514 CameraParameters2 mDefaultParameters;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700515 status_t cacheCameraInfo(sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800516 };
517
518 // HALv3-specific camera fields, including the actual device interface
519 struct DeviceInfo3 : public DeviceInfo {
520 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800521
522 virtual status_t setTorchMode(bool enabled) override;
523 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100524 virtual bool isAPI1Compatible() const override;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700525 virtual status_t dumpState(int fd) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800526 virtual status_t getCameraCharacteristics(
527 CameraMetadata *characteristics) const override;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700528 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
529 CameraMetadata *characteristics) const override;
Emilian Peev35ae8262018-11-08 13:11:32 +0000530 virtual status_t isSessionConfigurationSupported(
531 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
532 bool *status /*out*/)
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700533 override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800534
Emilian Peev71c73a22017-03-21 16:35:51 +0000535 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
536 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800537 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700538 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700539 const std::vector<std::string>& publicCameraIds, sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800540 virtual ~DeviceInfo3();
541 private:
542 CameraMetadata mCameraCharacteristics;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700543 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700544 void queryPhysicalCameraIds();
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700545 SystemCameraKind getSystemCameraKind();
Shuzhen Wang268a1362018-10-16 16:32:59 -0700546 status_t fixupMonochromeTags();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000547 status_t addDynamicDepthTags();
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800548 status_t deriveHeicTags();
549 status_t addRotateCropTags();
550
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000551 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag,
552 android_pixel_format_t format,
553 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/);
554 void getSupportedDurations( const CameraMetadata& ch, uint32_t tag,
555 android_pixel_format_t format,
556 const std::vector<std::tuple<size_t, size_t>>& sizes,
557 std::vector<int64_t> *durations/*out*/);
558 void getSupportedDynamicDepthDurations(const std::vector<int64_t>& depthDurations,
559 const std::vector<int64_t>& blobDurations,
560 std::vector<int64_t> *dynamicDepthDurations /*out*/);
561 static void getSupportedDynamicDepthSizes(
562 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
563 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
564 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
565 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/);
Shuzhen Wang268a1362018-10-16 16:32:59 -0700566 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys,
567 uint32_t keyTag);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800568 status_t fillHeicStreamCombinations(std::vector<int32_t>* outputs,
569 std::vector<int64_t>* durations,
570 std::vector<int64_t>* stallDurations,
571 const camera_metadata_entry& halStreamConfigs,
572 const camera_metadata_entry& halStreamDurations);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800573 };
574
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800575 private:
576 std::string mType;
577 uint32_t mId;
578
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700579 std::mutex mLock;
580
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800581 CameraProviderManager *mManager;
582
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800583 bool mInitialized = false;
584
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800585 // Templated method to instantiate the right kind of DeviceInfo and call the
586 // right CameraProvider getCameraDeviceInterface_* method.
587 template<class DeviceInfoT>
588 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
Emilian Peev71c73a22017-03-21 16:35:51 +0000589 const metadata_vendor_id_t tagId, const std::string &id,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700590 uint16_t minorVersion);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800591
592 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
593 template<class InterfaceT>
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700594 sp<InterfaceT> startDeviceInterface(const std::string &name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800595
596 // Parse provider instance name for type and id
597 static status_t parseProviderName(const std::string& name,
598 std::string *type, uint32_t *id);
599
600 // Parse device instance name for device version, type, and id.
601 static status_t parseDeviceName(const std::string& name,
602 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
Emilian Peev71c73a22017-03-21 16:35:51 +0000603
604 // Generate vendor tag id
605 static metadata_vendor_id_t generateVendorTagId(const std::string &name);
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100606
607 void removeDevice(std::string id);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800608 };
609
610 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
611 // and the calling code doesn't mutate the list of providers or their lists of devices.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800612 // Finds the first device of the given ID that falls within the requested version range
613 // minVersion <= deviceVersion < maxVersion
614 // No guarantees on the order of traversal
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800615 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800616 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
617 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800618
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700619 status_t addProviderLocked(const std::string& newProvider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700620
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800621 status_t removeProvider(const std::string& provider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700622 sp<StatusListener> getStatusListener() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800623
624 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
625
626 std::vector<sp<ProviderInfo>> mProviders;
627
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700628 void addProviderToMap(
629 const std::string &cameraId,
630 sp<hardware::camera::provider::V2_4::ICameraProvider> provider,
631 bool isTorchUsage);
632 void removeCameraIdFromMap(
633 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> &map,
634 const std::string &cameraId);
635
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800636 static const char* deviceStatusToString(
637 const hardware::camera::common::V1_0::CameraDeviceStatus&);
638 static const char* torchStatusToString(
639 const hardware::camera::common::V1_0::TorchModeStatus&);
640
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700641 status_t getCameraCharacteristicsLocked(const std::string &id,
642 CameraMetadata* characteristics) const;
643 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700644
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700645 status_t getSystemCameraKindLocked(const std::string& id, SystemCameraKind *kind) const;
646 std::pair<bool, ProviderInfo::DeviceInfo *> isHiddenPhysicalCameraInternal(const std::string& cameraId) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700647
648 void collectDeviceIdsLocked(const std::vector<std::string> deviceIds,
649 std::vector<std::string>& normalDeviceIds,
650 std::vector<std::string>& systemCameraDeviceIds) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800651};
652
653} // namespace android
654
655#endif