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