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