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