blob: 8727e7f6866b6e64dda0ac5371f1dcf0cf38dfd6 [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>
Shuzhen Wang394ad702020-07-23 13:01:54 -070025#include <future>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080026
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -080027#include <camera/camera2/ConcurrentCamera.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080028#include <camera/CameraParameters2.h>
29#include <camera/CameraMetadata.h>
30#include <camera/CameraBase.h>
31#include <utils/Errors.h>
32#include <android/hardware/camera/common/1.0/types.h>
Eino-Ville Talvala63f36112018-12-06 14:57:03 -080033#include <android/hardware/camera/provider/2.5/ICameraProvider.h>
Shuzhen Wang43858162020-01-10 13:42:15 -080034#include <android/hardware/camera/provider/2.6/ICameraProviderCallback.h>
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -080035#include <android/hardware/camera/provider/2.6/ICameraProvider.h>
Emilian Peev35ae8262018-11-08 13:11:32 +000036#include <android/hardware/camera/device/3.4/ICameraDeviceSession.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080037#include <android/hidl/manager/1.0/IServiceNotification.h>
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080038#include <camera/VendorTagDescriptor.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080039
40namespace android {
41
42/**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080043 * The vendor tag descriptor class that takes HIDL vendor tag information as
44 * input. Not part of VendorTagDescriptor class because that class is used
45 * in AIDL generated sources which don't have access to HIDL headers.
46 */
47class HidlVendorTagDescriptor : public VendorTagDescriptor {
48public:
49 /**
50 * Create a VendorTagDescriptor object from the HIDL VendorTagSection
51 * vector.
52 *
53 * Returns OK on success, or a negative error code.
54 */
55 static status_t createDescriptorFromHidl(
56 const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts,
57 /*out*/
58 sp<VendorTagDescriptor>& descriptor);
59};
60
Jayant Chowdhary5216b212019-07-17 09:26:23 -070061enum SystemCameraKind {
62 /**
63 * These camera devices are visible to all apps and system components alike
64 */
65 PUBLIC = 0,
66
67 /**
68 * These camera devices are visible only to processes having the
69 * android.permission.SYSTEM_CAMERA permission. They are not exposed to 3P
70 * apps.
71 */
72 SYSTEM_ONLY_CAMERA,
73
74 /**
75 * These camera devices are visible only to HAL clients (that try to connect
76 * on a hwbinder thread).
77 */
78 HIDDEN_SECURE_CAMERA
79};
80
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080081/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080082 * A manager for all camera providers available on an Android device.
83 *
84 * Responsible for enumerating providers and the individual camera devices
85 * they export, both at startup and as providers and devices are added/removed.
86 *
87 * Provides methods for requesting information about individual devices and for
88 * opening them for active use.
89 *
90 */
91class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification {
92public:
93
94 ~CameraProviderManager();
95
96 // Tiny proxy for the static methods in a HIDL interface that communicate with the hardware
97 // service manager, to be replacable in unit tests with a fake.
98 struct ServiceInteractionProxy {
99 virtual bool registerForNotifications(
100 const std::string &serviceName,
101 const sp<hidl::manager::V1_0::IServiceNotification>
102 &notification) = 0;
Eino-Ville Talvalaec960602019-10-15 11:46:16 -0700103 // Will not wait for service to start if it's not already running
104 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService(
105 const std::string &serviceName) = 0;
106 // Will block for service if it exists but isn't running
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800107 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
108 const std::string &serviceName) = 0;
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700109 virtual hardware::hidl_vec<hardware::hidl_string> listServices() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800110 virtual ~ServiceInteractionProxy() {}
111 };
112
113 // Standard use case - call into the normal generated static methods which invoke
114 // the real hardware service manager
115 struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
116 virtual bool registerForNotifications(
117 const std::string &serviceName,
118 const sp<hidl::manager::V1_0::IServiceNotification>
119 &notification) override {
120 return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
121 serviceName, notification);
122 }
Eino-Ville Talvalaec960602019-10-15 11:46:16 -0700123 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService(
124 const std::string &serviceName) override {
125 return hardware::camera::provider::V2_4::ICameraProvider::tryGetService(serviceName);
126 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800127 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
128 const std::string &serviceName) override {
129 return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
130 }
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700131
132 virtual hardware::hidl_vec<hardware::hidl_string> listServices() override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800133 };
134
135 /**
136 * Listener interface for device/torch status changes
137 */
138 struct StatusListener : virtual public RefBase {
139 ~StatusListener() {}
140
141 virtual void onDeviceStatusChanged(const String8 &cameraId,
142 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
Shuzhen Wang43858162020-01-10 13:42:15 -0800143 virtual void onDeviceStatusChanged(const String8 &cameraId,
144 const String8 &physicalCameraId,
145 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800146 virtual void onTorchStatusChanged(const String8 &cameraId,
147 hardware::camera::common::V1_0::TorchModeStatus newStatus) = 0;
Emilian Peevaee727d2017-05-04 16:35:48 +0100148 virtual void onNewProviderRegistered() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800149 };
150
151 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700152 * Represents the mode a camera device is currently in
153 */
154 enum class DeviceMode {
155 TORCH,
156 CAMERA
157 };
158
159 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800160 * Initialize the manager and give it a status listener; optionally accepts a service
161 * interaction proxy.
162 *
163 * The default proxy communicates via the hardware service manager; alternate proxies can be
164 * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
165 */
166 status_t initialize(wp<StatusListener> listener,
167 ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);
168
169 /**
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700170 * Retrieve the total number of available cameras.
171 * This value may change dynamically as cameras are added or removed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800172 */
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700173 std::pair<int, int> getCameraCount() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800174
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800175 std::vector<std::string> getCameraDeviceIds() const;
176
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800177 /**
Emilian Peevf53f66e2017-04-11 14:29:43 +0100178 * Retrieve the number of API1 compatible cameras; these are internal and
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800179 * backwards-compatible. This is the set of cameras that will be
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800180 * accessible via the old camera API.
181 * The return value may change dynamically due to external camera hotplug.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800182 */
Emilian Peevf53f66e2017-04-11 14:29:43 +0100183 std::vector<std::string> getAPI1CompatibleCameraDeviceIds() const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700184
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800185 /**
186 * Return true if a device with a given ID and major version exists
187 */
188 bool isValidDevice(const std::string &id, uint16_t majorVersion) const;
189
190 /**
191 * Return true if a device with a given ID has a flash unit. Returns false
192 * for devices that are unknown.
193 */
194 bool hasFlashUnit(const std::string &id) const;
195
196 /**
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800197 * Return true if the camera device has native zoom ratio support.
198 */
199 bool supportNativeZoomRatio(const std::string &id) const;
200
201 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800202 * Return the resource cost of this camera device
203 */
204 status_t getResourceCost(const std::string &id,
205 hardware::camera::common::V1_0::CameraResourceCost* cost) const;
206
207 /**
208 * Return the old camera API camera info
209 */
210 status_t getCameraInfo(const std::string &id,
211 hardware::CameraInfo* info) const;
212
213 /**
214 * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does
215 * not have a v3 or newer HAL version.
216 */
217 status_t getCameraCharacteristics(const std::string &id,
218 CameraMetadata* characteristics) const;
219
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800220 status_t isConcurrentSessionConfigurationSupported(
221 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration>
222 &cameraIdsAndSessionConfigs,
223 bool *isSupported);
224
Jayant Chowdharycad23c22020-03-10 15:04:59 -0700225 std::vector<std::unordered_set<std::string>> getConcurrentCameraIds() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800226 /**
Emilian Peev35ae8262018-11-08 13:11:32 +0000227 * Check for device support of specific stream combination.
228 */
229 status_t isSessionConfigurationSupported(const std::string& id,
230 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
231 bool *status /*out*/) const;
232
233 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800234 * Return the highest supported device interface version for this ID
235 */
236 status_t getHighestSupportedVersion(const std::string &id,
237 hardware::hidl_version *v);
238
239 /**
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700240 * Check if a given camera device support setTorchMode API.
241 */
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700242 bool supportSetTorchMode(const std::string &id) const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700243
244 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800245 * Turn on or off the flashlight on a given camera device.
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700246 * May fail if the device does not support this API, is in active use, or if the device
247 * doesn't exist, etc.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800248 */
249 status_t setTorchMode(const std::string &id, bool enabled);
250
251 /**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800252 * Setup vendor tags for all registered providers
253 */
254 status_t setUpVendorTags();
255
256 /**
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800257 * Inform registered providers about a device state change, such as folding or unfolding
258 */
259 status_t notifyDeviceStateChange(
260 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> newState);
261
262 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800263 * Open an active session to a camera device.
264 *
265 * This fully powers on the camera device hardware, and returns a handle to a
266 * session to be used for hardware configuration and operation.
267 */
268 status_t openSession(const std::string &id,
269 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
270 /*out*/
271 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session);
272
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800273 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700274 * Save the ICameraProvider while it is being used by a camera or torch client
275 */
276 void saveRef(DeviceMode usageType, const std::string &cameraId,
277 sp<hardware::camera::provider::V2_4::ICameraProvider> provider);
278
279 /**
280 * Notify that the camera or torch is no longer being used by a camera client
281 */
282 void removeRef(DeviceMode usageType, const std::string &cameraId);
283
284 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800285 * IServiceNotification::onRegistration
286 * Invoked by the hardware service manager when a new camera provider is registered
287 */
288 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
289 const hardware::hidl_string& name,
290 bool preexisting) override;
291
292 /**
293 * Dump out information about available providers and devices
294 */
295 status_t dump(int fd, const Vector<String16>& args);
296
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800297 /**
298 * Conversion methods between HAL Status and status_t and strings
299 */
300 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
301 static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
302
Emilian Peev71c73a22017-03-21 16:35:51 +0000303 /*
304 * Return provider type for a specific device.
305 */
306 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id,
307 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
308 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
309
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700310 /*
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700311 * Check if a camera is a logical camera. And if yes, return
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700312 * the physical camera ids.
313 */
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700314 bool isLogicalCamera(const std::string& id, std::vector<std::string>* physicalCameraIds);
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700315
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700316 status_t getSystemCameraKind(const std::string& id, SystemCameraKind *kind) const;
317 bool isHiddenPhysicalCamera(const std::string& cameraId) const;
Emilian Peev538c90e2018-12-17 18:03:19 +0000318
319 static const float kDepthARTolerance;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800320private:
321 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
322 mutable std::mutex mInterfaceMutex;
323
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800324 // the status listener update callbacks will lock mStatusMutex
325 mutable std::mutex mStatusListenerMutex;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800326 wp<StatusListener> mListener;
327 ServiceInteractionProxy* mServiceProxy;
328
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800329 // Current overall Android device physical status
330 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
331
Shuzhen Wang6ba8eb22018-07-08 13:10:44 -0700332 // mProviderLifecycleLock is locked during onRegistration and removeProvider
333 mutable std::mutex mProviderLifecycleLock;
334
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800335 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
336
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700337 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
338 // ICameraProvider alive while it is in use by the camera with the given ID for camera
339 // capabilities
340 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
341 mCameraProviderByCameraId;
342
343 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
344 // ICameraProvider alive while it is in use by the camera with the given ID for torch
345 // capabilities
346 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
347 mTorchProviderByCameraId;
348
349 // Lock for accessing mCameraProviderByCameraId and mTorchProviderByCameraId
350 std::mutex mProviderInterfaceMapLock;
351
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700352 struct ProviderInfo :
Shuzhen Wang43858162020-01-10 13:42:15 -0800353 virtual public hardware::camera::provider::V2_6::ICameraProviderCallback,
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700354 virtual public hardware::hidl_death_recipient
355 {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800356 const std::string mProviderName;
Emilian Peev71c73a22017-03-21 16:35:51 +0000357 const metadata_vendor_id_t mProviderTagid;
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800358 int mMinorVersion;
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800359 sp<VendorTagDescriptor> mVendorTagDescriptor;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700360 bool mSetTorchModeSupported;
361 bool mIsRemote;
362
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800363 // Current overall Android device physical status
364 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
365
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700366 // This pointer is used to keep a reference to the ICameraProvider that was last accessed.
367 wp<hardware::camera::provider::V2_4::ICameraProvider> mActiveInterface;
368
369 sp<hardware::camera::provider::V2_4::ICameraProvider> mSavedInterface;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800370
371 ProviderInfo(const std::string &providerName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800372 CameraProviderManager *manager);
373 ~ProviderInfo();
374
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800375 status_t initialize(sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
376 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
377 currentDeviceState);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700378
379 const sp<hardware::camera::provider::V2_4::ICameraProvider> startProviderInterface();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800380
381 const std::string& getType() const;
382
383 status_t addDevice(const std::string& name,
384 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
385 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
386 /*out*/ std::string *parsedId = nullptr);
387
388 status_t dump(int fd, const Vector<String16>& args) const;
389
390 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
Shuzhen Wang43858162020-01-10 13:42:15 -0800391 hardware::Return<void> cameraDeviceStatusChange(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800392 const hardware::hidl_string& cameraDeviceName,
393 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
Shuzhen Wang43858162020-01-10 13:42:15 -0800394 hardware::Return<void> torchModeStatusChange(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800395 const hardware::hidl_string& cameraDeviceName,
396 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
Shuzhen Wang43858162020-01-10 13:42:15 -0800397 hardware::Return<void> physicalCameraDeviceStatusChange(
398 const hardware::hidl_string& cameraDeviceName,
399 const hardware::hidl_string& physicalCameraDeviceName,
400 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800401
Shuzhen Wang394ad702020-07-23 13:01:54 -0700402 status_t cameraDeviceStatusChangeLocked(
403 std::string* id, const hardware::hidl_string& cameraDeviceName,
404 hardware::camera::common::V1_0::CameraDeviceStatus newStatus);
405 status_t physicalCameraDeviceStatusChangeLocked(
406 std::string* id, std::string* physicalId,
407 const hardware::hidl_string& cameraDeviceName,
408 const hardware::hidl_string& physicalCameraDeviceName,
409 hardware::camera::common::V1_0::CameraDeviceStatus newStatus);
410
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700411 // hidl_death_recipient interface - this locks the parent mInterfaceMutex
412 virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
413
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800414 /**
415 * Setup vendor tags for this provider
416 */
417 status_t setUpVendorTags();
418
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800419 /**
420 * Notify provider about top-level device physical state changes
421 */
422 status_t notifyDeviceStateChange(
423 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
424 newDeviceState);
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800425
426 std::vector<std::unordered_set<std::string>> getConcurrentCameraIdCombinations();
427
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800428 /**
429 * Query the camera provider for concurrent stream configuration support
430 */
431 status_t isConcurrentSessionConfigurationSupported(
432 const hardware::hidl_vec<
433 hardware::camera::provider::V2_6::CameraIdAndStreamCombination>
434 &halCameraIdsAndStreamCombinations,
435 bool *isSupported);
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800436
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800437 // Basic device information, common to all camera devices
438 struct DeviceInfo {
439 const std::string mName; // Full instance name
440 const std::string mId; // ID section of full name
441 const hardware::hidl_version mVersion;
Emilian Peev71c73a22017-03-21 16:35:51 +0000442 const metadata_vendor_id_t mProviderTagid;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700443 bool mIsLogicalCamera;
444 std::vector<std::string> mPhysicalIds;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700445 hardware::CameraInfo mInfo;
446 sp<IBase> mSavedInterface;
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700447 SystemCameraKind mSystemCameraKind = SystemCameraKind::PUBLIC;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800448
449 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
450
451 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
452
Shuzhen Wang79680432020-03-05 11:53:46 -0800453 wp<ProviderInfo> mParentProvider;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700454
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800455 bool hasFlashUnit() const { return mHasFlashUnit; }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800456 bool supportNativeZoomRatio() const { return mSupportNativeZoomRatio; }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800457 virtual status_t setTorchMode(bool enabled) = 0;
458 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100459 virtual bool isAPI1Compatible() const = 0;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700460 virtual status_t dumpState(int fd) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800461 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
462 (void) characteristics;
463 return INVALID_OPERATION;
464 }
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700465 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
466 CameraMetadata *characteristics) const {
467 (void) physicalCameraId;
468 (void) characteristics;
469 return INVALID_OPERATION;
470 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800471
Emilian Peev35ae8262018-11-08 13:11:32 +0000472 virtual status_t isSessionConfigurationSupported(
473 const hardware::camera::device::V3_4::StreamConfiguration &/*configuration*/,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700474 bool * /*status*/) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000475 return INVALID_OPERATION;
476 }
477
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700478 template<class InterfaceT>
479 sp<InterfaceT> startDeviceInterface();
480
Emilian Peev71c73a22017-03-21 16:35:51 +0000481 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
482 const std::string &id, const hardware::hidl_version& version,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700483 const std::vector<std::string>& publicCameraIds,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700484 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
485 sp<ProviderInfo> parentProvider) :
Emilian Peev71c73a22017-03-21 16:35:51 +0000486 mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700487 mIsLogicalCamera(false), mResourceCost(resourceCost),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800488 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700489 mParentProvider(parentProvider), mHasFlashUnit(false),
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800490 mSupportNativeZoomRatio(false), mPublicCameraIds(publicCameraIds) {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800491 virtual ~DeviceInfo();
492 protected:
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800493 bool mHasFlashUnit; // const after constructor
494 bool mSupportNativeZoomRatio; // const after constructor
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700495 const std::vector<std::string>& mPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800496
497 template<class InterfaceT>
498 static status_t setTorchMode(InterfaceT& interface, bool enabled);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700499
500 template<class InterfaceT>
501 status_t setTorchModeForDevice(bool enabled) {
502 // Don't save the ICameraProvider interface here because we assume that this was
503 // called from CameraProviderManager::setTorchMode(), which does save it.
504 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
505 return DeviceInfo::setTorchMode(interface, enabled);
506 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800507 };
508 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800509 std::unordered_set<std::string> mUniqueCameraIds;
Yin-Chia Yehe8e9e192017-03-16 15:23:51 -0700510 int mUniqueDeviceCount;
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700511 std::vector<std::string> mUniqueAPI1CompatibleCameraIds;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700512 // The initial public camera IDs published by the camera provider.
513 // Currently logical multi-camera is not supported for hot-plug camera.
514 // And we use this list to keep track of initial public camera IDs
515 // advertised by the provider, and to distinguish against "hidden"
516 // physical camera IDs.
517 std::vector<std::string> mProviderPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800518
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800519 // HALv3-specific camera fields, including the actual device interface
520 struct DeviceInfo3 : public DeviceInfo {
521 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800522
523 virtual status_t setTorchMode(bool enabled) override;
524 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100525 virtual bool isAPI1Compatible() const override;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700526 virtual status_t dumpState(int fd) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800527 virtual status_t getCameraCharacteristics(
528 CameraMetadata *characteristics) const override;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700529 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
530 CameraMetadata *characteristics) const override;
Emilian Peev35ae8262018-11-08 13:11:32 +0000531 virtual status_t isSessionConfigurationSupported(
532 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
533 bool *status /*out*/)
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700534 override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800535
Emilian Peev71c73a22017-03-21 16:35:51 +0000536 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
537 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800538 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700539 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700540 const std::vector<std::string>& publicCameraIds, sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800541 virtual ~DeviceInfo3();
542 private:
543 CameraMetadata mCameraCharacteristics;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700544 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700545 void queryPhysicalCameraIds();
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700546 SystemCameraKind getSystemCameraKind();
Shuzhen Wang268a1362018-10-16 16:32:59 -0700547 status_t fixupMonochromeTags();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000548 status_t addDynamicDepthTags();
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800549 status_t deriveHeicTags();
550 status_t addRotateCropTags();
Shuzhen Wang9bf8a6f2020-05-01 09:49:04 -0700551 status_t addPreCorrectionActiveArraySize();
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800552
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000553 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag,
554 android_pixel_format_t format,
555 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/);
556 void getSupportedDurations( const CameraMetadata& ch, uint32_t tag,
557 android_pixel_format_t format,
558 const std::vector<std::tuple<size_t, size_t>>& sizes,
559 std::vector<int64_t> *durations/*out*/);
560 void getSupportedDynamicDepthDurations(const std::vector<int64_t>& depthDurations,
561 const std::vector<int64_t>& blobDurations,
562 std::vector<int64_t> *dynamicDepthDurations /*out*/);
563 static void getSupportedDynamicDepthSizes(
564 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
565 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
566 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
567 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/);
Shuzhen Wang268a1362018-10-16 16:32:59 -0700568 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys,
569 uint32_t keyTag);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800570 status_t fillHeicStreamCombinations(std::vector<int32_t>* outputs,
571 std::vector<int64_t>* durations,
572 std::vector<int64_t>* stallDurations,
573 const camera_metadata_entry& halStreamConfigs,
574 const camera_metadata_entry& halStreamDurations);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800575 };
576
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800577 private:
578 std::string mType;
579 uint32_t mId;
580
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700581 std::mutex mLock;
582
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800583 CameraProviderManager *mManager;
584
Shuzhen Wang394ad702020-07-23 13:01:54 -0700585 struct CameraStatusInfoT {
586 bool isPhysicalCameraStatus = false;
587 hardware::hidl_string cameraId;
588 hardware::hidl_string physicalCameraId;
589 hardware::camera::common::V1_0::CameraDeviceStatus status;
590 CameraStatusInfoT(bool isForPhysicalCamera, const hardware::hidl_string& id,
591 const hardware::hidl_string& physicalId,
592 hardware::camera::common::V1_0::CameraDeviceStatus s) :
593 isPhysicalCameraStatus(isForPhysicalCamera), cameraId(id),
594 physicalCameraId(physicalId), status(s) {}
595 };
596
597 // Lock to synchronize between initialize() and camera status callbacks
598 std::mutex mInitLock;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800599 bool mInitialized = false;
Shuzhen Wang394ad702020-07-23 13:01:54 -0700600 std::vector<CameraStatusInfoT> mCachedStatus;
601 // End of scope for mInitLock
602
603 std::future<void> mInitialStatusCallbackFuture;
604 void notifyInitialStatusChange(sp<StatusListener> listener,
605 std::unique_ptr<std::vector<CameraStatusInfoT>> cachedStatus);
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800606
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800607 std::vector<std::unordered_set<std::string>> mConcurrentCameraIdCombinations;
608
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800609 // Templated method to instantiate the right kind of DeviceInfo and call the
610 // right CameraProvider getCameraDeviceInterface_* method.
611 template<class DeviceInfoT>
612 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
Emilian Peev71c73a22017-03-21 16:35:51 +0000613 const metadata_vendor_id_t tagId, const std::string &id,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700614 uint16_t minorVersion);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800615
616 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
617 template<class InterfaceT>
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700618 sp<InterfaceT> startDeviceInterface(const std::string &name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800619
620 // Parse provider instance name for type and id
621 static status_t parseProviderName(const std::string& name,
622 std::string *type, uint32_t *id);
623
624 // Parse device instance name for device version, type, and id.
625 static status_t parseDeviceName(const std::string& name,
626 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
Emilian Peev71c73a22017-03-21 16:35:51 +0000627
628 // Generate vendor tag id
629 static metadata_vendor_id_t generateVendorTagId(const std::string &name);
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100630
631 void removeDevice(std::string id);
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800632
633 // Expects to have mLock locked
634 status_t reCacheConcurrentStreamingCameraIdsLocked();
635 // Expects to have mLock locked
Jayant Chowdharycad23c22020-03-10 15:04:59 -0700636 status_t getConcurrentCameraIdsInternalLocked(
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800637 sp<hardware::camera::provider::V2_6::ICameraProvider> &interface2_6);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800638 };
639
640 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
641 // and the calling code doesn't mutate the list of providers or their lists of devices.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800642 // Finds the first device of the given ID that falls within the requested version range
643 // minVersion <= deviceVersion < maxVersion
644 // No guarantees on the order of traversal
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800645 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800646 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
647 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800648
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700649 status_t addProviderLocked(const std::string& newProvider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700650
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800651 bool isLogicalCameraLocked(const std::string& id, std::vector<std::string>* physicalCameraIds);
652
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800653 status_t removeProvider(const std::string& provider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700654 sp<StatusListener> getStatusListener() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800655
656 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
657
658 std::vector<sp<ProviderInfo>> mProviders;
659
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700660 void addProviderToMap(
661 const std::string &cameraId,
662 sp<hardware::camera::provider::V2_4::ICameraProvider> provider,
663 bool isTorchUsage);
664 void removeCameraIdFromMap(
665 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> &map,
666 const std::string &cameraId);
667
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800668 static const char* deviceStatusToString(
669 const hardware::camera::common::V1_0::CameraDeviceStatus&);
670 static const char* torchStatusToString(
671 const hardware::camera::common::V1_0::TorchModeStatus&);
672
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700673 status_t getCameraCharacteristicsLocked(const std::string &id,
674 CameraMetadata* characteristics) const;
675 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700676
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700677 status_t getSystemCameraKindLocked(const std::string& id, SystemCameraKind *kind) const;
678 std::pair<bool, ProviderInfo::DeviceInfo *> isHiddenPhysicalCameraInternal(const std::string& cameraId) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700679
680 void collectDeviceIdsLocked(const std::vector<std::string> deviceIds,
681 std::vector<std::string>& normalDeviceIds,
682 std::vector<std::string>& systemCameraDeviceIds) const;
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800683
684 status_t convertToHALStreamCombinationAndCameraIdsLocked(
685 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration>
686 &cameraIdsAndSessionConfigs,
687 hardware::hidl_vec<hardware::camera::provider::V2_6::CameraIdAndStreamCombination>
688 *halCameraIdsAndStreamCombinations,
689 bool *earlyExit);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800690};
691
692} // namespace android
693
694#endif