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 | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 31 | #include <binder/AppOpsManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 32 | #include <binder/IPCThreadState.h> |
| 33 | #include <binder/IServiceManager.h> |
| 34 | #include <binder/MemoryBase.h> |
| 35 | #include <binder/MemoryHeapBase.h> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 36 | #include <binder/ProcessInfoService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | #include <cutils/atomic.h> |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 38 | #include <cutils/properties.h> |
Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 39 | #include <gui/Surface.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | #include <hardware/hardware.h> |
| 41 | #include <media/AudioSystem.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 42 | #include <media/IMediaHTTPService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 43 | #include <media/mediaplayer.h> |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 44 | #include <mediautils/BatteryNotifier.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 45 | #include <utils/Errors.h> |
| 46 | #include <utils/Log.h> |
| 47 | #include <utils/String16.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 48 | #include <utils/Trace.h> |
| 49 | #include <system/camera_vendor_tags.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 50 | #include <system/camera_metadata.h> |
| 51 | #include <system/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
| 53 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 54 | #include "api1/CameraClient.h" |
| 55 | #include "api1/Camera2Client.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 56 | #include "api2/CameraDeviceClient.h" |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 57 | #include "utils/CameraTraces.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame^] | 59 | namespace { |
| 60 | const char* kPermissionServiceName = "permission"; |
| 61 | }; // namespace anonymous |
| 62 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 63 | namespace android { |
| 64 | |
| 65 | // ---------------------------------------------------------------------------- |
| 66 | // Logging support -- this is for debugging only |
| 67 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 68 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 69 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 70 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 71 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 72 | |
| 73 | static void setLogLevel(int level) { |
| 74 | android_atomic_write(level, &gLogLevel); |
| 75 | } |
| 76 | |
| 77 | // ---------------------------------------------------------------------------- |
| 78 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 79 | extern "C" { |
| 80 | static void camera_device_status_change( |
| 81 | const struct camera_module_callbacks* callbacks, |
| 82 | int camera_id, |
| 83 | int new_status) { |
| 84 | sp<CameraService> cs = const_cast<CameraService*>( |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 85 | static_cast<const CameraService*>(callbacks)); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 86 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 87 | cs->onDeviceStatusChanged(static_cast<camera_device_status_t>(camera_id), |
| 88 | static_cast<camera_device_status_t>(new_status)); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 89 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 90 | |
| 91 | static void torch_mode_status_change( |
| 92 | const struct camera_module_callbacks* callbacks, |
| 93 | const char* camera_id, |
| 94 | int new_status) { |
| 95 | if (!callbacks || !camera_id) { |
| 96 | ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__, |
| 97 | callbacks, camera_id); |
| 98 | } |
| 99 | sp<CameraService> cs = const_cast<CameraService*>( |
| 100 | static_cast<const CameraService*>(callbacks)); |
| 101 | |
| 102 | ICameraServiceListener::TorchStatus status; |
| 103 | switch (new_status) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 104 | case TORCH_MODE_STATUS_NOT_AVAILABLE: |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 105 | status = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 106 | break; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 107 | case TORCH_MODE_STATUS_AVAILABLE_OFF: |
| 108 | status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 109 | break; |
| 110 | case TORCH_MODE_STATUS_AVAILABLE_ON: |
| 111 | status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 112 | break; |
| 113 | default: |
| 114 | ALOGE("Unknown torch status %d", new_status); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | cs->onTorchStatusChanged( |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 119 | String8(camera_id), |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 120 | status); |
| 121 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 122 | } // extern "C" |
| 123 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 124 | // ---------------------------------------------------------------------------- |
| 125 | |
| 126 | // This is ugly and only safe if we never re-create the CameraService, but |
| 127 | // should be ok for now. |
| 128 | static CameraService *gCameraService; |
| 129 | |
Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 130 | CameraService::CameraService() : |
| 131 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), |
| 132 | mSoundRef(0), mModule(nullptr), |
| 133 | mNumberOfCameras(0), mNumberOfNormalCameras(0) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 134 | ALOGI("CameraService started (pid=%d)", getpid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 135 | gCameraService = this; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 136 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 137 | this->camera_device_status_change = android::camera_device_status_change; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 138 | this->torch_mode_status_change = android::torch_mode_status_change; |
| 139 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 140 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 143 | void CameraService::onFirstRef() |
| 144 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 145 | ALOGI("CameraService process starting"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 146 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 147 | BnCameraService::onFirstRef(); |
| 148 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 149 | // Update battery life tracking if service is restarting |
| 150 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 151 | notifier.noteResetCamera(); |
| 152 | notifier.noteResetFlashlight(); |
| 153 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 154 | camera_module_t *rawModule; |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 155 | int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID, |
| 156 | (const hw_module_t **)&rawModule); |
| 157 | if (err < 0) { |
| 158 | ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err)); |
| 159 | logServiceError("Could not load camera HAL module", err); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 160 | return; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 161 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 162 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 163 | mModule = new CameraModule(rawModule); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 164 | err = mModule->init(); |
| 165 | if (err != OK) { |
| 166 | ALOGE("Could not initialize camera HAL module: %d (%s)", err, |
| 167 | strerror(-err)); |
| 168 | logServiceError("Could not initialize camera HAL module", err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 169 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 170 | delete mModule; |
| 171 | mModule = nullptr; |
| 172 | return; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 173 | } |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 174 | ALOGI("Loaded \"%s\" camera module", mModule->getModuleName()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 175 | |
| 176 | mNumberOfCameras = mModule->getNumberOfCameras(); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 177 | mNumberOfNormalCameras = mNumberOfCameras; |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 178 | |
Yin-Chia Yeh | d4a653a | 2015-10-14 11:05:44 -0700 | [diff] [blame] | 179 | // Setup vendor tags before we call get_camera_info the first time |
| 180 | // because HAL might need to setup static vendor keys in get_camera_info |
| 181 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
| 182 | if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2) { |
| 183 | setUpVendorTags(); |
| 184 | } |
| 185 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 186 | mFlashlight = new CameraFlashlight(*mModule, *this); |
| 187 | status_t res = mFlashlight->findFlashUnits(); |
| 188 | if (res) { |
| 189 | // impossible because we haven't open any camera devices. |
| 190 | ALOGE("Failed to find flash units."); |
| 191 | } |
| 192 | |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 193 | int latestStrangeCameraId = INT_MAX; |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 194 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 195 | String8 cameraId = String8::format("%d", i); |
| 196 | |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 197 | // Get camera info |
| 198 | |
| 199 | struct camera_info info; |
| 200 | bool haveInfo = true; |
| 201 | status_t rc = mModule->getCameraInfo(i, &info); |
| 202 | if (rc != NO_ERROR) { |
| 203 | ALOGE("%s: Received error loading camera info for device %d, cost and" |
| 204 | " conflicting devices fields set to defaults for this device.", |
| 205 | __FUNCTION__, i); |
| 206 | haveInfo = false; |
| 207 | } |
| 208 | |
| 209 | // Check for backwards-compatibility support |
| 210 | if (haveInfo) { |
| 211 | if (checkCameraCapabilities(i, info, &latestStrangeCameraId) != OK) { |
| 212 | delete mModule; |
| 213 | mModule = nullptr; |
| 214 | return; |
| 215 | } |
| 216 | } |
| 217 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 218 | // Defaults to use for cost and conflicting devices |
| 219 | int cost = 100; |
| 220 | char** conflicting_devices = nullptr; |
| 221 | size_t conflicting_devices_length = 0; |
| 222 | |
| 223 | // If using post-2.4 module version, query the cost + conflicting devices from the HAL |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 224 | if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 && haveInfo) { |
| 225 | cost = info.resource_cost; |
| 226 | conflicting_devices = info.conflicting_devices; |
| 227 | conflicting_devices_length = info.conflicting_devices_length; |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | std::set<String8> conflicting; |
| 231 | for (size_t i = 0; i < conflicting_devices_length; i++) { |
| 232 | conflicting.emplace(String8(conflicting_devices[i])); |
| 233 | } |
| 234 | |
| 235 | // Initialize state for each camera device |
| 236 | { |
| 237 | Mutex::Autolock lock(mCameraStatesLock); |
| 238 | mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost, |
| 239 | conflicting)); |
| 240 | } |
| 241 | |
| 242 | if (mFlashlight->hasFlashUnit(cameraId)) { |
| 243 | mTorchStatusMap.add(cameraId, |
| 244 | ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_1) { |
| 249 | mModule->setCallbacks(this); |
| 250 | } |
| 251 | |
Ruben Brunk | 2823ce0 | 2015-05-19 17:25:13 -0700 | [diff] [blame] | 252 | CameraService::pingCameraServiceProxy(); |
| 253 | } |
| 254 | |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 255 | sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() { |
Ruben Brunk | 2823ce0 | 2015-05-19 17:25:13 -0700 | [diff] [blame] | 256 | sp<IServiceManager> sm = defaultServiceManager(); |
| 257 | sp<IBinder> binder = sm->getService(String16("media.camera.proxy")); |
| 258 | if (binder == nullptr) { |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 259 | return nullptr; |
Ruben Brunk | 2823ce0 | 2015-05-19 17:25:13 -0700 | [diff] [blame] | 260 | } |
| 261 | sp<ICameraServiceProxy> proxyBinder = interface_cast<ICameraServiceProxy>(binder); |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 262 | return proxyBinder; |
| 263 | } |
| 264 | |
| 265 | void CameraService::pingCameraServiceProxy() { |
| 266 | sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy(); |
| 267 | if (proxyBinder == nullptr) return; |
Ruben Brunk | 2823ce0 | 2015-05-19 17:25:13 -0700 | [diff] [blame] | 268 | proxyBinder->pingForUserUpdate(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 271 | CameraService::~CameraService() { |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 272 | if (mModule) { |
| 273 | delete mModule; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 274 | mModule = nullptr; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 275 | } |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 276 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 277 | gCameraService = nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 280 | void CameraService::onDeviceStatusChanged(camera_device_status_t cameraId, |
| 281 | camera_device_status_t newStatus) { |
| 282 | ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__, |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 283 | cameraId, newStatus); |
| 284 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 285 | String8 id = String8::format("%d", cameraId); |
| 286 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 287 | |
| 288 | if (state == nullptr) { |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 289 | ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId); |
| 290 | return; |
| 291 | } |
| 292 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 293 | ICameraServiceListener::Status oldStatus = state->getStatus(); |
| 294 | |
| 295 | if (oldStatus == static_cast<ICameraServiceListener::Status>(newStatus)) { |
| 296 | ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 297 | return; |
| 298 | } |
| 299 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 300 | if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 301 | logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 302 | newStatus)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 303 | sp<BasicClient> clientToDisconnect; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 304 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 305 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 306 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 307 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 308 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 309 | // to this device until the status changes |
| 310 | updateStatus(ICameraServiceListener::STATUS_NOT_PRESENT, id); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 311 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 312 | // Remove cached shim parameters |
| 313 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 314 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 315 | // Remove the client from the list of active clients |
| 316 | clientToDisconnect = removeClientLocked(id); |
| 317 | |
| 318 | // Notify the client of disconnection |
| 319 | clientToDisconnect->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 320 | CaptureResultExtras{}); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 323 | ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL", |
| 324 | __FUNCTION__, id.string()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 325 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 326 | // Disconnect client |
| 327 | if (clientToDisconnect.get() != nullptr) { |
| 328 | // Ensure not in binder RPC so client disconnect PID checks work correctly |
| 329 | LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(), |
| 330 | "onDeviceStatusChanged must be called from the camera service process!"); |
| 331 | clientToDisconnect->disconnect(); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 334 | } else { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 335 | if (oldStatus == ICameraServiceListener::Status::STATUS_NOT_PRESENT) { |
| 336 | logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 337 | newStatus)); |
| 338 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 339 | updateStatus(static_cast<ICameraServiceListener::Status>(newStatus), id); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 344 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 345 | ICameraServiceListener::TorchStatus newStatus) { |
| 346 | Mutex::Autolock al(mTorchStatusMutex); |
| 347 | onTorchStatusChangedLocked(cameraId, newStatus); |
| 348 | } |
| 349 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 350 | void CameraService::onTorchStatusChangedLocked(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 351 | ICameraServiceListener::TorchStatus newStatus) { |
| 352 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
| 353 | __FUNCTION__, cameraId.string(), newStatus); |
| 354 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 355 | ICameraServiceListener::TorchStatus status; |
| 356 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 357 | if (res) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 358 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", |
| 359 | __FUNCTION__, cameraId.string(), strerror(-res), res); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 360 | return; |
| 361 | } |
| 362 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 363 | return; |
| 364 | } |
| 365 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 366 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 367 | if (res) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 368 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, |
| 369 | (uint32_t)newStatus, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 370 | return; |
| 371 | } |
| 372 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 373 | { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 374 | // Update battery life logging for flashlight |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 375 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 376 | auto iter = mTorchUidMap.find(cameraId); |
| 377 | if (iter != mTorchUidMap.end()) { |
| 378 | int oldUid = iter->second.second; |
| 379 | int newUid = iter->second.first; |
| 380 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 381 | if (oldUid != newUid) { |
| 382 | // If the UID has changed, log the status and update current UID in mTorchUidMap |
| 383 | if (status == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) { |
| 384 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 385 | } |
| 386 | if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) { |
| 387 | notifier.noteFlashlightOn(cameraId, newUid); |
| 388 | } |
| 389 | iter->second.second = newUid; |
| 390 | } else { |
| 391 | // If the UID has not changed, log the status |
| 392 | if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) { |
| 393 | notifier.noteFlashlightOn(cameraId, oldUid); |
| 394 | } else { |
| 395 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 402 | Mutex::Autolock lock(mStatusListenerLock); |
| 403 | for (auto& i : mListenerList) { |
| 404 | i->onTorchStatusChanged(newStatus, String16{cameraId}); |
| 405 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 409 | int32_t CameraService::getNumberOfCameras() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 410 | ATRACE_CALL(); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 411 | return getNumberOfCameras(CAMERA_TYPE_BACKWARD_COMPATIBLE); |
| 412 | } |
| 413 | |
| 414 | int32_t CameraService::getNumberOfCameras(int type) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 415 | ATRACE_CALL(); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 416 | switch (type) { |
| 417 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: |
| 418 | return mNumberOfNormalCameras; |
| 419 | case CAMERA_TYPE_ALL: |
| 420 | return mNumberOfCameras; |
| 421 | default: |
| 422 | ALOGW("%s: Unknown camera type %d, returning 0", |
| 423 | __FUNCTION__, type); |
| 424 | return 0; |
| 425 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | status_t CameraService::getCameraInfo(int cameraId, |
| 429 | struct CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 430 | ATRACE_CALL(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 431 | if (!mModule) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 432 | return -ENODEV; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 435 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 436 | return BAD_VALUE; |
| 437 | } |
| 438 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 439 | struct camera_info info; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 440 | status_t rc = filterGetInfoErrorCode( |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 441 | mModule->getCameraInfo(cameraId, &info)); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 442 | cameraInfo->facing = info.facing; |
| 443 | cameraInfo->orientation = info.orientation; |
| 444 | return rc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 447 | int CameraService::cameraIdToInt(const String8& cameraId) { |
| 448 | errno = 0; |
| 449 | size_t pos = 0; |
| 450 | int ret = stoi(std::string{cameraId.string()}, &pos); |
| 451 | if (errno != 0 || pos != cameraId.size()) { |
| 452 | return -1; |
| 453 | } |
| 454 | return ret; |
| 455 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 456 | |
| 457 | status_t CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 458 | ATRACE_CALL(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 459 | status_t ret = OK; |
| 460 | struct CameraInfo info; |
| 461 | if ((ret = getCameraInfo(cameraId, &info)) != OK) { |
| 462 | return ret; |
| 463 | } |
| 464 | |
| 465 | CameraMetadata shimInfo; |
| 466 | int32_t orientation = static_cast<int32_t>(info.orientation); |
| 467 | if ((ret = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) { |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ? |
| 472 | ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK; |
| 473 | if ((ret = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) { |
| 474 | return ret; |
| 475 | } |
| 476 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 477 | CameraParameters shimParams; |
| 478 | if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) { |
| 479 | // Error logged by callee |
| 480 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | Vector<Size> sizes; |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 484 | Vector<Size> jpegSizes; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 485 | Vector<int32_t> formats; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 486 | { |
| 487 | shimParams.getSupportedPreviewSizes(/*out*/sizes); |
| 488 | shimParams.getSupportedPreviewFormats(/*out*/formats); |
| 489 | shimParams.getSupportedPictureSizes(/*out*/jpegSizes); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // Always include IMPLEMENTATION_DEFINED |
| 493 | formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED); |
| 494 | |
| 495 | const size_t INTS_PER_CONFIG = 4; |
| 496 | |
| 497 | // Build available stream configurations metadata |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 498 | size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG; |
| 499 | |
| 500 | Vector<int32_t> streamConfigs; |
| 501 | streamConfigs.setCapacity(streamConfigSize); |
| 502 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 503 | for (size_t i = 0; i < formats.size(); ++i) { |
| 504 | for (size_t j = 0; j < sizes.size(); ++j) { |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 505 | streamConfigs.add(formats[i]); |
| 506 | streamConfigs.add(sizes[j].width); |
| 507 | streamConfigs.add(sizes[j].height); |
| 508 | streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 512 | for (size_t i = 0; i < jpegSizes.size(); ++i) { |
| 513 | streamConfigs.add(HAL_PIXEL_FORMAT_BLOB); |
| 514 | streamConfigs.add(jpegSizes[i].width); |
| 515 | streamConfigs.add(jpegSizes[i].height); |
| 516 | streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); |
| 517 | } |
| 518 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 519 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 520 | streamConfigs.array(), streamConfigSize)) != OK) { |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | int64_t fakeMinFrames[0]; |
| 525 | // TODO: Fixme, don't fake min frame durations. |
| 526 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, |
| 527 | fakeMinFrames, 0)) != OK) { |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | int64_t fakeStalls[0]; |
| 532 | // TODO: Fixme, don't fake stall durations. |
| 533 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, |
| 534 | fakeStalls, 0)) != OK) { |
| 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | *cameraInfo = shimInfo; |
| 539 | return OK; |
| 540 | } |
| 541 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 542 | status_t CameraService::getCameraCharacteristics(int cameraId, |
| 543 | CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 544 | ATRACE_CALL(); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 545 | if (!cameraInfo) { |
| 546 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
| 547 | return BAD_VALUE; |
| 548 | } |
| 549 | |
| 550 | if (!mModule) { |
| 551 | ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__); |
| 552 | return -ENODEV; |
| 553 | } |
| 554 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 555 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 556 | ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId); |
| 557 | return BAD_VALUE; |
| 558 | } |
| 559 | |
| 560 | int facing; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 561 | status_t ret = OK; |
Chien-Yu Chen | 676b21b | 2015-02-24 10:28:19 -0800 | [diff] [blame] | 562 | if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0 || |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 563 | getDeviceVersion(cameraId, &facing) < CAMERA_DEVICE_API_VERSION_3_0) { |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 564 | /** |
| 565 | * Backwards compatibility mode for old HALs: |
| 566 | * - Convert CameraInfo into static CameraMetadata properties. |
| 567 | * - Retrieve cached CameraParameters for this camera. If none exist, |
| 568 | * attempt to open CameraClient and retrieve the CameraParameters. |
| 569 | * - Convert cached CameraParameters into static CameraMetadata |
| 570 | * properties. |
| 571 | */ |
| 572 | ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 573 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 574 | if ((ret = generateShimMetadata(cameraId, cameraInfo)) != OK) { |
| 575 | return ret; |
| 576 | } |
Zhijun He | f05e50e | 2013-10-01 11:05:33 -0700 | [diff] [blame] | 577 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 578 | } else { |
| 579 | /** |
| 580 | * Normal HAL 2.1+ codepath. |
| 581 | */ |
| 582 | struct camera_info info; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 583 | ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info)); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 584 | *cameraInfo = info.static_camera_characteristics; |
| 585 | } |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 586 | |
| 587 | return ret; |
| 588 | } |
| 589 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 590 | int CameraService::getCallingPid() { |
| 591 | return IPCThreadState::self()->getCallingPid(); |
| 592 | } |
| 593 | |
| 594 | int CameraService::getCallingUid() { |
| 595 | return IPCThreadState::self()->getCallingUid(); |
| 596 | } |
| 597 | |
| 598 | String8 CameraService::getFormattedCurrentTime() { |
| 599 | time_t now = time(nullptr); |
| 600 | char formattedTime[64]; |
| 601 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
| 602 | return String8(formattedTime); |
| 603 | } |
| 604 | |
| 605 | int CameraService::getCameraPriorityFromProcState(int procState) { |
| 606 | // Find the priority for the camera usage based on the process state. Higher priority clients |
| 607 | // win for evictions. |
Ruben Brunk | be0b6b4 | 2015-05-12 16:10:52 -0700 | [diff] [blame] | 608 | if (procState < 0) { |
| 609 | ALOGE("%s: Received invalid process state %d from ActivityManagerService!", __FUNCTION__, |
| 610 | procState); |
| 611 | return -1; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 612 | } |
Eino-Ville Talvala | 52aad85 | 2015-09-03 12:24:24 -0700 | [diff] [blame] | 613 | // Treat sleeping TOP processes the same as regular TOP processes, for |
| 614 | // access priority. This is important for lock-screen camera launch scenarios |
| 615 | if (procState == PROCESS_STATE_TOP_SLEEPING) { |
| 616 | procState = PROCESS_STATE_TOP; |
| 617 | } |
Ruben Brunk | be0b6b4 | 2015-05-12 16:10:52 -0700 | [diff] [blame] | 618 | return INT_MAX - procState; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 621 | status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 622 | ATRACE_CALL(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 623 | if (!mModule) { |
| 624 | ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__); |
| 625 | return -ENODEV; |
| 626 | } |
| 627 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 628 | desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 629 | return OK; |
| 630 | } |
| 631 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 632 | int CameraService::getDeviceVersion(int cameraId, int* facing) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 633 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 634 | struct camera_info info; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 635 | if (mModule->getCameraInfo(cameraId, &info) != OK) { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 636 | return -1; |
| 637 | } |
| 638 | |
| 639 | int deviceVersion; |
Chien-Yu Chen | 676b21b | 2015-02-24 10:28:19 -0800 | [diff] [blame] | 640 | if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 641 | deviceVersion = info.device_version; |
| 642 | } else { |
| 643 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 644 | } |
| 645 | |
| 646 | if (facing) { |
| 647 | *facing = info.facing; |
| 648 | } |
| 649 | |
| 650 | return deviceVersion; |
| 651 | } |
| 652 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 653 | status_t CameraService::filterGetInfoErrorCode(status_t err) { |
| 654 | switch(err) { |
| 655 | case NO_ERROR: |
| 656 | case -EINVAL: |
| 657 | return err; |
| 658 | default: |
| 659 | break; |
| 660 | } |
| 661 | return -ENODEV; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 662 | } |
| 663 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 664 | bool CameraService::setUpVendorTags() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 665 | ATRACE_CALL(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 666 | vendor_tag_ops_t vOps = vendor_tag_ops_t(); |
| 667 | |
| 668 | // Check if vendor operations have been implemented |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 669 | if (!mModule->isVendorTagDefined()) { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 670 | ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__); |
| 671 | return false; |
| 672 | } |
| 673 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 674 | mModule->getVendorTagOps(&vOps); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 675 | |
| 676 | // Ensure all vendor operations are present |
| 677 | if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL || |
| 678 | vOps.get_section_name == NULL || vOps.get_tag_name == NULL || |
| 679 | vOps.get_tag_type == NULL) { |
| 680 | ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions." |
| 681 | , __FUNCTION__); |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | // Read all vendor tag definitions into a descriptor |
| 686 | sp<VendorTagDescriptor> desc; |
| 687 | status_t res; |
| 688 | if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc)) |
| 689 | != OK) { |
| 690 | ALOGE("%s: Could not generate descriptor from vendor tag operations," |
| 691 | "received error %s (%d). Camera clients will not be able to use" |
| 692 | "vendor tags", __FUNCTION__, strerror(res), res); |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | // Set the global descriptor to use with camera metadata |
| 697 | VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc); |
| 698 | return true; |
| 699 | } |
| 700 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 701 | status_t CameraService::makeClient(const sp<CameraService>& cameraService, |
| 702 | const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId, |
| 703 | int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode, |
| 704 | int halVersion, int deviceVersion, apiLevel effectiveApiLevel, |
| 705 | /*out*/sp<BasicClient>* client) { |
| 706 | |
| 707 | // TODO: Update CameraClients + HAL interface to use strings for Camera IDs |
| 708 | int id = cameraIdToInt(cameraId); |
| 709 | if (id == -1) { |
| 710 | ALOGE("%s: Invalid camera ID %s, cannot convert to integer.", __FUNCTION__, |
| 711 | cameraId.string()); |
| 712 | return BAD_VALUE; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 715 | if (halVersion < 0 || halVersion == deviceVersion) { |
| 716 | // Default path: HAL version is unspecified by caller, create CameraClient |
| 717 | // based on device version reported by the HAL. |
| 718 | switch(deviceVersion) { |
| 719 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 720 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 721 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
| 722 | *client = new CameraClient(cameraService, tmp, packageName, id, facing, |
| 723 | clientPid, clientUid, getpid(), legacyMode); |
| 724 | } else { // Camera2 API route |
| 725 | ALOGW("Camera using old HAL version: %d", deviceVersion); |
| 726 | return -EOPNOTSUPP; |
| 727 | } |
| 728 | break; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 729 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 730 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 731 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 732 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 733 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 734 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
| 735 | *client = new Camera2Client(cameraService, tmp, packageName, id, facing, |
| 736 | clientPid, clientUid, servicePid, legacyMode); |
| 737 | } else { // Camera2 API route |
| 738 | sp<ICameraDeviceCallbacks> tmp = |
| 739 | static_cast<ICameraDeviceCallbacks*>(cameraCb.get()); |
| 740 | *client = new CameraDeviceClient(cameraService, tmp, packageName, id, |
| 741 | facing, clientPid, clientUid, servicePid); |
| 742 | } |
| 743 | break; |
| 744 | default: |
| 745 | // Should not be reachable |
| 746 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 747 | return INVALID_OPERATION; |
| 748 | } |
| 749 | } else { |
| 750 | // A particular HAL version is requested by caller. Create CameraClient |
| 751 | // based on the requested HAL version. |
| 752 | if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 && |
| 753 | halVersion == CAMERA_DEVICE_API_VERSION_1_0) { |
| 754 | // Only support higher HAL version device opened as HAL1.0 device. |
| 755 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
| 756 | *client = new CameraClient(cameraService, tmp, packageName, id, facing, |
| 757 | clientPid, clientUid, servicePid, legacyMode); |
| 758 | } else { |
| 759 | // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet. |
| 760 | ALOGE("Invalid camera HAL version %x: HAL %x device can only be" |
| 761 | " opened as HAL %x device", halVersion, deviceVersion, |
| 762 | CAMERA_DEVICE_API_VERSION_1_0); |
| 763 | return INVALID_OPERATION; |
| 764 | } |
| 765 | } |
| 766 | return NO_ERROR; |
| 767 | } |
| 768 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 769 | String8 CameraService::toString(std::set<userid_t> intSet) { |
| 770 | String8 s(""); |
| 771 | bool first = true; |
| 772 | for (userid_t i : intSet) { |
| 773 | if (first) { |
| 774 | s.appendFormat("%d", i); |
| 775 | first = false; |
| 776 | } else { |
| 777 | s.appendFormat(", %d", i); |
| 778 | } |
| 779 | } |
| 780 | return s; |
| 781 | } |
| 782 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 783 | status_t CameraService::initializeShimMetadata(int cameraId) { |
| 784 | int uid = getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 785 | |
| 786 | String16 internalPackageName("media"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 787 | String8 id = String8::format("%d", cameraId); |
| 788 | status_t ret = NO_ERROR; |
| 789 | sp<Client> tmp = nullptr; |
| 790 | if ((ret = connectHelper<ICameraClient,Client>(sp<ICameraClient>{nullptr}, id, |
| 791 | static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED), internalPackageName, uid, API_1, |
| 792 | false, true, tmp)) != NO_ERROR) { |
| 793 | ALOGE("%s: Error %d (%s) initializing shim metadata.", __FUNCTION__, ret, strerror(ret)); |
| 794 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 795 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 796 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 799 | status_t CameraService::getLegacyParametersLazy(int cameraId, |
| 800 | /*out*/ |
| 801 | CameraParameters* parameters) { |
| 802 | |
| 803 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 804 | |
| 805 | status_t ret = 0; |
| 806 | |
| 807 | if (parameters == NULL) { |
| 808 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
| 809 | return BAD_VALUE; |
| 810 | } |
| 811 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 812 | String8 id = String8::format("%d", cameraId); |
| 813 | |
| 814 | // Check if we already have parameters |
| 815 | { |
| 816 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 817 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 818 | auto cameraState = getCameraState(id); |
| 819 | if (cameraState == nullptr) { |
| 820 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
| 821 | return BAD_VALUE; |
| 822 | } |
| 823 | CameraParameters p = cameraState->getShimParams(); |
| 824 | if (!p.isEmpty()) { |
| 825 | *parameters = p; |
| 826 | return NO_ERROR; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 830 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 831 | ret = initializeShimMetadata(cameraId); |
| 832 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 833 | if (ret != NO_ERROR) { |
| 834 | // Error already logged by callee |
| 835 | return ret; |
| 836 | } |
| 837 | |
| 838 | // Check for parameters again |
| 839 | { |
| 840 | // Scope for service lock |
| 841 | Mutex::Autolock lock(mServiceLock); |
| 842 | auto cameraState = getCameraState(id); |
| 843 | if (cameraState == nullptr) { |
| 844 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
| 845 | return BAD_VALUE; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 846 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 847 | CameraParameters p = cameraState->getShimParams(); |
| 848 | if (!p.isEmpty()) { |
| 849 | *parameters = p; |
| 850 | return NO_ERROR; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 854 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 855 | __FUNCTION__); |
| 856 | return INVALID_OPERATION; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 859 | status_t CameraService::validateConnectLocked(const String8& cameraId, /*inout*/int& clientUid) |
| 860 | const { |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 861 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 862 | int callingPid = getCallingPid(); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 863 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 864 | if (clientUid == USE_CALLING_UID) { |
| 865 | clientUid = getCallingUid(); |
| 866 | } else { |
| 867 | // We only trust our own process to forward client UIDs |
| 868 | if (callingPid != getpid()) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 869 | ALOGE("CameraService::connect X (PID %d) rejected (don't trust clientUid %d)", |
| 870 | callingPid, clientUid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 871 | return PERMISSION_DENIED; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 872 | } |
| 873 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 874 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 875 | if (!mModule) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 876 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 877 | callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 878 | return -ENODEV; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 881 | if (getCameraState(cameraId) == nullptr) { |
| 882 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 883 | cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 884 | return -ENODEV; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 887 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 888 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 889 | // Only allow clients who are being used by the current foreground device user, unless calling |
| 890 | // from our own process. |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 891 | if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
| 892 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 893 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
| 894 | toString(mAllowedUsers).string()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 895 | return PERMISSION_DENIED; |
| 896 | } |
| 897 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 898 | return checkIfDeviceIsUsable(cameraId); |
| 899 | } |
| 900 | |
| 901 | status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const { |
| 902 | auto cameraState = getCameraState(cameraId); |
| 903 | int callingPid = getCallingPid(); |
| 904 | if (cameraState == nullptr) { |
| 905 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 906 | cameraId.string()); |
| 907 | return -ENODEV; |
| 908 | } |
| 909 | |
| 910 | ICameraServiceListener::Status currentStatus = cameraState->getStatus(); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 911 | if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 912 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
| 913 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 914 | return -ENODEV; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 915 | } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 916 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
| 917 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 918 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 919 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 920 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 921 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 922 | } |
| 923 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 924 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
| 925 | const CameraService::DescriptorPtr& desc) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 926 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 927 | // Make a descriptor for the incoming client |
| 928 | auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc); |
| 929 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 930 | |
| 931 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
| 932 | String8(client->getPackageName())); |
| 933 | |
| 934 | if (evicted.size() > 0) { |
| 935 | // This should never happen - clients should already have been removed in disconnect |
| 936 | for (auto& i : evicted) { |
| 937 | ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect", |
| 938 | __FUNCTION__, i->getKey().string()); |
| 939 | } |
| 940 | |
| 941 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 942 | __FUNCTION__); |
| 943 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 944 | |
| 945 | // And register a death notification for the client callback. Do |
| 946 | // this last to avoid Binder policy where a nested Binder |
| 947 | // transaction might be pre-empted to service the client death |
| 948 | // notification if the client process dies before linkToDeath is |
| 949 | // invoked. |
| 950 | sp<IBinder> remoteCallback = client->getRemote(); |
| 951 | if (remoteCallback != nullptr) { |
| 952 | remoteCallback->linkToDeath(this); |
| 953 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid, |
| 957 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName, |
| 958 | /*out*/ |
| 959 | sp<BasicClient>* client, |
| 960 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 961 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 962 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 963 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 964 | DescriptorPtr clientDescriptor; |
| 965 | { |
| 966 | if (effectiveApiLevel == API_1) { |
| 967 | // If we are using API1, any existing client for this camera ID with the same remote |
| 968 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 969 | |
| 970 | auto current = mActiveClientManager.get(cameraId); |
| 971 | if (current != nullptr) { |
| 972 | auto clientSp = current->getValue(); |
| 973 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 974 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
| 975 | ALOGW("CameraService connect called from same client, but with a different" |
| 976 | " API level, evicting prior client..."); |
| 977 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 978 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 979 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 980 | *client = clientSp; |
| 981 | return NO_ERROR; |
| 982 | } |
| 983 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 984 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 985 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 986 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 987 | // Return error if the device was unplugged or removed by the HAL for some reason |
| 988 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 989 | return ret; |
| 990 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 991 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 992 | // Get current active client PIDs |
| 993 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 994 | ownerPids.push_back(clientPid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 995 | |
Chih-Hung Hsieh | 54b4246 | 2015-03-19 12:04:54 -0700 | [diff] [blame] | 996 | // Use the value +PROCESS_STATE_NONEXISTENT, to avoid taking |
| 997 | // address of PROCESS_STATE_NONEXISTENT as a reference argument |
| 998 | // for the vector constructor. PROCESS_STATE_NONEXISTENT does |
| 999 | // not have an out-of-class definition. |
| 1000 | std::vector<int> priorities(ownerPids.size(), +PROCESS_STATE_NONEXISTENT); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1001 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1002 | // Get priorites of all active PIDs |
| 1003 | ProcessInfoService::getProcessStatesFromPids(ownerPids.size(), &ownerPids[0], |
| 1004 | /*out*/&priorities[0]); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1005 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1006 | // Update all active clients' priorities |
| 1007 | std::map<int,int> pidToPriorityMap; |
| 1008 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
| 1009 | pidToPriorityMap.emplace(ownerPids[i], getCameraPriorityFromProcState(priorities[i])); |
| 1010 | } |
| 1011 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1012 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1013 | // Get state for the given cameraId |
| 1014 | auto state = getCameraState(cameraId); |
| 1015 | if (state == nullptr) { |
| 1016 | ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", |
| 1017 | clientPid, cameraId.string()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1018 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1019 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1020 | |
| 1021 | // Make descriptor for incoming client |
| 1022 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 1023 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 1024 | state->getConflicting(), |
| 1025 | getCameraPriorityFromProcState(priorities[priorities.size() - 1]), clientPid); |
| 1026 | |
| 1027 | // Find clients that would be evicted |
| 1028 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 1029 | |
| 1030 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 1031 | // background, so we cannot do evictions |
| 1032 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 1033 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 1034 | " priority).", clientPid); |
| 1035 | |
| 1036 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1037 | String8 curTime = getFormattedCurrentTime(); |
| 1038 | auto incompatibleClients = |
| 1039 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 1040 | |
| 1041 | String8 msg = String8::format("%s : DENIED connect device %s client for package %s " |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1042 | "(PID %d, priority %d) due to eviction policy", curTime.string(), |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1043 | cameraId.string(), packageName.string(), clientPid, |
| 1044 | getCameraPriorityFromProcState(priorities[priorities.size() - 1])); |
| 1045 | |
| 1046 | for (auto& i : incompatibleClients) { |
| 1047 | msg.appendFormat("\n - Blocked by existing device %s client for package %s" |
| 1048 | "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(), |
| 1049 | String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(), |
| 1050 | i->getPriority()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1051 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
| 1052 | PRId32 ", priority %" PRId32 ")", i->getKey().string(), |
| 1053 | String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(), |
| 1054 | i->getPriority()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1058 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1059 | mEventLog.add(msg); |
| 1060 | |
| 1061 | return -EBUSY; |
| 1062 | } |
| 1063 | |
| 1064 | for (auto& i : evicted) { |
| 1065 | sp<BasicClient> clientSp = i->getValue(); |
| 1066 | if (clientSp.get() == nullptr) { |
| 1067 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 1068 | |
| 1069 | // TODO: Remove this |
| 1070 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 1071 | __FUNCTION__); |
| 1072 | mActiveClientManager.remove(i); |
| 1073 | continue; |
| 1074 | } |
| 1075 | |
| 1076 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
| 1077 | i->getKey().string()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1078 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1079 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1080 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1081 | logEvent(String8::format("EVICT device %s client held by package %s (PID" |
| 1082 | " %" PRId32 ", priority %" PRId32 ")\n - Evicted by device %s client for" |
| 1083 | " package %s (PID %d, priority %" PRId32 ")", |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1084 | i->getKey().string(), String8{clientSp->getPackageName()}.string(), |
| 1085 | i->getOwnerId(), i->getPriority(), cameraId.string(), |
| 1086 | packageName.string(), clientPid, |
| 1087 | getCameraPriorityFromProcState(priorities[priorities.size() - 1]))); |
| 1088 | |
| 1089 | // Notify the client of disconnection |
| 1090 | clientSp->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 1091 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1092 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1095 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 1096 | // other clients from connecting in mServiceLockWrapper if held |
| 1097 | mServiceLock.unlock(); |
| 1098 | |
| 1099 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
| 1100 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 1101 | |
| 1102 | // Destroy evicted clients |
| 1103 | for (auto& i : evictedClients) { |
| 1104 | // 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] | 1105 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1106 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1107 | |
| 1108 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 1109 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1110 | for (const auto& i : evictedClients) { |
| 1111 | ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")", |
| 1112 | __FUNCTION__, i->getKey().string(), i->getOwnerId()); |
| 1113 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 1114 | if (ret == TIMED_OUT) { |
| 1115 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
| 1116 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), |
| 1117 | mActiveClientManager.toString().string()); |
| 1118 | return -EBUSY; |
| 1119 | } |
| 1120 | if (ret != NO_ERROR) { |
| 1121 | ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), " |
| 1122 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret), |
| 1123 | ret, mActiveClientManager.toString().string()); |
| 1124 | return ret; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | evictedClients.clear(); |
| 1129 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1130 | // Once clients have been disconnected, relock |
| 1131 | mServiceLock.lock(); |
| 1132 | |
| 1133 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 1134 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 1135 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1138 | *partial = clientDescriptor; |
| 1139 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1142 | status_t CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1143 | const sp<ICameraClient>& cameraClient, |
| 1144 | int cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1145 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1146 | int clientUid, |
| 1147 | /*out*/ |
| 1148 | sp<ICamera>& device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1149 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1150 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1151 | status_t ret = NO_ERROR; |
| 1152 | String8 id = String8::format("%d", cameraId); |
| 1153 | sp<Client> client = nullptr; |
| 1154 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, CAMERA_HAL_API_VERSION_UNSPECIFIED, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1155 | clientPackageName, clientUid, API_1, false, false, /*out*/client); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1156 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1157 | if(ret != NO_ERROR) { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1158 | logRejected(id, getCallingPid(), String8(clientPackageName), |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1159 | String8::format("%s (%d)", strerror(-ret), ret)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1160 | return ret; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1163 | device = client; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1164 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1167 | status_t CameraService::connectLegacy( |
| 1168 | const sp<ICameraClient>& cameraClient, |
| 1169 | int cameraId, int halVersion, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1170 | const String16& clientPackageName, |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1171 | int clientUid, |
| 1172 | /*out*/ |
| 1173 | sp<ICamera>& device) { |
| 1174 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1175 | ATRACE_CALL(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1176 | String8 id = String8::format("%d", cameraId); |
Chien-Yu Chen | 676b21b | 2015-02-24 10:28:19 -0800 | [diff] [blame] | 1177 | int apiVersion = mModule->getModuleApiVersion(); |
Igor Murashkin | 3d07d1a | 2014-06-20 11:27:03 -0700 | [diff] [blame] | 1178 | if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED && |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 1179 | apiVersion < CAMERA_MODULE_API_VERSION_2_3) { |
Igor Murashkin | 3d07d1a | 2014-06-20 11:27:03 -0700 | [diff] [blame] | 1180 | /* |
| 1181 | * Either the HAL version is unspecified in which case this just creates |
| 1182 | * a camera client selected by the latest device version, or |
| 1183 | * it's a particular version in which case the HAL must supported |
| 1184 | * the open_legacy call |
| 1185 | */ |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1186 | ALOGE("%s: camera HAL module version %x doesn't support connecting to legacy HAL devices!", |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 1187 | __FUNCTION__, apiVersion); |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1188 | logRejected(id, getCallingPid(), String8(clientPackageName), |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1189 | String8("HAL module version doesn't support legacy HAL connections")); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1190 | return INVALID_OPERATION; |
| 1191 | } |
| 1192 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1193 | status_t ret = NO_ERROR; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1194 | sp<Client> client = nullptr; |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1195 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion, clientPackageName, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1196 | clientUid, API_1, true, false, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1197 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1198 | if(ret != NO_ERROR) { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1199 | logRejected(id, getCallingPid(), String8(clientPackageName), |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1200 | String8::format("%s (%d)", strerror(-ret), ret)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1201 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1204 | device = client; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1205 | return NO_ERROR; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1208 | status_t CameraService::connectDevice( |
| 1209 | const sp<ICameraDeviceCallbacks>& cameraCb, |
| 1210 | int cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1211 | const String16& clientPackageName, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1212 | int clientUid, |
| 1213 | /*out*/ |
| 1214 | sp<ICameraDeviceUser>& device) { |
| 1215 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1216 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1217 | status_t ret = NO_ERROR; |
| 1218 | String8 id = String8::format("%d", cameraId); |
| 1219 | sp<CameraDeviceClient> client = nullptr; |
| 1220 | ret = connectHelper<ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1221 | CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, API_2, false, false, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1222 | /*out*/client); |
| 1223 | |
| 1224 | if(ret != NO_ERROR) { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1225 | logRejected(id, getCallingPid(), String8(clientPackageName), |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1226 | String8::format("%s (%d)", strerror(-ret), ret)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1227 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1228 | } |
| 1229 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1230 | device = client; |
| 1231 | return NO_ERROR; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1234 | status_t CameraService::setTorchMode(const String16& cameraId, bool enabled, |
| 1235 | const sp<IBinder>& clientBinder) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1236 | |
| 1237 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1238 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1239 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1240 | return -EINVAL; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1243 | String8 id = String8(cameraId.string()); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1244 | int uid = getCallingUid(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1245 | |
| 1246 | // verify id is valid. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1247 | auto state = getCameraState(id); |
| 1248 | if (state == nullptr) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 1249 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1250 | return -EINVAL; |
| 1251 | } |
| 1252 | |
| 1253 | ICameraServiceListener::Status cameraStatus = state->getStatus(); |
| 1254 | if (cameraStatus != ICameraServiceListener::STATUS_PRESENT && |
| 1255 | cameraStatus != ICameraServiceListener::STATUS_NOT_AVAILABLE) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 1256 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1257 | return -EINVAL; |
| 1258 | } |
| 1259 | |
| 1260 | { |
| 1261 | Mutex::Autolock al(mTorchStatusMutex); |
| 1262 | ICameraServiceListener::TorchStatus status; |
| 1263 | status_t res = getTorchStatusLocked(id, &status); |
| 1264 | if (res) { |
| 1265 | ALOGE("%s: getting current torch status failed for camera %s", |
| 1266 | __FUNCTION__, id.string()); |
| 1267 | return -EINVAL; |
| 1268 | } |
| 1269 | |
| 1270 | if (status == ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1271 | if (cameraStatus == ICameraServiceListener::STATUS_NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1272 | ALOGE("%s: torch mode of camera %s is not available because " |
| 1273 | "camera is in use", __FUNCTION__, id.string()); |
| 1274 | return -EBUSY; |
| 1275 | } else { |
| 1276 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 1277 | "insufficient resources", __FUNCTION__, id.string()); |
| 1278 | return -EUSERS; |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1283 | { |
| 1284 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 1285 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 1286 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1287 | if (mTorchUidMap.find(id) == mTorchUidMap.end()) { |
| 1288 | mTorchUidMap[id].first = uid; |
| 1289 | mTorchUidMap[id].second = uid; |
| 1290 | } else { |
| 1291 | // Set the pending UID |
| 1292 | mTorchUidMap[id].first = uid; |
| 1293 | } |
| 1294 | } |
| 1295 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1296 | status_t res = mFlashlight->setTorchMode(id, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1297 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1298 | if (res) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1299 | ALOGE("%s: setting torch mode of camera %s to %d failed. %s (%d)", |
| 1300 | __FUNCTION__, id.string(), enabled, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1301 | return res; |
| 1302 | } |
| 1303 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1304 | { |
| 1305 | // update the link to client's death |
| 1306 | Mutex::Autolock al(mTorchClientMapMutex); |
| 1307 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 1308 | if (enabled) { |
| 1309 | if (index == NAME_NOT_FOUND) { |
| 1310 | mTorchClientMap.add(id, clientBinder); |
| 1311 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1312 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1313 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 1314 | } |
| 1315 | clientBinder->linkToDeath(this); |
| 1316 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1317 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1318 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1319 | } |
| 1320 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1321 | return OK; |
| 1322 | } |
| 1323 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1324 | void CameraService::notifySystemEvent(int32_t eventId, const int32_t* args, size_t length) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1325 | ATRACE_CALL(); |
| 1326 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1327 | switch(eventId) { |
| 1328 | case ICameraService::USER_SWITCHED: { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1329 | doUserSwitch(/*newUserIds*/args, /*length*/length); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1330 | break; |
| 1331 | } |
| 1332 | case ICameraService::NO_EVENT: |
| 1333 | default: { |
| 1334 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 1335 | eventId); |
| 1336 | break; |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1341 | status_t CameraService::addListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1342 | ATRACE_CALL(); |
| 1343 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1344 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1345 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 1346 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 1347 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
| 1348 | return BAD_VALUE; |
| 1349 | } |
| 1350 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1351 | Mutex::Autolock lock(mServiceLock); |
| 1352 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1353 | { |
| 1354 | Mutex::Autolock lock(mStatusListenerLock); |
| 1355 | for (auto& it : mListenerList) { |
| 1356 | if (IInterface::asBinder(it) == IInterface::asBinder(listener)) { |
| 1357 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 1358 | __FUNCTION__, listener.get()); |
| 1359 | return ALREADY_EXISTS; |
| 1360 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1361 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1362 | |
| 1363 | mListenerList.push_back(listener); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1366 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1367 | /* Immediately signal current status to this listener only */ |
| 1368 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1369 | Mutex::Autolock lock(mCameraStatesLock); |
| 1370 | for (auto& i : mCameraStates) { |
| 1371 | // TODO: Update binder to use String16 for camera IDs and remove; |
| 1372 | int id = cameraIdToInt(i.first); |
| 1373 | if (id == -1) continue; |
| 1374 | |
| 1375 | listener->onStatusChanged(i.second->getStatus(), id); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1379 | /* Immediately signal current torch status to this listener only */ |
| 1380 | { |
| 1381 | Mutex::Autolock al(mTorchStatusMutex); |
| 1382 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1383 | String16 id = String16(mTorchStatusMap.keyAt(i).string()); |
| 1384 | listener->onTorchStatusChanged(mTorchStatusMap.valueAt(i), id); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1385 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1386 | } |
| 1387 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1388 | return OK; |
| 1389 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1390 | |
| 1391 | status_t CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1392 | ATRACE_CALL(); |
| 1393 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1394 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 1395 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 1396 | if (listener == 0) { |
| 1397 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
| 1398 | return BAD_VALUE; |
| 1399 | } |
| 1400 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1401 | Mutex::Autolock lock(mServiceLock); |
| 1402 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1403 | { |
| 1404 | Mutex::Autolock lock(mStatusListenerLock); |
| 1405 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
| 1406 | if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) { |
| 1407 | mListenerList.erase(it); |
| 1408 | return OK; |
| 1409 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 1414 | __FUNCTION__, listener.get()); |
| 1415 | |
| 1416 | return BAD_VALUE; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1419 | status_t CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1420 | |
| 1421 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1422 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 1423 | |
| 1424 | if (parameters == NULL) { |
| 1425 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
| 1426 | return BAD_VALUE; |
| 1427 | } |
| 1428 | |
| 1429 | status_t ret = 0; |
| 1430 | |
| 1431 | CameraParameters shimParams; |
| 1432 | if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) { |
| 1433 | // Error logged by caller |
| 1434 | return ret; |
| 1435 | } |
| 1436 | |
| 1437 | String8 shimParamsString8 = shimParams.flatten(); |
| 1438 | String16 shimParamsString16 = String16(shimParamsString8); |
| 1439 | |
| 1440 | *parameters = shimParamsString16; |
| 1441 | |
| 1442 | return OK; |
| 1443 | } |
| 1444 | |
| 1445 | status_t CameraService::supportsCameraApi(int cameraId, int apiVersion) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1446 | ATRACE_CALL(); |
| 1447 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1448 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 1449 | |
| 1450 | switch (apiVersion) { |
| 1451 | case API_VERSION_1: |
| 1452 | case API_VERSION_2: |
| 1453 | break; |
| 1454 | default: |
| 1455 | ALOGE("%s: Bad API version %d", __FUNCTION__, apiVersion); |
| 1456 | return BAD_VALUE; |
| 1457 | } |
| 1458 | |
| 1459 | int facing = -1; |
| 1460 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 1461 | |
| 1462 | switch(deviceVersion) { |
| 1463 | case CAMERA_DEVICE_API_VERSION_1_0: |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1464 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 1465 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 1466 | if (apiVersion == API_VERSION_2) { |
| 1467 | ALOGV("%s: Camera id %d uses HAL prior to HAL3.2, doesn't support api2 without shim", |
| 1468 | __FUNCTION__, cameraId); |
| 1469 | return -EOPNOTSUPP; |
| 1470 | } else { // if (apiVersion == API_VERSION_1) { |
| 1471 | ALOGV("%s: Camera id %d uses older HAL before 3.2, but api1 is always supported", |
| 1472 | __FUNCTION__, cameraId); |
| 1473 | return OK; |
| 1474 | } |
| 1475 | case CAMERA_DEVICE_API_VERSION_3_2: |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1476 | case CAMERA_DEVICE_API_VERSION_3_3: |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1477 | ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly", |
| 1478 | __FUNCTION__, cameraId); |
| 1479 | return OK; |
| 1480 | case -1: |
| 1481 | ALOGE("%s: Invalid camera id %d", __FUNCTION__, cameraId); |
| 1482 | return BAD_VALUE; |
| 1483 | default: |
| 1484 | ALOGE("%s: Unknown camera device HAL version: %d", __FUNCTION__, deviceVersion); |
| 1485 | return INVALID_OPERATION; |
| 1486 | } |
| 1487 | |
| 1488 | return OK; |
| 1489 | } |
| 1490 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1491 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1492 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1493 | for (auto& i : mActiveClientManager.getAll()) { |
| 1494 | auto clientSp = i->getValue(); |
| 1495 | if (clientSp.get() == client) { |
| 1496 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1497 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1498 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1499 | } |
| 1500 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1501 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
| 1502 | const int callingPid = getCallingPid(); |
| 1503 | const int servicePid = getpid(); |
| 1504 | bool ret = false; |
| 1505 | { |
| 1506 | // Acquire mServiceLock and prevent other clients from connecting |
| 1507 | std::unique_ptr<AutoConditionLock> lock = |
| 1508 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1509 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1510 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1511 | std::vector<sp<BasicClient>> evicted; |
| 1512 | for (auto& i : mActiveClientManager.getAll()) { |
| 1513 | auto clientSp = i->getValue(); |
| 1514 | if (clientSp.get() == nullptr) { |
| 1515 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 1516 | mActiveClientManager.remove(i); |
| 1517 | continue; |
| 1518 | } |
| 1519 | if (remote == clientSp->getRemote() && (callingPid == servicePid || |
| 1520 | callingPid == clientSp->getClientPid())) { |
| 1521 | mActiveClientManager.remove(i); |
| 1522 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1523 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1524 | // Notify the client of disconnection |
| 1525 | clientSp->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 1526 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1527 | } |
| 1528 | } |
| 1529 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1530 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 1531 | // other clients from connecting in mServiceLockWrapper if held |
| 1532 | mServiceLock.unlock(); |
| 1533 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1534 | // Do not clear caller identity, remote caller should be client proccess |
| 1535 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1536 | for (auto& i : evicted) { |
| 1537 | if (i.get() != nullptr) { |
| 1538 | i->disconnect(); |
| 1539 | ret = true; |
| 1540 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1541 | } |
| 1542 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1543 | // Reacquire mServiceLock |
| 1544 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1545 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1546 | } // lock is destroyed, allow further connect calls |
| 1547 | |
| 1548 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1549 | } |
| 1550 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1551 | |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 1552 | /** |
| 1553 | * Check camera capabilities, such as support for basic color operation |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 1554 | * Also check that the device HAL version is still in support |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 1555 | */ |
| 1556 | int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) { |
Yin-Chia Yeh | 654b4bf | 2015-12-08 12:19:31 -0800 | [diff] [blame] | 1557 | // device_version undefined in CAMERA_MODULE_API_VERSION_1_0, |
| 1558 | // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible |
| 1559 | if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) { |
| 1560 | // Verify the device version is in the supported range |
| 1561 | switch (info.device_version) { |
| 1562 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 1563 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 1564 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 1565 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 1566 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 1567 | // in support |
| 1568 | break; |
| 1569 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 1570 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 1571 | // no longer supported |
| 1572 | default: |
| 1573 | ALOGE("%s: Device %d has HAL version %x, which is not supported", |
| 1574 | __FUNCTION__, id, info.device_version); |
| 1575 | String8 msg = String8::format( |
| 1576 | "Unsupported device HAL version %x for device %d", |
| 1577 | info.device_version, id); |
| 1578 | logServiceError(msg.string(), NO_INIT); |
| 1579 | return NO_INIT; |
| 1580 | } |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 1581 | } |
| 1582 | |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 1583 | // Assume all devices pre-v3.3 are backward-compatible |
| 1584 | bool isBackwardCompatible = true; |
| 1585 | if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0 |
| 1586 | && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) { |
| 1587 | isBackwardCompatible = false; |
| 1588 | status_t res; |
| 1589 | camera_metadata_ro_entry_t caps; |
| 1590 | res = find_camera_metadata_ro_entry( |
| 1591 | info.static_camera_characteristics, |
| 1592 | ANDROID_REQUEST_AVAILABLE_CAPABILITIES, |
| 1593 | &caps); |
| 1594 | if (res != 0) { |
| 1595 | ALOGW("%s: Unable to find camera capabilities for camera device %d", |
| 1596 | __FUNCTION__, id); |
| 1597 | caps.count = 0; |
| 1598 | } |
| 1599 | for (size_t i = 0; i < caps.count; i++) { |
| 1600 | if (caps.data.u8[i] == |
| 1601 | ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) { |
| 1602 | isBackwardCompatible = true; |
| 1603 | break; |
| 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | if (!isBackwardCompatible) { |
| 1609 | mNumberOfNormalCameras--; |
| 1610 | *latestStrangeCameraId = id; |
| 1611 | } else { |
| 1612 | if (id > *latestStrangeCameraId) { |
| 1613 | ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. " |
| 1614 | "This is not allowed due backward-compatibility requirements", |
| 1615 | __FUNCTION__, id, *latestStrangeCameraId); |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 1616 | logServiceError("Invalid order of camera devices", NO_INIT); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 1617 | mNumberOfCameras = 0; |
| 1618 | mNumberOfNormalCameras = 0; |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 1619 | return NO_INIT; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 1620 | } |
| 1621 | } |
| 1622 | return OK; |
| 1623 | } |
| 1624 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1625 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
| 1626 | const String8& cameraId) const { |
| 1627 | std::shared_ptr<CameraState> state; |
| 1628 | { |
| 1629 | Mutex::Autolock lock(mCameraStatesLock); |
| 1630 | auto iter = mCameraStates.find(cameraId); |
| 1631 | if (iter != mCameraStates.end()) { |
| 1632 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1633 | } |
| 1634 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1635 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1638 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) { |
| 1639 | // Remove from active clients list |
| 1640 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 1641 | if (clientDescriptorPtr == nullptr) { |
| 1642 | ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__, |
| 1643 | cameraId.string()); |
| 1644 | return sp<BasicClient>{nullptr}; |
| 1645 | } |
| 1646 | |
| 1647 | return clientDescriptorPtr->getValue(); |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1650 | void CameraService::doUserSwitch(const int32_t* newUserId, size_t length) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1651 | // Acquire mServiceLock and prevent other clients from connecting |
| 1652 | std::unique_ptr<AutoConditionLock> lock = |
| 1653 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 1654 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1655 | std::set<userid_t> newAllowedUsers; |
| 1656 | for (size_t i = 0; i < length; i++) { |
| 1657 | if (newUserId[i] < 0) { |
| 1658 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
| 1659 | __FUNCTION__, newUserId[i]); |
| 1660 | return; |
| 1661 | } |
| 1662 | newAllowedUsers.insert(static_cast<userid_t>(newUserId[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1665 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1666 | if (newAllowedUsers == mAllowedUsers) { |
| 1667 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 1668 | return; |
| 1669 | } |
| 1670 | |
| 1671 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 1672 | |
| 1673 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1674 | |
| 1675 | // Current user has switched, evict all current clients. |
| 1676 | std::vector<sp<BasicClient>> evicted; |
| 1677 | for (auto& i : mActiveClientManager.getAll()) { |
| 1678 | auto clientSp = i->getValue(); |
| 1679 | |
| 1680 | if (clientSp.get() == nullptr) { |
| 1681 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 1682 | continue; |
| 1683 | } |
| 1684 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1685 | // Don't evict clients that are still allowed. |
| 1686 | uid_t clientUid = clientSp->getClientUid(); |
| 1687 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 1688 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 1689 | continue; |
| 1690 | } |
| 1691 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1692 | evicted.push_back(clientSp); |
| 1693 | |
| 1694 | String8 curTime = getFormattedCurrentTime(); |
| 1695 | |
| 1696 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
| 1697 | i->getKey().string()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1698 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1699 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1700 | logEvent(String8::format("EVICT device %s client held by package %s (PID %" |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1701 | PRId32 ", priority %" PRId32 ")\n - Evicted due to user switch.", |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1702 | i->getKey().string(), String8{clientSp->getPackageName()}.string(), |
| 1703 | i->getOwnerId(), i->getPriority())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1704 | |
| 1705 | } |
| 1706 | |
| 1707 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 1708 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 1709 | mServiceLock.unlock(); |
| 1710 | |
| 1711 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
| 1712 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 1713 | |
| 1714 | for (auto& i : evicted) { |
| 1715 | i->disconnect(); |
| 1716 | } |
| 1717 | |
| 1718 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 1719 | |
| 1720 | // Reacquire mServiceLock |
| 1721 | mServiceLock.lock(); |
| 1722 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1723 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1724 | void CameraService::logEvent(const char* event) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1725 | String8 curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1726 | Mutex::Autolock l(mLogLock); |
| 1727 | mEventLog.add(String8::format("%s : %s", curTime.string(), event)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1730 | void CameraService::logDisconnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1731 | const char* clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1732 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1733 | 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] | 1734 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | void CameraService::logConnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1738 | const char* clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1739 | // Log the clients evicted |
| 1740 | 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] | 1741 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | void CameraService::logRejected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1745 | const char* clientPackage, const char* reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1746 | // Log the client rejected |
| 1747 | 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] | 1748 | cameraId, clientPackage, clientPid, reason)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1751 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 1752 | const std::set<userid_t>& newUserIds) { |
| 1753 | String8 newUsers = toString(newUserIds); |
| 1754 | String8 oldUsers = toString(oldUserIds); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1755 | // Log the new and old users |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1756 | logEvent(String8::format("USER_SWITCH previous allowed users: %s , current allowed users: %s", |
| 1757 | oldUsers.string(), newUsers.string())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) { |
| 1761 | // Log the device removal |
| 1762 | logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason)); |
| 1763 | } |
| 1764 | |
| 1765 | void CameraService::logDeviceAdded(const char* cameraId, const char* reason) { |
| 1766 | // Log the device removal |
| 1767 | logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason)); |
| 1768 | } |
| 1769 | |
| 1770 | void CameraService::logClientDied(int clientPid, const char* reason) { |
| 1771 | // Log the device removal |
| 1772 | 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] | 1773 | } |
| 1774 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 1775 | void CameraService::logServiceError(const char* msg, int errorCode) { |
| 1776 | String8 curTime = getFormattedCurrentTime(); |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 1777 | 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] | 1778 | } |
| 1779 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1780 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 1781 | uint32_t flags) { |
| 1782 | |
| 1783 | const int pid = getCallingPid(); |
| 1784 | const int selfPid = getpid(); |
| 1785 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1786 | // Permission checks |
| 1787 | switch (code) { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1788 | case BnCameraService::CONNECT: |
| 1789 | case BnCameraService::CONNECT_DEVICE: |
| 1790 | case BnCameraService::CONNECT_LEGACY: { |
| 1791 | if (pid != selfPid) { |
| 1792 | // we're called from a different process, do the real check |
| 1793 | if (!checkCallingPermission( |
| 1794 | String16("android.permission.CAMERA"))) { |
| 1795 | const int uid = getCallingUid(); |
| 1796 | ALOGE("Permission Denial: " |
| 1797 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 1798 | return PERMISSION_DENIED; |
| 1799 | } |
| 1800 | } |
| 1801 | break; |
| 1802 | } |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1803 | case BnCameraService::NOTIFY_SYSTEM_EVENT: { |
| 1804 | if (pid != selfPid) { |
| 1805 | // Ensure we're being called by system_server, or similar process with |
| 1806 | // permissions to notify the camera service about system events |
| 1807 | if (!checkCallingPermission( |
| 1808 | String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) { |
| 1809 | const int uid = getCallingUid(); |
| 1810 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 1811 | " events from pid=%d, uid=%d", pid, uid); |
| 1812 | return PERMISSION_DENIED; |
| 1813 | } |
| 1814 | } |
| 1815 | break; |
| 1816 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | return BnCameraService::onTransact(code, data, reply, flags); |
| 1820 | } |
| 1821 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1822 | // We share the media players for shutter and recording sound for all clients. |
| 1823 | // A reference count is kept to determine when we will actually release the |
| 1824 | // media players. |
| 1825 | |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 1826 | MediaPlayer* CameraService::newMediaPlayer(const char *file) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1827 | MediaPlayer* mp = new MediaPlayer(); |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 1828 | if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 1829 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1830 | mp->prepare(); |
| 1831 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1832 | ALOGE("Failed to load CameraService sounds: %s", file); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1833 | return NULL; |
| 1834 | } |
| 1835 | return mp; |
| 1836 | } |
| 1837 | |
| 1838 | void CameraService::loadSound() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1839 | ATRACE_CALL(); |
| 1840 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1841 | Mutex::Autolock lock(mSoundLock); |
| 1842 | LOG1("CameraService::loadSound ref=%d", mSoundRef); |
| 1843 | if (mSoundRef++) return; |
| 1844 | |
| 1845 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
Chien-Yu Chen | 82104eb | 2015-10-14 11:29:31 -0700 | [diff] [blame] | 1846 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
| 1847 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | void CameraService::releaseSound() { |
| 1851 | Mutex::Autolock lock(mSoundLock); |
| 1852 | LOG1("CameraService::releaseSound ref=%d", mSoundRef); |
| 1853 | if (--mSoundRef) return; |
| 1854 | |
| 1855 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 1856 | if (mSoundPlayer[i] != 0) { |
| 1857 | mSoundPlayer[i]->disconnect(); |
| 1858 | mSoundPlayer[i].clear(); |
| 1859 | } |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1864 | ATRACE_CALL(); |
| 1865 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1866 | LOG1("playSound(%d)", kind); |
| 1867 | Mutex::Autolock lock(mSoundLock); |
| 1868 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 1869 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 1870 | player->seekTo(0); |
| 1871 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | // ---------------------------------------------------------------------------- |
| 1876 | |
| 1877 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 1878 | const sp<ICameraClient>& cameraClient, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1879 | const String16& clientPackageName, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1880 | int cameraId, int cameraFacing, |
| 1881 | int clientPid, uid_t clientUid, |
| 1882 | int servicePid) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 1883 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1884 | IInterface::asBinder(cameraClient), |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1885 | clientPackageName, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1886 | cameraId, cameraFacing, |
| 1887 | clientPid, clientUid, |
| 1888 | servicePid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1889 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1890 | int callingPid = getCallingPid(); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1891 | LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1892 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1893 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1894 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1895 | cameraService->loadSound(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1896 | |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1897 | LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1900 | // tear down the client |
| 1901 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1902 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1903 | mDestructionStarted = true; |
| 1904 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 1905 | mCameraService->releaseSound(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 1906 | // unconditionally disconnect. function is idempotent |
| 1907 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1910 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1911 | const sp<IBinder>& remoteCallback, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1912 | const String16& clientPackageName, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1913 | int cameraId, int cameraFacing, |
| 1914 | int clientPid, uid_t clientUid, |
| 1915 | int servicePid): |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1916 | mClientPackageName(clientPackageName), mDisconnected(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1917 | { |
| 1918 | mCameraService = cameraService; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1919 | mRemoteBinder = remoteCallback; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1920 | mCameraId = cameraId; |
| 1921 | mCameraFacing = cameraFacing; |
| 1922 | mClientPid = clientPid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1923 | mClientUid = clientUid; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1924 | mServicePid = servicePid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1925 | mOpsActive = false; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1926 | mDestructionStarted = false; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame^] | 1927 | |
| 1928 | // In some cases the calling code has no access to the package it runs under. |
| 1929 | // For example, NDK camera API. |
| 1930 | // In this case we will get the packages for the calling UID and pick the first one |
| 1931 | // for attributing the app op. This will work correctly for runtime permissions |
| 1932 | // as for legacy apps we will toggle the app op for all packages in the UID. |
| 1933 | // The caveat is that the operation may be attributed to the wrong package and |
| 1934 | // stats based on app ops may be slightly off. |
| 1935 | if (mClientPackageName.size() <= 0) { |
| 1936 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1937 | sp<IBinder> binder = sm->getService(String16(kPermissionServiceName)); |
| 1938 | if (binder == 0) { |
| 1939 | ALOGE("Cannot get permission service"); |
| 1940 | // Leave mClientPackageName unchanged (empty) and the further interaction |
| 1941 | // with camera will fail in BasicClient::startCameraOps |
| 1942 | return; |
| 1943 | } |
| 1944 | |
| 1945 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 1946 | Vector<String16> packages; |
| 1947 | |
| 1948 | permCtrl->getPackagesForUid(mClientUid, packages); |
| 1949 | |
| 1950 | if (packages.isEmpty()) { |
| 1951 | ALOGE("No packages for calling UID"); |
| 1952 | // Leave mClientPackageName unchanged (empty) and the further interaction |
| 1953 | // with camera will fail in BasicClient::startCameraOps |
| 1954 | return; |
| 1955 | } |
| 1956 | mClientPackageName = packages[0]; |
| 1957 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1961 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1962 | mDestructionStarted = true; |
| 1963 | } |
| 1964 | |
| 1965 | void CameraService::BasicClient::disconnect() { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1966 | if (mDisconnected) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1967 | return; |
| 1968 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1969 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1970 | |
| 1971 | mCameraService->removeByClient(this); |
| 1972 | mCameraService->logDisconnected(String8::format("%d", mCameraId), mClientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1973 | String8(mClientPackageName)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1974 | |
| 1975 | sp<IBinder> remote = getRemote(); |
| 1976 | if (remote != nullptr) { |
| 1977 | remote->unlinkToDeath(mCameraService); |
| 1978 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1979 | |
| 1980 | finishCameraOps(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1981 | ALOGI("%s: Disconnected client for camera %d for PID %d", __FUNCTION__, mCameraId, mClientPid); |
| 1982 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1983 | // client shouldn't be able to call into us anymore |
| 1984 | mClientPid = 0; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1985 | } |
| 1986 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 1987 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 1988 | // No dumping of clients directly over Binder, |
| 1989 | // must go through CameraService::dump |
| 1990 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
| 1991 | IPCThreadState::self()->getCallingUid(), NULL, 0); |
| 1992 | return OK; |
| 1993 | } |
| 1994 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1995 | String16 CameraService::BasicClient::getPackageName() const { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1996 | return mClientPackageName; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | |
| 2000 | int CameraService::BasicClient::getClientPid() const { |
| 2001 | return mClientPid; |
| 2002 | } |
| 2003 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2004 | uid_t CameraService::BasicClient::getClientUid() const { |
| 2005 | return mClientUid; |
| 2006 | } |
| 2007 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2008 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 2009 | // Defaults to API2. |
| 2010 | return level == API_2; |
| 2011 | } |
| 2012 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2013 | status_t CameraService::BasicClient::startCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2014 | ATRACE_CALL(); |
| 2015 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2016 | int32_t res; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2017 | // Notify app ops that the camera is not available |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2018 | mOpsCallback = new OpsCallback(this); |
| 2019 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2020 | { |
| 2021 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2022 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2023 | } |
| 2024 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2025 | mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2026 | mClientPackageName, mOpsCallback); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2027 | res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2028 | mClientUid, mClientPackageName); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2029 | |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 2030 | if (res == AppOpsManager::MODE_ERRORED) { |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2031 | ALOGI("Camera %d: Access for \"%s\" has been revoked", |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2032 | mCameraId, String8(mClientPackageName).string()); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2033 | return PERMISSION_DENIED; |
| 2034 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2035 | |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 2036 | if (res == AppOpsManager::MODE_IGNORED) { |
| 2037 | ALOGI("Camera %d: Access for \"%s\" has been restricted", |
| 2038 | mCameraId, String8(mClientPackageName).string()); |
Eino-Ville Talvala | e3afb2c | 2015-06-03 16:03:30 -0700 | [diff] [blame] | 2039 | // Return the same error as for device policy manager rejection |
| 2040 | return -EACCES; |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 2041 | } |
| 2042 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2043 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2044 | |
| 2045 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
| 2046 | mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2047 | String8::format("%d", mCameraId)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2048 | |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 2049 | // Transition device state to OPEN |
| 2050 | mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN, |
| 2051 | String8::format("%d", mCameraId)); |
| 2052 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2053 | return OK; |
| 2054 | } |
| 2055 | |
| 2056 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2057 | ATRACE_CALL(); |
| 2058 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2059 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2060 | if (mOpsActive) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2061 | // Notify app ops that the camera is available again |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2062 | mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2063 | mClientPackageName); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2064 | mOpsActive = false; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2065 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2066 | auto rejected = {ICameraServiceListener::STATUS_NOT_PRESENT, |
| 2067 | ICameraServiceListener::STATUS_ENUMERATING}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2068 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2069 | // Transition to PRESENT if the camera is not in either of the rejected states |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2070 | mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2071 | String8::format("%d", mCameraId), rejected); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2072 | |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 2073 | // Transition device state to CLOSED |
| 2074 | mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED, |
| 2075 | String8::format("%d", mCameraId)); |
| 2076 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2077 | // Notify flashlight that a camera device is closed. |
| 2078 | mCameraService->mFlashlight->deviceClosed( |
| 2079 | String8::format("%d", mCameraId)); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2080 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2081 | // Always stop watching, even if no camera op is active |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 2082 | if (mOpsCallback != NULL) { |
| 2083 | mAppOpsManager.stopWatchingMode(mOpsCallback); |
| 2084 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2085 | mOpsCallback.clear(); |
| 2086 | |
| 2087 | return OK; |
| 2088 | } |
| 2089 | |
| 2090 | void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2091 | ATRACE_CALL(); |
| 2092 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2093 | String8 name(packageName); |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2094 | String8 myName(mClientPackageName); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2095 | |
| 2096 | if (op != AppOpsManager::OP_CAMERA) { |
| 2097 | ALOGW("Unexpected app ops notification received: %d", op); |
| 2098 | return; |
| 2099 | } |
| 2100 | |
| 2101 | int32_t res; |
| 2102 | res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2103 | mClientUid, mClientPackageName); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2104 | ALOGV("checkOp returns: %d, %s ", res, |
| 2105 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 2106 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 2107 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 2108 | "UNKNOWN"); |
| 2109 | |
| 2110 | if (res != AppOpsManager::MODE_ALLOWED) { |
| 2111 | ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId, |
| 2112 | myName.string()); |
| 2113 | // Reset the client PID to allow server-initiated disconnect, |
| 2114 | // and to prevent further calls by client. |
| 2115 | mClientPid = getCallingPid(); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2116 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 2117 | notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2118 | disconnect(); |
| 2119 | } |
| 2120 | } |
| 2121 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2122 | // ---------------------------------------------------------------------------- |
| 2123 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2124 | // Provide client strong pointer for callbacks. |
| 2125 | sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) { |
| 2126 | String8 cameraId = String8::format("%d", (int)(intptr_t) user); |
| 2127 | auto clientDescriptor = gCameraService->mActiveClientManager.get(cameraId); |
| 2128 | if (clientDescriptor != nullptr) { |
| 2129 | return sp<Client>{ |
| 2130 | static_cast<Client*>(clientDescriptor->getValue().get())}; |
| 2131 | } |
| 2132 | return sp<Client>{nullptr}; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2133 | } |
| 2134 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2135 | void CameraService::Client::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 2136 | const CaptureResultExtras& resultExtras) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 2137 | (void) errorCode; |
| 2138 | (void) resultExtras; |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 2139 | if (mRemoteCallback != NULL) { |
| 2140 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); |
| 2141 | } else { |
| 2142 | ALOGE("mRemoteCallback is NULL!!"); |
| 2143 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2144 | } |
| 2145 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 2146 | // NOTE: function is idempotent |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 2147 | void CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 2148 | ALOGV("Client::disconnect"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2149 | BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 2150 | } |
| 2151 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2152 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 2153 | return level == API_1; |
| 2154 | } |
| 2155 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2156 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 2157 | mClient(client) { |
| 2158 | } |
| 2159 | |
| 2160 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 2161 | const String16& packageName) { |
| 2162 | sp<BasicClient> client = mClient.promote(); |
| 2163 | if (client != NULL) { |
| 2164 | client->opChanged(op, packageName); |
| 2165 | } |
| 2166 | } |
| 2167 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2168 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2169 | // CameraState |
| 2170 | // ---------------------------------------------------------------------------- |
| 2171 | |
| 2172 | CameraService::CameraState::CameraState(const String8& id, int cost, |
| 2173 | const std::set<String8>& conflicting) : mId(id), |
| 2174 | mStatus(ICameraServiceListener::STATUS_PRESENT), mCost(cost), mConflicting(conflicting) {} |
| 2175 | |
| 2176 | CameraService::CameraState::~CameraState() {} |
| 2177 | |
| 2178 | ICameraServiceListener::Status CameraService::CameraState::getStatus() const { |
| 2179 | Mutex::Autolock lock(mStatusLock); |
| 2180 | return mStatus; |
| 2181 | } |
| 2182 | |
| 2183 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 2184 | return mShimParams; |
| 2185 | } |
| 2186 | |
| 2187 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 2188 | mShimParams = params; |
| 2189 | } |
| 2190 | |
| 2191 | int CameraService::CameraState::getCost() const { |
| 2192 | return mCost; |
| 2193 | } |
| 2194 | |
| 2195 | std::set<String8> CameraService::CameraState::getConflicting() const { |
| 2196 | return mConflicting; |
| 2197 | } |
| 2198 | |
| 2199 | String8 CameraService::CameraState::getId() const { |
| 2200 | return mId; |
| 2201 | } |
| 2202 | |
| 2203 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2204 | // ClientEventListener |
| 2205 | // ---------------------------------------------------------------------------- |
| 2206 | |
| 2207 | void CameraService::ClientEventListener::onClientAdded( |
| 2208 | const resource_policy::ClientDescriptor<String8, |
| 2209 | sp<CameraService::BasicClient>>& descriptor) { |
| 2210 | auto basicClient = descriptor.getValue(); |
| 2211 | if (basicClient.get() != nullptr) { |
| 2212 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 2213 | notifier.noteStartCamera(descriptor.getKey(), |
| 2214 | static_cast<int>(basicClient->getClientUid())); |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | void CameraService::ClientEventListener::onClientRemoved( |
| 2219 | const resource_policy::ClientDescriptor<String8, |
| 2220 | sp<CameraService::BasicClient>>& descriptor) { |
| 2221 | auto basicClient = descriptor.getValue(); |
| 2222 | if (basicClient.get() != nullptr) { |
| 2223 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 2224 | notifier.noteStopCamera(descriptor.getKey(), |
| 2225 | static_cast<int>(basicClient->getClientUid())); |
| 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | |
| 2230 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2231 | // CameraClientManager |
| 2232 | // ---------------------------------------------------------------------------- |
| 2233 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2234 | CameraService::CameraClientManager::CameraClientManager() { |
| 2235 | setListener(std::make_shared<ClientEventListener>()); |
| 2236 | } |
| 2237 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2238 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 2239 | |
| 2240 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
| 2241 | const String8& id) const { |
| 2242 | auto descriptor = get(id); |
| 2243 | if (descriptor == nullptr) { |
| 2244 | return sp<BasicClient>{nullptr}; |
| 2245 | } |
| 2246 | return descriptor->getValue(); |
| 2247 | } |
| 2248 | |
| 2249 | String8 CameraService::CameraClientManager::toString() const { |
| 2250 | auto all = getAll(); |
| 2251 | String8 ret("["); |
| 2252 | bool hasAny = false; |
| 2253 | for (auto& i : all) { |
| 2254 | hasAny = true; |
| 2255 | String8 key = i->getKey(); |
| 2256 | int32_t cost = i->getCost(); |
| 2257 | int32_t pid = i->getOwnerId(); |
| 2258 | int32_t priority = i->getPriority(); |
| 2259 | auto conflicting = i->getConflicting(); |
| 2260 | auto clientSp = i->getValue(); |
| 2261 | String8 packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 2262 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2263 | if (clientSp.get() != nullptr) { |
| 2264 | packageName = String8{clientSp->getPackageName()}; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2265 | uid_t clientUid = clientSp->getClientUid(); |
| 2266 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2267 | } |
| 2268 | ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %" |
| 2269 | PRId32 ", ", key.string(), cost, pid, priority); |
| 2270 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2271 | if (clientSp.get() != nullptr) { |
| 2272 | ret.appendFormat("User Id: %d, ", clientUserId); |
| 2273 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2274 | if (packageName.size() != 0) { |
| 2275 | ret.appendFormat("Client Package Name: %s", packageName.string()); |
| 2276 | } |
| 2277 | |
| 2278 | ret.append(", Conflicting Client Devices: {"); |
| 2279 | for (auto& j : conflicting) { |
| 2280 | ret.appendFormat("%s, ", j.string()); |
| 2281 | } |
| 2282 | ret.append("})"); |
| 2283 | } |
| 2284 | if (hasAny) ret.append("\n"); |
| 2285 | ret.append("]\n"); |
| 2286 | return ret; |
| 2287 | } |
| 2288 | |
| 2289 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
| 2290 | const String8& key, const sp<BasicClient>& value, int32_t cost, |
| 2291 | const std::set<String8>& conflictingKeys, int32_t priority, int32_t ownerId) { |
| 2292 | |
| 2293 | return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>( |
| 2294 | key, value, cost, conflictingKeys, priority, ownerId); |
| 2295 | } |
| 2296 | |
| 2297 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
| 2298 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) { |
| 2299 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
| 2300 | partial->getConflicting(), partial->getPriority(), partial->getOwnerId()); |
| 2301 | } |
| 2302 | |
| 2303 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2304 | |
| 2305 | static const int kDumpLockRetries = 50; |
| 2306 | static const int kDumpLockSleep = 60000; |
| 2307 | |
| 2308 | static bool tryLock(Mutex& mutex) |
| 2309 | { |
| 2310 | bool locked = false; |
| 2311 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 2312 | if (mutex.tryLock() == NO_ERROR) { |
| 2313 | locked = true; |
| 2314 | break; |
| 2315 | } |
| 2316 | usleep(kDumpLockSleep); |
| 2317 | } |
| 2318 | return locked; |
| 2319 | } |
| 2320 | |
| 2321 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2322 | ATRACE_CALL(); |
| 2323 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2324 | String8 result("Dump of the Camera Service:\n"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2325 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
dcashman | fefa614 | 2015-09-09 13:43:01 -0700 | [diff] [blame] | 2326 | result = result.format("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2327 | "can't dump CameraService from pid=%d, uid=%d\n", |
| 2328 | getCallingPid(), |
| 2329 | getCallingUid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2330 | write(fd, result.string(), result.size()); |
| 2331 | } else { |
| 2332 | bool locked = tryLock(mServiceLock); |
| 2333 | // failed to lock - CameraService is probably deadlocked |
| 2334 | if (!locked) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 2335 | result.append("CameraService may be deadlocked\n"); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2336 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | bool hasClient = false; |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2340 | if (!mModule) { |
| 2341 | result = String8::format("No camera module available!\n"); |
| 2342 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 2343 | |
| 2344 | // Dump event log for error information |
| 2345 | dumpEventLog(fd); |
| 2346 | |
Kalle Lampila | 6ec3a15 | 2013-04-30 15:27:19 +0300 | [diff] [blame] | 2347 | if (locked) mServiceLock.unlock(); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2348 | return NO_ERROR; |
| 2349 | } |
| 2350 | |
Chien-Yu Chen | 676b21b | 2015-02-24 10:28:19 -0800 | [diff] [blame] | 2351 | result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion()); |
| 2352 | result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion()); |
| 2353 | result.appendFormat("Camera module name: %s\n", mModule->getModuleName()); |
| 2354 | result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2355 | result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras); |
Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 2356 | result.appendFormat("Number of normal camera devices: %d\n", mNumberOfNormalCameras); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2357 | String8 activeClientString = mActiveClientManager.toString(); |
| 2358 | result.appendFormat("Active Camera Clients:\n%s", activeClientString.string()); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2359 | result.appendFormat("Allowed users:\n%s\n", toString(mAllowedUsers).string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2360 | |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 2361 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 2362 | if (desc == NULL) { |
| 2363 | result.appendFormat("Vendor tags left unimplemented.\n"); |
| 2364 | } else { |
| 2365 | result.appendFormat("Vendor tag definitions:\n"); |
| 2366 | } |
| 2367 | |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2368 | write(fd, result.string(), result.size()); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 2369 | |
| 2370 | if (desc != NULL) { |
| 2371 | desc->dump(fd, /*verbosity*/2, /*indentation*/4); |
| 2372 | } |
| 2373 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 2374 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2375 | |
| 2376 | bool stateLocked = tryLock(mCameraStatesLock); |
| 2377 | if (!stateLocked) { |
| 2378 | result = String8::format("CameraStates in use, may be deadlocked\n"); |
| 2379 | write(fd, result.string(), result.size()); |
| 2380 | } |
| 2381 | |
| 2382 | for (auto& state : mCameraStates) { |
| 2383 | String8 cameraId = state.first; |
| 2384 | result = String8::format("Camera %s information:\n", cameraId.string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2385 | camera_info info; |
| 2386 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2387 | // TODO: Change getCameraInfo + HAL to use String cameraIds |
| 2388 | status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2389 | if (rc != OK) { |
| 2390 | result.appendFormat(" Error reading static information!\n"); |
| 2391 | write(fd, result.string(), result.size()); |
| 2392 | } else { |
| 2393 | result.appendFormat(" Facing: %s\n", |
| 2394 | info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT"); |
| 2395 | result.appendFormat(" Orientation: %d\n", info.orientation); |
| 2396 | int deviceVersion; |
Chien-Yu Chen | 676b21b | 2015-02-24 10:28:19 -0800 | [diff] [blame] | 2397 | if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2398 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 2399 | } else { |
| 2400 | deviceVersion = info.device_version; |
| 2401 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2402 | |
| 2403 | auto conflicting = state.second->getConflicting(); |
| 2404 | result.appendFormat(" Resource Cost: %d\n", state.second->getCost()); |
| 2405 | result.appendFormat(" Conflicting Devices:"); |
| 2406 | for (auto& id : conflicting) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 2407 | result.appendFormat(" %s", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2408 | } |
| 2409 | if (conflicting.size() == 0) { |
| 2410 | result.appendFormat(" NONE"); |
| 2411 | } |
| 2412 | result.appendFormat("\n"); |
| 2413 | |
| 2414 | result.appendFormat(" Device version: %#x\n", deviceVersion); |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 2415 | if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_0) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2416 | result.appendFormat(" Device static metadata:\n"); |
| 2417 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 428b77a | 2012-07-30 09:55:30 -0700 | [diff] [blame] | 2418 | dump_indented_camera_metadata(info.static_camera_characteristics, |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 2419 | fd, /*verbosity*/2, /*indentation*/4); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2420 | } else { |
| 2421 | write(fd, result.string(), result.size()); |
| 2422 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2423 | |
| 2424 | CameraParameters p = state.second->getShimParams(); |
| 2425 | if (!p.isEmpty()) { |
| 2426 | result = String8::format(" Camera1 API shim is using parameters:\n "); |
| 2427 | write(fd, result.string(), result.size()); |
| 2428 | p.dump(fd, args); |
| 2429 | } |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2432 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 2433 | if (clientDescriptor == nullptr) { |
| 2434 | result = String8::format(" Device %s is closed, no client instance\n", |
| 2435 | cameraId.string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2436 | write(fd, result.string(), result.size()); |
| 2437 | continue; |
| 2438 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2439 | hasClient = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2440 | result = String8::format(" Device %s is open. Client instance dump:\n\n", |
| 2441 | cameraId.string()); |
| 2442 | result.appendFormat("Client priority level: %d\n", clientDescriptor->getPriority()); |
| 2443 | result.appendFormat("Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 2444 | |
| 2445 | auto client = clientDescriptor->getValue(); |
| 2446 | result.appendFormat("Client package: %s\n", |
| 2447 | String8(client->getPackageName()).string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2448 | write(fd, result.string(), result.size()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2449 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 2450 | client->dumpClient(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2451 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2452 | |
| 2453 | if (stateLocked) mCameraStatesLock.unlock(); |
| 2454 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2455 | if (!hasClient) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2456 | result = String8::format("\nNo active camera clients yet.\n"); |
| 2457 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | if (locked) mServiceLock.unlock(); |
| 2461 | |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 2462 | // Dump camera traces if there were any |
| 2463 | write(fd, "\n", 1); |
| 2464 | camera3::CameraTraces::dump(fd, args); |
| 2465 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2466 | // change logging level |
| 2467 | int n = args.size(); |
| 2468 | for (int i = 0; i + 1 < n; i++) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 2469 | String16 verboseOption("-v"); |
| 2470 | if (args[i] == verboseOption) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2471 | String8 levelStr(args[i+1]); |
| 2472 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2473 | result = String8::format("\nSetting log level to %d.\n", level); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2474 | setLogLevel(level); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2475 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | } |
| 2479 | return NO_ERROR; |
| 2480 | } |
| 2481 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 2482 | void CameraService::dumpEventLog(int fd) { |
| 2483 | String8 result = String8("\nPrior client events (most recent at top):\n"); |
| 2484 | |
| 2485 | Mutex::Autolock l(mLogLock); |
| 2486 | for (const auto& msg : mEventLog) { |
| 2487 | result.appendFormat(" %s\n", msg.string()); |
| 2488 | } |
| 2489 | |
| 2490 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
| 2491 | result.append(" ...\n"); |
| 2492 | } else if (mEventLog.size() == 0) { |
| 2493 | result.append(" [no events yet]\n"); |
| 2494 | } |
| 2495 | result.append("\n"); |
| 2496 | |
| 2497 | write(fd, result.string(), result.size()); |
| 2498 | } |
| 2499 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2500 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2501 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2502 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 2503 | if (mTorchClientMap[i] == who) { |
| 2504 | // 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] | 2505 | String8 cameraId = mTorchClientMap.keyAt(i); |
| 2506 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 2507 | if (res) { |
| 2508 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 2509 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 2510 | return; |
| 2511 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2512 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2513 | break; |
| 2514 | } |
| 2515 | } |
| 2516 | } |
| 2517 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2518 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2519 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 2520 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2521 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 2522 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 2523 | */ |
| 2524 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2525 | logClientDied(getCallingPid(), String8("Binder died unexpectedly")); |
| 2526 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2527 | // check torch client |
| 2528 | handleTorchClientBinderDied(who); |
| 2529 | |
| 2530 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2531 | if(!evictClientIdByRemote(who)) { |
| 2532 | 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] | 2533 | return; |
| 2534 | } |
| 2535 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2536 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 2537 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2538 | } |
| 2539 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2540 | void CameraService::updateStatus(ICameraServiceListener::Status status, const String8& cameraId) { |
| 2541 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2542 | } |
| 2543 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2544 | void CameraService::updateStatus(ICameraServiceListener::Status status, const String8& cameraId, |
| 2545 | std::initializer_list<ICameraServiceListener::Status> rejectSourceStates) { |
| 2546 | // Do not lock mServiceLock here or can get into a deadlock from |
| 2547 | // connect() -> disconnect -> updateStatus |
| 2548 | |
| 2549 | auto state = getCameraState(cameraId); |
| 2550 | |
| 2551 | if (state == nullptr) { |
| 2552 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 2553 | cameraId.string()); |
| 2554 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2557 | // Update the status for this camera state, then send the onStatusChangedCallbacks to each |
| 2558 | // of the listeners with both the mStatusStatus and mStatusListenerLock held |
| 2559 | state->updateStatus(status, cameraId, rejectSourceStates, [this] |
| 2560 | (const String8& cameraId, ICameraServiceListener::Status status) { |
| 2561 | |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 2562 | if (status != ICameraServiceListener::STATUS_ENUMERATING) { |
| 2563 | // Update torch status if it has a flash unit. |
| 2564 | Mutex::Autolock al(mTorchStatusMutex); |
| 2565 | ICameraServiceListener::TorchStatus torchStatus; |
| 2566 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 2567 | NAME_NOT_FOUND) { |
| 2568 | ICameraServiceListener::TorchStatus newTorchStatus = |
| 2569 | status == ICameraServiceListener::STATUS_PRESENT ? |
| 2570 | ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF : |
| 2571 | ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 2572 | if (torchStatus != newTorchStatus) { |
| 2573 | onTorchStatusChangedLocked(cameraId, newTorchStatus); |
| 2574 | } |
| 2575 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | Mutex::Autolock lock(mStatusListenerLock); |
| 2579 | |
| 2580 | for (auto& listener : mListenerList) { |
| 2581 | // TODO: Refactor status listeners to use strings for Camera IDs and remove this. |
| 2582 | int id = cameraIdToInt(cameraId); |
| 2583 | if (id != -1) listener->onStatusChanged(status, id); |
| 2584 | } |
| 2585 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2586 | } |
| 2587 | |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 2588 | void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState, |
| 2589 | const String8& cameraId) { |
| 2590 | sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy(); |
| 2591 | if (proxyBinder == nullptr) return; |
| 2592 | String16 id(cameraId); |
| 2593 | proxyBinder->notifyCameraState(id, newState); |
| 2594 | } |
| 2595 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2596 | status_t CameraService::getTorchStatusLocked( |
| 2597 | const String8& cameraId, |
| 2598 | ICameraServiceListener::TorchStatus *status) const { |
| 2599 | if (!status) { |
| 2600 | return BAD_VALUE; |
| 2601 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2602 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 2603 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2604 | // invalid camera ID or the camera doesn't have a flash unit |
| 2605 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2606 | } |
| 2607 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2608 | *status = mTorchStatusMap.valueAt(index); |
| 2609 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2610 | } |
| 2611 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2612 | status_t CameraService::setTorchStatusLocked(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2613 | ICameraServiceListener::TorchStatus status) { |
| 2614 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 2615 | if (index == NAME_NOT_FOUND) { |
| 2616 | return BAD_VALUE; |
| 2617 | } |
| 2618 | ICameraServiceListener::TorchStatus& item = |
| 2619 | mTorchStatusMap.editValueAt(index); |
| 2620 | item = status; |
| 2621 | |
| 2622 | return OK; |
| 2623 | } |
| 2624 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2625 | }; // namespace android |