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