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