Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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_NDEBUG 0 |
| 18 | #define LOG_TAG "ACameraManager" |
| 19 | |
| 20 | #include <memory> |
| 21 | #include "ACameraManager.h" |
| 22 | #include "ACameraMetadata.h" |
| 23 | #include "ACameraDevice.h" |
| 24 | #include <utils/Vector.h> |
Ivan Podogov | ee844a8 | 2016-09-15 11:32:41 +0100 | [diff] [blame] | 25 | #include <cutils/properties.h> |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <camera/VendorTagDescriptor.h> |
| 28 | |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 29 | using namespace android::acam; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 30 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 31 | namespace android { |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 32 | namespace acam { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 33 | // Static member definitions |
| 34 | const char* CameraManagerGlobal::kCameraIdKey = "CameraId"; |
| 35 | const char* CameraManagerGlobal::kCallbackFpKey = "CallbackFp"; |
| 36 | const char* CameraManagerGlobal::kContextKey = "CallbackContext"; |
| 37 | Mutex CameraManagerGlobal::sLock; |
| 38 | CameraManagerGlobal* CameraManagerGlobal::sInstance = nullptr; |
| 39 | |
| 40 | CameraManagerGlobal& |
| 41 | CameraManagerGlobal::getInstance() { |
| 42 | Mutex::Autolock _l(sLock); |
| 43 | CameraManagerGlobal* instance = sInstance; |
| 44 | if (instance == nullptr) { |
| 45 | instance = new CameraManagerGlobal(); |
| 46 | sInstance = instance; |
| 47 | } |
| 48 | return *instance; |
| 49 | } |
| 50 | |
| 51 | CameraManagerGlobal::~CameraManagerGlobal() { |
| 52 | // clear sInstance so next getInstance call knows to create a new one |
| 53 | Mutex::Autolock _sl(sLock); |
| 54 | sInstance = nullptr; |
| 55 | Mutex::Autolock _l(mLock); |
| 56 | if (mCameraService != nullptr) { |
| 57 | IInterface::asBinder(mCameraService)->unlinkToDeath(mDeathNotifier); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 58 | mCameraService->removeListener(mCameraServiceListener); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 59 | } |
| 60 | mDeathNotifier.clear(); |
| 61 | if (mCbLooper != nullptr) { |
| 62 | mCbLooper->unregisterHandler(mHandler->id()); |
| 63 | mCbLooper->stop(); |
| 64 | } |
| 65 | mCbLooper.clear(); |
| 66 | mHandler.clear(); |
| 67 | mCameraServiceListener.clear(); |
| 68 | mCameraService.clear(); |
| 69 | } |
| 70 | |
Ivan Podogov | ee844a8 | 2016-09-15 11:32:41 +0100 | [diff] [blame] | 71 | static bool isCameraServiceDisabled() { |
| 72 | char value[PROPERTY_VALUE_MAX]; |
| 73 | property_get("config.disable_cameraservice", value, "0"); |
| 74 | return (strncmp(value, "0", 2) != 0 && strncasecmp(value, "false", 6) != 0); |
| 75 | } |
| 76 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 77 | sp<hardware::ICameraService> CameraManagerGlobal::getCameraService() { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 78 | Mutex::Autolock _l(mLock); |
| 79 | if (mCameraService.get() == nullptr) { |
Ivan Podogov | ee844a8 | 2016-09-15 11:32:41 +0100 | [diff] [blame] | 80 | if (isCameraServiceDisabled()) { |
| 81 | return mCameraService; |
| 82 | } |
| 83 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 84 | sp<IServiceManager> sm = defaultServiceManager(); |
| 85 | sp<IBinder> binder; |
| 86 | do { |
| 87 | binder = sm->getService(String16(kCameraServiceName)); |
| 88 | if (binder != nullptr) { |
| 89 | break; |
| 90 | } |
| 91 | ALOGW("CameraService not published, waiting..."); |
| 92 | usleep(kCameraServicePollDelay); |
| 93 | } while(true); |
| 94 | if (mDeathNotifier == nullptr) { |
| 95 | mDeathNotifier = new DeathNotifier(this); |
| 96 | } |
| 97 | binder->linkToDeath(mDeathNotifier); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 98 | mCameraService = interface_cast<hardware::ICameraService>(binder); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 99 | |
| 100 | // Setup looper thread to perfrom availiability callbacks |
| 101 | if (mCbLooper == nullptr) { |
| 102 | mCbLooper = new ALooper; |
| 103 | mCbLooper->setName("C2N-mgr-looper"); |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 104 | status_t err = mCbLooper->start( |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 105 | /*runOnCallingThread*/false, |
| 106 | /*canCallJava*/ true, |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 107 | PRIORITY_DEFAULT); |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 108 | if (err != OK) { |
| 109 | ALOGE("%s: Unable to start camera service listener looper: %s (%d)", |
| 110 | __FUNCTION__, strerror(-err), err); |
| 111 | mCbLooper.clear(); |
| 112 | return nullptr; |
| 113 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 114 | if (mHandler == nullptr) { |
| 115 | mHandler = new CallbackHandler(); |
| 116 | } |
| 117 | mCbLooper->registerHandler(mHandler); |
| 118 | } |
| 119 | |
| 120 | // register ICameraServiceListener |
| 121 | if (mCameraServiceListener == nullptr) { |
| 122 | mCameraServiceListener = new CameraServiceListener(this); |
| 123 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 124 | std::vector<hardware::CameraStatus> cameraStatuses{}; |
| 125 | mCameraService->addListener(mCameraServiceListener, &cameraStatuses); |
| 126 | for (auto& c : cameraStatuses) { |
| 127 | onStatusChangedLocked(c.status, c.cameraId); |
| 128 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 129 | |
| 130 | // setup vendor tags |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 131 | sp<VendorTagDescriptor> desc = new VendorTagDescriptor(); |
| 132 | binder::Status ret = mCameraService->getCameraVendorTagDescriptor(/*out*/desc.get()); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 133 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 134 | if (ret.isOk()) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 135 | if (0 < desc->getTagCount()) { |
| 136 | status_t err = VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc); |
| 137 | if (err != OK) { |
| 138 | ALOGE("%s: Failed to set vendor tag descriptors, received error %s (%d)", |
| 139 | __FUNCTION__, strerror(-err), err); |
| 140 | } |
| 141 | } else { |
| 142 | sp<VendorTagDescriptorCache> cache = |
| 143 | new VendorTagDescriptorCache(); |
| 144 | binder::Status res = |
| 145 | mCameraService->getCameraVendorTagCache( |
| 146 | /*out*/cache.get()); |
| 147 | if (res.serviceSpecificErrorCode() == |
| 148 | hardware::ICameraService::ERROR_DISCONNECTED) { |
| 149 | // No camera module available, not an error on devices with no cameras |
| 150 | VendorTagDescriptorCache::clearGlobalVendorTagCache(); |
| 151 | } else if (res.isOk()) { |
| 152 | status_t err = |
| 153 | VendorTagDescriptorCache::setAsGlobalVendorTagCache( |
| 154 | cache); |
| 155 | if (err != OK) { |
| 156 | ALOGE("%s: Failed to set vendor tag cache," |
| 157 | "received error %s (%d)", __FUNCTION__, |
| 158 | strerror(-err), err); |
| 159 | } |
| 160 | } else { |
| 161 | VendorTagDescriptorCache::clearGlobalVendorTagCache(); |
| 162 | ALOGE("%s: Failed to setup vendor tag cache: %s", |
| 163 | __FUNCTION__, res.toString8().string()); |
| 164 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 165 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 166 | } else if (ret.serviceSpecificErrorCode() == |
| 167 | hardware::ICameraService::ERROR_DEPRECATED_HAL) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 168 | ALOGW("%s: Camera HAL too old; does not support vendor tags", |
| 169 | __FUNCTION__); |
| 170 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
| 171 | } else { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 172 | ALOGE("%s: Failed to get vendor tag descriptors: %s", |
| 173 | __FUNCTION__, ret.toString8().string()); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | ALOGE_IF(mCameraService == nullptr, "no CameraService!?"); |
| 177 | return mCameraService; |
| 178 | } |
| 179 | |
| 180 | void CameraManagerGlobal::DeathNotifier::binderDied(const wp<IBinder>&) |
| 181 | { |
| 182 | ALOGE("Camera service binderDied!"); |
| 183 | sp<CameraManagerGlobal> cm = mCameraManager.promote(); |
| 184 | if (cm != nullptr) { |
| 185 | AutoMutex lock(cm->mLock); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 186 | for (auto& pair : cm->mDeviceStatusMap) { |
| 187 | const String8 &cameraId = pair.first; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 188 | cm->onStatusChangedLocked( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 189 | CameraServiceListener::STATUS_NOT_PRESENT, cameraId); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 190 | } |
| 191 | cm->mCameraService.clear(); |
| 192 | // TODO: consider adding re-connect call here? |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void CameraManagerGlobal::registerAvailabilityCallback( |
| 197 | const ACameraManager_AvailabilityCallbacks *callback) { |
| 198 | Mutex::Autolock _l(mLock); |
| 199 | Callback cb(callback); |
| 200 | auto pair = mCallbacks.insert(cb); |
| 201 | // Send initial callbacks if callback is newly registered |
| 202 | if (pair.second) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 203 | for (auto& pair : mDeviceStatusMap) { |
| 204 | const String8& cameraId = pair.first; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 205 | int32_t status = pair.second; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 206 | |
| 207 | sp<AMessage> msg = new AMessage(kWhatSendSingleCallback, mHandler); |
| 208 | ACameraManager_AvailabilityCallback cb = isStatusAvailable(status) ? |
| 209 | callback->onCameraAvailable : callback->onCameraUnavailable; |
| 210 | msg->setPointer(kCallbackFpKey, (void *) cb); |
| 211 | msg->setPointer(kContextKey, callback->context); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 212 | msg->setString(kCameraIdKey, AString(cameraId)); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 213 | msg->post(); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void CameraManagerGlobal::unregisterAvailabilityCallback( |
| 219 | const ACameraManager_AvailabilityCallbacks *callback) { |
| 220 | Mutex::Autolock _l(mLock); |
| 221 | Callback cb(callback); |
| 222 | mCallbacks.erase(cb); |
| 223 | } |
| 224 | |
Yin-Chia Yeh | 03f5575 | 2018-03-14 15:28:02 -0700 | [diff] [blame] | 225 | void CameraManagerGlobal::getCameraIdList(std::vector<String8>* cameraIds) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 226 | // Ensure that we have initialized/refreshed the list of available devices |
| 227 | auto cs = getCameraService(); |
| 228 | Mutex::Autolock _l(mLock); |
| 229 | |
| 230 | for(auto& deviceStatus : mDeviceStatusMap) { |
| 231 | if (deviceStatus.second == hardware::ICameraServiceListener::STATUS_NOT_PRESENT || |
| 232 | deviceStatus.second == hardware::ICameraServiceListener::STATUS_ENUMERATING) { |
| 233 | continue; |
| 234 | } |
| 235 | bool camera2Support = false; |
| 236 | binder::Status serviceRet = cs->supportsCameraApi(String16(deviceStatus.first), |
| 237 | hardware::ICameraService::API_VERSION_2, &camera2Support); |
| 238 | if (!serviceRet.isOk() || !camera2Support) { |
| 239 | continue; |
| 240 | } |
| 241 | cameraIds->push_back(deviceStatus.first); |
| 242 | } |
| 243 | } |
| 244 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 245 | bool CameraManagerGlobal::validStatus(int32_t status) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 246 | switch (status) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 247 | case hardware::ICameraServiceListener::STATUS_NOT_PRESENT: |
| 248 | case hardware::ICameraServiceListener::STATUS_PRESENT: |
| 249 | case hardware::ICameraServiceListener::STATUS_ENUMERATING: |
| 250 | case hardware::ICameraServiceListener::STATUS_NOT_AVAILABLE: |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 251 | return true; |
| 252 | default: |
| 253 | return false; |
| 254 | } |
| 255 | } |
| 256 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 257 | bool CameraManagerGlobal::isStatusAvailable(int32_t status) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 258 | switch (status) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 259 | case hardware::ICameraServiceListener::STATUS_PRESENT: |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 260 | return true; |
| 261 | default: |
| 262 | return false; |
| 263 | } |
| 264 | } |
| 265 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 266 | void CameraManagerGlobal::CallbackHandler::onMessageReceived( |
| 267 | const sp<AMessage> &msg) { |
| 268 | switch (msg->what()) { |
| 269 | case kWhatSendSingleCallback: |
| 270 | { |
| 271 | ACameraManager_AvailabilityCallback cb; |
| 272 | void* context; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 273 | AString cameraId; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 274 | bool found = msg->findPointer(kCallbackFpKey, (void**) &cb); |
| 275 | if (!found) { |
| 276 | ALOGE("%s: Cannot find camera callback fp!", __FUNCTION__); |
| 277 | return; |
| 278 | } |
| 279 | found = msg->findPointer(kContextKey, &context); |
| 280 | if (!found) { |
| 281 | ALOGE("%s: Cannot find callback context!", __FUNCTION__); |
| 282 | return; |
| 283 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 284 | found = msg->findString(kCameraIdKey, &cameraId); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 285 | if (!found) { |
| 286 | ALOGE("%s: Cannot find camera ID!", __FUNCTION__); |
| 287 | return; |
| 288 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 289 | (*cb)(context, cameraId.c_str()); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 290 | break; |
| 291 | } |
| 292 | default: |
| 293 | ALOGE("%s: unknown message type %d", __FUNCTION__, msg->what()); |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 298 | binder::Status CameraManagerGlobal::CameraServiceListener::onStatusChanged( |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 299 | int32_t status, const String16& cameraId) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 300 | sp<CameraManagerGlobal> cm = mCameraManager.promote(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 301 | if (cm != nullptr) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 302 | cm->onStatusChanged(status, String8(cameraId)); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 303 | } else { |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 304 | ALOGE("Cannot deliver status change. Global camera manager died"); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 305 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 306 | return binder::Status::ok(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void CameraManagerGlobal::onStatusChanged( |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 310 | int32_t status, const String8& cameraId) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 311 | Mutex::Autolock _l(mLock); |
| 312 | onStatusChangedLocked(status, cameraId); |
| 313 | } |
| 314 | |
| 315 | void CameraManagerGlobal::onStatusChangedLocked( |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 316 | int32_t status, const String8& cameraId) { |
| 317 | if (!validStatus(status)) { |
| 318 | ALOGE("%s: Invalid status %d", __FUNCTION__, status); |
| 319 | return; |
| 320 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 321 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 322 | bool firstStatus = (mDeviceStatusMap.count(cameraId) == 0); |
| 323 | int32_t oldStatus = firstStatus ? |
| 324 | status : // first status |
| 325 | mDeviceStatusMap[cameraId]; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 326 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 327 | if (!firstStatus && |
| 328 | isStatusAvailable(status) == isStatusAvailable(oldStatus)) { |
| 329 | // No status update. No need to send callback |
| 330 | return; |
| 331 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 332 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 333 | // Iterate through all registered callbacks |
| 334 | mDeviceStatusMap[cameraId] = status; |
| 335 | for (auto cb : mCallbacks) { |
| 336 | sp<AMessage> msg = new AMessage(kWhatSendSingleCallback, mHandler); |
| 337 | ACameraManager_AvailabilityCallback cbFp = isStatusAvailable(status) ? |
| 338 | cb.mAvailable : cb.mUnavailable; |
| 339 | msg->setPointer(kCallbackFpKey, (void *) cbFp); |
| 340 | msg->setPointer(kContextKey, cb.mContext); |
| 341 | msg->setString(kCameraIdKey, AString(cameraId)); |
| 342 | msg->post(); |
| 343 | } |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 344 | if (status == hardware::ICameraServiceListener::STATUS_NOT_PRESENT) { |
| 345 | mDeviceStatusMap.erase(cameraId); |
| 346 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 349 | } // namespace acam |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 350 | } // namespace android |
| 351 | |
| 352 | /** |
| 353 | * ACameraManger Implementation |
| 354 | */ |
| 355 | camera_status_t |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 356 | ACameraManager::getCameraIdList(ACameraIdList** cameraIdList) { |
| 357 | Mutex::Autolock _l(mLock); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 358 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 359 | std::vector<String8> idList; |
| 360 | CameraManagerGlobal::getInstance().getCameraIdList(&idList); |
| 361 | |
| 362 | int numCameras = idList.size(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 363 | ACameraIdList *out = new ACameraIdList; |
| 364 | if (!out) { |
| 365 | ALOGE("Allocate memory for ACameraIdList failed!"); |
| 366 | return ACAMERA_ERROR_NOT_ENOUGH_MEMORY; |
| 367 | } |
| 368 | out->numCameras = numCameras; |
Bjoern Johansson | 1a5954c | 2017-01-10 10:30:18 -0800 | [diff] [blame] | 369 | out->cameraIds = new const char*[numCameras]; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 370 | if (!out->cameraIds) { |
| 371 | ALOGE("Allocate memory for ACameraIdList failed!"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 372 | deleteCameraIdList(out); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 373 | return ACAMERA_ERROR_NOT_ENOUGH_MEMORY; |
| 374 | } |
| 375 | for (int i = 0; i < numCameras; i++) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 376 | const char* src = idList[i].string(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 377 | size_t dstSize = strlen(src) + 1; |
| 378 | char* dst = new char[dstSize]; |
| 379 | if (!dst) { |
| 380 | ALOGE("Allocate memory for ACameraIdList failed!"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 381 | deleteCameraIdList(out); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 382 | return ACAMERA_ERROR_NOT_ENOUGH_MEMORY; |
| 383 | } |
| 384 | strlcpy(dst, src, dstSize); |
| 385 | out->cameraIds[i] = dst; |
| 386 | } |
| 387 | *cameraIdList = out; |
| 388 | return ACAMERA_OK; |
| 389 | } |
| 390 | |
| 391 | void |
| 392 | ACameraManager::deleteCameraIdList(ACameraIdList* cameraIdList) { |
| 393 | if (cameraIdList != nullptr) { |
| 394 | if (cameraIdList->cameraIds != nullptr) { |
| 395 | for (int i = 0; i < cameraIdList->numCameras; i ++) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 396 | if (cameraIdList->cameraIds[i] != nullptr) { |
| 397 | delete[] cameraIdList->cameraIds[i]; |
| 398 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 399 | } |
| 400 | delete[] cameraIdList->cameraIds; |
| 401 | } |
| 402 | delete cameraIdList; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | camera_status_t ACameraManager::getCameraCharacteristics( |
Yin-Chia Yeh | dd045bf | 2018-08-20 12:39:19 -0700 | [diff] [blame] | 407 | const char* cameraIdStr, sp<ACameraMetadata>* characteristics) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 408 | Mutex::Autolock _l(mLock); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 409 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 410 | sp<hardware::ICameraService> cs = CameraManagerGlobal::getInstance().getCameraService(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 411 | if (cs == nullptr) { |
| 412 | ALOGE("%s: Cannot reach camera service!", __FUNCTION__); |
| 413 | return ACAMERA_ERROR_CAMERA_DISCONNECTED; |
| 414 | } |
| 415 | CameraMetadata rawMetadata; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 416 | binder::Status serviceRet = cs->getCameraCharacteristics(String16(cameraIdStr), &rawMetadata); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 417 | if (!serviceRet.isOk()) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 418 | switch(serviceRet.serviceSpecificErrorCode()) { |
| 419 | case hardware::ICameraService::ERROR_DISCONNECTED: |
| 420 | ALOGE("%s: Camera %s has been disconnected", __FUNCTION__, cameraIdStr); |
| 421 | return ACAMERA_ERROR_CAMERA_DISCONNECTED; |
| 422 | case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT: |
| 423 | ALOGE("%s: Camera ID %s does not exist!", __FUNCTION__, cameraIdStr); |
| 424 | return ACAMERA_ERROR_INVALID_PARAMETER; |
| 425 | default: |
| 426 | ALOGE("Get camera characteristics from camera service failed: %s", |
| 427 | serviceRet.toString8().string()); |
| 428 | return ACAMERA_ERROR_UNKNOWN; // should not reach here |
| 429 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | *characteristics = new ACameraMetadata( |
| 433 | rawMetadata.release(), ACameraMetadata::ACM_CHARACTERISTICS); |
| 434 | return ACAMERA_OK; |
| 435 | } |
| 436 | |
| 437 | camera_status_t |
| 438 | ACameraManager::openCamera( |
| 439 | const char* cameraId, |
| 440 | ACameraDevice_StateCallbacks* callback, |
| 441 | /*out*/ACameraDevice** outDevice) { |
Yin-Chia Yeh | dd045bf | 2018-08-20 12:39:19 -0700 | [diff] [blame] | 442 | sp<ACameraMetadata> chars; |
| 443 | camera_status_t ret = getCameraCharacteristics(cameraId, &chars); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 444 | Mutex::Autolock _l(mLock); |
| 445 | if (ret != ACAMERA_OK) { |
| 446 | ALOGE("%s: cannot get camera characteristics for camera %s. err %d", |
| 447 | __FUNCTION__, cameraId, ret); |
| 448 | return ACAMERA_ERROR_INVALID_PARAMETER; |
| 449 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 450 | |
Yin-Chia Yeh | dd045bf | 2018-08-20 12:39:19 -0700 | [diff] [blame] | 451 | ACameraDevice* device = new ACameraDevice(cameraId, callback, chars); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 452 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 453 | sp<hardware::ICameraService> cs = CameraManagerGlobal::getInstance().getCameraService(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 454 | if (cs == nullptr) { |
| 455 | ALOGE("%s: Cannot reach camera service!", __FUNCTION__); |
Yunlian Jiang | b01d8f7 | 2016-10-04 16:34:18 -0700 | [diff] [blame] | 456 | delete device; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 457 | return ACAMERA_ERROR_CAMERA_DISCONNECTED; |
| 458 | } |
| 459 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 460 | sp<hardware::camera2::ICameraDeviceCallbacks> callbacks = device->getServiceCallback(); |
| 461 | sp<hardware::camera2::ICameraDeviceUser> deviceRemote; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 462 | // No way to get package name from native. |
| 463 | // Send a zero length package name and let camera service figure it out from UID |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 464 | binder::Status serviceRet = cs->connectDevice( |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 465 | callbacks, String16(cameraId), String16(""), |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 466 | hardware::ICameraService::USE_CALLING_UID, /*out*/&deviceRemote); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 467 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 468 | if (!serviceRet.isOk()) { |
| 469 | ALOGE("%s: connect camera device failed: %s", __FUNCTION__, serviceRet.toString8().string()); |
Yin-Chia Yeh | 3e49be1 | 2016-04-12 16:00:33 -0700 | [diff] [blame] | 470 | // Convert serviceRet to camera_status_t |
| 471 | switch(serviceRet.serviceSpecificErrorCode()) { |
| 472 | case hardware::ICameraService::ERROR_DISCONNECTED: |
| 473 | ret = ACAMERA_ERROR_CAMERA_DISCONNECTED; |
| 474 | break; |
| 475 | case hardware::ICameraService::ERROR_CAMERA_IN_USE: |
| 476 | ret = ACAMERA_ERROR_CAMERA_IN_USE; |
| 477 | break; |
| 478 | case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE: |
| 479 | ret = ACAMERA_ERROR_MAX_CAMERA_IN_USE; |
| 480 | break; |
| 481 | case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT: |
| 482 | ret = ACAMERA_ERROR_INVALID_PARAMETER; |
| 483 | break; |
| 484 | case hardware::ICameraService::ERROR_DEPRECATED_HAL: |
| 485 | // Should not reach here since we filtered legacy HALs earlier |
| 486 | ret = ACAMERA_ERROR_INVALID_PARAMETER; |
| 487 | break; |
| 488 | case hardware::ICameraService::ERROR_DISABLED: |
| 489 | ret = ACAMERA_ERROR_CAMERA_DISABLED; |
| 490 | break; |
| 491 | case hardware::ICameraService::ERROR_PERMISSION_DENIED: |
| 492 | ret = ACAMERA_ERROR_PERMISSION_DENIED; |
| 493 | break; |
| 494 | case hardware::ICameraService::ERROR_INVALID_OPERATION: |
| 495 | default: |
| 496 | ret = ACAMERA_ERROR_UNKNOWN; |
| 497 | break; |
| 498 | } |
| 499 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 500 | delete device; |
Yin-Chia Yeh | 3e49be1 | 2016-04-12 16:00:33 -0700 | [diff] [blame] | 501 | return ret; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 502 | } |
| 503 | if (deviceRemote == nullptr) { |
| 504 | ALOGE("%s: connect camera device failed! remote device is null", __FUNCTION__); |
| 505 | delete device; |
| 506 | return ACAMERA_ERROR_CAMERA_DISCONNECTED; |
| 507 | } |
| 508 | device->setRemoteDevice(deviceRemote); |
| 509 | *outDevice = device; |
| 510 | return ACAMERA_OK; |
| 511 | } |
| 512 | |
| 513 | ACameraManager::~ACameraManager() { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 514 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 515 | } |