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