blob: b095ad73ecbde260bf0a28344d56e4e964c368b3 [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 /**
159 * IServiceNotification::onRegistration
160 * Invoked by the hardware service manager when a new camera provider is registered
161 */
162 virtual hardware::Return<void> onRegistration(const hardware::hidl_string& fqName,
163 const hardware::hidl_string& name,
164 bool preexisting) override;
165
166 /**
167 * Dump out information about available providers and devices
168 */
169 status_t dump(int fd, const Vector<String16>& args);
170
171private:
172 // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use
173 mutable std::mutex mInterfaceMutex;
174
175 wp<StatusListener> mListener;
176 ServiceInteractionProxy* mServiceProxy;
177
178 static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy;
179
180 struct ProviderInfo : virtual public hardware::camera::provider::V2_4::ICameraProviderCallback {
181 const std::string mProviderName;
182 const sp<hardware::camera::provider::V2_4::ICameraProvider> mInterface;
183
184 ProviderInfo(const std::string &providerName,
185 sp<hardware::camera::provider::V2_4::ICameraProvider>& interface,
186 CameraProviderManager *manager);
187 ~ProviderInfo();
188
189 status_t initialize();
190
191 const std::string& getType() const;
192
193 status_t addDevice(const std::string& name,
194 hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
195 hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
196 /*out*/ std::string *parsedId = nullptr);
197
198 status_t dump(int fd, const Vector<String16>& args) const;
199
200 // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex
201 virtual hardware::Return<void> cameraDeviceStatusChange(
202 const hardware::hidl_string& cameraDeviceName,
203 hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
204 virtual hardware::Return<void> torchModeStatusChange(
205 const hardware::hidl_string& cameraDeviceName,
206 hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
207
208 // Basic device information, common to all camera devices
209 struct DeviceInfo {
210 const std::string mName; // Full instance name
211 const std::string mId; // ID section of full name
212 const hardware::hidl_version mVersion;
213
214 const hardware::camera::common::V1_0::CameraResourceCost mResourceCost;
215
216 hardware::camera::common::V1_0::CameraDeviceStatus mStatus;
217
218 bool hasFlashUnit() const { return mHasFlashUnit; }
219 virtual status_t setTorchMode(bool enabled) = 0;
220 virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0;
221 virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const {
222 (void) characteristics;
223 return INVALID_OPERATION;
224 }
225
226 DeviceInfo(const std::string& name, const std::string &id,
227 const hardware::hidl_version& version,
228 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost) :
229 mName(name), mId(id), mVersion(version), mResourceCost(resourceCost),
230 mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT),
231 mHasFlashUnit(false) {}
232 virtual ~DeviceInfo();
233 protected:
234 bool mHasFlashUnit;
235
236 template<class InterfaceT>
237 static status_t setTorchMode(InterfaceT& interface, bool enabled);
238 };
239 std::vector<std::unique_ptr<DeviceInfo>> mDevices;
240
241 private:
242 std::string mType;
243 uint32_t mId;
244
245 CameraProviderManager *mManager;
246
247 // HALv1-specific camera fields, including the actual device interface
248 struct DeviceInfo1 : public DeviceInfo {
249 typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT;
250 const sp<InterfaceT> mInterface;
251
252 virtual status_t setTorchMode(bool enabled) override;
253 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
254
255 DeviceInfo1(const std::string& name, const std::string &id,
256 uint16_t minorVersion,
257 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
258 sp<InterfaceT> interface);
259 virtual ~DeviceInfo1();
260 private:
261 CameraParameters2 mDefaultParameters;
262 };
263
264 // HALv3-specific camera fields, including the actual device interface
265 struct DeviceInfo3 : public DeviceInfo {
266 typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
267 const sp<InterfaceT> mInterface;
268
269 virtual status_t setTorchMode(bool enabled) override;
270 virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
271 virtual status_t getCameraCharacteristics(
272 CameraMetadata *characteristics) const override;
273
274 DeviceInfo3(const std::string& name, const std::string &id,
275 uint16_t minorVersion,
276 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
277 sp<InterfaceT> interface);
278 virtual ~DeviceInfo3();
279 private:
280 CameraMetadata mCameraCharacteristics;
281 };
282
283 // Templated method to instantiate the right kind of DeviceInfo and call the
284 // right CameraProvider getCameraDeviceInterface_* method.
285 template<class DeviceInfoT>
286 std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name,
287 const std::string &id, uint16_t minorVersion) const;
288
289 // Helper for initializeDeviceInfo to use the right CameraProvider get method.
290 template<class InterfaceT>
291 sp<InterfaceT> getDeviceInterface(const std::string &name) const;
292
293 // Parse provider instance name for type and id
294 static status_t parseProviderName(const std::string& name,
295 std::string *type, uint32_t *id);
296
297 // Parse device instance name for device version, type, and id.
298 static status_t parseDeviceName(const std::string& name,
299 uint16_t *major, uint16_t *minor, std::string *type, std::string *id);
300 };
301
302 // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held
303 // and the calling code doesn't mutate the list of providers or their lists of devices.
304 ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id,
305 hardware::hidl_version minVersion = hardware::hidl_version{0,0}) const;
306
307 status_t addProvider(const std::string& newProvider, bool expected = true);
308 status_t removeProvider(const std::string& provider);
309
310 bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const;
311
312 std::vector<sp<ProviderInfo>> mProviders;
313
314 static status_t mapToStatusT(const hardware::camera::common::V1_0::Status& s);
315 static const char* statusToString(const hardware::camera::common::V1_0::Status&);
316 static const char* deviceStatusToString(
317 const hardware::camera::common::V1_0::CameraDeviceStatus&);
318 static const char* torchStatusToString(
319 const hardware::camera::common::V1_0::TorchModeStatus&);
320
321};
322
323} // namespace android
324
325#endif