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