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