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