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