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