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