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