blob: 2ef1f6fabfe879d14903c59f34557336d2486e65 [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
26#include <camera/CameraParameters2.h>
27#include <camera/CameraMetadata.h>
28#include <camera/CameraBase.h>
29#include <utils/Errors.h>
30#include <android/hardware/camera/common/1.0/types.h>
Eino-Ville Talvala63f36112018-12-06 14:57:03 -080031#include <android/hardware/camera/provider/2.5/ICameraProvider.h>
Emilian Peev35ae8262018-11-08 13:11:32 +000032#include <android/hardware/camera/device/3.4/ICameraDeviceSession.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080033#include <android/hidl/manager/1.0/IServiceNotification.h>
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080034#include <camera/VendorTagDescriptor.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080035
36namespace android {
37
38/**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080039 * The vendor tag descriptor class that takes HIDL vendor tag information as
40 * input. Not part of VendorTagDescriptor class because that class is used
41 * in AIDL generated sources which don't have access to HIDL headers.
42 */
43class HidlVendorTagDescriptor : public VendorTagDescriptor {
44public:
45 /**
46 * Create a VendorTagDescriptor object from the HIDL VendorTagSection
47 * vector.
48 *
49 * Returns OK on success, or a negative error code.
50 */
51 static status_t createDescriptorFromHidl(
52 const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts,
53 /*out*/
54 sp<VendorTagDescriptor>& descriptor);
55};
56
Jayant Chowdhary5216b212019-07-17 09:26:23 -070057enum SystemCameraKind {
58 /**
59 * These camera devices are visible to all apps and system components alike
60 */
61 PUBLIC = 0,
62
63 /**
64 * These camera devices are visible only to processes having the
65 * android.permission.SYSTEM_CAMERA permission. They are not exposed to 3P
66 * apps.
67 */
68 SYSTEM_ONLY_CAMERA,
69
70 /**
71 * These camera devices are visible only to HAL clients (that try to connect
72 * on a hwbinder thread).
73 */
74 HIDDEN_SECURE_CAMERA
75};
76
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080077/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080078 * A manager for all camera providers available on an Android device.
79 *
80 * Responsible for enumerating providers and the individual camera devices
81 * they export, both at startup and as providers and devices are added/removed.
82 *
83 * Provides methods for requesting information about individual devices and for
84 * opening them for active use.
85 *
86 */
87class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification {
88public:
89
90 ~CameraProviderManager();
91
92 // Tiny proxy for the static methods in a HIDL interface that communicate with the hardware
93 // service manager, to be replacable in unit tests with a fake.
94 struct ServiceInteractionProxy {
95 virtual bool registerForNotifications(
96 const std::string &serviceName,
97 const sp<hidl::manager::V1_0::IServiceNotification>
98 &notification) = 0;
Eino-Ville Talvalaec960602019-10-15 11:46:16 -070099 // Will not wait for service to start if it's not already running
100 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService(
101 const std::string &serviceName) = 0;
102 // Will block for service if it exists but isn't running
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800103 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
104 const std::string &serviceName) = 0;
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700105 virtual hardware::hidl_vec<hardware::hidl_string> listServices() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800106 virtual ~ServiceInteractionProxy() {}
107 };
108
109 // Standard use case - call into the normal generated static methods which invoke
110 // the real hardware service manager
111 struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
112 virtual bool registerForNotifications(
113 const std::string &serviceName,
114 const sp<hidl::manager::V1_0::IServiceNotification>
115 &notification) override {
116 return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
117 serviceName, notification);
118 }
Eino-Ville Talvalaec960602019-10-15 11:46:16 -0700119 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService(
120 const std::string &serviceName) override {
121 return hardware::camera::provider::V2_4::ICameraProvider::tryGetService(serviceName);
122 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800123 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
124 const std::string &serviceName) override {
125 return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
126 }
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700127
128 virtual hardware::hidl_vec<hardware::hidl_string> listServices() override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800129 };
130
131 /**
132 * Listener interface for device/torch status changes
133 */
134 struct StatusListener : virtual public RefBase {
135 ~StatusListener() {}
136
137 virtual void onDeviceStatusChanged(const String8 &cameraId,
138 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
139 virtual void onTorchStatusChanged(const String8 &cameraId,
140 hardware::camera::common::V1_0::TorchModeStatus newStatus) = 0;
Emilian Peevaee727d2017-05-04 16:35:48 +0100141 virtual void onNewProviderRegistered() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800142 };
143
144 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700145 * Represents the mode a camera device is currently in
146 */
147 enum class DeviceMode {
148 TORCH,
149 CAMERA
150 };
151
152 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800153 * Initialize the manager and give it a status listener; optionally accepts a service
154 * interaction proxy.
155 *
156 * The default proxy communicates via the hardware service manager; alternate proxies can be
157 * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
158 */
159 status_t initialize(wp<StatusListener> listener,
160 ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);
161
162 /**
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700163 * Retrieve the total number of available cameras.
164 * This value may change dynamically as cameras are added or removed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800165 */
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700166 std::pair<int, int> getCameraCount() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800167
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800168 std::vector<std::string> getCameraDeviceIds() const;
169
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800170 /**
Emilian Peevf53f66e2017-04-11 14:29:43 +0100171 * Retrieve the number of API1 compatible cameras; these are internal and
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800172 * backwards-compatible. This is the set of cameras that will be
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800173 * accessible via the old camera API.
174 * The return value may change dynamically due to external camera hotplug.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800175 */
Emilian Peevf53f66e2017-04-11 14:29:43 +0100176 std::vector<std::string> getAPI1CompatibleCameraDeviceIds() const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700177
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800178 /**
179 * Return true if a device with a given ID and major version exists
180 */
181 bool isValidDevice(const std::string &id, uint16_t majorVersion) const;
182
183 /**
184 * Return true if a device with a given ID has a flash unit. Returns false
185 * for devices that are unknown.
186 */
187 bool hasFlashUnit(const std::string &id) const;
188
189 /**
190 * Return the resource cost of this camera device
191 */
192 status_t getResourceCost(const std::string &id,
193 hardware::camera::common::V1_0::CameraResourceCost* cost) const;
194
195 /**
196 * Return the old camera API camera info
197 */
198 status_t getCameraInfo(const std::string &id,
199 hardware::CameraInfo* info) const;
200
201 /**
202 * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does
203 * not have a v3 or newer HAL version.
204 */
205 status_t getCameraCharacteristics(const std::string &id,
206 CameraMetadata* characteristics) const;
207
208 /**
Emilian Peev35ae8262018-11-08 13:11:32 +0000209 * Check for device support of specific stream combination.
210 */
211 status_t isSessionConfigurationSupported(const std::string& id,
212 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
213 bool *status /*out*/) const;
214
215 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800216 * Return the highest supported device interface version for this ID
217 */
218 status_t getHighestSupportedVersion(const std::string &id,
219 hardware::hidl_version *v);
220
221 /**
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700222 * Check if a given camera device support setTorchMode API.
223 */
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700224 bool supportSetTorchMode(const std::string &id) const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700225
226 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800227 * Turn on or off the flashlight on a given camera device.
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700228 * May fail if the device does not support this API, is in active use, or if the device
229 * doesn't exist, etc.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800230 */
231 status_t setTorchMode(const std::string &id, bool enabled);
232
233 /**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800234 * Setup vendor tags for all registered providers
235 */
236 status_t setUpVendorTags();
237
238 /**
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800239 * Inform registered providers about a device state change, such as folding or unfolding
240 */
241 status_t notifyDeviceStateChange(
242 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> newState);
243
244 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800245 * Open an active session to a camera device.
246 *
247 * This fully powers on the camera device hardware, and returns a handle to a
248 * session to be used for hardware configuration and operation.
249 */
250 status_t openSession(const std::string &id,
251 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
252 /*out*/
253 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session);
254
255 status_t openSession(const std::string &id,
256 const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback,
257 /*out*/
258 sp<hardware::camera::device::V1_0::ICameraDevice> *session);
259
260 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700261 * Save the ICameraProvider while it is being used by a camera or torch client
262 */
263 void saveRef(DeviceMode usageType, const std::string &cameraId,
264 sp<hardware::camera::provider::V2_4::ICameraProvider> provider);
265
266 /**
267 * Notify that the camera or torch is no longer being used by a camera client
268 */
269 void removeRef(DeviceMode usageType, const std::string &cameraId);
270
271 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800272 * IServiceNotification::onRegistration
273 * Invoked by the hardware service manager when a new camera provider is registered
274 */
275 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
276 const hardware::hidl_string& name,
277 bool preexisting) override;
278
279 /**
280 * Dump out information about available providers and devices
281 */
282 status_t dump(int fd, const Vector<String16>& args);
283
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800284 /**
285 * Conversion methods between HAL Status and status_t and strings
286 */
287 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
288 static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
289
Emilian Peev71c73a22017-03-21 16:35:51 +0000290 /*
291 * Return provider type for a specific device.
292 */
293 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id,
294 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
295 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
296
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700297 /*
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700298 * Check if a camera is a logical camera. And if yes, return
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700299 * the physical camera ids.
300 */
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700301 bool isLogicalCamera(const std::string& id, std::vector<std::string>* physicalCameraIds);
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700302
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700303 status_t getSystemCameraKind(const std::string& id, SystemCameraKind *kind) const;
304 bool isHiddenPhysicalCamera(const std::string& cameraId) const;
Emilian Peev538c90e2018-12-17 18:03:19 +0000305
306 static const float kDepthARTolerance;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800307private:
308 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
309 mutable std::mutex mInterfaceMutex;
310
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800311 // the status listener update callbacks will lock mStatusMutex
312 mutable std::mutex mStatusListenerMutex;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800313 wp<StatusListener> mListener;
314 ServiceInteractionProxy* mServiceProxy;
315
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800316 // Current overall Android device physical status
317 android::hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
318
Shuzhen Wang6ba8eb22018-07-08 13:10:44 -0700319 // mProviderLifecycleLock is locked during onRegistration and removeProvider
320 mutable std::mutex mProviderLifecycleLock;
321
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800322 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
323
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700324 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
325 // ICameraProvider alive while it is in use by the camera with the given ID for camera
326 // capabilities
327 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
328 mCameraProviderByCameraId;
329
330 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
331 // ICameraProvider alive while it is in use by the camera with the given ID for torch
332 // capabilities
333 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
334 mTorchProviderByCameraId;
335
336 // Lock for accessing mCameraProviderByCameraId and mTorchProviderByCameraId
337 std::mutex mProviderInterfaceMapLock;
338
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700339 struct ProviderInfo :
340 virtual public hardware::camera::provider::V2_4::ICameraProviderCallback,
341 virtual public hardware::hidl_death_recipient
342 {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800343 const std::string mProviderName;
Emilian Peev71c73a22017-03-21 16:35:51 +0000344 const metadata_vendor_id_t mProviderTagid;
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800345 int mMinorVersion;
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800346 sp<VendorTagDescriptor> mVendorTagDescriptor;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700347 bool mSetTorchModeSupported;
348 bool mIsRemote;
349
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800350 // Current overall Android device physical status
351 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState> mDeviceState;
352
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700353 // This pointer is used to keep a reference to the ICameraProvider that was last accessed.
354 wp<hardware::camera::provider::V2_4::ICameraProvider> mActiveInterface;
355
356 sp<hardware::camera::provider::V2_4::ICameraProvider> mSavedInterface;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800357
358 ProviderInfo(const std::string &providerName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800359 CameraProviderManager *manager);
360 ~ProviderInfo();
361
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800362 status_t initialize(sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
363 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
364 currentDeviceState);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700365
366 const sp<hardware::camera::provider::V2_4::ICameraProvider> startProviderInterface();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800367
368 const std::string& getType() const;
369
370 status_t addDevice(const std::string& name,
371 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
372 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
373 /*out*/ std::string *parsedId = nullptr);
374
375 status_t dump(int fd, const Vector<String16>& args) const;
376
377 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
378 virtual hardware::Return<void> cameraDeviceStatusChange(
379 const hardware::hidl_string& cameraDeviceName,
380 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
381 virtual hardware::Return<void> torchModeStatusChange(
382 const hardware::hidl_string& cameraDeviceName,
383 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
384
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700385 // hidl_death_recipient interface - this locks the parent mInterfaceMutex
386 virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
387
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800388 /**
389 * Setup vendor tags for this provider
390 */
391 status_t setUpVendorTags();
392
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800393 /**
394 * Notify provider about top-level device physical state changes
395 */
396 status_t notifyDeviceStateChange(
397 hardware::hidl_bitfield<hardware::camera::provider::V2_5::DeviceState>
398 newDeviceState);
399
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800400 // Basic device information, common to all camera devices
401 struct DeviceInfo {
402 const std::string mName; // Full instance name
403 const std::string mId; // ID section of full name
404 const hardware::hidl_version mVersion;
Emilian Peev71c73a22017-03-21 16:35:51 +0000405 const metadata_vendor_id_t mProviderTagid;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700406 bool mIsLogicalCamera;
407 std::vector<std::string> mPhysicalIds;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700408 hardware::CameraInfo mInfo;
409 sp<IBase> mSavedInterface;
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700410 SystemCameraKind mSystemCameraKind = SystemCameraKind::PUBLIC;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800411
412 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
413
414 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
415
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700416 sp<ProviderInfo> mParentProvider;
417
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800418 bool hasFlashUnit() const { return mHasFlashUnit; }
419 virtual status_t setTorchMode(bool enabled) = 0;
420 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100421 virtual bool isAPI1Compatible() const = 0;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700422 virtual status_t dumpState(int fd) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800423 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
424 (void) characteristics;
425 return INVALID_OPERATION;
426 }
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700427 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
428 CameraMetadata *characteristics) const {
429 (void) physicalCameraId;
430 (void) characteristics;
431 return INVALID_OPERATION;
432 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800433
Emilian Peev35ae8262018-11-08 13:11:32 +0000434 virtual status_t isSessionConfigurationSupported(
435 const hardware::camera::device::V3_4::StreamConfiguration &/*configuration*/,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700436 bool * /*status*/) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000437 return INVALID_OPERATION;
438 }
439
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700440 template<class InterfaceT>
441 sp<InterfaceT> startDeviceInterface();
442
Emilian Peev71c73a22017-03-21 16:35:51 +0000443 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
444 const std::string &id, const hardware::hidl_version& version,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700445 const std::vector<std::string>& publicCameraIds,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700446 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
447 sp<ProviderInfo> parentProvider) :
Emilian Peev71c73a22017-03-21 16:35:51 +0000448 mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700449 mIsLogicalCamera(false), mResourceCost(resourceCost),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800450 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700451 mParentProvider(parentProvider), mHasFlashUnit(false),
452 mPublicCameraIds(publicCameraIds) {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800453 virtual ~DeviceInfo();
454 protected:
455 bool mHasFlashUnit;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700456 const std::vector<std::string>& mPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800457
458 template<class InterfaceT>
459 static status_t setTorchMode(InterfaceT& interface, bool enabled);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700460
461 template<class InterfaceT>
462 status_t setTorchModeForDevice(bool enabled) {
463 // Don't save the ICameraProvider interface here because we assume that this was
464 // called from CameraProviderManager::setTorchMode(), which does save it.
465 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
466 return DeviceInfo::setTorchMode(interface, enabled);
467 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800468 };
469 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800470 std::unordered_set<std::string> mUniqueCameraIds;
Yin-Chia Yehe8e9e192017-03-16 15:23:51 -0700471 int mUniqueDeviceCount;
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700472 std::vector<std::string> mUniqueAPI1CompatibleCameraIds;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700473 // The initial public camera IDs published by the camera provider.
474 // Currently logical multi-camera is not supported for hot-plug camera.
475 // And we use this list to keep track of initial public camera IDs
476 // advertised by the provider, and to distinguish against "hidden"
477 // physical camera IDs.
478 std::vector<std::string> mProviderPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800479
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800480 // HALv1-specific camera fields, including the actual device interface
481 struct DeviceInfo1 : public DeviceInfo {
482 typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800483
484 virtual status_t setTorchMode(bool enabled) override;
485 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100486 //In case of Device1Info assume that we are always API1 compatible
487 virtual bool isAPI1Compatible() const override { return true; }
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700488 virtual status_t dumpState(int fd) override;
Emilian Peev71c73a22017-03-21 16:35:51 +0000489 DeviceInfo1(const std::string& name, const metadata_vendor_id_t tagId,
490 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800491 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700492 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700493 const std::vector<std::string>& publicCameraIds,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800494 sp<InterfaceT> interface);
495 virtual ~DeviceInfo1();
496 private:
497 CameraParameters2 mDefaultParameters;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700498 status_t cacheCameraInfo(sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800499 };
500
501 // HALv3-specific camera fields, including the actual device interface
502 struct DeviceInfo3 : public DeviceInfo {
503 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800504
505 virtual status_t setTorchMode(bool enabled) override;
506 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100507 virtual bool isAPI1Compatible() const override;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700508 virtual status_t dumpState(int fd) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800509 virtual status_t getCameraCharacteristics(
510 CameraMetadata *characteristics) const override;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700511 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
512 CameraMetadata *characteristics) const override;
Emilian Peev35ae8262018-11-08 13:11:32 +0000513 virtual status_t isSessionConfigurationSupported(
514 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
515 bool *status /*out*/)
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700516 override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800517
Emilian Peev71c73a22017-03-21 16:35:51 +0000518 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
519 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800520 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700521 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700522 const std::vector<std::string>& publicCameraIds, sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800523 virtual ~DeviceInfo3();
524 private:
525 CameraMetadata mCameraCharacteristics;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700526 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700527 void queryPhysicalCameraIds();
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700528 SystemCameraKind getSystemCameraKind();
Shuzhen Wang268a1362018-10-16 16:32:59 -0700529 status_t fixupMonochromeTags();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000530 status_t addDynamicDepthTags();
531 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag,
532 android_pixel_format_t format,
533 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/);
534 void getSupportedDurations( const CameraMetadata& ch, uint32_t tag,
535 android_pixel_format_t format,
536 const std::vector<std::tuple<size_t, size_t>>& sizes,
537 std::vector<int64_t> *durations/*out*/);
538 void getSupportedDynamicDepthDurations(const std::vector<int64_t>& depthDurations,
539 const std::vector<int64_t>& blobDurations,
540 std::vector<int64_t> *dynamicDepthDurations /*out*/);
Emilian Peevcbf174b2019-01-25 14:38:59 -0800541 static bool isDepthPhotoLibraryPresent();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000542 static void getSupportedDynamicDepthSizes(
543 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
544 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
545 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
546 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/);
Shuzhen Wang268a1362018-10-16 16:32:59 -0700547 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys,
548 uint32_t keyTag);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800549 status_t fillHeicStreamCombinations(std::vector<int32_t>* outputs,
550 std::vector<int64_t>* durations,
551 std::vector<int64_t>* stallDurations,
552 const camera_metadata_entry& halStreamConfigs,
553 const camera_metadata_entry& halStreamDurations);
554 status_t deriveHeicTags();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800555 };
556
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800557 private:
558 std::string mType;
559 uint32_t mId;
560
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700561 std::mutex mLock;
562
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800563 CameraProviderManager *mManager;
564
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800565 bool mInitialized = false;
566
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800567 // Templated method to instantiate the right kind of DeviceInfo and call the
568 // right CameraProvider getCameraDeviceInterface_* method.
569 template<class DeviceInfoT>
570 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
Emilian Peev71c73a22017-03-21 16:35:51 +0000571 const metadata_vendor_id_t tagId, const std::string &id,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700572 uint16_t minorVersion);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800573
574 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
575 template<class InterfaceT>
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700576 sp<InterfaceT> startDeviceInterface(const std::string &name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800577
578 // Parse provider instance name for type and id
579 static status_t parseProviderName(const std::string& name,
580 std::string *type, uint32_t *id);
581
582 // Parse device instance name for device version, type, and id.
583 static status_t parseDeviceName(const std::string& name,
584 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
Emilian Peev71c73a22017-03-21 16:35:51 +0000585
586 // Generate vendor tag id
587 static metadata_vendor_id_t generateVendorTagId(const std::string &name);
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100588
589 void removeDevice(std::string id);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800590 };
591
592 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
593 // and the calling code doesn't mutate the list of providers or their lists of devices.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800594 // Finds the first device of the given ID that falls within the requested version range
595 // minVersion <= deviceVersion < maxVersion
596 // No guarantees on the order of traversal
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800597 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800598 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
599 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800600
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700601 status_t addProviderLocked(const std::string& newProvider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700602
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800603 status_t removeProvider(const std::string& provider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700604 sp<StatusListener> getStatusListener() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800605
606 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
607
608 std::vector<sp<ProviderInfo>> mProviders;
609
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700610 void addProviderToMap(
611 const std::string &cameraId,
612 sp<hardware::camera::provider::V2_4::ICameraProvider> provider,
613 bool isTorchUsage);
614 void removeCameraIdFromMap(
615 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> &map,
616 const std::string &cameraId);
617
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800618 static const char* deviceStatusToString(
619 const hardware::camera::common::V1_0::CameraDeviceStatus&);
620 static const char* torchStatusToString(
621 const hardware::camera::common::V1_0::TorchModeStatus&);
622
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700623 status_t getCameraCharacteristicsLocked(const std::string &id,
624 CameraMetadata* characteristics) const;
625 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700626
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700627 status_t getSystemCameraKindLocked(const std::string& id, SystemCameraKind *kind) const;
628 std::pair<bool, ProviderInfo::DeviceInfo *> isHiddenPhysicalCameraInternal(const std::string& cameraId) const;
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700629
630 void collectDeviceIdsLocked(const std::vector<std::string> deviceIds,
631 std::vector<std::string>& normalDeviceIds,
632 std::vector<std::string>& systemCameraDeviceIds) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800633};
634
635} // namespace android
636
637#endif