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