blob: 20ca31970344d79057fb883f7d22199aa40eb35e [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;
Shuzhen Wang43858162020-01-10 13:42:15 -0800442 std::map<std::string, hardware::camera::common::V1_0::CameraDeviceStatus>
443 mPhysicalStatus;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800444
Shuzhen Wang79680432020-03-05 11:53:46 -0800445 wp<ProviderInfo> mParentProvider;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700446
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800447 bool hasFlashUnit() const { return mHasFlashUnit; }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800448 bool supportNativeZoomRatio() const { return mSupportNativeZoomRatio; }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800449 virtual status_t setTorchMode(bool enabled) = 0;
450 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100451 virtual bool isAPI1Compatible() const = 0;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700452 virtual status_t dumpState(int fd) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800453 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
454 (void) characteristics;
455 return INVALID_OPERATION;
456 }
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700457 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
458 CameraMetadata *characteristics) const {
459 (void) physicalCameraId;
460 (void) characteristics;
461 return INVALID_OPERATION;
462 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800463
Emilian Peev35ae8262018-11-08 13:11:32 +0000464 virtual status_t isSessionConfigurationSupported(
465 const hardware::camera::device::V3_4::StreamConfiguration &/*configuration*/,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700466 bool * /*status*/) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000467 return INVALID_OPERATION;
468 }
469
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700470 template<class InterfaceT>
471 sp<InterfaceT> startDeviceInterface();
472
Emilian Peev71c73a22017-03-21 16:35:51 +0000473 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
474 const std::string &id, const hardware::hidl_version& version,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700475 const std::vector<std::string>& publicCameraIds,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700476 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
477 sp<ProviderInfo> parentProvider) :
Emilian Peev71c73a22017-03-21 16:35:51 +0000478 mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700479 mIsLogicalCamera(false), mResourceCost(resourceCost),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800480 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700481 mParentProvider(parentProvider), mHasFlashUnit(false),
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800482 mSupportNativeZoomRatio(false), mPublicCameraIds(publicCameraIds) {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800483 virtual ~DeviceInfo();
484 protected:
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800485 bool mHasFlashUnit; // const after constructor
486 bool mSupportNativeZoomRatio; // const after constructor
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700487 const std::vector<std::string>& mPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800488
489 template<class InterfaceT>
490 static status_t setTorchMode(InterfaceT& interface, bool enabled);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700491
492 template<class InterfaceT>
493 status_t setTorchModeForDevice(bool enabled) {
494 // Don't save the ICameraProvider interface here because we assume that this was
495 // called from CameraProviderManager::setTorchMode(), which does save it.
496 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
497 return DeviceInfo::setTorchMode(interface, enabled);
498 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800499 };
500 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800501 std::unordered_set<std::string> mUniqueCameraIds;
Yin-Chia Yehe8e9e192017-03-16 15:23:51 -0700502 int mUniqueDeviceCount;
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700503 std::vector<std::string> mUniqueAPI1CompatibleCameraIds;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700504 // The initial public camera IDs published by the camera provider.
505 // Currently logical multi-camera is not supported for hot-plug camera.
506 // And we use this list to keep track of initial public camera IDs
507 // advertised by the provider, and to distinguish against "hidden"
508 // physical camera IDs.
509 std::vector<std::string> mProviderPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800510
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800511 // HALv3-specific camera fields, including the actual device interface
512 struct DeviceInfo3 : public DeviceInfo {
513 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800514
515 virtual status_t setTorchMode(bool enabled) override;
516 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100517 virtual bool isAPI1Compatible() const override;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700518 virtual status_t dumpState(int fd) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800519 virtual status_t getCameraCharacteristics(
520 CameraMetadata *characteristics) const override;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700521 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
522 CameraMetadata *characteristics) const override;
Emilian Peev35ae8262018-11-08 13:11:32 +0000523 virtual status_t isSessionConfigurationSupported(
524 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
525 bool *status /*out*/)
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700526 override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800527
Emilian Peev71c73a22017-03-21 16:35:51 +0000528 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
529 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800530 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700531 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700532 const std::vector<std::string>& publicCameraIds, sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800533 virtual ~DeviceInfo3();
534 private:
535 CameraMetadata mCameraCharacteristics;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700536 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700537 void queryPhysicalCameraIds();
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700538 SystemCameraKind getSystemCameraKind();
Shuzhen Wang268a1362018-10-16 16:32:59 -0700539 status_t fixupMonochromeTags();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000540 status_t addDynamicDepthTags();
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800541 status_t deriveHeicTags();
542 status_t addRotateCropTags();
Shuzhen Wang9bf8a6f2020-05-01 09:49:04 -0700543 status_t addPreCorrectionActiveArraySize();
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800544
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000545 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag,
546 android_pixel_format_t format,
547 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/);
548 void getSupportedDurations( const CameraMetadata& ch, uint32_t tag,
549 android_pixel_format_t format,
550 const std::vector<std::tuple<size_t, size_t>>& sizes,
551 std::vector<int64_t> *durations/*out*/);
552 void getSupportedDynamicDepthDurations(const std::vector<int64_t>& depthDurations,
553 const std::vector<int64_t>& blobDurations,
554 std::vector<int64_t> *dynamicDepthDurations /*out*/);
555 static void getSupportedDynamicDepthSizes(
556 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
557 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
558 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
559 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/);
Shuzhen Wang268a1362018-10-16 16:32:59 -0700560 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys,
561 uint32_t keyTag);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800562 status_t fillHeicStreamCombinations(std::vector<int32_t>* outputs,
563 std::vector<int64_t>* durations,
564 std::vector<int64_t>* stallDurations,
565 const camera_metadata_entry& halStreamConfigs,
566 const camera_metadata_entry& halStreamDurations);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800567 };
568
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800569 private:
570 std::string mType;
571 uint32_t mId;
572
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700573 std::mutex mLock;
574
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800575 CameraProviderManager *mManager;
576
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800577 bool mInitialized = false;
578
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800579 std::vector<std::unordered_set<std::string>> mConcurrentCameraIdCombinations;
580
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800581 // Templated method to instantiate the right kind of DeviceInfo and call the
582 // right CameraProvider getCameraDeviceInterface_* method.
583 template<class DeviceInfoT>
584 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
Emilian Peev71c73a22017-03-21 16:35:51 +0000585 const metadata_vendor_id_t tagId, const std::string &id,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700586 uint16_t minorVersion);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800587
588 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
589 template<class InterfaceT>
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700590 sp<InterfaceT> startDeviceInterface(const std::string &name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800591
592 // Parse provider instance name for type and id
593 static status_t parseProviderName(const std::string& name,
594 std::string *type, uint32_t *id);
595
596 // Parse device instance name for device version, type, and id.
597 static status_t parseDeviceName(const std::string& name,
598 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
Emilian Peev71c73a22017-03-21 16:35:51 +0000599
600 // Generate vendor tag id
601 static metadata_vendor_id_t generateVendorTagId(const std::string &name);
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100602
603 void removeDevice(std::string id);
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800604
605 // Expects to have mLock locked
606 status_t reCacheConcurrentStreamingCameraIdsLocked();
607 // Expects to have mLock locked
Jayant Chowdharycad23c22020-03-10 15:04:59 -0700608 status_t getConcurrentCameraIdsInternalLocked(
Jayant Chowdharycbe770a2020-02-14 11:14:46 -0800609 sp<hardware::camera::provider::V2_6::ICameraProvider> &interface2_6);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800610 };
611
612 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
613 // and the calling code doesn't mutate the list of providers or their lists of devices.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800614 // Finds the first device of the given ID that falls within the requested version range
615 // minVersion <= deviceVersion < maxVersion
616 // No guarantees on the order of traversal
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800617 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800618 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
619 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800620
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700621 status_t addProviderLocked(const std::string& newProvider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700622
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800623 bool isLogicalCameraLocked(const std::string& id, std::vector<std::string>* physicalCameraIds);
624
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800625 status_t removeProvider(const std::string& provider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700626 sp<StatusListener> getStatusListener() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800627
628 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
629
630 std::vector<sp<ProviderInfo>> mProviders;
631
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700632 void addProviderToMap(
633 const std::string &cameraId,
634 sp<hardware::camera::provider::V2_4::ICameraProvider> provider,
635 bool isTorchUsage);
636 void removeCameraIdFromMap(
637 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> &map,
638 const std::string &cameraId);
639
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800640 static const char* deviceStatusToString(
641 const hardware::camera::common::V1_0::CameraDeviceStatus&);
642 static const char* torchStatusToString(
643 const hardware::camera::common::V1_0::TorchModeStatus&);
644
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700645 status_t getCameraCharacteristicsLocked(const std::string &id,
646 CameraMetadata* characteristics) const;
647 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700648
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700649 status_t getSystemCameraKindLocked(const std::string& id, SystemCameraKind *kind) const;
650 std::pair<bool, ProviderInfo::DeviceInfo *> isHiddenPhysicalCameraInternal(const std::string& cameraId) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700651
652 void collectDeviceIdsLocked(const std::vector<std::string> deviceIds,
653 std::vector<std::string>& normalDeviceIds,
654 std::vector<std::string>& systemCameraDeviceIds) const;
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800655
656 status_t convertToHALStreamCombinationAndCameraIdsLocked(
657 const std::vector<hardware::camera2::utils::CameraIdAndSessionConfiguration>
658 &cameraIdsAndSessionConfigs,
659 hardware::hidl_vec<hardware::camera::provider::V2_6::CameraIdAndStreamCombination>
660 *halCameraIdsAndStreamCombinations,
661 bool *earlyExit);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800662};
663
664} // namespace android
665
666#endif