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