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