Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -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_TAG "CameraFlashlight" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 19 | // #define LOG_NDEBUG 0 |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 20 | |
| 21 | #include <utils/Log.h> |
| 22 | #include <utils/Trace.h> |
| 23 | #include <cutils/properties.h> |
| 24 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 25 | #include "CameraFlashlight.h" |
Jim Shargo | 7bc756d | 2024-07-01 16:41:32 +0000 | [diff] [blame^] | 26 | #include "camera/CameraMetadata.h" |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 27 | #include "camera/camera2/CaptureRequest.h" |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 31 | ///////////////////////////////////////////////////////////////////// |
| 32 | // CameraFlashlight implementation begins |
| 33 | // used by camera service to control flashflight. |
| 34 | ///////////////////////////////////////////////////////////////////// |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 35 | |
| 36 | CameraFlashlight::CameraFlashlight(sp<CameraProviderManager> providerManager, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 37 | CameraProviderManager::StatusListener* callbacks) : |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 38 | mProviderManager(providerManager), |
| 39 | mCallbacks(callbacks), |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 40 | mFlashlightMapInitialized(false) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | CameraFlashlight::~CameraFlashlight() { |
| 44 | } |
| 45 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 46 | status_t CameraFlashlight::createFlashlightControl(const std::string& cameraId) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 47 | ALOGV("%s: creating a flash light control for camera %s", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 48 | cameraId.c_str()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 49 | if (mFlashControl != NULL) { |
| 50 | return INVALID_OPERATION; |
| 51 | } |
| 52 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 53 | if (mProviderManager->supportSetTorchMode(cameraId)) { |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 54 | mFlashControl = new ProviderFlashControl(mProviderManager); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 55 | } else { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 56 | ALOGE("Flashlight control not supported by this device!"); |
| 57 | return NO_INIT; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | return OK; |
| 61 | } |
| 62 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 63 | status_t CameraFlashlight::setTorchMode(const std::string& cameraId, bool enabled) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 64 | if (!mFlashlightMapInitialized) { |
Ranjith Kagathi Ananda | 32ab9fd | 2015-10-08 16:41:09 -0700 | [diff] [blame] | 65 | ALOGE("%s: findFlashUnits() must be called before this method.", |
| 66 | __FUNCTION__); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 67 | return NO_INIT; |
| 68 | } |
| 69 | |
| 70 | ALOGV("%s: set torch mode of camera %s to %d", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 71 | cameraId.c_str(), enabled); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 72 | |
| 73 | status_t res = OK; |
| 74 | Mutex::Autolock l(mLock); |
| 75 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 76 | if (mOpenedCameraIds.indexOf(cameraId) != NAME_NOT_FOUND) { |
| 77 | // This case is needed to avoid state corruption during the following call sequence: |
| 78 | // CameraService::setTorchMode for camera ID 0 begins, does torch status checks |
| 79 | // CameraService::connect for camera ID 0 begins, calls prepareDeviceOpen, ends |
| 80 | // CameraService::setTorchMode for camera ID 0 continues, calls |
| 81 | // CameraFlashlight::setTorchMode |
| 82 | |
| 83 | // TODO: Move torch status checks and state updates behind this CameraFlashlight lock |
| 84 | // to avoid other similar race conditions. |
| 85 | ALOGE("%s: Camera device %s is in use, cannot set torch mode.", |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 86 | __FUNCTION__, cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 87 | return -EBUSY; |
| 88 | } |
| 89 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 90 | if (mFlashControl == NULL) { |
| 91 | res = createFlashlightControl(cameraId); |
| 92 | if (res) { |
| 93 | return res; |
| 94 | } |
| 95 | res = mFlashControl->setTorchMode(cameraId, enabled); |
| 96 | return res; |
| 97 | } |
| 98 | |
| 99 | // if flash control already exists, turning on torch mode may fail if it's |
| 100 | // tied to another camera device for module v2.3 and below. |
| 101 | res = mFlashControl->setTorchMode(cameraId, enabled); |
| 102 | if (res == BAD_INDEX) { |
| 103 | // flash control is tied to another camera device, need to close it and |
| 104 | // try again. |
| 105 | mFlashControl.clear(); |
| 106 | res = createFlashlightControl(cameraId); |
| 107 | if (res) { |
| 108 | return res; |
| 109 | } |
| 110 | res = mFlashControl->setTorchMode(cameraId, enabled); |
| 111 | } |
| 112 | |
| 113 | return res; |
| 114 | } |
| 115 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 116 | status_t CameraFlashlight::turnOnTorchWithStrengthLevel(const std::string& cameraId, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 117 | int32_t torchStrength) { |
| 118 | if (!mFlashlightMapInitialized) { |
| 119 | ALOGE("%s: findFlashUnits() must be called before this method.", |
| 120 | __FUNCTION__); |
| 121 | return NO_INIT; |
| 122 | } |
| 123 | |
| 124 | ALOGV("%s: set torch strength of camera %s to %d", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 125 | cameraId.c_str(), torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 126 | status_t res = OK; |
| 127 | Mutex::Autolock l(mLock); |
| 128 | |
| 129 | if (mOpenedCameraIds.indexOf(cameraId) != NAME_NOT_FOUND) { |
| 130 | ALOGE("%s: Camera device %s is in use, cannot be turned ON.", |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 131 | __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 132 | return -EBUSY; |
| 133 | } |
| 134 | |
| 135 | if (mFlashControl == NULL) { |
| 136 | res = createFlashlightControl(cameraId); |
| 137 | if (res) { |
| 138 | return res; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | res = mFlashControl->turnOnTorchWithStrengthLevel(cameraId, torchStrength); |
| 143 | return res; |
| 144 | } |
| 145 | |
| 146 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 147 | status_t CameraFlashlight::getTorchStrengthLevel(const std::string& cameraId, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 148 | int32_t* torchStrength) { |
| 149 | status_t res = OK; |
| 150 | if (!mFlashlightMapInitialized) { |
| 151 | ALOGE("%s: findFlashUnits() must be called before this method.", |
| 152 | __FUNCTION__); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | Mutex::Autolock l(mLock); |
| 157 | |
| 158 | if (mFlashControl == NULL) { |
| 159 | res = createFlashlightControl(cameraId); |
| 160 | if (res) { |
| 161 | return res; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | res = mFlashControl->getTorchStrengthLevel(cameraId, torchStrength); |
| 166 | return res; |
| 167 | } |
| 168 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 169 | status_t CameraFlashlight::findFlashUnits() { |
| 170 | Mutex::Autolock l(mLock); |
| 171 | status_t res; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 172 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 173 | std::vector<std::string> cameraIds; |
Yin-Chia Yeh | 98c72a8 | 2018-08-28 11:20:16 -0700 | [diff] [blame] | 174 | std::vector<std::string> ids = mProviderManager->getCameraDeviceIds(); |
Yin-Chia Yeh | 6b1f611 | 2018-02-28 13:05:59 -0800 | [diff] [blame] | 175 | int numberOfCameras = static_cast<int>(ids.size()); |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 176 | cameraIds.resize(numberOfCameras); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 177 | // No module, must be provider |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 178 | for (size_t i = 0; i < cameraIds.size(); i++) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 179 | cameraIds[i] = ids[i]; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 180 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 181 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 182 | mFlashControl.clear(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 183 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 184 | for (auto &id : cameraIds) { |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 185 | ssize_t index = mHasFlashlightMap.indexOfKey(id); |
| 186 | if (0 <= index) { |
| 187 | continue; |
| 188 | } |
| 189 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 190 | bool hasFlash = false; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 191 | res = createFlashlightControl(id); |
| 192 | if (res) { |
| 193 | ALOGE("%s: failed to create flash control for %s", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 194 | id.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 195 | } else { |
| 196 | res = mFlashControl->hasFlashUnit(id, &hasFlash); |
| 197 | if (res == -EUSERS || res == -EBUSY) { |
| 198 | ALOGE("%s: failed to check if camera %s has a flash unit. Some " |
| 199 | "camera devices may be opened", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 200 | id.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 201 | return res; |
| 202 | } else if (res) { |
| 203 | ALOGE("%s: failed to check if camera %s has a flash unit. %s" |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 204 | " (%d)", __FUNCTION__, id.c_str(), strerror(-res), |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 205 | res); |
| 206 | } |
| 207 | |
| 208 | mFlashControl.clear(); |
| 209 | } |
| 210 | mHasFlashlightMap.add(id, hasFlash); |
| 211 | } |
| 212 | |
| 213 | mFlashlightMapInitialized = true; |
| 214 | return OK; |
| 215 | } |
| 216 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 217 | bool CameraFlashlight::hasFlashUnit(const std::string& cameraId) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 218 | Mutex::Autolock l(mLock); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 219 | return hasFlashUnitLocked(cameraId); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 222 | bool CameraFlashlight::hasFlashUnitLocked(const std::string& cameraId) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 223 | if (!mFlashlightMapInitialized) { |
Ranjith Kagathi Ananda | 32ab9fd | 2015-10-08 16:41:09 -0700 | [diff] [blame] | 224 | ALOGE("%s: findFlashUnits() must be called before this method.", |
| 225 | __FUNCTION__); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | |
| 229 | ssize_t index = mHasFlashlightMap.indexOfKey(cameraId); |
| 230 | if (index == NAME_NOT_FOUND) { |
Yin-Chia Yeh | 6b1f611 | 2018-02-28 13:05:59 -0800 | [diff] [blame] | 231 | // Might be external camera |
| 232 | ALOGW("%s: camera %s not present when findFlashUnits() was called", |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 233 | __FUNCTION__, cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 234 | return false; |
| 235 | } |
| 236 | |
| 237 | return mHasFlashlightMap.valueAt(index); |
| 238 | } |
| 239 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 240 | bool CameraFlashlight::isBackwardCompatibleMode(const std::string& cameraId) { |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 241 | bool backwardCompatibleMode = false; |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 242 | if (mProviderManager != nullptr && |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 243 | !mProviderManager->supportSetTorchMode(cameraId)) { |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 244 | backwardCompatibleMode = true; |
| 245 | } |
| 246 | return backwardCompatibleMode; |
| 247 | } |
| 248 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 249 | status_t CameraFlashlight::prepareDeviceOpen(const std::string& cameraId) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 250 | ALOGV("%s: prepare for device open", __FUNCTION__); |
| 251 | |
| 252 | Mutex::Autolock l(mLock); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 253 | if (!mFlashlightMapInitialized) { |
Ranjith Kagathi Ananda | 32ab9fd | 2015-10-08 16:41:09 -0700 | [diff] [blame] | 254 | ALOGE("%s: findFlashUnits() must be called before this method.", |
| 255 | __FUNCTION__); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 256 | return NO_INIT; |
| 257 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 258 | |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 259 | if (isBackwardCompatibleMode(cameraId)) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 260 | // framework is going to open a camera device, all flash light control |
| 261 | // should be closed for backward compatible support. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 262 | mFlashControl.clear(); |
| 263 | |
| 264 | if (mOpenedCameraIds.size() == 0) { |
| 265 | // notify torch unavailable for all cameras with a flash |
Yin-Chia Yeh | 98c72a8 | 2018-08-28 11:20:16 -0700 | [diff] [blame] | 266 | std::vector<std::string> ids = mProviderManager->getCameraDeviceIds(); |
Yin-Chia Yeh | 6b1f611 | 2018-02-28 13:05:59 -0800 | [diff] [blame] | 267 | int numCameras = static_cast<int>(ids.size()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 268 | for (int i = 0; i < numCameras; i++) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 269 | if (hasFlashUnitLocked(ids[i])) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 270 | mCallbacks->onTorchStatusChanged( |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 271 | ids[i], TorchModeStatus::NOT_AVAILABLE); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 272 | } |
| 273 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 274 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 275 | |
| 276 | // close flash control that may be opened by calling hasFlashUnitLocked. |
| 277 | mFlashControl.clear(); |
| 278 | } |
| 279 | |
| 280 | if (mOpenedCameraIds.indexOf(cameraId) == NAME_NOT_FOUND) { |
| 281 | mOpenedCameraIds.add(cameraId); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | return OK; |
| 285 | } |
| 286 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 287 | status_t CameraFlashlight::deviceClosed(const std::string& cameraId) { |
| 288 | ALOGV("%s: device %s is closed", __FUNCTION__, cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 289 | |
| 290 | Mutex::Autolock l(mLock); |
| 291 | if (!mFlashlightMapInitialized) { |
Ranjith Kagathi Ananda | 32ab9fd | 2015-10-08 16:41:09 -0700 | [diff] [blame] | 292 | ALOGE("%s: findFlashUnits() must be called before this method.", |
| 293 | __FUNCTION__); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 294 | return NO_INIT; |
| 295 | } |
| 296 | |
| 297 | ssize_t index = mOpenedCameraIds.indexOf(cameraId); |
| 298 | if (index == NAME_NOT_FOUND) { |
| 299 | ALOGE("%s: couldn't find camera %s in the opened list", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 300 | cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 301 | } else { |
| 302 | mOpenedCameraIds.removeAt(index); |
| 303 | } |
| 304 | |
| 305 | // Cannot do anything until all cameras are closed. |
| 306 | if (mOpenedCameraIds.size() != 0) |
| 307 | return OK; |
| 308 | |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 309 | if (isBackwardCompatibleMode(cameraId)) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 310 | // notify torch available for all cameras with a flash |
Yin-Chia Yeh | 98c72a8 | 2018-08-28 11:20:16 -0700 | [diff] [blame] | 311 | std::vector<std::string> ids = mProviderManager->getCameraDeviceIds(); |
Yin-Chia Yeh | 6b1f611 | 2018-02-28 13:05:59 -0800 | [diff] [blame] | 312 | int numCameras = static_cast<int>(ids.size()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 313 | for (int i = 0; i < numCameras; i++) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 314 | if (hasFlashUnitLocked(ids[i])) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 315 | mCallbacks->onTorchStatusChanged( |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 316 | ids[i], TorchModeStatus::AVAILABLE_OFF); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return OK; |
| 322 | } |
| 323 | // CameraFlashlight implementation ends |
| 324 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 325 | |
| 326 | FlashControlBase::~FlashControlBase() { |
| 327 | } |
| 328 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 329 | ///////////////////////////////////////////////////////////////////// |
| 330 | // ModuleFlashControl implementation begins |
| 331 | // Flash control for camera module v2.4 and above. |
| 332 | ///////////////////////////////////////////////////////////////////// |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 333 | ProviderFlashControl::ProviderFlashControl(sp<CameraProviderManager> providerManager) : |
| 334 | mProviderManager(providerManager) { |
| 335 | } |
| 336 | |
| 337 | ProviderFlashControl::~ProviderFlashControl() { |
| 338 | } |
| 339 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 340 | status_t ProviderFlashControl::hasFlashUnit(const std::string& cameraId, bool *hasFlash) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 341 | if (!hasFlash) { |
| 342 | return BAD_VALUE; |
| 343 | } |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 344 | *hasFlash = mProviderManager->hasFlashUnit(cameraId); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 345 | return OK; |
| 346 | } |
| 347 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 348 | status_t ProviderFlashControl::setTorchMode(const std::string& cameraId, bool enabled) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 349 | ALOGV("%s: set camera %s torch mode to %d", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 350 | cameraId.c_str(), enabled); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 351 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 352 | return mProviderManager->setTorchMode(cameraId, enabled); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 353 | } |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 354 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 355 | status_t ProviderFlashControl::turnOnTorchWithStrengthLevel(const std::string& cameraId, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 356 | int32_t torchStrength) { |
| 357 | ALOGV("%s: change torch strength level of camera %s to %d", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 358 | cameraId.c_str(), torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 359 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 360 | return mProviderManager->turnOnTorchWithStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 363 | status_t ProviderFlashControl::getTorchStrengthLevel(const std::string& cameraId, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 364 | int32_t* torchStrength) { |
| 365 | ALOGV("%s: get torch strength level of camera %s", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 366 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 367 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 368 | return mProviderManager->getTorchStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 369 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 370 | // ProviderFlashControl implementation ends |
| 371 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 372 | } |