blob: fbd7d2e8f80f3faed07a877f7136bae8234bd8e9 [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>
31#include <android/hardware/camera/provider/2.4/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/hardware/camera/provider/2.4/ICameraProviderCallbacks.h>
34#include <android/hidl/manager/1.0/IServiceNotification.h>
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080035#include <camera/VendorTagDescriptor.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080036
37namespace android {
38
39/**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080040 * The vendor tag descriptor class that takes HIDL vendor tag information as
41 * input. Not part of VendorTagDescriptor class because that class is used
42 * in AIDL generated sources which don't have access to HIDL headers.
43 */
44class HidlVendorTagDescriptor : public VendorTagDescriptor {
45public:
46 /**
47 * Create a VendorTagDescriptor object from the HIDL VendorTagSection
48 * vector.
49 *
50 * Returns OK on success, or a negative error code.
51 */
52 static status_t createDescriptorFromHidl(
53 const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts,
54 /*out*/
55 sp<VendorTagDescriptor>& descriptor);
56};
57
58/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080059 * A manager for all camera providers available on an Android device.
60 *
61 * Responsible for enumerating providers and the individual camera devices
62 * they export, both at startup and as providers and devices are added/removed.
63 *
64 * Provides methods for requesting information about individual devices and for
65 * opening them for active use.
66 *
67 */
68class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification {
69public:
70
71 ~CameraProviderManager();
72
73 // Tiny proxy for the static methods in a HIDL interface that communicate with the hardware
74 // service manager, to be replacable in unit tests with a fake.
75 struct ServiceInteractionProxy {
76 virtual bool registerForNotifications(
77 const std::string &serviceName,
78 const sp<hidl::manager::V1_0::IServiceNotification>
79 &notification) = 0;
80 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
81 const std::string &serviceName) = 0;
82 virtual ~ServiceInteractionProxy() {}
83 };
84
85 // Standard use case - call into the normal generated static methods which invoke
86 // the real hardware service manager
87 struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
88 virtual bool registerForNotifications(
89 const std::string &serviceName,
90 const sp<hidl::manager::V1_0::IServiceNotification>
91 &notification) override {
92 return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
93 serviceName, notification);
94 }
95 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
96 const std::string &serviceName) override {
97 return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
98 }
99 };
100
101 /**
102 * Listener interface for device/torch status changes
103 */
104 struct StatusListener : virtual public RefBase {
105 ~StatusListener() {}
106
107 virtual void onDeviceStatusChanged(const String8 &cameraId,
108 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
109 virtual void onTorchStatusChanged(const String8 &cameraId,
110 hardware::camera::common::V1_0::TorchModeStatus newStatus) = 0;
Emilian Peevaee727d2017-05-04 16:35:48 +0100111 virtual void onNewProviderRegistered() = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800112 };
113
114 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700115 * Represents the mode a camera device is currently in
116 */
117 enum class DeviceMode {
118 TORCH,
119 CAMERA
120 };
121
122 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800123 * Initialize the manager and give it a status listener; optionally accepts a service
124 * interaction proxy.
125 *
126 * The default proxy communicates via the hardware service manager; alternate proxies can be
127 * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
128 */
129 status_t initialize(wp<StatusListener> listener,
130 ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);
131
132 /**
133 * Retrieve the total number of available cameras. This value may change dynamically as cameras
134 * are added or removed.
135 */
136 int getCameraCount() const;
137
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800138 std::vector<std::string> getCameraDeviceIds() const;
139
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800140 /**
Emilian Peevf53f66e2017-04-11 14:29:43 +0100141 * Retrieve the number of API1 compatible cameras; these are internal and
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800142 * backwards-compatible. This is the set of cameras that will be
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800143 * accessible via the old camera API.
144 * The return value may change dynamically due to external camera hotplug.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800145 */
Emilian Peevf53f66e2017-04-11 14:29:43 +0100146 std::vector<std::string> getAPI1CompatibleCameraDeviceIds() const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700147
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800148 /**
149 * Return true if a device with a given ID and major version exists
150 */
151 bool isValidDevice(const std::string &id, uint16_t majorVersion) const;
152
153 /**
154 * Return true if a device with a given ID has a flash unit. Returns false
155 * for devices that are unknown.
156 */
157 bool hasFlashUnit(const std::string &id) const;
158
159 /**
160 * Return the resource cost of this camera device
161 */
162 status_t getResourceCost(const std::string &id,
163 hardware::camera::common::V1_0::CameraResourceCost* cost) const;
164
165 /**
166 * Return the old camera API camera info
167 */
168 status_t getCameraInfo(const std::string &id,
169 hardware::CameraInfo* info) const;
170
171 /**
172 * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does
173 * not have a v3 or newer HAL version.
174 */
175 status_t getCameraCharacteristics(const std::string &id,
176 CameraMetadata* characteristics) const;
177
178 /**
Emilian Peev35ae8262018-11-08 13:11:32 +0000179 * Check for device support of specific stream combination.
180 */
181 status_t isSessionConfigurationSupported(const std::string& id,
182 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
183 bool *status /*out*/) const;
184
185 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800186 * Return the highest supported device interface version for this ID
187 */
188 status_t getHighestSupportedVersion(const std::string &id,
189 hardware::hidl_version *v);
190
191 /**
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700192 * Check if a given camera device support setTorchMode API.
193 */
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700194 bool supportSetTorchMode(const std::string &id) const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700195
196 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800197 * Turn on or off the flashlight on a given camera device.
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700198 * May fail if the device does not support this API, is in active use, or if the device
199 * doesn't exist, etc.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800200 */
201 status_t setTorchMode(const std::string &id, bool enabled);
202
203 /**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800204 * Setup vendor tags for all registered providers
205 */
206 status_t setUpVendorTags();
207
208 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800209 * Open an active session to a camera device.
210 *
211 * This fully powers on the camera device hardware, and returns a handle to a
212 * session to be used for hardware configuration and operation.
213 */
214 status_t openSession(const std::string &id,
215 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
216 /*out*/
217 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session);
218
219 status_t openSession(const std::string &id,
220 const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback,
221 /*out*/
222 sp<hardware::camera::device::V1_0::ICameraDevice> *session);
223
224 /**
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700225 * Save the ICameraProvider while it is being used by a camera or torch client
226 */
227 void saveRef(DeviceMode usageType, const std::string &cameraId,
228 sp<hardware::camera::provider::V2_4::ICameraProvider> provider);
229
230 /**
231 * Notify that the camera or torch is no longer being used by a camera client
232 */
233 void removeRef(DeviceMode usageType, const std::string &cameraId);
234
235 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800236 * IServiceNotification::onRegistration
237 * Invoked by the hardware service manager when a new camera provider is registered
238 */
239 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
240 const hardware::hidl_string& name,
241 bool preexisting) override;
242
243 /**
244 * Dump out information about available providers and devices
245 */
246 status_t dump(int fd, const Vector<String16>& args);
247
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800248 /**
249 * Conversion methods between HAL Status and status_t and strings
250 */
251 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
252 static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
253
Emilian Peev71c73a22017-03-21 16:35:51 +0000254 /*
255 * Return provider type for a specific device.
256 */
257 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id,
258 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
259 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
260
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700261 /*
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700262 * Check if a camera is a logical camera. And if yes, return
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700263 * the physical camera ids.
264 */
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700265 bool isLogicalCamera(const std::string& id, std::vector<std::string>* physicalCameraIds);
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700266
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700267 bool isHiddenPhysicalCamera(const std::string& cameraId);
Emilian Peev538c90e2018-12-17 18:03:19 +0000268
269 static const float kDepthARTolerance;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800270private:
271 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
272 mutable std::mutex mInterfaceMutex;
273
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800274 // the status listener update callbacks will lock mStatusMutex
275 mutable std::mutex mStatusListenerMutex;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800276 wp<StatusListener> mListener;
277 ServiceInteractionProxy* mServiceProxy;
278
Shuzhen Wang6ba8eb22018-07-08 13:10:44 -0700279 // mProviderLifecycleLock is locked during onRegistration and removeProvider
280 mutable std::mutex mProviderLifecycleLock;
281
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800282 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
283
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700284 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
285 // ICameraProvider alive while it is in use by the camera with the given ID for camera
286 // capabilities
287 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
288 mCameraProviderByCameraId;
289
290 // Mapping from CameraDevice IDs to CameraProviders. This map is used to keep the
291 // ICameraProvider alive while it is in use by the camera with the given ID for torch
292 // capabilities
293 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>>
294 mTorchProviderByCameraId;
295
296 // Lock for accessing mCameraProviderByCameraId and mTorchProviderByCameraId
297 std::mutex mProviderInterfaceMapLock;
298
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700299 struct ProviderInfo :
300 virtual public hardware::camera::provider::V2_4::ICameraProviderCallback,
301 virtual public hardware::hidl_death_recipient
302 {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800303 const std::string mProviderName;
Emilian Peev71c73a22017-03-21 16:35:51 +0000304 const metadata_vendor_id_t mProviderTagid;
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800305 sp<VendorTagDescriptor> mVendorTagDescriptor;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700306 bool mSetTorchModeSupported;
307 bool mIsRemote;
308
309 // This pointer is used to keep a reference to the ICameraProvider that was last accessed.
310 wp<hardware::camera::provider::V2_4::ICameraProvider> mActiveInterface;
311
312 sp<hardware::camera::provider::V2_4::ICameraProvider> mSavedInterface;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800313
314 ProviderInfo(const std::string &providerName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800315 CameraProviderManager *manager);
316 ~ProviderInfo();
317
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700318 status_t initialize(sp<hardware::camera::provider::V2_4::ICameraProvider>& interface);
319
320 const sp<hardware::camera::provider::V2_4::ICameraProvider> startProviderInterface();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800321
322 const std::string& getType() const;
323
324 status_t addDevice(const std::string& name,
325 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
326 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
327 /*out*/ std::string *parsedId = nullptr);
328
329 status_t dump(int fd, const Vector<String16>& args) const;
330
331 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
332 virtual hardware::Return<void> cameraDeviceStatusChange(
333 const hardware::hidl_string& cameraDeviceName,
334 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
335 virtual hardware::Return<void> torchModeStatusChange(
336 const hardware::hidl_string& cameraDeviceName,
337 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
338
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700339 // hidl_death_recipient interface - this locks the parent mInterfaceMutex
340 virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
341
Peter Kalauskas1b3c9072018-11-07 12:41:53 -0800342 /**
343 * Setup vendor tags for this provider
344 */
345 status_t setUpVendorTags();
346
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800347 // Basic device information, common to all camera devices
348 struct DeviceInfo {
349 const std::string mName; // Full instance name
350 const std::string mId; // ID section of full name
351 const hardware::hidl_version mVersion;
Emilian Peev71c73a22017-03-21 16:35:51 +0000352 const metadata_vendor_id_t mProviderTagid;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700353 bool mIsLogicalCamera;
354 std::vector<std::string> mPhysicalIds;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700355 hardware::CameraInfo mInfo;
356 sp<IBase> mSavedInterface;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800357
358 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
359
360 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
361
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700362 sp<ProviderInfo> mParentProvider;
363
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800364 bool hasFlashUnit() const { return mHasFlashUnit; }
365 virtual status_t setTorchMode(bool enabled) = 0;
366 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100367 virtual bool isAPI1Compatible() const = 0;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700368 virtual status_t dumpState(int fd) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800369 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
370 (void) characteristics;
371 return INVALID_OPERATION;
372 }
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700373 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
374 CameraMetadata *characteristics) const {
375 (void) physicalCameraId;
376 (void) characteristics;
377 return INVALID_OPERATION;
378 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800379
Emilian Peev35ae8262018-11-08 13:11:32 +0000380 virtual status_t isSessionConfigurationSupported(
381 const hardware::camera::device::V3_4::StreamConfiguration &/*configuration*/,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700382 bool * /*status*/) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000383 return INVALID_OPERATION;
384 }
385
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700386 template<class InterfaceT>
387 sp<InterfaceT> startDeviceInterface();
388
Emilian Peev71c73a22017-03-21 16:35:51 +0000389 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
390 const std::string &id, const hardware::hidl_version& version,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700391 const std::vector<std::string>& publicCameraIds,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700392 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
393 sp<ProviderInfo> parentProvider) :
Emilian Peev71c73a22017-03-21 16:35:51 +0000394 mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700395 mIsLogicalCamera(false), mResourceCost(resourceCost),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800396 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700397 mParentProvider(parentProvider), mHasFlashUnit(false),
398 mPublicCameraIds(publicCameraIds) {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800399 virtual ~DeviceInfo();
400 protected:
401 bool mHasFlashUnit;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700402 const std::vector<std::string>& mPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800403
404 template<class InterfaceT>
405 static status_t setTorchMode(InterfaceT& interface, bool enabled);
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700406
407 template<class InterfaceT>
408 status_t setTorchModeForDevice(bool enabled) {
409 // Don't save the ICameraProvider interface here because we assume that this was
410 // called from CameraProviderManager::setTorchMode(), which does save it.
411 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
412 return DeviceInfo::setTorchMode(interface, enabled);
413 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800414 };
415 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800416 std::unordered_set<std::string> mUniqueCameraIds;
Yin-Chia Yehe8e9e192017-03-16 15:23:51 -0700417 int mUniqueDeviceCount;
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700418 std::vector<std::string> mUniqueAPI1CompatibleCameraIds;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700419 // The initial public camera IDs published by the camera provider.
420 // Currently logical multi-camera is not supported for hot-plug camera.
421 // And we use this list to keep track of initial public camera IDs
422 // advertised by the provider, and to distinguish against "hidden"
423 // physical camera IDs.
424 std::vector<std::string> mProviderPublicCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800425
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800426 // HALv1-specific camera fields, including the actual device interface
427 struct DeviceInfo1 : public DeviceInfo {
428 typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800429
430 virtual status_t setTorchMode(bool enabled) override;
431 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100432 //In case of Device1Info assume that we are always API1 compatible
433 virtual bool isAPI1Compatible() const override { return true; }
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700434 virtual status_t dumpState(int fd) override;
Emilian Peev71c73a22017-03-21 16:35:51 +0000435 DeviceInfo1(const std::string& name, const metadata_vendor_id_t tagId,
436 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800437 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700438 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700439 const std::vector<std::string>& publicCameraIds,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800440 sp<InterfaceT> interface);
441 virtual ~DeviceInfo1();
442 private:
443 CameraParameters2 mDefaultParameters;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700444 status_t cacheCameraInfo(sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800445 };
446
447 // HALv3-specific camera fields, including the actual device interface
448 struct DeviceInfo3 : public DeviceInfo {
449 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800450
451 virtual status_t setTorchMode(bool enabled) override;
452 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100453 virtual bool isAPI1Compatible() const override;
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700454 virtual status_t dumpState(int fd) override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800455 virtual status_t getCameraCharacteristics(
456 CameraMetadata *characteristics) const override;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700457 virtual status_t getPhysicalCameraCharacteristics(const std::string& physicalCameraId,
458 CameraMetadata *characteristics) const override;
Emilian Peev35ae8262018-11-08 13:11:32 +0000459 virtual status_t isSessionConfigurationSupported(
460 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
461 bool *status /*out*/)
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700462 override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800463
Emilian Peev71c73a22017-03-21 16:35:51 +0000464 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
465 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800466 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700467 sp<ProviderInfo> parentProvider,
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700468 const std::vector<std::string>& publicCameraIds, sp<InterfaceT> interface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800469 virtual ~DeviceInfo3();
470 private:
471 CameraMetadata mCameraCharacteristics;
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700472 std::unordered_map<std::string, CameraMetadata> mPhysicalCameraCharacteristics;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700473 void queryPhysicalCameraIds();
Shuzhen Wang268a1362018-10-16 16:32:59 -0700474 status_t fixupMonochromeTags();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000475 status_t addDynamicDepthTags();
476 static void getSupportedSizes(const CameraMetadata& ch, uint32_t tag,
477 android_pixel_format_t format,
478 std::vector<std::tuple<size_t, size_t>> *sizes /*out*/);
479 void getSupportedDurations( const CameraMetadata& ch, uint32_t tag,
480 android_pixel_format_t format,
481 const std::vector<std::tuple<size_t, size_t>>& sizes,
482 std::vector<int64_t> *durations/*out*/);
483 void getSupportedDynamicDepthDurations(const std::vector<int64_t>& depthDurations,
484 const std::vector<int64_t>& blobDurations,
485 std::vector<int64_t> *dynamicDepthDurations /*out*/);
Emilian Peevcbf174b2019-01-25 14:38:59 -0800486 static bool isDepthPhotoLibraryPresent();
Emilian Peev4c6d2b52019-01-04 17:13:56 +0000487 static void getSupportedDynamicDepthSizes(
488 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
489 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
490 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
491 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/);
Shuzhen Wang268a1362018-10-16 16:32:59 -0700492 status_t removeAvailableKeys(CameraMetadata& c, const std::vector<uint32_t>& keys,
493 uint32_t keyTag);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800494 };
495
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800496 private:
497 std::string mType;
498 uint32_t mId;
499
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700500 std::mutex mLock;
501
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800502 CameraProviderManager *mManager;
503
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800504 bool mInitialized = false;
505
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800506 // Templated method to instantiate the right kind of DeviceInfo and call the
507 // right CameraProvider getCameraDeviceInterface_* method.
508 template<class DeviceInfoT>
509 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
Emilian Peev71c73a22017-03-21 16:35:51 +0000510 const metadata_vendor_id_t tagId, const std::string &id,
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700511 uint16_t minorVersion);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800512
513 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
514 template<class InterfaceT>
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700515 sp<InterfaceT> startDeviceInterface(const std::string &name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800516
517 // Parse provider instance name for type and id
518 static status_t parseProviderName(const std::string& name,
519 std::string *type, uint32_t *id);
520
521 // Parse device instance name for device version, type, and id.
522 static status_t parseDeviceName(const std::string& name,
523 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
Emilian Peev71c73a22017-03-21 16:35:51 +0000524
525 // Generate vendor tag id
526 static metadata_vendor_id_t generateVendorTagId(const std::string &name);
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100527
528 void removeDevice(std::string id);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800529 };
530
531 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
532 // and the calling code doesn't mutate the list of providers or their lists of devices.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800533 // Finds the first device of the given ID that falls within the requested version range
534 // minVersion <= deviceVersion < maxVersion
535 // No guarantees on the order of traversal
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800536 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800537 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
538 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800539
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700540 status_t addProviderLocked(const std::string& newProvider, bool expected = true);
541
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800542 status_t removeProvider(const std::string& provider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700543 sp<StatusListener> getStatusListener() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800544
545 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
546
547 std::vector<sp<ProviderInfo>> mProviders;
548
Peter Kalauskasa29c1352018-10-10 12:05:42 -0700549 void addProviderToMap(
550 const std::string &cameraId,
551 sp<hardware::camera::provider::V2_4::ICameraProvider> provider,
552 bool isTorchUsage);
553 void removeCameraIdFromMap(
554 std::unordered_map<std::string, sp<hardware::camera::provider::V2_4::ICameraProvider>> &map,
555 const std::string &cameraId);
556
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800557 static const char* deviceStatusToString(
558 const hardware::camera::common::V1_0::CameraDeviceStatus&);
559 static const char* torchStatusToString(
560 const hardware::camera::common::V1_0::TorchModeStatus&);
561
Shuzhen Wange8aceb52018-05-21 12:00:56 -0700562 status_t getCameraCharacteristicsLocked(const std::string &id,
563 CameraMetadata* characteristics) const;
564 void filterLogicalCameraIdsLocked(std::vector<std::string>& deviceIds) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800565};
566
567} // namespace android
568
569#endif