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