Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 33 | namespace 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 | */ |
| 45 | class CameraProviderManager : virtual public hidl::manager::V1_0::IServiceNotification { |
| 46 | public: |
| 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 | ¬ification) = 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 | ¬ification) 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 Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 159 | * 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 Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 175 | * 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 Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 187 | /** |
| 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 Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 193 | private: |
| 194 | // All private members, unless otherwise noted, expect mInterfaceMutex to be locked before use |
| 195 | mutable std::mutex mInterfaceMutex; |
| 196 | |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 197 | // the status listener update callbacks will lock mStatusMutex |
| 198 | mutable std::mutex mStatusListenerMutex; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 199 | wp<StatusListener> mListener; |
| 200 | ServiceInteractionProxy* mServiceProxy; |
| 201 | |
| 202 | static HardwareServiceInteractionProxy sHardwareServiceInteractionProxy; |
| 203 | |
| 204 | struct ProviderInfo : virtual public hardware::camera::provider::V2_4::ICameraProviderCallback { |
| 205 | const std::string mProviderName; |
| 206 | const sp<hardware::camera::provider::V2_4::ICameraProvider> mInterface; |
| 207 | |
| 208 | ProviderInfo(const std::string &providerName, |
| 209 | sp<hardware::camera::provider::V2_4::ICameraProvider>& interface, |
| 210 | CameraProviderManager *manager); |
| 211 | ~ProviderInfo(); |
| 212 | |
| 213 | status_t initialize(); |
| 214 | |
| 215 | const std::string& getType() const; |
| 216 | |
| 217 | status_t addDevice(const std::string& name, |
| 218 | hardware::camera::common::V1_0::CameraDeviceStatus initialStatus = |
| 219 | hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT, |
| 220 | /*out*/ std::string *parsedId = nullptr); |
| 221 | |
| 222 | status_t dump(int fd, const Vector<String16>& args) const; |
| 223 | |
| 224 | // ICameraProviderCallbacks interface - these lock the parent mInterfaceMutex |
| 225 | virtual hardware::Return<void> cameraDeviceStatusChange( |
| 226 | const hardware::hidl_string& cameraDeviceName, |
| 227 | hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override; |
| 228 | virtual hardware::Return<void> torchModeStatusChange( |
| 229 | const hardware::hidl_string& cameraDeviceName, |
| 230 | hardware::camera::common::V1_0::TorchModeStatus newStatus) override; |
| 231 | |
| 232 | // Basic device information, common to all camera devices |
| 233 | struct DeviceInfo { |
| 234 | const std::string mName; // Full instance name |
| 235 | const std::string mId; // ID section of full name |
| 236 | const hardware::hidl_version mVersion; |
| 237 | |
| 238 | const hardware::camera::common::V1_0::CameraResourceCost mResourceCost; |
| 239 | |
| 240 | hardware::camera::common::V1_0::CameraDeviceStatus mStatus; |
| 241 | |
| 242 | bool hasFlashUnit() const { return mHasFlashUnit; } |
| 243 | virtual status_t setTorchMode(bool enabled) = 0; |
| 244 | virtual status_t getCameraInfo(hardware::CameraInfo *info) const = 0; |
| 245 | virtual status_t getCameraCharacteristics(CameraMetadata *characteristics) const { |
| 246 | (void) characteristics; |
| 247 | return INVALID_OPERATION; |
| 248 | } |
| 249 | |
| 250 | DeviceInfo(const std::string& name, const std::string &id, |
| 251 | const hardware::hidl_version& version, |
| 252 | const hardware::camera::common::V1_0::CameraResourceCost& resourceCost) : |
| 253 | mName(name), mId(id), mVersion(version), mResourceCost(resourceCost), |
| 254 | mStatus(hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT), |
| 255 | mHasFlashUnit(false) {} |
| 256 | virtual ~DeviceInfo(); |
| 257 | protected: |
| 258 | bool mHasFlashUnit; |
| 259 | |
| 260 | template<class InterfaceT> |
| 261 | static status_t setTorchMode(InterfaceT& interface, bool enabled); |
| 262 | }; |
| 263 | std::vector<std::unique_ptr<DeviceInfo>> mDevices; |
| 264 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 265 | // HALv1-specific camera fields, including the actual device interface |
| 266 | struct DeviceInfo1 : public DeviceInfo { |
| 267 | typedef hardware::camera::device::V1_0::ICameraDevice InterfaceT; |
| 268 | const sp<InterfaceT> mInterface; |
| 269 | |
| 270 | virtual status_t setTorchMode(bool enabled) override; |
| 271 | virtual status_t getCameraInfo(hardware::CameraInfo *info) const override; |
| 272 | |
| 273 | DeviceInfo1(const std::string& name, const std::string &id, |
| 274 | uint16_t minorVersion, |
| 275 | const hardware::camera::common::V1_0::CameraResourceCost& resourceCost, |
| 276 | sp<InterfaceT> interface); |
| 277 | virtual ~DeviceInfo1(); |
| 278 | private: |
| 279 | CameraParameters2 mDefaultParameters; |
| 280 | }; |
| 281 | |
| 282 | // HALv3-specific camera fields, including the actual device interface |
| 283 | struct DeviceInfo3 : public DeviceInfo { |
| 284 | typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT; |
| 285 | const sp<InterfaceT> mInterface; |
| 286 | |
| 287 | virtual status_t setTorchMode(bool enabled) override; |
| 288 | virtual status_t getCameraInfo(hardware::CameraInfo *info) const override; |
| 289 | virtual status_t getCameraCharacteristics( |
| 290 | CameraMetadata *characteristics) const override; |
| 291 | |
| 292 | DeviceInfo3(const std::string& name, const std::string &id, |
| 293 | uint16_t minorVersion, |
| 294 | const hardware::camera::common::V1_0::CameraResourceCost& resourceCost, |
| 295 | sp<InterfaceT> interface); |
| 296 | virtual ~DeviceInfo3(); |
| 297 | private: |
| 298 | CameraMetadata mCameraCharacteristics; |
| 299 | }; |
| 300 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 301 | private: |
| 302 | std::string mType; |
| 303 | uint32_t mId; |
| 304 | |
| 305 | CameraProviderManager *mManager; |
| 306 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 307 | // Templated method to instantiate the right kind of DeviceInfo and call the |
| 308 | // right CameraProvider getCameraDeviceInterface_* method. |
| 309 | template<class DeviceInfoT> |
| 310 | std::unique_ptr<DeviceInfo> initializeDeviceInfo(const std::string &name, |
| 311 | const std::string &id, uint16_t minorVersion) const; |
| 312 | |
| 313 | // Helper for initializeDeviceInfo to use the right CameraProvider get method. |
| 314 | template<class InterfaceT> |
| 315 | sp<InterfaceT> getDeviceInterface(const std::string &name) const; |
| 316 | |
| 317 | // Parse provider instance name for type and id |
| 318 | static status_t parseProviderName(const std::string& name, |
| 319 | std::string *type, uint32_t *id); |
| 320 | |
| 321 | // Parse device instance name for device version, type, and id. |
| 322 | static status_t parseDeviceName(const std::string& name, |
| 323 | uint16_t *major, uint16_t *minor, std::string *type, std::string *id); |
| 324 | }; |
| 325 | |
| 326 | // Utility to find a DeviceInfo by ID; pointer is only valid while mInterfaceMutex is held |
| 327 | // and the calling code doesn't mutate the list of providers or their lists of devices. |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 328 | // Finds the first device of the given ID that falls within the requested version range |
| 329 | // minVersion <= deviceVersion < maxVersion |
| 330 | // No guarantees on the order of traversal |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 331 | ProviderInfo::DeviceInfo* findDeviceInfoLocked(const std::string& id, |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 332 | hardware::hidl_version minVersion = hardware::hidl_version{0,0}, |
| 333 | hardware::hidl_version maxVersion = hardware::hidl_version{1000,0}) const; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 334 | |
| 335 | status_t addProvider(const std::string& newProvider, bool expected = true); |
| 336 | status_t removeProvider(const std::string& provider); |
| 337 | |
| 338 | bool isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const; |
| 339 | |
| 340 | std::vector<sp<ProviderInfo>> mProviders; |
| 341 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 342 | static const char* deviceStatusToString( |
| 343 | const hardware::camera::common::V1_0::CameraDeviceStatus&); |
| 344 | static const char* torchStatusToString( |
| 345 | const hardware::camera::common::V1_0::TorchModeStatus&); |
| 346 | |
| 347 | }; |
| 348 | |
| 349 | } // namespace android |
| 350 | |
| 351 | #endif |