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