Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 2 | * Copyright (C) 2008 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 | */ |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 16 | |
| 17 | #define LOG_TAG "CameraService" |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 20 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <climits> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 24 | #include <cstdlib> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 25 | #include <cstring> |
| 26 | #include <ctime> |
| 27 | #include <string> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 28 | #include <sys/types.h> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 29 | #include <inttypes.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 30 | #include <pthread.h> |
| 31 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 32 | #include <android/hardware/ICamera.h> |
| 33 | #include <android/hardware/ICameraClient.h> |
| 34 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 35 | #include <android-base/macros.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 36 | #include <android-base/parseint.h> |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 38 | #include <binder/ActivityManager.h> |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 39 | #include <binder/AppOpsManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 41 | #include <binder/MemoryBase.h> |
| 42 | #include <binder/MemoryHeapBase.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 43 | #include <binder/PermissionController.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 44 | #include <binder/IResultReceiver.h> |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 45 | #include <binderthreadstate/CallerUtils.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 46 | #include <cutils/atomic.h> |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 47 | #include <cutils/properties.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 48 | #include <cutils/misc.h> |
Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 49 | #include <gui/Surface.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 50 | #include <hardware/hardware.h> |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 51 | #include "hidl/HidlCameraService.h" |
| 52 | #include <hidl/HidlTransportSupport.h> |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 53 | #include <hwbinder/IPCThreadState.h> |
Eino-Ville Talvala | d89821e | 2016-04-20 11:23:50 -0700 | [diff] [blame] | 54 | #include <memunreachable/memunreachable.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 55 | #include <media/AudioSystem.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 56 | #include <media/IMediaHTTPService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 57 | #include <media/mediaplayer.h> |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 58 | #include <mediautils/BatteryNotifier.h> |
Steven Moreland | 886d732 | 2021-04-02 04:19:45 +0000 | [diff] [blame] | 59 | #include <processinfo/ProcessInfoService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 60 | #include <utils/Errors.h> |
| 61 | #include <utils/Log.h> |
| 62 | #include <utils/String16.h> |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 63 | #include <utils/SystemClock.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 64 | #include <utils/Trace.h> |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 65 | #include <utils/CallStack.h> |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 66 | #include <private/android_filesystem_config.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 67 | #include <system/camera_vendor_tags.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 68 | #include <system/camera_metadata.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 69 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 70 | #include <system/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 71 | |
| 72 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 73 | #include "api1/Camera2Client.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 74 | #include "api2/CameraDeviceClient.h" |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 75 | #include "utils/CameraTraces.h" |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 76 | #include "utils/TagMonitor.h" |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 77 | #include "utils/CameraThreadState.h" |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 78 | #include "utils/CameraServiceProxyWrapper.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 79 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 80 | namespace { |
| 81 | const char* kPermissionServiceName = "permission"; |
| 82 | }; // namespace anonymous |
| 83 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 84 | namespace android { |
| 85 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 86 | using base::StringPrintf; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 87 | using binder::Status; |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 88 | using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 89 | using hardware::ICamera; |
| 90 | using hardware::ICameraClient; |
| 91 | using hardware::ICameraServiceListener; |
| 92 | using hardware::camera::common::V1_0::CameraDeviceStatus; |
| 93 | using hardware::camera::common::V1_0::TorchModeStatus; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 94 | using hardware::camera2::utils::CameraIdAndSessionConfiguration; |
| 95 | using hardware::camera2::utils::ConcurrentCameraIdCombination; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 96 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 97 | // ---------------------------------------------------------------------------- |
| 98 | // Logging support -- this is for debugging only |
| 99 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 100 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 101 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 102 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 103 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 104 | |
| 105 | static void setLogLevel(int level) { |
| 106 | android_atomic_write(level, &gLogLevel); |
| 107 | } |
| 108 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 109 | // Convenience methods for constructing binder::Status objects for error returns |
| 110 | |
| 111 | #define STATUS_ERROR(errorCode, errorString) \ |
| 112 | binder::Status::fromServiceSpecificError(errorCode, \ |
| 113 | String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString)) |
| 114 | |
| 115 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ |
| 116 | binder::Status::fromServiceSpecificError(errorCode, \ |
| 117 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \ |
| 118 | __VA_ARGS__)) |
| 119 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 120 | // ---------------------------------------------------------------------------- |
| 121 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 122 | static const String16 sDumpPermission("android.permission.DUMP"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 123 | static const String16 sManageCameraPermission("android.permission.MANAGE_CAMERA"); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 124 | static const String16 sCameraPermission("android.permission.CAMERA"); |
| 125 | static const String16 sSystemCameraPermission("android.permission.SYSTEM_CAMERA"); |
| 126 | static const String16 |
| 127 | sCameraSendSystemEventsPermission("android.permission.CAMERA_SEND_SYSTEM_EVENTS"); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 128 | static const String16 sCameraOpenCloseListenerPermission( |
| 129 | "android.permission.CAMERA_OPEN_CLOSE_LISTENER"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 130 | |
Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame] | 131 | static constexpr int32_t kVendorClientScore = resource_policy::PERCEPTIBLE_APP_ADJ; |
| 132 | static constexpr int32_t kVendorClientState = ActivityManager::PROCESS_STATE_PERSISTENT_UI; |
| 133 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 134 | const String8 CameraService::kOfflineDevice("offline-"); |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 135 | |
Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 136 | CameraService::CameraService() : |
| 137 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 138 | mNumberOfCameras(0), |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 139 | mNumberOfCamerasWithoutSystemCamera(0), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 140 | mSoundRef(0), mInitialized(false), |
| 141 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 142 | ALOGI("CameraService started (pid=%d)", getpid()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 143 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 146 | void CameraService::onFirstRef() |
| 147 | { |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 148 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 149 | ALOGI("CameraService process starting"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 150 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 151 | BnCameraService::onFirstRef(); |
| 152 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 153 | // Update battery life tracking if service is restarting |
| 154 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 155 | notifier.noteResetCamera(); |
| 156 | notifier.noteResetFlashlight(); |
| 157 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 158 | status_t res = INVALID_OPERATION; |
Eino-Ville Talvala | 9cbbc83 | 2017-01-23 15:39:53 -0800 | [diff] [blame] | 159 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 160 | res = enumerateProviders(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 161 | if (res == OK) { |
| 162 | mInitialized = true; |
| 163 | } |
| 164 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 165 | mUidPolicy = new UidPolicy(this); |
| 166 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 167 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this); |
| 168 | mSensorPrivacyPolicy->registerSelf(); |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 169 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 170 | sp<HidlCameraService> hcs = HidlCameraService::getInstance(this); |
| 171 | if (hcs->registerAsService() != android::OK) { |
| 172 | ALOGE("%s: Failed to register default android.frameworks.cameraservice.service@1.0", |
| 173 | __FUNCTION__); |
| 174 | } |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 175 | |
| 176 | // This needs to be last call in this function, so that it's as close to |
| 177 | // ServiceManager::addService() as possible. |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 178 | CameraServiceProxyWrapper::pingCameraServiceProxy(); |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 179 | ALOGI("CameraService pinged cameraservice proxy"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 182 | status_t CameraService::enumerateProviders() { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 183 | status_t res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 184 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 185 | std::vector<std::string> deviceIds; |
| 186 | { |
| 187 | Mutex::Autolock l(mServiceLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 188 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 189 | if (nullptr == mCameraProviderManager.get()) { |
| 190 | mCameraProviderManager = new CameraProviderManager(); |
| 191 | res = mCameraProviderManager->initialize(this); |
| 192 | if (res != OK) { |
| 193 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", |
| 194 | __FUNCTION__, strerror(-res), res); |
| 195 | return res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 199 | |
| 200 | // Setup vendor tags before we call get_camera_info the first time |
| 201 | // because HAL might need to setup static vendor keys in get_camera_info |
| 202 | // TODO: maybe put this into CameraProviderManager::initialize()? |
| 203 | mCameraProviderManager->setUpVendorTags(); |
| 204 | |
| 205 | if (nullptr == mFlashlight.get()) { |
| 206 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 209 | res = mFlashlight->findFlashUnits(); |
| 210 | if (res != OK) { |
| 211 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); |
| 212 | } |
| 213 | |
| 214 | deviceIds = mCameraProviderManager->getCameraDeviceIds(); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | for (auto& cameraId : deviceIds) { |
| 219 | String8 id8 = String8(cameraId.c_str()); |
Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 220 | if (getCameraState(id8) == nullptr) { |
| 221 | onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT); |
| 222 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return OK; |
| 226 | } |
| 227 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 228 | void CameraService::broadcastTorchModeStatus(const String8& cameraId, TorchModeStatus status, |
| 229 | SystemCameraKind systemCameraKind) { |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 230 | Mutex::Autolock lock(mStatusListenerLock); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 231 | for (auto& i : mListenerList) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 232 | if (shouldSkipStatusUpdates(systemCameraKind, i->isVendorListener(), i->getListenerPid(), |
| 233 | i->getListenerUid())) { |
| 234 | ALOGV("Skipping torch callback for system-only camera device %s", |
| 235 | cameraId.c_str()); |
| 236 | continue; |
| 237 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 238 | i->getListener()->onTorchStatusChanged(mapToInterface(status), String16{cameraId}); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 242 | CameraService::~CameraService() { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 243 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 244 | mUidPolicy->unregisterSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 245 | mSensorPrivacyPolicy->unregisterSelf(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 248 | void CameraService::onNewProviderRegistered() { |
| 249 | enumerateProviders(); |
| 250 | } |
| 251 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 252 | void CameraService::filterAPI1SystemCameraLocked( |
| 253 | const std::vector<std::string> &normalDeviceIds) { |
| 254 | mNormalDeviceIdsWithoutSystemCamera.clear(); |
| 255 | for (auto &deviceId : normalDeviceIds) { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 256 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 257 | if (getSystemCameraKind(String8(deviceId.c_str()), &deviceKind) != OK) { |
| 258 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, deviceId.c_str()); |
| 259 | continue; |
| 260 | } |
| 261 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 262 | // All system camera ids will necessarily come after public camera |
| 263 | // device ids as per the HAL interface contract. |
| 264 | break; |
| 265 | } |
| 266 | mNormalDeviceIdsWithoutSystemCamera.push_back(deviceId); |
| 267 | } |
| 268 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, |
| 269 | mNormalDeviceIdsWithoutSystemCamera.size()); |
| 270 | } |
| 271 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 272 | status_t CameraService::getSystemCameraKind(const String8& cameraId, SystemCameraKind *kind) const { |
| 273 | auto state = getCameraState(cameraId); |
| 274 | if (state != nullptr) { |
| 275 | *kind = state->getSystemCameraKind(); |
| 276 | return OK; |
| 277 | } |
| 278 | // Hidden physical camera ids won't have CameraState |
| 279 | return mCameraProviderManager->getSystemCameraKind(cameraId.c_str(), kind); |
| 280 | } |
| 281 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 282 | void CameraService::updateCameraNumAndIds() { |
| 283 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 284 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); |
| 285 | // Excludes hidden secure cameras |
| 286 | mNumberOfCameras = |
| 287 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; |
| 288 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 289 | mNormalDeviceIds = |
| 290 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 291 | filterAPI1SystemCameraLocked(mNormalDeviceIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 294 | void CameraService::addStates(const String8 id) { |
| 295 | std::string cameraId(id.c_str()); |
| 296 | hardware::camera::common::V1_0::CameraResourceCost cost; |
| 297 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 298 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 299 | if (res != OK) { |
| 300 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); |
| 301 | return; |
| 302 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 303 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); |
| 304 | if (res != OK) { |
| 305 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); |
| 306 | return; |
| 307 | } |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 308 | std::set<String8> conflicting; |
| 309 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { |
| 310 | conflicting.emplace(String8(cost.conflictingDevices[i].c_str())); |
| 311 | } |
| 312 | |
| 313 | { |
| 314 | Mutex::Autolock lock(mCameraStatesLock); |
| 315 | mCameraStates.emplace(id, std::make_shared<CameraState>(id, cost.resourceCost, |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 316 | conflicting, deviceKind)); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 320 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 321 | mTorchStatusMap.add(id, TorchModeStatus::AVAILABLE_OFF); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 322 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 323 | broadcastTorchModeStatus(id, TorchModeStatus::AVAILABLE_OFF, deviceKind); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 324 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 325 | |
| 326 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 327 | logDeviceAdded(id, "Device added"); |
| 328 | } |
| 329 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 330 | void CameraService::removeStates(const String8 id) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 331 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 332 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 333 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 334 | mTorchStatusMap.removeItem(id); |
| 335 | } |
| 336 | |
| 337 | { |
| 338 | Mutex::Autolock lock(mCameraStatesLock); |
| 339 | mCameraStates.erase(id); |
| 340 | } |
| 341 | } |
| 342 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 343 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 344 | CameraDeviceStatus newHalStatus) { |
| 345 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, |
| 346 | id.string(), newHalStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 347 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 348 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 349 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 350 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 351 | |
| 352 | if (state == nullptr) { |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 353 | if (newStatus == StatusInternal::PRESENT) { |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 354 | ALOGI("%s: Unknown camera ID %s, a new camera is added", |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 355 | __FUNCTION__, id.string()); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 356 | |
| 357 | // First add as absent to make sure clients are notified below |
| 358 | addStates(id); |
| 359 | |
| 360 | updateStatus(newStatus, id); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 361 | } else { |
| 362 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string()); |
| 363 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 364 | return; |
| 365 | } |
| 366 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 367 | StatusInternal oldStatus = state->getStatus(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 368 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 369 | if (oldStatus == newStatus) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 370 | ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 371 | return; |
| 372 | } |
| 373 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 374 | if (newStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 375 | logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 376 | newStatus)); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 377 | |
| 378 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 379 | // to this device until the status changes |
| 380 | updateStatus(StatusInternal::NOT_PRESENT, id); |
| 381 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 382 | sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 383 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 384 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 385 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 386 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 387 | // Remove cached shim parameters |
| 388 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 389 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 390 | // Remove online as well as offline client from the list of active clients, |
| 391 | // if they are present |
| 392 | clientToDisconnectOnline = removeClientLocked(id); |
| 393 | clientToDisconnectOffline = removeClientLocked(kOfflineDevice + id); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 394 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 395 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 396 | disconnectClient(id, clientToDisconnectOnline); |
| 397 | disconnectClient(kOfflineDevice + id, clientToDisconnectOffline); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 398 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 399 | removeStates(id); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 400 | } else { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 401 | if (oldStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 402 | logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 403 | newStatus)); |
| 404 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 405 | updateStatus(newStatus, id); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 406 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 407 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 408 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 409 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 410 | const String8& physicalId, |
| 411 | CameraDeviceStatus newHalStatus) { |
| 412 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", |
| 413 | __FUNCTION__, id.string(), physicalId.string(), newHalStatus); |
| 414 | |
| 415 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 416 | |
| 417 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 418 | |
| 419 | if (state == nullptr) { |
| 420 | ALOGE("%s: Physical camera id %s status change on a non-present ID %s", |
| 421 | __FUNCTION__, id.string(), physicalId.string()); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | StatusInternal logicalCameraStatus = state->getStatus(); |
| 426 | if (logicalCameraStatus != StatusInternal::PRESENT && |
| 427 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 428 | ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d", |
| 429 | __FUNCTION__, physicalId.string(), newHalStatus, logicalCameraStatus); |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | bool updated = false; |
| 434 | if (newStatus == StatusInternal::PRESENT) { |
| 435 | updated = state->removeUnavailablePhysicalId(physicalId); |
| 436 | } else { |
| 437 | updated = state->addUnavailablePhysicalId(physicalId); |
| 438 | } |
| 439 | |
| 440 | if (updated) { |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 441 | String8 idCombo = id + " : " + physicalId; |
| 442 | if (newStatus == StatusInternal::PRESENT) { |
| 443 | logDeviceAdded(idCombo, |
| 444 | String8::format("Device status changed to %d", newStatus)); |
| 445 | } else { |
| 446 | logDeviceRemoved(idCombo, |
| 447 | String8::format("Device status changed to %d", newStatus)); |
| 448 | } |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 449 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 450 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 451 | if (getSystemCameraKind(id, &deviceKind) != OK) { |
| 452 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.string()); |
| 453 | return; |
| 454 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 455 | String16 id16(id), physicalId16(physicalId); |
| 456 | Mutex::Autolock lock(mStatusListenerLock); |
| 457 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 458 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 459 | listener->getListenerPid(), listener->getListenerUid())) { |
| 460 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 461 | id.c_str()); |
| 462 | continue; |
| 463 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 464 | listener->getListener()->onPhysicalCameraStatusChanged(mapToInterface(newStatus), |
| 465 | id16, physicalId16); |
| 466 | } |
| 467 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 470 | void CameraService::disconnectClient(const String8& id, sp<BasicClient> clientToDisconnect) { |
| 471 | if (clientToDisconnect.get() != nullptr) { |
| 472 | ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL", |
| 473 | __FUNCTION__, id.string()); |
| 474 | // Notify the client of disconnection |
| 475 | clientToDisconnect->notifyError( |
| 476 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 477 | CaptureResultExtras{}); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 478 | clientToDisconnect->disconnect(); |
| 479 | } |
| 480 | } |
| 481 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 482 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 483 | TorchModeStatus newStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 484 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 485 | status_t res = getSystemCameraKind(cameraId, &systemCameraKind); |
| 486 | if (res != OK) { |
| 487 | ALOGE("%s: Could not get system camera kind for camera id %s", __FUNCTION__, |
| 488 | cameraId.string()); |
| 489 | return; |
| 490 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 491 | Mutex::Autolock al(mTorchStatusMutex); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 492 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 495 | void CameraService::onTorchStatusChangedLocked(const String8& cameraId, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 496 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 497 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
| 498 | __FUNCTION__, cameraId.string(), newStatus); |
| 499 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 500 | TorchModeStatus status; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 501 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 502 | if (res) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 503 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", |
| 504 | __FUNCTION__, cameraId.string(), strerror(-res), res); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 505 | return; |
| 506 | } |
| 507 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 508 | return; |
| 509 | } |
| 510 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 511 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 512 | if (res) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 513 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, |
| 514 | (uint32_t)newStatus, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 515 | return; |
| 516 | } |
| 517 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 518 | { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 519 | // Update battery life logging for flashlight |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 520 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 521 | auto iter = mTorchUidMap.find(cameraId); |
| 522 | if (iter != mTorchUidMap.end()) { |
| 523 | int oldUid = iter->second.second; |
| 524 | int newUid = iter->second.first; |
| 525 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 526 | if (oldUid != newUid) { |
| 527 | // If the UID has changed, log the status and update current UID in mTorchUidMap |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 528 | if (status == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 529 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 530 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 531 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 532 | notifier.noteFlashlightOn(cameraId, newUid); |
| 533 | } |
| 534 | iter->second.second = newUid; |
| 535 | } else { |
| 536 | // If the UID has not changed, log the status |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 537 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 538 | notifier.noteFlashlightOn(cameraId, oldUid); |
| 539 | } else { |
| 540 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 545 | broadcastTorchModeStatus(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 548 | static bool hasPermissionsForSystemCamera(int callingPid, int callingUid) { |
| 549 | return checkPermission(sSystemCameraPermission, callingPid, callingUid) && |
| 550 | checkPermission(sCameraPermission, callingPid, callingUid); |
| 551 | } |
| 552 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 553 | Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 554 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 555 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 556 | bool hasSystemCameraPermissions = |
| 557 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), |
| 558 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 559 | switch (type) { |
| 560 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 561 | if (hasSystemCameraPermissions) { |
| 562 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); |
| 563 | } else { |
| 564 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); |
| 565 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 566 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 567 | case CAMERA_TYPE_ALL: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 568 | if (hasSystemCameraPermissions) { |
| 569 | *numCameras = mNumberOfCameras; |
| 570 | } else { |
| 571 | *numCameras = mNumberOfCamerasWithoutSystemCamera; |
| 572 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 573 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 574 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 575 | ALOGW("%s: Unknown camera type %d", |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 576 | __FUNCTION__, type); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 577 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 578 | "Unknown camera type %d", type); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 579 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 580 | return Status::ok(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 583 | Status CameraService::getCameraInfo(int cameraId, |
| 584 | CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 585 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 586 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 587 | std::string cameraIdStr = cameraIdIntToStrLocked(cameraId); |
| 588 | if (shouldRejectSystemCameraConnection(String8(cameraIdStr.c_str()))) { |
| 589 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 590 | "characteristics for system only device %s: ", cameraIdStr.c_str()); |
| 591 | } |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 592 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 593 | if (!mInitialized) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 594 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 595 | "Camera subsystem is not available"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 596 | } |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 597 | bool hasSystemCameraPermissions = |
| 598 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), |
| 599 | CameraThreadState::getCallingUid()); |
| 600 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; |
| 601 | if (hasSystemCameraPermissions) { |
| 602 | cameraIdBound = mNumberOfCameras; |
| 603 | } |
| 604 | if (cameraId < 0 || cameraId >= cameraIdBound) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 605 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 606 | "CameraId is not valid"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 609 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 610 | status_t err = mCameraProviderManager->getCameraInfo( |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 611 | cameraIdStr.c_str(), cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 612 | if (err != OK) { |
| 613 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 614 | "Error retrieving camera info from device %d: %s (%d)", cameraId, |
| 615 | strerror(-err), err); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 616 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 617 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 618 | return ret; |
| 619 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 620 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 621 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 622 | const std::vector<std::string> *deviceIds = &mNormalDeviceIdsWithoutSystemCamera; |
| 623 | auto callingPid = CameraThreadState::getCallingPid(); |
| 624 | auto callingUid = CameraThreadState::getCallingUid(); |
| 625 | if (checkPermission(sSystemCameraPermission, callingPid, callingUid) || |
| 626 | getpid() == callingPid) { |
| 627 | deviceIds = &mNormalDeviceIds; |
| 628 | } |
| 629 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(deviceIds->size())) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 630 | ALOGE("%s: input id %d invalid: valid range (0, %zu)", |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 631 | __FUNCTION__, cameraIdInt, deviceIds->size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 632 | return std::string{}; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 633 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 634 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 635 | return (*deviceIds)[cameraIdInt]; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | String8 CameraService::cameraIdIntToStr(int cameraIdInt) { |
| 639 | Mutex::Autolock lock(mServiceLock); |
| 640 | return String8(cameraIdIntToStrLocked(cameraIdInt).c_str()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | Status CameraService::getCameraCharacteristics(const String16& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 644 | CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 645 | ATRACE_CALL(); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 646 | if (!cameraInfo) { |
| 647 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 648 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 651 | if (!mInitialized) { |
| 652 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 653 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 654 | "Camera subsystem is not available");; |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 657 | if (shouldRejectSystemCameraConnection(String8(cameraId))) { |
| 658 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 659 | "characteristics for system only device %s: ", String8(cameraId).string()); |
| 660 | } |
| 661 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 662 | Status ret{}; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 663 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 664 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
| 665 | String8(cameraId).string(), cameraInfo); |
| 666 | if (res != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 667 | if (res == NAME_NOT_FOUND) { |
| 668 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " |
| 669 | "characteristics for unknown device %s: %s (%d)", String8(cameraId).string(), |
| 670 | strerror(-res), res); |
| 671 | } else { |
| 672 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " |
| 673 | "characteristics for device %s: %s (%d)", String8(cameraId).string(), |
| 674 | strerror(-res), res); |
| 675 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 676 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 677 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 678 | if (getSystemCameraKind(String8(cameraId), &deviceKind) != OK) { |
| 679 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, String8(cameraId).string()); |
| 680 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera kind " |
| 681 | "for device %s", String8(cameraId).string()); |
| 682 | } |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 683 | int callingPid = CameraThreadState::getCallingPid(); |
| 684 | int callingUid = CameraThreadState::getCallingUid(); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 685 | std::vector<int32_t> tagsRemoved; |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 686 | // If it's not calling from cameraserver, check the permission only if |
| 687 | // android.permission.CAMERA is required. If android.permission.SYSTEM_CAMERA was needed, |
| 688 | // it would've already been checked in shouldRejectSystemCameraConnection. |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 689 | if ((callingPid != getpid()) && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 690 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 691 | !checkPermission(sCameraPermission, callingPid, callingUid)) { |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 692 | res = cameraInfo->removePermissionEntries( |
| 693 | mCameraProviderManager->getProviderTagIdLocked(String8(cameraId).string()), |
| 694 | &tagsRemoved); |
| 695 | if (res != OK) { |
| 696 | cameraInfo->clear(); |
| 697 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to remove camera" |
| 698 | " characteristics needing camera permission for device %s: %s (%d)", |
| 699 | String8(cameraId).string(), strerror(-res), res); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | if (!tagsRemoved.empty()) { |
| 704 | res = cameraInfo->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, |
| 705 | tagsRemoved.data(), tagsRemoved.size()); |
| 706 | if (res != OK) { |
| 707 | cameraInfo->clear(); |
| 708 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to insert camera " |
| 709 | "keys needing permission for device %s: %s (%d)", String8(cameraId).string(), |
| 710 | strerror(-res), res); |
| 711 | } |
| 712 | } |
| 713 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 714 | return ret; |
| 715 | } |
| 716 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 717 | String8 CameraService::getFormattedCurrentTime() { |
| 718 | time_t now = time(nullptr); |
| 719 | char formattedTime[64]; |
| 720 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
| 721 | return String8(formattedTime); |
| 722 | } |
| 723 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 724 | Status CameraService::getCameraVendorTagDescriptor( |
| 725 | /*out*/ |
| 726 | hardware::camera2::params::VendorTagDescriptor* desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 727 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 728 | if (!mInitialized) { |
| 729 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 730 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 731 | } |
Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 732 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 733 | if (globalDescriptor != nullptr) { |
| 734 | *desc = *(globalDescriptor.get()); |
| 735 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 736 | return Status::ok(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 739 | Status CameraService::getCameraVendorTagCache( |
| 740 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { |
| 741 | ATRACE_CALL(); |
| 742 | if (!mInitialized) { |
| 743 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 744 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 745 | "Camera subsystem not available"); |
| 746 | } |
| 747 | sp<VendorTagDescriptorCache> globalCache = |
| 748 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 749 | if (globalCache != nullptr) { |
| 750 | *cache = *(globalCache.get()); |
| 751 | } |
| 752 | return Status::ok(); |
| 753 | } |
| 754 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 755 | void CameraService::clearCachedVariables() { |
| 756 | BasicClient::BasicClient::sCameraService = nullptr; |
| 757 | } |
| 758 | |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 759 | int CameraService::getDeviceVersion(const String8& cameraId, int* facing, int* orientation) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 760 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 761 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 762 | int deviceVersion = 0; |
| 763 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 764 | status_t res; |
| 765 | hardware::hidl_version maxVersion{0,0}; |
| 766 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(), |
| 767 | &maxVersion); |
| 768 | if (res != OK) return -1; |
| 769 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 770 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 771 | hardware::CameraInfo info; |
| 772 | if (facing) { |
| 773 | res = mCameraProviderManager->getCameraInfo(cameraId.string(), &info); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 774 | if (res != OK) return -1; |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 775 | *facing = info.facing; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 776 | if (orientation) { |
| 777 | *orientation = info.orientation; |
| 778 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 779 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 780 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 781 | return deviceVersion; |
| 782 | } |
| 783 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 784 | Status CameraService::filterGetInfoErrorCode(status_t err) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 785 | switch(err) { |
| 786 | case NO_ERROR: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 787 | return Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 788 | case BAD_VALUE: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 789 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 790 | "CameraId is not valid for HAL module"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 791 | case NO_INIT: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 792 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 793 | "Camera device not available"); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 794 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 795 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 796 | "Camera HAL encountered error %d: %s", |
| 797 | err, strerror(-err)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 798 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 799 | } |
| 800 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 801 | Status CameraService::makeClient(const sp<CameraService>& cameraService, |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 802 | const sp<IInterface>& cameraCb, const String16& packageName, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 803 | const std::optional<String16>& featureId, const String8& cameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 804 | int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid, |
| 805 | int servicePid, int deviceVersion, apiLevel effectiveApiLevel, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 806 | /*out*/sp<BasicClient>* client) { |
| 807 | |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 808 | // Create CameraClient based on device version reported by the HAL. |
| 809 | switch(deviceVersion) { |
| 810 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 811 | ALOGE("Camera using old HAL version: %d", deviceVersion); |
| 812 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, |
| 813 | "Camera device \"%s\" HAL version %d no longer supported", |
| 814 | cameraId.string(), deviceVersion); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 815 | break; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 816 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 817 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 818 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 819 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 820 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 821 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 822 | case CAMERA_DEVICE_API_VERSION_3_6: |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 823 | case CAMERA_DEVICE_API_VERSION_3_7: |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 824 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 825 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 826 | *client = new Camera2Client(cameraService, tmp, packageName, featureId, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 827 | cameraId, api1CameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 828 | facing, sensorOrientation, clientPid, clientUid, |
Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 829 | servicePid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 830 | } else { // Camera2 API route |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 831 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = |
| 832 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 833 | *client = new CameraDeviceClient(cameraService, tmp, packageName, featureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 834 | cameraId, facing, sensorOrientation, clientPid, clientUid, servicePid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 835 | } |
| 836 | break; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 837 | default: |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 838 | // Should not be reachable |
| 839 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 840 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 841 | "Camera device \"%s\" has unknown HAL version %d", |
| 842 | cameraId.string(), deviceVersion); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 843 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 844 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 845 | } |
| 846 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 847 | String8 CameraService::toString(std::set<userid_t> intSet) { |
| 848 | String8 s(""); |
| 849 | bool first = true; |
| 850 | for (userid_t i : intSet) { |
| 851 | if (first) { |
| 852 | s.appendFormat("%d", i); |
| 853 | first = false; |
| 854 | } else { |
| 855 | s.appendFormat(", %d", i); |
| 856 | } |
| 857 | } |
| 858 | return s; |
| 859 | } |
| 860 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 861 | int32_t CameraService::mapToInterface(TorchModeStatus status) { |
| 862 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 863 | switch (status) { |
| 864 | case TorchModeStatus::NOT_AVAILABLE: |
| 865 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 866 | break; |
| 867 | case TorchModeStatus::AVAILABLE_OFF: |
| 868 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 869 | break; |
| 870 | case TorchModeStatus::AVAILABLE_ON: |
| 871 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
| 872 | break; |
| 873 | default: |
| 874 | ALOGW("Unknown new flash status: %d", status); |
| 875 | } |
| 876 | return serviceStatus; |
| 877 | } |
| 878 | |
| 879 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { |
| 880 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; |
| 881 | switch (status) { |
| 882 | case CameraDeviceStatus::NOT_PRESENT: |
| 883 | serviceStatus = StatusInternal::NOT_PRESENT; |
| 884 | break; |
| 885 | case CameraDeviceStatus::PRESENT: |
| 886 | serviceStatus = StatusInternal::PRESENT; |
| 887 | break; |
| 888 | case CameraDeviceStatus::ENUMERATING: |
| 889 | serviceStatus = StatusInternal::ENUMERATING; |
| 890 | break; |
| 891 | default: |
| 892 | ALOGW("Unknown new HAL device status: %d", status); |
| 893 | } |
| 894 | return serviceStatus; |
| 895 | } |
| 896 | |
| 897 | int32_t CameraService::mapToInterface(StatusInternal status) { |
| 898 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 899 | switch (status) { |
| 900 | case StatusInternal::NOT_PRESENT: |
| 901 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 902 | break; |
| 903 | case StatusInternal::PRESENT: |
| 904 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; |
| 905 | break; |
| 906 | case StatusInternal::ENUMERATING: |
| 907 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; |
| 908 | break; |
| 909 | case StatusInternal::NOT_AVAILABLE: |
| 910 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; |
| 911 | break; |
| 912 | case StatusInternal::UNKNOWN: |
| 913 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; |
| 914 | break; |
| 915 | default: |
| 916 | ALOGW("Unknown new internal device status: %d", status); |
| 917 | } |
| 918 | return serviceStatus; |
| 919 | } |
| 920 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 921 | Status CameraService::initializeShimMetadata(int cameraId) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 922 | int uid = CameraThreadState::getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 923 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 924 | String16 internalPackageName("cameraserver"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 925 | String8 id = String8::format("%d", cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 926 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 927 | sp<Client> tmp = nullptr; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 928 | if (!(ret = connectHelper<ICameraClient,Client>( |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 929 | sp<ICameraClient>{nullptr}, id, cameraId, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 930 | internalPackageName, {}, uid, USE_CALLING_PID, |
Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 931 | API_1, /*shimUpdateOnly*/ true, /*out*/ tmp) |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 932 | ).isOk()) { |
| 933 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string()); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 934 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 935 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 938 | Status CameraService::getLegacyParametersLazy(int cameraId, |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 939 | /*out*/ |
| 940 | CameraParameters* parameters) { |
| 941 | |
| 942 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 943 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 944 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 945 | |
| 946 | if (parameters == NULL) { |
| 947 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 948 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 951 | String8 id = String8::format("%d", cameraId); |
| 952 | |
| 953 | // Check if we already have parameters |
| 954 | { |
| 955 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 956 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 957 | auto cameraState = getCameraState(id); |
| 958 | if (cameraState == nullptr) { |
| 959 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 960 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 961 | "Invalid camera ID: %s", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 962 | } |
| 963 | CameraParameters p = cameraState->getShimParams(); |
| 964 | if (!p.isEmpty()) { |
| 965 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 966 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 970 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 971 | ret = initializeShimMetadata(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 972 | CameraThreadState::restoreCallingIdentity(token); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 973 | if (!ret.isOk()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 974 | // Error already logged by callee |
| 975 | return ret; |
| 976 | } |
| 977 | |
| 978 | // Check for parameters again |
| 979 | { |
| 980 | // Scope for service lock |
| 981 | Mutex::Autolock lock(mServiceLock); |
| 982 | auto cameraState = getCameraState(id); |
| 983 | if (cameraState == nullptr) { |
| 984 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 985 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 986 | "Invalid camera ID: %s", id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 987 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 988 | CameraParameters p = cameraState->getShimParams(); |
| 989 | if (!p.isEmpty()) { |
| 990 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 991 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 995 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 996 | __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 997 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1000 | // Can camera service trust the caller based on the calling UID? |
| 1001 | static bool isTrustedCallingUid(uid_t uid) { |
| 1002 | switch (uid) { |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1003 | case AID_MEDIA: // mediaserver |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1004 | case AID_CAMERASERVER: // cameraserver |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1005 | case AID_RADIO: // telephony |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1006 | return true; |
| 1007 | default: |
| 1008 | return false; |
| 1009 | } |
| 1010 | } |
| 1011 | |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 1012 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { |
| 1013 | PermissionController pc; |
| 1014 | uid = pc.getPackageUid(packageName, 0); |
| 1015 | if (uid <= 0) { |
| 1016 | ALOGE("Unknown package: '%s'", String8(packageName).string()); |
| 1017 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).string()); |
| 1018 | return BAD_VALUE; |
| 1019 | } |
| 1020 | |
| 1021 | if (userId < 0) { |
| 1022 | ALOGE("Invalid user: %d", userId); |
| 1023 | dprintf(err, "Invalid user: %d\n", userId); |
| 1024 | return BAD_VALUE; |
| 1025 | } |
| 1026 | |
| 1027 | uid = multiuser_get_uid(userId, uid); |
| 1028 | return NO_ERROR; |
| 1029 | } |
| 1030 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1031 | Status CameraService::validateConnectLocked(const String8& cameraId, |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1032 | const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, |
| 1033 | /*out*/int& originalClientPid) const { |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1034 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1035 | #ifdef __BRILLO__ |
| 1036 | UNUSED(clientName8); |
| 1037 | UNUSED(clientUid); |
| 1038 | UNUSED(clientPid); |
| 1039 | UNUSED(originalClientPid); |
| 1040 | #else |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1041 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, |
| 1042 | originalClientPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1043 | if (!allowed.isOk()) { |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1044 | return allowed; |
| 1045 | } |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1046 | #endif // __BRILLO__ |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1047 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1048 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1049 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1050 | if (!mInitialized) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1051 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 1052 | callingPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1053 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1054 | "No camera HAL module available to open camera device \"%s\"", cameraId.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1057 | if (getCameraState(cameraId) == nullptr) { |
| 1058 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1059 | cameraId.string()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1060 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1061 | "No camera device with ID \"%s\" available", cameraId.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1064 | status_t err = checkIfDeviceIsUsable(cameraId); |
| 1065 | if (err != NO_ERROR) { |
| 1066 | switch(err) { |
| 1067 | case -ENODEV: |
| 1068 | case -EBUSY: |
| 1069 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1070 | "No camera device with ID \"%s\" currently available", cameraId.string()); |
| 1071 | default: |
| 1072 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1073 | "Unknown error connecting to ID \"%s\"", cameraId.string()); |
| 1074 | } |
| 1075 | } |
| 1076 | return Status::ok(); |
Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1079 | Status CameraService::validateClientPermissionsLocked(const String8& cameraId, |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1080 | const String8& clientName8, int& clientUid, int& clientPid, |
| 1081 | /*out*/int& originalClientPid) const { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1082 | int callingPid = CameraThreadState::getCallingPid(); |
| 1083 | int callingUid = CameraThreadState::getCallingUid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1084 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1085 | // Check if we can trust clientUid |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1086 | if (clientUid == USE_CALLING_UID) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1087 | clientUid = callingUid; |
| 1088 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1089 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1090 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1091 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1092 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1093 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1094 | callingPid, callingUid, cameraId.string(), |
| 1095 | clientName8.string(), clientUid, clientPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1098 | // Check if we can trust clientPid |
| 1099 | if (clientPid == USE_CALLING_PID) { |
| 1100 | clientPid = callingPid; |
| 1101 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1102 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1103 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1104 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1105 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1106 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1107 | callingPid, callingUid, cameraId.string(), |
| 1108 | clientName8.string(), clientUid, clientPid); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1111 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1112 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", |
| 1113 | cameraId.c_str()); |
| 1114 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, "No camera device with ID \"%s\" is" |
| 1115 | "available", cameraId.string()); |
| 1116 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1117 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1118 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 1119 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 1120 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "No camera device with ID \"%s\"" |
| 1121 | "found while trying to query device kind", cameraId.string()); |
| 1122 | |
| 1123 | } |
| 1124 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1125 | // If it's not calling from cameraserver, check the permission if the |
| 1126 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for |
| 1127 | // android.permission.SYSTEM_CAMERA for system only camera devices). |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1128 | if (callingPid != getpid() && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1129 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1130 | !checkPermission(sCameraPermission, clientPid, clientUid)) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1131 | ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1132 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1133 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission", |
| 1134 | clientName8.string(), clientUid, clientPid, cameraId.string()); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1137 | // Make sure the UID is in an active state to use the camera |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 1138 | if (!mUidPolicy->isUidActive(callingUid, String16(clientName8))) { |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1139 | int32_t procState = mUidPolicy->getProcState(callingUid); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1140 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", |
| 1141 | clientPid, clientUid); |
| 1142 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1143 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" |
| 1144 | "calling UID %d proc state %" PRId32 ")", |
| 1145 | clientName8.string(), clientUid, clientPid, cameraId.string(), |
| 1146 | callingUid, procState); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1149 | // If sensor privacy is enabled then prevent access to the camera |
| 1150 | if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
| 1151 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); |
| 1152 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1153 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" when sensor privacy " |
| 1154 | "is enabled", clientName8.string(), clientUid, clientPid, cameraId.string()); |
| 1155 | } |
| 1156 | |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1157 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's |
| 1158 | // connected to camera service directly. |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1159 | originalClientPid = clientPid; |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1160 | clientPid = callingPid; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1161 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1162 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1163 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1164 | // Only allow clients who are being used by the current foreground device user, unless calling |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1165 | // from our own process OR the caller is using the cameraserver's HIDL interface. |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 1166 | if (getCurrentServingCall() != BinderCallType::HWBINDER && callingPid != getpid() && |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1167 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1168 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 1169 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
| 1170 | toString(mAllowedUsers).string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1171 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1172 | "Callers from device user %d are not currently allowed to connect to camera \"%s\"", |
| 1173 | clientUserId, cameraId.string()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1176 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const { |
| 1180 | auto cameraState = getCameraState(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1181 | int callingPid = CameraThreadState::getCallingPid(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1182 | if (cameraState == nullptr) { |
| 1183 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1184 | cameraId.string()); |
| 1185 | return -ENODEV; |
| 1186 | } |
| 1187 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1188 | StatusInternal currentStatus = cameraState->getStatus(); |
| 1189 | if (currentStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1190 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
| 1191 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1192 | return -ENODEV; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1193 | } else if (currentStatus == StatusInternal::ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1194 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
| 1195 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1196 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1197 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1198 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1199 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1202 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
| 1203 | const CameraService::DescriptorPtr& desc) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1204 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1205 | // Make a descriptor for the incoming client |
| 1206 | auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc); |
| 1207 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 1208 | |
| 1209 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
| 1210 | String8(client->getPackageName())); |
| 1211 | |
| 1212 | if (evicted.size() > 0) { |
| 1213 | // This should never happen - clients should already have been removed in disconnect |
| 1214 | for (auto& i : evicted) { |
| 1215 | ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect", |
| 1216 | __FUNCTION__, i->getKey().string()); |
| 1217 | } |
| 1218 | |
| 1219 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 1220 | __FUNCTION__); |
| 1221 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1222 | |
| 1223 | // And register a death notification for the client callback. Do |
| 1224 | // this last to avoid Binder policy where a nested Binder |
| 1225 | // transaction might be pre-empted to service the client death |
| 1226 | // notification if the client process dies before linkToDeath is |
| 1227 | // invoked. |
| 1228 | sp<IBinder> remoteCallback = client->getRemote(); |
| 1229 | if (remoteCallback != nullptr) { |
| 1230 | remoteCallback->linkToDeath(this); |
| 1231 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid, |
| 1235 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName, |
| 1236 | /*out*/ |
| 1237 | sp<BasicClient>* client, |
| 1238 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1239 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1240 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1241 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1242 | DescriptorPtr clientDescriptor; |
| 1243 | { |
| 1244 | if (effectiveApiLevel == API_1) { |
| 1245 | // If we are using API1, any existing client for this camera ID with the same remote |
| 1246 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 1247 | |
| 1248 | auto current = mActiveClientManager.get(cameraId); |
| 1249 | if (current != nullptr) { |
| 1250 | auto clientSp = current->getValue(); |
| 1251 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1252 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
| 1253 | ALOGW("CameraService connect called from same client, but with a different" |
| 1254 | " API level, evicting prior client..."); |
| 1255 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1256 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1257 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1258 | *client = clientSp; |
| 1259 | return NO_ERROR; |
| 1260 | } |
| 1261 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1262 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1263 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 1264 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1265 | // Get current active client PIDs |
| 1266 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 1267 | ownerPids.push_back(clientPid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1268 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1269 | std::vector<int> priorityScores(ownerPids.size()); |
| 1270 | std::vector<int> states(ownerPids.size()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1271 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1272 | // Get priority scores of all active PIDs |
| 1273 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids( |
| 1274 | ownerPids.size(), &ownerPids[0], /*out*/&states[0], |
| 1275 | /*out*/&priorityScores[0]); |
| 1276 | if (err != OK) { |
| 1277 | ALOGE("%s: Priority score query failed: %d", |
| 1278 | __FUNCTION__, err); |
| 1279 | return err; |
| 1280 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1281 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1282 | // Update all active clients' priorities |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1283 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1284 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1285 | pidToPriorityMap.emplace(ownerPids[i], |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 1286 | resource_policy::ClientPriority(priorityScores[i], states[i], |
| 1287 | /* isVendorClient won't get copied over*/ false)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1288 | } |
| 1289 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1290 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1291 | // Get state for the given cameraId |
| 1292 | auto state = getCameraState(cameraId); |
| 1293 | if (state == nullptr) { |
| 1294 | ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", |
| 1295 | clientPid, cameraId.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1296 | // Should never get here because validateConnectLocked should have errored out |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1297 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1298 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1299 | |
| 1300 | // Make descriptor for incoming client |
Shuzhen Wang | 2db86ff | 2018-04-25 01:11:17 +0000 | [diff] [blame] | 1301 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1302 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 1303 | state->getConflicting(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1304 | priorityScores[priorityScores.size() - 1], |
| 1305 | clientPid, |
| 1306 | states[states.size() - 1]); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1307 | |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1308 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); |
| 1309 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1310 | // Find clients that would be evicted |
| 1311 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 1312 | |
| 1313 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 1314 | // background, so we cannot do evictions |
| 1315 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 1316 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 1317 | " priority).", clientPid); |
| 1318 | |
| 1319 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1320 | String8 curTime = getFormattedCurrentTime(); |
| 1321 | auto incompatibleClients = |
| 1322 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 1323 | |
| 1324 | String8 msg = String8::format("%s : DENIED connect device %s client for package %s " |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1325 | "(PID %d, score %d state %d) due to eviction policy", curTime.string(), |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1326 | cameraId.string(), packageName.string(), clientPid, |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1327 | clientPriority.getScore(), clientPriority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1328 | |
| 1329 | for (auto& i : incompatibleClients) { |
| 1330 | msg.appendFormat("\n - Blocked by existing device %s client for package %s" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1331 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", |
| 1332 | i->getKey().string(), |
| 1333 | String8{i->getValue()->getPackageName()}.string(), |
| 1334 | i->getOwnerId(), i->getPriority().getScore(), |
| 1335 | i->getPriority().getState()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1336 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1337 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(), |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1338 | String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1339 | i->getPriority().getScore(), i->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1343 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1344 | mEventLog.add(msg); |
| 1345 | |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1346 | auto current = mActiveClientManager.get(cameraId); |
| 1347 | if (current != nullptr) { |
| 1348 | return -EBUSY; // CAMERA_IN_USE |
| 1349 | } else { |
| 1350 | return -EUSERS; // MAX_CAMERAS_IN_USE |
| 1351 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | for (auto& i : evicted) { |
| 1355 | sp<BasicClient> clientSp = i->getValue(); |
| 1356 | if (clientSp.get() == nullptr) { |
| 1357 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 1358 | |
| 1359 | // TODO: Remove this |
| 1360 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 1361 | __FUNCTION__); |
| 1362 | mActiveClientManager.remove(i); |
| 1363 | continue; |
| 1364 | } |
| 1365 | |
| 1366 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
| 1367 | i->getKey().string()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1368 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1369 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1370 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1371 | logEvent(String8::format("EVICT device %s client held by package %s (PID" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1372 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" |
| 1373 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1374 | i->getKey().string(), String8{clientSp->getPackageName()}.string(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1375 | i->getOwnerId(), i->getPriority().getScore(), |
| 1376 | i->getPriority().getState(), cameraId.string(), |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1377 | packageName.string(), clientPid, clientPriority.getScore(), |
| 1378 | clientPriority.getState())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1379 | |
| 1380 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1381 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1382 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1383 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1386 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 1387 | // other clients from connecting in mServiceLockWrapper if held |
| 1388 | mServiceLock.unlock(); |
| 1389 | |
| 1390 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1391 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1392 | |
| 1393 | // Destroy evicted clients |
| 1394 | for (auto& i : evictedClients) { |
| 1395 | // Disconnect is blocking, and should only have returned when HAL has cleaned up |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1396 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1397 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1398 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1399 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1400 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1401 | for (const auto& i : evictedClients) { |
| 1402 | ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")", |
| 1403 | __FUNCTION__, i->getKey().string(), i->getOwnerId()); |
| 1404 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 1405 | if (ret == TIMED_OUT) { |
| 1406 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
| 1407 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), |
| 1408 | mActiveClientManager.toString().string()); |
| 1409 | return -EBUSY; |
| 1410 | } |
| 1411 | if (ret != NO_ERROR) { |
| 1412 | ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), " |
| 1413 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret), |
| 1414 | ret, mActiveClientManager.toString().string()); |
| 1415 | return ret; |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | evictedClients.clear(); |
| 1420 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1421 | // Once clients have been disconnected, relock |
| 1422 | mServiceLock.lock(); |
| 1423 | |
| 1424 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 1425 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 1426 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1429 | *partial = clientDescriptor; |
| 1430 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1433 | Status CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1434 | const sp<ICameraClient>& cameraClient, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1435 | int api1CameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1436 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1437 | int clientUid, |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1438 | int clientPid, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1439 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1440 | sp<ICamera>* device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1441 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1442 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1443 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1444 | |
| 1445 | String8 id = cameraIdIntToStr(api1CameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1446 | sp<Client> client = nullptr; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1447 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1448 | clientPackageName, {}, clientUid, clientPid, API_1, |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 1449 | /*shimUpdateOnly*/ false, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1450 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1451 | if(!ret.isOk()) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1452 | logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageName), |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1453 | ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1454 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1457 | *device = client; |
| 1458 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1461 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, |
| 1462 | bool isVendorListener, int clientPid, int clientUid) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1463 | // If the client is not a vendor client, don't add listener if |
| 1464 | // a) the camera is a publicly hidden secure camera OR |
| 1465 | // b) the camera is a system only camera and the client doesn't |
| 1466 | // have android.permission.SYSTEM_CAMERA permissions. |
| 1467 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || |
| 1468 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
| 1469 | !hasPermissionsForSystemCamera(clientPid, clientUid)))) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 1470 | return true; |
| 1471 | } |
| 1472 | return false; |
| 1473 | } |
| 1474 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1475 | bool CameraService::shouldRejectSystemCameraConnection(const String8& cameraId) const { |
| 1476 | // Rules for rejection: |
| 1477 | // 1) If cameraserver tries to access this camera device, accept the |
| 1478 | // connection. |
| 1479 | // 2) The camera device is a publicly hidden secure camera device AND some |
| 1480 | // component is trying to access it on a non-hwbinder thread (generally a non HAL client), |
| 1481 | // reject it. |
| 1482 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY |
| 1483 | // and the serving thread is a non hwbinder thread, the client must have |
| 1484 | // android.permission.SYSTEM_CAMERA permissions to connect. |
| 1485 | |
| 1486 | int cPid = CameraThreadState::getCallingPid(); |
| 1487 | int cUid = CameraThreadState::getCallingUid(); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1488 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 1489 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1490 | // This isn't a known camera ID, so it's not a system camera |
| 1491 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); |
| 1492 | return false; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1493 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1494 | |
| 1495 | // (1) Cameraserver trying to connect, accept. |
| 1496 | if (CameraThreadState::getCallingPid() == getpid()) { |
| 1497 | return false; |
| 1498 | } |
| 1499 | // (2) |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 1500 | if (getCurrentServingCall() != BinderCallType::HWBINDER && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1501 | systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { |
| 1502 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); |
| 1503 | return true; |
| 1504 | } |
| 1505 | // (3) Here we only check for permissions if it is a system only camera device. This is since |
| 1506 | // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some |
| 1507 | // characteristics) even if clients don't have android.permission.CAMERA. We do not want the |
| 1508 | // same behavior for system camera devices. |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 1509 | if (getCurrentServingCall() != BinderCallType::HWBINDER && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1510 | systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
| 1511 | !hasPermissionsForSystemCamera(cPid, cUid)) { |
| 1512 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", |
| 1513 | cameraId.c_str()); |
| 1514 | return true; |
| 1515 | } |
| 1516 | |
| 1517 | return false; |
| 1518 | } |
| 1519 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1520 | Status CameraService::connectDevice( |
| 1521 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1522 | const String16& cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1523 | const String16& clientPackageName, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1524 | const std::optional<String16>& clientFeatureId, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1525 | int clientUid, |
| 1526 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1527 | sp<hardware::camera2::ICameraDeviceUser>* device) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1528 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1529 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1530 | Status ret = Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1531 | String8 id = String8(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1532 | sp<CameraDeviceClient> client = nullptr; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1533 | String16 clientPackageNameAdj = clientPackageName; |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 1534 | |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 1535 | if (getCurrentServingCall() == BinderCallType::HWBINDER) { |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1536 | std::string vendorClient = |
| 1537 | StringPrintf("vendor.client.pid<%d>", CameraThreadState::getCallingPid()); |
| 1538 | clientPackageNameAdj = String16(vendorClient.c_str()); |
| 1539 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1540 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1541 | /*api1CameraId*/-1, clientPackageNameAdj, clientFeatureId, |
Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 1542 | clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, /*out*/client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1543 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1544 | if(!ret.isOk()) { |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1545 | logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageNameAdj), |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1546 | ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1547 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1548 | } |
| 1549 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1550 | *device = client; |
| 1551 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1552 | } |
| 1553 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1554 | template<class CALLBACK, class CLIENT> |
| 1555 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1556 | int api1CameraId, const String16& clientPackageName, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1557 | const std::optional<String16>& clientFeatureId, int clientUid, int clientPid, |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 1558 | apiLevel effectiveApiLevel, bool shimUpdateOnly, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1559 | /*out*/sp<CLIENT>& device) { |
| 1560 | binder::Status ret = binder::Status::ok(); |
| 1561 | |
| 1562 | String8 clientName8(clientPackageName); |
| 1563 | |
| 1564 | int originalClientPid = 0; |
| 1565 | |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1566 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1567 | "Camera API version %d", clientPid, clientName8.string(), cameraId.string(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1568 | static_cast<int>(effectiveApiLevel)); |
| 1569 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1570 | nsecs_t openTimeNs = systemTime(); |
| 1571 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1572 | sp<CLIENT> client = nullptr; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1573 | int facing = -1; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1574 | int orientation = 0; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1575 | bool isNdk = (clientPackageName.size() == 0); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1576 | { |
| 1577 | // Acquire mServiceLock and prevent other clients from connecting |
| 1578 | std::unique_ptr<AutoConditionLock> lock = |
| 1579 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 1580 | |
| 1581 | if (lock == nullptr) { |
| 1582 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." |
| 1583 | , clientPid); |
| 1584 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1585 | "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting", |
| 1586 | cameraId.string(), clientName8.string(), clientPid); |
| 1587 | } |
| 1588 | |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 1589 | // Enforce client permissions and do basic validity checks |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1590 | if(!(ret = validateConnectLocked(cameraId, clientName8, |
| 1591 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { |
| 1592 | return ret; |
| 1593 | } |
| 1594 | |
| 1595 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 1596 | // we were doing a shim update, return immediately |
| 1597 | if (shimUpdateOnly) { |
| 1598 | auto cameraState = getCameraState(cameraId); |
| 1599 | if (cameraState != nullptr) { |
| 1600 | if (!cameraState->getShimParams().isEmpty()) return ret; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | status_t err; |
| 1605 | |
| 1606 | sp<BasicClient> clientTmp = nullptr; |
| 1607 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial; |
| 1608 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, |
| 1609 | IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp, |
| 1610 | /*out*/&partial)) != NO_ERROR) { |
| 1611 | switch (err) { |
| 1612 | case -ENODEV: |
| 1613 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1614 | "No camera device with ID \"%s\" currently available", |
| 1615 | cameraId.string()); |
| 1616 | case -EBUSY: |
| 1617 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 1618 | "Higher-priority client using camera, ID \"%s\" currently unavailable", |
| 1619 | cameraId.string()); |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1620 | case -EUSERS: |
| 1621 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1622 | "Too many cameras already open, cannot open camera \"%s\"", |
| 1623 | cameraId.string()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1624 | default: |
| 1625 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1626 | "Unexpected error %s (%d) opening camera \"%s\"", |
| 1627 | strerror(-err), err, cameraId.string()); |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | if (clientTmp.get() != nullptr) { |
| 1632 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 1633 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 1634 | return ret; |
| 1635 | } |
| 1636 | |
| 1637 | // give flashlight a chance to close devices if necessary. |
| 1638 | mFlashlight->prepareDeviceOpen(cameraId); |
| 1639 | |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1640 | int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing, /*out*/&orientation); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 1641 | if (facing == -1) { |
| 1642 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string()); |
| 1643 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1644 | "Unable to get camera device \"%s\" facing", cameraId.string()); |
| 1645 | } |
| 1646 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1647 | sp<BasicClient> tmp = nullptr; |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 1648 | if(!(ret = makeClient(this, cameraCb, clientPackageName, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 1649 | cameraId, api1CameraId, facing, orientation, |
Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 1650 | clientPid, clientUid, getpid(), |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1651 | deviceVersion, effectiveApiLevel, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1652 | /*out*/&tmp)).isOk()) { |
| 1653 | return ret; |
| 1654 | } |
| 1655 | client = static_cast<CLIENT*>(tmp.get()); |
| 1656 | |
| 1657 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 1658 | __FUNCTION__); |
| 1659 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 1660 | err = client->initialize(mCameraProviderManager, mMonitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1661 | if (err != OK) { |
| 1662 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1663 | // Errors could be from the HAL module open call or from AppOpsManager |
| 1664 | switch(err) { |
| 1665 | case BAD_VALUE: |
| 1666 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1667 | "Illegal argument to HAL module for camera \"%s\"", cameraId.string()); |
| 1668 | case -EBUSY: |
| 1669 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 1670 | "Camera \"%s\" is already open", cameraId.string()); |
| 1671 | case -EUSERS: |
| 1672 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1673 | "Too many cameras already open, cannot open camera \"%s\"", |
| 1674 | cameraId.string()); |
| 1675 | case PERMISSION_DENIED: |
| 1676 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1677 | "No permission to open camera \"%s\"", cameraId.string()); |
| 1678 | case -EACCES: |
| 1679 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1680 | "Camera \"%s\" disabled by policy", cameraId.string()); |
| 1681 | case -ENODEV: |
| 1682 | default: |
| 1683 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1684 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(), |
| 1685 | strerror(-err), err); |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | // Update shim paremeters for legacy clients |
| 1690 | if (effectiveApiLevel == API_1) { |
| 1691 | // Assume we have always received a Client subclass for API1 |
| 1692 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 1693 | String8 rawParams = shimClient->getParameters(); |
| 1694 | CameraParameters params(rawParams); |
| 1695 | |
| 1696 | auto cameraState = getCameraState(cameraId); |
| 1697 | if (cameraState != nullptr) { |
| 1698 | cameraState->setShimParams(params); |
| 1699 | } else { |
| 1700 | ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.", |
| 1701 | __FUNCTION__, cameraId.string()); |
| 1702 | } |
| 1703 | } |
| 1704 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1705 | // Set rotate-and-crop override behavior |
| 1706 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { |
| 1707 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1708 | } else if (CameraServiceProxyWrapper::isRotateAndCropOverrideNeeded(clientPackageName, |
| 1709 | orientation, facing)) { |
| 1710 | client->setRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_90); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1711 | } |
| 1712 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 1713 | // Set camera muting behavior |
| 1714 | if (client->supportsCameraMute()) { |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 1715 | bool isCameraPrivacyEnabled = |
| 1716 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(multiuser_get_user_id(clientUid)); |
| 1717 | client->setCameraMute(mOverrideCameraMuteMode || isCameraPrivacyEnabled); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 1718 | } |
| 1719 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1720 | if (shimUpdateOnly) { |
| 1721 | // If only updating legacy shim parameters, immediately disconnect client |
| 1722 | mServiceLock.unlock(); |
| 1723 | client->disconnect(); |
| 1724 | mServiceLock.lock(); |
| 1725 | } else { |
| 1726 | // Otherwise, add client to active clients list |
| 1727 | finishConnectLocked(client, partial); |
| 1728 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 1729 | |
| 1730 | client->setImageDumpMask(mImageDumpMask); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1731 | } // lock is destroyed, allow further connect calls |
| 1732 | |
| 1733 | // Important: release the mutex here so the client can call back into the service from its |
| 1734 | // destructor (can be at the end of the call) |
| 1735 | device = client; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1736 | |
| 1737 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); |
| 1738 | CameraServiceProxyWrapper::logOpen(cameraId, facing, clientPackageName, |
| 1739 | effectiveApiLevel, isNdk, openLatencyMs); |
| 1740 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1741 | return ret; |
| 1742 | } |
| 1743 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 1744 | status_t CameraService::addOfflineClient(String8 cameraId, sp<BasicClient> offlineClient) { |
| 1745 | if (offlineClient.get() == nullptr) { |
| 1746 | return BAD_VALUE; |
| 1747 | } |
| 1748 | |
| 1749 | { |
| 1750 | // Acquire mServiceLock and prevent other clients from connecting |
| 1751 | std::unique_ptr<AutoConditionLock> lock = |
| 1752 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 1753 | |
| 1754 | if (lock == nullptr) { |
| 1755 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." |
| 1756 | , __FUNCTION__, offlineClient->getClientPid()); |
| 1757 | return TIMED_OUT; |
| 1758 | } |
| 1759 | |
| 1760 | auto onlineClientDesc = mActiveClientManager.get(cameraId); |
| 1761 | if (onlineClientDesc.get() == nullptr) { |
| 1762 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, |
| 1763 | cameraId.c_str()); |
| 1764 | return BAD_VALUE; |
| 1765 | } |
| 1766 | |
| 1767 | // Offline clients do not evict or conflict with other online devices. Resource sharing |
| 1768 | // conflicts are handled by the camera provider which will either succeed or fail before |
| 1769 | // reaching this method. |
| 1770 | const auto& onlinePriority = onlineClientDesc->getPriority(); |
| 1771 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( |
| 1772 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, |
| 1773 | /*conflictingKeys*/ std::set<String8>(), onlinePriority.getScore(), |
| 1774 | onlineClientDesc->getOwnerId(), onlinePriority.getState()); |
| 1775 | |
| 1776 | // Allow only one offline device per camera |
| 1777 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); |
| 1778 | if (!incompatibleClients.empty()) { |
| 1779 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); |
| 1780 | return BAD_VALUE; |
| 1781 | } |
| 1782 | |
| 1783 | auto err = offlineClient->initialize(mCameraProviderManager, mMonitorTags); |
| 1784 | if (err != OK) { |
| 1785 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); |
| 1786 | return err; |
| 1787 | } |
| 1788 | |
| 1789 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); |
| 1790 | if (evicted.size() > 0) { |
| 1791 | for (auto& i : evicted) { |
| 1792 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", |
| 1793 | __FUNCTION__, i->getKey().string()); |
| 1794 | } |
| 1795 | |
| 1796 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " |
| 1797 | "properly", __FUNCTION__); |
| 1798 | |
| 1799 | return BAD_VALUE; |
| 1800 | } |
| 1801 | |
| 1802 | logConnectedOffline(offlineClientDesc->getKey(), |
| 1803 | static_cast<int>(offlineClientDesc->getOwnerId()), |
| 1804 | String8(offlineClient->getPackageName())); |
| 1805 | |
| 1806 | sp<IBinder> remoteCallback = offlineClient->getRemote(); |
| 1807 | if (remoteCallback != nullptr) { |
| 1808 | remoteCallback->linkToDeath(this); |
| 1809 | } |
| 1810 | } // lock is destroyed, allow further connect calls |
| 1811 | |
| 1812 | return OK; |
| 1813 | } |
| 1814 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1815 | Status CameraService::setTorchMode(const String16& cameraId, bool enabled, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1816 | const sp<IBinder>& clientBinder) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1817 | Mutex::Autolock lock(mServiceLock); |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1818 | |
| 1819 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1820 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1821 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1822 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1823 | "Torch client Binder is null"); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1824 | } |
| 1825 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1826 | String8 id = String8(cameraId.string()); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1827 | int uid = CameraThreadState::getCallingUid(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1828 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 1829 | if (shouldRejectSystemCameraConnection(id)) { |
| 1830 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" |
| 1831 | " for system only device %s: ", id.string()); |
| 1832 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1833 | // verify id is valid. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1834 | auto state = getCameraState(id); |
| 1835 | if (state == nullptr) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 1836 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1837 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1838 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1839 | } |
| 1840 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1841 | StatusInternal cameraStatus = state->getStatus(); |
| 1842 | if (cameraStatus != StatusInternal::PRESENT && |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 1843 | cameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 1844 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1845 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1846 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | { |
| 1850 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1851 | TorchModeStatus status; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1852 | status_t err = getTorchStatusLocked(id, &status); |
| 1853 | if (err != OK) { |
| 1854 | if (err == NAME_NOT_FOUND) { |
| 1855 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1856 | "Camera \"%s\" does not have a flash unit", id.string()); |
| 1857 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1858 | ALOGE("%s: getting current torch status failed for camera %s", |
| 1859 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1860 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1861 | "Error updating torch status for camera \"%s\": %s (%d)", id.string(), |
| 1862 | strerror(-err), err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1863 | } |
| 1864 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1865 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 1866 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1867 | ALOGE("%s: torch mode of camera %s is not available because " |
| 1868 | "camera is in use", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1869 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 1870 | "Torch for camera \"%s\" is not available due to an existing camera user", |
| 1871 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1872 | } else { |
| 1873 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 1874 | "insufficient resources", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1875 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1876 | "Torch for camera \"%s\" is not available due to insufficient resources", |
| 1877 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1882 | { |
| 1883 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 1884 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 1885 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1886 | if (mTorchUidMap.find(id) == mTorchUidMap.end()) { |
| 1887 | mTorchUidMap[id].first = uid; |
| 1888 | mTorchUidMap[id].second = uid; |
| 1889 | } else { |
| 1890 | // Set the pending UID |
| 1891 | mTorchUidMap[id].first = uid; |
| 1892 | } |
| 1893 | } |
| 1894 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1895 | status_t err = mFlashlight->setTorchMode(id, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1896 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1897 | if (err != OK) { |
| 1898 | int32_t errorCode; |
| 1899 | String8 msg; |
| 1900 | switch (err) { |
| 1901 | case -ENOSYS: |
| 1902 | msg = String8::format("Camera \"%s\" has no flashlight", |
| 1903 | id.string()); |
| 1904 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 1905 | break; |
| 1906 | default: |
| 1907 | msg = String8::format( |
| 1908 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", |
| 1909 | id.string(), enabled, strerror(-err), err); |
| 1910 | errorCode = ERROR_INVALID_OPERATION; |
| 1911 | } |
| 1912 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1913 | return STATUS_ERROR(errorCode, msg.string()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1914 | } |
| 1915 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1916 | { |
| 1917 | // update the link to client's death |
| 1918 | Mutex::Autolock al(mTorchClientMapMutex); |
| 1919 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 1920 | if (enabled) { |
| 1921 | if (index == NAME_NOT_FOUND) { |
| 1922 | mTorchClientMap.add(id, clientBinder); |
| 1923 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1924 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1925 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 1926 | } |
| 1927 | clientBinder->linkToDeath(this); |
| 1928 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1929 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1930 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1931 | } |
| 1932 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 1933 | int clientPid = CameraThreadState::getCallingPid(); |
| 1934 | const char *id_cstr = id.c_str(); |
| 1935 | const char *torchState = enabled ? "on" : "off"; |
| 1936 | ALOGI("Torch for camera id %s turned %s for client PID %d", id_cstr, torchState, clientPid); |
| 1937 | logTorchEvent(id_cstr, torchState , clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1938 | return Status::ok(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1939 | } |
| 1940 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1941 | Status CameraService::notifySystemEvent(int32_t eventId, |
| 1942 | const std::vector<int32_t>& args) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 1943 | const int pid = CameraThreadState::getCallingPid(); |
| 1944 | const int selfPid = getpid(); |
| 1945 | |
| 1946 | // Permission checks |
| 1947 | if (pid != selfPid) { |
| 1948 | // Ensure we're being called by system_server, or similar process with |
| 1949 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1950 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 1951 | const int uid = CameraThreadState::getCallingUid(); |
| 1952 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 1953 | " events from pid=%d, uid=%d", pid, uid); |
| 1954 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 1955 | "No permission to send updates to camera service about system events" |
| 1956 | " from pid=%d, uid=%d", pid, uid); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 1957 | } |
| 1958 | } |
| 1959 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1960 | ATRACE_CALL(); |
| 1961 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1962 | switch(eventId) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1963 | case ICameraService::EVENT_USER_SWITCHED: { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1964 | // Try to register for UID and sensor privacy policy updates, in case we're recovering |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 1965 | // from a system server crash |
| 1966 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1967 | mSensorPrivacyPolicy->registerSelf(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1968 | doUserSwitch(/*newUserIds*/ args); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1969 | break; |
| 1970 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1971 | case ICameraService::EVENT_NONE: |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1972 | default: { |
| 1973 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 1974 | eventId); |
| 1975 | break; |
| 1976 | } |
| 1977 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1978 | return Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 1981 | void CameraService::notifyMonitoredUids() { |
| 1982 | Mutex::Autolock lock(mStatusListenerLock); |
| 1983 | |
| 1984 | for (const auto& it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1985 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 1986 | if (!ret.isOk()) { |
| 1987 | ALOGE("%s: Failed to trigger permission callback: %d", __FUNCTION__, |
| 1988 | ret.exceptionCode()); |
| 1989 | } |
| 1990 | } |
| 1991 | } |
| 1992 | |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 1993 | Status CameraService::notifyDeviceStateChange(int64_t newState) { |
| 1994 | const int pid = CameraThreadState::getCallingPid(); |
| 1995 | const int selfPid = getpid(); |
| 1996 | |
| 1997 | // Permission checks |
| 1998 | if (pid != selfPid) { |
| 1999 | // Ensure we're being called by system_server, or similar process with |
| 2000 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2001 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2002 | const int uid = CameraThreadState::getCallingUid(); |
| 2003 | ALOGE("Permission Denial: cannot send updates to camera service about device" |
| 2004 | " state changes from pid=%d, uid=%d", pid, uid); |
| 2005 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2006 | "No permission to send updates to camera service about device state" |
| 2007 | " changes from pid=%d, uid=%d", pid, uid); |
| 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | ATRACE_CALL(); |
| 2012 | |
| 2013 | using hardware::camera::provider::V2_5::DeviceState; |
| 2014 | hardware::hidl_bitfield<DeviceState> newDeviceState{}; |
| 2015 | if (newState & ICameraService::DEVICE_STATE_BACK_COVERED) { |
| 2016 | newDeviceState |= DeviceState::BACK_COVERED; |
| 2017 | } |
| 2018 | if (newState & ICameraService::DEVICE_STATE_FRONT_COVERED) { |
| 2019 | newDeviceState |= DeviceState::FRONT_COVERED; |
| 2020 | } |
| 2021 | if (newState & ICameraService::DEVICE_STATE_FOLDED) { |
| 2022 | newDeviceState |= DeviceState::FOLDED; |
| 2023 | } |
| 2024 | // Only map vendor bits directly |
| 2025 | uint64_t vendorBits = static_cast<uint64_t>(newState) & 0xFFFFFFFF00000000l; |
| 2026 | newDeviceState |= vendorBits; |
| 2027 | |
| 2028 | ALOGV("%s: New device state 0x%" PRIx64, __FUNCTION__, newDeviceState); |
| 2029 | Mutex::Autolock l(mServiceLock); |
| 2030 | mCameraProviderManager->notifyDeviceStateChange(newDeviceState); |
| 2031 | |
| 2032 | return Status::ok(); |
| 2033 | } |
| 2034 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2035 | Status CameraService::notifyDisplayConfigurationChange() { |
| 2036 | ATRACE_CALL(); |
| 2037 | const int callingPid = CameraThreadState::getCallingPid(); |
| 2038 | const int selfPid = getpid(); |
| 2039 | |
| 2040 | // Permission checks |
| 2041 | if (callingPid != selfPid) { |
| 2042 | // Ensure we're being called by system_server, or similar process with |
| 2043 | // permissions to notify the camera service about system events |
| 2044 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
| 2045 | const int uid = CameraThreadState::getCallingUid(); |
| 2046 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" |
| 2047 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2048 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2049 | "No permission to send updates to camera service about orientation" |
| 2050 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | Mutex::Autolock lock(mServiceLock); |
| 2055 | |
| 2056 | // Don't do anything if rotate-and-crop override via cmd is active |
| 2057 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); |
| 2058 | |
| 2059 | const auto clients = mActiveClientManager.getAll(); |
| 2060 | for (auto& current : clients) { |
| 2061 | if (current != nullptr) { |
| 2062 | const auto basicClient = current->getValue(); |
| 2063 | if (basicClient.get() != nullptr) { |
| 2064 | if (CameraServiceProxyWrapper::isRotateAndCropOverrideNeeded( |
| 2065 | basicClient->getPackageName(), basicClient->getCameraOrientation(), |
| 2066 | basicClient->getCameraFacing())) { |
| 2067 | basicClient->setRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_90); |
| 2068 | } else { |
| 2069 | basicClient->setRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE); |
| 2070 | } |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | return Status::ok(); |
| 2076 | } |
| 2077 | |
| 2078 | Status CameraService::getConcurrentCameraIds( |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2079 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { |
| 2080 | ATRACE_CALL(); |
| 2081 | if (!concurrentCameraIds) { |
| 2082 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); |
| 2083 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); |
| 2084 | } |
| 2085 | |
| 2086 | if (!mInitialized) { |
| 2087 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 2088 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2089 | "Camera subsystem is not available"); |
| 2090 | } |
| 2091 | // First call into the provider and get the set of concurrent camera |
| 2092 | // combinations |
| 2093 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = |
Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 2094 | mCameraProviderManager->getConcurrentCameraIds(); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2095 | for (auto &combination : concurrentCameraCombinations) { |
| 2096 | std::vector<std::string> validCombination; |
| 2097 | for (auto &cameraId : combination) { |
| 2098 | // if the camera state is not present, skip |
| 2099 | String8 cameraIdStr(cameraId.c_str()); |
| 2100 | auto state = getCameraState(cameraIdStr); |
| 2101 | if (state == nullptr) { |
| 2102 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); |
| 2103 | continue; |
| 2104 | } |
| 2105 | StatusInternal status = state->getStatus(); |
| 2106 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { |
| 2107 | continue; |
| 2108 | } |
| 2109 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { |
| 2110 | continue; |
| 2111 | } |
| 2112 | validCombination.push_back(cameraId); |
| 2113 | } |
| 2114 | if (validCombination.size() != 0) { |
| 2115 | concurrentCameraIds->push_back(std::move(validCombination)); |
| 2116 | } |
| 2117 | } |
| 2118 | return Status::ok(); |
| 2119 | } |
| 2120 | |
| 2121 | Status CameraService::isConcurrentSessionConfigurationSupported( |
| 2122 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, |
| 2123 | /*out*/bool* isSupported) { |
| 2124 | if (!isSupported) { |
| 2125 | ALOGE("%s: isSupported is NULL", __FUNCTION__); |
| 2126 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); |
| 2127 | } |
| 2128 | |
| 2129 | if (!mInitialized) { |
| 2130 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 2131 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2132 | "Camera subsystem is not available"); |
| 2133 | } |
| 2134 | |
| 2135 | // Check for camera permissions |
| 2136 | int callingPid = CameraThreadState::getCallingPid(); |
| 2137 | int callingUid = CameraThreadState::getCallingUid(); |
| 2138 | if ((callingPid != getpid()) && !checkPermission(sCameraPermission, callingPid, callingUid)) { |
| 2139 | ALOGE("%s: pid %d doesn't have camera permissions", __FUNCTION__, callingPid); |
| 2140 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 2141 | "android.permission.CAMERA needed to call" |
| 2142 | "isConcurrentSessionConfigurationSupported"); |
| 2143 | } |
| 2144 | |
| 2145 | status_t res = |
| 2146 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( |
| 2147 | cameraIdsAndSessionConfigurations, isSupported); |
| 2148 | if (res != OK) { |
| 2149 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " |
| 2150 | "support %s (%d)", strerror(-res), res); |
| 2151 | } |
| 2152 | return Status::ok(); |
| 2153 | } |
| 2154 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2155 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, |
| 2156 | /*out*/ |
| 2157 | std::vector<hardware::CameraStatus> *cameraStatuses) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2158 | return addListenerHelper(listener, cameraStatuses); |
| 2159 | } |
| 2160 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2161 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, |
| 2162 | std::vector<hardware::CameraStatus>* cameraStatuses) { |
| 2163 | return addListenerHelper(listener, cameraStatuses, false, true); |
| 2164 | } |
| 2165 | |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2166 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, |
| 2167 | /*out*/ |
| 2168 | std::vector<hardware::CameraStatus> *cameraStatuses, |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2169 | bool isVendorListener, bool isProcessLocalTest) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2170 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2171 | ATRACE_CALL(); |
| 2172 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2173 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2174 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 2175 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2176 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2177 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2180 | auto clientUid = CameraThreadState::getCallingUid(); |
| 2181 | auto clientPid = CameraThreadState::getCallingPid(); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2182 | bool openCloseCallbackAllowed = checkPermission(sCameraOpenCloseListenerPermission, |
| 2183 | clientPid, clientUid); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2184 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2185 | Mutex::Autolock lock(mServiceLock); |
| 2186 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2187 | { |
| 2188 | Mutex::Autolock lock(mStatusListenerLock); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2189 | for (const auto &it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2190 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2191 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 2192 | __FUNCTION__, listener.get()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2193 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2194 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2195 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2196 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2197 | sp<ServiceListener> serviceListener = |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2198 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, |
| 2199 | openCloseCallbackAllowed); |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2200 | auto ret = serviceListener->initialize(isProcessLocalTest); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2201 | if (ret != NO_ERROR) { |
| 2202 | String8 msg = String8::format("Failed to initialize service listener: %s (%d)", |
| 2203 | strerror(-ret), ret); |
| 2204 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2205 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 2206 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2207 | // The listener still needs to be added to the list of listeners, regardless of what |
| 2208 | // permissions the listener process has / whether it is a vendor listener. Since it might be |
| 2209 | // eligible to listen to other camera ids. |
| 2210 | mListenerList.emplace_back(serviceListener); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2211 | mUidPolicy->registerMonitorUid(clientUid); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2212 | } |
| 2213 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2214 | /* Collect current devices and status */ |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2215 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2216 | Mutex::Autolock lock(mCameraStatesLock); |
| 2217 | for (auto& i : mCameraStates) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 2218 | cameraStatuses->emplace_back(i.first, |
| 2219 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2220 | } |
| 2221 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2222 | // Remove the camera statuses that should be hidden from the client, we do |
| 2223 | // this after collecting the states in order to avoid holding |
| 2224 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at |
| 2225 | // the same time. |
| 2226 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), |
| 2227 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { |
| 2228 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 2229 | if (getSystemCameraKind(s.cameraId, &deviceKind) != OK) { |
| 2230 | ALOGE("%s: Invalid camera id %s, skipping status update", |
| 2231 | __FUNCTION__, s.cameraId.c_str()); |
| 2232 | return true; |
| 2233 | } |
| 2234 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, |
| 2235 | clientUid);}), cameraStatuses->end()); |
| 2236 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2237 | //cameraStatuses will have non-eligible camera ids removed. |
| 2238 | std::set<String16> idsChosenForCallback; |
| 2239 | for (const auto &s : *cameraStatuses) { |
| 2240 | idsChosenForCallback.insert(String16(s.cameraId)); |
| 2241 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2242 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2243 | /* |
| 2244 | * Immediately signal current torch status to this listener only |
| 2245 | * This may be a subset of all the devices, so don't include it in the response directly |
| 2246 | */ |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2247 | { |
| 2248 | Mutex::Autolock al(mTorchStatusMutex); |
| 2249 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2250 | String16 id = String16(mTorchStatusMap.keyAt(i).string()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2251 | // The camera id is visible to the client. Fine to send torch |
| 2252 | // callback. |
| 2253 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { |
| 2254 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id); |
| 2255 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2256 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2257 | } |
| 2258 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2259 | return Status::ok(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2260 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2261 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2262 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2263 | ATRACE_CALL(); |
| 2264 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2265 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 2266 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2267 | if (listener == 0) { |
| 2268 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2269 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2270 | } |
| 2271 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2272 | Mutex::Autolock lock(mServiceLock); |
| 2273 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2274 | { |
| 2275 | Mutex::Autolock lock(mStatusListenerLock); |
| 2276 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2277 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { |
| 2278 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid()); |
| 2279 | IInterface::asBinder(listener)->unlinkToDeath(*it); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2280 | mListenerList.erase(it); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2281 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2282 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 2287 | __FUNCTION__, listener.get()); |
| 2288 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2289 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2290 | } |
| 2291 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2292 | Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2293 | |
| 2294 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2295 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 2296 | |
| 2297 | if (parameters == NULL) { |
| 2298 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2299 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2300 | } |
| 2301 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2302 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2303 | |
| 2304 | CameraParameters shimParams; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2305 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2306 | // Error logged by caller |
| 2307 | return ret; |
| 2308 | } |
| 2309 | |
| 2310 | String8 shimParamsString8 = shimParams.flatten(); |
| 2311 | String16 shimParamsString16 = String16(shimParamsString8); |
| 2312 | |
| 2313 | *parameters = shimParamsString16; |
| 2314 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2315 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2316 | } |
| 2317 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2318 | Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion, |
| 2319 | /*out*/ bool *isSupported) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2320 | ATRACE_CALL(); |
| 2321 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2322 | const String8 id = String8(cameraId); |
| 2323 | |
| 2324 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2325 | |
| 2326 | switch (apiVersion) { |
| 2327 | case API_VERSION_1: |
| 2328 | case API_VERSION_2: |
| 2329 | break; |
| 2330 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2331 | String8 msg = String8::format("Unknown API version %d", apiVersion); |
| 2332 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2333 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2334 | } |
| 2335 | |
Emilian Peev | 28ad2ea | 2017-02-07 16:14:32 +0000 | [diff] [blame] | 2336 | int deviceVersion = getDeviceVersion(id); |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2337 | switch (deviceVersion) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2338 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 2339 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 2340 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 2341 | if (apiVersion == API_VERSION_2) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2342 | ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim", |
| 2343 | __FUNCTION__, id.string(), deviceVersion); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2344 | *isSupported = false; |
| 2345 | } else { // if (apiVersion == API_VERSION_1) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2346 | ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported", |
| 2347 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2348 | *isSupported = true; |
| 2349 | } |
| 2350 | break; |
| 2351 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 2352 | case CAMERA_DEVICE_API_VERSION_3_3: |
Zhijun He | 4afbdec | 2016-05-04 15:42:51 -0700 | [diff] [blame] | 2353 | case CAMERA_DEVICE_API_VERSION_3_4: |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2354 | case CAMERA_DEVICE_API_VERSION_3_5: |
Yin-Chia Yeh | 56d98ba | 2020-01-16 16:57:14 -0800 | [diff] [blame] | 2355 | case CAMERA_DEVICE_API_VERSION_3_6: |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 2356 | case CAMERA_DEVICE_API_VERSION_3_7: |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2357 | ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly", |
| 2358 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2359 | *isSupported = true; |
| 2360 | break; |
| 2361 | case -1: { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2362 | String8 msg = String8::format("Unknown camera ID %s", id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2363 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2364 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2365 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2366 | default: { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2367 | String8 msg = String8::format("Unknown device version %x for device %s", |
| 2368 | deviceVersion, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2369 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2370 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string()); |
| 2371 | } |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2372 | } |
| 2373 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2374 | return Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2375 | } |
| 2376 | |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2377 | Status CameraService::isHiddenPhysicalCamera(const String16& cameraId, |
| 2378 | /*out*/ bool *isSupported) { |
| 2379 | ATRACE_CALL(); |
| 2380 | |
| 2381 | const String8 id = String8(cameraId); |
| 2382 | |
| 2383 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
| 2384 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(id.string()); |
| 2385 | |
| 2386 | return Status::ok(); |
| 2387 | } |
| 2388 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2389 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2390 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2391 | for (auto& i : mActiveClientManager.getAll()) { |
| 2392 | auto clientSp = i->getValue(); |
| 2393 | if (clientSp.get() == client) { |
| 2394 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2395 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2396 | } |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2397 | updateAudioRestrictionLocked(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2398 | } |
| 2399 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2400 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2401 | bool ret = false; |
| 2402 | { |
| 2403 | // Acquire mServiceLock and prevent other clients from connecting |
| 2404 | std::unique_ptr<AutoConditionLock> lock = |
| 2405 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2406 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2407 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2408 | std::vector<sp<BasicClient>> evicted; |
| 2409 | for (auto& i : mActiveClientManager.getAll()) { |
| 2410 | auto clientSp = i->getValue(); |
| 2411 | if (clientSp.get() == nullptr) { |
| 2412 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 2413 | mActiveClientManager.remove(i); |
| 2414 | continue; |
| 2415 | } |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 2416 | if (remote == clientSp->getRemote()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2417 | mActiveClientManager.remove(i); |
| 2418 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2419 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2420 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2421 | clientSp->notifyError( |
| 2422 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2423 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2424 | } |
| 2425 | } |
| 2426 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2427 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 2428 | // other clients from connecting in mServiceLockWrapper if held |
| 2429 | mServiceLock.unlock(); |
| 2430 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2431 | // Do not clear caller identity, remote caller should be client proccess |
| 2432 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2433 | for (auto& i : evicted) { |
| 2434 | if (i.get() != nullptr) { |
| 2435 | i->disconnect(); |
| 2436 | ret = true; |
| 2437 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2438 | } |
| 2439 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2440 | // Reacquire mServiceLock |
| 2441 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2442 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2443 | } // lock is destroyed, allow further connect calls |
| 2444 | |
| 2445 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2446 | } |
| 2447 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2448 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
| 2449 | const String8& cameraId) const { |
| 2450 | std::shared_ptr<CameraState> state; |
| 2451 | { |
| 2452 | Mutex::Autolock lock(mCameraStatesLock); |
| 2453 | auto iter = mCameraStates.find(cameraId); |
| 2454 | if (iter != mCameraStates.end()) { |
| 2455 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2456 | } |
| 2457 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2458 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2459 | } |
| 2460 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2461 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) { |
| 2462 | // Remove from active clients list |
| 2463 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 2464 | if (clientDescriptorPtr == nullptr) { |
| 2465 | ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__, |
| 2466 | cameraId.string()); |
| 2467 | return sp<BasicClient>{nullptr}; |
| 2468 | } |
| 2469 | |
| 2470 | return clientDescriptorPtr->getValue(); |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 2471 | } |
| 2472 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2473 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2474 | // Acquire mServiceLock and prevent other clients from connecting |
| 2475 | std::unique_ptr<AutoConditionLock> lock = |
| 2476 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 2477 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2478 | std::set<userid_t> newAllowedUsers; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2479 | for (size_t i = 0; i < newUserIds.size(); i++) { |
| 2480 | if (newUserIds[i] < 0) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2481 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2482 | __FUNCTION__, newUserIds[i]); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2483 | return; |
| 2484 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2485 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2486 | } |
| 2487 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2488 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2489 | if (newAllowedUsers == mAllowedUsers) { |
| 2490 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 2491 | return; |
| 2492 | } |
| 2493 | |
| 2494 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 2495 | |
| 2496 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2497 | |
| 2498 | // Current user has switched, evict all current clients. |
| 2499 | std::vector<sp<BasicClient>> evicted; |
| 2500 | for (auto& i : mActiveClientManager.getAll()) { |
| 2501 | auto clientSp = i->getValue(); |
| 2502 | |
| 2503 | if (clientSp.get() == nullptr) { |
| 2504 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 2505 | continue; |
| 2506 | } |
| 2507 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2508 | // Don't evict clients that are still allowed. |
| 2509 | uid_t clientUid = clientSp->getClientUid(); |
| 2510 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 2511 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 2512 | continue; |
| 2513 | } |
| 2514 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2515 | evicted.push_back(clientSp); |
| 2516 | |
| 2517 | String8 curTime = getFormattedCurrentTime(); |
| 2518 | |
| 2519 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
| 2520 | i->getKey().string()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2521 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2522 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2523 | logEvent(String8::format("EVICT device %s client held by package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2524 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due" |
| 2525 | " to user switch.", i->getKey().string(), |
| 2526 | String8{clientSp->getPackageName()}.string(), |
| 2527 | i->getOwnerId(), i->getPriority().getScore(), |
| 2528 | i->getPriority().getState())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2529 | |
| 2530 | } |
| 2531 | |
| 2532 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 2533 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 2534 | mServiceLock.unlock(); |
| 2535 | |
| 2536 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2537 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2538 | |
| 2539 | for (auto& i : evicted) { |
| 2540 | i->disconnect(); |
| 2541 | } |
| 2542 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2543 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2544 | |
| 2545 | // Reacquire mServiceLock |
| 2546 | mServiceLock.lock(); |
| 2547 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2548 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2549 | void CameraService::logEvent(const char* event) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2550 | String8 curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2551 | Mutex::Autolock l(mLogLock); |
| 2552 | mEventLog.add(String8::format("%s : %s", curTime.string(), event)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2553 | } |
| 2554 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2555 | void CameraService::logDisconnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2556 | const char* clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2557 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2558 | logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2559 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2560 | } |
| 2561 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2562 | void CameraService::logDisconnectedOffline(const char* cameraId, int clientPid, |
| 2563 | const char* clientPackage) { |
| 2564 | // Log the clients evicted |
| 2565 | logEvent(String8::format("DISCONNECT offline device %s client for package %s (PID %d)", |
| 2566 | cameraId, clientPackage, clientPid)); |
| 2567 | } |
| 2568 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2569 | void CameraService::logConnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2570 | const char* clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2571 | // Log the clients evicted |
| 2572 | logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2573 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2576 | void CameraService::logConnectedOffline(const char* cameraId, int clientPid, |
| 2577 | const char* clientPackage) { |
| 2578 | // Log the clients evicted |
| 2579 | logEvent(String8::format("CONNECT offline device %s client for package %s (PID %d)", cameraId, |
| 2580 | clientPackage, clientPid)); |
| 2581 | } |
| 2582 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2583 | void CameraService::logRejected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2584 | const char* clientPackage, const char* reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2585 | // Log the client rejected |
| 2586 | logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)", |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2587 | cameraId, clientPackage, clientPid, reason)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2588 | } |
| 2589 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 2590 | void CameraService::logTorchEvent(const char* cameraId, const char *torchState, int clientPid) { |
| 2591 | // Log torch event |
| 2592 | logEvent(String8::format("Torch for camera id %s turned %s for client PID %d", cameraId, |
| 2593 | torchState, clientPid)); |
| 2594 | } |
| 2595 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2596 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 2597 | const std::set<userid_t>& newUserIds) { |
| 2598 | String8 newUsers = toString(newUserIds); |
| 2599 | String8 oldUsers = toString(oldUserIds); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 2600 | if (oldUsers.size() == 0) { |
| 2601 | oldUsers = "<None>"; |
| 2602 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2603 | // Log the new and old users |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 2604 | logEvent(String8::format("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s", |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2605 | oldUsers.string(), newUsers.string())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) { |
| 2609 | // Log the device removal |
| 2610 | logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason)); |
| 2611 | } |
| 2612 | |
| 2613 | void CameraService::logDeviceAdded(const char* cameraId, const char* reason) { |
| 2614 | // Log the device removal |
| 2615 | logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason)); |
| 2616 | } |
| 2617 | |
| 2618 | void CameraService::logClientDied(int clientPid, const char* reason) { |
| 2619 | // Log the device removal |
| 2620 | logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason)); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2621 | } |
| 2622 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 2623 | void CameraService::logServiceError(const char* msg, int errorCode) { |
| 2624 | String8 curTime = getFormattedCurrentTime(); |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 2625 | logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode))); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2628 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 2629 | uint32_t flags) { |
| 2630 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2631 | // Permission checks |
| 2632 | switch (code) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 2633 | case SHELL_COMMAND_TRANSACTION: { |
| 2634 | int in = data.readFileDescriptor(); |
| 2635 | int out = data.readFileDescriptor(); |
| 2636 | int err = data.readFileDescriptor(); |
| 2637 | int argc = data.readInt32(); |
| 2638 | Vector<String16> args; |
| 2639 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 2640 | args.add(data.readString16()); |
| 2641 | } |
| 2642 | sp<IBinder> unusedCallback; |
| 2643 | sp<IResultReceiver> resultReceiver; |
| 2644 | status_t status; |
| 2645 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 2646 | return status; |
| 2647 | } |
| 2648 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 2649 | return status; |
| 2650 | } |
| 2651 | status = shellCommand(in, out, err, args); |
| 2652 | if (resultReceiver != nullptr) { |
| 2653 | resultReceiver->send(status); |
| 2654 | } |
| 2655 | return NO_ERROR; |
| 2656 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | return BnCameraService::onTransact(code, data, reply, flags); |
| 2660 | } |
| 2661 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2662 | // We share the media players for shutter and recording sound for all clients. |
| 2663 | // A reference count is kept to determine when we will actually release the |
| 2664 | // media players. |
| 2665 | |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 2666 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { |
| 2667 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 2668 | status_t error; |
| 2669 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 2670 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 2671 | error = mp->prepare(); |
| 2672 | } |
| 2673 | if (error != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2674 | ALOGE("Failed to load CameraService sounds: %s", file); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 2675 | mp->disconnect(); |
| 2676 | mp.clear(); |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 2677 | return nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2678 | } |
| 2679 | return mp; |
| 2680 | } |
| 2681 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2682 | void CameraService::increaseSoundRef() { |
| 2683 | Mutex::Autolock lock(mSoundLock); |
| 2684 | mSoundRef++; |
| 2685 | } |
| 2686 | |
| 2687 | void CameraService::loadSoundLocked(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2688 | ATRACE_CALL(); |
| 2689 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2690 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); |
| 2691 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { |
| 2692 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); |
| 2693 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { |
| 2694 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 2695 | } |
| 2696 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) { |
| 2697 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); |
| 2698 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { |
| 2699 | mSoundPlayer[SOUND_RECORDING_START] = |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 2700 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2701 | } |
| 2702 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { |
| 2703 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); |
| 2704 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { |
| 2705 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
| 2706 | } |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 2707 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2708 | } |
| 2709 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2710 | void CameraService::decreaseSoundRef() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2711 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2712 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2713 | if (--mSoundRef) return; |
| 2714 | |
| 2715 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 2716 | if (mSoundPlayer[i] != 0) { |
| 2717 | mSoundPlayer[i]->disconnect(); |
| 2718 | mSoundPlayer[i].clear(); |
| 2719 | } |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2724 | ATRACE_CALL(); |
| 2725 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2726 | LOG1("playSound(%d)", kind); |
Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame^] | 2727 | if (kind < 0 || kind >= NUM_SOUNDS) { |
| 2728 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); |
| 2729 | return; |
| 2730 | } |
| 2731 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2732 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2733 | loadSoundLocked(kind); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2734 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 2735 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 2736 | player->seekTo(0); |
| 2737 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | // ---------------------------------------------------------------------------- |
| 2742 | |
| 2743 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 2744 | const sp<ICameraClient>& cameraClient, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2745 | const String16& clientPackageName, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 2746 | const std::optional<String16>& clientFeatureId, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2747 | const String8& cameraIdStr, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2748 | int api1CameraId, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2749 | int clientPid, uid_t clientUid, |
| 2750 | int servicePid) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 2751 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 2752 | IInterface::asBinder(cameraClient), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 2753 | clientPackageName, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2754 | cameraIdStr, cameraFacing, sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2755 | clientPid, clientUid, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2756 | servicePid), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2757 | mCameraId(api1CameraId) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2758 | { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2759 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2760 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2761 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 2762 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2763 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2764 | cameraService->increaseSoundRef(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2765 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2766 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2767 | } |
| 2768 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2769 | // tear down the client |
| 2770 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 2771 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2772 | mDestructionStarted = true; |
| 2773 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2774 | sCameraService->decreaseSoundRef(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 2775 | // unconditionally disconnect. function is idempotent |
| 2776 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2777 | } |
| 2778 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2779 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; |
| 2780 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2781 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2782 | const sp<IBinder>& remoteCallback, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 2783 | const String16& clientPackageName, const std::optional<String16>& clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2784 | const String8& cameraIdStr, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2785 | int clientPid, uid_t clientUid, |
| 2786 | int servicePid): |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 2787 | mDestructionStarted(false), |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2788 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), mOrientation(sensorOrientation), |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 2789 | mClientPackageName(clientPackageName), mClientFeatureId(clientFeatureId), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 2790 | mClientPid(clientPid), mClientUid(clientUid), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2791 | mServicePid(servicePid), |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 2792 | mDisconnected(false), mUidIsTrusted(false), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2793 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 2794 | mRemoteBinder(remoteCallback), |
| 2795 | mOpsActive(false), |
| 2796 | mOpsStreaming(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2797 | { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2798 | if (sCameraService == nullptr) { |
| 2799 | sCameraService = cameraService; |
| 2800 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 2801 | |
| 2802 | // In some cases the calling code has no access to the package it runs under. |
| 2803 | // For example, NDK camera API. |
| 2804 | // In this case we will get the packages for the calling UID and pick the first one |
| 2805 | // for attributing the app op. This will work correctly for runtime permissions |
| 2806 | // as for legacy apps we will toggle the app op for all packages in the UID. |
| 2807 | // The caveat is that the operation may be attributed to the wrong package and |
| 2808 | // stats based on app ops may be slightly off. |
| 2809 | if (mClientPackageName.size() <= 0) { |
| 2810 | sp<IServiceManager> sm = defaultServiceManager(); |
| 2811 | sp<IBinder> binder = sm->getService(String16(kPermissionServiceName)); |
| 2812 | if (binder == 0) { |
| 2813 | ALOGE("Cannot get permission service"); |
| 2814 | // Leave mClientPackageName unchanged (empty) and the further interaction |
| 2815 | // with camera will fail in BasicClient::startCameraOps |
| 2816 | return; |
| 2817 | } |
| 2818 | |
| 2819 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 2820 | Vector<String16> packages; |
| 2821 | |
| 2822 | permCtrl->getPackagesForUid(mClientUid, packages); |
| 2823 | |
| 2824 | if (packages.isEmpty()) { |
| 2825 | ALOGE("No packages for calling UID"); |
| 2826 | // Leave mClientPackageName unchanged (empty) and the further interaction |
| 2827 | // with camera will fail in BasicClient::startCameraOps |
| 2828 | return; |
| 2829 | } |
| 2830 | mClientPackageName = packages[0]; |
| 2831 | } |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 2832 | if (getCurrentServingCall() != BinderCallType::HWBINDER) { |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2833 | mAppOpsManager = std::make_unique<AppOpsManager>(); |
| 2834 | } |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 2835 | |
| 2836 | mUidIsTrusted = isTrustedCallingUid(mClientUid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 2840 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2841 | mDestructionStarted = true; |
| 2842 | } |
| 2843 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2844 | binder::Status CameraService::BasicClient::disconnect() { |
| 2845 | binder::Status res = Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2846 | if (mDisconnected) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2847 | return res; |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2848 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 2849 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2850 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2851 | sCameraService->removeByClient(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2852 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, String8(mClientPackageName)); |
Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 2853 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, |
| 2854 | mCameraIdStr.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2855 | |
| 2856 | sp<IBinder> remote = getRemote(); |
| 2857 | if (remote != nullptr) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2858 | remote->unlinkToDeath(sCameraService); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2859 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2860 | |
| 2861 | finishCameraOps(); |
Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 2862 | // Notify flashlight that a camera device is closed. |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2863 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); |
| 2864 | ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(), |
| 2865 | mClientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2866 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2867 | // client shouldn't be able to call into us anymore |
| 2868 | mClientPid = 0; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2869 | |
| 2870 | return res; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2871 | } |
| 2872 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 2873 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 2874 | // No dumping of clients directly over Binder, |
| 2875 | // must go through CameraService::dump |
| 2876 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2877 | CameraThreadState::getCallingUid(), NULL, 0); |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 2878 | return OK; |
| 2879 | } |
| 2880 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2881 | String16 CameraService::BasicClient::getPackageName() const { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2882 | return mClientPackageName; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2883 | } |
| 2884 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2885 | int CameraService::BasicClient::getCameraFacing() const { |
| 2886 | return mCameraFacing; |
| 2887 | } |
| 2888 | |
| 2889 | int CameraService::BasicClient::getCameraOrientation() const { |
| 2890 | return mOrientation; |
| 2891 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2892 | |
| 2893 | int CameraService::BasicClient::getClientPid() const { |
| 2894 | return mClientPid; |
| 2895 | } |
| 2896 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2897 | uid_t CameraService::BasicClient::getClientUid() const { |
| 2898 | return mClientUid; |
| 2899 | } |
| 2900 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2901 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 2902 | // Defaults to API2. |
| 2903 | return level == API_2; |
| 2904 | } |
| 2905 | |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 2906 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2907 | { |
| 2908 | Mutex::Autolock l(mAudioRestrictionLock); |
| 2909 | mAudioRestriction = mode; |
| 2910 | } |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 2911 | sCameraService->updateAudioRestriction(); |
| 2912 | return OK; |
| 2913 | } |
| 2914 | |
| 2915 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2916 | return sCameraService->updateAudioRestriction(); |
| 2917 | } |
| 2918 | |
| 2919 | int32_t CameraService::BasicClient::getAudioRestriction() const { |
| 2920 | Mutex::Autolock l(mAudioRestrictionLock); |
| 2921 | return mAudioRestriction; |
| 2922 | } |
| 2923 | |
| 2924 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { |
| 2925 | switch (mode) { |
| 2926 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: |
| 2927 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: |
| 2928 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: |
| 2929 | return true; |
| 2930 | default: |
| 2931 | return false; |
| 2932 | } |
| 2933 | } |
| 2934 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 2935 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { |
| 2936 | if (mode == AppOpsManager::MODE_ERRORED) { |
| 2937 | ALOGI("Camera %s: Access for \"%s\" has been revoked", |
| 2938 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 2939 | return PERMISSION_DENIED; |
| 2940 | } else if (!mUidIsTrusted && mode == AppOpsManager::MODE_IGNORED) { |
| 2941 | // If the calling Uid is trusted (a native service), the AppOpsManager could |
| 2942 | // return MODE_IGNORED. Do not treat such case as error. |
| 2943 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, |
| 2944 | mClientPackageName); |
| 2945 | bool isCameraPrivacyEnabled = |
| 2946 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled( |
| 2947 | multiuser_get_user_id(mClientUid)); |
| 2948 | if (!isUidActive || !isCameraPrivacyEnabled) { |
| 2949 | ALOGI("Camera %s: Access for \"%s\" has been restricted", |
| 2950 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 2951 | // Return the same error as for device policy manager rejection |
| 2952 | return -EACCES; |
| 2953 | } |
| 2954 | } |
| 2955 | return OK; |
| 2956 | } |
| 2957 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2958 | status_t CameraService::BasicClient::startCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2959 | ATRACE_CALL(); |
| 2960 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2961 | { |
| 2962 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2963 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2964 | } |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2965 | if (mAppOpsManager != nullptr) { |
| 2966 | // Notify app ops that the camera is not available |
| 2967 | mOpsCallback = new OpsCallback(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2968 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 2969 | mClientPackageName, mOpsCallback); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2970 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 2971 | // Just check for camera acccess here on open - delay startOp until |
| 2972 | // camera frames start streaming in startCameraStreamingOps |
| 2973 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 2974 | mClientPackageName); |
| 2975 | status_t res = handleAppOpMode(mode); |
| 2976 | if (res != OK) { |
| 2977 | return res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2978 | } |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 2979 | } |
| 2980 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2981 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2982 | |
| 2983 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2984 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2985 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2986 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid); |
| 2987 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2988 | // Notify listeners of camera open/close status |
| 2989 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); |
| 2990 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2991 | return OK; |
| 2992 | } |
| 2993 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 2994 | status_t CameraService::BasicClient::startCameraStreamingOps() { |
| 2995 | ATRACE_CALL(); |
| 2996 | |
| 2997 | if (!mOpsActive) { |
| 2998 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 2999 | return INVALID_OPERATION; |
| 3000 | } |
| 3001 | if (mOpsStreaming) { |
| 3002 | ALOGV("%s: Streaming already active!", __FUNCTION__); |
| 3003 | return OK; |
| 3004 | } |
| 3005 | |
| 3006 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", |
| 3007 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3008 | |
| 3009 | if (mAppOpsManager != nullptr) { |
| 3010 | int32_t mode = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, |
| 3011 | mClientPackageName, /*startIfModeDefault*/ false, mClientFeatureId, |
| 3012 | String16("start camera ") + String16(mCameraIdStr)); |
| 3013 | status_t res = handleAppOpMode(mode); |
| 3014 | if (res != OK) { |
| 3015 | return res; |
| 3016 | } |
| 3017 | } |
| 3018 | |
| 3019 | mOpsStreaming = true; |
| 3020 | |
| 3021 | return OK; |
| 3022 | } |
| 3023 | |
| 3024 | status_t CameraService::BasicClient::finishCameraStreamingOps() { |
| 3025 | ATRACE_CALL(); |
| 3026 | |
| 3027 | if (!mOpsActive) { |
| 3028 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3029 | return INVALID_OPERATION; |
| 3030 | } |
| 3031 | if (!mOpsStreaming) { |
| 3032 | ALOGV("%s: Streaming not active!", __FUNCTION__); |
| 3033 | return OK; |
| 3034 | } |
| 3035 | |
| 3036 | if (mAppOpsManager != nullptr) { |
| 3037 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3038 | mClientPackageName, mClientFeatureId); |
| 3039 | mOpsStreaming = false; |
| 3040 | } |
| 3041 | |
| 3042 | return OK; |
| 3043 | } |
| 3044 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3045 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3046 | ATRACE_CALL(); |
| 3047 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3048 | if (mOpsStreaming) { |
| 3049 | // Make sure we've notified everyone about camera stopping |
| 3050 | finishCameraStreamingOps(); |
| 3051 | } |
| 3052 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3053 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3054 | if (mOpsActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3055 | mOpsActive = false; |
| 3056 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3057 | // This function is called when a client disconnects. This should |
| 3058 | // release the camera, but actually only if it was in a proper |
| 3059 | // functional state, i.e. with status NOT_AVAILABLE |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3060 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3061 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3062 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3063 | // Transition to PRESENT if the camera is not in either of the rejected states |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3064 | sCameraService->updateStatus(StatusInternal::PRESENT, |
| 3065 | mCameraIdStr, rejected); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3066 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3067 | // Always stop watching, even if no camera op is active |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3068 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 3069 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3070 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3071 | mOpsCallback.clear(); |
| 3072 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3073 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid); |
| 3074 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3075 | // Notify listeners of camera open/close status |
| 3076 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); |
| 3077 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3078 | return OK; |
| 3079 | } |
| 3080 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3081 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3082 | ATRACE_CALL(); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3083 | if (mAppOpsManager == nullptr) { |
| 3084 | return; |
| 3085 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3086 | // TODO : add offline camera session case |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3087 | if (op != AppOpsManager::OP_CAMERA) { |
| 3088 | ALOGW("Unexpected app ops notification received: %d", op); |
| 3089 | return; |
| 3090 | } |
| 3091 | |
| 3092 | int32_t res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3093 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3094 | mClientUid, mClientPackageName); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3095 | ALOGV("checkOp returns: %d, %s ", res, |
| 3096 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 3097 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 3098 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 3099 | "UNKNOWN"); |
| 3100 | |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3101 | if (res == AppOpsManager::MODE_ERRORED) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3102 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(), |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3103 | String8(mClientPackageName).string()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3104 | block(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3105 | } else if (res == AppOpsManager::MODE_IGNORED) { |
| 3106 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3107 | bool isCameraPrivacyEnabled = |
| 3108 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled( |
| 3109 | multiuser_get_user_id(mClientUid)); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3110 | ALOGI("Camera %s: Access for \"%s\" has been restricted, isUidTrusted %d, isUidActive %d", |
| 3111 | mCameraIdStr.string(), String8(mClientPackageName).string(), |
| 3112 | mUidIsTrusted, isUidActive); |
| 3113 | // If the calling Uid is trusted (a native service), or the client Uid is active (WAR for |
| 3114 | // b/175320666), the AppOpsManager could return MODE_IGNORED. Do not treat such cases as |
| 3115 | // error. |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3116 | if (!mUidIsTrusted && isUidActive && isCameraPrivacyEnabled) { |
| 3117 | setCameraMute(true); |
| 3118 | } else if (!mUidIsTrusted && !isUidActive) { |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3119 | block(); |
| 3120 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3121 | } else if (res == AppOpsManager::MODE_ALLOWED) { |
| 3122 | setCameraMute(sCameraService->mOverrideCameraMuteMode); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3123 | } |
| 3124 | } |
| 3125 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3126 | void CameraService::BasicClient::block() { |
| 3127 | ATRACE_CALL(); |
| 3128 | |
| 3129 | // Reset the client PID to allow server-initiated disconnect, |
| 3130 | // and to prevent further calls by client. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3131 | mClientPid = CameraThreadState::getCallingPid(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3132 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 3133 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); |
| 3134 | disconnect(); |
| 3135 | } |
| 3136 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3137 | // ---------------------------------------------------------------------------- |
| 3138 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3139 | void CameraService::Client::notifyError(int32_t errorCode, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3140 | const CaptureResultExtras& resultExtras) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 3141 | (void) resultExtras; |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3142 | if (mRemoteCallback != NULL) { |
Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 3143 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; |
| 3144 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { |
| 3145 | api1ErrorCode = CAMERA_ERROR_DISABLED; |
| 3146 | } |
| 3147 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3148 | } else { |
| 3149 | ALOGE("mRemoteCallback is NULL!!"); |
| 3150 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3151 | } |
| 3152 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3153 | // NOTE: function is idempotent |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3154 | binder::Status CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3155 | ALOGV("Client::disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3156 | return BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 3157 | } |
| 3158 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3159 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 3160 | return level == API_1; |
| 3161 | } |
| 3162 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3163 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 3164 | mClient(client) { |
| 3165 | } |
| 3166 | |
| 3167 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 3168 | const String16& packageName) { |
| 3169 | sp<BasicClient> client = mClient.promote(); |
| 3170 | if (client != NULL) { |
| 3171 | client->opChanged(op, packageName); |
| 3172 | } |
| 3173 | } |
| 3174 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3175 | // ---------------------------------------------------------------------------- |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3176 | // UidPolicy |
| 3177 | // ---------------------------------------------------------------------------- |
| 3178 | |
| 3179 | void CameraService::UidPolicy::registerSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3180 | Mutex::Autolock _l(mUidLock); |
| 3181 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3182 | if (mRegistered) return; |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3183 | status_t res = mAm.linkToDeath(this); |
| 3184 | mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3185 | | ActivityManager::UID_OBSERVER_IDLE |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3186 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE, |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3187 | ActivityManager::PROCESS_STATE_UNKNOWN, |
| 3188 | String16("cameraserver")); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3189 | if (res == OK) { |
| 3190 | mRegistered = true; |
| 3191 | ALOGV("UidPolicy: Registered with ActivityManager"); |
| 3192 | } |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | void CameraService::UidPolicy::unregisterSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3196 | Mutex::Autolock _l(mUidLock); |
| 3197 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3198 | mAm.unregisterUidObserver(this); |
| 3199 | mAm.unlinkToDeath(this); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3200 | mRegistered = false; |
| 3201 | mActiveUids.clear(); |
| 3202 | ALOGV("UidPolicy: Unregistered with ActivityManager"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { |
| 3206 | onUidIdle(uid, disabled); |
| 3207 | } |
| 3208 | |
| 3209 | void CameraService::UidPolicy::onUidActive(uid_t uid) { |
| 3210 | Mutex::Autolock _l(mUidLock); |
| 3211 | mActiveUids.insert(uid); |
| 3212 | } |
| 3213 | |
| 3214 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { |
| 3215 | bool deleted = false; |
| 3216 | { |
| 3217 | Mutex::Autolock _l(mUidLock); |
| 3218 | if (mActiveUids.erase(uid) > 0) { |
| 3219 | deleted = true; |
| 3220 | } |
| 3221 | } |
| 3222 | if (deleted) { |
| 3223 | sp<CameraService> service = mService.promote(); |
| 3224 | if (service != nullptr) { |
| 3225 | service->blockClientsForUid(uid); |
| 3226 | } |
| 3227 | } |
| 3228 | } |
| 3229 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3230 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 3231 | int64_t procStateSeq __unused, int32_t capability __unused) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3232 | bool procStateChange = false; |
| 3233 | { |
| 3234 | Mutex::Autolock _l(mUidLock); |
| 3235 | if ((mMonitoredUids.find(uid) != mMonitoredUids.end()) && |
| 3236 | (mMonitoredUids[uid].first != procState)) { |
| 3237 | mMonitoredUids[uid].first = procState; |
| 3238 | procStateChange = true; |
| 3239 | } |
| 3240 | } |
| 3241 | |
| 3242 | if (procStateChange) { |
| 3243 | sp<CameraService> service = mService.promote(); |
| 3244 | if (service != nullptr) { |
| 3245 | service->notifyMonitoredUids(); |
| 3246 | } |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid) { |
| 3251 | Mutex::Autolock _l(mUidLock); |
| 3252 | auto it = mMonitoredUids.find(uid); |
| 3253 | if (it != mMonitoredUids.end()) { |
| 3254 | it->second.second++; |
| 3255 | } else { |
| 3256 | mMonitoredUids.emplace( |
| 3257 | std::pair<uid_t, std::pair<int32_t, size_t>> (uid, |
| 3258 | std::pair<int32_t, size_t> (ActivityManager::PROCESS_STATE_NONEXISTENT, 1))); |
| 3259 | } |
| 3260 | } |
| 3261 | |
| 3262 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid) { |
| 3263 | Mutex::Autolock _l(mUidLock); |
| 3264 | auto it = mMonitoredUids.find(uid); |
| 3265 | if (it != mMonitoredUids.end()) { |
| 3266 | it->second.second--; |
| 3267 | if (it->second.second == 0) { |
| 3268 | mMonitoredUids.erase(it); |
| 3269 | } |
| 3270 | } else { |
| 3271 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 3272 | } |
| 3273 | } |
| 3274 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3275 | bool CameraService::UidPolicy::isUidActive(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3276 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3277 | return isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3278 | } |
| 3279 | |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3280 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; |
| 3281 | static const int64_t kPollUidActiveTimeoutMillis = 50; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3282 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3283 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3284 | // Non-app UIDs are considered always active |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3285 | // If activity manager is unreachable, assume everything is active |
| 3286 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3287 | return true; |
| 3288 | } |
| 3289 | auto it = mOverrideUids.find(uid); |
| 3290 | if (it != mOverrideUids.end()) { |
| 3291 | return it->second; |
| 3292 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3293 | bool active = mActiveUids.find(uid) != mActiveUids.end(); |
| 3294 | if (!active) { |
| 3295 | // We want active UIDs to always access camera with their first attempt since |
| 3296 | // there is no guarantee the app is robustly written and would retry getting |
| 3297 | // the camera on failure. The inverse case is not a problem as we would take |
| 3298 | // camera away soon once we get the callback that the uid is no longer active. |
| 3299 | ActivityManager am; |
| 3300 | // Okay to access with a lock held as UID changes are dispatched without |
| 3301 | // a lock and we are a higher level component. |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3302 | int64_t startTimeMillis = 0; |
| 3303 | do { |
| 3304 | // TODO: Fix this b/109950150! |
| 3305 | // Okay this is a hack. There is a race between the UID turning active and |
| 3306 | // activity being resumed. The proper fix is very risky, so we temporary add |
| 3307 | // some polling which should happen pretty rarely anyway as the race is hard |
| 3308 | // to hit. |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3309 | active = mActiveUids.find(uid) != mActiveUids.end(); |
Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 3310 | if (!active) active = am.isUidActive(uid, callingPackage); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3311 | if (active) { |
| 3312 | break; |
| 3313 | } |
| 3314 | if (startTimeMillis <= 0) { |
| 3315 | startTimeMillis = uptimeMillis(); |
| 3316 | } |
| 3317 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3318 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3319 | if (remainingTimeMillis <= 0) { |
| 3320 | break; |
| 3321 | } |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3322 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); |
| 3323 | |
| 3324 | mUidLock.unlock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3325 | usleep(remainingTimeMillis * 1000); |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3326 | mUidLock.lock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3327 | } while (true); |
| 3328 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3329 | if (active) { |
| 3330 | // Now that we found out the UID is actually active, cache that |
| 3331 | mActiveUids.insert(uid); |
| 3332 | } |
| 3333 | } |
| 3334 | return active; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3335 | } |
| 3336 | |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 3337 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { |
| 3338 | Mutex::Autolock _l(mUidLock); |
| 3339 | return getProcStateLocked(uid); |
| 3340 | } |
| 3341 | |
| 3342 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { |
| 3343 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 3344 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
| 3345 | procState = mMonitoredUids[uid].first; |
| 3346 | } |
| 3347 | return procState; |
| 3348 | } |
| 3349 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3350 | void CameraService::UidPolicy::UidPolicy::addOverrideUid(uid_t uid, |
| 3351 | String16 callingPackage, bool active) { |
| 3352 | updateOverrideUid(uid, callingPackage, active, true); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3353 | } |
| 3354 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3355 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, String16 callingPackage) { |
| 3356 | updateOverrideUid(uid, callingPackage, false, false); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3357 | } |
| 3358 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3359 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 3360 | Mutex::Autolock _l(mUidLock); |
| 3361 | ALOGV("UidPolicy: ActivityManager has died"); |
| 3362 | mRegistered = false; |
| 3363 | mActiveUids.clear(); |
| 3364 | } |
| 3365 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3366 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, String16 callingPackage, |
| 3367 | bool active, bool insert) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3368 | bool wasActive = false; |
| 3369 | bool isActive = false; |
| 3370 | { |
| 3371 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3372 | wasActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3373 | mOverrideUids.erase(uid); |
| 3374 | if (insert) { |
| 3375 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 3376 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3377 | isActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3378 | } |
| 3379 | if (wasActive != isActive && !isActive) { |
| 3380 | sp<CameraService> service = mService.promote(); |
| 3381 | if (service != nullptr) { |
| 3382 | service->blockClientsForUid(uid); |
| 3383 | } |
| 3384 | } |
| 3385 | } |
| 3386 | |
| 3387 | // ---------------------------------------------------------------------------- |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3388 | // SensorPrivacyPolicy |
| 3389 | // ---------------------------------------------------------------------------- |
| 3390 | void CameraService::SensorPrivacyPolicy::registerSelf() { |
| 3391 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3392 | if (mRegistered) { |
| 3393 | return; |
| 3394 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3395 | hasCameraPrivacyFeature(); // Called so the result is cached |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 3396 | mSpm.addSensorPrivacyListener(this); |
| 3397 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); |
| 3398 | status_t res = mSpm.linkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3399 | if (res == OK) { |
| 3400 | mRegistered = true; |
| 3401 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); |
| 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { |
| 3406 | Mutex::Autolock _l(mSensorPrivacyLock); |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 3407 | mSpm.removeSensorPrivacyListener(this); |
| 3408 | mSpm.unlinkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3409 | mRegistered = false; |
| 3410 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); |
| 3411 | } |
| 3412 | |
| 3413 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
| 3414 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3415 | return mSensorPrivacyEnabled; |
| 3416 | } |
| 3417 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3418 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled(userid_t userId) { |
| 3419 | if (!hasCameraPrivacyFeature()) { |
| 3420 | return false; |
| 3421 | } |
| 3422 | return mSpm.isIndividualSensorPrivacyEnabled(userId, |
| 3423 | SensorPrivacyManager::INDIVIDUAL_SENSOR_CAMERA); |
| 3424 | } |
| 3425 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3426 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) { |
| 3427 | { |
| 3428 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3429 | mSensorPrivacyEnabled = enabled; |
| 3430 | } |
| 3431 | // if sensor privacy is enabled then block all clients from accessing the camera |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3432 | if (enabled) { |
| 3433 | sp<CameraService> service = mService.promote(); |
| 3434 | if (service != nullptr) { |
| 3435 | service->blockAllClients(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3436 | } |
| 3437 | } |
| 3438 | return binder::Status::ok(); |
| 3439 | } |
| 3440 | |
| 3441 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 3442 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3443 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); |
| 3444 | mRegistered = false; |
| 3445 | } |
| 3446 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3447 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { |
Evan Severson | 671806e | 2021-04-20 16:44:53 -0700 | [diff] [blame] | 3448 | return mSpm.supportsSensorToggle(SensorPrivacyManager::INDIVIDUAL_SENSOR_CAMERA); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3449 | } |
| 3450 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3451 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3452 | // CameraState |
| 3453 | // ---------------------------------------------------------------------------- |
| 3454 | |
| 3455 | CameraService::CameraState::CameraState(const String8& id, int cost, |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3456 | const std::set<String8>& conflicting, SystemCameraKind systemCameraKind) : mId(id), |
| 3457 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), |
| 3458 | mSystemCameraKind(systemCameraKind) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3459 | |
| 3460 | CameraService::CameraState::~CameraState() {} |
| 3461 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3462 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3463 | Mutex::Autolock lock(mStatusLock); |
| 3464 | return mStatus; |
| 3465 | } |
| 3466 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3467 | std::vector<String8> CameraService::CameraState::getUnavailablePhysicalIds() const { |
| 3468 | Mutex::Autolock lock(mStatusLock); |
| 3469 | std::vector<String8> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); |
| 3470 | return res; |
| 3471 | } |
| 3472 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3473 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 3474 | return mShimParams; |
| 3475 | } |
| 3476 | |
| 3477 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 3478 | mShimParams = params; |
| 3479 | } |
| 3480 | |
| 3481 | int CameraService::CameraState::getCost() const { |
| 3482 | return mCost; |
| 3483 | } |
| 3484 | |
| 3485 | std::set<String8> CameraService::CameraState::getConflicting() const { |
| 3486 | return mConflicting; |
| 3487 | } |
| 3488 | |
| 3489 | String8 CameraService::CameraState::getId() const { |
| 3490 | return mId; |
| 3491 | } |
| 3492 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3493 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { |
| 3494 | return mSystemCameraKind; |
| 3495 | } |
| 3496 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3497 | bool CameraService::CameraState::addUnavailablePhysicalId(const String8& physicalId) { |
| 3498 | Mutex::Autolock lock(mStatusLock); |
| 3499 | auto result = mUnavailablePhysicalIds.insert(physicalId); |
| 3500 | return result.second; |
| 3501 | } |
| 3502 | |
| 3503 | bool CameraService::CameraState::removeUnavailablePhysicalId(const String8& physicalId) { |
| 3504 | Mutex::Autolock lock(mStatusLock); |
| 3505 | auto count = mUnavailablePhysicalIds.erase(physicalId); |
| 3506 | return count > 0; |
| 3507 | } |
| 3508 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3509 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3510 | // ClientEventListener |
| 3511 | // ---------------------------------------------------------------------------- |
| 3512 | |
| 3513 | void CameraService::ClientEventListener::onClientAdded( |
| 3514 | const resource_policy::ClientDescriptor<String8, |
| 3515 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 3516 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3517 | if (basicClient.get() != nullptr) { |
| 3518 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 3519 | notifier.noteStartCamera(descriptor.getKey(), |
| 3520 | static_cast<int>(basicClient->getClientUid())); |
| 3521 | } |
| 3522 | } |
| 3523 | |
| 3524 | void CameraService::ClientEventListener::onClientRemoved( |
| 3525 | const resource_policy::ClientDescriptor<String8, |
| 3526 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 3527 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3528 | if (basicClient.get() != nullptr) { |
| 3529 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 3530 | notifier.noteStopCamera(descriptor.getKey(), |
| 3531 | static_cast<int>(basicClient->getClientUid())); |
| 3532 | } |
| 3533 | } |
| 3534 | |
| 3535 | |
| 3536 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3537 | // CameraClientManager |
| 3538 | // ---------------------------------------------------------------------------- |
| 3539 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3540 | CameraService::CameraClientManager::CameraClientManager() { |
| 3541 | setListener(std::make_shared<ClientEventListener>()); |
| 3542 | } |
| 3543 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3544 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 3545 | |
| 3546 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
| 3547 | const String8& id) const { |
| 3548 | auto descriptor = get(id); |
| 3549 | if (descriptor == nullptr) { |
| 3550 | return sp<BasicClient>{nullptr}; |
| 3551 | } |
| 3552 | return descriptor->getValue(); |
| 3553 | } |
| 3554 | |
| 3555 | String8 CameraService::CameraClientManager::toString() const { |
| 3556 | auto all = getAll(); |
| 3557 | String8 ret("["); |
| 3558 | bool hasAny = false; |
| 3559 | for (auto& i : all) { |
| 3560 | hasAny = true; |
| 3561 | String8 key = i->getKey(); |
| 3562 | int32_t cost = i->getCost(); |
| 3563 | int32_t pid = i->getOwnerId(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3564 | int32_t score = i->getPriority().getScore(); |
| 3565 | int32_t state = i->getPriority().getState(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3566 | auto conflicting = i->getConflicting(); |
| 3567 | auto clientSp = i->getValue(); |
| 3568 | String8 packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 3569 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3570 | if (clientSp.get() != nullptr) { |
| 3571 | packageName = String8{clientSp->getPackageName()}; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3572 | uid_t clientUid = clientSp->getClientUid(); |
| 3573 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3574 | } |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3575 | ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" |
| 3576 | PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3577 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3578 | if (clientSp.get() != nullptr) { |
| 3579 | ret.appendFormat("User Id: %d, ", clientUserId); |
| 3580 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3581 | if (packageName.size() != 0) { |
| 3582 | ret.appendFormat("Client Package Name: %s", packageName.string()); |
| 3583 | } |
| 3584 | |
| 3585 | ret.append(", Conflicting Client Devices: {"); |
| 3586 | for (auto& j : conflicting) { |
| 3587 | ret.appendFormat("%s, ", j.string()); |
| 3588 | } |
| 3589 | ret.append("})"); |
| 3590 | } |
| 3591 | if (hasAny) ret.append("\n"); |
| 3592 | ret.append("]\n"); |
| 3593 | return ret; |
| 3594 | } |
| 3595 | |
| 3596 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
| 3597 | const String8& key, const sp<BasicClient>& value, int32_t cost, |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3598 | const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId, |
| 3599 | int32_t state) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3600 | |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 3601 | bool isVendorClient = getCurrentServingCall() == BinderCallType::HWBINDER; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 3602 | int32_t score_adj = isVendorClient ? kVendorClientScore : score; |
| 3603 | int32_t state_adj = isVendorClient ? kVendorClientState: state; |
| 3604 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3605 | return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>( |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 3606 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, isVendorClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
| 3610 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) { |
| 3611 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3612 | partial->getConflicting(), partial->getPriority().getScore(), |
| 3613 | partial->getOwnerId(), partial->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3617 | |
| 3618 | static const int kDumpLockRetries = 50; |
| 3619 | static const int kDumpLockSleep = 60000; |
| 3620 | |
| 3621 | static bool tryLock(Mutex& mutex) |
| 3622 | { |
| 3623 | bool locked = false; |
| 3624 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 3625 | if (mutex.tryLock() == NO_ERROR) { |
| 3626 | locked = true; |
| 3627 | break; |
| 3628 | } |
| 3629 | usleep(kDumpLockSleep); |
| 3630 | } |
| 3631 | return locked; |
| 3632 | } |
| 3633 | |
| 3634 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3635 | ATRACE_CALL(); |
| 3636 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3637 | if (checkCallingPermission(sDumpPermission) == false) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3638 | dprintf(fd, "Permission Denial: can't dump CameraService from pid=%d, uid=%d\n", |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3639 | CameraThreadState::getCallingPid(), |
| 3640 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3641 | return NO_ERROR; |
| 3642 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3643 | bool locked = tryLock(mServiceLock); |
| 3644 | // failed to lock - CameraService is probably deadlocked |
| 3645 | if (!locked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3646 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3647 | } |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3648 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3649 | if (!mInitialized) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3650 | dprintf(fd, "!! No camera HAL available !!\n"); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 3651 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3652 | // Dump event log for error information |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3653 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3654 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3655 | if (locked) mServiceLock.unlock(); |
| 3656 | return NO_ERROR; |
| 3657 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3658 | dprintf(fd, "\n== Service global info: ==\n\n"); |
| 3659 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3660 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 3661 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", |
| 3662 | mNormalDeviceIdsWithoutSystemCamera.size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3663 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { |
| 3664 | dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); |
| 3665 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3666 | String8 activeClientString = mActiveClientManager.toString(); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3667 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string()); |
| 3668 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3669 | |
| 3670 | dumpEventLog(fd); |
| 3671 | |
| 3672 | bool stateLocked = tryLock(mCameraStatesLock); |
| 3673 | if (!stateLocked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3674 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3675 | } |
| 3676 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 3677 | int argSize = args.size(); |
| 3678 | for (int i = 0; i < argSize; i++) { |
| 3679 | if (args[i] == TagMonitor::kMonitorOption) { |
| 3680 | if (i + 1 < argSize) { |
| 3681 | mMonitorTags = String8(args[i + 1]); |
| 3682 | } |
| 3683 | break; |
| 3684 | } |
| 3685 | } |
| 3686 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3687 | for (auto& state : mCameraStates) { |
| 3688 | String8 cameraId = state.first; |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3689 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3690 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3691 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3692 | CameraParameters p = state.second->getShimParams(); |
| 3693 | if (!p.isEmpty()) { |
| 3694 | dprintf(fd, " Camera1 API shim is using parameters:\n "); |
| 3695 | p.dump(fd, args); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 3699 | if (clientDescriptor != nullptr) { |
| 3700 | dprintf(fd, " Device %s is open. Client instance dump:\n", |
| 3701 | cameraId.string()); |
| 3702 | dprintf(fd, " Client priority score: %d state: %d\n", |
| 3703 | clientDescriptor->getPriority().getScore(), |
| 3704 | clientDescriptor->getPriority().getState()); |
| 3705 | dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 3706 | |
| 3707 | auto client = clientDescriptor->getValue(); |
| 3708 | dprintf(fd, " Client package: %s\n", |
| 3709 | String8(client->getPackageName()).string()); |
| 3710 | |
| 3711 | client->dumpClient(fd, args); |
| 3712 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3713 | dprintf(fd, " Device %s is closed, no client instance\n", |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3714 | cameraId.string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3715 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3716 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | if (stateLocked) mCameraStatesLock.unlock(); |
| 3720 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3721 | if (locked) mServiceLock.unlock(); |
| 3722 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 3723 | mCameraProviderManager->dump(fd, args); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3724 | |
| 3725 | dprintf(fd, "\n== Vendor tags: ==\n\n"); |
| 3726 | |
| 3727 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 3728 | if (desc == NULL) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 3729 | sp<VendorTagDescriptorCache> cache = |
| 3730 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 3731 | if (cache == NULL) { |
| 3732 | dprintf(fd, "No vendor tags.\n"); |
| 3733 | } else { |
| 3734 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 3735 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3736 | } else { |
| 3737 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 3738 | } |
| 3739 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3740 | // Dump camera traces if there were any |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3741 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3742 | camera3::CameraTraces::dump(fd, args); |
| 3743 | |
| 3744 | // Process dump arguments, if any |
| 3745 | int n = args.size(); |
| 3746 | String16 verboseOption("-v"); |
| 3747 | String16 unreachableOption("--unreachable"); |
| 3748 | for (int i = 0; i < n; i++) { |
| 3749 | if (args[i] == verboseOption) { |
| 3750 | // change logging level |
| 3751 | if (i + 1 >= n) continue; |
| 3752 | String8 levelStr(args[i+1]); |
| 3753 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3754 | dprintf(fd, "\nSetting log level to %d.\n", level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3755 | setLogLevel(level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3756 | } else if (args[i] == unreachableOption) { |
| 3757 | // Dump memory analysis |
| 3758 | // TODO - should limit be an argument parameter? |
| 3759 | UnreachableMemoryInfo info; |
| 3760 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); |
| 3761 | if (!success) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3762 | dprintf(fd, "\n== Unable to dump unreachable memory. " |
| 3763 | "Try disabling SELinux enforcement. ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3764 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3765 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3766 | std::string s = info.ToString(/*log_contents*/ true); |
| 3767 | write(fd, s.c_str(), s.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3768 | } |
| 3769 | } |
| 3770 | } |
| 3771 | return NO_ERROR; |
| 3772 | } |
| 3773 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3774 | void CameraService::dumpEventLog(int fd) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3775 | dprintf(fd, "\n== Camera service events log (most recent at top): ==\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3776 | |
| 3777 | Mutex::Autolock l(mLogLock); |
| 3778 | for (const auto& msg : mEventLog) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3779 | dprintf(fd, " %s\n", msg.string()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3783 | dprintf(fd, " ...\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3784 | } else if (mEventLog.size() == 0) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3785 | dprintf(fd, " [no events yet]\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3786 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3787 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3788 | } |
| 3789 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3790 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3791 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3792 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 3793 | if (mTorchClientMap[i] == who) { |
| 3794 | // turn off the torch mode that was turned on by dead client |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3795 | String8 cameraId = mTorchClientMap.keyAt(i); |
| 3796 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 3797 | if (res) { |
| 3798 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 3799 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 3800 | return; |
| 3801 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3802 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3803 | break; |
| 3804 | } |
| 3805 | } |
| 3806 | } |
| 3807 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3808 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3809 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 3810 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3811 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 3812 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 3813 | */ |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 3814 | // PID here is approximate and can be wrong. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3815 | logClientDied(CameraThreadState::getCallingPid(), String8("Binder died unexpectedly")); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3816 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3817 | // check torch client |
| 3818 | handleTorchClientBinderDied(who); |
| 3819 | |
| 3820 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3821 | if(!evictClientIdByRemote(who)) { |
| 3822 | ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3823 | return; |
| 3824 | } |
| 3825 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3826 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 3827 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3828 | } |
| 3829 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3830 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3831 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3832 | } |
| 3833 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3834 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId, |
| 3835 | std::initializer_list<StatusInternal> rejectSourceStates) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3836 | // Do not lock mServiceLock here or can get into a deadlock from |
| 3837 | // connect() -> disconnect -> updateStatus |
| 3838 | |
| 3839 | auto state = getCameraState(cameraId); |
| 3840 | |
| 3841 | if (state == nullptr) { |
| 3842 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 3843 | cameraId.string()); |
| 3844 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3845 | } |
| 3846 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3847 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 3848 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 3849 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 3850 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 3851 | return; |
| 3852 | } |
Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 3853 | bool supportsHAL3 = false; |
| 3854 | // supportsCameraApi also holds mInterfaceMutex, we can't call it in the |
| 3855 | // HIDL onStatusChanged wrapper call (we'll hold mStatusListenerLock and |
| 3856 | // mInterfaceMutex together, which can lead to deadlocks) |
| 3857 | binder::Status sRet = |
| 3858 | supportsCameraApi(String16(cameraId), hardware::ICameraService::API_VERSION_2, |
| 3859 | &supportsHAL3); |
| 3860 | if (!sRet.isOk()) { |
| 3861 | ALOGW("%s: Failed to determine if device supports HAL3 %s, supportsCameraApi call failed", |
| 3862 | __FUNCTION__, cameraId.string()); |
| 3863 | return; |
| 3864 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3865 | |
| 3866 | // Collect the logical cameras without holding mStatusLock in updateStatus |
| 3867 | // as that can lead to a deadlock(b/162192331). |
| 3868 | auto logicalCameraIds = getLogicalCameras(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3869 | // Update the status for this camera state, then send the onStatusChangedCallbacks to each |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3870 | // of the listeners with both the mStatusLock and mStatusListenerLock held |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3871 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, &supportsHAL3, |
| 3872 | &logicalCameraIds] |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3873 | (const String8& cameraId, StatusInternal status) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3874 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3875 | if (status != StatusInternal::ENUMERATING) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 3876 | // Update torch status if it has a flash unit. |
| 3877 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3878 | TorchModeStatus torchStatus; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 3879 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 3880 | NAME_NOT_FOUND) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3881 | TorchModeStatus newTorchStatus = |
| 3882 | status == StatusInternal::PRESENT ? |
| 3883 | TorchModeStatus::AVAILABLE_OFF : |
| 3884 | TorchModeStatus::NOT_AVAILABLE; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 3885 | if (torchStatus != newTorchStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3886 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 3887 | } |
| 3888 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | Mutex::Autolock lock(mStatusListenerLock); |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3892 | notifyPhysicalCameraStatusLocked(mapToInterface(status), String16(cameraId), |
| 3893 | logicalCameraIds, deviceKind); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3894 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3895 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 3896 | bool isVendorListener = listener->isVendorListener(); |
| 3897 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, |
| 3898 | listener->getListenerPid(), listener->getListenerUid()) || |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 3899 | (isVendorListener && !supportsHAL3)) { |
Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 3900 | ALOGV("Skipping discovery callback for system-only camera/HAL1 device %s", |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3901 | cameraId.c_str()); |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3902 | continue; |
| 3903 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3904 | listener->getListener()->onStatusChanged(mapToInterface(status), |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3905 | String16(cameraId)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3906 | } |
| 3907 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3908 | } |
| 3909 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3910 | void CameraService::updateOpenCloseStatus(const String8& cameraId, bool open, |
| 3911 | const String16& clientPackageName) { |
| 3912 | Mutex::Autolock lock(mStatusListenerLock); |
| 3913 | |
| 3914 | for (const auto& it : mListenerList) { |
| 3915 | if (!it->isOpenCloseCallbackAllowed()) { |
| 3916 | continue; |
| 3917 | } |
| 3918 | |
| 3919 | binder::Status ret; |
| 3920 | String16 cameraId64(cameraId); |
| 3921 | if (open) { |
| 3922 | ret = it->getListener()->onCameraOpened(cameraId64, clientPackageName); |
| 3923 | } else { |
| 3924 | ret = it->getListener()->onCameraClosed(cameraId64); |
| 3925 | } |
| 3926 | if (!ret.isOk()) { |
| 3927 | ALOGE("%s: Failed to trigger onCameraOpened/onCameraClosed callback: %d", __FUNCTION__, |
| 3928 | ret.exceptionCode()); |
| 3929 | } |
| 3930 | } |
| 3931 | } |
| 3932 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3933 | template<class Func> |
| 3934 | void CameraService::CameraState::updateStatus(StatusInternal status, |
| 3935 | const String8& cameraId, |
| 3936 | std::initializer_list<StatusInternal> rejectSourceStates, |
| 3937 | Func onStatusUpdatedLocked) { |
| 3938 | Mutex::Autolock lock(mStatusLock); |
| 3939 | StatusInternal oldStatus = mStatus; |
| 3940 | mStatus = status; |
| 3941 | |
| 3942 | if (oldStatus == status) { |
| 3943 | return; |
| 3944 | } |
| 3945 | |
| 3946 | ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__, |
| 3947 | cameraId.string(), oldStatus, status); |
| 3948 | |
| 3949 | if (oldStatus == StatusInternal::NOT_PRESENT && |
| 3950 | (status != StatusInternal::PRESENT && |
| 3951 | status != StatusInternal::ENUMERATING)) { |
| 3952 | |
| 3953 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 3954 | __FUNCTION__); |
| 3955 | mStatus = oldStatus; |
| 3956 | return; |
| 3957 | } |
| 3958 | |
| 3959 | /** |
| 3960 | * Sometimes we want to conditionally do a transition. |
| 3961 | * For example if a client disconnects, we want to go to PRESENT |
| 3962 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 3963 | */ |
| 3964 | for (auto& rejectStatus : rejectSourceStates) { |
| 3965 | if (oldStatus == rejectStatus) { |
| 3966 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
| 3967 | "state was was in one of the bad states.", __FUNCTION__, cameraId.string()); |
| 3968 | mStatus = oldStatus; |
| 3969 | return; |
| 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | onStatusUpdatedLocked(cameraId, status); |
| 3974 | } |
| 3975 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3976 | status_t CameraService::getTorchStatusLocked( |
| 3977 | const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3978 | TorchModeStatus *status) const { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3979 | if (!status) { |
| 3980 | return BAD_VALUE; |
| 3981 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3982 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 3983 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3984 | // invalid camera ID or the camera doesn't have a flash unit |
| 3985 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3986 | } |
| 3987 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3988 | *status = mTorchStatusMap.valueAt(index); |
| 3989 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3990 | } |
| 3991 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3992 | status_t CameraService::setTorchStatusLocked(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3993 | TorchModeStatus status) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3994 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 3995 | if (index == NAME_NOT_FOUND) { |
| 3996 | return BAD_VALUE; |
| 3997 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3998 | mTorchStatusMap.editValueAt(index) = status; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3999 | |
| 4000 | return OK; |
| 4001 | } |
| 4002 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4003 | std::list<String16> CameraService::getLogicalCameras( |
| 4004 | const String8& physicalCameraId) { |
| 4005 | std::list<String16> retList; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4006 | Mutex::Autolock lock(mCameraStatesLock); |
| 4007 | for (const auto& state : mCameraStates) { |
| 4008 | std::vector<std::string> physicalCameraIds; |
| 4009 | if (!mCameraProviderManager->isLogicalCamera(state.first.c_str(), &physicalCameraIds)) { |
| 4010 | // This is not a logical multi-camera. |
| 4011 | continue; |
| 4012 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4013 | if (std::find(physicalCameraIds.begin(), physicalCameraIds.end(), physicalCameraId.c_str()) |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4014 | == physicalCameraIds.end()) { |
| 4015 | // cameraId is not a physical camera of this logical multi-camera. |
| 4016 | continue; |
| 4017 | } |
| 4018 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4019 | retList.emplace_back(String16(state.first)); |
| 4020 | } |
| 4021 | return retList; |
| 4022 | } |
| 4023 | |
| 4024 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, |
| 4025 | const String16& physicalCameraId, const std::list<String16>& logicalCameraIds, |
| 4026 | SystemCameraKind deviceKind) { |
| 4027 | // mStatusListenerLock is expected to be locked |
| 4028 | for (const auto& logicalCameraId : logicalCameraIds) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4029 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4030 | // Note: we check only the deviceKind of the physical camera id |
| 4031 | // since, logical camera ids and their physical camera ids are |
| 4032 | // guaranteed to have the same system camera kind. |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 4033 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 4034 | listener->getListenerPid(), listener->getListenerUid())) { |
| 4035 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4036 | String8(physicalCameraId).c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 4037 | continue; |
| 4038 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4039 | listener->getListener()->onPhysicalCameraStatusChanged(status, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4040 | logicalCameraId, physicalCameraId); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4041 | } |
| 4042 | } |
| 4043 | } |
| 4044 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4045 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4046 | void CameraService::blockClientsForUid(uid_t uid) { |
| 4047 | const auto clients = mActiveClientManager.getAll(); |
| 4048 | for (auto& current : clients) { |
| 4049 | if (current != nullptr) { |
| 4050 | const auto basicClient = current->getValue(); |
| 4051 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { |
| 4052 | basicClient->block(); |
| 4053 | } |
| 4054 | } |
| 4055 | } |
| 4056 | } |
| 4057 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4058 | void CameraService::blockAllClients() { |
| 4059 | const auto clients = mActiveClientManager.getAll(); |
| 4060 | for (auto& current : clients) { |
| 4061 | if (current != nullptr) { |
| 4062 | const auto basicClient = current->getValue(); |
| 4063 | if (basicClient.get() != nullptr) { |
| 4064 | basicClient->block(); |
| 4065 | } |
| 4066 | } |
| 4067 | } |
| 4068 | } |
| 4069 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4070 | // NOTE: This is a remote API - make sure all args are validated |
| 4071 | status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) { |
| 4072 | if (!checkCallingPermission(sManageCameraPermission, nullptr, nullptr)) { |
| 4073 | return PERMISSION_DENIED; |
| 4074 | } |
| 4075 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 4076 | return BAD_VALUE; |
| 4077 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4078 | if (args.size() >= 3 && args[0] == String16("set-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4079 | return handleSetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4080 | } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4081 | return handleResetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4082 | } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4083 | return handleGetUidState(args, out, err); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4084 | } else if (args.size() >= 2 && args[0] == String16("set-rotate-and-crop")) { |
| 4085 | return handleSetRotateAndCrop(args); |
| 4086 | } else if (args.size() >= 1 && args[0] == String16("get-rotate-and-crop")) { |
| 4087 | return handleGetRotateAndCrop(out); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4088 | } else if (args.size() >= 2 && args[0] == String16("set-image-dump-mask")) { |
| 4089 | return handleSetImageDumpMask(args); |
| 4090 | } else if (args.size() >= 1 && args[0] == String16("get-image-dump-mask")) { |
| 4091 | return handleGetImageDumpMask(out); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 4092 | } else if (args.size() >= 2 && args[0] == String16("set-camera-mute")) { |
| 4093 | return handleSetCameraMute(args); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4094 | } else if (args.size() == 1 && args[0] == String16("help")) { |
| 4095 | printHelp(out); |
| 4096 | return NO_ERROR; |
| 4097 | } |
| 4098 | printHelp(err); |
| 4099 | return BAD_VALUE; |
| 4100 | } |
| 4101 | |
| 4102 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4103 | String16 packageName = args[1]; |
| 4104 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4105 | bool active = false; |
| 4106 | if (args[2] == String16("active")) { |
| 4107 | active = true; |
| 4108 | } else if ((args[2] != String16("idle"))) { |
| 4109 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string()); |
| 4110 | return BAD_VALUE; |
| 4111 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4112 | |
| 4113 | int userId = 0; |
| 4114 | if (args.size() >= 5 && args[3] == String16("--user")) { |
| 4115 | userId = atoi(String8(args[4])); |
| 4116 | } |
| 4117 | |
| 4118 | uid_t uid; |
| 4119 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
| 4120 | return BAD_VALUE; |
| 4121 | } |
| 4122 | |
| 4123 | mUidPolicy->addOverrideUid(uid, packageName, active); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4124 | return NO_ERROR; |
| 4125 | } |
| 4126 | |
| 4127 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4128 | String16 packageName = args[1]; |
| 4129 | |
| 4130 | int userId = 0; |
| 4131 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 4132 | userId = atoi(String8(args[3])); |
| 4133 | } |
| 4134 | |
| 4135 | uid_t uid; |
| 4136 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4137 | return BAD_VALUE; |
| 4138 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4139 | |
| 4140 | mUidPolicy->removeOverrideUid(uid, packageName); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4141 | return NO_ERROR; |
| 4142 | } |
| 4143 | |
| 4144 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4145 | String16 packageName = args[1]; |
| 4146 | |
| 4147 | int userId = 0; |
| 4148 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 4149 | userId = atoi(String8(args[3])); |
| 4150 | } |
| 4151 | |
| 4152 | uid_t uid; |
| 4153 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4154 | return BAD_VALUE; |
| 4155 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4156 | |
| 4157 | if (mUidPolicy->isUidActive(uid, packageName)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4158 | return dprintf(out, "active\n"); |
| 4159 | } else { |
| 4160 | return dprintf(out, "idle\n"); |
| 4161 | } |
| 4162 | } |
| 4163 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4164 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { |
| 4165 | int rotateValue = atoi(String8(args[1])); |
| 4166 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || |
| 4167 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; |
| 4168 | Mutex::Autolock lock(mServiceLock); |
| 4169 | |
| 4170 | mOverrideRotateAndCropMode = rotateValue; |
| 4171 | |
| 4172 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; |
| 4173 | |
| 4174 | const auto clients = mActiveClientManager.getAll(); |
| 4175 | for (auto& current : clients) { |
| 4176 | if (current != nullptr) { |
| 4177 | const auto basicClient = current->getValue(); |
| 4178 | if (basicClient.get() != nullptr) { |
| 4179 | basicClient->setRotateAndCropOverride(rotateValue); |
| 4180 | } |
| 4181 | } |
| 4182 | } |
| 4183 | |
| 4184 | return OK; |
| 4185 | } |
| 4186 | |
| 4187 | status_t CameraService::handleGetRotateAndCrop(int out) { |
| 4188 | Mutex::Autolock lock(mServiceLock); |
| 4189 | |
| 4190 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); |
| 4191 | } |
| 4192 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4193 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { |
| 4194 | char *endPtr; |
| 4195 | errno = 0; |
| 4196 | String8 maskString8 = String8(args[1]); |
| 4197 | long maskValue = strtol(maskString8.c_str(), &endPtr, 10); |
| 4198 | |
| 4199 | if (errno != 0) return BAD_VALUE; |
| 4200 | if (endPtr != maskString8.c_str() + maskString8.size()) return BAD_VALUE; |
| 4201 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; |
| 4202 | |
| 4203 | Mutex::Autolock lock(mServiceLock); |
| 4204 | |
| 4205 | mImageDumpMask = maskValue; |
| 4206 | |
| 4207 | return OK; |
| 4208 | } |
| 4209 | |
| 4210 | status_t CameraService::handleGetImageDumpMask(int out) { |
| 4211 | Mutex::Autolock lock(mServiceLock); |
| 4212 | |
| 4213 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); |
| 4214 | } |
| 4215 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 4216 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { |
| 4217 | int muteValue = strtol(String8(args[1]), nullptr, 10); |
| 4218 | if (errno != 0) return BAD_VALUE; |
| 4219 | |
| 4220 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; |
| 4221 | Mutex::Autolock lock(mServiceLock); |
| 4222 | |
| 4223 | mOverrideCameraMuteMode = (muteValue == 1); |
| 4224 | |
| 4225 | const auto clients = mActiveClientManager.getAll(); |
| 4226 | for (auto& current : clients) { |
| 4227 | if (current != nullptr) { |
| 4228 | const auto basicClient = current->getValue(); |
| 4229 | if (basicClient.get() != nullptr) { |
| 4230 | if (basicClient->supportsCameraMute()) { |
| 4231 | basicClient->setCameraMute(mOverrideCameraMuteMode); |
| 4232 | } |
| 4233 | } |
| 4234 | } |
| 4235 | } |
| 4236 | |
| 4237 | return OK; |
| 4238 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4239 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4240 | status_t CameraService::printHelp(int out) { |
| 4241 | return dprintf(out, "Camera service commands:\n" |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4242 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 4243 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 4244 | " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n" |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4245 | " set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" |
| 4246 | " Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" |
| 4247 | " get-rotate-and-crop returns the current override rotate-and-crop value\n" |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4248 | " set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" |
| 4249 | " Valid values 0=OFF, 1=ON for JPEG\n" |
| 4250 | " get-image-dump-mask returns the current image-dump-mask value\n" |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 4251 | " set-camera-mute <0/1> enable or disable camera muting\n" |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4252 | " help print this message\n"); |
| 4253 | } |
| 4254 | |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4255 | int32_t CameraService::updateAudioRestriction() { |
| 4256 | Mutex::Autolock lock(mServiceLock); |
| 4257 | return updateAudioRestrictionLocked(); |
| 4258 | } |
| 4259 | |
| 4260 | int32_t CameraService::updateAudioRestrictionLocked() { |
| 4261 | int32_t mode = 0; |
| 4262 | // iterate through all active client |
| 4263 | for (const auto& i : mActiveClientManager.getAll()) { |
| 4264 | const auto clientSp = i->getValue(); |
| 4265 | mode |= clientSp->getAudioRestriction(); |
| 4266 | } |
| 4267 | |
| 4268 | bool modeChanged = (mAudioRestriction != mode); |
| 4269 | mAudioRestriction = mode; |
| 4270 | if (modeChanged) { |
| 4271 | mAppOps.setCameraAudioRestriction(mode); |
| 4272 | } |
| 4273 | return mode; |
| 4274 | } |
| 4275 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4276 | }; // namespace android |