blob: c9e0a33c3b51f1280887bc156991dce51df395fa [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
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -080026#include <camera/camera2/ConcurrentCamera.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080027#include <camera/CameraParameters2.h>
28#include <camera/CameraMetadata.h>
29#include <camera/CameraBase.h>
30#include <utils/Errors.h>
31#include <android/hardware/camera/common/1.0/types.h>
Eino-Ville Talvala63f36112018-12-06 14:57:03 -080032#include <android/hardware/camera/provider/2.5/ICameraProvider.h>
Shuzhen Wang43858162020-01-10 13:42:15 -080033#include <android/hardware/camera/provider/2.6/ICameraProviderCallback.h>
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -080034#include <android/hardware/camera/provider/2.6/ICameraProvider.h>
Emilian Peev35ae8262018-11-08 13:11:32 +000035#include <android/hardware/camera/device/3.4/ICameraDeviceSession.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080036#include <android/hidl/manager/1.0/IServiceNotification.h>
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080037#include <camera/VendorTagDescriptor.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080038
39namespace android {
40
41/**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080042 * The vendor tag descriptor class that takes HIDL vendor tag information as
43 * input. Not part of VendorTagDescriptor class because that class is used
44 * in AIDL generated sources which don't have access to HIDL headers.
45 */
46class HidlVendorTagDescriptor : public VendorTagDescriptor {
47public:
48 /**
49 * Create a VendorTagDescriptor object from the HIDL VendorTagSection
50 * vector.
51 *
52 * Returns OK on success, or a negative error code.
53 */
54 static status_t createDescriptorFromHidl(
55 const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts,
56 /*out*/
57 sp<VendorTagDescriptor>& descriptor);
58};
59
Jayant Chowdhary5216b212019-07-17 09:26:23 -070060enum SystemCameraKind {
61 /**
62 * These camera devices are visible to all apps and system components alike
63 */
64 PUBLIC = 0,
65
66 /**
67 * These camera devices are visible only to processes having the
68 * android.permission.SYSTEM_CAMERA permission. They are not exposed to 3P
69 * apps.
70 */
71 SYSTEM_ONLY_CAMERA,
72
73 /**
74 * These camera devices are visible only to HAL clients (that try to connect
75 * on a hwbinder thread).
76 */
77 HIDDEN_SECURE_CAMERA
78};
79
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080080/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080081 * A manager for all camera providers available on an Android device.
82 *
83 * Responsible for enumerating providers and the individual camera devices
84 * they export, both at startup and as providers and devices are added/removed.
85 *
86 * Provides methods for requesting information about individual devices and for
87 * opening them for active use.
88 *
89 */
90class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification {
91public:
92
93 ~CameraProviderManager();
94
95 // Tiny proxy for the static methods in a HIDL interface that communicate with the hardware
96 // service manager, to be replacable in unit tests with a fake.
97 struct ServiceInteractionProxy {
98 virtual bool registerForNotifications(
99 const std::string &serviceName,
100 const sp<hidl::manager::V1_0::IServiceNotification>
101 &notification) = 0;
Eino-Ville Talvalaec960602019-10-15 11:46:16 -0700102 // Will not wait for service to start if it's not already running
103 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService(
104 const std::string &serviceName) = 0;
105 // Will block for service if it exists but isn't running
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800106 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
107 const std::string &serviceName) = 0;
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700108 virtual hardware::hidl_vec<hardware::hidl_string> listServices() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800109 virtual ~ServiceInteractionProxy() {}
110 };
111
112 // Standard use case - call into the normal generated static methods which invoke
113 // the real hardware service manager
114 struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
115 virtual bool registerForNotifications(
116 const std::string &serviceName,
117 const sp<hidl::manager::V1_0::IServiceNotification>
118 &notification) override {
119 return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
120 serviceName, notification);
121 }
Eino-Ville Talvalaec960602019-10-15 11:46:16 -0700122 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService(
123 const std::string &serviceName) override {
124 return hardware::camera::provider::V2_4::ICameraProvider::tryGetService(serviceName);
125 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800126 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
127 const std::string &serviceName) override {
128 return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
129 }
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700130
131 virtual hardware::hidl_vec<hardware::hidl_string> listServices() override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800132 };
133
134 /**
135 * Listener interface for device/torch status changes
136 */
137 struct StatusListener : virtual public RefBase {
138 ~StatusListener() {}
139
140 virtual void onDeviceStatusChanged(const String8 &cameraId,
141 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
Shuzhen Wang43858162020-01-10 13:42:15 -0800142 virtual void onDeviceStatusChanged(const String8 &cameraId,
143 const String8 &physicalCameraId,
144 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800145 virtual void onTorchStatusChanged(const String8 &cameraId,
Jayant Chowdhary407571f2021-10-05 14:36:13 -0700146 hardware::camera::common::V1_0::TorchModeStatus newStatus,
147 SystemCameraKind kind) = 0;
148 virtual void onTorchStatusChanged(const String8 &cameraId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800149 hardware::camera::common::V1_0::TorchModeStatus newStatus) = 0;
Emilian Peevaee727d2017-05-04 16:35:48 +0100150 virtual void onNewProviderRegistered() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800151 };
152
153 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700154 * Represents the mode a camera device is currently in
155 */
156 enum class DeviceMode {
157 TORCH,
158 CAMERA
159 };
160
161 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800162 * Initialize the manager and give it a status listener; optionally accepts a service
163 * interaction proxy.
164 *
165 * The default proxy communicates via the hardware service manager; alternate proxies can be
166 * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
167 */
168 status_t initialize(wp<StatusListener> listener,
169 ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);
170
171 /**
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700172 * Retrieve the total number of available cameras.
173 * This value may change dynamically as cameras are added or removed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800174 */
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700175 std::pair<int, int> getCameraCount() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800176
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800177 std::vector<std::string> getCameraDeviceIds() const;
178
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800179 /**
Emilian Peevf53f66e2017-04-11 14:29:43 +0100180 * Retrieve the number of API1 compatible cameras; these are internal and
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800181 * backwards-compatible. This is the set of cameras that will be
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800182 * accessible via the old camera API.
183 * The return value may change dynamically due to external camera hotplug.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800184 */
Emilian Peevf53f66e2017-04-11 14:29:43 +0100185 std::vector<std::string> getAPI1CompatibleCameraDeviceIds() const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700186
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800187 /**
188 * Return true if a device with a given ID and major version exists
189 */
190 bool isValidDevice(const std::string &id, uint16_t majorVersion) const;
191
192 /**
193 * Return true if a device with a given ID has a flash unit. Returns false
194 * for devices that are unknown.
195 */
196 bool hasFlashUnit(const std::string &id) const;
197
198 /**
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800199 * Return true if the camera device has native zoom ratio support.
200 */
201 bool supportNativeZoomRatio(const std::string &id) const;
202
203 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800204 * Return the resource cost of this camera device
205 */
206 status_t getResourceCost(const std::string &id,
207 hardware::camera::common::V1_0::CameraResourceCost* cost) const;
208
209 /**
210 * Return the old camera API camera info
211 */
212 status_t getCameraInfo(const std::string &id,
213 hardware::CameraInfo* info) const;
214
215 /**
216 * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does
217 * not have a v3 or newer HAL version.
218 */
219 status_t getCameraCharacteristics(const std::string &id,
220 CameraMetadata* characteristics) const;
221
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800222 status_t isConcurrentSessionConfigurationSupported(
223 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration>
224 &cameraIdsAndSessionConfigs,
225 bool *isSupported);
226
Jayant Chowdharycad23c22020-03-10 15:04:59 -0700227 std::vector<std::unordered_set<std::string>> getConcurrentCameraIds() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800228 /**
Emilian Peev35ae8262018-11-08 13:11:32 +0000229 * Check for device support of specific stream combination.
230 */
231 status_t isSessionConfigurationSupported(const std::string& id,
232 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
233 bool *status /*out*/) const;
234
235 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800236 * Return the highest supported device interface version for this ID
237 */
238 status_t getHighestSupportedVersion(const std::string &id,
239 hardware::hidl_version *v);
240
241 /**
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700242 * Check if a given camera device support setTorchMode API.
243 */
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700244 bool supportSetTorchMode(const std::string &id) const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700245
246 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800247 * Turn on or off the flashlight on a given camera device.
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700248 * May fail if the device does not support this API, is in active use, or if the device
249 * doesn't exist, etc.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800250 */
251 status_t setTorchMode(const std::string &id, bool enabled);
252
253 /**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800254 * Setup vendor tags for all registered providers
255 */
256 status_t setUpVendorTags();
257
258 /**
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800259 * Inform registered providers about a device state change, such as folding or unfolding
260 */
261 status_t notifyDeviceStateChange(
262 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> newState);
263
264 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800265 * Open an active session to a camera device.
266 *
267 * This fully powers on the camera device hardware, and returns a handle to a
268 * session to be used for hardware configuration and operation.
269 */
270 status_t openSession(const std::string &id,
271 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
272 /*out*/
273 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session);
274
275 status_t openSession(const std::string &id,
276 const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback,
277 /*out*/
278 sp<hardware::camera::device::V1_0::ICameraDevice> *session);
279
280 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700281 * Save the ICameraProvider while it is being used by a camera or torch client
282 */
283 void saveRef(DeviceMode usageType, const std::string &cameraId,
284 sp<hardware::camera::provider::V2_4::ICameraProvider> provider);
285
286 /**
287 * Notify that the camera or torch is no longer being used by a camera client
288 */
289 void removeRef(DeviceMode usageType, const std::string &cameraId);
290
291 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800292 * IServiceNotification::onRegistration
293 * Invoked by the hardware service manager when a new camera provider is registered
294 */
295 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
296 const hardware::hidl_string& name,
297 bool preexisting) override;
298
299 /**
300 * Dump out information about available providers and devices
301 */
302 status_t dump(int fd, const Vector<String16>& args);
303
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800304 /**
305 * Conversion methods between HAL Status and status_t and strings
306 */
307 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
308 static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
309
Emilian Peev71c73a22017-03-21 16:35:51 +0000310 /*
311 * Return provider type for a specific device.
312 */
313 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id,
314 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
315 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
316
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700317 /*
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700318 * Check if a camera is a logical camera. And if yes, return
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700319 * the physical camera ids.
320 */
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700321 bool isLogicalCamera(const std::string& id, std::vector<std::string>* physicalCameraIds);
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700322
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700323 status_t getSystemCameraKind(const std::string& id, SystemCameraKind *kind) const;
324 bool isHiddenPhysicalCamera(const std::string& cameraId) const;
Emilian Peev538c90e2018-12-17 18:03:19 +0000325
326 static const float kDepthARTolerance;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800327private:
328 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
329 mutable std::mutex mInterfaceMutex;
330
331 wp<StatusListener> mListener;
332 ServiceInteractionProxy* mServiceProxy;
333
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800334 // Current overall Android device physical status
335 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
336
Shuzhen Wang6ba8eb22018-07-08 13:10:44 -0700337 // mProviderLifecycleLock is locked during onRegistration and removeProvider
338 mutable std::mutex mProviderLifecycleLock;
339
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800340 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
341
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700342 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
343 // ICameraProvider alive while it is in use by the camera with the given ID for camera
344 // capabilities
345 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
346 mCameraProviderByCameraId;
347
348 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
349 // ICameraProvider alive while it is in use by the camera with the given ID for torch
350 // capabilities
351 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
352 mTorchProviderByCameraId;
353
354 // Lock for accessing mCameraProviderByCameraId and mTorchProviderByCameraId
355 std::mutex mProviderInterfaceMapLock;
356
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700357 struct ProviderInfo :
Shuzhen Wang43858162020-01-10 13:42:15 -0800358 virtual public hardware::camera::provider::V2_6::ICameraProviderCallback,
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700359 virtual public hardware::hidl_death_recipient
360 {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800361 const std::string mProviderName;
Emilian Peev71c73a22017-03-21 16:35:51 +0000362 const metadata_vendor_id_t mProviderTagid;
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800363 int mMinorVersion;
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800364 sp<VendorTagDescriptor> mVendorTagDescriptor;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700365 bool mSetTorchModeSupported;
366 bool mIsRemote;
367
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800368 // Current overall Android device physical status
369 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
370
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700371 // This pointer is used to keep a reference to the ICameraProvider that was last accessed.
372 wp<hardware::camera::provider::V2_4::ICameraProvider> mActiveInterface;
373
374 sp<hardware::camera::provider::V2_4::ICameraProvider> mSavedInterface;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800375
376 ProviderInfo(const std::string &providerName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800377 CameraProviderManager *manager);
378 ~ProviderInfo();
379
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800380 status_t initialize(sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
381 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
382 currentDeviceState);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700383
384 const sp<hardware::camera::provider::V2_4::ICameraProvider> startProviderInterface();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800385
386 const std::string& getType() const;
387
388 status_t addDevice(const std::string& name,
389 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
390 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
391 /*out*/ std::string *parsedId = nullptr);
392
393 status_t dump(int fd, const Vector<String16>& args) const;
394
395 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
Shuzhen Wang43858162020-01-10 13:42:15 -0800396 hardware::Return<void> cameraDeviceStatusChange(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800397 const hardware::hidl_string& cameraDeviceName,
398 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
Shuzhen Wang43858162020-01-10 13:42:15 -0800399 hardware::Return<void> torchModeStatusChange(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800400 const hardware::hidl_string& cameraDeviceName,
401 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
Shuzhen Wang43858162020-01-10 13:42:15 -0800402 hardware::Return<void> physicalCameraDeviceStatusChange(
403 const hardware::hidl_string& cameraDeviceName,
404 const hardware::hidl_string& physicalCameraDeviceName,
405 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800406
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700407 // hidl_death_recipient interface - this locks the parent mInterfaceMutex
408 virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
409
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800410 /**
411 * Setup vendor tags for this provider
412 */
413 status_t setUpVendorTags();
414
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800415 /**
416 * Notify provider about top-level device physical state changes
417 */
418 status_t notifyDeviceStateChange(
419 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
420 newDeviceState);
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800421
422 std::vector<std::unordered_set<std::string>> getConcurrentCameraIdCombinations();
423
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800424 /**
425 * Query the camera provider for concurrent stream configuration support
426 */
427 status_t isConcurrentSessionConfigurationSupported(
428 const hardware::hidl_vec<
429 hardware::camera::provider::V2_6::CameraIdAndStreamCombination>
430 &halCameraIdsAndStreamCombinations,
431 bool *isSupported);
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800432
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800433 // Basic device information, common to all camera devices
434 struct DeviceInfo {
435 const std::string mName; // Full instance name
436 const std::string mId; // ID section of full name
437 const hardware::hidl_version mVersion;
Emilian Peev71c73a22017-03-21 16:35:51 +0000438 const metadata_vendor_id_t mProviderTagid;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700439 bool mIsLogicalCamera;
440 std::vector<std::string> mPhysicalIds;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700441 hardware::CameraInfo mInfo;
442 sp<IBase> mSavedInterface;
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700443 SystemCameraKind mSystemCameraKind = SystemCameraKind::PUBLIC;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800444
445 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
446
447 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
Shuzhen Wang43858162020-01-10 13:42:15 -0800448 std::map<std::string, hardware::camera::common::V1_0::CameraDeviceStatus>
449 mPhysicalStatus;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800450
Shuzhen Wang79680432020-03-05 11:53:46 -0800451 wp<ProviderInfo> mParentProvider;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700452
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800453 bool hasFlashUnit() const { return mHasFlashUnit; }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800454 bool supportNativeZoomRatio() const { return mSupportNativeZoomRatio; }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800455 virtual status_t setTorchMode(bool enabled) = 0;
456 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100457 virtual bool isAPI1Compatible() const = 0;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700458 virtual status_t dumpState(int fd) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800459 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
460 (void) characteristics;
461 return INVALID_OPERATION;
462 }
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700463 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
464 CameraMetadata *characteristics) const {
465 (void) physicalCameraId;
466 (void) characteristics;
467 return INVALID_OPERATION;
468 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800469
Emilian Peev35ae8262018-11-08 13:11:32 +0000470 virtual status_t isSessionConfigurationSupported(
471 const hardware::camera::device::V3_4::StreamConfiguration &/*configuration*/,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700472 bool * /*status*/) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000473 return INVALID_OPERATION;
474 }
475
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700476 template<class InterfaceT>
477 sp<InterfaceT> startDeviceInterface();
478
Emilian Peev71c73a22017-03-21 16:35:51 +0000479 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
480 const std::string &id, const hardware::hidl_version& version,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700481 const std::vector<std::string>& publicCameraIds,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700482 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
483 sp<ProviderInfo> parentProvider) :
Emilian Peev71c73a22017-03-21 16:35:51 +0000484 mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700485 mIsLogicalCamera(false), mResourceCost(resourceCost),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800486 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700487 mParentProvider(parentProvider), mHasFlashUnit(false),
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800488 mSupportNativeZoomRatio(false), mPublicCameraIds(publicCameraIds) {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800489 virtual ~DeviceInfo();
490 protected:
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800491 bool mHasFlashUnit; // const after constructor
492 bool mSupportNativeZoomRatio; // const after constructor
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700493 const std::vector<std::string>& mPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800494
495 template<class InterfaceT>
496 static status_t setTorchMode(InterfaceT& interface, bool enabled);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700497
498 template<class InterfaceT>
499 status_t setTorchModeForDevice(bool enabled) {
500 // Don't save the ICameraProvider interface here because we assume that this was
501 // called from CameraProviderManager::setTorchMode(), which does save it.
502 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
503 return DeviceInfo::setTorchMode(interface, enabled);
504 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800505 };
506 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800507 std::unordered_set<std::string> mUniqueCameraIds;
Yin-Chia Yehe8e9e192017-03-16 15:23:51 -0700508 int mUniqueDeviceCount;
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700509 std::vector<std::string> mUniqueAPI1CompatibleCameraIds;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700510 // The initial public camera IDs published by the camera provider.
511 // Currently logical multi-camera is not supported for hot-plug camera.
512 // And we use this list to keep track of initial public camera IDs
513 // advertised by the provider, and to distinguish against "hidden"
514 // physical camera IDs.
515 std::vector<std::string> mProviderPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800516
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800517 // HALv1-specific camera fields, including the actual device interface
518 struct DeviceInfo1 : public DeviceInfo {
519 typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800520
521 virtual status_t setTorchMode(bool enabled) override;
522 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100523 //In case of Device1Info assume that we are always API1 compatible
524 virtual bool isAPI1Compatible() const override { return true; }
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700525 virtual status_t dumpState(int fd) override;
Emilian Peev71c73a22017-03-21 16:35:51 +0000526 DeviceInfo1(const std::string& name, const metadata_vendor_id_t tagId,
527 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800528 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700529 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700530 const std::vector<std::string>& publicCameraIds,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800531 sp<InterfaceT> interface);
532 virtual ~DeviceInfo1();
533 private:
534 CameraParameters2 mDefaultParameters;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700535 status_t cacheCameraInfo(sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800536 };
537
538 // HALv3-specific camera fields, including the actual device interface
539 struct DeviceInfo3 : public DeviceInfo {
540 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800541
542 virtual status_t setTorchMode(bool enabled) override;
543 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100544 virtual bool isAPI1Compatible() const override;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700545 virtual status_t dumpState(int fd) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800546 virtual status_t getCameraCharacteristics(
547 CameraMetadata *characteristics) const override;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700548 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
549 CameraMetadata *characteristics) const override;
Emilian Peev35ae8262018-11-08 13:11:32 +0000550 virtual status_t isSessionConfigurationSupported(
551 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
552 bool *status /*out*/)
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700553 override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800554
Emilian Peev71c73a22017-03-21 16:35:51 +0000555 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
556 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800557 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700558 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700559 const std::vector<std::string>& publicCameraIds, sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800560 virtual ~DeviceInfo3();
561 private:
562 CameraMetadata mCameraCharacteristics;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700563 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700564 void queryPhysicalCameraIds();
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700565 SystemCameraKind getSystemCameraKind();
Shuzhen Wang268a1362018-10-16 16:32:59 -0700566 status_t fixupMonochromeTags();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000567 status_t addDynamicDepthTags();
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800568 status_t deriveHeicTags();
569 status_t addRotateCropTags();
Shuzhen Wang9bf8a6f2020-05-01 09:49:04 -0700570 status_t addPreCorrectionActiveArraySize();
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800571
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000572 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag,
573 android_pixel_format_t format,
574 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/);
575 void getSupportedDurations( const CameraMetadata& ch, uint32_t tag,
576 android_pixel_format_t format,
577 const std::vector<std::tuple<size_t, size_t>>& sizes,
578 std::vector<int64_t> *durations/*out*/);
579 void getSupportedDynamicDepthDurations(const std::vector<int64_t>& depthDurations,
580 const std::vector<int64_t>& blobDurations,
581 std::vector<int64_t> *dynamicDepthDurations /*out*/);
582 static void getSupportedDynamicDepthSizes(
583 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
584 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
585 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
586 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/);
Shuzhen Wang268a1362018-10-16 16:32:59 -0700587 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys,
588 uint32_t keyTag);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800589 status_t fillHeicStreamCombinations(std::vector<int32_t>* outputs,
590 std::vector<int64_t>* durations,
591 std::vector<int64_t>* stallDurations,
592 const camera_metadata_entry& halStreamConfigs,
593 const camera_metadata_entry& halStreamDurations);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800594 };
595
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800596 private:
597 std::string mType;
598 uint32_t mId;
599
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700600 std::mutex mLock;
601
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800602 CameraProviderManager *mManager;
603
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800604 bool mInitialized = false;
605
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800606 std::vector<std::unordered_set<std::string>> mConcurrentCameraIdCombinations;
607
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800608 // Templated method to instantiate the right kind of DeviceInfo and call the
609 // right CameraProvider getCameraDeviceInterface_* method.
610 template<class DeviceInfoT>
611 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
Emilian Peev71c73a22017-03-21 16:35:51 +0000612 const metadata_vendor_id_t tagId, const std::string &id,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700613 uint16_t minorVersion);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800614
615 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
616 template<class InterfaceT>
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700617 sp<InterfaceT> startDeviceInterface(const std::string &name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800618
619 // Parse provider instance name for type and id
620 static status_t parseProviderName(const std::string& name,
621 std::string *type, uint32_t *id);
622
623 // Parse device instance name for device version, type, and id.
624 static status_t parseDeviceName(const std::string& name,
625 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
Emilian Peev71c73a22017-03-21 16:35:51 +0000626
627 // Generate vendor tag id
628 static metadata_vendor_id_t generateVendorTagId(const std::string &name);
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100629
630 void removeDevice(std::string id);
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800631
632 // Expects to have mLock locked
633 status_t reCacheConcurrentStreamingCameraIdsLocked();
634 // Expects to have mLock locked
Jayant Chowdharycad23c22020-03-10 15:04:59 -0700635 status_t getConcurrentCameraIdsInternalLocked(
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800636 sp<hardware::camera::provider::V2_6::ICameraProvider> &interface2_6);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800637 };
638
639 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
640 // and the calling code doesn't mutate the list of providers or their lists of devices.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800641 // Finds the first device of the given ID that falls within the requested version range
642 // minVersion <= deviceVersion < maxVersion
643 // No guarantees on the order of traversal
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800644 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800645 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
646 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800647
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700648 status_t addProviderLocked(const std::string& newProvider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700649
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800650 bool isLogicalCameraLocked(const std::string& id, std::vector<std::string>* physicalCameraIds);
651
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800652 status_t removeProvider(const std::string& provider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700653 sp<StatusListener> getStatusListener() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800654
655 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
656
657 std::vector<sp<ProviderInfo>> mProviders;
658
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700659 void addProviderToMap(
660 const std::string &cameraId,
661 sp<hardware::camera::provider::V2_4::ICameraProvider> provider,
662 bool isTorchUsage);
663 void removeCameraIdFromMap(
664 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> &map,
665 const std::string &cameraId);
666
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800667 static const char* deviceStatusToString(
668 const hardware::camera::common::V1_0::CameraDeviceStatus&);
669 static const char* torchStatusToString(
670 const hardware::camera::common::V1_0::TorchModeStatus&);
671
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700672 status_t getCameraCharacteristicsLocked(const std::string &id,
673 CameraMetadata* characteristics) const;
674 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700675
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700676 status_t getSystemCameraKindLocked(const std::string& id, SystemCameraKind *kind) const;
677 std::pair<bool, ProviderInfo::DeviceInfo *> isHiddenPhysicalCameraInternal(const std::string& cameraId) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700678
679 void collectDeviceIdsLocked(const std::vector<std::string> deviceIds,
680 std::vector<std::string>& normalDeviceIds,
681 std::vector<std::string>& systemCameraDeviceIds) const;
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800682
683 status_t convertToHALStreamCombinationAndCameraIdsLocked(
684 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration>
685 &cameraIdsAndSessionConfigs,
686 hardware::hidl_vec<hardware::camera::provider::V2_6::CameraIdAndStreamCombination>
687 *halCameraIdsAndStreamCombinations,
688 bool *earlyExit);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800689};
690
691} // namespace android
692
693#endif