blob: 42da2278ca81b9e59cdde92beecdadf6a56820e7 [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,
146 hardware::camera::common::V1_0::TorchModeStatus newStatus) = 0;
Emilian Peevaee727d2017-05-04 16:35:48 +0100147 virtual void onNewProviderRegistered() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800148 };
149
150 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700151 * Represents the mode a camera device is currently in
152 */
153 enum class DeviceMode {
154 TORCH,
155 CAMERA
156 };
157
158 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800159 * Initialize the manager and give it a status listener; optionally accepts a service
160 * interaction proxy.
161 *
162 * The default proxy communicates via the hardware service manager; alternate proxies can be
163 * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
164 */
165 status_t initialize(wp<StatusListener> listener,
166 ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);
167
168 /**
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700169 * Retrieve the total number of available cameras.
170 * This value may change dynamically as cameras are added or removed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800171 */
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700172 std::pair<int, int> getCameraCount() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800173
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800174 std::vector<std::string> getCameraDeviceIds() const;
175
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800176 /**
Emilian Peevf53f66e2017-04-11 14:29:43 +0100177 * Retrieve the number of API1 compatible cameras; these are internal and
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800178 * backwards-compatible. This is the set of cameras that will be
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800179 * accessible via the old camera API.
180 * The return value may change dynamically due to external camera hotplug.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800181 */
Emilian Peevf53f66e2017-04-11 14:29:43 +0100182 std::vector<std::string> getAPI1CompatibleCameraDeviceIds() const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700183
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800184 /**
185 * Return true if a device with a given ID and major version exists
186 */
187 bool isValidDevice(const std::string &id, uint16_t majorVersion) const;
188
189 /**
190 * Return true if a device with a given ID has a flash unit. Returns false
191 * for devices that are unknown.
192 */
193 bool hasFlashUnit(const std::string &id) const;
194
195 /**
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800196 * Return true if the camera device has native zoom ratio support.
197 */
198 bool supportNativeZoomRatio(const std::string &id) const;
199
200 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800201 * Return the resource cost of this camera device
202 */
203 status_t getResourceCost(const std::string &id,
204 hardware::camera::common::V1_0::CameraResourceCost* cost) const;
205
206 /**
207 * Return the old camera API camera info
208 */
209 status_t getCameraInfo(const std::string &id,
210 hardware::CameraInfo* info) const;
211
212 /**
213 * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does
214 * not have a v3 or newer HAL version.
215 */
216 status_t getCameraCharacteristics(const std::string &id,
217 CameraMetadata* characteristics) const;
218
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800219 status_t isConcurrentSessionConfigurationSupported(
220 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration>
221 &cameraIdsAndSessionConfigs,
222 bool *isSupported);
223
224 std::vector<std::unordered_set<std::string>> getConcurrentStreamingCameraIds() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800225 /**
Emilian Peev35ae8262018-11-08 13:11:32 +0000226 * Check for device support of specific stream combination.
227 */
228 status_t isSessionConfigurationSupported(const std::string& id,
229 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
230 bool *status /*out*/) const;
231
232 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800233 * Return the highest supported device interface version for this ID
234 */
235 status_t getHighestSupportedVersion(const std::string &id,
236 hardware::hidl_version *v);
237
238 /**
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700239 * Check if a given camera device support setTorchMode API.
240 */
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700241 bool supportSetTorchMode(const std::string &id) const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700242
243 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800244 * Turn on or off the flashlight on a given camera device.
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700245 * May fail if the device does not support this API, is in active use, or if the device
246 * doesn't exist, etc.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800247 */
248 status_t setTorchMode(const std::string &id, bool enabled);
249
250 /**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800251 * Setup vendor tags for all registered providers
252 */
253 status_t setUpVendorTags();
254
255 /**
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800256 * Inform registered providers about a device state change, such as folding or unfolding
257 */
258 status_t notifyDeviceStateChange(
259 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> newState);
260
261 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800262 * Open an active session to a camera device.
263 *
264 * This fully powers on the camera device hardware, and returns a handle to a
265 * session to be used for hardware configuration and operation.
266 */
267 status_t openSession(const std::string &id,
268 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
269 /*out*/
270 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session);
271
272 status_t openSession(const std::string &id,
273 const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback,
274 /*out*/
275 sp<hardware::camera::device::V1_0::ICameraDevice> *session);
276
277 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700278 * Save the ICameraProvider while it is being used by a camera or torch client
279 */
280 void saveRef(DeviceMode usageType, const std::string &cameraId,
281 sp<hardware::camera::provider::V2_4::ICameraProvider> provider);
282
283 /**
284 * Notify that the camera or torch is no longer being used by a camera client
285 */
286 void removeRef(DeviceMode usageType, const std::string &cameraId);
287
288 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800289 * IServiceNotification::onRegistration
290 * Invoked by the hardware service manager when a new camera provider is registered
291 */
292 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
293 const hardware::hidl_string& name,
294 bool preexisting) override;
295
296 /**
297 * Dump out information about available providers and devices
298 */
299 status_t dump(int fd, const Vector<String16>& args);
300
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800301 /**
302 * Conversion methods between HAL Status and status_t and strings
303 */
304 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
305 static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
306
Emilian Peev71c73a22017-03-21 16:35:51 +0000307 /*
308 * Return provider type for a specific device.
309 */
310 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id,
311 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
312 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
313
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700314 /*
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700315 * Check if a camera is a logical camera. And if yes, return
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700316 * the physical camera ids.
317 */
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700318 bool isLogicalCamera(const std::string& id, std::vector<std::string>* physicalCameraIds);
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700319
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700320 status_t getSystemCameraKind(const std::string& id, SystemCameraKind *kind) const;
321 bool isHiddenPhysicalCamera(const std::string& cameraId) const;
Emilian Peev538c90e2018-12-17 18:03:19 +0000322
323 static const float kDepthARTolerance;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800324private:
325 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
326 mutable std::mutex mInterfaceMutex;
327
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800328 // the status listener update callbacks will lock mStatusMutex
329 mutable std::mutex mStatusListenerMutex;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800330 wp<StatusListener> mListener;
331 ServiceInteractionProxy* mServiceProxy;
332
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800333 // Current overall Android device physical status
334 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
335
Shuzhen Wang6ba8eb22018-07-08 13:10:44 -0700336 // mProviderLifecycleLock is locked during onRegistration and removeProvider
337 mutable std::mutex mProviderLifecycleLock;
338
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800339 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
340
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700341 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
342 // ICameraProvider alive while it is in use by the camera with the given ID for camera
343 // capabilities
344 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
345 mCameraProviderByCameraId;
346
347 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
348 // ICameraProvider alive while it is in use by the camera with the given ID for torch
349 // capabilities
350 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
351 mTorchProviderByCameraId;
352
353 // Lock for accessing mCameraProviderByCameraId and mTorchProviderByCameraId
354 std::mutex mProviderInterfaceMapLock;
355
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700356 struct ProviderInfo :
Shuzhen Wang43858162020-01-10 13:42:15 -0800357 virtual public hardware::camera::provider::V2_6::ICameraProviderCallback,
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700358 virtual public hardware::hidl_death_recipient
359 {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800360 const std::string mProviderName;
Emilian Peev71c73a22017-03-21 16:35:51 +0000361 const metadata_vendor_id_t mProviderTagid;
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800362 int mMinorVersion;
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800363 sp<VendorTagDescriptor> mVendorTagDescriptor;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700364 bool mSetTorchModeSupported;
365 bool mIsRemote;
366
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800367 // Current overall Android device physical status
368 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
369
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700370 // This pointer is used to keep a reference to the ICameraProvider that was last accessed.
371 wp<hardware::camera::provider::V2_4::ICameraProvider> mActiveInterface;
372
373 sp<hardware::camera::provider::V2_4::ICameraProvider> mSavedInterface;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800374
375 ProviderInfo(const std::string &providerName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800376 CameraProviderManager *manager);
377 ~ProviderInfo();
378
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800379 status_t initialize(sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
380 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
381 currentDeviceState);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700382
383 const sp<hardware::camera::provider::V2_4::ICameraProvider> startProviderInterface();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800384
385 const std::string& getType() const;
386
387 status_t addDevice(const std::string& name,
388 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
389 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
390 /*out*/ std::string *parsedId = nullptr);
391
392 status_t dump(int fd, const Vector<String16>& args) const;
393
394 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
Shuzhen Wang43858162020-01-10 13:42:15 -0800395 hardware::Return<void> cameraDeviceStatusChange(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800396 const hardware::hidl_string& cameraDeviceName,
397 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
Shuzhen Wang43858162020-01-10 13:42:15 -0800398 hardware::Return<void> torchModeStatusChange(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800399 const hardware::hidl_string& cameraDeviceName,
400 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
Shuzhen Wang43858162020-01-10 13:42:15 -0800401 hardware::Return<void> physicalCameraDeviceStatusChange(
402 const hardware::hidl_string& cameraDeviceName,
403 const hardware::hidl_string& physicalCameraDeviceName,
404 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800405
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700406 // hidl_death_recipient interface - this locks the parent mInterfaceMutex
407 virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
408
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800409 /**
410 * Setup vendor tags for this provider
411 */
412 status_t setUpVendorTags();
413
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800414 /**
415 * Notify provider about top-level device physical state changes
416 */
417 status_t notifyDeviceStateChange(
418 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
419 newDeviceState);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800420 /**
421 * Query the camera provider for concurrent stream configuration support
422 */
423 status_t isConcurrentSessionConfigurationSupported(
424 const hardware::hidl_vec<
425 hardware::camera::provider::V2_6::CameraIdAndStreamCombination>
426 &halCameraIdsAndStreamCombinations,
427 bool *isSupported);
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800428
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800429 // Basic device information, common to all camera devices
430 struct DeviceInfo {
431 const std::string mName; // Full instance name
432 const std::string mId; // ID section of full name
433 const hardware::hidl_version mVersion;
Emilian Peev71c73a22017-03-21 16:35:51 +0000434 const metadata_vendor_id_t mProviderTagid;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700435 bool mIsLogicalCamera;
436 std::vector<std::string> mPhysicalIds;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700437 hardware::CameraInfo mInfo;
438 sp<IBase> mSavedInterface;
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700439 SystemCameraKind mSystemCameraKind = SystemCameraKind::PUBLIC;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800440
441 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
442
443 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
Shuzhen Wang43858162020-01-10 13:42:15 -0800444 std::map<std::string, hardware::camera::common::V1_0::CameraDeviceStatus>
445 mPhysicalStatus;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800446
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700447 sp<ProviderInfo> mParentProvider;
448
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800449 bool hasFlashUnit() const { return mHasFlashUnit; }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800450 bool supportNativeZoomRatio() const { return mSupportNativeZoomRatio; }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800451 virtual status_t setTorchMode(bool enabled) = 0;
452 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100453 virtual bool isAPI1Compatible() const = 0;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700454 virtual status_t dumpState(int fd) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800455 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
456 (void) characteristics;
457 return INVALID_OPERATION;
458 }
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700459 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
460 CameraMetadata *characteristics) const {
461 (void) physicalCameraId;
462 (void) characteristics;
463 return INVALID_OPERATION;
464 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800465
Emilian Peev35ae8262018-11-08 13:11:32 +0000466 virtual status_t isSessionConfigurationSupported(
467 const hardware::camera::device::V3_4::StreamConfiguration &/*configuration*/,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700468 bool * /*status*/) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000469 return INVALID_OPERATION;
470 }
471
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700472 template<class InterfaceT>
473 sp<InterfaceT> startDeviceInterface();
474
Emilian Peev71c73a22017-03-21 16:35:51 +0000475 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
476 const std::string &id, const hardware::hidl_version& version,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700477 const std::vector<std::string>& publicCameraIds,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700478 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
479 sp<ProviderInfo> parentProvider) :
Emilian Peev71c73a22017-03-21 16:35:51 +0000480 mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700481 mIsLogicalCamera(false), mResourceCost(resourceCost),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800482 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700483 mParentProvider(parentProvider), mHasFlashUnit(false),
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800484 mSupportNativeZoomRatio(false), mPublicCameraIds(publicCameraIds) {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800485 virtual ~DeviceInfo();
486 protected:
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800487 bool mHasFlashUnit; // const after constructor
488 bool mSupportNativeZoomRatio; // const after constructor
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700489 const std::vector<std::string>& mPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800490
491 template<class InterfaceT>
492 static status_t setTorchMode(InterfaceT& interface, bool enabled);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700493
494 template<class InterfaceT>
495 status_t setTorchModeForDevice(bool enabled) {
496 // Don't save the ICameraProvider interface here because we assume that this was
497 // called from CameraProviderManager::setTorchMode(), which does save it.
498 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
499 return DeviceInfo::setTorchMode(interface, enabled);
500 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800501 };
502 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800503 std::unordered_set<std::string> mUniqueCameraIds;
Yin-Chia Yehe8e9e192017-03-16 15:23:51 -0700504 int mUniqueDeviceCount;
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700505 std::vector<std::string> mUniqueAPI1CompatibleCameraIds;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700506 // The initial public camera IDs published by the camera provider.
507 // Currently logical multi-camera is not supported for hot-plug camera.
508 // And we use this list to keep track of initial public camera IDs
509 // advertised by the provider, and to distinguish against "hidden"
510 // physical camera IDs.
511 std::vector<std::string> mProviderPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800512
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800513 std::vector<std::unordered_set<std::string>> mConcurrentCameraIdCombinations;
514
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800515 // HALv1-specific camera fields, including the actual device interface
516 struct DeviceInfo1 : public DeviceInfo {
517 typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800518
519 virtual status_t setTorchMode(bool enabled) override;
520 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100521 //In case of Device1Info assume that we are always API1 compatible
522 virtual bool isAPI1Compatible() const override { return true; }
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700523 virtual status_t dumpState(int fd) override;
Emilian Peev71c73a22017-03-21 16:35:51 +0000524 DeviceInfo1(const std::string& name, const metadata_vendor_id_t tagId,
525 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800526 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700527 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700528 const std::vector<std::string>& publicCameraIds,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800529 sp<InterfaceT> interface);
530 virtual ~DeviceInfo1();
531 private:
532 CameraParameters2 mDefaultParameters;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700533 status_t cacheCameraInfo(sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800534 };
535
536 // HALv3-specific camera fields, including the actual device interface
537 struct DeviceInfo3 : public DeviceInfo {
538 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800539
540 virtual status_t setTorchMode(bool enabled) override;
541 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100542 virtual bool isAPI1Compatible() const override;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700543 virtual status_t dumpState(int fd) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800544 virtual status_t getCameraCharacteristics(
545 CameraMetadata *characteristics) const override;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700546 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
547 CameraMetadata *characteristics) const override;
Emilian Peev35ae8262018-11-08 13:11:32 +0000548 virtual status_t isSessionConfigurationSupported(
549 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
550 bool *status /*out*/)
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700551 override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800552
Emilian Peev71c73a22017-03-21 16:35:51 +0000553 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
554 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800555 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700556 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700557 const std::vector<std::string>& publicCameraIds, sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800558 virtual ~DeviceInfo3();
559 private:
560 CameraMetadata mCameraCharacteristics;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700561 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700562 void queryPhysicalCameraIds();
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700563 SystemCameraKind getSystemCameraKind();
Shuzhen Wang268a1362018-10-16 16:32:59 -0700564 status_t fixupMonochromeTags();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000565 status_t addDynamicDepthTags();
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800566 status_t deriveHeicTags();
567 status_t addRotateCropTags();
568
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000569 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag,
570 android_pixel_format_t format,
571 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/);
572 void getSupportedDurations( const CameraMetadata& ch, uint32_t tag,
573 android_pixel_format_t format,
574 const std::vector<std::tuple<size_t, size_t>>& sizes,
575 std::vector<int64_t> *durations/*out*/);
576 void getSupportedDynamicDepthDurations(const std::vector<int64_t>& depthDurations,
577 const std::vector<int64_t>& blobDurations,
578 std::vector<int64_t> *dynamicDepthDurations /*out*/);
579 static void getSupportedDynamicDepthSizes(
580 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
581 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
582 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
583 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/);
Shuzhen Wang268a1362018-10-16 16:32:59 -0700584 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys,
585 uint32_t keyTag);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800586 status_t fillHeicStreamCombinations(std::vector<int32_t>* outputs,
587 std::vector<int64_t>* durations,
588 std::vector<int64_t>* stallDurations,
589 const camera_metadata_entry& halStreamConfigs,
590 const camera_metadata_entry& halStreamDurations);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800591 };
592
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800593 private:
594 std::string mType;
595 uint32_t mId;
596
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700597 std::mutex mLock;
598
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800599 CameraProviderManager *mManager;
600
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800601 bool mInitialized = false;
602
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800603 // Templated method to instantiate the right kind of DeviceInfo and call the
604 // right CameraProvider getCameraDeviceInterface_* method.
605 template<class DeviceInfoT>
606 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
Emilian Peev71c73a22017-03-21 16:35:51 +0000607 const metadata_vendor_id_t tagId, const std::string &id,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700608 uint16_t minorVersion);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800609
610 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
611 template<class InterfaceT>
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700612 sp<InterfaceT> startDeviceInterface(const std::string &name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800613
614 // Parse provider instance name for type and id
615 static status_t parseProviderName(const std::string& name,
616 std::string *type, uint32_t *id);
617
618 // Parse device instance name for device version, type, and id.
619 static status_t parseDeviceName(const std::string& name,
620 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
Emilian Peev71c73a22017-03-21 16:35:51 +0000621
622 // Generate vendor tag id
623 static metadata_vendor_id_t generateVendorTagId(const std::string &name);
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100624
625 void removeDevice(std::string id);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800626 };
627
628 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
629 // and the calling code doesn't mutate the list of providers or their lists of devices.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800630 // Finds the first device of the given ID that falls within the requested version range
631 // minVersion <= deviceVersion < maxVersion
632 // No guarantees on the order of traversal
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800633 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800634 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
635 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800636
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700637 status_t addProviderLocked(const std::string& newProvider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700638
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800639 bool isLogicalCameraLocked(const std::string& id, std::vector<std::string>* physicalCameraIds);
640
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800641 status_t removeProvider(const std::string& provider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700642 sp<StatusListener> getStatusListener() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800643
644 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
645
646 std::vector<sp<ProviderInfo>> mProviders;
647
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700648 void addProviderToMap(
649 const std::string &cameraId,
650 sp<hardware::camera::provider::V2_4::ICameraProvider> provider,
651 bool isTorchUsage);
652 void removeCameraIdFromMap(
653 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> &map,
654 const std::string &cameraId);
655
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800656 static const char* deviceStatusToString(
657 const hardware::camera::common::V1_0::CameraDeviceStatus&);
658 static const char* torchStatusToString(
659 const hardware::camera::common::V1_0::TorchModeStatus&);
660
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700661 status_t getCameraCharacteristicsLocked(const std::string &id,
662 CameraMetadata* characteristics) const;
663 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700664
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700665 status_t getSystemCameraKindLocked(const std::string& id, SystemCameraKind *kind) const;
666 std::pair<bool, ProviderInfo::DeviceInfo *> isHiddenPhysicalCameraInternal(const std::string& cameraId) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700667
668 void collectDeviceIdsLocked(const std::vector<std::string> deviceIds,
669 std::vector<std::string>& normalDeviceIds,
670 std::vector<std::string>& systemCameraDeviceIds) const;
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800671
672 status_t convertToHALStreamCombinationAndCameraIdsLocked(
673 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration>
674 &cameraIdsAndSessionConfigs,
675 hardware::hidl_vec<hardware::camera::provider::V2_6::CameraIdAndStreamCombination>
676 *halCameraIdsAndStreamCombinations,
677 bool *earlyExit);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800678};
679
680} // namespace android
681
682#endif