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