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