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> |
Jayant Chowdhary | f5b9cc6 | 2020-09-08 16:25:17 -0700 | [diff] [blame] | 27 | #include <camera/CameraUtils.h> |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 28 | #include <camera/StringUtils.h> |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 29 | #include <camera/VendorTagDescriptor.h> |
| 30 | |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 31 | using namespace android::acam; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 32 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 33 | namespace android { |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 34 | namespace acam { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 35 | |
| 36 | // TODO(b/291736219): Add device-awareness to ACameraManager. |
| 37 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 38 | // Static member definitions |
| 39 | const char* CameraManagerGlobal::kCameraIdKey = "CameraId"; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 40 | const char* CameraManagerGlobal::kPhysicalCameraIdKey = "PhysicalCameraId"; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 41 | const char* CameraManagerGlobal::kCallbackFpKey = "CallbackFp"; |
| 42 | const char* CameraManagerGlobal::kContextKey = "CallbackContext"; |
Shuzhen Wang | 7e54068 | 2020-04-10 13:30:25 -0700 | [diff] [blame] | 43 | const nsecs_t CameraManagerGlobal::kCallbackDrainTimeout = 5000000; // 5 ms |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 44 | Mutex CameraManagerGlobal::sLock; |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 45 | wp<CameraManagerGlobal> CameraManagerGlobal::sInstance = nullptr; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 46 | |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 47 | sp<CameraManagerGlobal> CameraManagerGlobal::getInstance() { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 48 | Mutex::Autolock _l(sLock); |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 49 | sp<CameraManagerGlobal> instance = sInstance.promote(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 50 | if (instance == nullptr) { |
| 51 | instance = new CameraManagerGlobal(); |
| 52 | sInstance = instance; |
| 53 | } |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 54 | return instance; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | CameraManagerGlobal::~CameraManagerGlobal() { |
| 58 | // clear sInstance so next getInstance call knows to create a new one |
| 59 | Mutex::Autolock _sl(sLock); |
| 60 | sInstance = nullptr; |
| 61 | Mutex::Autolock _l(mLock); |
| 62 | if (mCameraService != nullptr) { |
| 63 | IInterface::asBinder(mCameraService)->unlinkToDeath(mDeathNotifier); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 64 | mCameraService->removeListener(mCameraServiceListener); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 65 | } |
| 66 | mDeathNotifier.clear(); |
| 67 | if (mCbLooper != nullptr) { |
| 68 | mCbLooper->unregisterHandler(mHandler->id()); |
| 69 | mCbLooper->stop(); |
| 70 | } |
| 71 | mCbLooper.clear(); |
| 72 | mHandler.clear(); |
| 73 | mCameraServiceListener.clear(); |
| 74 | mCameraService.clear(); |
| 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); |
Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 79 | return getCameraServiceLocked(); |
| 80 | } |
| 81 | |
| 82 | sp<hardware::ICameraService> CameraManagerGlobal::getCameraServiceLocked() { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 83 | if (mCameraService.get() == nullptr) { |
Jayant Chowdhary | f5b9cc6 | 2020-09-08 16:25:17 -0700 | [diff] [blame] | 84 | if (CameraUtils::isCameraServiceDisabled()) { |
Ivan Podogov | ee844a8 | 2016-09-15 11:32:41 +0100 | [diff] [blame] | 85 | return mCameraService; |
| 86 | } |
| 87 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 88 | sp<IServiceManager> sm = defaultServiceManager(); |
| 89 | sp<IBinder> binder; |
| 90 | do { |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 91 | binder = sm->getService(toString16(kCameraServiceName)); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 92 | if (binder != nullptr) { |
| 93 | break; |
| 94 | } |
| 95 | ALOGW("CameraService not published, waiting..."); |
| 96 | usleep(kCameraServicePollDelay); |
| 97 | } while(true); |
| 98 | if (mDeathNotifier == nullptr) { |
| 99 | mDeathNotifier = new DeathNotifier(this); |
| 100 | } |
| 101 | binder->linkToDeath(mDeathNotifier); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 102 | mCameraService = interface_cast<hardware::ICameraService>(binder); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 103 | |
| 104 | // Setup looper thread to perfrom availiability callbacks |
| 105 | if (mCbLooper == nullptr) { |
| 106 | mCbLooper = new ALooper; |
| 107 | mCbLooper->setName("C2N-mgr-looper"); |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 108 | status_t err = mCbLooper->start( |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 109 | /*runOnCallingThread*/false, |
| 110 | /*canCallJava*/ true, |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 111 | PRIORITY_DEFAULT); |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 112 | if (err != OK) { |
| 113 | ALOGE("%s: Unable to start camera service listener looper: %s (%d)", |
| 114 | __FUNCTION__, strerror(-err), err); |
| 115 | mCbLooper.clear(); |
| 116 | return nullptr; |
| 117 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 118 | if (mHandler == nullptr) { |
Shuzhen Wang | 7e54068 | 2020-04-10 13:30:25 -0700 | [diff] [blame] | 119 | mHandler = new CallbackHandler(this); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 120 | } |
| 121 | mCbLooper->registerHandler(mHandler); |
| 122 | } |
| 123 | |
| 124 | // register ICameraServiceListener |
| 125 | if (mCameraServiceListener == nullptr) { |
| 126 | mCameraServiceListener = new CameraServiceListener(this); |
| 127 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 128 | std::vector<hardware::CameraStatus> cameraStatuses{}; |
| 129 | mCameraService->addListener(mCameraServiceListener, &cameraStatuses); |
| 130 | for (auto& c : cameraStatuses) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 131 | // Skip callback for cameras not belonging to the default device, as NDK doesn't support |
| 132 | // device awareness yet. |
| 133 | if (c.deviceId != kDefaultDeviceId) { |
| 134 | continue; |
| 135 | } |
| 136 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 137 | onStatusChangedLocked(c.status, c.cameraId); |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 138 | |
| 139 | for (auto& unavailablePhysicalId : c.unavailablePhysicalIds) { |
| 140 | onStatusChangedLocked(hardware::ICameraServiceListener::STATUS_NOT_PRESENT, |
| 141 | c.cameraId, unavailablePhysicalId); |
| 142 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 143 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 144 | |
| 145 | // setup vendor tags |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 146 | sp<VendorTagDescriptor> desc = new VendorTagDescriptor(); |
| 147 | binder::Status ret = mCameraService->getCameraVendorTagDescriptor(/*out*/desc.get()); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 148 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 149 | if (ret.isOk()) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 150 | if (0 < desc->getTagCount()) { |
| 151 | status_t err = VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc); |
| 152 | if (err != OK) { |
| 153 | ALOGE("%s: Failed to set vendor tag descriptors, received error %s (%d)", |
| 154 | __FUNCTION__, strerror(-err), err); |
| 155 | } |
| 156 | } else { |
| 157 | sp<VendorTagDescriptorCache> cache = |
| 158 | new VendorTagDescriptorCache(); |
| 159 | binder::Status res = |
| 160 | mCameraService->getCameraVendorTagCache( |
| 161 | /*out*/cache.get()); |
| 162 | if (res.serviceSpecificErrorCode() == |
| 163 | hardware::ICameraService::ERROR_DISCONNECTED) { |
| 164 | // No camera module available, not an error on devices with no cameras |
| 165 | VendorTagDescriptorCache::clearGlobalVendorTagCache(); |
| 166 | } else if (res.isOk()) { |
| 167 | status_t err = |
| 168 | VendorTagDescriptorCache::setAsGlobalVendorTagCache( |
| 169 | cache); |
| 170 | if (err != OK) { |
| 171 | ALOGE("%s: Failed to set vendor tag cache," |
| 172 | "received error %s (%d)", __FUNCTION__, |
| 173 | strerror(-err), err); |
| 174 | } |
| 175 | } else { |
| 176 | VendorTagDescriptorCache::clearGlobalVendorTagCache(); |
| 177 | ALOGE("%s: Failed to setup vendor tag cache: %s", |
Tomasz Wasilczyk | 12b04a5 | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 178 | __FUNCTION__, res.toString8().c_str()); |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 179 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 180 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 181 | } else if (ret.serviceSpecificErrorCode() == |
| 182 | hardware::ICameraService::ERROR_DEPRECATED_HAL) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 183 | ALOGW("%s: Camera HAL too old; does not support vendor tags", |
| 184 | __FUNCTION__); |
| 185 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
| 186 | } else { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 187 | ALOGE("%s: Failed to get vendor tag descriptors: %s", |
Tomasz Wasilczyk | 12b04a5 | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 188 | __FUNCTION__, ret.toString8().c_str()); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | ALOGE_IF(mCameraService == nullptr, "no CameraService!?"); |
| 192 | return mCameraService; |
| 193 | } |
| 194 | |
| 195 | void CameraManagerGlobal::DeathNotifier::binderDied(const wp<IBinder>&) |
| 196 | { |
| 197 | ALOGE("Camera service binderDied!"); |
| 198 | sp<CameraManagerGlobal> cm = mCameraManager.promote(); |
| 199 | if (cm != nullptr) { |
| 200 | AutoMutex lock(cm->mLock); |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 201 | std::vector<std::string> cameraIdList; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 202 | for (auto& pair : cm->mDeviceStatusMap) { |
Rucha Katakwar | c6e6401 | 2021-08-12 06:32:42 +0000 | [diff] [blame] | 203 | cameraIdList.push_back(pair.first); |
| 204 | } |
| 205 | |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 206 | for (const std::string& cameraId : cameraIdList) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 207 | cm->onStatusChangedLocked( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 208 | CameraServiceListener::STATUS_NOT_PRESENT, cameraId); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 209 | } |
| 210 | cm->mCameraService.clear(); |
| 211 | // TODO: consider adding re-connect call here? |
| 212 | } |
| 213 | } |
| 214 | |
Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 215 | void CameraManagerGlobal::registerExtendedAvailabilityCallback( |
| 216 | const ACameraManager_ExtendedAvailabilityCallbacks *callback) { |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 217 | return registerAvailCallback<ACameraManager_ExtendedAvailabilityCallbacks>(callback); |
Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void CameraManagerGlobal::unregisterExtendedAvailabilityCallback( |
| 221 | const ACameraManager_ExtendedAvailabilityCallbacks *callback) { |
| 222 | Mutex::Autolock _l(mLock); |
Shuzhen Wang | 7e54068 | 2020-04-10 13:30:25 -0700 | [diff] [blame] | 223 | |
| 224 | drainPendingCallbacksLocked(); |
| 225 | |
Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 226 | Callback cb(callback); |
| 227 | mCallbacks.erase(cb); |
| 228 | } |
| 229 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 230 | void CameraManagerGlobal::registerAvailabilityCallback( |
| 231 | const ACameraManager_AvailabilityCallbacks *callback) { |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 232 | return registerAvailCallback<ACameraManager_AvailabilityCallbacks>(callback); |
| 233 | } |
| 234 | |
| 235 | void CameraManagerGlobal::unregisterAvailabilityCallback( |
| 236 | const ACameraManager_AvailabilityCallbacks *callback) { |
| 237 | Mutex::Autolock _l(mLock); |
Shuzhen Wang | 7e54068 | 2020-04-10 13:30:25 -0700 | [diff] [blame] | 238 | |
| 239 | drainPendingCallbacksLocked(); |
| 240 | |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 241 | Callback cb(callback); |
| 242 | mCallbacks.erase(cb); |
| 243 | } |
| 244 | |
Shuzhen Wang | 7e54068 | 2020-04-10 13:30:25 -0700 | [diff] [blame] | 245 | void CameraManagerGlobal::onCallbackCalled() { |
| 246 | Mutex::Autolock _l(mLock); |
| 247 | if (mPendingCallbackCnt > 0) { |
| 248 | mPendingCallbackCnt--; |
| 249 | } |
| 250 | mCallbacksCond.signal(); |
| 251 | } |
| 252 | |
| 253 | void CameraManagerGlobal::drainPendingCallbacksLocked() { |
| 254 | while (mPendingCallbackCnt > 0) { |
| 255 | auto res = mCallbacksCond.waitRelative(mLock, kCallbackDrainTimeout); |
| 256 | if (res != NO_ERROR) { |
| 257 | ALOGE("%s: Error waiting to drain callbacks: %s(%d)", |
| 258 | __FUNCTION__, strerror(-res), res); |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 264 | template<class T> |
| 265 | void CameraManagerGlobal::registerAvailCallback(const T *callback) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 266 | Mutex::Autolock _l(mLock); |
Kwangkyu Park | 01f8443 | 2023-09-15 17:08:17 +0900 | [diff] [blame] | 267 | getCameraServiceLocked(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 268 | Callback cb(callback); |
| 269 | auto pair = mCallbacks.insert(cb); |
| 270 | // Send initial callbacks if callback is newly registered |
| 271 | if (pair.second) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 272 | for (auto& pair : mDeviceStatusMap) { |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 273 | const std::string& cameraId = pair.first; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 274 | int32_t status = pair.second.getStatus(); |
Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 275 | // Don't send initial callbacks for camera ids which don't support |
| 276 | // camera2 |
| 277 | if (!pair.second.supportsHAL3) { |
| 278 | continue; |
| 279 | } |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 280 | |
| 281 | // Camera available/unavailable callback |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 282 | sp<AMessage> msg = new AMessage(kWhatSendSingleCallback, mHandler); |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 283 | ACameraManager_AvailabilityCallback cbFunc = isStatusAvailable(status) ? |
| 284 | cb.mAvailable : cb.mUnavailable; |
| 285 | msg->setPointer(kCallbackFpKey, (void *) cbFunc); |
| 286 | msg->setPointer(kContextKey, cb.mContext); |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 287 | msg->setString(kCameraIdKey, AString(cameraId.c_str())); |
Shuzhen Wang | 7e54068 | 2020-04-10 13:30:25 -0700 | [diff] [blame] | 288 | mPendingCallbackCnt++; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 289 | msg->post(); |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 290 | |
| 291 | // Physical camera unavailable callback |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 292 | std::set<std::string> unavailablePhysicalCameras = |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 293 | pair.second.getUnavailablePhysicalIds(); |
| 294 | for (const auto& physicalCameraId : unavailablePhysicalCameras) { |
| 295 | sp<AMessage> msg = new AMessage(kWhatSendSinglePhysicalCameraCallback, mHandler); |
| 296 | ACameraManager_PhysicalCameraAvailabilityCallback cbFunc = |
| 297 | cb.mPhysicalCamUnavailable; |
| 298 | msg->setPointer(kCallbackFpKey, (void *) cbFunc); |
| 299 | msg->setPointer(kContextKey, cb.mContext); |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 300 | msg->setString(kCameraIdKey, AString(cameraId.c_str())); |
| 301 | msg->setString(kPhysicalCameraIdKey, AString(physicalCameraId.c_str())); |
Shuzhen Wang | 7e54068 | 2020-04-10 13:30:25 -0700 | [diff] [blame] | 302 | mPendingCallbackCnt++; |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 303 | msg->post(); |
| 304 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 309 | bool CameraManagerGlobal::supportsCamera2ApiLocked(const std::string &cameraId) { |
Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 310 | bool camera2Support = false; |
| 311 | auto cs = getCameraServiceLocked(); |
| 312 | binder::Status serviceRet = |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 313 | cs->supportsCameraApi(cameraId, |
Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 314 | hardware::ICameraService::API_VERSION_2, &camera2Support); |
| 315 | if (!serviceRet.isOk()) { |
| 316 | ALOGE("%s: supportsCameraApi2Locked() call failed for cameraId %s", |
| 317 | __FUNCTION__, cameraId.c_str()); |
| 318 | return false; |
| 319 | } |
| 320 | return camera2Support; |
| 321 | } |
| 322 | |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 323 | void CameraManagerGlobal::getCameraIdList(std::vector<std::string>* cameraIds) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 324 | // Ensure that we have initialized/refreshed the list of available devices |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 325 | Mutex::Autolock _l(mLock); |
Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 326 | // Needed to make sure we're connected to cameraservice |
| 327 | getCameraServiceLocked(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 328 | for(auto& deviceStatus : mDeviceStatusMap) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 329 | int32_t status = deviceStatus.second.getStatus(); |
| 330 | if (status == hardware::ICameraServiceListener::STATUS_NOT_PRESENT || |
| 331 | status == hardware::ICameraServiceListener::STATUS_ENUMERATING) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 332 | continue; |
| 333 | } |
Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 334 | if (!deviceStatus.second.supportsHAL3) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 335 | continue; |
| 336 | } |
| 337 | cameraIds->push_back(deviceStatus.first); |
| 338 | } |
| 339 | } |
| 340 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 341 | bool CameraManagerGlobal::validStatus(int32_t status) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 342 | switch (status) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 343 | case hardware::ICameraServiceListener::STATUS_NOT_PRESENT: |
| 344 | case hardware::ICameraServiceListener::STATUS_PRESENT: |
| 345 | case hardware::ICameraServiceListener::STATUS_ENUMERATING: |
| 346 | case hardware::ICameraServiceListener::STATUS_NOT_AVAILABLE: |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 347 | return true; |
| 348 | default: |
| 349 | return false; |
| 350 | } |
| 351 | } |
| 352 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 353 | bool CameraManagerGlobal::isStatusAvailable(int32_t status) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 354 | switch (status) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 355 | case hardware::ICameraServiceListener::STATUS_PRESENT: |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 356 | return true; |
| 357 | default: |
| 358 | return false; |
| 359 | } |
| 360 | } |
| 361 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 362 | void CameraManagerGlobal::CallbackHandler::onMessageReceived( |
Shuzhen Wang | 7e54068 | 2020-04-10 13:30:25 -0700 | [diff] [blame] | 363 | const sp<AMessage> &msg) { |
| 364 | onMessageReceivedInternal(msg); |
| 365 | if (msg->what() == kWhatSendSingleCallback || |
| 366 | msg->what() == kWhatSendSingleAccessCallback || |
| 367 | msg->what() == kWhatSendSinglePhysicalCameraCallback) { |
| 368 | notifyParent(); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void CameraManagerGlobal::CallbackHandler::onMessageReceivedInternal( |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 373 | const sp<AMessage> &msg) { |
| 374 | switch (msg->what()) { |
| 375 | case kWhatSendSingleCallback: |
| 376 | { |
| 377 | ACameraManager_AvailabilityCallback cb; |
| 378 | void* context; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 379 | AString cameraId; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 380 | bool found = msg->findPointer(kCallbackFpKey, (void**) &cb); |
| 381 | if (!found) { |
| 382 | ALOGE("%s: Cannot find camera callback fp!", __FUNCTION__); |
| 383 | return; |
| 384 | } |
| 385 | found = msg->findPointer(kContextKey, &context); |
| 386 | if (!found) { |
| 387 | ALOGE("%s: Cannot find callback context!", __FUNCTION__); |
| 388 | return; |
| 389 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 390 | found = msg->findString(kCameraIdKey, &cameraId); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 391 | if (!found) { |
| 392 | ALOGE("%s: Cannot find camera ID!", __FUNCTION__); |
| 393 | return; |
| 394 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 395 | (*cb)(context, cameraId.c_str()); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 396 | break; |
| 397 | } |
Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 398 | case kWhatSendSingleAccessCallback: |
| 399 | { |
| 400 | ACameraManager_AccessPrioritiesChangedCallback cb; |
| 401 | void* context; |
| 402 | AString cameraId; |
| 403 | bool found = msg->findPointer(kCallbackFpKey, (void**) &cb); |
| 404 | if (!found) { |
| 405 | ALOGE("%s: Cannot find camera callback fp!", __FUNCTION__); |
| 406 | return; |
| 407 | } |
| 408 | found = msg->findPointer(kContextKey, &context); |
| 409 | if (!found) { |
| 410 | ALOGE("%s: Cannot find callback context!", __FUNCTION__); |
| 411 | return; |
| 412 | } |
| 413 | (*cb)(context); |
| 414 | break; |
| 415 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 416 | case kWhatSendSinglePhysicalCameraCallback: |
| 417 | { |
| 418 | ACameraManager_PhysicalCameraAvailabilityCallback cb; |
| 419 | void* context; |
| 420 | AString cameraId; |
| 421 | AString physicalCameraId; |
| 422 | bool found = msg->findPointer(kCallbackFpKey, (void**) &cb); |
| 423 | if (!found) { |
| 424 | ALOGE("%s: Cannot find camera callback fp!", __FUNCTION__); |
| 425 | return; |
| 426 | } |
| 427 | if (cb == nullptr) { |
| 428 | // Physical camera callback is null |
| 429 | return; |
| 430 | } |
| 431 | found = msg->findPointer(kContextKey, &context); |
| 432 | if (!found) { |
| 433 | ALOGE("%s: Cannot find callback context!", __FUNCTION__); |
| 434 | return; |
| 435 | } |
| 436 | found = msg->findString(kCameraIdKey, &cameraId); |
| 437 | if (!found) { |
| 438 | ALOGE("%s: Cannot find camera ID!", __FUNCTION__); |
| 439 | return; |
| 440 | } |
| 441 | found = msg->findString(kPhysicalCameraIdKey, &physicalCameraId); |
| 442 | if (!found) { |
| 443 | ALOGE("%s: Cannot find physical camera ID!", __FUNCTION__); |
| 444 | return; |
| 445 | } |
| 446 | (*cb)(context, cameraId.c_str(), physicalCameraId.c_str()); |
| 447 | break; |
| 448 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 449 | default: |
| 450 | ALOGE("%s: unknown message type %d", __FUNCTION__, msg->what()); |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | |
Shuzhen Wang | 7e54068 | 2020-04-10 13:30:25 -0700 | [diff] [blame] | 455 | void CameraManagerGlobal::CallbackHandler::notifyParent() { |
| 456 | sp<CameraManagerGlobal> parent = mParent.promote(); |
| 457 | if (parent != nullptr) { |
| 458 | parent->onCallbackCalled(); |
| 459 | } |
| 460 | } |
| 461 | |
Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 462 | binder::Status CameraManagerGlobal::CameraServiceListener::onCameraAccessPrioritiesChanged() { |
| 463 | sp<CameraManagerGlobal> cm = mCameraManager.promote(); |
| 464 | if (cm != nullptr) { |
| 465 | cm->onCameraAccessPrioritiesChanged(); |
| 466 | } else { |
| 467 | ALOGE("Cannot deliver camera access priority callback. Global camera manager died"); |
| 468 | } |
| 469 | return binder::Status::ok(); |
| 470 | } |
| 471 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 472 | binder::Status CameraManagerGlobal::CameraServiceListener::onStatusChanged( |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 473 | int32_t status, const std::string& cameraId, int deviceId) { |
| 474 | // Skip callback for cameras not belonging to the default device, as NDK doesn't support |
| 475 | // device awareness yet. |
| 476 | if (deviceId != kDefaultDeviceId) { |
| 477 | return binder::Status::ok(); |
| 478 | } |
| 479 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 480 | sp<CameraManagerGlobal> cm = mCameraManager.promote(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 481 | if (cm != nullptr) { |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 482 | cm->onStatusChanged(status, cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 483 | } else { |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 484 | ALOGE("Cannot deliver status change. Global camera manager died"); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 485 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 486 | return binder::Status::ok(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 489 | binder::Status CameraManagerGlobal::CameraServiceListener::onPhysicalCameraStatusChanged( |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 490 | int32_t status, const std::string& cameraId, const std::string& physicalCameraId, |
| 491 | int deviceId) { |
| 492 | // Skip callback for cameras not belonging to the default device, as NDK doesn't support |
| 493 | // device awareness yet. |
| 494 | if (deviceId != kDefaultDeviceId) { |
| 495 | return binder::Status::ok(); |
| 496 | } |
| 497 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 498 | sp<CameraManagerGlobal> cm = mCameraManager.promote(); |
| 499 | if (cm != nullptr) { |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 500 | cm->onStatusChanged(status, cameraId, physicalCameraId); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 501 | } else { |
| 502 | ALOGE("Cannot deliver physical camera status change. Global camera manager died"); |
| 503 | } |
| 504 | return binder::Status::ok(); |
| 505 | } |
| 506 | |
Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 507 | void CameraManagerGlobal::onCameraAccessPrioritiesChanged() { |
| 508 | Mutex::Autolock _l(mLock); |
| 509 | for (auto cb : mCallbacks) { |
| 510 | sp<AMessage> msg = new AMessage(kWhatSendSingleAccessCallback, mHandler); |
| 511 | ACameraManager_AccessPrioritiesChangedCallback cbFp = cb.mAccessPriorityChanged; |
| 512 | if (cbFp != nullptr) { |
| 513 | msg->setPointer(kCallbackFpKey, (void *) cbFp); |
| 514 | msg->setPointer(kContextKey, cb.mContext); |
Shuzhen Wang | 7e54068 | 2020-04-10 13:30:25 -0700 | [diff] [blame] | 515 | mPendingCallbackCnt++; |
Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 516 | msg->post(); |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 521 | void CameraManagerGlobal::onStatusChanged( |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 522 | int32_t status, const std::string& cameraId) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 523 | Mutex::Autolock _l(mLock); |
| 524 | onStatusChangedLocked(status, cameraId); |
| 525 | } |
| 526 | |
| 527 | void CameraManagerGlobal::onStatusChangedLocked( |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 528 | int32_t status, const std::string& cameraId) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 529 | if (!validStatus(status)) { |
| 530 | ALOGE("%s: Invalid status %d", __FUNCTION__, status); |
| 531 | return; |
| 532 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 533 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 534 | bool firstStatus = (mDeviceStatusMap.count(cameraId) == 0); |
| 535 | int32_t oldStatus = firstStatus ? |
| 536 | status : // first status |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 537 | mDeviceStatusMap[cameraId].getStatus(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 538 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 539 | if (!firstStatus && |
| 540 | isStatusAvailable(status) == isStatusAvailable(oldStatus)) { |
| 541 | // No status update. No need to send callback |
| 542 | return; |
| 543 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 544 | |
Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 545 | bool supportsHAL3 = supportsCamera2ApiLocked(cameraId); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 546 | if (firstStatus) { |
| 547 | mDeviceStatusMap.emplace(std::piecewise_construct, |
| 548 | std::forward_as_tuple(cameraId), |
| 549 | std::forward_as_tuple(status, supportsHAL3)); |
| 550 | } else { |
| 551 | mDeviceStatusMap[cameraId].updateStatus(status); |
| 552 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 553 | // Iterate through all registered callbacks |
Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 554 | if (supportsHAL3) { |
| 555 | for (auto cb : mCallbacks) { |
| 556 | sp<AMessage> msg = new AMessage(kWhatSendSingleCallback, mHandler); |
| 557 | ACameraManager_AvailabilityCallback cbFp = isStatusAvailable(status) ? |
| 558 | cb.mAvailable : cb.mUnavailable; |
| 559 | msg->setPointer(kCallbackFpKey, (void *) cbFp); |
| 560 | msg->setPointer(kContextKey, cb.mContext); |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 561 | msg->setString(kCameraIdKey, AString(cameraId.c_str())); |
Shuzhen Wang | 7e54068 | 2020-04-10 13:30:25 -0700 | [diff] [blame] | 562 | mPendingCallbackCnt++; |
Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 563 | msg->post(); |
| 564 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 565 | } |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 566 | if (status == hardware::ICameraServiceListener::STATUS_NOT_PRESENT) { |
| 567 | mDeviceStatusMap.erase(cameraId); |
| 568 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 571 | void CameraManagerGlobal::onStatusChanged( |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 572 | int32_t status, const std::string& cameraId, const std::string& physicalCameraId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 573 | Mutex::Autolock _l(mLock); |
| 574 | onStatusChangedLocked(status, cameraId, physicalCameraId); |
| 575 | } |
| 576 | |
| 577 | void CameraManagerGlobal::onStatusChangedLocked( |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 578 | int32_t status, const std::string& cameraId, const std::string& physicalCameraId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 579 | if (!validStatus(status)) { |
| 580 | ALOGE("%s: Invalid status %d", __FUNCTION__, status); |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | auto logicalStatus = mDeviceStatusMap.find(cameraId); |
| 585 | if (logicalStatus == mDeviceStatusMap.end()) { |
| 586 | ALOGE("%s: Physical camera id %s status change on a non-present id %s", |
| 587 | __FUNCTION__, physicalCameraId.c_str(), cameraId.c_str()); |
| 588 | return; |
| 589 | } |
| 590 | int32_t logicalCamStatus = mDeviceStatusMap[cameraId].getStatus(); |
| 591 | if (logicalCamStatus != hardware::ICameraServiceListener::STATUS_PRESENT && |
| 592 | logicalCamStatus != hardware::ICameraServiceListener::STATUS_NOT_AVAILABLE) { |
| 593 | ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d", |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 594 | __FUNCTION__, physicalCameraId.c_str(), status, logicalCamStatus); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 595 | return; |
| 596 | } |
| 597 | |
| 598 | bool supportsHAL3 = supportsCamera2ApiLocked(cameraId); |
| 599 | |
| 600 | bool updated = false; |
| 601 | if (status == hardware::ICameraServiceListener::STATUS_PRESENT) { |
| 602 | updated = mDeviceStatusMap[cameraId].removeUnavailablePhysicalId(physicalCameraId); |
| 603 | } else { |
| 604 | updated = mDeviceStatusMap[cameraId].addUnavailablePhysicalId(physicalCameraId); |
| 605 | } |
| 606 | |
| 607 | // Iterate through all registered callbacks |
| 608 | if (supportsHAL3 && updated) { |
| 609 | for (auto cb : mCallbacks) { |
| 610 | sp<AMessage> msg = new AMessage(kWhatSendSinglePhysicalCameraCallback, mHandler); |
| 611 | ACameraManager_PhysicalCameraAvailabilityCallback cbFp = isStatusAvailable(status) ? |
| 612 | cb.mPhysicalCamAvailable : cb.mPhysicalCamUnavailable; |
| 613 | msg->setPointer(kCallbackFpKey, (void *) cbFp); |
| 614 | msg->setPointer(kContextKey, cb.mContext); |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 615 | msg->setString(kCameraIdKey, AString(cameraId.c_str())); |
| 616 | msg->setString(kPhysicalCameraIdKey, AString(physicalCameraId.c_str())); |
Shuzhen Wang | 7e54068 | 2020-04-10 13:30:25 -0700 | [diff] [blame] | 617 | mPendingCallbackCnt++; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 618 | msg->post(); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | int32_t CameraManagerGlobal::StatusAndHAL3Support::getStatus() { |
| 624 | std::lock_guard<std::mutex> lock(mLock); |
| 625 | return status; |
| 626 | } |
| 627 | |
| 628 | void CameraManagerGlobal::StatusAndHAL3Support::updateStatus(int32_t newStatus) { |
| 629 | std::lock_guard<std::mutex> lock(mLock); |
| 630 | status = newStatus; |
| 631 | } |
| 632 | |
| 633 | bool CameraManagerGlobal::StatusAndHAL3Support::addUnavailablePhysicalId( |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 634 | const std::string& physicalCameraId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 635 | std::lock_guard<std::mutex> lock(mLock); |
| 636 | auto result = unavailablePhysicalIds.insert(physicalCameraId); |
| 637 | return result.second; |
| 638 | } |
| 639 | |
| 640 | bool CameraManagerGlobal::StatusAndHAL3Support::removeUnavailablePhysicalId( |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 641 | const std::string& physicalCameraId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 642 | std::lock_guard<std::mutex> lock(mLock); |
| 643 | auto count = unavailablePhysicalIds.erase(physicalCameraId); |
| 644 | return count > 0; |
| 645 | } |
| 646 | |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 647 | std::set<std::string> CameraManagerGlobal::StatusAndHAL3Support::getUnavailablePhysicalIds() { |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 648 | std::lock_guard<std::mutex> lock(mLock); |
| 649 | return unavailablePhysicalIds; |
| 650 | } |
| 651 | |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 652 | } // namespace acam |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 653 | } // namespace android |
| 654 | |
| 655 | /** |
| 656 | * ACameraManger Implementation |
| 657 | */ |
| 658 | camera_status_t |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 659 | ACameraManager::getCameraIdList(ACameraIdList** cameraIdList) { |
| 660 | Mutex::Autolock _l(mLock); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 661 | |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 662 | std::vector<std::string> idList; |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 663 | CameraManagerGlobal::getInstance()->getCameraIdList(&idList); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 664 | |
| 665 | int numCameras = idList.size(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 666 | ACameraIdList *out = new ACameraIdList; |
| 667 | if (!out) { |
| 668 | ALOGE("Allocate memory for ACameraIdList failed!"); |
| 669 | return ACAMERA_ERROR_NOT_ENOUGH_MEMORY; |
| 670 | } |
| 671 | out->numCameras = numCameras; |
Bjoern Johansson | 1a5954c | 2017-01-10 10:30:18 -0800 | [diff] [blame] | 672 | out->cameraIds = new const char*[numCameras]; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 673 | if (!out->cameraIds) { |
| 674 | ALOGE("Allocate memory for ACameraIdList failed!"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 675 | deleteCameraIdList(out); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 676 | return ACAMERA_ERROR_NOT_ENOUGH_MEMORY; |
| 677 | } |
| 678 | for (int i = 0; i < numCameras; i++) { |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 679 | const char* src = idList[i].c_str(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 680 | size_t dstSize = strlen(src) + 1; |
| 681 | char* dst = new char[dstSize]; |
| 682 | if (!dst) { |
| 683 | ALOGE("Allocate memory for ACameraIdList failed!"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 684 | deleteCameraIdList(out); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 685 | return ACAMERA_ERROR_NOT_ENOUGH_MEMORY; |
| 686 | } |
| 687 | strlcpy(dst, src, dstSize); |
| 688 | out->cameraIds[i] = dst; |
| 689 | } |
| 690 | *cameraIdList = out; |
| 691 | return ACAMERA_OK; |
| 692 | } |
| 693 | |
| 694 | void |
| 695 | ACameraManager::deleteCameraIdList(ACameraIdList* cameraIdList) { |
| 696 | if (cameraIdList != nullptr) { |
| 697 | if (cameraIdList->cameraIds != nullptr) { |
| 698 | for (int i = 0; i < cameraIdList->numCameras; i ++) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 699 | if (cameraIdList->cameraIds[i] != nullptr) { |
| 700 | delete[] cameraIdList->cameraIds[i]; |
| 701 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 702 | } |
| 703 | delete[] cameraIdList->cameraIds; |
| 704 | } |
| 705 | delete cameraIdList; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | camera_status_t ACameraManager::getCameraCharacteristics( |
Yin-Chia Yeh | dd045bf | 2018-08-20 12:39:19 -0700 | [diff] [blame] | 710 | const char* cameraIdStr, sp<ACameraMetadata>* characteristics) { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 711 | Mutex::Autolock _l(mLock); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 712 | |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 713 | sp<hardware::ICameraService> cs = CameraManagerGlobal::getInstance()->getCameraService(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 714 | if (cs == nullptr) { |
| 715 | ALOGE("%s: Cannot reach camera service!", __FUNCTION__); |
| 716 | return ACAMERA_ERROR_CAMERA_DISCONNECTED; |
| 717 | } |
Austin Borger | 3560b7e | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 718 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 719 | CameraMetadata rawMetadata; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 720 | int targetSdkVersion = android_get_application_target_sdk_version(); |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 721 | binder::Status serviceRet = cs->getCameraCharacteristics(cameraIdStr, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 722 | targetSdkVersion, /*rotationOverride*/hardware::ICameraService::ROTATION_OVERRIDE_NONE, |
| 723 | kDefaultDeviceId, /*devicePolicy*/0, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 724 | &rawMetadata); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 725 | if (!serviceRet.isOk()) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 726 | switch(serviceRet.serviceSpecificErrorCode()) { |
| 727 | case hardware::ICameraService::ERROR_DISCONNECTED: |
| 728 | ALOGE("%s: Camera %s has been disconnected", __FUNCTION__, cameraIdStr); |
| 729 | return ACAMERA_ERROR_CAMERA_DISCONNECTED; |
| 730 | case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT: |
| 731 | ALOGE("%s: Camera ID %s does not exist!", __FUNCTION__, cameraIdStr); |
| 732 | return ACAMERA_ERROR_INVALID_PARAMETER; |
| 733 | default: |
| 734 | ALOGE("Get camera characteristics from camera service failed: %s", |
Tomasz Wasilczyk | 12b04a5 | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 735 | serviceRet.toString8().c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 736 | return ACAMERA_ERROR_UNKNOWN; // should not reach here |
| 737 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | *characteristics = new ACameraMetadata( |
| 741 | rawMetadata.release(), ACameraMetadata::ACM_CHARACTERISTICS); |
| 742 | return ACAMERA_OK; |
| 743 | } |
| 744 | |
| 745 | camera_status_t |
| 746 | ACameraManager::openCamera( |
| 747 | const char* cameraId, |
| 748 | ACameraDevice_StateCallbacks* callback, |
| 749 | /*out*/ACameraDevice** outDevice) { |
Yin-Chia Yeh | dd045bf | 2018-08-20 12:39:19 -0700 | [diff] [blame] | 750 | sp<ACameraMetadata> chars; |
| 751 | camera_status_t ret = getCameraCharacteristics(cameraId, &chars); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 752 | Mutex::Autolock _l(mLock); |
| 753 | if (ret != ACAMERA_OK) { |
| 754 | ALOGE("%s: cannot get camera characteristics for camera %s. err %d", |
| 755 | __FUNCTION__, cameraId, ret); |
| 756 | return ACAMERA_ERROR_INVALID_PARAMETER; |
| 757 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 758 | |
Yin-Chia Yeh | dd045bf | 2018-08-20 12:39:19 -0700 | [diff] [blame] | 759 | ACameraDevice* device = new ACameraDevice(cameraId, callback, chars); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 760 | |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 761 | sp<hardware::ICameraService> cs = CameraManagerGlobal::getInstance()->getCameraService(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 762 | if (cs == nullptr) { |
| 763 | ALOGE("%s: Cannot reach camera service!", __FUNCTION__); |
Yunlian Jiang | b01d8f7 | 2016-10-04 16:34:18 -0700 | [diff] [blame] | 764 | delete device; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 765 | return ACAMERA_ERROR_CAMERA_DISCONNECTED; |
| 766 | } |
| 767 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 768 | sp<hardware::camera2::ICameraDeviceCallbacks> callbacks = device->getServiceCallback(); |
| 769 | sp<hardware::camera2::ICameraDeviceUser> deviceRemote; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 770 | int targetSdkVersion = android_get_application_target_sdk_version(); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 771 | // No way to get package name from native. |
| 772 | // 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] | 773 | binder::Status serviceRet = cs->connectDevice( |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 774 | callbacks, cameraId, "", {}, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 775 | hardware::ICameraService::USE_CALLING_UID, /*oomScoreOffset*/0, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 776 | targetSdkVersion, /*rotationOverride*/hardware::ICameraService::ROTATION_OVERRIDE_NONE, |
| 777 | kDefaultDeviceId, /*devicePolicy*/0, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 778 | /*out*/&deviceRemote); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 779 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 780 | if (!serviceRet.isOk()) { |
Tomasz Wasilczyk | 12b04a5 | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 781 | ALOGE("%s: connect camera device failed: %s", __FUNCTION__, serviceRet.toString8().c_str()); |
Yin-Chia Yeh | 3e49be1 | 2016-04-12 16:00:33 -0700 | [diff] [blame] | 782 | // Convert serviceRet to camera_status_t |
| 783 | switch(serviceRet.serviceSpecificErrorCode()) { |
| 784 | case hardware::ICameraService::ERROR_DISCONNECTED: |
| 785 | ret = ACAMERA_ERROR_CAMERA_DISCONNECTED; |
| 786 | break; |
| 787 | case hardware::ICameraService::ERROR_CAMERA_IN_USE: |
| 788 | ret = ACAMERA_ERROR_CAMERA_IN_USE; |
| 789 | break; |
| 790 | case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE: |
| 791 | ret = ACAMERA_ERROR_MAX_CAMERA_IN_USE; |
| 792 | break; |
| 793 | case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT: |
| 794 | ret = ACAMERA_ERROR_INVALID_PARAMETER; |
| 795 | break; |
| 796 | case hardware::ICameraService::ERROR_DEPRECATED_HAL: |
| 797 | // Should not reach here since we filtered legacy HALs earlier |
| 798 | ret = ACAMERA_ERROR_INVALID_PARAMETER; |
| 799 | break; |
| 800 | case hardware::ICameraService::ERROR_DISABLED: |
| 801 | ret = ACAMERA_ERROR_CAMERA_DISABLED; |
| 802 | break; |
| 803 | case hardware::ICameraService::ERROR_PERMISSION_DENIED: |
| 804 | ret = ACAMERA_ERROR_PERMISSION_DENIED; |
| 805 | break; |
| 806 | case hardware::ICameraService::ERROR_INVALID_OPERATION: |
| 807 | default: |
| 808 | ret = ACAMERA_ERROR_UNKNOWN; |
| 809 | break; |
| 810 | } |
| 811 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 812 | delete device; |
Yin-Chia Yeh | 3e49be1 | 2016-04-12 16:00:33 -0700 | [diff] [blame] | 813 | return ret; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 814 | } |
| 815 | if (deviceRemote == nullptr) { |
| 816 | ALOGE("%s: connect camera device failed! remote device is null", __FUNCTION__); |
| 817 | delete device; |
| 818 | return ACAMERA_ERROR_CAMERA_DISCONNECTED; |
| 819 | } |
| 820 | device->setRemoteDevice(deviceRemote); |
| 821 | *outDevice = device; |
| 822 | return ACAMERA_OK; |
| 823 | } |
| 824 | |
| 825 | ACameraManager::~ACameraManager() { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 826 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 827 | } |