blob: 5ae16cdbb8f6f1c5df49694ee220e2ff528b5157 [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>
21#include <string>
22#include <mutex>
23
24#include <camera/CameraParameters2.h>
25#include <camera/CameraMetadata.h>
26#include <camera/CameraBase.h>
27#include <utils/Errors.h>
28#include <android/hardware/camera/common/1.0/types.h>
29#include <android/hardware/camera/provider/2.4/ICameraProvider.h>
30//#include <android/hardware/camera/provider/2.4/ICameraProviderCallbacks.h>
31#include <android/hidl/manager/1.0/IServiceNotification.h>
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080032#include <camera/VendorTagDescriptor.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080033
34namespace android {
35
36/**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -080037 * The vendor tag descriptor class that takes HIDL vendor tag information as
38 * input. Not part of VendorTagDescriptor class because that class is used
39 * in AIDL generated sources which don't have access to HIDL headers.
40 */
41class HidlVendorTagDescriptor : public VendorTagDescriptor {
42public:
43 /**
44 * Create a VendorTagDescriptor object from the HIDL VendorTagSection
45 * vector.
46 *
47 * Returns OK on success, or a negative error code.
48 */
49 static status_t createDescriptorFromHidl(
50 const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts,
51 /*out*/
52 sp<VendorTagDescriptor>& descriptor);
53};
54
55/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080056 * A manager for all camera providers available on an Android device.
57 *
58 * Responsible for enumerating providers and the individual camera devices
59 * they export, both at startup and as providers and devices are added/removed.
60 *
61 * Provides methods for requesting information about individual devices and for
62 * opening them for active use.
63 *
64 */
65class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification {
66public:
67
68 ~CameraProviderManager();
69
70 // Tiny proxy for the static methods in a HIDL interface that communicate with the hardware
71 // service manager, to be replacable in unit tests with a fake.
72 struct ServiceInteractionProxy {
73 virtual bool registerForNotifications(
74 const std::string &serviceName,
75 const sp<hidl::manager::V1_0::IServiceNotification>
76 &notification) = 0;
77 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
78 const std::string &serviceName) = 0;
79 virtual ~ServiceInteractionProxy() {}
80 };
81
82 // Standard use case - call into the normal generated static methods which invoke
83 // the real hardware service manager
84 struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
85 virtual bool registerForNotifications(
86 const std::string &serviceName,
87 const sp<hidl::manager::V1_0::IServiceNotification>
88 &notification) override {
89 return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
90 serviceName, notification);
91 }
92 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
93 const std::string &serviceName) override {
94 return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
95 }
96 };
97
98 /**
99 * Listener interface for device/torch status changes
100 */
101 struct StatusListener : virtual public RefBase {
102 ~StatusListener() {}
103
104 virtual void onDeviceStatusChanged(const String8 &cameraId,
105 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) = 0;
106 virtual void onTorchStatusChanged(const String8 &cameraId,
107 hardware::camera::common::V1_0::TorchModeStatus newStatus) = 0;
108 };
109
110 /**
111 * Initialize the manager and give it a status listener; optionally accepts a service
112 * interaction proxy.
113 *
114 * The default proxy communicates via the hardware service manager; alternate proxies can be
115 * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
116 */
117 status_t initialize(wp<StatusListener> listener,
118 ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);
119
120 /**
121 * Retrieve the total number of available cameras. This value may change dynamically as cameras
122 * are added or removed.
123 */
124 int getCameraCount() const;
125
126 /**
127 * Retrieve the number of 'standard' cameras; these are internal and
128 * backwards-compatible. This is the set of cameras that will be
129 * accessible via the old camera API, with IDs in range of
130 * [0, getStandardCameraCount()-1]. This value is not expected to change dynamically.
131 */
132 int getStandardCameraCount() const;
133
134 std::vector<std::string> getCameraDeviceIds() const;
135
136 /**
137 * Return true if a device with a given ID and major version exists
138 */
139 bool isValidDevice(const std::string &id, uint16_t majorVersion) const;
140
141 /**
142 * Return true if a device with a given ID has a flash unit. Returns false
143 * for devices that are unknown.
144 */
145 bool hasFlashUnit(const std::string &id) const;
146
147 /**
148 * Return the resource cost of this camera device
149 */
150 status_t getResourceCost(const std::string &id,
151 hardware::camera::common::V1_0::CameraResourceCost* cost) const;
152
153 /**
154 * Return the old camera API camera info
155 */
156 status_t getCameraInfo(const std::string &id,
157 hardware::CameraInfo* info) const;
158
159 /**
160 * Return API2 camera characteristics - returns NAME_NOT_FOUND if a device ID does
161 * not have a v3 or newer HAL version.
162 */
163 status_t getCameraCharacteristics(const std::string &id,
164 CameraMetadata* characteristics) const;
165
166 /**
167 * Return the highest supported device interface version for this ID
168 */
169 status_t getHighestSupportedVersion(const std::string &id,
170 hardware::hidl_version *v);
171
172 /**
173 * Turn on or off the flashlight on a given camera device.
174 * May fail if the device is in active use, or if the device doesn't exist, etc.
175 */
176 status_t setTorchMode(const std::string &id, bool enabled);
177
178 /**
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800179 * Setup vendor tags for all registered providers
180 */
181 status_t setUpVendorTags();
182
183 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800184 * Open an active session to a camera device.
185 *
186 * This fully powers on the camera device hardware, and returns a handle to a
187 * session to be used for hardware configuration and operation.
188 */
189 status_t openSession(const std::string &id,
190 const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
191 /*out*/
192 sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session);
193
194 status_t openSession(const std::string &id,
195 const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback,
196 /*out*/
197 sp<hardware::camera::device::V1_0::ICameraDevice> *session);
198
199 /**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800200 * IServiceNotification::onRegistration
201 * Invoked by the hardware service manager when a new camera provider is registered
202 */
203 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
204 const hardware::hidl_string& name,
205 bool preexisting) override;
206
207 /**
208 * Dump out information about available providers and devices
209 */
210 status_t dump(int fd, const Vector<String16>& args);
211
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800212 /**
213 * Conversion methods between HAL Status and status_t and strings
214 */
215 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
216 static const char* statusToString(const hardware::camera::common::V1_0::Status& s);
217
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800218private:
219 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
220 mutable std::mutex mInterfaceMutex;
221
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800222 // the status listener update callbacks will lock mStatusMutex
223 mutable std::mutex mStatusListenerMutex;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800224 wp<StatusListener> mListener;
225 ServiceInteractionProxy* mServiceProxy;
226
227 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
228
229 struct ProviderInfo : virtual public hardware::camera::provider::V2_4::ICameraProviderCallback {
230 const std::string mProviderName;
231 const sp<hardware::camera::provider::V2_4::ICameraProvider> mInterface;
232
233 ProviderInfo(const std::string &providerName,
234 sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
235 CameraProviderManager *manager);
236 ~ProviderInfo();
237
238 status_t initialize();
239
240 const std::string& getType() const;
241
242 status_t addDevice(const std::string& name,
243 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
244 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
245 /*out*/ std::string *parsedId = nullptr);
246
247 status_t dump(int fd, const Vector<String16>& args) const;
248
249 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
250 virtual hardware::Return<void> cameraDeviceStatusChange(
251 const hardware::hidl_string& cameraDeviceName,
252 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
253 virtual hardware::Return<void> torchModeStatusChange(
254 const hardware::hidl_string& cameraDeviceName,
255 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
256
257 // Basic device information, common to all camera devices
258 struct DeviceInfo {
259 const std::string mName; // Full instance name
260 const std::string mId; // ID section of full name
261 const hardware::hidl_version mVersion;
262
263 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
264
265 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
266
267 bool hasFlashUnit() const { return mHasFlashUnit; }
268 virtual status_t setTorchMode(bool enabled) = 0;
269 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
270 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
271 (void) characteristics;
272 return INVALID_OPERATION;
273 }
274
275 DeviceInfo(const std::string& name, const std::string &id,
276 const hardware::hidl_version& version,
277 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost) :
278 mName(name), mId(id), mVersion(version), mResourceCost(resourceCost),
279 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
280 mHasFlashUnit(false) {}
281 virtual ~DeviceInfo();
282 protected:
283 bool mHasFlashUnit;
284
285 template<class InterfaceT>
286 static status_t setTorchMode(InterfaceT& interface, bool enabled);
287 };
288 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
289
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800290 // HALv1-specific camera fields, including the actual device interface
291 struct DeviceInfo1 : public DeviceInfo {
292 typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT;
293 const sp<InterfaceT> mInterface;
294
295 virtual status_t setTorchMode(bool enabled) override;
296 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
297
298 DeviceInfo1(const std::string& name, const std::string &id,
299 uint16_t minorVersion,
300 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
301 sp<InterfaceT> interface);
302 virtual ~DeviceInfo1();
303 private:
304 CameraParameters2 mDefaultParameters;
305 };
306
307 // HALv3-specific camera fields, including the actual device interface
308 struct DeviceInfo3 : public DeviceInfo {
309 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
310 const sp<InterfaceT> mInterface;
311
312 virtual status_t setTorchMode(bool enabled) override;
313 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
314 virtual status_t getCameraCharacteristics(
315 CameraMetadata *characteristics) const override;
316
317 DeviceInfo3(const std::string& name, const std::string &id,
318 uint16_t minorVersion,
319 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
320 sp<InterfaceT> interface);
321 virtual ~DeviceInfo3();
322 private:
323 CameraMetadata mCameraCharacteristics;
324 };
325
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800326 private:
327 std::string mType;
328 uint32_t mId;
329
330 CameraProviderManager *mManager;
331
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800332 // Templated method to instantiate the right kind of DeviceInfo and call the
333 // right CameraProvider getCameraDeviceInterface_* method.
334 template<class DeviceInfoT>
335 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
336 const std::string &id, uint16_t minorVersion) const;
337
338 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
339 template<class InterfaceT>
340 sp<InterfaceT> getDeviceInterface(const std::string &name) const;
341
342 // Parse provider instance name for type and id
343 static status_t parseProviderName(const std::string& name,
344 std::string *type, uint32_t *id);
345
346 // Parse device instance name for device version, type, and id.
347 static status_t parseDeviceName(const std::string& name,
348 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
349 };
350
351 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
352 // and the calling code doesn't mutate the list of providers or their lists of devices.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800353 // Finds the first device of the given ID that falls within the requested version range
354 // minVersion <= deviceVersion < maxVersion
355 // No guarantees on the order of traversal
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800356 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800357 hardware::hidl_version minVersion = hardware::hidl_version{0,0},
358 hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800359
360 status_t addProvider(const std::string& newProvider, bool expected = true);
361 status_t removeProvider(const std::string& provider);
362
363 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
364
365 std::vector<sp<ProviderInfo>> mProviders;
366
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800367 static const char* deviceStatusToString(
368 const hardware::camera::common::V1_0::CameraDeviceStatus&);
369 static const char* torchStatusToString(
370 const hardware::camera::common::V1_0::TorchModeStatus&);
371
372};
373
374} // namespace android
375
376#endif