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