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 | #define LOG_TAG "CameraProviderManager" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include "CameraProviderManager.h" |
| 22 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 23 | #include <algorithm> |
Yin-Chia Yeh | 6540509 | 2017-01-13 15:42:28 -0800 | [diff] [blame] | 24 | #include <chrono> |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 25 | #include <inttypes.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 26 | #include <hidl/ServiceManagement.h> |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 27 | #include <functional> |
| 28 | #include <camera_metadata_hidden.h> |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 29 | #include <android-base/parseint.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | using namespace ::android::hardware::camera; |
| 34 | using namespace ::android::hardware::camera::common::V1_0; |
| 35 | |
| 36 | namespace { |
| 37 | // Hardcoded name for the passthrough HAL implementation, since it can't be discovered via the |
| 38 | // service manager |
| 39 | const std::string kLegacyProviderName("legacy/0"); |
Yin-Chia Yeh | d78041a | 2018-01-20 13:45:38 -0800 | [diff] [blame] | 40 | const std::string kExternalProviderName("external/0"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 41 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 42 | } // anonymous namespace |
| 43 | |
| 44 | CameraProviderManager::HardwareServiceInteractionProxy |
| 45 | CameraProviderManager::sHardwareServiceInteractionProxy{}; |
| 46 | |
| 47 | CameraProviderManager::~CameraProviderManager() { |
| 48 | } |
| 49 | |
| 50 | status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener, |
| 51 | ServiceInteractionProxy* proxy) { |
Yin-Chia Yeh | 4c5b1c7 | 2017-01-31 13:20:56 -0800 | [diff] [blame] | 52 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 53 | if (proxy == nullptr) { |
| 54 | ALOGE("%s: No valid service interaction proxy provided", __FUNCTION__); |
| 55 | return BAD_VALUE; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 56 | } |
Yin-Chia Yeh | 4c5b1c7 | 2017-01-31 13:20:56 -0800 | [diff] [blame] | 57 | mListener = listener; |
| 58 | mServiceProxy = proxy; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 59 | |
Yin-Chia Yeh | 4c5b1c7 | 2017-01-31 13:20:56 -0800 | [diff] [blame] | 60 | // Registering will trigger notifications for all already-known providers |
| 61 | bool success = mServiceProxy->registerForNotifications( |
| 62 | /* instance name, empty means no filter */ "", |
| 63 | this); |
| 64 | if (!success) { |
| 65 | ALOGE("%s: Unable to register with hardware service manager for notifications " |
| 66 | "about camera providers", __FUNCTION__); |
| 67 | return INVALID_OPERATION; |
Yin-Chia Yeh | 6540509 | 2017-01-13 15:42:28 -0800 | [diff] [blame] | 68 | } |
Emilian Peev | 5d7e515 | 2017-02-22 15:37:48 +0000 | [diff] [blame] | 69 | |
Eino-Ville Talvala | 6566536 | 2017-02-24 13:07:56 -0800 | [diff] [blame] | 70 | // See if there's a passthrough HAL, but let's not complain if there's not |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 71 | addProviderLocked(kLegacyProviderName, /*expected*/ false); |
Yin-Chia Yeh | d78041a | 2018-01-20 13:45:38 -0800 | [diff] [blame] | 72 | addProviderLocked(kExternalProviderName, /*expected*/ false); |
Eino-Ville Talvala | 6566536 | 2017-02-24 13:07:56 -0800 | [diff] [blame] | 73 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 74 | return OK; |
| 75 | } |
| 76 | |
| 77 | int CameraProviderManager::getCameraCount() const { |
| 78 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 79 | int count = 0; |
| 80 | for (auto& provider : mProviders) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 81 | count += provider->mUniqueCameraIds.size(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 82 | } |
| 83 | return count; |
| 84 | } |
| 85 | |
| 86 | std::vector<std::string> CameraProviderManager::getCameraDeviceIds() const { |
| 87 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 88 | std::vector<std::string> deviceIds; |
| 89 | for (auto& provider : mProviders) { |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 90 | for (auto& id : provider->mUniqueCameraIds) { |
| 91 | deviceIds.push_back(id); |
| 92 | } |
| 93 | } |
| 94 | return deviceIds; |
| 95 | } |
| 96 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 97 | std::vector<std::string> CameraProviderManager::getAPI1CompatibleCameraDeviceIds() const { |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 98 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 99 | std::vector<std::string> deviceIds; |
| 100 | for (auto& provider : mProviders) { |
Emilian Peev | e1c48ed | 2018-03-02 12:25:08 +0000 | [diff] [blame] | 101 | for (auto& id : provider->mUniqueAPI1CompatibleCameraIds) { |
| 102 | deviceIds.push_back(id); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 105 | |
| 106 | std::sort(deviceIds.begin(), deviceIds.end(), |
| 107 | [](const std::string& a, const std::string& b) -> bool { |
| 108 | uint32_t aUint = 0, bUint = 0; |
| 109 | bool aIsUint = base::ParseUint(a, &aUint); |
| 110 | bool bIsUint = base::ParseUint(b, &bUint); |
| 111 | |
| 112 | // Uint device IDs first |
| 113 | if (aIsUint && bIsUint) { |
| 114 | return aUint < bUint; |
| 115 | } else if (aIsUint) { |
| 116 | return true; |
| 117 | } else if (bIsUint) { |
| 118 | return false; |
| 119 | } |
| 120 | // Simple string compare if both id are not uint |
| 121 | return a < b; |
| 122 | }); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 123 | return deviceIds; |
| 124 | } |
| 125 | |
| 126 | bool CameraProviderManager::isValidDevice(const std::string &id, uint16_t majorVersion) const { |
| 127 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 128 | return isValidDeviceLocked(id, majorVersion); |
| 129 | } |
| 130 | |
| 131 | bool CameraProviderManager::isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const { |
| 132 | for (auto& provider : mProviders) { |
| 133 | for (auto& deviceInfo : provider->mDevices) { |
| 134 | if (deviceInfo->mId == id && deviceInfo->mVersion.get_major() == majorVersion) { |
| 135 | return true; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | bool CameraProviderManager::hasFlashUnit(const std::string &id) const { |
| 143 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 144 | |
| 145 | auto deviceInfo = findDeviceInfoLocked(id); |
| 146 | if (deviceInfo == nullptr) return false; |
| 147 | |
| 148 | return deviceInfo->hasFlashUnit(); |
| 149 | } |
| 150 | |
| 151 | status_t CameraProviderManager::getResourceCost(const std::string &id, |
| 152 | CameraResourceCost* cost) const { |
| 153 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 154 | |
| 155 | auto deviceInfo = findDeviceInfoLocked(id); |
| 156 | if (deviceInfo == nullptr) return NAME_NOT_FOUND; |
| 157 | |
| 158 | *cost = deviceInfo->mResourceCost; |
| 159 | return OK; |
| 160 | } |
| 161 | |
| 162 | status_t CameraProviderManager::getCameraInfo(const std::string &id, |
| 163 | hardware::CameraInfo* info) const { |
| 164 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 165 | |
| 166 | auto deviceInfo = findDeviceInfoLocked(id); |
| 167 | if (deviceInfo == nullptr) return NAME_NOT_FOUND; |
| 168 | |
| 169 | return deviceInfo->getCameraInfo(info); |
| 170 | } |
| 171 | |
| 172 | status_t CameraProviderManager::getCameraCharacteristics(const std::string &id, |
| 173 | CameraMetadata* characteristics) const { |
| 174 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 175 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 176 | auto deviceInfo = findDeviceInfoLocked(id, /*minVersion*/ {3,0}, /*maxVersion*/ {4,0}); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 177 | if (deviceInfo == nullptr) return NAME_NOT_FOUND; |
| 178 | |
| 179 | return deviceInfo->getCameraCharacteristics(characteristics); |
| 180 | } |
| 181 | |
| 182 | status_t CameraProviderManager::getHighestSupportedVersion(const std::string &id, |
| 183 | hardware::hidl_version *v) { |
| 184 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 185 | |
| 186 | hardware::hidl_version maxVersion{0,0}; |
| 187 | bool found = false; |
| 188 | for (auto& provider : mProviders) { |
| 189 | for (auto& deviceInfo : provider->mDevices) { |
| 190 | if (deviceInfo->mId == id) { |
| 191 | if (deviceInfo->mVersion > maxVersion) { |
| 192 | maxVersion = deviceInfo->mVersion; |
| 193 | found = true; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | if (!found) { |
| 199 | return NAME_NOT_FOUND; |
| 200 | } |
| 201 | *v = maxVersion; |
| 202 | return OK; |
| 203 | } |
| 204 | |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 205 | bool CameraProviderManager::supportSetTorchMode(const std::string &id) { |
| 206 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 207 | bool support = false; |
| 208 | for (auto& provider : mProviders) { |
| 209 | auto deviceInfo = findDeviceInfoLocked(id); |
| 210 | if (deviceInfo != nullptr) { |
Yin-Chia Yeh | 73d0374 | 2017-09-29 12:01:02 -0700 | [diff] [blame] | 211 | auto ret = provider->mInterface->isSetTorchModeSupported( |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 212 | [&support](auto status, bool supported) { |
| 213 | if (status == Status::OK) { |
| 214 | support = supported; |
| 215 | } |
| 216 | }); |
Yin-Chia Yeh | 73d0374 | 2017-09-29 12:01:02 -0700 | [diff] [blame] | 217 | if (!ret.isOk()) { |
| 218 | ALOGE("%s: Transaction error checking torch mode support '%s': %s", |
| 219 | __FUNCTION__, provider->mProviderName.c_str(), ret.description().c_str()); |
| 220 | } |
| 221 | break; |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | return support; |
| 225 | } |
| 226 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 227 | status_t CameraProviderManager::setTorchMode(const std::string &id, bool enabled) { |
| 228 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 229 | |
| 230 | auto deviceInfo = findDeviceInfoLocked(id); |
| 231 | if (deviceInfo == nullptr) return NAME_NOT_FOUND; |
| 232 | |
| 233 | return deviceInfo->setTorchMode(enabled); |
| 234 | } |
| 235 | |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 236 | status_t CameraProviderManager::setUpVendorTags() { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 237 | sp<VendorTagDescriptorCache> tagCache = new VendorTagDescriptorCache(); |
| 238 | |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 239 | for (auto& provider : mProviders) { |
| 240 | hardware::hidl_vec<VendorTagSection> vts; |
| 241 | Status status; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 242 | hardware::Return<void> ret; |
| 243 | ret = provider->mInterface->getVendorTags( |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 244 | [&](auto s, const auto& vendorTagSecs) { |
| 245 | status = s; |
| 246 | if (s == Status::OK) { |
| 247 | vts = vendorTagSecs; |
| 248 | } |
| 249 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 250 | if (!ret.isOk()) { |
| 251 | ALOGE("%s: Transaction error getting vendor tags from provider '%s': %s", |
| 252 | __FUNCTION__, provider->mProviderName.c_str(), ret.description().c_str()); |
| 253 | return DEAD_OBJECT; |
| 254 | } |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 255 | if (status != Status::OK) { |
| 256 | return mapToStatusT(status); |
| 257 | } |
| 258 | |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 259 | // Read all vendor tag definitions into a descriptor |
| 260 | sp<VendorTagDescriptor> desc; |
| 261 | status_t res; |
| 262 | if ((res = HidlVendorTagDescriptor::createDescriptorFromHidl(vts, /*out*/desc)) |
| 263 | != OK) { |
| 264 | ALOGE("%s: Could not generate descriptor from vendor tag operations," |
| 265 | "received error %s (%d). Camera clients will not be able to use" |
| 266 | "vendor tags", __FUNCTION__, strerror(res), res); |
| 267 | return res; |
| 268 | } |
| 269 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 270 | tagCache->addVendorDescriptor(provider->mProviderTagid, desc); |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 271 | } |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 272 | |
| 273 | VendorTagDescriptorCache::setAsGlobalVendorTagCache(tagCache); |
| 274 | |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 275 | return OK; |
| 276 | } |
| 277 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 278 | status_t CameraProviderManager::openSession(const std::string &id, |
| 279 | const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback, |
| 280 | /*out*/ |
| 281 | sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session) { |
| 282 | |
| 283 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 284 | |
| 285 | auto deviceInfo = findDeviceInfoLocked(id, |
| 286 | /*minVersion*/ {3,0}, /*maxVersion*/ {4,0}); |
| 287 | if (deviceInfo == nullptr) return NAME_NOT_FOUND; |
| 288 | |
| 289 | auto *deviceInfo3 = static_cast<ProviderInfo::DeviceInfo3*>(deviceInfo); |
| 290 | |
| 291 | Status status; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 292 | hardware::Return<void> ret; |
| 293 | ret = deviceInfo3->mInterface->open(callback, [&status, &session] |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 294 | (Status s, const sp<device::V3_2::ICameraDeviceSession>& cameraSession) { |
| 295 | status = s; |
| 296 | if (status == Status::OK) { |
| 297 | *session = cameraSession; |
| 298 | } |
| 299 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 300 | if (!ret.isOk()) { |
| 301 | ALOGE("%s: Transaction error opening a session for camera device %s: %s", |
| 302 | __FUNCTION__, id.c_str(), ret.description().c_str()); |
| 303 | return DEAD_OBJECT; |
| 304 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 305 | return mapToStatusT(status); |
| 306 | } |
| 307 | |
| 308 | status_t CameraProviderManager::openSession(const std::string &id, |
| 309 | const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback, |
| 310 | /*out*/ |
| 311 | sp<hardware::camera::device::V1_0::ICameraDevice> *session) { |
| 312 | |
| 313 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 314 | |
| 315 | auto deviceInfo = findDeviceInfoLocked(id, |
| 316 | /*minVersion*/ {1,0}, /*maxVersion*/ {2,0}); |
| 317 | if (deviceInfo == nullptr) return NAME_NOT_FOUND; |
| 318 | |
| 319 | auto *deviceInfo1 = static_cast<ProviderInfo::DeviceInfo1*>(deviceInfo); |
| 320 | |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 321 | hardware::Return<Status> status = deviceInfo1->mInterface->open(callback); |
| 322 | if (!status.isOk()) { |
| 323 | ALOGE("%s: Transaction error opening a session for camera device %s: %s", |
| 324 | __FUNCTION__, id.c_str(), status.description().c_str()); |
| 325 | return DEAD_OBJECT; |
| 326 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 327 | if (status == Status::OK) { |
| 328 | *session = deviceInfo1->mInterface; |
| 329 | } |
| 330 | return mapToStatusT(status); |
| 331 | } |
| 332 | |
| 333 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 334 | hardware::Return<void> CameraProviderManager::onRegistration( |
| 335 | const hardware::hidl_string& /*fqName*/, |
| 336 | const hardware::hidl_string& name, |
| 337 | bool /*preexisting*/) { |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 338 | { |
| 339 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 340 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 341 | addProviderLocked(name); |
| 342 | } |
| 343 | |
| 344 | sp<StatusListener> listener = getStatusListener(); |
| 345 | if (nullptr != listener.get()) { |
| 346 | listener->onNewProviderRegistered(); |
| 347 | } |
| 348 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 349 | return hardware::Return<void>(); |
| 350 | } |
| 351 | |
| 352 | status_t CameraProviderManager::dump(int fd, const Vector<String16>& args) { |
| 353 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 354 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 355 | for (auto& provider : mProviders) { |
| 356 | provider->dump(fd, args); |
| 357 | } |
| 358 | return OK; |
| 359 | } |
| 360 | |
| 361 | CameraProviderManager::ProviderInfo::DeviceInfo* CameraProviderManager::findDeviceInfoLocked( |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 362 | const std::string& id, |
| 363 | hardware::hidl_version minVersion, hardware::hidl_version maxVersion) const { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 364 | for (auto& provider : mProviders) { |
| 365 | for (auto& deviceInfo : provider->mDevices) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 366 | if (deviceInfo->mId == id && |
| 367 | minVersion <= deviceInfo->mVersion && maxVersion >= deviceInfo->mVersion) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 368 | return deviceInfo.get(); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | return nullptr; |
| 373 | } |
| 374 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 375 | metadata_vendor_id_t CameraProviderManager::getProviderTagIdLocked( |
| 376 | const std::string& id, hardware::hidl_version minVersion, |
| 377 | hardware::hidl_version maxVersion) const { |
| 378 | metadata_vendor_id_t ret = CAMERA_METADATA_INVALID_VENDOR_ID; |
| 379 | |
| 380 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 381 | for (auto& provider : mProviders) { |
| 382 | for (auto& deviceInfo : provider->mDevices) { |
| 383 | if (deviceInfo->mId == id && |
| 384 | minVersion <= deviceInfo->mVersion && |
| 385 | maxVersion >= deviceInfo->mVersion) { |
| 386 | return provider->mProviderTagid; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | return ret; |
| 392 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 393 | |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 394 | status_t CameraProviderManager::addProviderLocked(const std::string& newProvider, bool expected) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 395 | for (const auto& providerInfo : mProviders) { |
| 396 | if (providerInfo->mProviderName == newProvider) { |
| 397 | ALOGW("%s: Camera provider HAL with name '%s' already registered", __FUNCTION__, |
| 398 | newProvider.c_str()); |
| 399 | return ALREADY_EXISTS; |
| 400 | } |
| 401 | } |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 402 | |
| 403 | sp<provider::V2_4::ICameraProvider> interface; |
| 404 | interface = mServiceProxy->getService(newProvider); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 405 | |
| 406 | if (interface == nullptr) { |
| 407 | if (expected) { |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 408 | ALOGE("%s: Camera provider HAL '%s' is not actually available", __FUNCTION__, |
| 409 | newProvider.c_str()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 410 | return BAD_VALUE; |
| 411 | } else { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 412 | return OK; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | sp<ProviderInfo> providerInfo = |
| 417 | new ProviderInfo(newProvider, interface, this); |
| 418 | status_t res = providerInfo->initialize(); |
| 419 | if (res != OK) { |
| 420 | return res; |
| 421 | } |
| 422 | |
| 423 | mProviders.push_back(providerInfo); |
| 424 | |
| 425 | return OK; |
| 426 | } |
| 427 | |
| 428 | status_t CameraProviderManager::removeProvider(const std::string& provider) { |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 429 | std::unique_lock<std::mutex> lock(mInterfaceMutex); |
| 430 | std::vector<String8> removedDeviceIds; |
| 431 | status_t res = NAME_NOT_FOUND; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 432 | for (auto it = mProviders.begin(); it != mProviders.end(); it++) { |
| 433 | if ((*it)->mProviderName == provider) { |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 434 | removedDeviceIds.reserve((*it)->mDevices.size()); |
| 435 | for (auto& deviceInfo : (*it)->mDevices) { |
| 436 | removedDeviceIds.push_back(String8(deviceInfo->mId.c_str())); |
| 437 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 438 | mProviders.erase(it); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 439 | res = OK; |
| 440 | break; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 443 | if (res != OK) { |
| 444 | ALOGW("%s: Camera provider HAL with name '%s' is not registered", __FUNCTION__, |
| 445 | provider.c_str()); |
| 446 | } else { |
| 447 | // Inform camera service of loss of presence for all the devices from this provider, |
| 448 | // without lock held for reentrancy |
| 449 | sp<StatusListener> listener = getStatusListener(); |
| 450 | if (listener != nullptr) { |
| 451 | lock.unlock(); |
| 452 | for (auto& id : removedDeviceIds) { |
| 453 | listener->onDeviceStatusChanged(id, CameraDeviceStatus::NOT_PRESENT); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | return res; |
| 458 | } |
| 459 | |
| 460 | sp<CameraProviderManager::StatusListener> CameraProviderManager::getStatusListener() const { |
| 461 | return mListener.promote(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /**** Methods for ProviderInfo ****/ |
| 465 | |
| 466 | |
| 467 | CameraProviderManager::ProviderInfo::ProviderInfo( |
| 468 | const std::string &providerName, |
| 469 | sp<provider::V2_4::ICameraProvider>& interface, |
| 470 | CameraProviderManager *manager) : |
| 471 | mProviderName(providerName), |
| 472 | mInterface(interface), |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 473 | mProviderTagid(generateVendorTagId(providerName)), |
Emilian Peev | cdb74a6 | 2017-05-11 20:29:52 +0100 | [diff] [blame] | 474 | mUniqueDeviceCount(0), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 475 | mManager(manager) { |
| 476 | (void) mManager; |
| 477 | } |
| 478 | |
| 479 | status_t CameraProviderManager::ProviderInfo::initialize() { |
| 480 | status_t res = parseProviderName(mProviderName, &mType, &mId); |
| 481 | if (res != OK) { |
| 482 | ALOGE("%s: Invalid provider name, ignoring", __FUNCTION__); |
| 483 | return BAD_VALUE; |
| 484 | } |
Yin-Chia Yeh | 6540509 | 2017-01-13 15:42:28 -0800 | [diff] [blame] | 485 | ALOGI("Connecting to new camera provider: %s, isRemote? %d", |
| 486 | mProviderName.c_str(), mInterface->isRemote()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 487 | // cameraDeviceStatusChange callbacks may be called (and causing new devices added) |
| 488 | // before setCallback returns |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 489 | hardware::Return<Status> status = mInterface->setCallback(this); |
| 490 | if (!status.isOk()) { |
| 491 | ALOGE("%s: Transaction error setting up callbacks with camera provider '%s': %s", |
| 492 | __FUNCTION__, mProviderName.c_str(), status.description().c_str()); |
| 493 | return DEAD_OBJECT; |
| 494 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 495 | if (status != Status::OK) { |
| 496 | ALOGE("%s: Unable to register callbacks with camera provider '%s'", |
| 497 | __FUNCTION__, mProviderName.c_str()); |
| 498 | return mapToStatusT(status); |
| 499 | } |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 500 | |
| 501 | hardware::Return<bool> linked = mInterface->linkToDeath(this, /*cookie*/ mId); |
| 502 | if (!linked.isOk()) { |
| 503 | ALOGE("%s: Transaction error in linking to camera provider '%s' death: %s", |
| 504 | __FUNCTION__, mProviderName.c_str(), linked.description().c_str()); |
| 505 | return DEAD_OBJECT; |
| 506 | } else if (!linked) { |
| 507 | ALOGW("%s: Unable to link to provider '%s' death notifications", |
| 508 | __FUNCTION__, mProviderName.c_str()); |
| 509 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 510 | |
| 511 | // Get initial list of camera devices, if any |
| 512 | std::vector<std::string> devices; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 513 | hardware::Return<void> ret = mInterface->getCameraIdList([&status, &devices]( |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 514 | Status idStatus, |
| 515 | const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames) { |
| 516 | status = idStatus; |
| 517 | if (status == Status::OK) { |
| 518 | for (size_t i = 0; i < cameraDeviceNames.size(); i++) { |
| 519 | devices.push_back(cameraDeviceNames[i]); |
| 520 | } |
| 521 | } }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 522 | if (!ret.isOk()) { |
| 523 | ALOGE("%s: Transaction error in getting camera ID list from provider '%s': %s", |
| 524 | __FUNCTION__, mProviderName.c_str(), linked.description().c_str()); |
| 525 | return DEAD_OBJECT; |
| 526 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 527 | if (status != Status::OK) { |
| 528 | ALOGE("%s: Unable to query for camera devices from provider '%s'", |
| 529 | __FUNCTION__, mProviderName.c_str()); |
| 530 | return mapToStatusT(status); |
| 531 | } |
| 532 | |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 533 | sp<StatusListener> listener = mManager->getStatusListener(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 534 | for (auto& device : devices) { |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 535 | std::string id; |
| 536 | status_t res = addDevice(device, |
| 537 | hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT, &id); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 538 | if (res != OK) { |
| 539 | ALOGE("%s: Unable to enumerate camera device '%s': %s (%d)", |
| 540 | __FUNCTION__, device.c_str(), strerror(-res), res); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 541 | continue; |
| 542 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | ALOGI("Camera provider %s ready with %zu camera devices", |
| 546 | mProviderName.c_str(), mDevices.size()); |
| 547 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 548 | mInitialized = true; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 549 | return OK; |
| 550 | } |
| 551 | |
| 552 | const std::string& CameraProviderManager::ProviderInfo::getType() const { |
| 553 | return mType; |
| 554 | } |
| 555 | |
| 556 | status_t CameraProviderManager::ProviderInfo::addDevice(const std::string& name, |
| 557 | CameraDeviceStatus initialStatus, /*out*/ std::string* parsedId) { |
| 558 | |
| 559 | ALOGI("Enumerating new camera device: %s", name.c_str()); |
| 560 | |
| 561 | uint16_t major, minor; |
| 562 | std::string type, id; |
| 563 | |
| 564 | status_t res = parseDeviceName(name, &major, &minor, &type, &id); |
| 565 | if (res != OK) { |
| 566 | return res; |
| 567 | } |
| 568 | if (type != mType) { |
| 569 | ALOGE("%s: Device type %s does not match provider type %s", __FUNCTION__, |
| 570 | type.c_str(), mType.c_str()); |
| 571 | return BAD_VALUE; |
| 572 | } |
| 573 | if (mManager->isValidDeviceLocked(id, major)) { |
| 574 | ALOGE("%s: Device %s: ID %s is already in use for device major version %d", __FUNCTION__, |
| 575 | name.c_str(), id.c_str(), major); |
| 576 | return BAD_VALUE; |
| 577 | } |
| 578 | |
| 579 | std::unique_ptr<DeviceInfo> deviceInfo; |
| 580 | switch (major) { |
| 581 | case 1: |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 582 | deviceInfo = initializeDeviceInfo<DeviceInfo1>(name, mProviderTagid, |
| 583 | id, minor); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 584 | break; |
| 585 | case 3: |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 586 | deviceInfo = initializeDeviceInfo<DeviceInfo3>(name, mProviderTagid, |
| 587 | id, minor); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 588 | break; |
| 589 | default: |
| 590 | ALOGE("%s: Device %s: Unknown HIDL device HAL major version %d:", __FUNCTION__, |
| 591 | name.c_str(), major); |
| 592 | return BAD_VALUE; |
| 593 | } |
| 594 | if (deviceInfo == nullptr) return BAD_VALUE; |
| 595 | deviceInfo->mStatus = initialStatus; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 596 | bool isAPI1Compatible = deviceInfo->isAPI1Compatible(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 597 | |
| 598 | mDevices.push_back(std::move(deviceInfo)); |
| 599 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 600 | mUniqueCameraIds.insert(id); |
| 601 | if (isAPI1Compatible) { |
| 602 | mUniqueAPI1CompatibleCameraIds.insert(id); |
| 603 | } |
| 604 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 605 | if (parsedId != nullptr) { |
| 606 | *parsedId = id; |
| 607 | } |
| 608 | return OK; |
| 609 | } |
| 610 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 611 | void CameraProviderManager::ProviderInfo::removeDevice(std::string id) { |
| 612 | for (auto it = mDevices.begin(); it != mDevices.end(); it++) { |
| 613 | if ((*it)->mId == id) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 614 | mUniqueCameraIds.erase(id); |
| 615 | if ((*it)->isAPI1Compatible()) { |
| 616 | mUniqueAPI1CompatibleCameraIds.erase(id); |
| 617 | } |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 618 | mDevices.erase(it); |
| 619 | break; |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 624 | status_t CameraProviderManager::ProviderInfo::dump(int fd, const Vector<String16>&) const { |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 625 | dprintf(fd, "== Camera Provider HAL %s (v2.4, %s) static info: %zu devices: ==\n", |
| 626 | mProviderName.c_str(), mInterface->isRemote() ? "remote" : "passthrough", |
| 627 | mDevices.size()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 628 | |
| 629 | for (auto& device : mDevices) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 630 | dprintf(fd, "== Camera HAL device %s (v%d.%d) static information: ==\n", device->mName.c_str(), |
| 631 | device->mVersion.get_major(), device->mVersion.get_minor()); |
| 632 | dprintf(fd, " Resource cost: %d\n", device->mResourceCost.resourceCost); |
| 633 | if (device->mResourceCost.conflictingDevices.size() == 0) { |
| 634 | dprintf(fd, " Conflicting devices: None\n"); |
| 635 | } else { |
| 636 | dprintf(fd, " Conflicting devices:\n"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 637 | for (size_t i = 0; i < device->mResourceCost.conflictingDevices.size(); i++) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 638 | dprintf(fd, " %s\n", |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 639 | device->mResourceCost.conflictingDevices[i].c_str()); |
| 640 | } |
| 641 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 642 | dprintf(fd, " API1 info:\n"); |
| 643 | dprintf(fd, " Has a flash unit: %s\n", |
| 644 | device->hasFlashUnit() ? "true" : "false"); |
| 645 | hardware::CameraInfo info; |
| 646 | status_t res = device->getCameraInfo(&info); |
| 647 | if (res != OK) { |
| 648 | dprintf(fd, " <Error reading camera info: %s (%d)>\n", |
| 649 | strerror(-res), res); |
| 650 | } else { |
| 651 | dprintf(fd, " Facing: %s\n", |
| 652 | info.facing == hardware::CAMERA_FACING_BACK ? "Back" : "Front"); |
| 653 | dprintf(fd, " Orientation: %d\n", info.orientation); |
| 654 | } |
| 655 | CameraMetadata info2; |
| 656 | res = device->getCameraCharacteristics(&info2); |
| 657 | if (res == INVALID_OPERATION) { |
| 658 | dprintf(fd, " API2 not directly supported\n"); |
| 659 | } else if (res != OK) { |
| 660 | dprintf(fd, " <Error reading camera characteristics: %s (%d)>\n", |
| 661 | strerror(-res), res); |
| 662 | } else { |
| 663 | dprintf(fd, " API2 camera characteristics:\n"); |
| 664 | info2.dump(fd, /*verbosity*/ 2, /*indentation*/ 4); |
| 665 | } |
Yin-Chia Yeh | 487785a | 2018-01-02 12:06:57 -0800 | [diff] [blame] | 666 | |
| 667 | dprintf(fd, "== Camera HAL device %s (v%d.%d) dumpState: ==\n", device->mName.c_str(), |
| 668 | device->mVersion.get_major(), device->mVersion.get_minor()); |
| 669 | res = device->dumpState(fd); |
| 670 | if (res != OK) { |
| 671 | dprintf(fd, " <Error dumping device %s state: %s (%d)>\n", |
| 672 | device->mName.c_str(), strerror(-res), res); |
| 673 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 674 | } |
| 675 | return OK; |
| 676 | } |
| 677 | |
| 678 | hardware::Return<void> CameraProviderManager::ProviderInfo::cameraDeviceStatusChange( |
| 679 | const hardware::hidl_string& cameraDeviceName, |
| 680 | CameraDeviceStatus newStatus) { |
| 681 | sp<StatusListener> listener; |
| 682 | std::string id; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 683 | bool initialized = false; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 684 | { |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 685 | std::lock_guard<std::mutex> lock(mLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 686 | bool known = false; |
| 687 | for (auto& deviceInfo : mDevices) { |
| 688 | if (deviceInfo->mName == cameraDeviceName) { |
| 689 | ALOGI("Camera device %s status is now %s, was %s", cameraDeviceName.c_str(), |
| 690 | deviceStatusToString(newStatus), deviceStatusToString(deviceInfo->mStatus)); |
| 691 | deviceInfo->mStatus = newStatus; |
| 692 | // TODO: Handle device removal (NOT_PRESENT) |
| 693 | id = deviceInfo->mId; |
| 694 | known = true; |
| 695 | break; |
| 696 | } |
| 697 | } |
| 698 | // Previously unseen device; status must not be NOT_PRESENT |
| 699 | if (!known) { |
| 700 | if (newStatus == CameraDeviceStatus::NOT_PRESENT) { |
| 701 | ALOGW("Camera provider %s says an unknown camera device %s is not present. Curious.", |
| 702 | mProviderName.c_str(), cameraDeviceName.c_str()); |
| 703 | return hardware::Void(); |
| 704 | } |
| 705 | addDevice(cameraDeviceName, newStatus, &id); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 706 | } else if (newStatus == CameraDeviceStatus::NOT_PRESENT) { |
| 707 | removeDevice(id); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 708 | } |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 709 | listener = mManager->getStatusListener(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 710 | initialized = mInitialized; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 711 | } |
| 712 | // Call without lock held to allow reentrancy into provider manager |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 713 | // Don't send the callback if providerInfo hasn't been initialized. |
| 714 | // CameraService will initialize device status after provider is |
| 715 | // initialized |
| 716 | if (listener != nullptr && initialized) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 717 | listener->onDeviceStatusChanged(String8(id.c_str()), newStatus); |
| 718 | } |
| 719 | return hardware::Void(); |
| 720 | } |
| 721 | |
| 722 | hardware::Return<void> CameraProviderManager::ProviderInfo::torchModeStatusChange( |
| 723 | const hardware::hidl_string& cameraDeviceName, |
| 724 | TorchModeStatus newStatus) { |
| 725 | sp<StatusListener> listener; |
| 726 | std::string id; |
| 727 | { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 728 | std::lock_guard<std::mutex> lock(mManager->mStatusListenerMutex); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 729 | bool known = false; |
| 730 | for (auto& deviceInfo : mDevices) { |
| 731 | if (deviceInfo->mName == cameraDeviceName) { |
| 732 | ALOGI("Camera device %s torch status is now %s", cameraDeviceName.c_str(), |
| 733 | torchStatusToString(newStatus)); |
| 734 | id = deviceInfo->mId; |
| 735 | known = true; |
| 736 | break; |
| 737 | } |
| 738 | } |
| 739 | if (!known) { |
| 740 | ALOGW("Camera provider %s says an unknown camera %s now has torch status %d. Curious.", |
| 741 | mProviderName.c_str(), cameraDeviceName.c_str(), newStatus); |
| 742 | return hardware::Void(); |
| 743 | } |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 744 | listener = mManager->getStatusListener(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 745 | } |
| 746 | // Call without lock held to allow reentrancy into provider manager |
| 747 | if (listener != nullptr) { |
| 748 | listener->onTorchStatusChanged(String8(id.c_str()), newStatus); |
| 749 | } |
| 750 | return hardware::Void(); |
| 751 | } |
| 752 | |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 753 | void CameraProviderManager::ProviderInfo::serviceDied(uint64_t cookie, |
| 754 | const wp<hidl::base::V1_0::IBase>& who) { |
| 755 | (void) who; |
| 756 | ALOGI("Camera provider '%s' has died; removing it", mProviderName.c_str()); |
| 757 | if (cookie != mId) { |
| 758 | ALOGW("%s: Unexpected serviceDied cookie %" PRIu64 ", expected %" PRIu32, |
| 759 | __FUNCTION__, cookie, mId); |
| 760 | } |
| 761 | mManager->removeProvider(mProviderName); |
| 762 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 763 | |
| 764 | template<class DeviceInfoT> |
| 765 | std::unique_ptr<CameraProviderManager::ProviderInfo::DeviceInfo> |
| 766 | CameraProviderManager::ProviderInfo::initializeDeviceInfo( |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 767 | const std::string &name, const metadata_vendor_id_t tagId, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 768 | const std::string &id, uint16_t minorVersion) const { |
| 769 | Status status; |
| 770 | |
| 771 | auto cameraInterface = |
| 772 | getDeviceInterface<typename DeviceInfoT::InterfaceT>(name); |
| 773 | if (cameraInterface == nullptr) return nullptr; |
| 774 | |
| 775 | CameraResourceCost resourceCost; |
| 776 | cameraInterface->getResourceCost([&status, &resourceCost]( |
| 777 | Status s, CameraResourceCost cost) { |
| 778 | status = s; |
| 779 | resourceCost = cost; |
| 780 | }); |
| 781 | if (status != Status::OK) { |
| 782 | ALOGE("%s: Unable to obtain resource costs for camera device %s: %s", __FUNCTION__, |
| 783 | name.c_str(), statusToString(status)); |
| 784 | return nullptr; |
| 785 | } |
Shuzhen Wang | 4728369 | 2018-04-24 18:05:02 -0700 | [diff] [blame] | 786 | |
| 787 | for (auto& conflictName : resourceCost.conflictingDevices) { |
| 788 | uint16_t major, minor; |
| 789 | std::string type, id; |
| 790 | status_t res = parseDeviceName(conflictName, &major, &minor, &type, &id); |
| 791 | if (res != OK) { |
| 792 | ALOGE("%s: Failed to parse conflicting device %s", __FUNCTION__, conflictName.c_str()); |
| 793 | return nullptr; |
| 794 | } |
| 795 | conflictName = id; |
| 796 | } |
| 797 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 798 | return std::unique_ptr<DeviceInfo>( |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 799 | new DeviceInfoT(name, tagId, id, minorVersion, resourceCost, |
| 800 | cameraInterface)); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | template<class InterfaceT> |
| 804 | sp<InterfaceT> |
| 805 | CameraProviderManager::ProviderInfo::getDeviceInterface(const std::string &name) const { |
| 806 | ALOGE("%s: Device %s: Unknown HIDL device HAL major version %d:", __FUNCTION__, |
| 807 | name.c_str(), InterfaceT::version.get_major()); |
| 808 | return nullptr; |
| 809 | } |
| 810 | |
| 811 | template<> |
| 812 | sp<device::V1_0::ICameraDevice> |
| 813 | CameraProviderManager::ProviderInfo::getDeviceInterface |
| 814 | <device::V1_0::ICameraDevice>(const std::string &name) const { |
| 815 | Status status; |
| 816 | sp<device::V1_0::ICameraDevice> cameraInterface; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 817 | hardware::Return<void> ret; |
| 818 | ret = mInterface->getCameraDeviceInterface_V1_x(name, [&status, &cameraInterface]( |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 819 | Status s, sp<device::V1_0::ICameraDevice> interface) { |
| 820 | status = s; |
| 821 | cameraInterface = interface; |
| 822 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 823 | if (!ret.isOk()) { |
| 824 | ALOGE("%s: Transaction error trying to obtain interface for camera device %s: %s", |
| 825 | __FUNCTION__, name.c_str(), ret.description().c_str()); |
| 826 | return nullptr; |
| 827 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 828 | if (status != Status::OK) { |
| 829 | ALOGE("%s: Unable to obtain interface for camera device %s: %s", __FUNCTION__, |
| 830 | name.c_str(), statusToString(status)); |
| 831 | return nullptr; |
| 832 | } |
| 833 | return cameraInterface; |
| 834 | } |
| 835 | |
| 836 | template<> |
| 837 | sp<device::V3_2::ICameraDevice> |
| 838 | CameraProviderManager::ProviderInfo::getDeviceInterface |
| 839 | <device::V3_2::ICameraDevice>(const std::string &name) const { |
| 840 | Status status; |
| 841 | sp<device::V3_2::ICameraDevice> cameraInterface; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 842 | hardware::Return<void> ret; |
| 843 | ret = mInterface->getCameraDeviceInterface_V3_x(name, [&status, &cameraInterface]( |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 844 | Status s, sp<device::V3_2::ICameraDevice> interface) { |
| 845 | status = s; |
| 846 | cameraInterface = interface; |
| 847 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 848 | if (!ret.isOk()) { |
| 849 | ALOGE("%s: Transaction error trying to obtain interface for camera device %s: %s", |
| 850 | __FUNCTION__, name.c_str(), ret.description().c_str()); |
| 851 | return nullptr; |
| 852 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 853 | if (status != Status::OK) { |
| 854 | ALOGE("%s: Unable to obtain interface for camera device %s: %s", __FUNCTION__, |
| 855 | name.c_str(), statusToString(status)); |
| 856 | return nullptr; |
| 857 | } |
| 858 | return cameraInterface; |
| 859 | } |
| 860 | |
| 861 | CameraProviderManager::ProviderInfo::DeviceInfo::~DeviceInfo() {} |
| 862 | |
| 863 | template<class InterfaceT> |
| 864 | status_t CameraProviderManager::ProviderInfo::DeviceInfo::setTorchMode(InterfaceT& interface, |
| 865 | bool enabled) { |
| 866 | Status s = interface->setTorchMode(enabled ? TorchMode::ON : TorchMode::OFF); |
| 867 | return mapToStatusT(s); |
| 868 | } |
| 869 | |
| 870 | CameraProviderManager::ProviderInfo::DeviceInfo1::DeviceInfo1(const std::string& name, |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 871 | const metadata_vendor_id_t tagId, const std::string &id, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 872 | uint16_t minorVersion, |
| 873 | const CameraResourceCost& resourceCost, |
| 874 | sp<InterfaceT> interface) : |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 875 | DeviceInfo(name, tagId, id, hardware::hidl_version{1, minorVersion}, |
| 876 | resourceCost), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 877 | mInterface(interface) { |
| 878 | // Get default parameters and initialize flash unit availability |
| 879 | // Requires powering on the camera device |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 880 | hardware::Return<Status> status = mInterface->open(nullptr); |
| 881 | if (!status.isOk()) { |
| 882 | ALOGE("%s: Transaction error opening camera device %s to check for a flash unit: %s", |
| 883 | __FUNCTION__, mId.c_str(), status.description().c_str()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 884 | return; |
| 885 | } |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 886 | if (status != Status::OK) { |
| 887 | ALOGE("%s: Unable to open camera device %s to check for a flash unit: %s", __FUNCTION__, |
| 888 | mId.c_str(), CameraProviderManager::statusToString(status)); |
| 889 | return; |
| 890 | } |
| 891 | hardware::Return<void> ret; |
| 892 | ret = mInterface->getParameters([this](const hardware::hidl_string& parms) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 893 | mDefaultParameters.unflatten(String8(parms.c_str())); |
| 894 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 895 | if (!ret.isOk()) { |
| 896 | ALOGE("%s: Transaction error reading camera device %s params to check for a flash unit: %s", |
| 897 | __FUNCTION__, mId.c_str(), status.description().c_str()); |
| 898 | return; |
| 899 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 900 | const char *flashMode = |
| 901 | mDefaultParameters.get(CameraParameters::KEY_SUPPORTED_FLASH_MODES); |
| 902 | if (flashMode && strstr(flashMode, CameraParameters::FLASH_MODE_TORCH)) { |
| 903 | mHasFlashUnit = true; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 904 | } |
| 905 | |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 906 | ret = mInterface->close(); |
| 907 | if (!ret.isOk()) { |
| 908 | ALOGE("%s: Transaction error closing camera device %s after check for a flash unit: %s", |
| 909 | __FUNCTION__, mId.c_str(), status.description().c_str()); |
| 910 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | CameraProviderManager::ProviderInfo::DeviceInfo1::~DeviceInfo1() {} |
| 914 | |
| 915 | status_t CameraProviderManager::ProviderInfo::DeviceInfo1::setTorchMode(bool enabled) { |
| 916 | return DeviceInfo::setTorchMode(mInterface, enabled); |
| 917 | } |
| 918 | |
| 919 | status_t CameraProviderManager::ProviderInfo::DeviceInfo1::getCameraInfo( |
| 920 | hardware::CameraInfo *info) const { |
| 921 | if (info == nullptr) return BAD_VALUE; |
| 922 | |
| 923 | Status status; |
| 924 | device::V1_0::CameraInfo cInfo; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 925 | hardware::Return<void> ret; |
| 926 | ret = mInterface->getCameraInfo([&status, &cInfo](Status s, device::V1_0::CameraInfo camInfo) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 927 | status = s; |
| 928 | cInfo = camInfo; |
| 929 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 930 | if (!ret.isOk()) { |
| 931 | ALOGE("%s: Transaction error reading camera info from device %s: %s", |
| 932 | __FUNCTION__, mId.c_str(), ret.description().c_str()); |
| 933 | return DEAD_OBJECT; |
| 934 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 935 | if (status != Status::OK) { |
| 936 | return mapToStatusT(status); |
| 937 | } |
| 938 | |
| 939 | switch(cInfo.facing) { |
| 940 | case device::V1_0::CameraFacing::BACK: |
| 941 | info->facing = hardware::CAMERA_FACING_BACK; |
| 942 | break; |
| 943 | case device::V1_0::CameraFacing::EXTERNAL: |
| 944 | // Map external to front for legacy API |
| 945 | case device::V1_0::CameraFacing::FRONT: |
| 946 | info->facing = hardware::CAMERA_FACING_FRONT; |
| 947 | break; |
| 948 | default: |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 949 | ALOGW("%s: Device %s: Unknown camera facing: %d", |
| 950 | __FUNCTION__, mId.c_str(), cInfo.facing); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 951 | info->facing = hardware::CAMERA_FACING_BACK; |
| 952 | } |
| 953 | info->orientation = cInfo.orientation; |
| 954 | |
| 955 | return OK; |
| 956 | } |
| 957 | |
Yin-Chia Yeh | 487785a | 2018-01-02 12:06:57 -0800 | [diff] [blame] | 958 | status_t CameraProviderManager::ProviderInfo::DeviceInfo1::dumpState(int fd) const { |
| 959 | native_handle_t* handle = native_handle_create(1,0); |
| 960 | handle->data[0] = fd; |
| 961 | hardware::Return<Status> s = mInterface->dumpState(handle); |
| 962 | native_handle_delete(handle); |
| 963 | if (!s.isOk()) { |
| 964 | return INVALID_OPERATION; |
| 965 | } |
| 966 | return mapToStatusT(s); |
| 967 | } |
| 968 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 969 | CameraProviderManager::ProviderInfo::DeviceInfo3::DeviceInfo3(const std::string& name, |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 970 | const metadata_vendor_id_t tagId, const std::string &id, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 971 | uint16_t minorVersion, |
| 972 | const CameraResourceCost& resourceCost, |
| 973 | sp<InterfaceT> interface) : |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 974 | DeviceInfo(name, tagId, id, hardware::hidl_version{3, minorVersion}, |
| 975 | resourceCost), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 976 | mInterface(interface) { |
| 977 | // Get camera characteristics and initialize flash unit availability |
| 978 | Status status; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 979 | hardware::Return<void> ret; |
| 980 | ret = mInterface->getCameraCharacteristics([&status, this](Status s, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 981 | device::V3_2::CameraMetadata metadata) { |
| 982 | status = s; |
| 983 | if (s == Status::OK) { |
| 984 | camera_metadata_t *buffer = |
| 985 | reinterpret_cast<camera_metadata_t*>(metadata.data()); |
Yin-Chia Yeh | 238ef5f | 2017-04-18 15:01:15 -0700 | [diff] [blame] | 986 | size_t expectedSize = metadata.size(); |
| 987 | int res = validate_camera_metadata_structure(buffer, &expectedSize); |
| 988 | if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) { |
| 989 | set_camera_metadata_vendor_id(buffer, mProviderTagid); |
| 990 | mCameraCharacteristics = buffer; |
| 991 | } else { |
| 992 | ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__); |
| 993 | status = Status::INTERNAL_ERROR; |
| 994 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 995 | } |
| 996 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 997 | if (!ret.isOk()) { |
| 998 | ALOGE("%s: Transaction error getting camera characteristics for device %s" |
| 999 | " to check for a flash unit: %s", __FUNCTION__, mId.c_str(), |
| 1000 | ret.description().c_str()); |
| 1001 | return; |
| 1002 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1003 | if (status != Status::OK) { |
| 1004 | ALOGE("%s: Unable to get camera characteristics for device %s: %s (%d)", |
| 1005 | __FUNCTION__, mId.c_str(), CameraProviderManager::statusToString(status), status); |
| 1006 | return; |
| 1007 | } |
| 1008 | camera_metadata_entry flashAvailable = |
| 1009 | mCameraCharacteristics.find(ANDROID_FLASH_INFO_AVAILABLE); |
| 1010 | if (flashAvailable.count == 1 && |
| 1011 | flashAvailable.data.u8[0] == ANDROID_FLASH_INFO_AVAILABLE_TRUE) { |
| 1012 | mHasFlashUnit = true; |
| 1013 | } else { |
| 1014 | mHasFlashUnit = false; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | CameraProviderManager::ProviderInfo::DeviceInfo3::~DeviceInfo3() {} |
| 1019 | |
| 1020 | status_t CameraProviderManager::ProviderInfo::DeviceInfo3::setTorchMode(bool enabled) { |
| 1021 | return DeviceInfo::setTorchMode(mInterface, enabled); |
| 1022 | } |
| 1023 | |
| 1024 | status_t CameraProviderManager::ProviderInfo::DeviceInfo3::getCameraInfo( |
| 1025 | hardware::CameraInfo *info) const { |
| 1026 | if (info == nullptr) return BAD_VALUE; |
| 1027 | |
| 1028 | camera_metadata_ro_entry facing = |
| 1029 | mCameraCharacteristics.find(ANDROID_LENS_FACING); |
| 1030 | if (facing.count == 1) { |
| 1031 | switch (facing.data.u8[0]) { |
| 1032 | case ANDROID_LENS_FACING_BACK: |
| 1033 | info->facing = hardware::CAMERA_FACING_BACK; |
| 1034 | break; |
| 1035 | case ANDROID_LENS_FACING_EXTERNAL: |
| 1036 | // Map external to front for legacy API |
| 1037 | case ANDROID_LENS_FACING_FRONT: |
| 1038 | info->facing = hardware::CAMERA_FACING_FRONT; |
| 1039 | break; |
| 1040 | } |
| 1041 | } else { |
| 1042 | ALOGE("%s: Unable to find android.lens.facing static metadata", __FUNCTION__); |
| 1043 | return NAME_NOT_FOUND; |
| 1044 | } |
| 1045 | |
| 1046 | camera_metadata_ro_entry orientation = |
| 1047 | mCameraCharacteristics.find(ANDROID_SENSOR_ORIENTATION); |
| 1048 | if (orientation.count == 1) { |
| 1049 | info->orientation = orientation.data.i32[0]; |
| 1050 | } else { |
| 1051 | ALOGE("%s: Unable to find android.sensor.orientation static metadata", __FUNCTION__); |
| 1052 | return NAME_NOT_FOUND; |
| 1053 | } |
| 1054 | |
| 1055 | return OK; |
| 1056 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1057 | bool CameraProviderManager::ProviderInfo::DeviceInfo3::isAPI1Compatible() const { |
| 1058 | bool isBackwardCompatible = false; |
| 1059 | camera_metadata_ro_entry_t caps = mCameraCharacteristics.find( |
| 1060 | ANDROID_REQUEST_AVAILABLE_CAPABILITIES); |
| 1061 | for (size_t i = 0; i < caps.count; i++) { |
| 1062 | if (caps.data.u8[i] == |
| 1063 | ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) { |
| 1064 | isBackwardCompatible = true; |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | return isBackwardCompatible; |
| 1070 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1071 | |
Yin-Chia Yeh | 487785a | 2018-01-02 12:06:57 -0800 | [diff] [blame] | 1072 | status_t CameraProviderManager::ProviderInfo::DeviceInfo3::dumpState(int fd) const { |
| 1073 | native_handle_t* handle = native_handle_create(1,0); |
| 1074 | handle->data[0] = fd; |
| 1075 | auto ret = mInterface->dumpState(handle); |
| 1076 | native_handle_delete(handle); |
| 1077 | if (!ret.isOk()) { |
| 1078 | return INVALID_OPERATION; |
| 1079 | } |
| 1080 | return OK; |
| 1081 | } |
| 1082 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1083 | status_t CameraProviderManager::ProviderInfo::DeviceInfo3::getCameraCharacteristics( |
| 1084 | CameraMetadata *characteristics) const { |
| 1085 | if (characteristics == nullptr) return BAD_VALUE; |
| 1086 | |
| 1087 | *characteristics = mCameraCharacteristics; |
| 1088 | return OK; |
| 1089 | } |
| 1090 | |
| 1091 | status_t CameraProviderManager::ProviderInfo::parseProviderName(const std::string& name, |
| 1092 | std::string *type, uint32_t *id) { |
| 1093 | // Format must be "<type>/<id>" |
| 1094 | #define ERROR_MSG_PREFIX "%s: Invalid provider name '%s'. " \ |
| 1095 | "Should match '<type>/<id>' - " |
| 1096 | |
| 1097 | if (!type || !id) return INVALID_OPERATION; |
| 1098 | |
| 1099 | std::string::size_type slashIdx = name.find('/'); |
| 1100 | if (slashIdx == std::string::npos || slashIdx == name.size() - 1) { |
| 1101 | ALOGE(ERROR_MSG_PREFIX |
| 1102 | "does not have / separator between type and id", |
| 1103 | __FUNCTION__, name.c_str()); |
| 1104 | return BAD_VALUE; |
| 1105 | } |
| 1106 | |
| 1107 | std::string typeVal = name.substr(0, slashIdx); |
| 1108 | |
| 1109 | char *endPtr; |
| 1110 | errno = 0; |
| 1111 | long idVal = strtol(name.c_str() + slashIdx + 1, &endPtr, 10); |
| 1112 | if (errno != 0) { |
| 1113 | ALOGE(ERROR_MSG_PREFIX |
| 1114 | "cannot parse provider id as an integer: %s (%d)", |
| 1115 | __FUNCTION__, name.c_str(), strerror(errno), errno); |
| 1116 | return BAD_VALUE; |
| 1117 | } |
| 1118 | if (endPtr != name.c_str() + name.size()) { |
| 1119 | ALOGE(ERROR_MSG_PREFIX |
| 1120 | "provider id has unexpected length", |
| 1121 | __FUNCTION__, name.c_str()); |
| 1122 | return BAD_VALUE; |
| 1123 | } |
| 1124 | if (idVal < 0) { |
| 1125 | ALOGE(ERROR_MSG_PREFIX |
| 1126 | "id is negative: %ld", |
| 1127 | __FUNCTION__, name.c_str(), idVal); |
| 1128 | return BAD_VALUE; |
| 1129 | } |
| 1130 | |
| 1131 | #undef ERROR_MSG_PREFIX |
| 1132 | |
| 1133 | *type = typeVal; |
| 1134 | *id = static_cast<uint32_t>(idVal); |
| 1135 | |
| 1136 | return OK; |
| 1137 | } |
| 1138 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 1139 | metadata_vendor_id_t CameraProviderManager::ProviderInfo::generateVendorTagId( |
| 1140 | const std::string &name) { |
| 1141 | metadata_vendor_id_t ret = std::hash<std::string> {} (name); |
| 1142 | // CAMERA_METADATA_INVALID_VENDOR_ID is not a valid hash value |
| 1143 | if (CAMERA_METADATA_INVALID_VENDOR_ID == ret) { |
| 1144 | ret = 0; |
| 1145 | } |
| 1146 | |
| 1147 | return ret; |
| 1148 | } |
| 1149 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1150 | status_t CameraProviderManager::ProviderInfo::parseDeviceName(const std::string& name, |
| 1151 | uint16_t *major, uint16_t *minor, std::string *type, std::string *id) { |
| 1152 | |
| 1153 | // Format must be "device@<major>.<minor>/<type>/<id>" |
| 1154 | |
| 1155 | #define ERROR_MSG_PREFIX "%s: Invalid device name '%s'. " \ |
| 1156 | "Should match 'device@<major>.<minor>/<type>/<id>' - " |
| 1157 | |
| 1158 | if (!major || !minor || !type || !id) return INVALID_OPERATION; |
| 1159 | |
| 1160 | // Verify starting prefix |
| 1161 | const char expectedPrefix[] = "device@"; |
| 1162 | |
| 1163 | if (name.find(expectedPrefix) != 0) { |
| 1164 | ALOGE(ERROR_MSG_PREFIX |
| 1165 | "does not start with '%s'", |
| 1166 | __FUNCTION__, name.c_str(), expectedPrefix); |
| 1167 | return BAD_VALUE; |
| 1168 | } |
| 1169 | |
| 1170 | // Extract major/minor versions |
| 1171 | constexpr std::string::size_type atIdx = sizeof(expectedPrefix) - 2; |
| 1172 | std::string::size_type dotIdx = name.find('.', atIdx); |
| 1173 | if (dotIdx == std::string::npos) { |
| 1174 | ALOGE(ERROR_MSG_PREFIX |
| 1175 | "does not have @<major>. version section", |
| 1176 | __FUNCTION__, name.c_str()); |
| 1177 | return BAD_VALUE; |
| 1178 | } |
| 1179 | std::string::size_type typeSlashIdx = name.find('/', dotIdx); |
| 1180 | if (typeSlashIdx == std::string::npos) { |
| 1181 | ALOGE(ERROR_MSG_PREFIX |
| 1182 | "does not have .<minor>/ version section", |
| 1183 | __FUNCTION__, name.c_str()); |
| 1184 | return BAD_VALUE; |
| 1185 | } |
| 1186 | |
| 1187 | char *endPtr; |
| 1188 | errno = 0; |
| 1189 | long majorVal = strtol(name.c_str() + atIdx + 1, &endPtr, 10); |
| 1190 | if (errno != 0) { |
| 1191 | ALOGE(ERROR_MSG_PREFIX |
| 1192 | "cannot parse major version: %s (%d)", |
| 1193 | __FUNCTION__, name.c_str(), strerror(errno), errno); |
| 1194 | return BAD_VALUE; |
| 1195 | } |
| 1196 | if (endPtr != name.c_str() + dotIdx) { |
| 1197 | ALOGE(ERROR_MSG_PREFIX |
| 1198 | "major version has unexpected length", |
| 1199 | __FUNCTION__, name.c_str()); |
| 1200 | return BAD_VALUE; |
| 1201 | } |
| 1202 | long minorVal = strtol(name.c_str() + dotIdx + 1, &endPtr, 10); |
| 1203 | if (errno != 0) { |
| 1204 | ALOGE(ERROR_MSG_PREFIX |
| 1205 | "cannot parse minor version: %s (%d)", |
| 1206 | __FUNCTION__, name.c_str(), strerror(errno), errno); |
| 1207 | return BAD_VALUE; |
| 1208 | } |
| 1209 | if (endPtr != name.c_str() + typeSlashIdx) { |
| 1210 | ALOGE(ERROR_MSG_PREFIX |
| 1211 | "minor version has unexpected length", |
| 1212 | __FUNCTION__, name.c_str()); |
| 1213 | return BAD_VALUE; |
| 1214 | } |
| 1215 | if (majorVal < 0 || majorVal > UINT16_MAX || minorVal < 0 || minorVal > UINT16_MAX) { |
| 1216 | ALOGE(ERROR_MSG_PREFIX |
| 1217 | "major/minor version is out of range of uint16_t: %ld.%ld", |
| 1218 | __FUNCTION__, name.c_str(), majorVal, minorVal); |
| 1219 | return BAD_VALUE; |
| 1220 | } |
| 1221 | |
| 1222 | // Extract type and id |
| 1223 | |
| 1224 | std::string::size_type instanceSlashIdx = name.find('/', typeSlashIdx + 1); |
| 1225 | if (instanceSlashIdx == std::string::npos) { |
| 1226 | ALOGE(ERROR_MSG_PREFIX |
| 1227 | "does not have /<type>/ component", |
| 1228 | __FUNCTION__, name.c_str()); |
| 1229 | return BAD_VALUE; |
| 1230 | } |
| 1231 | std::string typeVal = name.substr(typeSlashIdx + 1, instanceSlashIdx - typeSlashIdx - 1); |
| 1232 | |
| 1233 | if (instanceSlashIdx == name.size() - 1) { |
| 1234 | ALOGE(ERROR_MSG_PREFIX |
| 1235 | "does not have an /<id> component", |
| 1236 | __FUNCTION__, name.c_str()); |
| 1237 | return BAD_VALUE; |
| 1238 | } |
| 1239 | std::string idVal = name.substr(instanceSlashIdx + 1); |
| 1240 | |
| 1241 | #undef ERROR_MSG_PREFIX |
| 1242 | |
| 1243 | *major = static_cast<uint16_t>(majorVal); |
| 1244 | *minor = static_cast<uint16_t>(minorVal); |
| 1245 | *type = typeVal; |
| 1246 | *id = idVal; |
| 1247 | |
| 1248 | return OK; |
| 1249 | } |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | CameraProviderManager::ProviderInfo::~ProviderInfo() { |
| 1254 | // Destruction of ProviderInfo is only supposed to happen when the respective |
| 1255 | // CameraProvider interface dies, so do not unregister callbacks. |
| 1256 | |
| 1257 | } |
| 1258 | |
| 1259 | status_t CameraProviderManager::mapToStatusT(const Status& s) { |
| 1260 | switch(s) { |
| 1261 | case Status::OK: |
| 1262 | return OK; |
| 1263 | case Status::ILLEGAL_ARGUMENT: |
| 1264 | return BAD_VALUE; |
| 1265 | case Status::CAMERA_IN_USE: |
| 1266 | return -EBUSY; |
| 1267 | case Status::MAX_CAMERAS_IN_USE: |
| 1268 | return -EUSERS; |
| 1269 | case Status::METHOD_NOT_SUPPORTED: |
| 1270 | return UNKNOWN_TRANSACTION; |
| 1271 | case Status::OPERATION_NOT_SUPPORTED: |
| 1272 | return INVALID_OPERATION; |
| 1273 | case Status::CAMERA_DISCONNECTED: |
| 1274 | return DEAD_OBJECT; |
| 1275 | case Status::INTERNAL_ERROR: |
| 1276 | return INVALID_OPERATION; |
| 1277 | } |
| 1278 | ALOGW("Unexpected HAL status code %d", s); |
| 1279 | return INVALID_OPERATION; |
| 1280 | } |
| 1281 | |
| 1282 | const char* CameraProviderManager::statusToString(const Status& s) { |
| 1283 | switch(s) { |
| 1284 | case Status::OK: |
| 1285 | return "OK"; |
| 1286 | case Status::ILLEGAL_ARGUMENT: |
| 1287 | return "ILLEGAL_ARGUMENT"; |
| 1288 | case Status::CAMERA_IN_USE: |
| 1289 | return "CAMERA_IN_USE"; |
| 1290 | case Status::MAX_CAMERAS_IN_USE: |
| 1291 | return "MAX_CAMERAS_IN_USE"; |
| 1292 | case Status::METHOD_NOT_SUPPORTED: |
| 1293 | return "METHOD_NOT_SUPPORTED"; |
| 1294 | case Status::OPERATION_NOT_SUPPORTED: |
| 1295 | return "OPERATION_NOT_SUPPORTED"; |
| 1296 | case Status::CAMERA_DISCONNECTED: |
| 1297 | return "CAMERA_DISCONNECTED"; |
| 1298 | case Status::INTERNAL_ERROR: |
| 1299 | return "INTERNAL_ERROR"; |
| 1300 | } |
| 1301 | ALOGW("Unexpected HAL status code %d", s); |
| 1302 | return "UNKNOWN_ERROR"; |
| 1303 | } |
| 1304 | |
| 1305 | const char* CameraProviderManager::deviceStatusToString(const CameraDeviceStatus& s) { |
| 1306 | switch(s) { |
| 1307 | case CameraDeviceStatus::NOT_PRESENT: |
| 1308 | return "NOT_PRESENT"; |
| 1309 | case CameraDeviceStatus::PRESENT: |
| 1310 | return "PRESENT"; |
| 1311 | case CameraDeviceStatus::ENUMERATING: |
| 1312 | return "ENUMERATING"; |
| 1313 | } |
| 1314 | ALOGW("Unexpected HAL device status code %d", s); |
| 1315 | return "UNKNOWN_STATUS"; |
| 1316 | } |
| 1317 | |
| 1318 | const char* CameraProviderManager::torchStatusToString(const TorchModeStatus& s) { |
| 1319 | switch(s) { |
| 1320 | case TorchModeStatus::NOT_AVAILABLE: |
| 1321 | return "NOT_AVAILABLE"; |
| 1322 | case TorchModeStatus::AVAILABLE_OFF: |
| 1323 | return "AVAILABLE_OFF"; |
| 1324 | case TorchModeStatus::AVAILABLE_ON: |
| 1325 | return "AVAILABLE_ON"; |
| 1326 | } |
| 1327 | ALOGW("Unexpected HAL torch mode status code %d", s); |
| 1328 | return "UNKNOWN_STATUS"; |
| 1329 | } |
| 1330 | |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 1331 | |
| 1332 | status_t HidlVendorTagDescriptor::createDescriptorFromHidl( |
| 1333 | const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts, |
| 1334 | /*out*/ |
| 1335 | sp<VendorTagDescriptor>& descriptor) { |
| 1336 | |
| 1337 | int tagCount = 0; |
| 1338 | |
| 1339 | for (size_t s = 0; s < vts.size(); s++) { |
| 1340 | tagCount += vts[s].tags.size(); |
| 1341 | } |
| 1342 | |
| 1343 | if (tagCount < 0 || tagCount > INT32_MAX) { |
| 1344 | ALOGE("%s: tag count %d from vendor tag sections is invalid.", __FUNCTION__, tagCount); |
| 1345 | return BAD_VALUE; |
| 1346 | } |
| 1347 | |
| 1348 | Vector<uint32_t> tagArray; |
| 1349 | LOG_ALWAYS_FATAL_IF(tagArray.resize(tagCount) != tagCount, |
| 1350 | "%s: too many (%u) vendor tags defined.", __FUNCTION__, tagCount); |
| 1351 | |
| 1352 | |
| 1353 | sp<HidlVendorTagDescriptor> desc = new HidlVendorTagDescriptor(); |
| 1354 | desc->mTagCount = tagCount; |
| 1355 | |
| 1356 | SortedVector<String8> sections; |
| 1357 | KeyedVector<uint32_t, String8> tagToSectionMap; |
| 1358 | |
| 1359 | int idx = 0; |
| 1360 | for (size_t s = 0; s < vts.size(); s++) { |
| 1361 | const hardware::camera::common::V1_0::VendorTagSection& section = vts[s]; |
| 1362 | const char *sectionName = section.sectionName.c_str(); |
| 1363 | if (sectionName == NULL) { |
| 1364 | ALOGE("%s: no section name defined for vendor tag section %zu.", __FUNCTION__, s); |
| 1365 | return BAD_VALUE; |
| 1366 | } |
| 1367 | String8 sectionString(sectionName); |
| 1368 | sections.add(sectionString); |
| 1369 | |
| 1370 | for (size_t j = 0; j < section.tags.size(); j++) { |
| 1371 | uint32_t tag = section.tags[j].tagId; |
| 1372 | if (tag < CAMERA_METADATA_VENDOR_TAG_BOUNDARY) { |
| 1373 | ALOGE("%s: vendor tag %d not in vendor tag section.", __FUNCTION__, tag); |
| 1374 | return BAD_VALUE; |
| 1375 | } |
| 1376 | |
| 1377 | tagArray.editItemAt(idx++) = section.tags[j].tagId; |
| 1378 | |
| 1379 | const char *tagName = section.tags[j].tagName.c_str(); |
| 1380 | if (tagName == NULL) { |
| 1381 | ALOGE("%s: no tag name defined for vendor tag %d.", __FUNCTION__, tag); |
| 1382 | return BAD_VALUE; |
| 1383 | } |
| 1384 | desc->mTagToNameMap.add(tag, String8(tagName)); |
| 1385 | tagToSectionMap.add(tag, sectionString); |
| 1386 | |
| 1387 | int tagType = (int) section.tags[j].tagType; |
| 1388 | if (tagType < 0 || tagType >= NUM_TYPES) { |
| 1389 | ALOGE("%s: tag type %d from vendor ops does not exist.", __FUNCTION__, tagType); |
| 1390 | return BAD_VALUE; |
| 1391 | } |
| 1392 | desc->mTagToTypeMap.add(tag, tagType); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | desc->mSections = sections; |
| 1397 | |
| 1398 | for (size_t i = 0; i < tagArray.size(); ++i) { |
| 1399 | uint32_t tag = tagArray[i]; |
| 1400 | String8 sectionString = tagToSectionMap.valueFor(tag); |
| 1401 | |
| 1402 | // Set up tag to section index map |
| 1403 | ssize_t index = sections.indexOf(sectionString); |
| 1404 | LOG_ALWAYS_FATAL_IF(index < 0, "index %zd must be non-negative", index); |
| 1405 | desc->mTagToSectionMap.add(tag, static_cast<uint32_t>(index)); |
| 1406 | |
| 1407 | // Set up reverse mapping |
| 1408 | ssize_t reverseIndex = -1; |
| 1409 | if ((reverseIndex = desc->mReverseMapping.indexOfKey(sectionString)) < 0) { |
| 1410 | KeyedVector<String8, uint32_t>* nameMapper = new KeyedVector<String8, uint32_t>(); |
| 1411 | reverseIndex = desc->mReverseMapping.add(sectionString, nameMapper); |
| 1412 | } |
| 1413 | desc->mReverseMapping[reverseIndex]->add(desc->mTagToNameMap.valueFor(tag), tag); |
| 1414 | } |
| 1415 | |
George Burgess IV | a0b8496 | 2017-08-29 17:46:19 -0700 | [diff] [blame] | 1416 | descriptor = std::move(desc); |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 1417 | return OK; |
| 1418 | } |
| 1419 | |
| 1420 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1421 | } // namespace android |