blob: 12da5e6e336ed2c8c667c92af7dba82ba5afddf4 [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
Jayant Chowdharycad23c22020-03-10 15:04:59 -0700224 std::vector<std::unordered_set<std::string>> getConcurrentCameraIds() 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
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800272 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700273 * Save the ICameraProvider while it is being used by a camera or torch client
274 */
275 void saveRef(DeviceMode usageType, const std::string &cameraId,
276 sp<hardware::camera::provider::V2_4::ICameraProvider> provider);
277
278 /**
279 * Notify that the camera or torch is no longer being used by a camera client
280 */
281 void removeRef(DeviceMode usageType, const std::string &cameraId);
282
283 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800284 * IServiceNotification::onRegistration
285 * Invoked by the hardware service manager when a new camera provider is registered
286 */
287 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
288 const hardware::hidl_string& name,
289 bool preexisting) override;
290
291 /**
292 * Dump out information about available providers and devices
293 */
294 status_t dump(int fd, const Vector<String16>& args);
295
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800296 /**
297 * Conversion methods between HAL Status and status_t and strings
298 */
299 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
300 static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
301
Emilian Peev71c73a22017-03-21 16:35:51 +0000302 /*
303 * Return provider type for a specific device.
304 */
305 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id,
306 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
307 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
308
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700309 /*
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700310 * Check if a camera is a logical camera. And if yes, return
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700311 * the physical camera ids.
312 */
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700313 bool isLogicalCamera(const std::string& id, std::vector<std::string>* physicalCameraIds);
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700314
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700315 status_t getSystemCameraKind(const std::string& id, SystemCameraKind *kind) const;
316 bool isHiddenPhysicalCamera(const std::string& cameraId) const;
Emilian Peev538c90e2018-12-17 18:03:19 +0000317
318 static const float kDepthARTolerance;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800319private:
320 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
321 mutable std::mutex mInterfaceMutex;
322
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800323 // the status listener update callbacks will lock mStatusMutex
324 mutable std::mutex mStatusListenerMutex;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800325 wp<StatusListener> mListener;
326 ServiceInteractionProxy* mServiceProxy;
327
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800328 // Current overall Android device physical status
329 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
330
Shuzhen Wang6ba8eb22018-07-08 13:10:44 -0700331 // mProviderLifecycleLock is locked during onRegistration and removeProvider
332 mutable std::mutex mProviderLifecycleLock;
333
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800334 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
335
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700336 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
337 // ICameraProvider alive while it is in use by the camera with the given ID for camera
338 // capabilities
339 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
340 mCameraProviderByCameraId;
341
342 // 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 torch
344 // capabilities
345 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
346 mTorchProviderByCameraId;
347
348 // Lock for accessing mCameraProviderByCameraId and mTorchProviderByCameraId
349 std::mutex mProviderInterfaceMapLock;
350
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700351 struct ProviderInfo :
Shuzhen Wang43858162020-01-10 13:42:15 -0800352 virtual public hardware::camera::provider::V2_6::ICameraProviderCallback,
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700353 virtual public hardware::hidl_death_recipient
354 {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800355 const std::string mProviderName;
Emilian Peev71c73a22017-03-21 16:35:51 +0000356 const metadata_vendor_id_t mProviderTagid;
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800357 int mMinorVersion;
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800358 sp<VendorTagDescriptor> mVendorTagDescriptor;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700359 bool mSetTorchModeSupported;
360 bool mIsRemote;
361
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800362 // Current overall Android device physical status
363 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
364
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700365 // This pointer is used to keep a reference to the ICameraProvider that was last accessed.
366 wp<hardware::camera::provider::V2_4::ICameraProvider> mActiveInterface;
367
368 sp<hardware::camera::provider::V2_4::ICameraProvider> mSavedInterface;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800369
370 ProviderInfo(const std::string &providerName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800371 CameraProviderManager *manager);
372 ~ProviderInfo();
373
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800374 status_t initialize(sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
375 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
376 currentDeviceState);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700377
378 const sp<hardware::camera::provider::V2_4::ICameraProvider> startProviderInterface();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800379
380 const std::string& getType() const;
381
382 status_t addDevice(const std::string& name,
383 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
384 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
385 /*out*/ std::string *parsedId = nullptr);
386
387 status_t dump(int fd, const Vector<String16>& args) const;
388
389 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
Shuzhen Wang43858162020-01-10 13:42:15 -0800390 hardware::Return<void> cameraDeviceStatusChange(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800391 const hardware::hidl_string& cameraDeviceName,
392 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
Shuzhen Wang43858162020-01-10 13:42:15 -0800393 hardware::Return<void> torchModeStatusChange(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800394 const hardware::hidl_string& cameraDeviceName,
395 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
Shuzhen Wang43858162020-01-10 13:42:15 -0800396 hardware::Return<void> physicalCameraDeviceStatusChange(
397 const hardware::hidl_string& cameraDeviceName,
398 const hardware::hidl_string& physicalCameraDeviceName,
399 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800400
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700401 // hidl_death_recipient interface - this locks the parent mInterfaceMutex
402 virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
403
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800404 /**
405 * Setup vendor tags for this provider
406 */
407 status_t setUpVendorTags();
408
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800409 /**
410 * Notify provider about top-level device physical state changes
411 */
412 status_t notifyDeviceStateChange(
413 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
414 newDeviceState);
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800415
416 std::vector<std::unordered_set<std::string>> getConcurrentCameraIdCombinations();
417
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800418 /**
419 * Query the camera provider for concurrent stream configuration support
420 */
421 status_t isConcurrentSessionConfigurationSupported(
422 const hardware::hidl_vec<
423 hardware::camera::provider::V2_6::CameraIdAndStreamCombination>
424 &halCameraIdsAndStreamCombinations,
425 bool *isSupported);
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800426
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800427 // Basic device information, common to all camera devices
428 struct DeviceInfo {
429 const std::string mName; // Full instance name
430 const std::string mId; // ID section of full name
431 const hardware::hidl_version mVersion;
Emilian Peev71c73a22017-03-21 16:35:51 +0000432 const metadata_vendor_id_t mProviderTagid;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700433 bool mIsLogicalCamera;
434 std::vector<std::string> mPhysicalIds;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700435 hardware::CameraInfo mInfo;
436 sp<IBase> mSavedInterface;
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700437 SystemCameraKind mSystemCameraKind = SystemCameraKind::PUBLIC;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800438
439 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
440
441 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
442
Shuzhen Wang79680432020-03-05 11:53:46 -0800443 wp<ProviderInfo> mParentProvider;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700444
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800445 bool hasFlashUnit() const { return mHasFlashUnit; }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800446 bool supportNativeZoomRatio() const { return mSupportNativeZoomRatio; }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800447 virtual status_t setTorchMode(bool enabled) = 0;
448 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100449 virtual bool isAPI1Compatible() const = 0;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700450 virtual status_t dumpState(int fd) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800451 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
452 (void) characteristics;
453 return INVALID_OPERATION;
454 }
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700455 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
456 CameraMetadata *characteristics) const {
457 (void) physicalCameraId;
458 (void) characteristics;
459 return INVALID_OPERATION;
460 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800461
Emilian Peev35ae8262018-11-08 13:11:32 +0000462 virtual status_t isSessionConfigurationSupported(
463 const hardware::camera::device::V3_4::StreamConfiguration &/*configuration*/,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700464 bool * /*status*/) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000465 return INVALID_OPERATION;
466 }
467
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700468 template<class InterfaceT>
469 sp<InterfaceT> startDeviceInterface();
470
Emilian Peev71c73a22017-03-21 16:35:51 +0000471 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
472 const std::string &id, const hardware::hidl_version& version,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700473 const std::vector<std::string>& publicCameraIds,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700474 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
475 sp<ProviderInfo> parentProvider) :
Emilian Peev71c73a22017-03-21 16:35:51 +0000476 mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700477 mIsLogicalCamera(false), mResourceCost(resourceCost),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800478 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700479 mParentProvider(parentProvider), mHasFlashUnit(false),
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800480 mSupportNativeZoomRatio(false), mPublicCameraIds(publicCameraIds) {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800481 virtual ~DeviceInfo();
482 protected:
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800483 bool mHasFlashUnit; // const after constructor
484 bool mSupportNativeZoomRatio; // const after constructor
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700485 const std::vector<std::string>& mPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800486
487 template<class InterfaceT>
488 static status_t setTorchMode(InterfaceT& interface, bool enabled);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700489
490 template<class InterfaceT>
491 status_t setTorchModeForDevice(bool enabled) {
492 // Don't save the ICameraProvider interface here because we assume that this was
493 // called from CameraProviderManager::setTorchMode(), which does save it.
494 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
495 return DeviceInfo::setTorchMode(interface, enabled);
496 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800497 };
498 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800499 std::unordered_set<std::string> mUniqueCameraIds;
Yin-Chia Yehe8e9e192017-03-16 15:23:51 -0700500 int mUniqueDeviceCount;
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700501 std::vector<std::string> mUniqueAPI1CompatibleCameraIds;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700502 // The initial public camera IDs published by the camera provider.
503 // Currently logical multi-camera is not supported for hot-plug camera.
504 // And we use this list to keep track of initial public camera IDs
505 // advertised by the provider, and to distinguish against "hidden"
506 // physical camera IDs.
507 std::vector<std::string> mProviderPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800508
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800509 // HALv3-specific camera fields, including the actual device interface
510 struct DeviceInfo3 : public DeviceInfo {
511 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800512
513 virtual status_t setTorchMode(bool enabled) override;
514 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100515 virtual bool isAPI1Compatible() const override;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700516 virtual status_t dumpState(int fd) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800517 virtual status_t getCameraCharacteristics(
518 CameraMetadata *characteristics) const override;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700519 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
520 CameraMetadata *characteristics) const override;
Emilian Peev35ae8262018-11-08 13:11:32 +0000521 virtual status_t isSessionConfigurationSupported(
522 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
523 bool *status /*out*/)
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700524 override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800525
Emilian Peev71c73a22017-03-21 16:35:51 +0000526 DeviceInfo3(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, sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800531 virtual ~DeviceInfo3();
532 private:
533 CameraMetadata mCameraCharacteristics;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700534 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700535 void queryPhysicalCameraIds();
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700536 SystemCameraKind getSystemCameraKind();
Shuzhen Wang268a1362018-10-16 16:32:59 -0700537 status_t fixupMonochromeTags();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000538 status_t addDynamicDepthTags();
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800539 status_t deriveHeicTags();
540 status_t addRotateCropTags();
Shuzhen Wang9bf8a6f2020-05-01 09:49:04 -0700541 status_t addPreCorrectionActiveArraySize();
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800542
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000543 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag,
544 android_pixel_format_t format,
545 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/);
546 void getSupportedDurations( const CameraMetadata& ch, uint32_t tag,
547 android_pixel_format_t format,
548 const std::vector<std::tuple<size_t, size_t>>& sizes,
549 std::vector<int64_t> *durations/*out*/);
550 void getSupportedDynamicDepthDurations(const std::vector<int64_t>& depthDurations,
551 const std::vector<int64_t>& blobDurations,
552 std::vector<int64_t> *dynamicDepthDurations /*out*/);
553 static void getSupportedDynamicDepthSizes(
554 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
555 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
556 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
557 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/);
Shuzhen Wang268a1362018-10-16 16:32:59 -0700558 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys,
559 uint32_t keyTag);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800560 status_t fillHeicStreamCombinations(std::vector<int32_t>* outputs,
561 std::vector<int64_t>* durations,
562 std::vector<int64_t>* stallDurations,
563 const camera_metadata_entry& halStreamConfigs,
564 const camera_metadata_entry& halStreamDurations);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800565 };
566
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800567 private:
568 std::string mType;
569 uint32_t mId;
570
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700571 std::mutex mLock;
572
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800573 CameraProviderManager *mManager;
574
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800575 bool mInitialized = false;
576
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800577 std::vector<std::unordered_set<std::string>> mConcurrentCameraIdCombinations;
578
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800579 // Templated method to instantiate the right kind of DeviceInfo and call the
580 // right CameraProvider getCameraDeviceInterface_* method.
581 template<class DeviceInfoT>
582 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
Emilian Peev71c73a22017-03-21 16:35:51 +0000583 const metadata_vendor_id_t tagId, const std::string &id,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700584 uint16_t minorVersion);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800585
586 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
587 template<class InterfaceT>
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700588 sp<InterfaceT> startDeviceInterface(const std::string &name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800589
590 // Parse provider instance name for type and id
591 static status_t parseProviderName(const std::string& name,
592 std::string *type, uint32_t *id);
593
594 // Parse device instance name for device version, type, and id.
595 static status_t parseDeviceName(const std::string& name,
596 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
Emilian Peev71c73a22017-03-21 16:35:51 +0000597
598 // Generate vendor tag id
599 static metadata_vendor_id_t generateVendorTagId(const std::string &name);
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100600
601 void removeDevice(std::string id);
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800602
603 // Expects to have mLock locked
604 status_t reCacheConcurrentStreamingCameraIdsLocked();
605 // Expects to have mLock locked
Jayant Chowdharycad23c22020-03-10 15:04:59 -0700606 status_t getConcurrentCameraIdsInternalLocked(
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800607 sp<hardware::camera::provider::V2_6::ICameraProvider> &interface2_6);
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
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800621 bool isLogicalCameraLocked(const std::string& id, std::vector<std::string>* physicalCameraIds);
622
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800623 status_t removeProvider(const std::string& provider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700624 sp<StatusListener> getStatusListener() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800625
626 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
627
628 std::vector<sp<ProviderInfo>> mProviders;
629
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700630 void addProviderToMap(
631 const std::string &cameraId,
632 sp<hardware::camera::provider::V2_4::ICameraProvider> provider,
633 bool isTorchUsage);
634 void removeCameraIdFromMap(
635 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> &map,
636 const std::string &cameraId);
637
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800638 static const char* deviceStatusToString(
639 const hardware::camera::common::V1_0::CameraDeviceStatus&);
640 static const char* torchStatusToString(
641 const hardware::camera::common::V1_0::TorchModeStatus&);
642
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700643 status_t getCameraCharacteristicsLocked(const std::string &id,
644 CameraMetadata* characteristics) const;
645 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700646
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700647 status_t getSystemCameraKindLocked(const std::string& id, SystemCameraKind *kind) const;
648 std::pair<bool, ProviderInfo::DeviceInfo *> isHiddenPhysicalCameraInternal(const std::string& cameraId) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700649
650 void collectDeviceIdsLocked(const std::vector<std::string> deviceIds,
651 std::vector<std::string>& normalDeviceIds,
652 std::vector<std::string>& systemCameraDeviceIds) const;
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800653
654 status_t convertToHALStreamCombinationAndCameraIdsLocked(
655 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration>
656 &cameraIdsAndSessionConfigs,
657 hardware::hidl_vec<hardware::camera::provider::V2_6::CameraIdAndStreamCombination>
658 *halCameraIdsAndStreamCombinations,
659 bool *earlyExit);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800660};
661
662} // namespace android
663
664#endif