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