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