blob: 9cce2452a252aa9bf4ab6c826d147d567f7ec04e [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>
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -070021#include <set>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080022#include <string>
23#include <mutex>
24
25#include <camera/CameraParameters2.h>
26#include <camera/CameraMetadata.h>
27#include <camera/CameraBase.h>
28#include <utils/Errors.h>
29#include <android/hardware/camera/common/1.0/types.h>
30#include <android/hardware/camera/provider/2.4/ICameraProvider.h>
31//#include <android/hardware/camera/provider/2.4/ICameraProviderCallbacks.h>
32#include <android/hidl/manager/1.0/IServiceNotification.h>
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080033#include <camera/VendorTagDescriptor.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080034
35namespace android {
36
37/**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080038 * The vendor tag descriptor class that takes HIDL vendor tag information as
39 * input. Not part of VendorTagDescriptor class because that class is used
40 * in AIDL generated sources which don't have access to HIDL headers.
41 */
42class HidlVendorTagDescriptor : public VendorTagDescriptor {
43public:
44 /**
45 * Create a VendorTagDescriptor object from the HIDL VendorTagSection
46 * vector.
47 *
48 * Returns OK on success, or a negative error code.
49 */
50 static status_t createDescriptorFromHidl(
51 const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts,
52 /*out*/
53 sp<VendorTagDescriptor>& descriptor);
54};
55
56/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080057 * A manager for all camera providers available on an Android device.
58 *
59 * Responsible for enumerating providers and the individual camera devices
60 * they export, both at startup and as providers and devices are added/removed.
61 *
62 * Provides methods for requesting information about individual devices and for
63 * opening them for active use.
64 *
65 */
66class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification {
67public:
68
69 ~CameraProviderManager();
70
71 // Tiny proxy for the static methods in a HIDL interface that communicate with the hardware
72 // service manager, to be replacable in unit tests with a fake.
73 struct ServiceInteractionProxy {
74 virtual bool registerForNotifications(
75 const std::string &serviceName,
76 const sp<hidl::manager::V1_0::IServiceNotification>
77 &notification) = 0;
78 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
79 const std::string &serviceName) = 0;
80 virtual ~ServiceInteractionProxy() {}
81 };
82
83 // Standard use case - call into the normal generated static methods which invoke
84 // the real hardware service manager
85 struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
86 virtual bool registerForNotifications(
87 const std::string &serviceName,
88 const sp<hidl::manager::V1_0::IServiceNotification>
89 &notification) override {
90 return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
91 serviceName, notification);
92 }
93 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
94 const std::string &serviceName) override {
95 return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
96 }
97 };
98
99 /**
100 * Listener interface for device/torch status changes
101 */
102 struct StatusListener : virtual public RefBase {
103 ~StatusListener() {}
104
105 virtual void onDeviceStatusChanged(const String8 &cameraId,
106 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
107 virtual void onTorchStatusChanged(const String8 &cameraId,
108 hardware::camera::common::V1_0::TorchModeStatus newStatus) = 0;
109 };
110
111 /**
112 * Initialize the manager and give it a status listener; optionally accepts a service
113 * interaction proxy.
114 *
115 * The default proxy communicates via the hardware service manager; alternate proxies can be
116 * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
117 */
118 status_t initialize(wp<StatusListener> listener,
119 ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);
120
121 /**
122 * Retrieve the total number of available cameras. This value may change dynamically as cameras
123 * are added or removed.
124 */
125 int getCameraCount() const;
126
127 /**
Emilian Peevf53f66e2017-04-11 14:29:43 +0100128 * Retrieve the number of API1 compatible cameras; these are internal and
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800129 * backwards-compatible. This is the set of cameras that will be
130 * accessible via the old camera API, with IDs in range of
Emilian Peevf53f66e2017-04-11 14:29:43 +0100131 * [0, getAPI1CompatibleCameraCount()-1]. This value is not expected to change dynamically.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800132 */
Emilian Peevf53f66e2017-04-11 14:29:43 +0100133 int getAPI1CompatibleCameraCount() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800134
135 std::vector<std::string> getCameraDeviceIds() const;
136
Emilian Peevf53f66e2017-04-11 14:29:43 +0100137 std::vector<std::string> getAPI1CompatibleCameraDeviceIds() const;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700138
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800139 /**
140 * Return true if a device with a given ID and major version exists
141 */
142 bool isValidDevice(const std::string &id, uint16_t majorVersion) const;
143
144 /**
145 * Return true if a device with a given ID has a flash unit. Returns false
146 * for devices that are unknown.
147 */
148 bool hasFlashUnit(const std::string &id) const;
149
150 /**
151 * Return the resource cost of this camera device
152 */
153 status_t getResourceCost(const std::string &id,
154 hardware::camera::common::V1_0::CameraResourceCost* cost) const;
155
156 /**
157 * Return the old camera API camera info
158 */
159 status_t getCameraInfo(const std::string &id,
160 hardware::CameraInfo* info) const;
161
162 /**
163 * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does
164 * not have a v3 or newer HAL version.
165 */
166 status_t getCameraCharacteristics(const std::string &id,
167 CameraMetadata* characteristics) const;
168
169 /**
170 * Return the highest supported device interface version for this ID
171 */
172 status_t getHighestSupportedVersion(const std::string &id,
173 hardware::hidl_version *v);
174
175 /**
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700176 * Check if a given camera device support setTorchMode API.
177 */
178 bool supportSetTorchMode(const std::string &id);
179
180 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800181 * Turn on or off the flashlight on a given camera device.
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700182 * May fail if the device does not support this API, is in active use, or if the device
183 * doesn't exist, etc.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800184 */
185 status_t setTorchMode(const std::string &id, bool enabled);
186
187 /**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800188 * Setup vendor tags for all registered providers
189 */
190 status_t setUpVendorTags();
191
192 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800193 * Open an active session to a camera device.
194 *
195 * This fully powers on the camera device hardware, and returns a handle to a
196 * session to be used for hardware configuration and operation.
197 */
198 status_t openSession(const std::string &id,
199 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
200 /*out*/
201 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session);
202
203 status_t openSession(const std::string &id,
204 const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback,
205 /*out*/
206 sp<hardware::camera::device::V1_0::ICameraDevice> *session);
207
208 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800209 * IServiceNotification::onRegistration
210 * Invoked by the hardware service manager when a new camera provider is registered
211 */
212 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
213 const hardware::hidl_string& name,
214 bool preexisting) override;
215
216 /**
217 * Dump out information about available providers and devices
218 */
219 status_t dump(int fd, const Vector<String16>& args);
220
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800221 /**
222 * Conversion methods between HAL Status and status_t and strings
223 */
224 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
225 static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
226
Emilian Peev71c73a22017-03-21 16:35:51 +0000227 /*
228 * Return provider type for a specific device.
229 */
230 metadata_vendor_id_t getProviderTagIdLocked(const std::string& id,
231 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
232 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
233
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800234private:
235 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
236 mutable std::mutex mInterfaceMutex;
237
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800238 // the status listener update callbacks will lock mStatusMutex
239 mutable std::mutex mStatusListenerMutex;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800240 wp<StatusListener> mListener;
241 ServiceInteractionProxy* mServiceProxy;
242
243 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
244
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700245 struct ProviderInfo :
246 virtual public hardware::camera::provider::V2_4::ICameraProviderCallback,
247 virtual public hardware::hidl_death_recipient
248 {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800249 const std::string mProviderName;
250 const sp<hardware::camera::provider::V2_4::ICameraProvider> mInterface;
Emilian Peev71c73a22017-03-21 16:35:51 +0000251 const metadata_vendor_id_t mProviderTagid;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800252
253 ProviderInfo(const std::string &providerName,
254 sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
255 CameraProviderManager *manager);
256 ~ProviderInfo();
257
258 status_t initialize();
259
260 const std::string& getType() const;
261
262 status_t addDevice(const std::string& name,
263 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
264 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
265 /*out*/ std::string *parsedId = nullptr);
266
267 status_t dump(int fd, const Vector<String16>& args) const;
268
269 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
270 virtual hardware::Return<void> cameraDeviceStatusChange(
271 const hardware::hidl_string& cameraDeviceName,
272 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
273 virtual hardware::Return<void> torchModeStatusChange(
274 const hardware::hidl_string& cameraDeviceName,
275 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
276
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700277 // hidl_death_recipient interface - this locks the parent mInterfaceMutex
278 virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
279
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800280 // Basic device information, common to all camera devices
281 struct DeviceInfo {
282 const std::string mName; // Full instance name
283 const std::string mId; // ID section of full name
284 const hardware::hidl_version mVersion;
Emilian Peev71c73a22017-03-21 16:35:51 +0000285 const metadata_vendor_id_t mProviderTagid;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800286
287 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
288
289 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
290
291 bool hasFlashUnit() const { return mHasFlashUnit; }
292 virtual status_t setTorchMode(bool enabled) = 0;
293 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100294 virtual bool isAPI1Compatible() const = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800295 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
296 (void) characteristics;
297 return INVALID_OPERATION;
298 }
299
Emilian Peev71c73a22017-03-21 16:35:51 +0000300 DeviceInfo(const std::string& name, const metadata_vendor_id_t tagId,
301 const std::string &id, const hardware::hidl_version& version,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800302 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost) :
Emilian Peev71c73a22017-03-21 16:35:51 +0000303 mName(name), mId(id), mVersion(version), mProviderTagid(tagId),
304 mResourceCost(resourceCost),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800305 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
306 mHasFlashUnit(false) {}
307 virtual ~DeviceInfo();
308 protected:
309 bool mHasFlashUnit;
310
311 template<class InterfaceT>
312 static status_t setTorchMode(InterfaceT& interface, bool enabled);
313 };
314 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700315 std::set<std::string> mUniqueCameraIds;
Yin-Chia Yehe8e9e192017-03-16 15:23:51 -0700316 int mUniqueDeviceCount;
Emilian Peevcdb74a62017-05-11 20:29:52 +0100317 std::set<std::string> mUniqueAPI1CompatibleCameraIds;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800318
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800319 // HALv1-specific camera fields, including the actual device interface
320 struct DeviceInfo1 : public DeviceInfo {
321 typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT;
322 const sp<InterfaceT> mInterface;
323
324 virtual status_t setTorchMode(bool enabled) override;
325 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100326 //In case of Device1Info assume that we are always API1 compatible
327 virtual bool isAPI1Compatible() const override { return true; }
Emilian Peev71c73a22017-03-21 16:35:51 +0000328 DeviceInfo1(const std::string& name, const metadata_vendor_id_t tagId,
329 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800330 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
331 sp<InterfaceT> interface);
332 virtual ~DeviceInfo1();
333 private:
334 CameraParameters2 mDefaultParameters;
335 };
336
337 // HALv3-specific camera fields, including the actual device interface
338 struct DeviceInfo3 : public DeviceInfo {
339 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
340 const sp<InterfaceT> mInterface;
341
342 virtual status_t setTorchMode(bool enabled) override;
343 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100344 virtual bool isAPI1Compatible() const override;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800345 virtual status_t getCameraCharacteristics(
346 CameraMetadata *characteristics) const override;
347
Emilian Peev71c73a22017-03-21 16:35:51 +0000348 DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
349 const std::string &id, uint16_t minorVersion,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800350 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
351 sp<InterfaceT> interface);
352 virtual ~DeviceInfo3();
353 private:
354 CameraMetadata mCameraCharacteristics;
355 };
356
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800357 private:
358 std::string mType;
359 uint32_t mId;
360
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700361 std::mutex mLock;
362
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800363 CameraProviderManager *mManager;
364
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800365 // Templated method to instantiate the right kind of DeviceInfo and call the
366 // right CameraProvider getCameraDeviceInterface_* method.
367 template<class DeviceInfoT>
368 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
Emilian Peev71c73a22017-03-21 16:35:51 +0000369 const metadata_vendor_id_t tagId, const std::string &id,
370 uint16_t minorVersion) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800371
372 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
373 template<class InterfaceT>
374 sp<InterfaceT> getDeviceInterface(const std::string &name) const;
375
376 // Parse provider instance name for type and id
377 static status_t parseProviderName(const std::string& name,
378 std::string *type, uint32_t *id);
379
380 // Parse device instance name for device version, type, and id.
381 static status_t parseDeviceName(const std::string& name,
382 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
Emilian Peev71c73a22017-03-21 16:35:51 +0000383
384 // Generate vendor tag id
385 static metadata_vendor_id_t generateVendorTagId(const std::string &name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800386 };
387
388 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
389 // and the calling code doesn't mutate the list of providers or their lists of devices.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800390 // Finds the first device of the given ID that falls within the requested version range
391 // minVersion <= deviceVersion < maxVersion
392 // No guarantees on the order of traversal
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800393 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800394 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
395 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800396
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700397 status_t addProviderLocked(const std::string& newProvider, bool expected = true);
398
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800399 status_t removeProvider(const std::string& provider);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700400 sp<StatusListener> getStatusListener() const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800401
402 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
403
404 std::vector<sp<ProviderInfo>> mProviders;
405
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800406 static const char* deviceStatusToString(
407 const hardware::camera::common::V1_0::CameraDeviceStatus&);
408 static const char* torchStatusToString(
409 const hardware::camera::common::V1_0::TorchModeStatus&);
410
411};
412
413} // namespace android
414
415#endif