blob: a0e5f8fb51e9d4719ca1fcff596e842d4355cd42 [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
273 status_t openSession(const std::string &id,
274 const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback,
275 /*out*/
276 sp<hardware::camera::device::V1_0::ICameraDevice> *session);
277
278 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700279 * Save the ICameraProvider while it is being used by a camera or torch client
280 */
281 void saveRef(DeviceMode usageType, const std::string &cameraId,
282 sp<hardware::camera::provider::V2_4::ICameraProvider> provider);
283
284 /**
285 * Notify that the camera or torch is no longer being used by a camera client
286 */
287 void removeRef(DeviceMode usageType, const std::string &cameraId);
288
289 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800290 * IServiceNotification::onRegistration
291 * Invoked by the hardware service manager when a new camera provider is registered
292 */
293 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
294 const hardware::hidl_string& name,
295 bool preexisting) override;
296
297 /**
298 * Dump out information about available providers and devices
299 */
300 status_t dump(int fd, const Vector<String16>& args);
301
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800302 /**
303 * Conversion methods between HAL Status and status_t and strings
304 */
305 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
306 static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
307
Emilian Peev71c73a22017-03-21 16:35:51 +0000308 /*
309 * Return provider type for a specific device.
310 */
311 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id,
312 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
313 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
314
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700315 /*
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700316 * Check if a camera is a logical camera. And if yes, return
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700317 * the physical camera ids.
318 */
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700319 bool isLogicalCamera(const std::string& id, std::vector<std::string>* physicalCameraIds);
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700320
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700321 status_t getSystemCameraKind(const std::string& id, SystemCameraKind *kind) const;
322 bool isHiddenPhysicalCamera(const std::string& cameraId) const;
Emilian Peev538c90e2018-12-17 18:03:19 +0000323
324 static const float kDepthARTolerance;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800325private:
326 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
327 mutable std::mutex mInterfaceMutex;
328
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800329 // the status listener update callbacks will lock mStatusMutex
330 mutable std::mutex mStatusListenerMutex;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800331 wp<StatusListener> mListener;
332 ServiceInteractionProxy* mServiceProxy;
333
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800334 // Current overall Android device physical status
335 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
336
Shuzhen Wang6ba8eb22018-07-08 13:10:44 -0700337 // mProviderLifecycleLock is locked during onRegistration and removeProvider
338 mutable std::mutex mProviderLifecycleLock;
339
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800340 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
341
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700342 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
343 // ICameraProvider alive while it is in use by the camera with the given ID for camera
344 // capabilities
345 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
346 mCameraProviderByCameraId;
347
348 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
349 // ICameraProvider alive while it is in use by the camera with the given ID for torch
350 // capabilities
351 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
352 mTorchProviderByCameraId;
353
354 // Lock for accessing mCameraProviderByCameraId and mTorchProviderByCameraId
355 std::mutex mProviderInterfaceMapLock;
356
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700357 struct ProviderInfo :
Shuzhen Wang43858162020-01-10 13:42:15 -0800358 virtual public hardware::camera::provider::V2_6::ICameraProviderCallback,
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700359 virtual public hardware::hidl_death_recipient
360 {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800361 const std::string mProviderName;
Emilian Peev71c73a22017-03-21 16:35:51 +0000362 const metadata_vendor_id_t mProviderTagid;
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800363 int mMinorVersion;
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800364 sp<VendorTagDescriptor> mVendorTagDescriptor;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700365 bool mSetTorchModeSupported;
366 bool mIsRemote;
367
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800368 // Current overall Android device physical status
369 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
370
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700371 // This pointer is used to keep a reference to the ICameraProvider that was last accessed.
372 wp<hardware::camera::provider::V2_4::ICameraProvider> mActiveInterface;
373
374 sp<hardware::camera::provider::V2_4::ICameraProvider> mSavedInterface;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800375
376 ProviderInfo(const std::string &providerName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800377 CameraProviderManager *manager);
378 ~ProviderInfo();
379
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800380 status_t initialize(sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
381 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
382 currentDeviceState);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700383
384 const sp<hardware::camera::provider::V2_4::ICameraProvider> startProviderInterface();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800385
386 const std::string& getType() const;
387
388 status_t addDevice(const std::string& name,
389 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
390 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
391 /*out*/ std::string *parsedId = nullptr);
392
393 status_t dump(int fd, const Vector<String16>& args) const;
394
395 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
Shuzhen Wang43858162020-01-10 13:42:15 -0800396 hardware::Return<void> cameraDeviceStatusChange(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800397 const hardware::hidl_string& cameraDeviceName,
398 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
Shuzhen Wang43858162020-01-10 13:42:15 -0800399 hardware::Return<void> torchModeStatusChange(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800400 const hardware::hidl_string& cameraDeviceName,
401 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
Shuzhen Wang43858162020-01-10 13:42:15 -0800402 hardware::Return<void> physicalCameraDeviceStatusChange(
403 const hardware::hidl_string& cameraDeviceName,
404 const hardware::hidl_string& physicalCameraDeviceName,
405 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800406
Shuzhen Wang394ad702020-07-23 13:01:54 -0700407 status_t cameraDeviceStatusChangeLocked(
408 std::string* id, const hardware::hidl_string& cameraDeviceName,
409 hardware::camera::common::V1_0::CameraDeviceStatus newStatus);
410 status_t physicalCameraDeviceStatusChangeLocked(
411 std::string* id, std::string* physicalId,
412 const hardware::hidl_string& cameraDeviceName,
413 const hardware::hidl_string& physicalCameraDeviceName,
414 hardware::camera::common::V1_0::CameraDeviceStatus newStatus);
415
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700416 // hidl_death_recipient interface - this locks the parent mInterfaceMutex
417 virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
418
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800419 /**
420 * Setup vendor tags for this provider
421 */
422 status_t setUpVendorTags();
423
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800424 /**
425 * Notify provider about top-level device physical state changes
426 */
427 status_t notifyDeviceStateChange(
428 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
429 newDeviceState);
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800430
431 std::vector<std::unordered_set<std::string>> getConcurrentCameraIdCombinations();
432
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800433 /**
434 * Query the camera provider for concurrent stream configuration support
435 */
436 status_t isConcurrentSessionConfigurationSupported(
437 const hardware::hidl_vec<
438 hardware::camera::provider::V2_6::CameraIdAndStreamCombination>
439 &halCameraIdsAndStreamCombinations,
440 bool *isSupported);
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800441
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800442 // Basic device information, common to all camera devices
443 struct DeviceInfo {
444 const std::string mName; // Full instance name
445 const std::string mId; // ID section of full name
446 const hardware::hidl_version mVersion;
Emilian Peev71c73a22017-03-21 16:35:51 +0000447 const metadata_vendor_id_t mProviderTagid;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700448 bool mIsLogicalCamera;
449 std::vector<std::string> mPhysicalIds;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700450 hardware::CameraInfo mInfo;
451 sp<IBase> mSavedInterface;
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700452 SystemCameraKind mSystemCameraKind = SystemCameraKind::PUBLIC;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800453
454 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
455
456 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
457
Shuzhen Wang79680432020-03-05 11:53:46 -0800458 wp<ProviderInfo> mParentProvider;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700459
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800460 bool hasFlashUnit() const { return mHasFlashUnit; }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800461 bool supportNativeZoomRatio() const { return mSupportNativeZoomRatio; }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800462 virtual status_t setTorchMode(bool enabled) = 0;
463 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100464 virtual bool isAPI1Compatible() const = 0;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700465 virtual status_t dumpState(int fd) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800466 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
467 (void) characteristics;
468 return INVALID_OPERATION;
469 }
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700470 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
471 CameraMetadata *characteristics) const {
472 (void) physicalCameraId;
473 (void) characteristics;
474 return INVALID_OPERATION;
475 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800476
Emilian Peev35ae8262018-11-08 13:11:32 +0000477 virtual status_t isSessionConfigurationSupported(
478 const hardware::camera::device::V3_4::StreamConfiguration &/*configuration*/,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700479 bool * /*status*/) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000480 return INVALID_OPERATION;
481 }
482
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700483 template<class InterfaceT>
484 sp<InterfaceT> startDeviceInterface();
485
Emilian Peev71c73a22017-03-21 16:35:51 +0000486 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
487 const std::string &id, const hardware::hidl_version& version,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700488 const std::vector<std::string>& publicCameraIds,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700489 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
490 sp<ProviderInfo> parentProvider) :
Emilian Peev71c73a22017-03-21 16:35:51 +0000491 mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700492 mIsLogicalCamera(false), mResourceCost(resourceCost),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800493 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700494 mParentProvider(parentProvider), mHasFlashUnit(false),
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800495 mSupportNativeZoomRatio(false), mPublicCameraIds(publicCameraIds) {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800496 virtual ~DeviceInfo();
497 protected:
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800498 bool mHasFlashUnit; // const after constructor
499 bool mSupportNativeZoomRatio; // const after constructor
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700500 const std::vector<std::string>& mPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800501
502 template<class InterfaceT>
503 static status_t setTorchMode(InterfaceT& interface, bool enabled);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700504
505 template<class InterfaceT>
506 status_t setTorchModeForDevice(bool enabled) {
507 // Don't save the ICameraProvider interface here because we assume that this was
508 // called from CameraProviderManager::setTorchMode(), which does save it.
509 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
510 return DeviceInfo::setTorchMode(interface, enabled);
511 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800512 };
513 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800514 std::unordered_set<std::string> mUniqueCameraIds;
Yin-Chia Yehe8e9e192017-03-16 15:23:51 -0700515 int mUniqueDeviceCount;
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700516 std::vector<std::string> mUniqueAPI1CompatibleCameraIds;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700517 // The initial public camera IDs published by the camera provider.
518 // Currently logical multi-camera is not supported for hot-plug camera.
519 // And we use this list to keep track of initial public camera IDs
520 // advertised by the provider, and to distinguish against "hidden"
521 // physical camera IDs.
522 std::vector<std::string> mProviderPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800523
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800524 // HALv1-specific camera fields, including the actual device interface
525 struct DeviceInfo1 : public DeviceInfo {
526 typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800527
528 virtual status_t setTorchMode(bool enabled) override;
529 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100530 //In case of Device1Info assume that we are always API1 compatible
531 virtual bool isAPI1Compatible() const override { return true; }
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700532 virtual status_t dumpState(int fd) override;
Emilian Peev71c73a22017-03-21 16:35:51 +0000533 DeviceInfo1(const std::string& name, const metadata_vendor_id_t tagId,
534 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800535 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700536 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700537 const std::vector<std::string>& publicCameraIds,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800538 sp<InterfaceT> interface);
539 virtual ~DeviceInfo1();
540 private:
541 CameraParameters2 mDefaultParameters;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700542 status_t cacheCameraInfo(sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800543 };
544
545 // HALv3-specific camera fields, including the actual device interface
546 struct DeviceInfo3 : public DeviceInfo {
547 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800548
549 virtual status_t setTorchMode(bool enabled) override;
550 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100551 virtual bool isAPI1Compatible() const override;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700552 virtual status_t dumpState(int fd) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800553 virtual status_t getCameraCharacteristics(
554 CameraMetadata *characteristics) const override;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700555 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
556 CameraMetadata *characteristics) const override;
Emilian Peev35ae8262018-11-08 13:11:32 +0000557 virtual status_t isSessionConfigurationSupported(
558 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
559 bool *status /*out*/)
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700560 override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800561
Emilian Peev71c73a22017-03-21 16:35:51 +0000562 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
563 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800564 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700565 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700566 const std::vector<std::string>& publicCameraIds, sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800567 virtual ~DeviceInfo3();
568 private:
569 CameraMetadata mCameraCharacteristics;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700570 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700571 void queryPhysicalCameraIds();
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700572 SystemCameraKind getSystemCameraKind();
Shuzhen Wang268a1362018-10-16 16:32:59 -0700573 status_t fixupMonochromeTags();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000574 status_t addDynamicDepthTags();
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800575 status_t deriveHeicTags();
576 status_t addRotateCropTags();
Shuzhen Wang9bf8a6f2020-05-01 09:49:04 -0700577 status_t addPreCorrectionActiveArraySize();
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800578
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000579 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag,
580 android_pixel_format_t format,
581 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/);
582 void getSupportedDurations( const CameraMetadata& ch, uint32_t tag,
583 android_pixel_format_t format,
584 const std::vector<std::tuple<size_t, size_t>>& sizes,
585 std::vector<int64_t> *durations/*out*/);
586 void getSupportedDynamicDepthDurations(const std::vector<int64_t>& depthDurations,
587 const std::vector<int64_t>& blobDurations,
588 std::vector<int64_t> *dynamicDepthDurations /*out*/);
589 static void getSupportedDynamicDepthSizes(
590 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
591 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
592 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
593 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/);
Shuzhen Wang268a1362018-10-16 16:32:59 -0700594 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys,
595 uint32_t keyTag);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800596 status_t fillHeicStreamCombinations(std::vector<int32_t>* outputs,
597 std::vector<int64_t>* durations,
598 std::vector<int64_t>* stallDurations,
599 const camera_metadata_entry& halStreamConfigs,
600 const camera_metadata_entry& halStreamDurations);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800601 };
602
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800603 private:
604 std::string mType;
605 uint32_t mId;
606
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700607 std::mutex mLock;
608
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800609 CameraProviderManager *mManager;
610
Shuzhen Wang394ad702020-07-23 13:01:54 -0700611 struct CameraStatusInfoT {
612 bool isPhysicalCameraStatus = false;
613 hardware::hidl_string cameraId;
614 hardware::hidl_string physicalCameraId;
615 hardware::camera::common::V1_0::CameraDeviceStatus status;
616 CameraStatusInfoT(bool isForPhysicalCamera, const hardware::hidl_string& id,
617 const hardware::hidl_string& physicalId,
618 hardware::camera::common::V1_0::CameraDeviceStatus s) :
619 isPhysicalCameraStatus(isForPhysicalCamera), cameraId(id),
620 physicalCameraId(physicalId), status(s) {}
621 };
622
623 // Lock to synchronize between initialize() and camera status callbacks
624 std::mutex mInitLock;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800625 bool mInitialized = false;
Shuzhen Wang394ad702020-07-23 13:01:54 -0700626 std::vector<CameraStatusInfoT> mCachedStatus;
627 // End of scope for mInitLock
628
629 std::future<void> mInitialStatusCallbackFuture;
630 void notifyInitialStatusChange(sp<StatusListener> listener,
631 std::unique_ptr<std::vector<CameraStatusInfoT>> cachedStatus);
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800632
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800633 std::vector<std::unordered_set<std::string>> mConcurrentCameraIdCombinations;
634
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800635 // Templated method to instantiate the right kind of DeviceInfo and call the
636 // right CameraProvider getCameraDeviceInterface_* method.
637 template<class DeviceInfoT>
638 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
Emilian Peev71c73a22017-03-21 16:35:51 +0000639 const metadata_vendor_id_t tagId, const std::string &id,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700640 uint16_t minorVersion);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800641
642 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
643 template<class InterfaceT>
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700644 sp<InterfaceT> startDeviceInterface(const std::string &name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800645
646 // Parse provider instance name for type and id
647 static status_t parseProviderName(const std::string& name,
648 std::string *type, uint32_t *id);
649
650 // Parse device instance name for device version, type, and id.
651 static status_t parseDeviceName(const std::string& name,
652 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
Emilian Peev71c73a22017-03-21 16:35:51 +0000653
654 // Generate vendor tag id
655 static metadata_vendor_id_t generateVendorTagId(const std::string &name);
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100656
657 void removeDevice(std::string id);
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800658
659 // Expects to have mLock locked
660 status_t reCacheConcurrentStreamingCameraIdsLocked();
661 // Expects to have mLock locked
Jayant Chowdharycad23c22020-03-10 15:04:59 -0700662 status_t getConcurrentCameraIdsInternalLocked(
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800663 sp<hardware::camera::provider::V2_6::ICameraProvider> &interface2_6);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800664 };
665
666 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
667 // and the calling code doesn't mutate the list of providers or their lists of devices.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800668 // Finds the first device of the given ID that falls within the requested version range
669 // minVersion <= deviceVersion < maxVersion
670 // No guarantees on the order of traversal
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800671 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800672 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
673 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800674
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700675 status_t addProviderLocked(const std::string& newProvider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700676
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800677 bool isLogicalCameraLocked(const std::string& id, std::vector<std::string>* physicalCameraIds);
678
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800679 status_t removeProvider(const std::string& provider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700680 sp<StatusListener> getStatusListener() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800681
682 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
683
684 std::vector<sp<ProviderInfo>> mProviders;
685
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700686 void addProviderToMap(
687 const std::string &cameraId,
688 sp<hardware::camera::provider::V2_4::ICameraProvider> provider,
689 bool isTorchUsage);
690 void removeCameraIdFromMap(
691 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> &map,
692 const std::string &cameraId);
693
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800694 static const char* deviceStatusToString(
695 const hardware::camera::common::V1_0::CameraDeviceStatus&);
696 static const char* torchStatusToString(
697 const hardware::camera::common::V1_0::TorchModeStatus&);
698
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700699 status_t getCameraCharacteristicsLocked(const std::string &id,
700 CameraMetadata* characteristics) const;
701 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700702
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700703 status_t getSystemCameraKindLocked(const std::string& id, SystemCameraKind *kind) const;
704 std::pair<bool, ProviderInfo::DeviceInfo *> isHiddenPhysicalCameraInternal(const std::string& cameraId) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700705
706 void collectDeviceIdsLocked(const std::vector<std::string> deviceIds,
707 std::vector<std::string>& normalDeviceIds,
708 std::vector<std::string>& systemCameraDeviceIds) const;
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800709
710 status_t convertToHALStreamCombinationAndCameraIdsLocked(
711 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration>
712 &cameraIdsAndSessionConfigs,
713 hardware::hidl_vec<hardware::camera::provider::V2_6::CameraIdAndStreamCombination>
714 *halCameraIdsAndStreamCombinations,
715 bool *earlyExit);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800716};
717
718} // namespace android
719
720#endif