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