| 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> | 
| Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 24 | #include <cstdlib> | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 25 | #include <cstring> | 
|  | 26 | #include <ctime> | 
|  | 27 | #include <string> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 28 | #include <sys/types.h> | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 29 | #include <inttypes.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 30 | #include <pthread.h> | 
|  | 31 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 32 | #include <android/hardware/ICamera.h> | 
|  | 33 | #include <android/hardware/ICameraClient.h> | 
|  | 34 |  | 
| Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 35 | #include <android-base/macros.h> | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 36 | #include <android-base/parseint.h> | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 38 | #include <binder/ActivityManager.h> | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 39 | #include <binder/AppOpsManager.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | #include <binder/IPCThreadState.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 41 | #include <binder/MemoryBase.h> | 
|  | 42 | #include <binder/MemoryHeapBase.h> | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 43 | #include <binder/PermissionController.h> | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 44 | #include <binder/ProcessInfoService.h> | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 45 | #include <binder/IResultReceiver.h> | 
| Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 46 | #include <binderthreadstate/CallerUtils.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 47 | #include <cutils/atomic.h> | 
| Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 48 | #include <cutils/properties.h> | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 49 | #include <cutils/misc.h> | 
| Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 50 | #include <gui/Surface.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 51 | #include <hardware/hardware.h> | 
| Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 52 | #include "hidl/HidlCameraService.h" | 
|  | 53 | #include <hidl/HidlTransportSupport.h> | 
| Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 54 | #include <hwbinder/IPCThreadState.h> | 
| Eino-Ville Talvala | d89821e | 2016-04-20 11:23:50 -0700 | [diff] [blame] | 55 | #include <memunreachable/memunreachable.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 56 | #include <media/AudioSystem.h> | 
| Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 57 | #include <media/IMediaHTTPService.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 | #include <media/mediaplayer.h> | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 59 | #include <mediautils/BatteryNotifier.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 60 | #include <utils/Errors.h> | 
|  | 61 | #include <utils/Log.h> | 
|  | 62 | #include <utils/String16.h> | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 63 | #include <utils/SystemClock.h> | 
| Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 64 | #include <utils/Trace.h> | 
| Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 65 | #include <utils/CallStack.h> | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 66 | #include <private/android_filesystem_config.h> | 
| Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 67 | #include <system/camera_vendor_tags.h> | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 68 | #include <system/camera_metadata.h> | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 69 |  | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 70 | #include <system/camera.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 71 |  | 
|  | 72 | #include "CameraService.h" | 
| Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 73 | #include "api1/Camera2Client.h" | 
| Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 74 | #include "api2/CameraDeviceClient.h" | 
| Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 75 | #include "utils/CameraTraces.h" | 
| Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 76 | #include "utils/TagMonitor.h" | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 77 | #include "utils/CameraThreadState.h" | 
| Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 78 | #include "utils/CameraServiceProxyWrapper.h" | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 79 |  | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 80 | namespace { | 
|  | 81 | const char* kPermissionServiceName = "permission"; | 
|  | 82 | }; // namespace anonymous | 
|  | 83 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 84 | namespace android { | 
|  | 85 |  | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 86 | using base::StringPrintf; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 87 | using binder::Status; | 
| Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 88 | using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService; | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 89 | using hardware::ICamera; | 
|  | 90 | using hardware::ICameraClient; | 
|  | 91 | using hardware::ICameraServiceListener; | 
|  | 92 | using hardware::camera::common::V1_0::CameraDeviceStatus; | 
|  | 93 | using hardware::camera::common::V1_0::TorchModeStatus; | 
| Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 94 | using hardware::camera2::utils::CameraIdAndSessionConfiguration; | 
|  | 95 | using hardware::camera2::utils::ConcurrentCameraIdCombination; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 96 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 97 | // ---------------------------------------------------------------------------- | 
|  | 98 | // Logging support -- this is for debugging only | 
|  | 99 | // Use "adb shell dumpsys media.camera -v 1" to change it. | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 100 | volatile int32_t gLogLevel = 0; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 101 |  | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 102 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); | 
|  | 103 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 104 |  | 
|  | 105 | static void setLogLevel(int level) { | 
|  | 106 | android_atomic_write(level, &gLogLevel); | 
|  | 107 | } | 
|  | 108 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 109 | // Convenience methods for constructing binder::Status objects for error returns | 
|  | 110 |  | 
|  | 111 | #define STATUS_ERROR(errorCode, errorString) \ | 
|  | 112 | binder::Status::fromServiceSpecificError(errorCode, \ | 
|  | 113 | String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString)) | 
|  | 114 |  | 
|  | 115 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ | 
|  | 116 | binder::Status::fromServiceSpecificError(errorCode, \ | 
|  | 117 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \ | 
|  | 118 | __VA_ARGS__)) | 
|  | 119 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 120 | // ---------------------------------------------------------------------------- | 
|  | 121 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 122 | static const String16 sDumpPermission("android.permission.DUMP"); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 123 | static const String16 sManageCameraPermission("android.permission.MANAGE_CAMERA"); | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 124 | static const String16 sCameraPermission("android.permission.CAMERA"); | 
|  | 125 | static const String16 sSystemCameraPermission("android.permission.SYSTEM_CAMERA"); | 
|  | 126 | static const String16 | 
|  | 127 | sCameraSendSystemEventsPermission("android.permission.CAMERA_SEND_SYSTEM_EVENTS"); | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 128 | static const String16 sCameraOpenCloseListenerPermission( | 
|  | 129 | "android.permission.CAMERA_OPEN_CLOSE_LISTENER"); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 130 |  | 
| Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame] | 131 | static constexpr int32_t kVendorClientScore = resource_policy::PERCEPTIBLE_APP_ADJ; | 
|  | 132 | static constexpr int32_t kVendorClientState = ActivityManager::PROCESS_STATE_PERSISTENT_UI; | 
|  | 133 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 134 | const String8 CameraService::kOfflineDevice("offline-"); | 
| Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 135 |  | 
| Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 136 | CameraService::CameraService() : | 
|  | 137 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 138 | mNumberOfCameras(0), | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 139 | mNumberOfCamerasWithoutSystemCamera(0), | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 140 | mSoundRef(0), mInitialized(false), | 
|  | 141 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) { | 
| Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 142 | ALOGI("CameraService started (pid=%d)", getpid()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 143 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
| Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 146 | void CameraService::onFirstRef() | 
|  | 147 | { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 148 | ALOGI("CameraService process starting"); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 149 |  | 
| Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 150 | BnCameraService::onFirstRef(); | 
|  | 151 |  | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 152 | // Update battery life tracking if service is restarting | 
|  | 153 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); | 
|  | 154 | notifier.noteResetCamera(); | 
|  | 155 | notifier.noteResetFlashlight(); | 
|  | 156 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 157 | status_t res = INVALID_OPERATION; | 
| Eino-Ville Talvala | 9cbbc83 | 2017-01-23 15:39:53 -0800 | [diff] [blame] | 158 |  | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 159 | res = enumerateProviders(); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 160 | if (res == OK) { | 
|  | 161 | mInitialized = true; | 
|  | 162 | } | 
|  | 163 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 164 | mUidPolicy = new UidPolicy(this); | 
|  | 165 | mUidPolicy->registerSelf(); | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 166 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this); | 
|  | 167 | mSensorPrivacyPolicy->registerSelf(); | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 168 | mAppOps.setCameraAudioRestriction(mAudioRestriction); | 
| Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 169 | sp<HidlCameraService> hcs = HidlCameraService::getInstance(this); | 
|  | 170 | if (hcs->registerAsService() != android::OK) { | 
|  | 171 | ALOGE("%s: Failed to register default android.frameworks.cameraservice.service@1.0", | 
|  | 172 | __FUNCTION__); | 
|  | 173 | } | 
| Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 174 |  | 
|  | 175 | // This needs to be last call in this function, so that it's as close to | 
|  | 176 | // ServiceManager::addService() as possible. | 
| Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 177 | CameraServiceProxyWrapper::pingCameraServiceProxy(); | 
| Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 178 | ALOGI("CameraService pinged cameraservice proxy"); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 179 | } | 
|  | 180 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 181 | status_t CameraService::enumerateProviders() { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 182 | status_t res; | 
| Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 183 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 184 | std::vector<std::string> deviceIds; | 
|  | 185 | { | 
|  | 186 | Mutex::Autolock l(mServiceLock); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 187 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 188 | if (nullptr == mCameraProviderManager.get()) { | 
|  | 189 | mCameraProviderManager = new CameraProviderManager(); | 
|  | 190 | res = mCameraProviderManager->initialize(this); | 
|  | 191 | if (res != OK) { | 
|  | 192 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", | 
|  | 193 | __FUNCTION__, strerror(-res), res); | 
|  | 194 | return res; | 
| Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 195 | } | 
|  | 196 | } | 
|  | 197 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 198 |  | 
|  | 199 | // Setup vendor tags before we call get_camera_info the first time | 
|  | 200 | // because HAL might need to setup static vendor keys in get_camera_info | 
|  | 201 | // TODO: maybe put this into CameraProviderManager::initialize()? | 
|  | 202 | mCameraProviderManager->setUpVendorTags(); | 
|  | 203 |  | 
|  | 204 | if (nullptr == mFlashlight.get()) { | 
|  | 205 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); | 
| Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 206 | } | 
|  | 207 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 208 | res = mFlashlight->findFlashUnits(); | 
|  | 209 | if (res != OK) { | 
|  | 210 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | deviceIds = mCameraProviderManager->getCameraDeviceIds(); | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 |  | 
|  | 217 | for (auto& cameraId : deviceIds) { | 
|  | 218 | String8 id8 = String8(cameraId.c_str()); | 
| Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 219 | if (getCameraState(id8) == nullptr) { | 
|  | 220 | onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT); | 
|  | 221 | } | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
|  | 224 | return OK; | 
|  | 225 | } | 
|  | 226 |  | 
| Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 227 | void CameraService::broadcastTorchModeStatus(const String8& cameraId, TorchModeStatus status) { | 
|  | 228 | Mutex::Autolock lock(mStatusListenerLock); | 
|  | 229 |  | 
|  | 230 | for (auto& i : mListenerList) { | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 231 | i->getListener()->onTorchStatusChanged(mapToInterface(status), String16{cameraId}); | 
| Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 232 | } | 
|  | 233 | } | 
|  | 234 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 235 | CameraService::~CameraService() { | 
| Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 236 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 237 | mUidPolicy->unregisterSelf(); | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 238 | mSensorPrivacyPolicy->unregisterSelf(); | 
| Evan Severson | 190ad25 | 2021-01-07 20:37:15 -0800 | [diff] [blame] | 239 |  | 
|  | 240 | for (auto const& [_, policy] : mCameraSensorPrivacyPolicies) { | 
|  | 241 | policy->unregisterSelf(); | 
|  | 242 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
| Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 245 | void CameraService::onNewProviderRegistered() { | 
|  | 246 | enumerateProviders(); | 
|  | 247 | } | 
|  | 248 |  | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 249 | void CameraService::filterAPI1SystemCameraLocked( | 
|  | 250 | const std::vector<std::string> &normalDeviceIds) { | 
|  | 251 | mNormalDeviceIdsWithoutSystemCamera.clear(); | 
|  | 252 | for (auto &deviceId : normalDeviceIds) { | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 253 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
|  | 254 | if (getSystemCameraKind(String8(deviceId.c_str()), &deviceKind) != OK) { | 
|  | 255 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, deviceId.c_str()); | 
|  | 256 | continue; | 
|  | 257 | } | 
|  | 258 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 259 | // All system camera ids will necessarily come after public camera | 
|  | 260 | // device ids as per the HAL interface contract. | 
|  | 261 | break; | 
|  | 262 | } | 
|  | 263 | mNormalDeviceIdsWithoutSystemCamera.push_back(deviceId); | 
|  | 264 | } | 
|  | 265 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, | 
|  | 266 | mNormalDeviceIdsWithoutSystemCamera.size()); | 
|  | 267 | } | 
|  | 268 |  | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 269 | status_t CameraService::getSystemCameraKind(const String8& cameraId, SystemCameraKind *kind) const { | 
|  | 270 | auto state = getCameraState(cameraId); | 
|  | 271 | if (state != nullptr) { | 
|  | 272 | *kind = state->getSystemCameraKind(); | 
|  | 273 | return OK; | 
|  | 274 | } | 
|  | 275 | // Hidden physical camera ids won't have CameraState | 
|  | 276 | return mCameraProviderManager->getSystemCameraKind(cameraId.c_str(), kind); | 
|  | 277 | } | 
|  | 278 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 279 | void CameraService::updateCameraNumAndIds() { | 
|  | 280 | Mutex::Autolock l(mServiceLock); | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 281 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); | 
|  | 282 | // Excludes hidden secure cameras | 
|  | 283 | mNumberOfCameras = | 
|  | 284 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; | 
|  | 285 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 286 | mNormalDeviceIds = | 
|  | 287 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 288 | filterAPI1SystemCameraLocked(mNormalDeviceIds); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 289 | } | 
|  | 290 |  | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 291 | void CameraService::addStates(const String8 id) { | 
|  | 292 | std::string cameraId(id.c_str()); | 
|  | 293 | hardware::camera::common::V1_0::CameraResourceCost cost; | 
|  | 294 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 295 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 296 | if (res != OK) { | 
|  | 297 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); | 
|  | 298 | return; | 
|  | 299 | } | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 300 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); | 
|  | 301 | if (res != OK) { | 
|  | 302 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); | 
|  | 303 | return; | 
|  | 304 | } | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 305 | std::set<String8> conflicting; | 
|  | 306 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { | 
|  | 307 | conflicting.emplace(String8(cost.conflictingDevices[i].c_str())); | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | { | 
|  | 311 | Mutex::Autolock lock(mCameraStatesLock); | 
|  | 312 | mCameraStates.emplace(id, std::make_shared<CameraState>(id, cost.resourceCost, | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 313 | conflicting, deviceKind)); | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 314 | } | 
|  | 315 |  | 
|  | 316 | if (mFlashlight->hasFlashUnit(id)) { | 
| Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 317 | Mutex::Autolock al(mTorchStatusMutex); | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 318 | mTorchStatusMap.add(id, TorchModeStatus::AVAILABLE_OFF); | 
| Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 319 |  | 
|  | 320 | broadcastTorchModeStatus(id, TorchModeStatus::AVAILABLE_OFF); | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 321 | } | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 322 |  | 
|  | 323 | updateCameraNumAndIds(); | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 324 | logDeviceAdded(id, "Device added"); | 
|  | 325 | } | 
|  | 326 |  | 
| Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 327 | void CameraService::removeStates(const String8 id) { | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 328 | updateCameraNumAndIds(); | 
| Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 329 | if (mFlashlight->hasFlashUnit(id)) { | 
| Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 330 | Mutex::Autolock al(mTorchStatusMutex); | 
| Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 331 | mTorchStatusMap.removeItem(id); | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | { | 
|  | 335 | Mutex::Autolock lock(mCameraStatesLock); | 
|  | 336 | mCameraStates.erase(id); | 
|  | 337 | } | 
|  | 338 | } | 
|  | 339 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 340 | void CameraService::onDeviceStatusChanged(const String8& id, | 
|  | 341 | CameraDeviceStatus newHalStatus) { | 
|  | 342 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, | 
|  | 343 | id.string(), newHalStatus); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 344 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 345 | StatusInternal newStatus = mapToInternal(newHalStatus); | 
|  | 346 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 347 | std::shared_ptr<CameraState> state = getCameraState(id); | 
|  | 348 |  | 
|  | 349 | if (state == nullptr) { | 
| Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 350 | if (newStatus == StatusInternal::PRESENT) { | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 351 | ALOGI("%s: Unknown camera ID %s, a new camera is added", | 
| Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 352 | __FUNCTION__, id.string()); | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 353 |  | 
|  | 354 | // First add as absent to make sure clients are notified below | 
|  | 355 | addStates(id); | 
|  | 356 |  | 
|  | 357 | updateStatus(newStatus, id); | 
| Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 358 | } else { | 
|  | 359 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string()); | 
|  | 360 | } | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 361 | return; | 
|  | 362 | } | 
|  | 363 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 364 | StatusInternal oldStatus = state->getStatus(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 365 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 366 | if (oldStatus == newStatus) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 367 | 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] | 368 | return; | 
|  | 369 | } | 
|  | 370 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 371 | if (newStatus == StatusInternal::NOT_PRESENT) { | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 372 | logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus, | 
|  | 373 | newStatus)); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 374 |  | 
|  | 375 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect | 
|  | 376 | // to this device until the status changes | 
|  | 377 | updateStatus(StatusInternal::NOT_PRESENT, id); | 
|  | 378 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 379 | sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline; | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 380 | { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 381 | // Don't do this in updateStatus to avoid deadlock over mServiceLock | 
|  | 382 | Mutex::Autolock lock(mServiceLock); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 383 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 384 | // Remove cached shim parameters | 
|  | 385 | state->setShimParams(CameraParameters()); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 386 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 387 | // Remove online as well as offline client from the list of active clients, | 
|  | 388 | // if they are present | 
|  | 389 | clientToDisconnectOnline = removeClientLocked(id); | 
|  | 390 | clientToDisconnectOffline = removeClientLocked(kOfflineDevice + id); | 
| Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 391 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 392 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 393 | disconnectClient(id, clientToDisconnectOnline); | 
|  | 394 | disconnectClient(kOfflineDevice + id, clientToDisconnectOffline); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 395 |  | 
| Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 396 | removeStates(id); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 397 | } else { | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 398 | if (oldStatus == StatusInternal::NOT_PRESENT) { | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 399 | logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus, | 
|  | 400 | newStatus)); | 
|  | 401 | } | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 402 | updateStatus(newStatus, id); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 403 | } | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 404 | } | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 405 |  | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 406 | void CameraService::onDeviceStatusChanged(const String8& id, | 
|  | 407 | const String8& physicalId, | 
|  | 408 | CameraDeviceStatus newHalStatus) { | 
|  | 409 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", | 
|  | 410 | __FUNCTION__, id.string(), physicalId.string(), newHalStatus); | 
|  | 411 |  | 
|  | 412 | StatusInternal newStatus = mapToInternal(newHalStatus); | 
|  | 413 |  | 
|  | 414 | std::shared_ptr<CameraState> state = getCameraState(id); | 
|  | 415 |  | 
|  | 416 | if (state == nullptr) { | 
|  | 417 | ALOGE("%s: Physical camera id %s status change on a non-present ID %s", | 
|  | 418 | __FUNCTION__, id.string(), physicalId.string()); | 
|  | 419 | return; | 
|  | 420 | } | 
|  | 421 |  | 
|  | 422 | StatusInternal logicalCameraStatus = state->getStatus(); | 
|  | 423 | if (logicalCameraStatus != StatusInternal::PRESENT && | 
|  | 424 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { | 
|  | 425 | ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d", | 
|  | 426 | __FUNCTION__, physicalId.string(), newHalStatus, logicalCameraStatus); | 
|  | 427 | return; | 
|  | 428 | } | 
|  | 429 |  | 
|  | 430 | bool updated = false; | 
|  | 431 | if (newStatus == StatusInternal::PRESENT) { | 
|  | 432 | updated = state->removeUnavailablePhysicalId(physicalId); | 
|  | 433 | } else { | 
|  | 434 | updated = state->addUnavailablePhysicalId(physicalId); | 
|  | 435 | } | 
|  | 436 |  | 
|  | 437 | if (updated) { | 
| Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 438 | String8 idCombo = id + " : " + physicalId; | 
|  | 439 | if (newStatus == StatusInternal::PRESENT) { | 
|  | 440 | logDeviceAdded(idCombo, | 
|  | 441 | String8::format("Device status changed to %d", newStatus)); | 
|  | 442 | } else { | 
|  | 443 | logDeviceRemoved(idCombo, | 
|  | 444 | String8::format("Device status changed to %d", newStatus)); | 
|  | 445 | } | 
| Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 446 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) | 
|  | 447 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
|  | 448 | if (getSystemCameraKind(id, &deviceKind) != OK) { | 
|  | 449 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.string()); | 
|  | 450 | return; | 
|  | 451 | } | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 452 | String16 id16(id), physicalId16(physicalId); | 
|  | 453 | Mutex::Autolock lock(mStatusListenerLock); | 
|  | 454 | for (auto& listener : mListenerList) { | 
| Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 455 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), | 
|  | 456 | listener->getListenerPid(), listener->getListenerUid())) { | 
|  | 457 | ALOGV("Skipping discovery callback for system-only camera device %s", | 
|  | 458 | id.c_str()); | 
|  | 459 | continue; | 
|  | 460 | } | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 461 | listener->getListener()->onPhysicalCameraStatusChanged(mapToInterface(newStatus), | 
|  | 462 | id16, physicalId16); | 
|  | 463 | } | 
|  | 464 | } | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 465 | } | 
|  | 466 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 467 | void CameraService::disconnectClient(const String8& id, sp<BasicClient> clientToDisconnect) { | 
|  | 468 | if (clientToDisconnect.get() != nullptr) { | 
|  | 469 | ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL", | 
|  | 470 | __FUNCTION__, id.string()); | 
|  | 471 | // Notify the client of disconnection | 
|  | 472 | clientToDisconnect->notifyError( | 
|  | 473 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, | 
|  | 474 | CaptureResultExtras{}); | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 475 | clientToDisconnect->disconnect(); | 
|  | 476 | } | 
|  | 477 | } | 
|  | 478 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 479 | void CameraService::onTorchStatusChanged(const String8& cameraId, | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 480 | TorchModeStatus newStatus) { | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 481 | Mutex::Autolock al(mTorchStatusMutex); | 
|  | 482 | onTorchStatusChangedLocked(cameraId, newStatus); | 
|  | 483 | } | 
|  | 484 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 485 | void CameraService::onTorchStatusChangedLocked(const String8& cameraId, | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 486 | TorchModeStatus newStatus) { | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 487 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", | 
|  | 488 | __FUNCTION__, cameraId.string(), newStatus); | 
|  | 489 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 490 | TorchModeStatus status; | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 491 | status_t res = getTorchStatusLocked(cameraId, &status); | 
|  | 492 | if (res) { | 
| Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 493 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", | 
|  | 494 | __FUNCTION__, cameraId.string(), strerror(-res), res); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 495 | return; | 
|  | 496 | } | 
|  | 497 | if (status == newStatus) { | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 498 | return; | 
|  | 499 | } | 
|  | 500 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 501 | res = setTorchStatusLocked(cameraId, newStatus); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 502 | if (res) { | 
| Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 503 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, | 
|  | 504 | (uint32_t)newStatus, strerror(-res), res); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 505 | return; | 
|  | 506 | } | 
|  | 507 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 508 | { | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 509 | // Update battery life logging for flashlight | 
| Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 510 | Mutex::Autolock al(mTorchUidMapMutex); | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 511 | auto iter = mTorchUidMap.find(cameraId); | 
|  | 512 | if (iter != mTorchUidMap.end()) { | 
|  | 513 | int oldUid = iter->second.second; | 
|  | 514 | int newUid = iter->second.first; | 
|  | 515 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); | 
|  | 516 | if (oldUid != newUid) { | 
|  | 517 | // 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] | 518 | if (status == TorchModeStatus::AVAILABLE_ON) { | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 519 | notifier.noteFlashlightOff(cameraId, oldUid); | 
|  | 520 | } | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 521 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 522 | notifier.noteFlashlightOn(cameraId, newUid); | 
|  | 523 | } | 
|  | 524 | iter->second.second = newUid; | 
|  | 525 | } else { | 
|  | 526 | // If the UID has not changed, log the status | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 527 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 528 | notifier.noteFlashlightOn(cameraId, oldUid); | 
|  | 529 | } else { | 
|  | 530 | notifier.noteFlashlightOff(cameraId, oldUid); | 
|  | 531 | } | 
|  | 532 | } | 
|  | 533 | } | 
|  | 534 | } | 
|  | 535 |  | 
| Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 536 | broadcastTorchModeStatus(cameraId, newStatus); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 537 | } | 
|  | 538 |  | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 539 | static bool hasPermissionsForSystemCamera(int callingPid, int callingUid) { | 
|  | 540 | return checkPermission(sSystemCameraPermission, callingPid, callingUid) && | 
|  | 541 | checkPermission(sCameraPermission, callingPid, callingUid); | 
|  | 542 | } | 
|  | 543 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 544 | Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 545 | ATRACE_CALL(); | 
| Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 546 | Mutex::Autolock l(mServiceLock); | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 547 | bool hasSystemCameraPermissions = | 
|  | 548 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), | 
|  | 549 | CameraThreadState::getCallingUid()); | 
| Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 550 | switch (type) { | 
|  | 551 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 552 | if (hasSystemCameraPermissions) { | 
|  | 553 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); | 
|  | 554 | } else { | 
|  | 555 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); | 
|  | 556 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 557 | break; | 
| Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 558 | case CAMERA_TYPE_ALL: | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 559 | if (hasSystemCameraPermissions) { | 
|  | 560 | *numCameras = mNumberOfCameras; | 
|  | 561 | } else { | 
|  | 562 | *numCameras = mNumberOfCamerasWithoutSystemCamera; | 
|  | 563 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 564 | break; | 
| Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 565 | default: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 566 | ALOGW("%s: Unknown camera type %d", | 
| Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 567 | __FUNCTION__, type); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 568 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 569 | "Unknown camera type %d", type); | 
| Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 570 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 571 | return Status::ok(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 572 | } | 
|  | 573 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 574 | Status CameraService::getCameraInfo(int cameraId, | 
|  | 575 | CameraInfo* cameraInfo) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 576 | ATRACE_CALL(); | 
| Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 577 | Mutex::Autolock l(mServiceLock); | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 578 | std::string cameraIdStr = cameraIdIntToStrLocked(cameraId); | 
|  | 579 | if (shouldRejectSystemCameraConnection(String8(cameraIdStr.c_str()))) { | 
|  | 580 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" | 
|  | 581 | "characteristics for system only device %s: ", cameraIdStr.c_str()); | 
|  | 582 | } | 
| Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 583 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 584 | if (!mInitialized) { | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 585 | return STATUS_ERROR(ERROR_DISCONNECTED, | 
|  | 586 | "Camera subsystem is not available"); | 
| Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 587 | } | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 588 | bool hasSystemCameraPermissions = | 
|  | 589 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), | 
|  | 590 | CameraThreadState::getCallingUid()); | 
|  | 591 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; | 
|  | 592 | if (hasSystemCameraPermissions) { | 
|  | 593 | cameraIdBound = mNumberOfCameras; | 
|  | 594 | } | 
|  | 595 | if (cameraId < 0 || cameraId >= cameraIdBound) { | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 596 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, | 
|  | 597 | "CameraId is not valid"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 598 | } | 
|  | 599 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 600 | Status ret = Status::ok(); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 601 | status_t err = mCameraProviderManager->getCameraInfo( | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 602 | cameraIdStr.c_str(), cameraInfo); | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 603 | if (err != OK) { | 
|  | 604 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 605 | "Error retrieving camera info from device %d: %s (%d)", cameraId, | 
|  | 606 | strerror(-err), err); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 607 | } | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 608 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 609 | return ret; | 
|  | 610 | } | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 611 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 612 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt) { | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 613 | const std::vector<std::string> *deviceIds = &mNormalDeviceIdsWithoutSystemCamera; | 
|  | 614 | auto callingPid = CameraThreadState::getCallingPid(); | 
|  | 615 | auto callingUid = CameraThreadState::getCallingUid(); | 
|  | 616 | if (checkPermission(sSystemCameraPermission, callingPid, callingUid) || | 
|  | 617 | getpid() == callingPid) { | 
|  | 618 | deviceIds = &mNormalDeviceIds; | 
|  | 619 | } | 
|  | 620 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(deviceIds->size())) { | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 621 | ALOGE("%s: input id %d invalid: valid range  (0, %zu)", | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 622 | __FUNCTION__, cameraIdInt, deviceIds->size()); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 623 | return std::string{}; | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 624 | } | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 625 |  | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 626 | return (*deviceIds)[cameraIdInt]; | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 627 | } | 
|  | 628 |  | 
|  | 629 | String8 CameraService::cameraIdIntToStr(int cameraIdInt) { | 
|  | 630 | Mutex::Autolock lock(mServiceLock); | 
|  | 631 | return String8(cameraIdIntToStrLocked(cameraIdInt).c_str()); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 632 | } | 
|  | 633 |  | 
|  | 634 | Status CameraService::getCameraCharacteristics(const String16& cameraId, | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 635 | CameraMetadata* cameraInfo) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 636 | ATRACE_CALL(); | 
| Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 637 | if (!cameraInfo) { | 
|  | 638 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 639 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); | 
| Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 640 | } | 
|  | 641 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 642 | if (!mInitialized) { | 
|  | 643 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 644 | return STATUS_ERROR(ERROR_DISCONNECTED, | 
|  | 645 | "Camera subsystem is not available");; | 
| Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 646 | } | 
|  | 647 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 648 | if (shouldRejectSystemCameraConnection(String8(cameraId))) { | 
|  | 649 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" | 
|  | 650 | "characteristics for system only device %s: ", String8(cameraId).string()); | 
|  | 651 | } | 
|  | 652 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 653 | Status ret{}; | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 654 |  | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 655 | status_t res = mCameraProviderManager->getCameraCharacteristics( | 
|  | 656 | String8(cameraId).string(), cameraInfo); | 
|  | 657 | if (res != OK) { | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 658 | if (res == NAME_NOT_FOUND) { | 
|  | 659 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " | 
|  | 660 | "characteristics for unknown device %s: %s (%d)", String8(cameraId).string(), | 
|  | 661 | strerror(-res), res); | 
|  | 662 | } else { | 
|  | 663 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " | 
|  | 664 | "characteristics for device %s: %s (%d)", String8(cameraId).string(), | 
|  | 665 | strerror(-res), res); | 
|  | 666 | } | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 667 | } | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 668 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
|  | 669 | if (getSystemCameraKind(String8(cameraId), &deviceKind) != OK) { | 
|  | 670 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, String8(cameraId).string()); | 
|  | 671 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera kind " | 
|  | 672 | "for device %s", String8(cameraId).string()); | 
|  | 673 | } | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 674 | int callingPid = CameraThreadState::getCallingPid(); | 
|  | 675 | int callingUid = CameraThreadState::getCallingUid(); | 
| Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 676 | std::vector<int32_t> tagsRemoved; | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 677 | // If it's not calling from cameraserver, check the permission only if | 
|  | 678 | // android.permission.CAMERA is required. If android.permission.SYSTEM_CAMERA was needed, | 
|  | 679 | // it would've already been checked in shouldRejectSystemCameraConnection. | 
| Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 680 | if ((callingPid != getpid()) && | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 681 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 682 | !checkPermission(sCameraPermission, callingPid, callingUid)) { | 
| Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 683 | res = cameraInfo->removePermissionEntries( | 
|  | 684 | mCameraProviderManager->getProviderTagIdLocked(String8(cameraId).string()), | 
|  | 685 | &tagsRemoved); | 
|  | 686 | if (res != OK) { | 
|  | 687 | cameraInfo->clear(); | 
|  | 688 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to remove camera" | 
|  | 689 | " characteristics needing camera permission for device %s: %s (%d)", | 
|  | 690 | String8(cameraId).string(), strerror(-res), res); | 
|  | 691 | } | 
|  | 692 | } | 
|  | 693 |  | 
|  | 694 | if (!tagsRemoved.empty()) { | 
|  | 695 | res = cameraInfo->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, | 
|  | 696 | tagsRemoved.data(), tagsRemoved.size()); | 
|  | 697 | if (res != OK) { | 
|  | 698 | cameraInfo->clear(); | 
|  | 699 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to insert camera " | 
|  | 700 | "keys needing permission for device %s: %s (%d)", String8(cameraId).string(), | 
|  | 701 | strerror(-res), res); | 
|  | 702 | } | 
|  | 703 | } | 
|  | 704 |  | 
| Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 705 | return ret; | 
|  | 706 | } | 
|  | 707 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 708 | String8 CameraService::getFormattedCurrentTime() { | 
|  | 709 | time_t now = time(nullptr); | 
|  | 710 | char formattedTime[64]; | 
|  | 711 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); | 
|  | 712 | return String8(formattedTime); | 
|  | 713 | } | 
|  | 714 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 715 | Status CameraService::getCameraVendorTagDescriptor( | 
|  | 716 | /*out*/ | 
|  | 717 | hardware::camera2::params::VendorTagDescriptor* desc) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 718 | ATRACE_CALL(); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 719 | if (!mInitialized) { | 
|  | 720 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 721 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); | 
| Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 722 | } | 
| Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 723 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); | 
|  | 724 | if (globalDescriptor != nullptr) { | 
|  | 725 | *desc = *(globalDescriptor.get()); | 
|  | 726 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 727 | return Status::ok(); | 
| Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 728 | } | 
|  | 729 |  | 
| Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 730 | Status CameraService::getCameraVendorTagCache( | 
|  | 731 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { | 
|  | 732 | ATRACE_CALL(); | 
|  | 733 | if (!mInitialized) { | 
|  | 734 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); | 
|  | 735 | return STATUS_ERROR(ERROR_DISCONNECTED, | 
|  | 736 | "Camera subsystem not available"); | 
|  | 737 | } | 
|  | 738 | sp<VendorTagDescriptorCache> globalCache = | 
|  | 739 | VendorTagDescriptorCache::getGlobalVendorTagCache(); | 
|  | 740 | if (globalCache != nullptr) { | 
|  | 741 | *cache = *(globalCache.get()); | 
|  | 742 | } | 
|  | 743 | return Status::ok(); | 
|  | 744 | } | 
|  | 745 |  | 
| Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 746 | int CameraService::getDeviceVersion(const String8& cameraId, int* facing, int* orientation) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 747 | ATRACE_CALL(); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 748 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 749 | int deviceVersion = 0; | 
|  | 750 |  | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 751 | status_t res; | 
|  | 752 | hardware::hidl_version maxVersion{0,0}; | 
|  | 753 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(), | 
|  | 754 | &maxVersion); | 
|  | 755 | if (res != OK) return -1; | 
|  | 756 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 757 |  | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 758 | hardware::CameraInfo info; | 
|  | 759 | if (facing) { | 
|  | 760 | res = mCameraProviderManager->getCameraInfo(cameraId.string(), &info); | 
| Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 761 | if (res != OK) return -1; | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 762 | *facing = info.facing; | 
| Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 763 | if (orientation) { | 
|  | 764 | *orientation = info.orientation; | 
|  | 765 | } | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 766 | } | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 767 |  | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 768 | return deviceVersion; | 
|  | 769 | } | 
|  | 770 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 771 | Status CameraService::filterGetInfoErrorCode(status_t err) { | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 772 | switch(err) { | 
|  | 773 | case NO_ERROR: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 774 | return Status::ok(); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 775 | case BAD_VALUE: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 776 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, | 
|  | 777 | "CameraId is not valid for HAL module"); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 778 | case NO_INIT: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 779 | return STATUS_ERROR(ERROR_DISCONNECTED, | 
|  | 780 | "Camera device not available"); | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 781 | default: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 782 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 783 | "Camera HAL encountered error %d: %s", | 
|  | 784 | err, strerror(-err)); | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 785 | } | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 786 | } | 
|  | 787 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 788 | Status CameraService::makeClient(const sp<CameraService>& cameraService, | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 789 | const sp<IInterface>& cameraCb, const String16& packageName, | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 790 | const std::optional<String16>& featureId,  const String8& cameraId, | 
|  | 791 | int api1CameraId, int facing, int clientPid, uid_t clientUid, int servicePid, | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 792 | int deviceVersion, apiLevel effectiveApiLevel, | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 793 | /*out*/sp<BasicClient>* client) { | 
|  | 794 |  | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 795 | // Create CameraClient based on device version reported by the HAL. | 
|  | 796 | switch(deviceVersion) { | 
|  | 797 | case CAMERA_DEVICE_API_VERSION_1_0: | 
|  | 798 | ALOGE("Camera using old HAL version: %d", deviceVersion); | 
|  | 799 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, | 
|  | 800 | "Camera device \"%s\" HAL version %d no longer supported", | 
|  | 801 | cameraId.string(), deviceVersion); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 802 | break; | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 803 | case CAMERA_DEVICE_API_VERSION_3_0: | 
|  | 804 | case CAMERA_DEVICE_API_VERSION_3_1: | 
|  | 805 | case CAMERA_DEVICE_API_VERSION_3_2: | 
|  | 806 | case CAMERA_DEVICE_API_VERSION_3_3: | 
|  | 807 | case CAMERA_DEVICE_API_VERSION_3_4: | 
|  | 808 | case CAMERA_DEVICE_API_VERSION_3_5: | 
|  | 809 | case CAMERA_DEVICE_API_VERSION_3_6: | 
| Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 810 | case CAMERA_DEVICE_API_VERSION_3_7: | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 811 | if (effectiveApiLevel == API_1) { // Camera1 API route | 
|  | 812 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 813 | *client = new Camera2Client(cameraService, tmp, packageName, featureId, | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 814 | cameraId, api1CameraId, | 
|  | 815 | facing, clientPid, clientUid, | 
| Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 816 | servicePid); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 817 | } else { // Camera2 API route | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 818 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = | 
|  | 819 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 820 | *client = new CameraDeviceClient(cameraService, tmp, packageName, featureId, | 
|  | 821 | cameraId, facing, clientPid, clientUid, servicePid); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 822 | } | 
|  | 823 | break; | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 824 | default: | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 825 | // Should not be reachable | 
|  | 826 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 827 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 828 | "Camera device \"%s\" has unknown HAL version %d", | 
|  | 829 | cameraId.string(), deviceVersion); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 830 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 831 | return Status::ok(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 832 | } | 
|  | 833 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 834 | String8 CameraService::toString(std::set<userid_t> intSet) { | 
|  | 835 | String8 s(""); | 
|  | 836 | bool first = true; | 
|  | 837 | for (userid_t i : intSet) { | 
|  | 838 | if (first) { | 
|  | 839 | s.appendFormat("%d", i); | 
|  | 840 | first = false; | 
|  | 841 | } else { | 
|  | 842 | s.appendFormat(", %d", i); | 
|  | 843 | } | 
|  | 844 | } | 
|  | 845 | return s; | 
|  | 846 | } | 
|  | 847 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 848 | int32_t CameraService::mapToInterface(TorchModeStatus status) { | 
|  | 849 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; | 
|  | 850 | switch (status) { | 
|  | 851 | case TorchModeStatus::NOT_AVAILABLE: | 
|  | 852 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; | 
|  | 853 | break; | 
|  | 854 | case TorchModeStatus::AVAILABLE_OFF: | 
|  | 855 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; | 
|  | 856 | break; | 
|  | 857 | case TorchModeStatus::AVAILABLE_ON: | 
|  | 858 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; | 
|  | 859 | break; | 
|  | 860 | default: | 
|  | 861 | ALOGW("Unknown new flash status: %d", status); | 
|  | 862 | } | 
|  | 863 | return serviceStatus; | 
|  | 864 | } | 
|  | 865 |  | 
|  | 866 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { | 
|  | 867 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; | 
|  | 868 | switch (status) { | 
|  | 869 | case CameraDeviceStatus::NOT_PRESENT: | 
|  | 870 | serviceStatus = StatusInternal::NOT_PRESENT; | 
|  | 871 | break; | 
|  | 872 | case CameraDeviceStatus::PRESENT: | 
|  | 873 | serviceStatus = StatusInternal::PRESENT; | 
|  | 874 | break; | 
|  | 875 | case CameraDeviceStatus::ENUMERATING: | 
|  | 876 | serviceStatus = StatusInternal::ENUMERATING; | 
|  | 877 | break; | 
|  | 878 | default: | 
|  | 879 | ALOGW("Unknown new HAL device status: %d", status); | 
|  | 880 | } | 
|  | 881 | return serviceStatus; | 
|  | 882 | } | 
|  | 883 |  | 
|  | 884 | int32_t CameraService::mapToInterface(StatusInternal status) { | 
|  | 885 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; | 
|  | 886 | switch (status) { | 
|  | 887 | case StatusInternal::NOT_PRESENT: | 
|  | 888 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; | 
|  | 889 | break; | 
|  | 890 | case StatusInternal::PRESENT: | 
|  | 891 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; | 
|  | 892 | break; | 
|  | 893 | case StatusInternal::ENUMERATING: | 
|  | 894 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; | 
|  | 895 | break; | 
|  | 896 | case StatusInternal::NOT_AVAILABLE: | 
|  | 897 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; | 
|  | 898 | break; | 
|  | 899 | case StatusInternal::UNKNOWN: | 
|  | 900 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; | 
|  | 901 | break; | 
|  | 902 | default: | 
|  | 903 | ALOGW("Unknown new internal device status: %d", status); | 
|  | 904 | } | 
|  | 905 | return serviceStatus; | 
|  | 906 | } | 
|  | 907 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 908 | Status CameraService::initializeShimMetadata(int cameraId) { | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 909 | int uid = CameraThreadState::getCallingUid(); | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 910 |  | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 911 | String16 internalPackageName("cameraserver"); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 912 | String8 id = String8::format("%d", cameraId); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 913 | Status ret = Status::ok(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 914 | sp<Client> tmp = nullptr; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 915 | if (!(ret = connectHelper<ICameraClient,Client>( | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 916 | sp<ICameraClient>{nullptr}, id, cameraId, | 
| Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 917 | internalPackageName, {}, uid, USE_CALLING_PID, | 
| Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 918 | API_1, /*shimUpdateOnly*/ true, /*out*/ tmp) | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 919 | ).isOk()) { | 
|  | 920 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string()); | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 921 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 922 | return ret; | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 923 | } | 
|  | 924 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 925 | Status CameraService::getLegacyParametersLazy(int cameraId, | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 926 | /*out*/ | 
|  | 927 | CameraParameters* parameters) { | 
|  | 928 |  | 
|  | 929 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); | 
|  | 930 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 931 | Status ret = Status::ok(); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 932 |  | 
|  | 933 | if (parameters == NULL) { | 
|  | 934 | ALOGE("%s: parameters must not be null", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 935 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 936 | } | 
|  | 937 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 938 | String8 id = String8::format("%d", cameraId); | 
|  | 939 |  | 
|  | 940 | // Check if we already have parameters | 
|  | 941 | { | 
|  | 942 | // Scope for service lock | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 943 | Mutex::Autolock lock(mServiceLock); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 944 | auto cameraState = getCameraState(id); | 
|  | 945 | if (cameraState == nullptr) { | 
|  | 946 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 947 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 948 | "Invalid camera ID: %s", id.string()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 949 | } | 
|  | 950 | CameraParameters p = cameraState->getShimParams(); | 
|  | 951 | if (!p.isEmpty()) { | 
|  | 952 | *parameters = p; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 953 | return ret; | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 954 | } | 
|  | 955 | } | 
|  | 956 |  | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 957 | int64_t token = CameraThreadState::clearCallingIdentity(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 958 | ret = initializeShimMetadata(cameraId); | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 959 | CameraThreadState::restoreCallingIdentity(token); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 960 | if (!ret.isOk()) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 961 | // Error already logged by callee | 
|  | 962 | return ret; | 
|  | 963 | } | 
|  | 964 |  | 
|  | 965 | // Check for parameters again | 
|  | 966 | { | 
|  | 967 | // Scope for service lock | 
|  | 968 | Mutex::Autolock lock(mServiceLock); | 
|  | 969 | auto cameraState = getCameraState(id); | 
|  | 970 | if (cameraState == nullptr) { | 
|  | 971 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 972 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 973 | "Invalid camera ID: %s", id.string()); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 974 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 975 | CameraParameters p = cameraState->getShimParams(); | 
|  | 976 | if (!p.isEmpty()) { | 
|  | 977 | *parameters = p; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 978 | return ret; | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 979 | } | 
|  | 980 | } | 
|  | 981 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 982 | ALOGE("%s: Parameters were not initialized, or were empty.  Device may not be present.", | 
|  | 983 | __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 984 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 985 | } | 
|  | 986 |  | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 987 | // Can camera service trust the caller based on the calling UID? | 
|  | 988 | static bool isTrustedCallingUid(uid_t uid) { | 
|  | 989 | switch (uid) { | 
| Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 990 | case AID_MEDIA:        // mediaserver | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 991 | case AID_CAMERASERVER: // cameraserver | 
| Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 992 | case AID_RADIO:        // telephony | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 993 | return true; | 
|  | 994 | default: | 
|  | 995 | return false; | 
|  | 996 | } | 
|  | 997 | } | 
|  | 998 |  | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 999 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { | 
|  | 1000 | PermissionController pc; | 
|  | 1001 | uid = pc.getPackageUid(packageName, 0); | 
|  | 1002 | if (uid <= 0) { | 
|  | 1003 | ALOGE("Unknown package: '%s'", String8(packageName).string()); | 
|  | 1004 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).string()); | 
|  | 1005 | return BAD_VALUE; | 
|  | 1006 | } | 
|  | 1007 |  | 
|  | 1008 | if (userId < 0) { | 
|  | 1009 | ALOGE("Invalid user: %d", userId); | 
|  | 1010 | dprintf(err, "Invalid user: %d\n", userId); | 
|  | 1011 | return BAD_VALUE; | 
|  | 1012 | } | 
|  | 1013 |  | 
|  | 1014 | uid = multiuser_get_uid(userId, uid); | 
|  | 1015 | return NO_ERROR; | 
|  | 1016 | } | 
|  | 1017 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1018 | Status CameraService::validateConnectLocked(const String8& cameraId, | 
| Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1019 | const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, | 
|  | 1020 | /*out*/int& originalClientPid) const { | 
| Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1021 |  | 
| Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1022 | #ifdef __BRILLO__ | 
|  | 1023 | UNUSED(clientName8); | 
|  | 1024 | UNUSED(clientUid); | 
|  | 1025 | UNUSED(clientPid); | 
|  | 1026 | UNUSED(originalClientPid); | 
|  | 1027 | #else | 
| Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1028 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, | 
|  | 1029 | originalClientPid); | 
| Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1030 | if (!allowed.isOk()) { | 
| Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1031 | return allowed; | 
|  | 1032 | } | 
| Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1033 | #endif  // __BRILLO__ | 
| Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1034 |  | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1035 | int callingPid = CameraThreadState::getCallingPid(); | 
| Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1036 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1037 | if (!mInitialized) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1038 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", | 
|  | 1039 | callingPid); | 
| Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1040 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, | 
|  | 1041 | "No camera HAL module available to open camera device \"%s\"", cameraId.string()); | 
| Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1042 | } | 
|  | 1043 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1044 | if (getCameraState(cameraId) == nullptr) { | 
|  | 1045 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, | 
|  | 1046 | cameraId.string()); | 
| Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1047 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, | 
|  | 1048 | "No camera device with ID \"%s\" available", cameraId.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1049 | } | 
|  | 1050 |  | 
| Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1051 | status_t err = checkIfDeviceIsUsable(cameraId); | 
|  | 1052 | if (err != NO_ERROR) { | 
|  | 1053 | switch(err) { | 
|  | 1054 | case -ENODEV: | 
|  | 1055 | case -EBUSY: | 
|  | 1056 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, | 
|  | 1057 | "No camera device with ID \"%s\" currently available", cameraId.string()); | 
|  | 1058 | default: | 
|  | 1059 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 1060 | "Unknown error connecting to ID \"%s\"", cameraId.string()); | 
|  | 1061 | } | 
|  | 1062 | } | 
|  | 1063 | return Status::ok(); | 
| Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1064 | } | 
|  | 1065 |  | 
| Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1066 | Status CameraService::validateClientPermissionsLocked(const String8& cameraId, | 
| Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1067 | const String8& clientName8, int& clientUid, int& clientPid, | 
|  | 1068 | /*out*/int& originalClientPid) const { | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1069 | int callingPid = CameraThreadState::getCallingPid(); | 
|  | 1070 | int callingUid = CameraThreadState::getCallingUid(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1071 |  | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1072 | // Check if we can trust clientUid | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1073 | if (clientUid == USE_CALLING_UID) { | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1074 | clientUid = callingUid; | 
|  | 1075 | } else if (!isTrustedCallingUid(callingUid)) { | 
|  | 1076 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " | 
|  | 1077 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1078 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 1079 | "Untrusted caller (calling PID %d, UID %d) trying to " | 
|  | 1080 | "forward camera access to camera %s for client %s (PID %d, UID %d)", | 
|  | 1081 | callingPid, callingUid, cameraId.string(), | 
|  | 1082 | clientName8.string(), clientUid, clientPid); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1083 | } | 
|  | 1084 |  | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1085 | // Check if we can trust clientPid | 
|  | 1086 | if (clientPid == USE_CALLING_PID) { | 
|  | 1087 | clientPid = callingPid; | 
|  | 1088 | } else if (!isTrustedCallingUid(callingUid)) { | 
|  | 1089 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " | 
|  | 1090 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1091 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 1092 | "Untrusted caller (calling PID %d, UID %d) trying to " | 
|  | 1093 | "forward camera access to camera %s for client %s (PID %d, UID %d)", | 
|  | 1094 | callingPid, callingUid, cameraId.string(), | 
|  | 1095 | clientName8.string(), clientUid, clientPid); | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1096 | } | 
|  | 1097 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1098 | if (shouldRejectSystemCameraConnection(cameraId)) { | 
|  | 1099 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", | 
|  | 1100 | cameraId.c_str()); | 
|  | 1101 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, "No camera device with ID \"%s\" is" | 
|  | 1102 | "available", cameraId.string()); | 
|  | 1103 | } | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1104 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
|  | 1105 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { | 
|  | 1106 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); | 
|  | 1107 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "No camera device with ID \"%s\"" | 
|  | 1108 | "found while trying to query device kind", cameraId.string()); | 
|  | 1109 |  | 
|  | 1110 | } | 
|  | 1111 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1112 | // If it's not calling from cameraserver, check the permission if the | 
|  | 1113 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for | 
|  | 1114 | // android.permission.SYSTEM_CAMERA for system only camera devices). | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1115 | if (callingPid != getpid() && | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1116 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1117 | !checkPermission(sCameraPermission, clientPid, clientUid)) { | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1118 | 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] | 1119 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 1120 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission", | 
|  | 1121 | clientName8.string(), clientUid, clientPid, cameraId.string()); | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1122 | } | 
|  | 1123 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1124 | // Make sure the UID is in an active state to use the camera | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 1125 | if (!mUidPolicy->isUidActive(callingUid, String16(clientName8))) { | 
| Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1126 | int32_t procState = mUidPolicy->getProcState(callingUid); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1127 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", | 
|  | 1128 | clientPid, clientUid); | 
|  | 1129 | return STATUS_ERROR_FMT(ERROR_DISABLED, | 
| Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1130 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" | 
|  | 1131 | "calling UID %d proc state %" PRId32 ")", | 
|  | 1132 | clientName8.string(), clientUid, clientPid, cameraId.string(), | 
|  | 1133 | callingUid, procState); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1134 | } | 
|  | 1135 |  | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1136 | // If sensor privacy is enabled then prevent access to the camera | 
|  | 1137 | if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { | 
|  | 1138 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); | 
|  | 1139 | return STATUS_ERROR_FMT(ERROR_DISABLED, | 
|  | 1140 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" when sensor privacy " | 
|  | 1141 | "is enabled", clientName8.string(), clientUid, clientPid, cameraId.string()); | 
|  | 1142 | } | 
|  | 1143 |  | 
| Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1144 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's | 
|  | 1145 | // connected to camera service directly. | 
| Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1146 | originalClientPid = clientPid; | 
| Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1147 | clientPid = callingPid; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1148 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1149 | userid_t clientUserId = multiuser_get_user_id(clientUid); | 
| Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1150 |  | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1151 | // Only allow clients who are being used by the current foreground device user, unless calling | 
| Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1152 | // from our own process OR the caller is using the cameraserver's HIDL interface. | 
| Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 1153 | if (getCurrentServingCall() != BinderCallType::HWBINDER && callingPid != getpid() && | 
| Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1154 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1155 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " | 
|  | 1156 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, | 
|  | 1157 | toString(mAllowedUsers).string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1158 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 1159 | "Callers from device user %d are not currently allowed to connect to camera \"%s\"", | 
|  | 1160 | clientUserId, cameraId.string()); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1161 | } | 
|  | 1162 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1163 | return Status::ok(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1164 | } | 
|  | 1165 |  | 
|  | 1166 | status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const { | 
|  | 1167 | auto cameraState = getCameraState(cameraId); | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1168 | int callingPid = CameraThreadState::getCallingPid(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1169 | if (cameraState == nullptr) { | 
|  | 1170 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, | 
|  | 1171 | cameraId.string()); | 
|  | 1172 | return -ENODEV; | 
|  | 1173 | } | 
|  | 1174 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1175 | StatusInternal currentStatus = cameraState->getStatus(); | 
|  | 1176 | if (currentStatus == StatusInternal::NOT_PRESENT) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1177 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", | 
|  | 1178 | callingPid, cameraId.string()); | 
| Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1179 | return -ENODEV; | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1180 | } else if (currentStatus == StatusInternal::ENUMERATING) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1181 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", | 
|  | 1182 | callingPid, cameraId.string()); | 
| Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1183 | return -EBUSY; | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1184 | } | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1185 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1186 | return NO_ERROR; | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1187 | } | 
|  | 1188 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1189 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, | 
|  | 1190 | const CameraService::DescriptorPtr& desc) { | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1191 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1192 | // Make a descriptor for the incoming client | 
|  | 1193 | auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc); | 
|  | 1194 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); | 
|  | 1195 |  | 
|  | 1196 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), | 
|  | 1197 | String8(client->getPackageName())); | 
|  | 1198 |  | 
|  | 1199 | if (evicted.size() > 0) { | 
|  | 1200 | // This should never happen - clients should already have been removed in disconnect | 
|  | 1201 | for (auto& i : evicted) { | 
|  | 1202 | ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect", | 
|  | 1203 | __FUNCTION__, i->getKey().string()); | 
|  | 1204 | } | 
|  | 1205 |  | 
|  | 1206 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", | 
|  | 1207 | __FUNCTION__); | 
|  | 1208 | } | 
| Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1209 |  | 
|  | 1210 | // And register a death notification for the client callback. Do | 
|  | 1211 | // this last to avoid Binder policy where a nested Binder | 
|  | 1212 | // transaction might be pre-empted to service the client death | 
|  | 1213 | // notification if the client process dies before linkToDeath is | 
|  | 1214 | // invoked. | 
|  | 1215 | sp<IBinder> remoteCallback = client->getRemote(); | 
|  | 1216 | if (remoteCallback != nullptr) { | 
|  | 1217 | remoteCallback->linkToDeath(this); | 
|  | 1218 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1219 | } | 
|  | 1220 |  | 
|  | 1221 | status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid, | 
|  | 1222 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName, | 
|  | 1223 | /*out*/ | 
|  | 1224 | sp<BasicClient>* client, | 
|  | 1225 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1226 | ATRACE_CALL(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1227 | status_t ret = NO_ERROR; | 
| Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1228 | std::vector<DescriptorPtr> evictedClients; | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1229 | DescriptorPtr clientDescriptor; | 
|  | 1230 | { | 
|  | 1231 | if (effectiveApiLevel == API_1) { | 
|  | 1232 | // If we are using API1, any existing client for this camera ID with the same remote | 
|  | 1233 | // should be returned rather than evicted to allow MediaRecorder to work properly. | 
|  | 1234 |  | 
|  | 1235 | auto current = mActiveClientManager.get(cameraId); | 
|  | 1236 | if (current != nullptr) { | 
|  | 1237 | auto clientSp = current->getValue(); | 
|  | 1238 | if (clientSp.get() != nullptr) { // should never be needed | 
| Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1239 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { | 
|  | 1240 | ALOGW("CameraService connect called from same client, but with a different" | 
|  | 1241 | " API level, evicting prior client..."); | 
|  | 1242 | } else if (clientSp->getRemote() == remoteCallback) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1243 | ALOGI("CameraService::connect X (PID %d) (second call from same" | 
| Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1244 | " app binder, returning the same client)", clientPid); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1245 | *client = clientSp; | 
|  | 1246 | return NO_ERROR; | 
|  | 1247 | } | 
|  | 1248 | } | 
| Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1249 | } | 
| Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1250 | } | 
| Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 1251 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1252 | // Get current active client PIDs | 
|  | 1253 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); | 
|  | 1254 | ownerPids.push_back(clientPid); | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1255 |  | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1256 | std::vector<int> priorityScores(ownerPids.size()); | 
|  | 1257 | std::vector<int> states(ownerPids.size()); | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1258 |  | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1259 | // Get priority scores of all active PIDs | 
|  | 1260 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids( | 
|  | 1261 | ownerPids.size(), &ownerPids[0], /*out*/&states[0], | 
|  | 1262 | /*out*/&priorityScores[0]); | 
|  | 1263 | if (err != OK) { | 
|  | 1264 | ALOGE("%s: Priority score query failed: %d", | 
|  | 1265 | __FUNCTION__, err); | 
|  | 1266 | return err; | 
|  | 1267 | } | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1268 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1269 | // Update all active clients' priorities | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1270 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1271 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1272 | pidToPriorityMap.emplace(ownerPids[i], | 
| Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 1273 | resource_policy::ClientPriority(priorityScores[i], states[i], | 
|  | 1274 | /* isVendorClient won't get copied over*/ false)); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1275 | } | 
|  | 1276 | mActiveClientManager.updatePriorities(pidToPriorityMap); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1277 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1278 | // Get state for the given cameraId | 
|  | 1279 | auto state = getCameraState(cameraId); | 
|  | 1280 | if (state == nullptr) { | 
|  | 1281 | ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", | 
|  | 1282 | clientPid, cameraId.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1283 | // Should never get here because validateConnectLocked should have errored out | 
| Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1284 | return BAD_VALUE; | 
| Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1285 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1286 |  | 
|  | 1287 | // Make descriptor for incoming client | 
| Shuzhen Wang | 2db86ff | 2018-04-25 01:11:17 +0000 | [diff] [blame] | 1288 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1289 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), | 
|  | 1290 | state->getConflicting(), | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1291 | priorityScores[priorityScores.size() - 1], | 
|  | 1292 | clientPid, | 
|  | 1293 | states[states.size() - 1]); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1294 |  | 
| Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1295 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); | 
|  | 1296 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1297 | // Find clients that would be evicted | 
|  | 1298 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); | 
|  | 1299 |  | 
|  | 1300 | // If the incoming client was 'evicted,' higher priority clients have the camera in the | 
|  | 1301 | // background, so we cannot do evictions | 
|  | 1302 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { | 
|  | 1303 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" | 
|  | 1304 | " priority).", clientPid); | 
|  | 1305 |  | 
|  | 1306 | sp<BasicClient> clientSp = clientDescriptor->getValue(); | 
|  | 1307 | String8 curTime = getFormattedCurrentTime(); | 
|  | 1308 | auto incompatibleClients = | 
|  | 1309 | mActiveClientManager.getIncompatibleClients(clientDescriptor); | 
|  | 1310 |  | 
|  | 1311 | 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] | 1312 | "(PID %d, score %d state %d) due to eviction policy", curTime.string(), | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1313 | cameraId.string(), packageName.string(), clientPid, | 
| Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1314 | clientPriority.getScore(), clientPriority.getState()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1315 |  | 
|  | 1316 | for (auto& i : incompatibleClients) { | 
|  | 1317 | msg.appendFormat("\n   - Blocked by existing device %s client for package %s" | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1318 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", | 
|  | 1319 | i->getKey().string(), | 
|  | 1320 | String8{i->getValue()->getPackageName()}.string(), | 
|  | 1321 | i->getOwnerId(), i->getPriority().getScore(), | 
|  | 1322 | i->getPriority().getState()); | 
| Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1323 | ALOGE("   Conflicts with: Device %s, client package %s (PID %" | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1324 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(), | 
| Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1325 | String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(), | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1326 | i->getPriority().getScore(), i->getPriority().getState()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1327 | } | 
|  | 1328 |  | 
|  | 1329 | // Log the client's attempt | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1330 | Mutex::Autolock l(mLogLock); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1331 | mEventLog.add(msg); | 
|  | 1332 |  | 
| Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1333 | auto current = mActiveClientManager.get(cameraId); | 
|  | 1334 | if (current != nullptr) { | 
|  | 1335 | return -EBUSY; // CAMERA_IN_USE | 
|  | 1336 | } else { | 
|  | 1337 | return -EUSERS; // MAX_CAMERAS_IN_USE | 
|  | 1338 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1339 | } | 
|  | 1340 |  | 
|  | 1341 | for (auto& i : evicted) { | 
|  | 1342 | sp<BasicClient> clientSp = i->getValue(); | 
|  | 1343 | if (clientSp.get() == nullptr) { | 
|  | 1344 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); | 
|  | 1345 |  | 
|  | 1346 | // TODO: Remove this | 
|  | 1347 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", | 
|  | 1348 | __FUNCTION__); | 
|  | 1349 | mActiveClientManager.remove(i); | 
|  | 1350 | continue; | 
|  | 1351 | } | 
|  | 1352 |  | 
|  | 1353 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", | 
|  | 1354 | i->getKey().string()); | 
| Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1355 | evictedClients.push_back(i); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1356 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1357 | // Log the clients evicted | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1358 | logEvent(String8::format("EVICT device %s client held by package %s (PID" | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1359 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" | 
|  | 1360 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1361 | i->getKey().string(), String8{clientSp->getPackageName()}.string(), | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1362 | i->getOwnerId(), i->getPriority().getScore(), | 
|  | 1363 | i->getPriority().getState(), cameraId.string(), | 
| Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1364 | packageName.string(), clientPid, clientPriority.getScore(), | 
|  | 1365 | clientPriority.getState())); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1366 |  | 
|  | 1367 | // Notify the client of disconnection | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1368 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1369 | CaptureResultExtras()); | 
| Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1370 | } | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1371 | } | 
|  | 1372 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1373 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking | 
|  | 1374 | // other clients from connecting in mServiceLockWrapper if held | 
|  | 1375 | mServiceLock.unlock(); | 
|  | 1376 |  | 
|  | 1377 | // Clear caller identity temporarily so client disconnect PID checks work correctly | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1378 | int64_t token = CameraThreadState::clearCallingIdentity(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1379 |  | 
|  | 1380 | // Destroy evicted clients | 
|  | 1381 | for (auto& i : evictedClients) { | 
|  | 1382 | // 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] | 1383 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1384 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1385 |  | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1386 | CameraThreadState::restoreCallingIdentity(token); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1387 |  | 
| Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1388 | for (const auto& i : evictedClients) { | 
|  | 1389 | ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")", | 
|  | 1390 | __FUNCTION__, i->getKey().string(), i->getOwnerId()); | 
|  | 1391 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); | 
|  | 1392 | if (ret == TIMED_OUT) { | 
|  | 1393 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " | 
|  | 1394 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), | 
|  | 1395 | mActiveClientManager.toString().string()); | 
|  | 1396 | return -EBUSY; | 
|  | 1397 | } | 
|  | 1398 | if (ret != NO_ERROR) { | 
|  | 1399 | ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), " | 
|  | 1400 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret), | 
|  | 1401 | ret, mActiveClientManager.toString().string()); | 
|  | 1402 | return ret; | 
|  | 1403 | } | 
|  | 1404 | } | 
|  | 1405 |  | 
|  | 1406 | evictedClients.clear(); | 
|  | 1407 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1408 | // Once clients have been disconnected, relock | 
|  | 1409 | mServiceLock.lock(); | 
|  | 1410 |  | 
|  | 1411 | // Check again if the device was unplugged or something while we weren't holding mServiceLock | 
|  | 1412 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { | 
|  | 1413 | return ret; | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1414 | } | 
|  | 1415 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1416 | *partial = clientDescriptor; | 
|  | 1417 | return NO_ERROR; | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1418 | } | 
|  | 1419 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1420 | Status CameraService::connect( | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1421 | const sp<ICameraClient>& cameraClient, | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1422 | int api1CameraId, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1423 | const String16& clientPackageName, | 
| Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1424 | int clientUid, | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1425 | int clientPid, | 
| Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1426 | /*out*/ | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1427 | sp<ICamera>* device) { | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1428 |  | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1429 | ATRACE_CALL(); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1430 | Status ret = Status::ok(); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1431 |  | 
|  | 1432 | String8 id = cameraIdIntToStr(api1CameraId); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1433 | sp<Client> client = nullptr; | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1434 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId, | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1435 | clientPackageName, {}, clientUid, clientPid, API_1, | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 1436 | /*shimUpdateOnly*/ false, /*out*/client); | 
| Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1437 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1438 | if(!ret.isOk()) { | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1439 | logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageName), | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1440 | ret.toString8()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1441 | return ret; | 
| Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1442 | } | 
|  | 1443 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1444 | *device = client; | 
|  | 1445 | return ret; | 
| Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1446 | } | 
|  | 1447 |  | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1448 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, | 
|  | 1449 | bool isVendorListener, int clientPid, int clientUid) { | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1450 | // If the client is not a vendor client, don't add listener if | 
|  | 1451 | //   a) the camera is a publicly hidden secure camera OR | 
|  | 1452 | //   b) the camera is a system only camera and the client doesn't | 
|  | 1453 | //      have android.permission.SYSTEM_CAMERA permissions. | 
|  | 1454 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || | 
|  | 1455 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && | 
|  | 1456 | !hasPermissionsForSystemCamera(clientPid, clientUid)))) { | 
| Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 1457 | return true; | 
|  | 1458 | } | 
|  | 1459 | return false; | 
|  | 1460 | } | 
|  | 1461 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1462 | bool CameraService::shouldRejectSystemCameraConnection(const String8& cameraId) const { | 
|  | 1463 | // Rules for rejection: | 
|  | 1464 | // 1) If cameraserver tries to access this camera device, accept the | 
|  | 1465 | //    connection. | 
|  | 1466 | // 2) The camera device is a publicly hidden secure camera device AND some | 
|  | 1467 | //    component is trying to access it on a non-hwbinder thread (generally a non HAL client), | 
|  | 1468 | //    reject it. | 
|  | 1469 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY | 
|  | 1470 | //    and the serving thread is a non hwbinder thread, the client must have | 
|  | 1471 | //    android.permission.SYSTEM_CAMERA permissions to connect. | 
|  | 1472 |  | 
|  | 1473 | int cPid = CameraThreadState::getCallingPid(); | 
|  | 1474 | int cUid = CameraThreadState::getCallingUid(); | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1475 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; | 
|  | 1476 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1477 | // This isn't a known camera ID, so it's not a system camera | 
|  | 1478 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); | 
|  | 1479 | return false; | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1480 | } | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1481 |  | 
|  | 1482 | // (1) Cameraserver trying to connect, accept. | 
|  | 1483 | if (CameraThreadState::getCallingPid() == getpid()) { | 
|  | 1484 | return false; | 
|  | 1485 | } | 
|  | 1486 | // (2) | 
| Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 1487 | if (getCurrentServingCall() != BinderCallType::HWBINDER && | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1488 | systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { | 
|  | 1489 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); | 
|  | 1490 | return true; | 
|  | 1491 | } | 
|  | 1492 | // (3) Here we only check for permissions if it is a system only camera device. This is since | 
|  | 1493 | //     getCameraCharacteristics() allows for calls to succeed (albeit after hiding some | 
|  | 1494 | //     characteristics) even if clients don't have android.permission.CAMERA. We do not want the | 
|  | 1495 | //     same behavior for system camera devices. | 
| Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 1496 | if (getCurrentServingCall() != BinderCallType::HWBINDER && | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1497 | systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && | 
|  | 1498 | !hasPermissionsForSystemCamera(cPid, cUid)) { | 
|  | 1499 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", | 
|  | 1500 | cameraId.c_str()); | 
|  | 1501 | return true; | 
|  | 1502 | } | 
|  | 1503 |  | 
|  | 1504 | return false; | 
|  | 1505 | } | 
|  | 1506 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1507 | Status CameraService::connectDevice( | 
|  | 1508 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1509 | const String16& cameraId, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1510 | const String16& clientPackageName, | 
| Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1511 | const std::optional<String16>& clientFeatureId, | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1512 | int clientUid, | 
|  | 1513 | /*out*/ | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1514 | sp<hardware::camera2::ICameraDeviceUser>* device) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1515 |  | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1516 | ATRACE_CALL(); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1517 | Status ret = Status::ok(); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1518 | String8 id = String8(cameraId); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1519 | sp<CameraDeviceClient> client = nullptr; | 
| Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1520 | String16 clientPackageNameAdj = clientPackageName; | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 1521 |  | 
| Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 1522 | if (getCurrentServingCall() == BinderCallType::HWBINDER) { | 
| Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1523 | std::string vendorClient = | 
|  | 1524 | StringPrintf("vendor.client.pid<%d>", CameraThreadState::getCallingPid()); | 
|  | 1525 | clientPackageNameAdj = String16(vendorClient.c_str()); | 
|  | 1526 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1527 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id, | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1528 | /*api1CameraId*/-1, clientPackageNameAdj, clientFeatureId, | 
| Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 1529 | clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, /*out*/client); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1530 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1531 | if(!ret.isOk()) { | 
| Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1532 | logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageNameAdj), | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1533 | ret.toString8()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1534 | return ret; | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1535 | } | 
|  | 1536 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1537 | *device = client; | 
|  | 1538 | return ret; | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1539 | } | 
|  | 1540 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1541 | template<class CALLBACK, class CLIENT> | 
|  | 1542 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1543 | int api1CameraId, const String16& clientPackageName, | 
| Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1544 | const std::optional<String16>& clientFeatureId, int clientUid, int clientPid, | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 1545 | apiLevel effectiveApiLevel, bool shimUpdateOnly, | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1546 | /*out*/sp<CLIENT>& device) { | 
|  | 1547 | binder::Status ret = binder::Status::ok(); | 
|  | 1548 |  | 
|  | 1549 | String8 clientName8(clientPackageName); | 
|  | 1550 |  | 
|  | 1551 | int originalClientPid = 0; | 
|  | 1552 |  | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1553 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1554 | "Camera API version %d", clientPid, clientName8.string(), cameraId.string(), | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1555 | static_cast<int>(effectiveApiLevel)); | 
|  | 1556 |  | 
| Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1557 | nsecs_t openTimeNs = systemTime(); | 
|  | 1558 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1559 | sp<CLIENT> client = nullptr; | 
| Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1560 | int facing = -1; | 
| Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1561 | int orientation = 0; | 
| Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1562 | bool isNdk = (clientPackageName.size() == 0); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1563 | { | 
|  | 1564 | // Acquire mServiceLock and prevent other clients from connecting | 
|  | 1565 | std::unique_ptr<AutoConditionLock> lock = | 
|  | 1566 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); | 
|  | 1567 |  | 
|  | 1568 | if (lock == nullptr) { | 
|  | 1569 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." | 
|  | 1570 | , clientPid); | 
|  | 1571 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, | 
|  | 1572 | "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting", | 
|  | 1573 | cameraId.string(), clientName8.string(), clientPid); | 
|  | 1574 | } | 
|  | 1575 |  | 
| Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 1576 | // Enforce client permissions and do basic validity checks | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1577 | if(!(ret = validateConnectLocked(cameraId, clientName8, | 
|  | 1578 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { | 
|  | 1579 | return ret; | 
|  | 1580 | } | 
|  | 1581 |  | 
|  | 1582 | // Check the shim parameters after acquiring lock, if they have already been updated and | 
|  | 1583 | // we were doing a shim update, return immediately | 
|  | 1584 | if (shimUpdateOnly) { | 
|  | 1585 | auto cameraState = getCameraState(cameraId); | 
|  | 1586 | if (cameraState != nullptr) { | 
|  | 1587 | if (!cameraState->getShimParams().isEmpty()) return ret; | 
|  | 1588 | } | 
|  | 1589 | } | 
|  | 1590 |  | 
|  | 1591 | status_t err; | 
|  | 1592 |  | 
|  | 1593 | sp<BasicClient> clientTmp = nullptr; | 
|  | 1594 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial; | 
|  | 1595 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, | 
|  | 1596 | IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp, | 
|  | 1597 | /*out*/&partial)) != NO_ERROR) { | 
|  | 1598 | switch (err) { | 
|  | 1599 | case -ENODEV: | 
|  | 1600 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, | 
|  | 1601 | "No camera device with ID \"%s\" currently available", | 
|  | 1602 | cameraId.string()); | 
|  | 1603 | case -EBUSY: | 
|  | 1604 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, | 
|  | 1605 | "Higher-priority client using camera, ID \"%s\" currently unavailable", | 
|  | 1606 | cameraId.string()); | 
| Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1607 | case -EUSERS: | 
|  | 1608 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, | 
|  | 1609 | "Too many cameras already open, cannot open camera \"%s\"", | 
|  | 1610 | cameraId.string()); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1611 | default: | 
|  | 1612 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 1613 | "Unexpected error %s (%d) opening camera \"%s\"", | 
|  | 1614 | strerror(-err), err, cameraId.string()); | 
|  | 1615 | } | 
|  | 1616 | } | 
|  | 1617 |  | 
|  | 1618 | if (clientTmp.get() != nullptr) { | 
|  | 1619 | // Handle special case for API1 MediaRecorder where the existing client is returned | 
|  | 1620 | device = static_cast<CLIENT*>(clientTmp.get()); | 
|  | 1621 | return ret; | 
|  | 1622 | } | 
|  | 1623 |  | 
|  | 1624 | // give flashlight a chance to close devices if necessary. | 
|  | 1625 | mFlashlight->prepareDeviceOpen(cameraId); | 
|  | 1626 |  | 
| Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1627 | int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing, /*out*/&orientation); | 
| Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 1628 | if (facing == -1) { | 
|  | 1629 | ALOGE("%s: Unable to get camera device \"%s\"  facing", __FUNCTION__, cameraId.string()); | 
|  | 1630 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 1631 | "Unable to get camera device \"%s\" facing", cameraId.string()); | 
|  | 1632 | } | 
|  | 1633 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1634 | sp<BasicClient> tmp = nullptr; | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 1635 | if(!(ret = makeClient(this, cameraCb, clientPackageName, clientFeatureId, | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1636 | cameraId, api1CameraId, facing, | 
| Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 1637 | clientPid, clientUid, getpid(), | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1638 | deviceVersion, effectiveApiLevel, | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1639 | /*out*/&tmp)).isOk()) { | 
|  | 1640 | return ret; | 
|  | 1641 | } | 
|  | 1642 | client = static_cast<CLIENT*>(tmp.get()); | 
|  | 1643 |  | 
|  | 1644 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", | 
|  | 1645 | __FUNCTION__); | 
|  | 1646 |  | 
| Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 1647 | err = client->initialize(mCameraProviderManager, mMonitorTags); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1648 | if (err != OK) { | 
|  | 1649 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1650 | // Errors could be from the HAL module open call or from AppOpsManager | 
|  | 1651 | switch(err) { | 
|  | 1652 | case BAD_VALUE: | 
|  | 1653 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 1654 | "Illegal argument to HAL module for camera \"%s\"", cameraId.string()); | 
|  | 1655 | case -EBUSY: | 
|  | 1656 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, | 
|  | 1657 | "Camera \"%s\" is already open", cameraId.string()); | 
|  | 1658 | case -EUSERS: | 
|  | 1659 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, | 
|  | 1660 | "Too many cameras already open, cannot open camera \"%s\"", | 
|  | 1661 | cameraId.string()); | 
|  | 1662 | case PERMISSION_DENIED: | 
|  | 1663 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 1664 | "No permission to open camera \"%s\"", cameraId.string()); | 
|  | 1665 | case -EACCES: | 
|  | 1666 | return STATUS_ERROR_FMT(ERROR_DISABLED, | 
|  | 1667 | "Camera \"%s\" disabled by policy", cameraId.string()); | 
|  | 1668 | case -ENODEV: | 
|  | 1669 | default: | 
|  | 1670 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 1671 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(), | 
|  | 1672 | strerror(-err), err); | 
|  | 1673 | } | 
|  | 1674 | } | 
|  | 1675 |  | 
|  | 1676 | // Update shim paremeters for legacy clients | 
|  | 1677 | if (effectiveApiLevel == API_1) { | 
|  | 1678 | // Assume we have always received a Client subclass for API1 | 
|  | 1679 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); | 
|  | 1680 | String8 rawParams = shimClient->getParameters(); | 
|  | 1681 | CameraParameters params(rawParams); | 
|  | 1682 |  | 
|  | 1683 | auto cameraState = getCameraState(cameraId); | 
|  | 1684 | if (cameraState != nullptr) { | 
|  | 1685 | cameraState->setShimParams(params); | 
|  | 1686 | } else { | 
|  | 1687 | ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.", | 
|  | 1688 | __FUNCTION__, cameraId.string()); | 
|  | 1689 | } | 
|  | 1690 | } | 
|  | 1691 |  | 
| Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1692 | // Set rotate-and-crop override behavior | 
|  | 1693 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { | 
|  | 1694 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); | 
| Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1695 | } else if (CameraServiceProxyWrapper::isRotateAndCropOverrideNeeded(clientPackageName, | 
|  | 1696 | orientation, facing)) { | 
|  | 1697 | client->setRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_90); | 
| Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1698 | } | 
|  | 1699 |  | 
| Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 1700 | // Set camera muting behavior | 
|  | 1701 | if (client->supportsCameraMute()) { | 
| Evan Severson | 190ad25 | 2021-01-07 20:37:15 -0800 | [diff] [blame] | 1702 | client->setCameraMute(mOverrideCameraMuteMode || | 
|  | 1703 | isUserSensorPrivacyEnabledForUid(clientUid)); | 
| Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 1704 | } | 
|  | 1705 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1706 | if (shimUpdateOnly) { | 
|  | 1707 | // If only updating legacy shim parameters, immediately disconnect client | 
|  | 1708 | mServiceLock.unlock(); | 
|  | 1709 | client->disconnect(); | 
|  | 1710 | mServiceLock.lock(); | 
|  | 1711 | } else { | 
|  | 1712 | // Otherwise, add client to active clients list | 
|  | 1713 | finishConnectLocked(client, partial); | 
|  | 1714 | } | 
| Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 1715 |  | 
|  | 1716 | client->setImageDumpMask(mImageDumpMask); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1717 | } // lock is destroyed, allow further connect calls | 
|  | 1718 |  | 
|  | 1719 | // Important: release the mutex here so the client can call back into the service from its | 
|  | 1720 | // destructor (can be at the end of the call) | 
|  | 1721 | device = client; | 
| Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1722 |  | 
|  | 1723 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); | 
|  | 1724 | CameraServiceProxyWrapper::logOpen(cameraId, facing, clientPackageName, | 
|  | 1725 | effectiveApiLevel, isNdk, openLatencyMs); | 
|  | 1726 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1727 | return ret; | 
|  | 1728 | } | 
|  | 1729 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 1730 | status_t CameraService::addOfflineClient(String8 cameraId, sp<BasicClient> offlineClient) { | 
|  | 1731 | if (offlineClient.get() == nullptr) { | 
|  | 1732 | return BAD_VALUE; | 
|  | 1733 | } | 
|  | 1734 |  | 
|  | 1735 | { | 
|  | 1736 | // Acquire mServiceLock and prevent other clients from connecting | 
|  | 1737 | std::unique_ptr<AutoConditionLock> lock = | 
|  | 1738 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); | 
|  | 1739 |  | 
|  | 1740 | if (lock == nullptr) { | 
|  | 1741 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." | 
|  | 1742 | , __FUNCTION__, offlineClient->getClientPid()); | 
|  | 1743 | return TIMED_OUT; | 
|  | 1744 | } | 
|  | 1745 |  | 
|  | 1746 | auto onlineClientDesc = mActiveClientManager.get(cameraId); | 
|  | 1747 | if (onlineClientDesc.get() == nullptr) { | 
|  | 1748 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, | 
|  | 1749 | cameraId.c_str()); | 
|  | 1750 | return BAD_VALUE; | 
|  | 1751 | } | 
|  | 1752 |  | 
|  | 1753 | // Offline clients do not evict or conflict with other online devices. Resource sharing | 
|  | 1754 | // conflicts are handled by the camera provider which will either succeed or fail before | 
|  | 1755 | // reaching this method. | 
|  | 1756 | const auto& onlinePriority = onlineClientDesc->getPriority(); | 
|  | 1757 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( | 
|  | 1758 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, | 
|  | 1759 | /*conflictingKeys*/ std::set<String8>(), onlinePriority.getScore(), | 
|  | 1760 | onlineClientDesc->getOwnerId(), onlinePriority.getState()); | 
|  | 1761 |  | 
|  | 1762 | // Allow only one offline device per camera | 
|  | 1763 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); | 
|  | 1764 | if (!incompatibleClients.empty()) { | 
|  | 1765 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); | 
|  | 1766 | return BAD_VALUE; | 
|  | 1767 | } | 
|  | 1768 |  | 
|  | 1769 | auto err = offlineClient->initialize(mCameraProviderManager, mMonitorTags); | 
|  | 1770 | if (err != OK) { | 
|  | 1771 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); | 
|  | 1772 | return err; | 
|  | 1773 | } | 
|  | 1774 |  | 
|  | 1775 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); | 
|  | 1776 | if (evicted.size() > 0) { | 
|  | 1777 | for (auto& i : evicted) { | 
|  | 1778 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", | 
|  | 1779 | __FUNCTION__, i->getKey().string()); | 
|  | 1780 | } | 
|  | 1781 |  | 
|  | 1782 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " | 
|  | 1783 | "properly", __FUNCTION__); | 
|  | 1784 |  | 
|  | 1785 | return BAD_VALUE; | 
|  | 1786 | } | 
|  | 1787 |  | 
|  | 1788 | logConnectedOffline(offlineClientDesc->getKey(), | 
|  | 1789 | static_cast<int>(offlineClientDesc->getOwnerId()), | 
|  | 1790 | String8(offlineClient->getPackageName())); | 
|  | 1791 |  | 
|  | 1792 | sp<IBinder> remoteCallback = offlineClient->getRemote(); | 
|  | 1793 | if (remoteCallback != nullptr) { | 
|  | 1794 | remoteCallback->linkToDeath(this); | 
|  | 1795 | } | 
|  | 1796 | } // lock is destroyed, allow further connect calls | 
|  | 1797 |  | 
|  | 1798 | return OK; | 
|  | 1799 | } | 
|  | 1800 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1801 | Status CameraService::setTorchMode(const String16& cameraId, bool enabled, | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1802 | const sp<IBinder>& clientBinder) { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1803 | Mutex::Autolock lock(mServiceLock); | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1804 |  | 
|  | 1805 | ATRACE_CALL(); | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1806 | if (enabled && clientBinder == nullptr) { | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1807 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1808 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, | 
|  | 1809 | "Torch client Binder is null"); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1810 | } | 
|  | 1811 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1812 | String8 id = String8(cameraId.string()); | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1813 | int uid = CameraThreadState::getCallingUid(); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1814 |  | 
|  | 1815 | // verify id is valid. | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1816 | auto state = getCameraState(id); | 
|  | 1817 | if (state == nullptr) { | 
| Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 1818 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1819 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 1820 | "Camera ID \"%s\" is a not valid camera ID", id.string()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1821 | } | 
|  | 1822 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1823 | StatusInternal cameraStatus = state->getStatus(); | 
|  | 1824 | if (cameraStatus != StatusInternal::PRESENT && | 
| Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 1825 | cameraStatus != StatusInternal::NOT_AVAILABLE) { | 
|  | 1826 | 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] | 1827 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 1828 | "Camera ID \"%s\" is a not valid camera ID", id.string()); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1829 | } | 
|  | 1830 |  | 
|  | 1831 | { | 
|  | 1832 | Mutex::Autolock al(mTorchStatusMutex); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1833 | TorchModeStatus status; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1834 | status_t err = getTorchStatusLocked(id, &status); | 
|  | 1835 | if (err != OK) { | 
|  | 1836 | if (err == NAME_NOT_FOUND) { | 
|  | 1837 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 1838 | "Camera \"%s\" does not have a flash unit", id.string()); | 
|  | 1839 | } | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1840 | ALOGE("%s: getting current torch status failed for camera %s", | 
|  | 1841 | __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1842 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 1843 | "Error updating torch status for camera \"%s\": %s (%d)", id.string(), | 
|  | 1844 | strerror(-err), err); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1845 | } | 
|  | 1846 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1847 | if (status == TorchModeStatus::NOT_AVAILABLE) { | 
| Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 1848 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1849 | ALOGE("%s: torch mode of camera %s is not available because " | 
|  | 1850 | "camera is in use", __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1851 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, | 
|  | 1852 | "Torch for camera \"%s\" is not available due to an existing camera user", | 
|  | 1853 | id.string()); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1854 | } else { | 
|  | 1855 | ALOGE("%s: torch mode of camera %s is not available due to " | 
|  | 1856 | "insufficient resources", __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1857 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, | 
|  | 1858 | "Torch for camera \"%s\" is not available due to insufficient resources", | 
|  | 1859 | id.string()); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1860 | } | 
|  | 1861 | } | 
|  | 1862 | } | 
|  | 1863 |  | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1864 | { | 
|  | 1865 | // Update UID map - this is used in the torch status changed callbacks, so must be done | 
|  | 1866 | // before setTorchMode | 
| Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 1867 | Mutex::Autolock al(mTorchUidMapMutex); | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1868 | if (mTorchUidMap.find(id) == mTorchUidMap.end()) { | 
|  | 1869 | mTorchUidMap[id].first = uid; | 
|  | 1870 | mTorchUidMap[id].second = uid; | 
|  | 1871 | } else { | 
|  | 1872 | // Set the pending UID | 
|  | 1873 | mTorchUidMap[id].first = uid; | 
|  | 1874 | } | 
|  | 1875 | } | 
|  | 1876 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1877 | status_t err = mFlashlight->setTorchMode(id, enabled); | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1878 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1879 | if (err != OK) { | 
|  | 1880 | int32_t errorCode; | 
|  | 1881 | String8 msg; | 
|  | 1882 | switch (err) { | 
|  | 1883 | case -ENOSYS: | 
|  | 1884 | msg = String8::format("Camera \"%s\" has no flashlight", | 
|  | 1885 | id.string()); | 
|  | 1886 | errorCode = ERROR_ILLEGAL_ARGUMENT; | 
|  | 1887 | break; | 
|  | 1888 | default: | 
|  | 1889 | msg = String8::format( | 
|  | 1890 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", | 
|  | 1891 | id.string(), enabled, strerror(-err), err); | 
|  | 1892 | errorCode = ERROR_INVALID_OPERATION; | 
|  | 1893 | } | 
|  | 1894 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 1895 | return STATUS_ERROR(errorCode, msg.string()); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1896 | } | 
|  | 1897 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1898 | { | 
|  | 1899 | // update the link to client's death | 
|  | 1900 | Mutex::Autolock al(mTorchClientMapMutex); | 
|  | 1901 | ssize_t index = mTorchClientMap.indexOfKey(id); | 
|  | 1902 | if (enabled) { | 
|  | 1903 | if (index == NAME_NOT_FOUND) { | 
|  | 1904 | mTorchClientMap.add(id, clientBinder); | 
|  | 1905 | } else { | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1906 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1907 | mTorchClientMap.replaceValueAt(index, clientBinder); | 
|  | 1908 | } | 
|  | 1909 | clientBinder->linkToDeath(this); | 
|  | 1910 | } else if (index != NAME_NOT_FOUND) { | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1911 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1912 | } | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1913 | } | 
|  | 1914 |  | 
| Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 1915 | int clientPid = CameraThreadState::getCallingPid(); | 
|  | 1916 | const char *id_cstr = id.c_str(); | 
|  | 1917 | const char *torchState = enabled ? "on" : "off"; | 
|  | 1918 | ALOGI("Torch for camera id %s turned %s for client PID %d", id_cstr, torchState, clientPid); | 
|  | 1919 | logTorchEvent(id_cstr, torchState , clientPid); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1920 | return Status::ok(); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1921 | } | 
|  | 1922 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1923 | Status CameraService::notifySystemEvent(int32_t eventId, | 
|  | 1924 | const std::vector<int32_t>& args) { | 
| Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 1925 | const int pid = CameraThreadState::getCallingPid(); | 
|  | 1926 | const int selfPid = getpid(); | 
|  | 1927 |  | 
|  | 1928 | // Permission checks | 
|  | 1929 | if (pid != selfPid) { | 
|  | 1930 | // Ensure we're being called by system_server, or similar process with | 
|  | 1931 | // permissions to notify the camera service about system events | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1932 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { | 
| Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 1933 | const int uid = CameraThreadState::getCallingUid(); | 
|  | 1934 | ALOGE("Permission Denial: cannot send updates to camera service about system" | 
|  | 1935 | " events from pid=%d, uid=%d", pid, uid); | 
|  | 1936 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
| Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 1937 | "No permission to send updates to camera service about system events" | 
|  | 1938 | " from pid=%d, uid=%d", pid, uid); | 
| Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 1939 | } | 
|  | 1940 | } | 
|  | 1941 |  | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1942 | ATRACE_CALL(); | 
|  | 1943 |  | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1944 | switch(eventId) { | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1945 | case ICameraService::EVENT_USER_SWITCHED: { | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1946 | // Try to register for UID and sensor privacy policy updates, in case we're recovering | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 1947 | // from a system server crash | 
|  | 1948 | mUidPolicy->registerSelf(); | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1949 | mSensorPrivacyPolicy->registerSelf(); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1950 | doUserSwitch(/*newUserIds*/ args); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1951 | break; | 
|  | 1952 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1953 | case ICameraService::EVENT_NONE: | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1954 | default: { | 
|  | 1955 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, | 
|  | 1956 | eventId); | 
|  | 1957 | break; | 
|  | 1958 | } | 
|  | 1959 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1960 | return Status::ok(); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1961 | } | 
|  | 1962 |  | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 1963 | void CameraService::notifyMonitoredUids() { | 
|  | 1964 | Mutex::Autolock lock(mStatusListenerLock); | 
|  | 1965 |  | 
|  | 1966 | for (const auto& it : mListenerList) { | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1967 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 1968 | if (!ret.isOk()) { | 
|  | 1969 | ALOGE("%s: Failed to trigger permission callback: %d", __FUNCTION__, | 
|  | 1970 | ret.exceptionCode()); | 
|  | 1971 | } | 
|  | 1972 | } | 
|  | 1973 | } | 
|  | 1974 |  | 
| Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 1975 | Status CameraService::notifyDeviceStateChange(int64_t newState) { | 
|  | 1976 | const int pid = CameraThreadState::getCallingPid(); | 
|  | 1977 | const int selfPid = getpid(); | 
|  | 1978 |  | 
|  | 1979 | // Permission checks | 
|  | 1980 | if (pid != selfPid) { | 
|  | 1981 | // Ensure we're being called by system_server, or similar process with | 
|  | 1982 | // permissions to notify the camera service about system events | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1983 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { | 
| Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 1984 | const int uid = CameraThreadState::getCallingUid(); | 
|  | 1985 | ALOGE("Permission Denial: cannot send updates to camera service about device" | 
|  | 1986 | " state changes from pid=%d, uid=%d", pid, uid); | 
|  | 1987 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 1988 | "No permission to send updates to camera service about device state" | 
|  | 1989 | " changes from pid=%d, uid=%d", pid, uid); | 
|  | 1990 | } | 
|  | 1991 | } | 
|  | 1992 |  | 
|  | 1993 | ATRACE_CALL(); | 
|  | 1994 |  | 
|  | 1995 | using hardware::camera::provider::V2_5::DeviceState; | 
|  | 1996 | hardware::hidl_bitfield<DeviceState> newDeviceState{}; | 
|  | 1997 | if (newState & ICameraService::DEVICE_STATE_BACK_COVERED) { | 
|  | 1998 | newDeviceState |= DeviceState::BACK_COVERED; | 
|  | 1999 | } | 
|  | 2000 | if (newState & ICameraService::DEVICE_STATE_FRONT_COVERED) { | 
|  | 2001 | newDeviceState |= DeviceState::FRONT_COVERED; | 
|  | 2002 | } | 
|  | 2003 | if (newState & ICameraService::DEVICE_STATE_FOLDED) { | 
|  | 2004 | newDeviceState |= DeviceState::FOLDED; | 
|  | 2005 | } | 
|  | 2006 | // Only map vendor bits directly | 
|  | 2007 | uint64_t vendorBits = static_cast<uint64_t>(newState) & 0xFFFFFFFF00000000l; | 
|  | 2008 | newDeviceState |= vendorBits; | 
|  | 2009 |  | 
|  | 2010 | ALOGV("%s: New device state 0x%" PRIx64, __FUNCTION__, newDeviceState); | 
|  | 2011 | Mutex::Autolock l(mServiceLock); | 
|  | 2012 | mCameraProviderManager->notifyDeviceStateChange(newDeviceState); | 
|  | 2013 |  | 
|  | 2014 | return Status::ok(); | 
|  | 2015 | } | 
|  | 2016 |  | 
| Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 2017 | Status CameraService::getConcurrentCameraIds( | 
| Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2018 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { | 
|  | 2019 | ATRACE_CALL(); | 
|  | 2020 | if (!concurrentCameraIds) { | 
|  | 2021 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); | 
|  | 2022 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); | 
|  | 2023 | } | 
|  | 2024 |  | 
|  | 2025 | if (!mInitialized) { | 
|  | 2026 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); | 
|  | 2027 | return STATUS_ERROR(ERROR_DISCONNECTED, | 
|  | 2028 | "Camera subsystem is not available"); | 
|  | 2029 | } | 
|  | 2030 | // First call into the provider and get the set of concurrent camera | 
|  | 2031 | // combinations | 
|  | 2032 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = | 
| Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 2033 | mCameraProviderManager->getConcurrentCameraIds(); | 
| Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2034 | for (auto &combination : concurrentCameraCombinations) { | 
|  | 2035 | std::vector<std::string> validCombination; | 
|  | 2036 | for (auto &cameraId : combination) { | 
|  | 2037 | // if the camera state is not present, skip | 
|  | 2038 | String8 cameraIdStr(cameraId.c_str()); | 
|  | 2039 | auto state = getCameraState(cameraIdStr); | 
|  | 2040 | if (state == nullptr) { | 
|  | 2041 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); | 
|  | 2042 | continue; | 
|  | 2043 | } | 
|  | 2044 | StatusInternal status = state->getStatus(); | 
|  | 2045 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { | 
|  | 2046 | continue; | 
|  | 2047 | } | 
|  | 2048 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { | 
|  | 2049 | continue; | 
|  | 2050 | } | 
|  | 2051 | validCombination.push_back(cameraId); | 
|  | 2052 | } | 
|  | 2053 | if (validCombination.size() != 0) { | 
|  | 2054 | concurrentCameraIds->push_back(std::move(validCombination)); | 
|  | 2055 | } | 
|  | 2056 | } | 
|  | 2057 | return Status::ok(); | 
|  | 2058 | } | 
|  | 2059 |  | 
|  | 2060 | Status CameraService::isConcurrentSessionConfigurationSupported( | 
|  | 2061 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, | 
|  | 2062 | /*out*/bool* isSupported) { | 
|  | 2063 | if (!isSupported) { | 
|  | 2064 | ALOGE("%s: isSupported is NULL", __FUNCTION__); | 
|  | 2065 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); | 
|  | 2066 | } | 
|  | 2067 |  | 
|  | 2068 | if (!mInitialized) { | 
|  | 2069 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); | 
|  | 2070 | return STATUS_ERROR(ERROR_DISCONNECTED, | 
|  | 2071 | "Camera subsystem is not available"); | 
|  | 2072 | } | 
|  | 2073 |  | 
|  | 2074 | // Check for camera permissions | 
|  | 2075 | int callingPid = CameraThreadState::getCallingPid(); | 
|  | 2076 | int callingUid = CameraThreadState::getCallingUid(); | 
|  | 2077 | if ((callingPid != getpid()) && !checkPermission(sCameraPermission, callingPid, callingUid)) { | 
|  | 2078 | ALOGE("%s: pid %d doesn't have camera permissions", __FUNCTION__, callingPid); | 
|  | 2079 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, | 
|  | 2080 | "android.permission.CAMERA needed to call" | 
|  | 2081 | "isConcurrentSessionConfigurationSupported"); | 
|  | 2082 | } | 
|  | 2083 |  | 
|  | 2084 | status_t res = | 
|  | 2085 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( | 
|  | 2086 | cameraIdsAndSessionConfigurations, isSupported); | 
|  | 2087 | if (res != OK) { | 
|  | 2088 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " | 
|  | 2089 | "support %s (%d)", strerror(-res), res); | 
|  | 2090 | } | 
|  | 2091 | return Status::ok(); | 
|  | 2092 | } | 
|  | 2093 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2094 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, | 
|  | 2095 | /*out*/ | 
|  | 2096 | std::vector<hardware::CameraStatus> *cameraStatuses) { | 
| Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2097 | return addListenerHelper(listener, cameraStatuses); | 
|  | 2098 | } | 
|  | 2099 |  | 
|  | 2100 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, | 
|  | 2101 | /*out*/ | 
|  | 2102 | std::vector<hardware::CameraStatus> *cameraStatuses, | 
|  | 2103 | bool isVendorListener) { | 
|  | 2104 |  | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2105 | ATRACE_CALL(); | 
|  | 2106 |  | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2107 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2108 |  | 
| Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 2109 | if (listener == nullptr) { | 
| Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2110 | ALOGE("%s: Listener must not be null", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2111 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); | 
| Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2112 | } | 
|  | 2113 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2114 | auto clientUid = CameraThreadState::getCallingUid(); | 
|  | 2115 | auto clientPid = CameraThreadState::getCallingPid(); | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2116 | bool openCloseCallbackAllowed = checkPermission(sCameraOpenCloseListenerPermission, | 
|  | 2117 | clientPid, clientUid); | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2118 |  | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2119 | Mutex::Autolock lock(mServiceLock); | 
|  | 2120 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2121 | { | 
|  | 2122 | Mutex::Autolock lock(mStatusListenerLock); | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2123 | for (const auto &it : mListenerList) { | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2124 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2125 | ALOGW("%s: Tried to add listener %p which was already subscribed", | 
|  | 2126 | __FUNCTION__, listener.get()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2127 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2128 | } | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2129 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2130 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2131 | sp<ServiceListener> serviceListener = | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2132 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, | 
|  | 2133 | openCloseCallbackAllowed); | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2134 | auto ret = serviceListener->initialize(); | 
|  | 2135 | if (ret != NO_ERROR) { | 
|  | 2136 | String8 msg = String8::format("Failed to initialize service listener: %s (%d)", | 
|  | 2137 | strerror(-ret), ret); | 
|  | 2138 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 2139 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); | 
|  | 2140 | } | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2141 | // The listener still needs to be added to the list of listeners, regardless of what | 
|  | 2142 | // permissions the listener process has / whether it is a vendor listener. Since it might be | 
|  | 2143 | // eligible to listen to other camera ids. | 
|  | 2144 | mListenerList.emplace_back(serviceListener); | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2145 | mUidPolicy->registerMonitorUid(clientUid); | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2146 | } | 
|  | 2147 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2148 | /* Collect current devices and status */ | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2149 | { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2150 | Mutex::Autolock lock(mCameraStatesLock); | 
|  | 2151 | for (auto& i : mCameraStates) { | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 2152 | cameraStatuses->emplace_back(i.first, | 
|  | 2153 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds()); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2154 | } | 
|  | 2155 | } | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2156 | // Remove the camera statuses that should be hidden from the client, we do | 
|  | 2157 | // this after collecting the states in order to avoid holding | 
|  | 2158 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at | 
|  | 2159 | // the same time. | 
|  | 2160 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), | 
|  | 2161 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { | 
|  | 2162 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
|  | 2163 | if (getSystemCameraKind(s.cameraId, &deviceKind) != OK) { | 
|  | 2164 | ALOGE("%s: Invalid camera id %s, skipping status update", | 
|  | 2165 | __FUNCTION__, s.cameraId.c_str()); | 
|  | 2166 | return true; | 
|  | 2167 | } | 
|  | 2168 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, | 
|  | 2169 | clientUid);}), cameraStatuses->end()); | 
|  | 2170 |  | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2171 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2172 | /* | 
|  | 2173 | * Immediately signal current torch status to this listener only | 
|  | 2174 | * This may be a subset of all the devices, so don't include it in the response directly | 
|  | 2175 | */ | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2176 | { | 
|  | 2177 | Mutex::Autolock al(mTorchStatusMutex); | 
|  | 2178 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2179 | String16 id = String16(mTorchStatusMap.keyAt(i).string()); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2180 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2181 | } | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2182 | } | 
|  | 2183 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2184 | return Status::ok(); | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2185 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2186 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2187 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2188 | ATRACE_CALL(); | 
|  | 2189 |  | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2190 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); | 
|  | 2191 |  | 
| Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2192 | if (listener == 0) { | 
|  | 2193 | ALOGE("%s: Listener must not be null", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2194 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); | 
| Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2195 | } | 
|  | 2196 |  | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2197 | Mutex::Autolock lock(mServiceLock); | 
|  | 2198 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2199 | { | 
|  | 2200 | Mutex::Autolock lock(mStatusListenerLock); | 
|  | 2201 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2202 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { | 
|  | 2203 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid()); | 
|  | 2204 | IInterface::asBinder(listener)->unlinkToDeath(*it); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2205 | mListenerList.erase(it); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2206 | return Status::ok(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2207 | } | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2208 | } | 
|  | 2209 | } | 
|  | 2210 |  | 
|  | 2211 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", | 
|  | 2212 | __FUNCTION__, listener.get()); | 
|  | 2213 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2214 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2215 | } | 
|  | 2216 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2217 | Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2218 |  | 
|  | 2219 | ATRACE_CALL(); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2220 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); | 
|  | 2221 |  | 
|  | 2222 | if (parameters == NULL) { | 
|  | 2223 | ALOGE("%s: parameters must not be null", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2224 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2225 | } | 
|  | 2226 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2227 | Status ret = Status::ok(); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2228 |  | 
|  | 2229 | CameraParameters shimParams; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2230 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2231 | // Error logged by caller | 
|  | 2232 | return ret; | 
|  | 2233 | } | 
|  | 2234 |  | 
|  | 2235 | String8 shimParamsString8 = shimParams.flatten(); | 
|  | 2236 | String16 shimParamsString16 = String16(shimParamsString8); | 
|  | 2237 |  | 
|  | 2238 | *parameters = shimParamsString16; | 
|  | 2239 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2240 | return ret; | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2241 | } | 
|  | 2242 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2243 | Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion, | 
|  | 2244 | /*out*/ bool *isSupported) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2245 | ATRACE_CALL(); | 
|  | 2246 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2247 | const String8 id = String8(cameraId); | 
|  | 2248 |  | 
|  | 2249 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2250 |  | 
|  | 2251 | switch (apiVersion) { | 
|  | 2252 | case API_VERSION_1: | 
|  | 2253 | case API_VERSION_2: | 
|  | 2254 | break; | 
|  | 2255 | default: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2256 | String8 msg = String8::format("Unknown API version %d", apiVersion); | 
|  | 2257 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 2258 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2259 | } | 
|  | 2260 |  | 
| Emilian Peev | 28ad2ea | 2017-02-07 16:14:32 +0000 | [diff] [blame] | 2261 | int deviceVersion = getDeviceVersion(id); | 
| Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2262 | switch (deviceVersion) { | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2263 | case CAMERA_DEVICE_API_VERSION_1_0: | 
|  | 2264 | case CAMERA_DEVICE_API_VERSION_3_0: | 
|  | 2265 | case CAMERA_DEVICE_API_VERSION_3_1: | 
|  | 2266 | if (apiVersion == API_VERSION_2) { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2267 | ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim", | 
|  | 2268 | __FUNCTION__, id.string(), deviceVersion); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2269 | *isSupported = false; | 
|  | 2270 | } else { // if (apiVersion == API_VERSION_1) { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2271 | ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported", | 
|  | 2272 | __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2273 | *isSupported = true; | 
|  | 2274 | } | 
|  | 2275 | break; | 
|  | 2276 | case CAMERA_DEVICE_API_VERSION_3_2: | 
|  | 2277 | case CAMERA_DEVICE_API_VERSION_3_3: | 
| Zhijun He | 4afbdec | 2016-05-04 15:42:51 -0700 | [diff] [blame] | 2278 | case CAMERA_DEVICE_API_VERSION_3_4: | 
| Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2279 | case CAMERA_DEVICE_API_VERSION_3_5: | 
| Yin-Chia Yeh | 56d98ba | 2020-01-16 16:57:14 -0800 | [diff] [blame] | 2280 | case CAMERA_DEVICE_API_VERSION_3_6: | 
| Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 2281 | case CAMERA_DEVICE_API_VERSION_3_7: | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2282 | ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly", | 
|  | 2283 | __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2284 | *isSupported = true; | 
|  | 2285 | break; | 
|  | 2286 | case -1: { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2287 | String8 msg = String8::format("Unknown camera ID %s", id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2288 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 2289 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2290 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2291 | default: { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2292 | String8 msg = String8::format("Unknown device version %x for device %s", | 
|  | 2293 | deviceVersion, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2294 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 2295 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string()); | 
|  | 2296 | } | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2297 | } | 
|  | 2298 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2299 | return Status::ok(); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2300 | } | 
|  | 2301 |  | 
| Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2302 | Status CameraService::isHiddenPhysicalCamera(const String16& cameraId, | 
|  | 2303 | /*out*/ bool *isSupported) { | 
|  | 2304 | ATRACE_CALL(); | 
|  | 2305 |  | 
|  | 2306 | const String8 id = String8(cameraId); | 
|  | 2307 |  | 
|  | 2308 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); | 
|  | 2309 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(id.string()); | 
|  | 2310 |  | 
|  | 2311 | return Status::ok(); | 
|  | 2312 | } | 
|  | 2313 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2314 | void CameraService::removeByClient(const BasicClient* client) { | 
| Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2315 | Mutex::Autolock lock(mServiceLock); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2316 | for (auto& i : mActiveClientManager.getAll()) { | 
|  | 2317 | auto clientSp = i->getValue(); | 
|  | 2318 | if (clientSp.get() == client) { | 
|  | 2319 | mActiveClientManager.remove(i); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2320 | } | 
| Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2321 | } | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2322 | updateAudioRestrictionLocked(); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2323 | } | 
|  | 2324 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2325 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2326 | bool ret = false; | 
|  | 2327 | { | 
|  | 2328 | // Acquire mServiceLock and prevent other clients from connecting | 
|  | 2329 | std::unique_ptr<AutoConditionLock> lock = | 
|  | 2330 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2331 |  | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2332 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2333 | std::vector<sp<BasicClient>> evicted; | 
|  | 2334 | for (auto& i : mActiveClientManager.getAll()) { | 
|  | 2335 | auto clientSp = i->getValue(); | 
|  | 2336 | if (clientSp.get() == nullptr) { | 
|  | 2337 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); | 
|  | 2338 | mActiveClientManager.remove(i); | 
|  | 2339 | continue; | 
|  | 2340 | } | 
| Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 2341 | if (remote == clientSp->getRemote()) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2342 | mActiveClientManager.remove(i); | 
|  | 2343 | evicted.push_back(clientSp); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2344 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2345 | // Notify the client of disconnection | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2346 | clientSp->notifyError( | 
|  | 2347 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2348 | CaptureResultExtras()); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2349 | } | 
|  | 2350 | } | 
|  | 2351 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2352 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking | 
|  | 2353 | // other clients from connecting in mServiceLockWrapper if held | 
|  | 2354 | mServiceLock.unlock(); | 
|  | 2355 |  | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2356 | // Do not clear caller identity, remote caller should be client proccess | 
|  | 2357 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2358 | for (auto& i : evicted) { | 
|  | 2359 | if (i.get() != nullptr) { | 
|  | 2360 | i->disconnect(); | 
|  | 2361 | ret = true; | 
|  | 2362 | } | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2363 | } | 
|  | 2364 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2365 | // Reacquire mServiceLock | 
|  | 2366 | mServiceLock.lock(); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2367 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2368 | } // lock is destroyed, allow further connect calls | 
|  | 2369 |  | 
|  | 2370 | return ret; | 
| Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2371 | } | 
|  | 2372 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2373 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( | 
|  | 2374 | const String8& cameraId) const { | 
|  | 2375 | std::shared_ptr<CameraState> state; | 
|  | 2376 | { | 
|  | 2377 | Mutex::Autolock lock(mCameraStatesLock); | 
|  | 2378 | auto iter = mCameraStates.find(cameraId); | 
|  | 2379 | if (iter != mCameraStates.end()) { | 
|  | 2380 | state = iter->second; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2381 | } | 
|  | 2382 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2383 | return state; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2384 | } | 
|  | 2385 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2386 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) { | 
|  | 2387 | // Remove from active clients list | 
|  | 2388 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); | 
|  | 2389 | if (clientDescriptorPtr == nullptr) { | 
|  | 2390 | ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__, | 
|  | 2391 | cameraId.string()); | 
|  | 2392 | return sp<BasicClient>{nullptr}; | 
|  | 2393 | } | 
|  | 2394 |  | 
|  | 2395 | return clientDescriptorPtr->getValue(); | 
| Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 2396 | } | 
|  | 2397 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2398 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2399 | // Acquire mServiceLock and prevent other clients from connecting | 
|  | 2400 | std::unique_ptr<AutoConditionLock> lock = | 
|  | 2401 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); | 
|  | 2402 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2403 | std::set<userid_t> newAllowedUsers; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2404 | for (size_t i = 0; i < newUserIds.size(); i++) { | 
|  | 2405 | if (newUserIds[i] < 0) { | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2406 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2407 | __FUNCTION__, newUserIds[i]); | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2408 | return; | 
|  | 2409 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2410 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2411 | } | 
|  | 2412 |  | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2413 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2414 | if (newAllowedUsers == mAllowedUsers) { | 
|  | 2415 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); | 
|  | 2416 | return; | 
|  | 2417 | } | 
|  | 2418 |  | 
|  | 2419 | logUserSwitch(mAllowedUsers, newAllowedUsers); | 
|  | 2420 |  | 
|  | 2421 | mAllowedUsers = std::move(newAllowedUsers); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2422 |  | 
|  | 2423 | // Current user has switched, evict all current clients. | 
|  | 2424 | std::vector<sp<BasicClient>> evicted; | 
|  | 2425 | for (auto& i : mActiveClientManager.getAll()) { | 
|  | 2426 | auto clientSp = i->getValue(); | 
|  | 2427 |  | 
|  | 2428 | if (clientSp.get() == nullptr) { | 
|  | 2429 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); | 
|  | 2430 | continue; | 
|  | 2431 | } | 
|  | 2432 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2433 | // Don't evict clients that are still allowed. | 
|  | 2434 | uid_t clientUid = clientSp->getClientUid(); | 
|  | 2435 | userid_t clientUserId = multiuser_get_user_id(clientUid); | 
|  | 2436 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { | 
|  | 2437 | continue; | 
|  | 2438 | } | 
|  | 2439 |  | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2440 | evicted.push_back(clientSp); | 
|  | 2441 |  | 
|  | 2442 | String8 curTime = getFormattedCurrentTime(); | 
|  | 2443 |  | 
|  | 2444 | ALOGE("Evicting conflicting client for camera ID %s due to user change", | 
|  | 2445 | i->getKey().string()); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2446 |  | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2447 | // Log the clients evicted | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2448 | logEvent(String8::format("EVICT device %s client held by package %s (PID %" | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2449 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n   - Evicted due" | 
|  | 2450 | " to user switch.", i->getKey().string(), | 
|  | 2451 | String8{clientSp->getPackageName()}.string(), | 
|  | 2452 | i->getOwnerId(), i->getPriority().getScore(), | 
|  | 2453 | i->getPriority().getState())); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2454 |  | 
|  | 2455 | } | 
|  | 2456 |  | 
|  | 2457 | // Do not hold mServiceLock while disconnecting clients, but retain the condition | 
|  | 2458 | // blocking other clients from connecting in mServiceLockWrapper if held. | 
|  | 2459 | mServiceLock.unlock(); | 
|  | 2460 |  | 
|  | 2461 | // Clear caller identity temporarily so client disconnect PID checks work correctly | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2462 | int64_t token = CameraThreadState::clearCallingIdentity(); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2463 |  | 
|  | 2464 | for (auto& i : evicted) { | 
|  | 2465 | i->disconnect(); | 
|  | 2466 | } | 
|  | 2467 |  | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2468 | CameraThreadState::restoreCallingIdentity(token); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2469 |  | 
|  | 2470 | // Reacquire mServiceLock | 
|  | 2471 | mServiceLock.lock(); | 
|  | 2472 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2473 |  | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2474 | void CameraService::logEvent(const char* event) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2475 | String8 curTime = getFormattedCurrentTime(); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2476 | Mutex::Autolock l(mLogLock); | 
|  | 2477 | mEventLog.add(String8::format("%s : %s", curTime.string(), event)); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2478 | } | 
|  | 2479 |  | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2480 | void CameraService::logDisconnected(const char* cameraId, int clientPid, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2481 | const char* clientPackage) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2482 | // Log the clients evicted | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2483 | 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] | 2484 | clientPackage, clientPid)); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2485 | } | 
|  | 2486 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2487 | void CameraService::logDisconnectedOffline(const char* cameraId, int clientPid, | 
|  | 2488 | const char* clientPackage) { | 
|  | 2489 | // Log the clients evicted | 
|  | 2490 | logEvent(String8::format("DISCONNECT offline device %s client for package %s (PID %d)", | 
|  | 2491 | cameraId, clientPackage, clientPid)); | 
|  | 2492 | } | 
|  | 2493 |  | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2494 | void CameraService::logConnected(const char* cameraId, int clientPid, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2495 | const char* clientPackage) { | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2496 | // Log the clients evicted | 
|  | 2497 | 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] | 2498 | clientPackage, clientPid)); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2499 | } | 
|  | 2500 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2501 | void CameraService::logConnectedOffline(const char* cameraId, int clientPid, | 
|  | 2502 | const char* clientPackage) { | 
|  | 2503 | // Log the clients evicted | 
|  | 2504 | logEvent(String8::format("CONNECT offline device %s client for package %s (PID %d)", cameraId, | 
|  | 2505 | clientPackage, clientPid)); | 
|  | 2506 | } | 
|  | 2507 |  | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2508 | void CameraService::logRejected(const char* cameraId, int clientPid, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2509 | const char* clientPackage, const char* reason) { | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2510 | // Log the client rejected | 
|  | 2511 | 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] | 2512 | cameraId, clientPackage, clientPid, reason)); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2513 | } | 
|  | 2514 |  | 
| Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 2515 | void CameraService::logTorchEvent(const char* cameraId, const char *torchState, int clientPid) { | 
|  | 2516 | // Log torch event | 
|  | 2517 | logEvent(String8::format("Torch for camera id %s turned %s for client PID %d", cameraId, | 
|  | 2518 | torchState, clientPid)); | 
|  | 2519 | } | 
|  | 2520 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2521 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, | 
|  | 2522 | const std::set<userid_t>& newUserIds) { | 
|  | 2523 | String8 newUsers = toString(newUserIds); | 
|  | 2524 | String8 oldUsers = toString(oldUserIds); | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 2525 | if (oldUsers.size() == 0) { | 
|  | 2526 | oldUsers = "<None>"; | 
|  | 2527 | } | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2528 | // Log the new and old users | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 2529 | 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] | 2530 | oldUsers.string(), newUsers.string())); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2531 | } | 
|  | 2532 |  | 
|  | 2533 | void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) { | 
|  | 2534 | // Log the device removal | 
|  | 2535 | logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason)); | 
|  | 2536 | } | 
|  | 2537 |  | 
|  | 2538 | void CameraService::logDeviceAdded(const char* cameraId, const char* reason) { | 
|  | 2539 | // Log the device removal | 
|  | 2540 | logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason)); | 
|  | 2541 | } | 
|  | 2542 |  | 
|  | 2543 | void CameraService::logClientDied(int clientPid, const char* reason) { | 
|  | 2544 | // Log the device removal | 
|  | 2545 | 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] | 2546 | } | 
|  | 2547 |  | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 2548 | void CameraService::logServiceError(const char* msg, int errorCode) { | 
|  | 2549 | String8 curTime = getFormattedCurrentTime(); | 
| Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 2550 | 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] | 2551 | } | 
|  | 2552 |  | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2553 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, | 
|  | 2554 | uint32_t flags) { | 
|  | 2555 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2556 | // Permission checks | 
|  | 2557 | switch (code) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 2558 | case SHELL_COMMAND_TRANSACTION: { | 
|  | 2559 | int in = data.readFileDescriptor(); | 
|  | 2560 | int out = data.readFileDescriptor(); | 
|  | 2561 | int err = data.readFileDescriptor(); | 
|  | 2562 | int argc = data.readInt32(); | 
|  | 2563 | Vector<String16> args; | 
|  | 2564 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { | 
|  | 2565 | args.add(data.readString16()); | 
|  | 2566 | } | 
|  | 2567 | sp<IBinder> unusedCallback; | 
|  | 2568 | sp<IResultReceiver> resultReceiver; | 
|  | 2569 | status_t status; | 
|  | 2570 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { | 
|  | 2571 | return status; | 
|  | 2572 | } | 
|  | 2573 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { | 
|  | 2574 | return status; | 
|  | 2575 | } | 
|  | 2576 | status = shellCommand(in, out, err, args); | 
|  | 2577 | if (resultReceiver != nullptr) { | 
|  | 2578 | resultReceiver->send(status); | 
|  | 2579 | } | 
|  | 2580 | return NO_ERROR; | 
|  | 2581 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2582 | } | 
|  | 2583 |  | 
|  | 2584 | return BnCameraService::onTransact(code, data, reply, flags); | 
|  | 2585 | } | 
|  | 2586 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2587 | // We share the media players for shutter and recording sound for all clients. | 
|  | 2588 | // A reference count is kept to determine when we will actually release the | 
|  | 2589 | // media players. | 
|  | 2590 |  | 
| Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 2591 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { | 
|  | 2592 | sp<MediaPlayer> mp = new MediaPlayer(); | 
|  | 2593 | status_t error; | 
|  | 2594 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { | 
| Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 2595 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); | 
| Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 2596 | error = mp->prepare(); | 
|  | 2597 | } | 
|  | 2598 | if (error != NO_ERROR) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2599 | ALOGE("Failed to load CameraService sounds: %s", file); | 
| Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 2600 | mp->disconnect(); | 
|  | 2601 | mp.clear(); | 
| Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 2602 | return nullptr; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2603 | } | 
|  | 2604 | return mp; | 
|  | 2605 | } | 
|  | 2606 |  | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2607 | void CameraService::increaseSoundRef() { | 
|  | 2608 | Mutex::Autolock lock(mSoundLock); | 
|  | 2609 | mSoundRef++; | 
|  | 2610 | } | 
|  | 2611 |  | 
|  | 2612 | void CameraService::loadSoundLocked(sound_kind kind) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2613 | ATRACE_CALL(); | 
|  | 2614 |  | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2615 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); | 
|  | 2616 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { | 
|  | 2617 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); | 
|  | 2618 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { | 
|  | 2619 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); | 
|  | 2620 | } | 
|  | 2621 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] ==  NULL) { | 
|  | 2622 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); | 
|  | 2623 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { | 
|  | 2624 | mSoundPlayer[SOUND_RECORDING_START] = | 
| Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 2625 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2626 | } | 
|  | 2627 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { | 
|  | 2628 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); | 
|  | 2629 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { | 
|  | 2630 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); | 
|  | 2631 | } | 
| Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 2632 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2633 | } | 
|  | 2634 |  | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2635 | void CameraService::decreaseSoundRef() { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2636 | Mutex::Autolock lock(mSoundLock); | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2637 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2638 | if (--mSoundRef) return; | 
|  | 2639 |  | 
|  | 2640 | for (int i = 0; i < NUM_SOUNDS; i++) { | 
|  | 2641 | if (mSoundPlayer[i] != 0) { | 
|  | 2642 | mSoundPlayer[i]->disconnect(); | 
|  | 2643 | mSoundPlayer[i].clear(); | 
|  | 2644 | } | 
|  | 2645 | } | 
|  | 2646 | } | 
|  | 2647 |  | 
|  | 2648 | void CameraService::playSound(sound_kind kind) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2649 | ATRACE_CALL(); | 
|  | 2650 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2651 | LOG1("playSound(%d)", kind); | 
|  | 2652 | Mutex::Autolock lock(mSoundLock); | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2653 | loadSoundLocked(kind); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2654 | sp<MediaPlayer> player = mSoundPlayer[kind]; | 
|  | 2655 | if (player != 0) { | 
| Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 2656 | player->seekTo(0); | 
|  | 2657 | player->start(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2658 | } | 
|  | 2659 | } | 
|  | 2660 |  | 
|  | 2661 | // ---------------------------------------------------------------------------- | 
|  | 2662 |  | 
|  | 2663 | CameraService::Client::Client(const sp<CameraService>& cameraService, | 
| Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 2664 | const sp<ICameraClient>& cameraClient, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2665 | const String16& clientPackageName, | 
| Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 2666 | const std::optional<String16>& clientFeatureId, | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2667 | const String8& cameraIdStr, | 
|  | 2668 | int api1CameraId, int cameraFacing, | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2669 | int clientPid, uid_t clientUid, | 
|  | 2670 | int servicePid) : | 
| Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 2671 | CameraService::BasicClient(cameraService, | 
| Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 2672 | IInterface::asBinder(cameraClient), | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 2673 | clientPackageName, clientFeatureId, | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2674 | cameraIdStr, cameraFacing, | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2675 | clientPid, clientUid, | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2676 | servicePid), | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2677 | mCameraId(api1CameraId) | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2678 | { | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2679 | int callingPid = CameraThreadState::getCallingPid(); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2680 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2681 |  | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 2682 | mRemoteCallback = cameraClient; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2683 |  | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2684 | cameraService->increaseSoundRef(); | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2685 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2686 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2687 | } | 
|  | 2688 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2689 | // tear down the client | 
|  | 2690 | CameraService::Client::~Client() { | 
| Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 2691 | ALOGV("~Client"); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2692 | mDestructionStarted = true; | 
|  | 2693 |  | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2694 | sCameraService->decreaseSoundRef(); | 
| Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 2695 | // unconditionally disconnect. function is idempotent | 
|  | 2696 | Client::disconnect(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2697 | } | 
|  | 2698 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2699 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; | 
|  | 2700 |  | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2701 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2702 | const sp<IBinder>& remoteCallback, | 
| Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 2703 | const String16& clientPackageName, const std::optional<String16>& clientFeatureId, | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2704 | const String8& cameraIdStr, int cameraFacing, | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2705 | int clientPid, uid_t clientUid, | 
|  | 2706 | int servicePid): | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2707 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), | 
| Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 2708 | mClientPackageName(clientPackageName), mClientFeatureId(clientFeatureId), | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 2709 | mClientPid(clientPid), mClientUid(clientUid), | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2710 | mServicePid(servicePid), | 
| Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 2711 | mDisconnected(false), mUidIsTrusted(false), | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2712 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2713 | mRemoteBinder(remoteCallback) | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2714 | { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2715 | if (sCameraService == nullptr) { | 
|  | 2716 | sCameraService = cameraService; | 
|  | 2717 | } | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2718 | mOpsActive = false; | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2719 | mDestructionStarted = false; | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 2720 |  | 
|  | 2721 | // In some cases the calling code has no access to the package it runs under. | 
|  | 2722 | // For example, NDK camera API. | 
|  | 2723 | // In this case we will get the packages for the calling UID and pick the first one | 
|  | 2724 | // for attributing the app op. This will work correctly for runtime permissions | 
|  | 2725 | // as for legacy apps we will toggle the app op for all packages in the UID. | 
|  | 2726 | // The caveat is that the operation may be attributed to the wrong package and | 
|  | 2727 | // stats based on app ops may be slightly off. | 
|  | 2728 | if (mClientPackageName.size() <= 0) { | 
|  | 2729 | sp<IServiceManager> sm = defaultServiceManager(); | 
|  | 2730 | sp<IBinder> binder = sm->getService(String16(kPermissionServiceName)); | 
|  | 2731 | if (binder == 0) { | 
|  | 2732 | ALOGE("Cannot get permission service"); | 
|  | 2733 | // Leave mClientPackageName unchanged (empty) and the further interaction | 
|  | 2734 | // with camera will fail in BasicClient::startCameraOps | 
|  | 2735 | return; | 
|  | 2736 | } | 
|  | 2737 |  | 
|  | 2738 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); | 
|  | 2739 | Vector<String16> packages; | 
|  | 2740 |  | 
|  | 2741 | permCtrl->getPackagesForUid(mClientUid, packages); | 
|  | 2742 |  | 
|  | 2743 | if (packages.isEmpty()) { | 
|  | 2744 | ALOGE("No packages for calling UID"); | 
|  | 2745 | // Leave mClientPackageName unchanged (empty) and the further interaction | 
|  | 2746 | // with camera will fail in BasicClient::startCameraOps | 
|  | 2747 | return; | 
|  | 2748 | } | 
|  | 2749 | mClientPackageName = packages[0]; | 
|  | 2750 | } | 
| Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 2751 | if (getCurrentServingCall() != BinderCallType::HWBINDER) { | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2752 | mAppOpsManager = std::make_unique<AppOpsManager>(); | 
|  | 2753 | } | 
| Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 2754 |  | 
|  | 2755 | mUidIsTrusted = isTrustedCallingUid(mClientUid); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2756 | } | 
|  | 2757 |  | 
|  | 2758 | CameraService::BasicClient::~BasicClient() { | 
| Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 2759 | ALOGV("~BasicClient"); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2760 | mDestructionStarted = true; | 
|  | 2761 | } | 
|  | 2762 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2763 | binder::Status CameraService::BasicClient::disconnect() { | 
|  | 2764 | binder::Status res = Status::ok(); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2765 | if (mDisconnected) { | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2766 | return res; | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2767 | } | 
| Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 2768 | mDisconnected = true; | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2769 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2770 | sCameraService->removeByClient(this); | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2771 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, String8(mClientPackageName)); | 
| Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 2772 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, | 
|  | 2773 | mCameraIdStr.c_str()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2774 |  | 
|  | 2775 | sp<IBinder> remote = getRemote(); | 
|  | 2776 | if (remote != nullptr) { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2777 | remote->unlinkToDeath(sCameraService); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2778 | } | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2779 |  | 
|  | 2780 | finishCameraOps(); | 
| Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 2781 | // Notify flashlight that a camera device is closed. | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2782 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); | 
|  | 2783 | ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(), | 
|  | 2784 | mClientPid); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2785 |  | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2786 | // client shouldn't be able to call into us anymore | 
|  | 2787 | mClientPid = 0; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2788 |  | 
|  | 2789 | return res; | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2790 | } | 
|  | 2791 |  | 
| Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 2792 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { | 
|  | 2793 | // No dumping of clients directly over Binder, | 
|  | 2794 | // must go through CameraService::dump | 
|  | 2795 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2796 | CameraThreadState::getCallingUid(), NULL, 0); | 
| Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 2797 | return OK; | 
|  | 2798 | } | 
|  | 2799 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2800 | String16 CameraService::BasicClient::getPackageName() const { | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2801 | return mClientPackageName; | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2802 | } | 
|  | 2803 |  | 
|  | 2804 |  | 
|  | 2805 | int CameraService::BasicClient::getClientPid() const { | 
|  | 2806 | return mClientPid; | 
|  | 2807 | } | 
|  | 2808 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2809 | uid_t CameraService::BasicClient::getClientUid() const { | 
|  | 2810 | return mClientUid; | 
|  | 2811 | } | 
|  | 2812 |  | 
| Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2813 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { | 
|  | 2814 | // Defaults to API2. | 
|  | 2815 | return level == API_2; | 
|  | 2816 | } | 
|  | 2817 |  | 
| Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 2818 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2819 | { | 
|  | 2820 | Mutex::Autolock l(mAudioRestrictionLock); | 
|  | 2821 | mAudioRestriction = mode; | 
|  | 2822 | } | 
| Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 2823 | sCameraService->updateAudioRestriction(); | 
|  | 2824 | return OK; | 
|  | 2825 | } | 
|  | 2826 |  | 
|  | 2827 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2828 | return sCameraService->updateAudioRestriction(); | 
|  | 2829 | } | 
|  | 2830 |  | 
|  | 2831 | int32_t CameraService::BasicClient::getAudioRestriction() const { | 
|  | 2832 | Mutex::Autolock l(mAudioRestrictionLock); | 
|  | 2833 | return mAudioRestriction; | 
|  | 2834 | } | 
|  | 2835 |  | 
|  | 2836 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { | 
|  | 2837 | switch (mode) { | 
|  | 2838 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: | 
|  | 2839 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: | 
|  | 2840 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: | 
|  | 2841 | return true; | 
|  | 2842 | default: | 
|  | 2843 | return false; | 
|  | 2844 | } | 
|  | 2845 | } | 
|  | 2846 |  | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2847 | status_t CameraService::BasicClient::startCameraOps() { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2848 | ATRACE_CALL(); | 
|  | 2849 |  | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2850 | { | 
|  | 2851 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2852 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2853 | } | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2854 | if (mAppOpsManager != nullptr) { | 
|  | 2855 | // Notify app ops that the camera is not available | 
|  | 2856 | mOpsCallback = new OpsCallback(this); | 
|  | 2857 | int32_t res; | 
|  | 2858 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, | 
|  | 2859 | mClientPackageName, mOpsCallback); | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 2860 | res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, | 
|  | 2861 | mClientPackageName, /*startIfModeDefault*/ false, mClientFeatureId, | 
|  | 2862 | String16("start camera ") + String16(mCameraIdStr)); | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2863 |  | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2864 | if (res == AppOpsManager::MODE_ERRORED) { | 
|  | 2865 | ALOGI("Camera %s: Access for \"%s\" has been revoked", | 
|  | 2866 | mCameraIdStr.string(), String8(mClientPackageName).string()); | 
|  | 2867 | return PERMISSION_DENIED; | 
|  | 2868 | } | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2869 |  | 
| Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 2870 | // If the calling Uid is trusted (a native service), the AppOpsManager could | 
|  | 2871 | // return MODE_IGNORED. Do not treat such case as error. | 
|  | 2872 | if (!mUidIsTrusted && res == AppOpsManager::MODE_IGNORED) { | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2873 | ALOGI("Camera %s: Access for \"%s\" has been restricted", | 
|  | 2874 | mCameraIdStr.string(), String8(mClientPackageName).string()); | 
|  | 2875 | // Return the same error as for device policy manager rejection | 
|  | 2876 | return -EACCES; | 
|  | 2877 | } | 
| Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 2878 | } | 
|  | 2879 |  | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2880 | mOpsActive = true; | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2881 |  | 
|  | 2882 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2883 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2884 |  | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2885 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid); | 
|  | 2886 |  | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2887 | // Notify listeners of camera open/close status | 
|  | 2888 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); | 
|  | 2889 |  | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2890 | return OK; | 
|  | 2891 | } | 
|  | 2892 |  | 
|  | 2893 | status_t CameraService::BasicClient::finishCameraOps() { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2894 | ATRACE_CALL(); | 
|  | 2895 |  | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2896 | // Check if startCameraOps succeeded, and if so, finish the camera op | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2897 | if (mOpsActive) { | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2898 | // Notify app ops that the camera is available again | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2899 | if (mAppOpsManager != nullptr) { | 
|  | 2900 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 2901 | mClientPackageName, mClientFeatureId); | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2902 | mOpsActive = false; | 
|  | 2903 | } | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 2904 | // This function is called when a client disconnects. This should | 
|  | 2905 | // release the camera, but actually only if it was in a proper | 
|  | 2906 | // functional state, i.e. with status NOT_AVAILABLE | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2907 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 2908 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2909 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2910 | // 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] | 2911 | sCameraService->updateStatus(StatusInternal::PRESENT, | 
|  | 2912 | mCameraIdStr, rejected); | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2913 | } | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 2914 | // Always stop watching, even if no camera op is active | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2915 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { | 
|  | 2916 | mAppOpsManager->stopWatchingMode(mOpsCallback); | 
| Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 2917 | } | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2918 | mOpsCallback.clear(); | 
|  | 2919 |  | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2920 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid); | 
|  | 2921 |  | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2922 | // Notify listeners of camera open/close status | 
|  | 2923 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); | 
|  | 2924 |  | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2925 | return OK; | 
|  | 2926 | } | 
|  | 2927 |  | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2928 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2929 | ATRACE_CALL(); | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2930 | if (mAppOpsManager == nullptr) { | 
|  | 2931 | return; | 
|  | 2932 | } | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2933 | // TODO : add offline camera session case | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2934 | if (op != AppOpsManager::OP_CAMERA) { | 
|  | 2935 | ALOGW("Unexpected app ops notification received: %d", op); | 
|  | 2936 | return; | 
|  | 2937 | } | 
|  | 2938 |  | 
|  | 2939 | int32_t res; | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2940 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2941 | mClientUid, mClientPackageName); | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2942 | ALOGV("checkOp returns: %d, %s ", res, | 
|  | 2943 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : | 
|  | 2944 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : | 
|  | 2945 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : | 
|  | 2946 | "UNKNOWN"); | 
|  | 2947 |  | 
| Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 2948 | if (res == AppOpsManager::MODE_ERRORED) { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2949 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(), | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 2950 | String8(mClientPackageName).string()); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 2951 | block(); | 
| Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 2952 | } else if (res == AppOpsManager::MODE_IGNORED) { | 
|  | 2953 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); | 
|  | 2954 | ALOGI("Camera %s: Access for \"%s\" has been restricted, isUidTrusted %d, isUidActive %d", | 
|  | 2955 | mCameraIdStr.string(), String8(mClientPackageName).string(), | 
|  | 2956 | mUidIsTrusted, isUidActive); | 
|  | 2957 | // If the calling Uid is trusted (a native service), or the client Uid is active (WAR for | 
|  | 2958 | // b/175320666), the AppOpsManager could return MODE_IGNORED. Do not treat such cases as | 
|  | 2959 | // error. | 
|  | 2960 | if (!mUidIsTrusted && !isUidActive) { | 
|  | 2961 | block(); | 
|  | 2962 | } | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2963 | } | 
|  | 2964 | } | 
|  | 2965 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 2966 | void CameraService::BasicClient::block() { | 
|  | 2967 | ATRACE_CALL(); | 
|  | 2968 |  | 
|  | 2969 | // Reset the client PID to allow server-initiated disconnect, | 
|  | 2970 | // and to prevent further calls by client. | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2971 | mClientPid = CameraThreadState::getCallingPid(); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 2972 | CaptureResultExtras resultExtras; // a dummy result (invalid) | 
|  | 2973 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); | 
|  | 2974 | disconnect(); | 
|  | 2975 | } | 
|  | 2976 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2977 | // ---------------------------------------------------------------------------- | 
|  | 2978 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2979 | void CameraService::Client::notifyError(int32_t errorCode, | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2980 | const CaptureResultExtras& resultExtras) { | 
| Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 2981 | (void) resultExtras; | 
| Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 2982 | if (mRemoteCallback != NULL) { | 
| Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 2983 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; | 
|  | 2984 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { | 
|  | 2985 | api1ErrorCode = CAMERA_ERROR_DISABLED; | 
|  | 2986 | } | 
|  | 2987 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); | 
| Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 2988 | } else { | 
|  | 2989 | ALOGE("mRemoteCallback is NULL!!"); | 
|  | 2990 | } | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2991 | } | 
|  | 2992 |  | 
| Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 2993 | // NOTE: function is idempotent | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2994 | binder::Status CameraService::Client::disconnect() { | 
| Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 2995 | ALOGV("Client::disconnect"); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2996 | return BasicClient::disconnect(); | 
| Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 2997 | } | 
|  | 2998 |  | 
| Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2999 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { | 
|  | 3000 | return level == API_1; | 
|  | 3001 | } | 
|  | 3002 |  | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3003 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): | 
|  | 3004 | mClient(client) { | 
|  | 3005 | } | 
|  | 3006 |  | 
|  | 3007 | void CameraService::Client::OpsCallback::opChanged(int32_t op, | 
|  | 3008 | const String16& packageName) { | 
|  | 3009 | sp<BasicClient> client = mClient.promote(); | 
|  | 3010 | if (client != NULL) { | 
|  | 3011 | client->opChanged(op, packageName); | 
|  | 3012 | } | 
|  | 3013 | } | 
|  | 3014 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3015 | // ---------------------------------------------------------------------------- | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3016 | //                  UidPolicy | 
|  | 3017 | // ---------------------------------------------------------------------------- | 
|  | 3018 |  | 
|  | 3019 | void CameraService::UidPolicy::registerSelf() { | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3020 | Mutex::Autolock _l(mUidLock); | 
|  | 3021 |  | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3022 | if (mRegistered) return; | 
| Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3023 | status_t res = mAm.linkToDeath(this); | 
|  | 3024 | mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3025 | | ActivityManager::UID_OBSERVER_IDLE | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3026 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE, | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3027 | ActivityManager::PROCESS_STATE_UNKNOWN, | 
|  | 3028 | String16("cameraserver")); | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3029 | if (res == OK) { | 
|  | 3030 | mRegistered = true; | 
|  | 3031 | ALOGV("UidPolicy: Registered with ActivityManager"); | 
|  | 3032 | } | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3033 | } | 
|  | 3034 |  | 
|  | 3035 | void CameraService::UidPolicy::unregisterSelf() { | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3036 | Mutex::Autolock _l(mUidLock); | 
|  | 3037 |  | 
| Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3038 | mAm.unregisterUidObserver(this); | 
|  | 3039 | mAm.unlinkToDeath(this); | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3040 | mRegistered = false; | 
|  | 3041 | mActiveUids.clear(); | 
|  | 3042 | ALOGV("UidPolicy: Unregistered with ActivityManager"); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3043 | } | 
|  | 3044 |  | 
|  | 3045 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { | 
|  | 3046 | onUidIdle(uid, disabled); | 
|  | 3047 | } | 
|  | 3048 |  | 
|  | 3049 | void CameraService::UidPolicy::onUidActive(uid_t uid) { | 
|  | 3050 | Mutex::Autolock _l(mUidLock); | 
|  | 3051 | mActiveUids.insert(uid); | 
|  | 3052 | } | 
|  | 3053 |  | 
|  | 3054 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { | 
|  | 3055 | bool deleted = false; | 
|  | 3056 | { | 
|  | 3057 | Mutex::Autolock _l(mUidLock); | 
|  | 3058 | if (mActiveUids.erase(uid) > 0) { | 
|  | 3059 | deleted = true; | 
|  | 3060 | } | 
|  | 3061 | } | 
|  | 3062 | if (deleted) { | 
|  | 3063 | sp<CameraService> service = mService.promote(); | 
|  | 3064 | if (service != nullptr) { | 
|  | 3065 | service->blockClientsForUid(uid); | 
|  | 3066 | } | 
|  | 3067 | } | 
|  | 3068 | } | 
|  | 3069 |  | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3070 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, | 
| Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 3071 | int64_t procStateSeq __unused, int32_t capability __unused) { | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3072 | bool procStateChange = false; | 
|  | 3073 | { | 
|  | 3074 | Mutex::Autolock _l(mUidLock); | 
|  | 3075 | if ((mMonitoredUids.find(uid) != mMonitoredUids.end()) && | 
|  | 3076 | (mMonitoredUids[uid].first != procState)) { | 
|  | 3077 | mMonitoredUids[uid].first = procState; | 
|  | 3078 | procStateChange = true; | 
|  | 3079 | } | 
|  | 3080 | } | 
|  | 3081 |  | 
|  | 3082 | if (procStateChange) { | 
|  | 3083 | sp<CameraService> service = mService.promote(); | 
|  | 3084 | if (service != nullptr) { | 
|  | 3085 | service->notifyMonitoredUids(); | 
|  | 3086 | } | 
|  | 3087 | } | 
|  | 3088 | } | 
|  | 3089 |  | 
|  | 3090 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid) { | 
|  | 3091 | Mutex::Autolock _l(mUidLock); | 
|  | 3092 | auto it = mMonitoredUids.find(uid); | 
|  | 3093 | if (it != mMonitoredUids.end()) { | 
|  | 3094 | it->second.second++; | 
|  | 3095 | } else { | 
|  | 3096 | mMonitoredUids.emplace( | 
|  | 3097 | std::pair<uid_t, std::pair<int32_t, size_t>> (uid, | 
|  | 3098 | std::pair<int32_t, size_t> (ActivityManager::PROCESS_STATE_NONEXISTENT, 1))); | 
|  | 3099 | } | 
|  | 3100 | } | 
|  | 3101 |  | 
|  | 3102 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid) { | 
|  | 3103 | Mutex::Autolock _l(mUidLock); | 
|  | 3104 | auto it = mMonitoredUids.find(uid); | 
|  | 3105 | if (it != mMonitoredUids.end()) { | 
|  | 3106 | it->second.second--; | 
|  | 3107 | if (it->second.second == 0) { | 
|  | 3108 | mMonitoredUids.erase(it); | 
|  | 3109 | } | 
|  | 3110 | } else { | 
|  | 3111 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); | 
|  | 3112 | } | 
|  | 3113 | } | 
|  | 3114 |  | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3115 | bool CameraService::UidPolicy::isUidActive(uid_t uid, String16 callingPackage) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3116 | Mutex::Autolock _l(mUidLock); | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3117 | return isUidActiveLocked(uid, callingPackage); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3118 | } | 
|  | 3119 |  | 
| Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3120 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; | 
|  | 3121 | static const int64_t kPollUidActiveTimeoutMillis = 50; | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3122 |  | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3123 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, String16 callingPackage) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3124 | // Non-app UIDs are considered always active | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3125 | // If activity manager is unreachable, assume everything is active | 
|  | 3126 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3127 | return true; | 
|  | 3128 | } | 
|  | 3129 | auto it = mOverrideUids.find(uid); | 
|  | 3130 | if (it != mOverrideUids.end()) { | 
|  | 3131 | return it->second; | 
|  | 3132 | } | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3133 | bool active = mActiveUids.find(uid) != mActiveUids.end(); | 
|  | 3134 | if (!active) { | 
|  | 3135 | // We want active UIDs to always access camera with their first attempt since | 
|  | 3136 | // there is no guarantee the app is robustly written and would retry getting | 
|  | 3137 | // the camera on failure. The inverse case is not a problem as we would take | 
|  | 3138 | // camera away soon once we get the callback that the uid is no longer active. | 
|  | 3139 | ActivityManager am; | 
|  | 3140 | // Okay to access with a lock held as UID changes are dispatched without | 
|  | 3141 | // a lock and we are a higher level component. | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3142 | int64_t startTimeMillis = 0; | 
|  | 3143 | do { | 
|  | 3144 | // TODO: Fix this b/109950150! | 
|  | 3145 | // Okay this is a hack. There is a race between the UID turning active and | 
|  | 3146 | // activity being resumed. The proper fix is very risky, so we temporary add | 
|  | 3147 | // some polling which should happen pretty rarely anyway as the race is hard | 
|  | 3148 | // to hit. | 
| Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3149 | active = mActiveUids.find(uid) != mActiveUids.end(); | 
| Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 3150 | if (!active) active = am.isUidActive(uid, callingPackage); | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3151 | if (active) { | 
|  | 3152 | break; | 
|  | 3153 | } | 
|  | 3154 | if (startTimeMillis <= 0) { | 
|  | 3155 | startTimeMillis = uptimeMillis(); | 
|  | 3156 | } | 
|  | 3157 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; | 
| Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3158 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3159 | if (remainingTimeMillis <= 0) { | 
|  | 3160 | break; | 
|  | 3161 | } | 
| Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3162 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); | 
|  | 3163 |  | 
|  | 3164 | mUidLock.unlock(); | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3165 | usleep(remainingTimeMillis * 1000); | 
| Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3166 | mUidLock.lock(); | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3167 | } while (true); | 
|  | 3168 |  | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3169 | if (active) { | 
|  | 3170 | // Now that we found out the UID is actually active, cache that | 
|  | 3171 | mActiveUids.insert(uid); | 
|  | 3172 | } | 
|  | 3173 | } | 
|  | 3174 | return active; | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3175 | } | 
|  | 3176 |  | 
| Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 3177 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { | 
|  | 3178 | Mutex::Autolock _l(mUidLock); | 
|  | 3179 | return getProcStateLocked(uid); | 
|  | 3180 | } | 
|  | 3181 |  | 
|  | 3182 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { | 
|  | 3183 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; | 
|  | 3184 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { | 
|  | 3185 | procState = mMonitoredUids[uid].first; | 
|  | 3186 | } | 
|  | 3187 | return procState; | 
|  | 3188 | } | 
|  | 3189 |  | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3190 | void CameraService::UidPolicy::UidPolicy::addOverrideUid(uid_t uid, | 
|  | 3191 | String16 callingPackage, bool active) { | 
|  | 3192 | updateOverrideUid(uid, callingPackage, active, true); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3193 | } | 
|  | 3194 |  | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3195 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, String16 callingPackage) { | 
|  | 3196 | updateOverrideUid(uid, callingPackage, false, false); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3197 | } | 
|  | 3198 |  | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3199 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { | 
|  | 3200 | Mutex::Autolock _l(mUidLock); | 
|  | 3201 | ALOGV("UidPolicy: ActivityManager has died"); | 
|  | 3202 | mRegistered = false; | 
|  | 3203 | mActiveUids.clear(); | 
|  | 3204 | } | 
|  | 3205 |  | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3206 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, String16 callingPackage, | 
|  | 3207 | bool active, bool insert) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3208 | bool wasActive = false; | 
|  | 3209 | bool isActive = false; | 
|  | 3210 | { | 
|  | 3211 | Mutex::Autolock _l(mUidLock); | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3212 | wasActive = isUidActiveLocked(uid, callingPackage); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3213 | mOverrideUids.erase(uid); | 
|  | 3214 | if (insert) { | 
|  | 3215 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); | 
|  | 3216 | } | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3217 | isActive = isUidActiveLocked(uid, callingPackage); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3218 | } | 
|  | 3219 | if (wasActive != isActive && !isActive) { | 
|  | 3220 | sp<CameraService> service = mService.promote(); | 
|  | 3221 | if (service != nullptr) { | 
|  | 3222 | service->blockClientsForUid(uid); | 
|  | 3223 | } | 
|  | 3224 | } | 
|  | 3225 | } | 
|  | 3226 |  | 
|  | 3227 | // ---------------------------------------------------------------------------- | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3228 | //                  SensorPrivacyPolicy | 
|  | 3229 | // ---------------------------------------------------------------------------- | 
|  | 3230 | void CameraService::SensorPrivacyPolicy::registerSelf() { | 
|  | 3231 | Mutex::Autolock _l(mSensorPrivacyLock); | 
|  | 3232 | if (mRegistered) { | 
|  | 3233 | return; | 
|  | 3234 | } | 
| Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 3235 | mSpm.addSensorPrivacyListener(this); | 
|  | 3236 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); | 
|  | 3237 | status_t res = mSpm.linkToDeath(this); | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3238 | if (res == OK) { | 
|  | 3239 | mRegistered = true; | 
|  | 3240 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); | 
|  | 3241 | } | 
|  | 3242 | } | 
|  | 3243 |  | 
| Evan Severson | 190ad25 | 2021-01-07 20:37:15 -0800 | [diff] [blame] | 3244 | status_t CameraService::SensorPrivacyPolicy::registerSelfForIndividual(int userId) { | 
|  | 3245 | Mutex::Autolock _l(mSensorPrivacyLock); | 
|  | 3246 | if (mRegistered) { | 
|  | 3247 | return OK; | 
|  | 3248 | } | 
|  | 3249 |  | 
|  | 3250 | status_t res = mSpm.addIndividualSensorPrivacyListener(userId, | 
|  | 3251 | SensorPrivacyManager::INDIVIDUAL_SENSOR_CAMERA, this); | 
|  | 3252 | if (res != OK) { | 
|  | 3253 | ALOGE("Unable to register camera privacy listener: %s (%d)", strerror(-res), res); | 
|  | 3254 | return res; | 
|  | 3255 | } | 
|  | 3256 |  | 
|  | 3257 | res = mSpm.isIndividualSensorPrivacyEnabled(userId, | 
|  | 3258 | SensorPrivacyManager::INDIVIDUAL_SENSOR_CAMERA, mSensorPrivacyEnabled); | 
|  | 3259 | if (res != OK) { | 
|  | 3260 | ALOGE("Unable to check camera privacy: %s (%d)", strerror(-res), res); | 
|  | 3261 | return res; | 
|  | 3262 | } | 
|  | 3263 |  | 
|  | 3264 | res = mSpm.linkToDeath(this); | 
|  | 3265 | if (res != OK) { | 
|  | 3266 | ALOGE("Register link to death failed for sensor privacy: %s (%d)", strerror(-res), res); | 
|  | 3267 | return res; | 
|  | 3268 | } | 
|  | 3269 |  | 
|  | 3270 | mRegistered = true; | 
| Evan Severson | dd51a01 | 2021-02-03 09:15:21 -0800 | [diff] [blame] | 3271 | mIsIndividual = true; | 
|  | 3272 | mUserId = userId; | 
| Evan Severson | 190ad25 | 2021-01-07 20:37:15 -0800 | [diff] [blame] | 3273 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); | 
|  | 3274 | return OK; | 
|  | 3275 | } | 
|  | 3276 |  | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3277 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { | 
|  | 3278 | Mutex::Autolock _l(mSensorPrivacyLock); | 
| Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 3279 | mSpm.removeSensorPrivacyListener(this); | 
|  | 3280 | mSpm.unlinkToDeath(this); | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3281 | mRegistered = false; | 
|  | 3282 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); | 
|  | 3283 | } | 
|  | 3284 |  | 
|  | 3285 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { | 
|  | 3286 | Mutex::Autolock _l(mSensorPrivacyLock); | 
|  | 3287 | return mSensorPrivacyEnabled; | 
|  | 3288 | } | 
|  | 3289 |  | 
|  | 3290 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) { | 
|  | 3291 | { | 
|  | 3292 | Mutex::Autolock _l(mSensorPrivacyLock); | 
|  | 3293 | mSensorPrivacyEnabled = enabled; | 
|  | 3294 | } | 
|  | 3295 | // if sensor privacy is enabled then block all clients from accessing the camera | 
| Evan Severson | 190ad25 | 2021-01-07 20:37:15 -0800 | [diff] [blame] | 3296 | sp<CameraService> service = mService.promote(); | 
|  | 3297 | if (service != nullptr) { | 
| Evan Severson | dd51a01 | 2021-02-03 09:15:21 -0800 | [diff] [blame] | 3298 | if (mIsIndividual) { | 
|  | 3299 | service->setMuteForAllClients(mUserId, enabled); | 
| Evan Severson | 190ad25 | 2021-01-07 20:37:15 -0800 | [diff] [blame] | 3300 | } else { | 
|  | 3301 | if (enabled) { | 
|  | 3302 | service->blockAllClients(); | 
|  | 3303 | } | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3304 | } | 
|  | 3305 | } | 
|  | 3306 | return binder::Status::ok(); | 
|  | 3307 | } | 
|  | 3308 |  | 
|  | 3309 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { | 
|  | 3310 | Mutex::Autolock _l(mSensorPrivacyLock); | 
|  | 3311 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); | 
|  | 3312 | mRegistered = false; | 
|  | 3313 | } | 
|  | 3314 |  | 
|  | 3315 | // ---------------------------------------------------------------------------- | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3316 | //                  CameraState | 
|  | 3317 | // ---------------------------------------------------------------------------- | 
|  | 3318 |  | 
|  | 3319 | CameraService::CameraState::CameraState(const String8& id, int cost, | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3320 | const std::set<String8>& conflicting, SystemCameraKind systemCameraKind) : mId(id), | 
|  | 3321 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), | 
|  | 3322 | mSystemCameraKind(systemCameraKind) {} | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3323 |  | 
|  | 3324 | CameraService::CameraState::~CameraState() {} | 
|  | 3325 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3326 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3327 | Mutex::Autolock lock(mStatusLock); | 
|  | 3328 | return mStatus; | 
|  | 3329 | } | 
|  | 3330 |  | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3331 | std::vector<String8> CameraService::CameraState::getUnavailablePhysicalIds() const { | 
|  | 3332 | Mutex::Autolock lock(mStatusLock); | 
|  | 3333 | std::vector<String8> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); | 
|  | 3334 | return res; | 
|  | 3335 | } | 
|  | 3336 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3337 | CameraParameters CameraService::CameraState::getShimParams() const { | 
|  | 3338 | return mShimParams; | 
|  | 3339 | } | 
|  | 3340 |  | 
|  | 3341 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { | 
|  | 3342 | mShimParams = params; | 
|  | 3343 | } | 
|  | 3344 |  | 
|  | 3345 | int CameraService::CameraState::getCost() const { | 
|  | 3346 | return mCost; | 
|  | 3347 | } | 
|  | 3348 |  | 
|  | 3349 | std::set<String8> CameraService::CameraState::getConflicting() const { | 
|  | 3350 | return mConflicting; | 
|  | 3351 | } | 
|  | 3352 |  | 
|  | 3353 | String8 CameraService::CameraState::getId() const { | 
|  | 3354 | return mId; | 
|  | 3355 | } | 
|  | 3356 |  | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3357 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { | 
|  | 3358 | return mSystemCameraKind; | 
|  | 3359 | } | 
|  | 3360 |  | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3361 | bool CameraService::CameraState::addUnavailablePhysicalId(const String8& physicalId) { | 
|  | 3362 | Mutex::Autolock lock(mStatusLock); | 
|  | 3363 | auto result = mUnavailablePhysicalIds.insert(physicalId); | 
|  | 3364 | return result.second; | 
|  | 3365 | } | 
|  | 3366 |  | 
|  | 3367 | bool CameraService::CameraState::removeUnavailablePhysicalId(const String8& physicalId) { | 
|  | 3368 | Mutex::Autolock lock(mStatusLock); | 
|  | 3369 | auto count = mUnavailablePhysicalIds.erase(physicalId); | 
|  | 3370 | return count > 0; | 
|  | 3371 | } | 
|  | 3372 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3373 | // ---------------------------------------------------------------------------- | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3374 | //                  ClientEventListener | 
|  | 3375 | // ---------------------------------------------------------------------------- | 
|  | 3376 |  | 
|  | 3377 | void CameraService::ClientEventListener::onClientAdded( | 
|  | 3378 | const resource_policy::ClientDescriptor<String8, | 
|  | 3379 | sp<CameraService::BasicClient>>& descriptor) { | 
| Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 3380 | const auto& basicClient = descriptor.getValue(); | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3381 | if (basicClient.get() != nullptr) { | 
|  | 3382 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); | 
|  | 3383 | notifier.noteStartCamera(descriptor.getKey(), | 
|  | 3384 | static_cast<int>(basicClient->getClientUid())); | 
|  | 3385 | } | 
|  | 3386 | } | 
|  | 3387 |  | 
|  | 3388 | void CameraService::ClientEventListener::onClientRemoved( | 
|  | 3389 | const resource_policy::ClientDescriptor<String8, | 
|  | 3390 | sp<CameraService::BasicClient>>& descriptor) { | 
| Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 3391 | const auto& basicClient = descriptor.getValue(); | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3392 | if (basicClient.get() != nullptr) { | 
|  | 3393 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); | 
|  | 3394 | notifier.noteStopCamera(descriptor.getKey(), | 
|  | 3395 | static_cast<int>(basicClient->getClientUid())); | 
|  | 3396 | } | 
|  | 3397 | } | 
|  | 3398 |  | 
|  | 3399 |  | 
|  | 3400 | // ---------------------------------------------------------------------------- | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3401 | //                  CameraClientManager | 
|  | 3402 | // ---------------------------------------------------------------------------- | 
|  | 3403 |  | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3404 | CameraService::CameraClientManager::CameraClientManager() { | 
|  | 3405 | setListener(std::make_shared<ClientEventListener>()); | 
|  | 3406 | } | 
|  | 3407 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3408 | CameraService::CameraClientManager::~CameraClientManager() {} | 
|  | 3409 |  | 
|  | 3410 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( | 
|  | 3411 | const String8& id) const { | 
|  | 3412 | auto descriptor = get(id); | 
|  | 3413 | if (descriptor == nullptr) { | 
|  | 3414 | return sp<BasicClient>{nullptr}; | 
|  | 3415 | } | 
|  | 3416 | return descriptor->getValue(); | 
|  | 3417 | } | 
|  | 3418 |  | 
|  | 3419 | String8 CameraService::CameraClientManager::toString() const { | 
|  | 3420 | auto all = getAll(); | 
|  | 3421 | String8 ret("["); | 
|  | 3422 | bool hasAny = false; | 
|  | 3423 | for (auto& i : all) { | 
|  | 3424 | hasAny = true; | 
|  | 3425 | String8 key = i->getKey(); | 
|  | 3426 | int32_t cost = i->getCost(); | 
|  | 3427 | int32_t pid = i->getOwnerId(); | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3428 | int32_t score = i->getPriority().getScore(); | 
|  | 3429 | int32_t state = i->getPriority().getState(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3430 | auto conflicting = i->getConflicting(); | 
|  | 3431 | auto clientSp = i->getValue(); | 
|  | 3432 | String8 packageName; | 
| Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 3433 | userid_t clientUserId = 0; | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3434 | if (clientSp.get() != nullptr) { | 
|  | 3435 | packageName = String8{clientSp->getPackageName()}; | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3436 | uid_t clientUid = clientSp->getClientUid(); | 
|  | 3437 | clientUserId = multiuser_get_user_id(clientUid); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3438 | } | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3439 | ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" | 
|  | 3440 | PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3441 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3442 | if (clientSp.get() != nullptr) { | 
|  | 3443 | ret.appendFormat("User Id: %d, ", clientUserId); | 
|  | 3444 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3445 | if (packageName.size() != 0) { | 
|  | 3446 | ret.appendFormat("Client Package Name: %s", packageName.string()); | 
|  | 3447 | } | 
|  | 3448 |  | 
|  | 3449 | ret.append(", Conflicting Client Devices: {"); | 
|  | 3450 | for (auto& j : conflicting) { | 
|  | 3451 | ret.appendFormat("%s, ", j.string()); | 
|  | 3452 | } | 
|  | 3453 | ret.append("})"); | 
|  | 3454 | } | 
|  | 3455 | if (hasAny) ret.append("\n"); | 
|  | 3456 | ret.append("]\n"); | 
|  | 3457 | return ret; | 
|  | 3458 | } | 
|  | 3459 |  | 
|  | 3460 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( | 
|  | 3461 | const String8& key, const sp<BasicClient>& value, int32_t cost, | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3462 | const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId, | 
|  | 3463 | int32_t state) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3464 |  | 
| Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 3465 | bool isVendorClient = getCurrentServingCall() == BinderCallType::HWBINDER; | 
| Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 3466 | int32_t score_adj = isVendorClient ? kVendorClientScore : score; | 
|  | 3467 | int32_t state_adj = isVendorClient ? kVendorClientState: state; | 
|  | 3468 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3469 | return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>( | 
| Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 3470 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, isVendorClient); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3471 | } | 
|  | 3472 |  | 
|  | 3473 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( | 
|  | 3474 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) { | 
|  | 3475 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3476 | partial->getConflicting(), partial->getPriority().getScore(), | 
|  | 3477 | partial->getOwnerId(), partial->getPriority().getState()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3478 | } | 
|  | 3479 |  | 
|  | 3480 | // ---------------------------------------------------------------------------- | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3481 |  | 
|  | 3482 | static const int kDumpLockRetries = 50; | 
|  | 3483 | static const int kDumpLockSleep = 60000; | 
|  | 3484 |  | 
|  | 3485 | static bool tryLock(Mutex& mutex) | 
|  | 3486 | { | 
|  | 3487 | bool locked = false; | 
|  | 3488 | for (int i = 0; i < kDumpLockRetries; ++i) { | 
|  | 3489 | if (mutex.tryLock() == NO_ERROR) { | 
|  | 3490 | locked = true; | 
|  | 3491 | break; | 
|  | 3492 | } | 
|  | 3493 | usleep(kDumpLockSleep); | 
|  | 3494 | } | 
|  | 3495 | return locked; | 
|  | 3496 | } | 
|  | 3497 |  | 
|  | 3498 | status_t CameraService::dump(int fd, const Vector<String16>& args) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3499 | ATRACE_CALL(); | 
|  | 3500 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3501 | if (checkCallingPermission(sDumpPermission) == false) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3502 | dprintf(fd, "Permission Denial: can't dump CameraService from pid=%d, uid=%d\n", | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3503 | CameraThreadState::getCallingPid(), | 
|  | 3504 | CameraThreadState::getCallingUid()); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3505 | return NO_ERROR; | 
|  | 3506 | } | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3507 | bool locked = tryLock(mServiceLock); | 
|  | 3508 | // failed to lock - CameraService is probably deadlocked | 
|  | 3509 | if (!locked) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3510 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3511 | } | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3512 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3513 | if (!mInitialized) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3514 | dprintf(fd, "!! No camera HAL available !!\n"); | 
| Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 3515 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3516 | // Dump event log for error information | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3517 | dumpEventLog(fd); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3518 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3519 | if (locked) mServiceLock.unlock(); | 
|  | 3520 | return NO_ERROR; | 
|  | 3521 | } | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3522 | dprintf(fd, "\n== Service global info: ==\n\n"); | 
|  | 3523 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3524 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 3525 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", | 
|  | 3526 | mNormalDeviceIdsWithoutSystemCamera.size()); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3527 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { | 
|  | 3528 | dprintf(fd, "    Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); | 
|  | 3529 | } | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3530 | String8 activeClientString = mActiveClientManager.toString(); | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3531 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string()); | 
|  | 3532 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string()); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3533 |  | 
|  | 3534 | dumpEventLog(fd); | 
|  | 3535 |  | 
|  | 3536 | bool stateLocked = tryLock(mCameraStatesLock); | 
|  | 3537 | if (!stateLocked) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3538 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3539 | } | 
|  | 3540 |  | 
| Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 3541 | int argSize = args.size(); | 
|  | 3542 | for (int i = 0; i < argSize; i++) { | 
|  | 3543 | if (args[i] == TagMonitor::kMonitorOption) { | 
|  | 3544 | if (i + 1 < argSize) { | 
|  | 3545 | mMonitorTags = String8(args[i + 1]); | 
|  | 3546 | } | 
|  | 3547 | break; | 
|  | 3548 | } | 
|  | 3549 | } | 
|  | 3550 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3551 | for (auto& state : mCameraStates) { | 
|  | 3552 | String8 cameraId = state.first; | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3553 |  | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3554 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string()); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3555 |  | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3556 | CameraParameters p = state.second->getShimParams(); | 
|  | 3557 | if (!p.isEmpty()) { | 
|  | 3558 | dprintf(fd, "  Camera1 API shim is using parameters:\n        "); | 
|  | 3559 | p.dump(fd, args); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3560 | } | 
|  | 3561 |  | 
|  | 3562 | auto clientDescriptor = mActiveClientManager.get(cameraId); | 
| Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 3563 | if (clientDescriptor != nullptr) { | 
|  | 3564 | dprintf(fd, "  Device %s is open. Client instance dump:\n", | 
|  | 3565 | cameraId.string()); | 
|  | 3566 | dprintf(fd, "    Client priority score: %d state: %d\n", | 
|  | 3567 | clientDescriptor->getPriority().getScore(), | 
|  | 3568 | clientDescriptor->getPriority().getState()); | 
|  | 3569 | dprintf(fd, "    Client PID: %d\n", clientDescriptor->getOwnerId()); | 
|  | 3570 |  | 
|  | 3571 | auto client = clientDescriptor->getValue(); | 
|  | 3572 | dprintf(fd, "    Client package: %s\n", | 
|  | 3573 | String8(client->getPackageName()).string()); | 
|  | 3574 |  | 
|  | 3575 | client->dumpClient(fd, args); | 
|  | 3576 | } else { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3577 | dprintf(fd, "  Device %s is closed, no client instance\n", | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3578 | cameraId.string()); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3579 | } | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3580 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3581 | } | 
|  | 3582 |  | 
|  | 3583 | if (stateLocked) mCameraStatesLock.unlock(); | 
|  | 3584 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3585 | if (locked) mServiceLock.unlock(); | 
|  | 3586 |  | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 3587 | mCameraProviderManager->dump(fd, args); | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3588 |  | 
|  | 3589 | dprintf(fd, "\n== Vendor tags: ==\n\n"); | 
|  | 3590 |  | 
|  | 3591 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); | 
|  | 3592 | if (desc == NULL) { | 
| Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 3593 | sp<VendorTagDescriptorCache> cache = | 
|  | 3594 | VendorTagDescriptorCache::getGlobalVendorTagCache(); | 
|  | 3595 | if (cache == NULL) { | 
|  | 3596 | dprintf(fd, "No vendor tags.\n"); | 
|  | 3597 | } else { | 
|  | 3598 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); | 
|  | 3599 | } | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3600 | } else { | 
|  | 3601 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); | 
|  | 3602 | } | 
|  | 3603 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3604 | // Dump camera traces if there were any | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3605 | dprintf(fd, "\n"); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3606 | camera3::CameraTraces::dump(fd, args); | 
|  | 3607 |  | 
|  | 3608 | // Process dump arguments, if any | 
|  | 3609 | int n = args.size(); | 
|  | 3610 | String16 verboseOption("-v"); | 
|  | 3611 | String16 unreachableOption("--unreachable"); | 
|  | 3612 | for (int i = 0; i < n; i++) { | 
|  | 3613 | if (args[i] == verboseOption) { | 
|  | 3614 | // change logging level | 
|  | 3615 | if (i + 1 >= n) continue; | 
|  | 3616 | String8 levelStr(args[i+1]); | 
|  | 3617 | int level = atoi(levelStr.string()); | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3618 | dprintf(fd, "\nSetting log level to %d.\n", level); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3619 | setLogLevel(level); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3620 | } else if (args[i] == unreachableOption) { | 
|  | 3621 | // Dump memory analysis | 
|  | 3622 | // TODO - should limit be an argument parameter? | 
|  | 3623 | UnreachableMemoryInfo info; | 
|  | 3624 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); | 
|  | 3625 | if (!success) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3626 | dprintf(fd, "\n== Unable to dump unreachable memory. " | 
|  | 3627 | "Try disabling SELinux enforcement. ==\n"); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3628 | } else { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3629 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3630 | std::string s = info.ToString(/*log_contents*/ true); | 
|  | 3631 | write(fd, s.c_str(), s.size()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3632 | } | 
|  | 3633 | } | 
|  | 3634 | } | 
|  | 3635 | return NO_ERROR; | 
|  | 3636 | } | 
|  | 3637 |  | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3638 | void CameraService::dumpEventLog(int fd) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3639 | 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] | 3640 |  | 
|  | 3641 | Mutex::Autolock l(mLogLock); | 
|  | 3642 | for (const auto& msg : mEventLog) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3643 | dprintf(fd, "  %s\n", msg.string()); | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3644 | } | 
|  | 3645 |  | 
|  | 3646 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3647 | dprintf(fd, "  ...\n"); | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3648 | } else if (mEventLog.size() == 0) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3649 | dprintf(fd, "  [no events yet]\n"); | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3650 | } | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3651 | dprintf(fd, "\n"); | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3652 | } | 
|  | 3653 |  | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3654 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3655 | Mutex::Autolock al(mTorchClientMapMutex); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3656 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { | 
|  | 3657 | if (mTorchClientMap[i] == who) { | 
|  | 3658 | // 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] | 3659 | String8 cameraId = mTorchClientMap.keyAt(i); | 
|  | 3660 | status_t res = mFlashlight->setTorchMode(cameraId, false); | 
|  | 3661 | if (res) { | 
|  | 3662 | ALOGE("%s: torch client died but couldn't turn off torch: " | 
|  | 3663 | "%s (%d)", __FUNCTION__, strerror(-res), res); | 
|  | 3664 | return; | 
|  | 3665 | } | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3666 | mTorchClientMap.removeItemsAt(i); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3667 | break; | 
|  | 3668 | } | 
|  | 3669 | } | 
|  | 3670 | } | 
|  | 3671 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3672 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { | 
| Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3673 |  | 
| Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 3674 | /** | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3675 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the | 
|  | 3676 | * binder driver | 
| Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 3677 | */ | 
| Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 3678 | // PID here is approximate and can be wrong. | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3679 | logClientDied(CameraThreadState::getCallingPid(), String8("Binder died unexpectedly")); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3680 |  | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3681 | // check torch client | 
|  | 3682 | handleTorchClientBinderDied(who); | 
|  | 3683 |  | 
|  | 3684 | // check camera device client | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3685 | if(!evictClientIdByRemote(who)) { | 
|  | 3686 | 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] | 3687 | return; | 
|  | 3688 | } | 
|  | 3689 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3690 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", | 
|  | 3691 | __FUNCTION__); | 
| Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3692 | } | 
|  | 3693 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3694 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3695 | updateStatus(status, cameraId, {}); | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3696 | } | 
|  | 3697 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3698 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId, | 
|  | 3699 | std::initializer_list<StatusInternal> rejectSourceStates) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3700 | // Do not lock mServiceLock here or can get into a deadlock from | 
|  | 3701 | // connect() -> disconnect -> updateStatus | 
|  | 3702 |  | 
|  | 3703 | auto state = getCameraState(cameraId); | 
|  | 3704 |  | 
|  | 3705 | if (state == nullptr) { | 
|  | 3706 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, | 
|  | 3707 | cameraId.string()); | 
|  | 3708 | return; | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3709 | } | 
|  | 3710 |  | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3711 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) | 
|  | 3712 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
|  | 3713 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { | 
|  | 3714 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); | 
|  | 3715 | return; | 
|  | 3716 | } | 
| Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 3717 | bool supportsHAL3 = false; | 
|  | 3718 | // supportsCameraApi also holds mInterfaceMutex, we can't call it in the | 
|  | 3719 | // HIDL onStatusChanged wrapper call (we'll hold mStatusListenerLock and | 
|  | 3720 | // mInterfaceMutex together, which can lead to deadlocks) | 
|  | 3721 | binder::Status sRet = | 
|  | 3722 | supportsCameraApi(String16(cameraId), hardware::ICameraService::API_VERSION_2, | 
|  | 3723 | &supportsHAL3); | 
|  | 3724 | if (!sRet.isOk()) { | 
|  | 3725 | ALOGW("%s: Failed to determine if device supports HAL3 %s, supportsCameraApi call failed", | 
|  | 3726 | __FUNCTION__, cameraId.string()); | 
|  | 3727 | return; | 
|  | 3728 | } | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3729 |  | 
|  | 3730 | // Collect the logical cameras without holding mStatusLock in updateStatus | 
|  | 3731 | // as that can lead to a deadlock(b/162192331). | 
|  | 3732 | auto logicalCameraIds = getLogicalCameras(cameraId); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3733 | // Update the status for this camera state, then send the onStatusChangedCallbacks to each | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3734 | // of the listeners with both the mStatusLock and mStatusListenerLock held | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3735 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, &supportsHAL3, | 
|  | 3736 | &logicalCameraIds] | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3737 | (const String8& cameraId, StatusInternal status) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3738 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3739 | if (status != StatusInternal::ENUMERATING) { | 
| Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 3740 | // Update torch status if it has a flash unit. | 
|  | 3741 | Mutex::Autolock al(mTorchStatusMutex); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3742 | TorchModeStatus torchStatus; | 
| Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 3743 | if (getTorchStatusLocked(cameraId, &torchStatus) != | 
|  | 3744 | NAME_NOT_FOUND) { | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3745 | TorchModeStatus newTorchStatus = | 
|  | 3746 | status == StatusInternal::PRESENT ? | 
|  | 3747 | TorchModeStatus::AVAILABLE_OFF : | 
|  | 3748 | TorchModeStatus::NOT_AVAILABLE; | 
| Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 3749 | if (torchStatus != newTorchStatus) { | 
|  | 3750 | onTorchStatusChangedLocked(cameraId, newTorchStatus); | 
|  | 3751 | } | 
|  | 3752 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3753 | } | 
|  | 3754 |  | 
|  | 3755 | Mutex::Autolock lock(mStatusListenerLock); | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3756 | notifyPhysicalCameraStatusLocked(mapToInterface(status), String16(cameraId), | 
|  | 3757 | logicalCameraIds, deviceKind); | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3758 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3759 | for (auto& listener : mListenerList) { | 
| Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 3760 | bool isVendorListener = listener->isVendorListener(); | 
|  | 3761 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, | 
|  | 3762 | listener->getListenerPid(), listener->getListenerUid()) || | 
| Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 3763 | (isVendorListener && !supportsHAL3)) { | 
| Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 3764 | ALOGV("Skipping discovery callback for system-only camera/HAL1 device %s", | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3765 | cameraId.c_str()); | 
| Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3766 | continue; | 
|  | 3767 | } | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3768 | listener->getListener()->onStatusChanged(mapToInterface(status), | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3769 | String16(cameraId)); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3770 | } | 
|  | 3771 | }); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3772 | } | 
|  | 3773 |  | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3774 | void CameraService::updateOpenCloseStatus(const String8& cameraId, bool open, | 
|  | 3775 | const String16& clientPackageName) { | 
|  | 3776 | Mutex::Autolock lock(mStatusListenerLock); | 
|  | 3777 |  | 
|  | 3778 | for (const auto& it : mListenerList) { | 
|  | 3779 | if (!it->isOpenCloseCallbackAllowed()) { | 
|  | 3780 | continue; | 
|  | 3781 | } | 
|  | 3782 |  | 
|  | 3783 | binder::Status ret; | 
|  | 3784 | String16 cameraId64(cameraId); | 
|  | 3785 | if (open) { | 
|  | 3786 | ret = it->getListener()->onCameraOpened(cameraId64, clientPackageName); | 
|  | 3787 | } else { | 
|  | 3788 | ret = it->getListener()->onCameraClosed(cameraId64); | 
|  | 3789 | } | 
|  | 3790 | if (!ret.isOk()) { | 
|  | 3791 | ALOGE("%s: Failed to trigger onCameraOpened/onCameraClosed callback: %d", __FUNCTION__, | 
|  | 3792 | ret.exceptionCode()); | 
|  | 3793 | } | 
|  | 3794 | } | 
|  | 3795 | } | 
|  | 3796 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3797 | template<class Func> | 
|  | 3798 | void CameraService::CameraState::updateStatus(StatusInternal status, | 
|  | 3799 | const String8& cameraId, | 
|  | 3800 | std::initializer_list<StatusInternal> rejectSourceStates, | 
|  | 3801 | Func onStatusUpdatedLocked) { | 
|  | 3802 | Mutex::Autolock lock(mStatusLock); | 
|  | 3803 | StatusInternal oldStatus = mStatus; | 
|  | 3804 | mStatus = status; | 
|  | 3805 |  | 
|  | 3806 | if (oldStatus == status) { | 
|  | 3807 | return; | 
|  | 3808 | } | 
|  | 3809 |  | 
|  | 3810 | ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__, | 
|  | 3811 | cameraId.string(), oldStatus, status); | 
|  | 3812 |  | 
|  | 3813 | if (oldStatus == StatusInternal::NOT_PRESENT && | 
|  | 3814 | (status != StatusInternal::PRESENT && | 
|  | 3815 | status != StatusInternal::ENUMERATING)) { | 
|  | 3816 |  | 
|  | 3817 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", | 
|  | 3818 | __FUNCTION__); | 
|  | 3819 | mStatus = oldStatus; | 
|  | 3820 | return; | 
|  | 3821 | } | 
|  | 3822 |  | 
|  | 3823 | /** | 
|  | 3824 | * Sometimes we want to conditionally do a transition. | 
|  | 3825 | * For example if a client disconnects, we want to go to PRESENT | 
|  | 3826 | * only if we weren't already in NOT_PRESENT or ENUMERATING. | 
|  | 3827 | */ | 
|  | 3828 | for (auto& rejectStatus : rejectSourceStates) { | 
|  | 3829 | if (oldStatus == rejectStatus) { | 
|  | 3830 | ALOGV("%s: Rejecting status transition for Camera ID %s,  since the source " | 
|  | 3831 | "state was was in one of the bad states.", __FUNCTION__, cameraId.string()); | 
|  | 3832 | mStatus = oldStatus; | 
|  | 3833 | return; | 
|  | 3834 | } | 
|  | 3835 | } | 
|  | 3836 |  | 
|  | 3837 | onStatusUpdatedLocked(cameraId, status); | 
|  | 3838 | } | 
|  | 3839 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3840 | status_t CameraService::getTorchStatusLocked( | 
|  | 3841 | const String8& cameraId, | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3842 | TorchModeStatus *status) const { | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3843 | if (!status) { | 
|  | 3844 | return BAD_VALUE; | 
|  | 3845 | } | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3846 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); | 
|  | 3847 | if (index == NAME_NOT_FOUND) { | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3848 | // invalid camera ID or the camera doesn't have a flash unit | 
|  | 3849 | return NAME_NOT_FOUND; | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3850 | } | 
|  | 3851 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3852 | *status = mTorchStatusMap.valueAt(index); | 
|  | 3853 | return OK; | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3854 | } | 
|  | 3855 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3856 | status_t CameraService::setTorchStatusLocked(const String8& cameraId, | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3857 | TorchModeStatus status) { | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3858 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); | 
|  | 3859 | if (index == NAME_NOT_FOUND) { | 
|  | 3860 | return BAD_VALUE; | 
|  | 3861 | } | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3862 | mTorchStatusMap.editValueAt(index) = status; | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3863 |  | 
|  | 3864 | return OK; | 
|  | 3865 | } | 
|  | 3866 |  | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3867 | std::list<String16> CameraService::getLogicalCameras( | 
|  | 3868 | const String8& physicalCameraId) { | 
|  | 3869 | std::list<String16> retList; | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3870 | Mutex::Autolock lock(mCameraStatesLock); | 
|  | 3871 | for (const auto& state : mCameraStates) { | 
|  | 3872 | std::vector<std::string> physicalCameraIds; | 
|  | 3873 | if (!mCameraProviderManager->isLogicalCamera(state.first.c_str(), &physicalCameraIds)) { | 
|  | 3874 | // This is not a logical multi-camera. | 
|  | 3875 | continue; | 
|  | 3876 | } | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3877 | if (std::find(physicalCameraIds.begin(), physicalCameraIds.end(), physicalCameraId.c_str()) | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3878 | == physicalCameraIds.end()) { | 
|  | 3879 | // cameraId is not a physical camera of this logical multi-camera. | 
|  | 3880 | continue; | 
|  | 3881 | } | 
|  | 3882 |  | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3883 | retList.emplace_back(String16(state.first)); | 
|  | 3884 | } | 
|  | 3885 | return retList; | 
|  | 3886 | } | 
|  | 3887 |  | 
|  | 3888 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, | 
|  | 3889 | const String16& physicalCameraId, const std::list<String16>& logicalCameraIds, | 
|  | 3890 | SystemCameraKind deviceKind) { | 
|  | 3891 | // mStatusListenerLock is expected to be locked | 
|  | 3892 | for (const auto& logicalCameraId : logicalCameraIds) { | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3893 | for (auto& listener : mListenerList) { | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3894 | // Note: we check only the deviceKind of the physical camera id | 
|  | 3895 | // since, logical camera ids and their physical camera ids are | 
|  | 3896 | // guaranteed to have the same system camera kind. | 
| Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 3897 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), | 
|  | 3898 | listener->getListenerPid(), listener->getListenerUid())) { | 
|  | 3899 | ALOGV("Skipping discovery callback for system-only camera device %s", | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3900 | String8(physicalCameraId).c_str()); | 
| Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 3901 | continue; | 
|  | 3902 | } | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3903 | listener->getListener()->onPhysicalCameraStatusChanged(status, | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3904 | logicalCameraId, physicalCameraId); | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3905 | } | 
|  | 3906 | } | 
|  | 3907 | } | 
|  | 3908 |  | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 3909 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3910 | void CameraService::blockClientsForUid(uid_t uid) { | 
|  | 3911 | const auto clients = mActiveClientManager.getAll(); | 
|  | 3912 | for (auto& current : clients) { | 
|  | 3913 | if (current != nullptr) { | 
|  | 3914 | const auto basicClient = current->getValue(); | 
|  | 3915 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { | 
|  | 3916 | basicClient->block(); | 
|  | 3917 | } | 
|  | 3918 | } | 
|  | 3919 | } | 
|  | 3920 | } | 
|  | 3921 |  | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3922 | void CameraService::blockAllClients() { | 
|  | 3923 | const auto clients = mActiveClientManager.getAll(); | 
|  | 3924 | for (auto& current : clients) { | 
|  | 3925 | if (current != nullptr) { | 
|  | 3926 | const auto basicClient = current->getValue(); | 
|  | 3927 | if (basicClient.get() != nullptr) { | 
|  | 3928 | basicClient->block(); | 
|  | 3929 | } | 
|  | 3930 | } | 
|  | 3931 | } | 
|  | 3932 | } | 
|  | 3933 |  | 
| Evan Severson | 190ad25 | 2021-01-07 20:37:15 -0800 | [diff] [blame] | 3934 | void CameraService::setMuteForAllClients(userid_t userId, bool enabled) { | 
|  | 3935 | const auto clients = mActiveClientManager.getAll(); | 
|  | 3936 | for (auto& current : clients) { | 
|  | 3937 | if (current != nullptr) { | 
|  | 3938 | const auto basicClient = current->getValue(); | 
|  | 3939 | if (basicClient.get() != nullptr | 
|  | 3940 | && multiuser_get_user_id(basicClient->getClientUid()) == userId) { | 
|  | 3941 | basicClient->setCameraMute(enabled); | 
|  | 3942 | } | 
|  | 3943 | } | 
|  | 3944 | } | 
|  | 3945 | } | 
|  | 3946 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3947 | // NOTE: This is a remote API - make sure all args are validated | 
|  | 3948 | status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) { | 
|  | 3949 | if (!checkCallingPermission(sManageCameraPermission, nullptr, nullptr)) { | 
|  | 3950 | return PERMISSION_DENIED; | 
|  | 3951 | } | 
|  | 3952 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { | 
|  | 3953 | return BAD_VALUE; | 
|  | 3954 | } | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 3955 | if (args.size() >= 3 && args[0] == String16("set-uid-state")) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3956 | return handleSetUidState(args, err); | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 3957 | } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3958 | return handleResetUidState(args, err); | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 3959 | } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3960 | return handleGetUidState(args, out, err); | 
| Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 3961 | } else if (args.size() >= 2 && args[0] == String16("set-rotate-and-crop")) { | 
|  | 3962 | return handleSetRotateAndCrop(args); | 
|  | 3963 | } else if (args.size() >= 1 && args[0] == String16("get-rotate-and-crop")) { | 
|  | 3964 | return handleGetRotateAndCrop(out); | 
| Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 3965 | } else if (args.size() >= 2 && args[0] == String16("set-image-dump-mask")) { | 
|  | 3966 | return handleSetImageDumpMask(args); | 
|  | 3967 | } else if (args.size() >= 1 && args[0] == String16("get-image-dump-mask")) { | 
|  | 3968 | return handleGetImageDumpMask(out); | 
| Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 3969 | } else if (args.size() >= 2 && args[0] == String16("set-camera-mute")) { | 
|  | 3970 | return handleSetCameraMute(args); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3971 | } else if (args.size() == 1 && args[0] == String16("help")) { | 
|  | 3972 | printHelp(out); | 
|  | 3973 | return NO_ERROR; | 
|  | 3974 | } | 
|  | 3975 | printHelp(err); | 
|  | 3976 | return BAD_VALUE; | 
|  | 3977 | } | 
|  | 3978 |  | 
|  | 3979 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 3980 | String16 packageName = args[1]; | 
|  | 3981 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3982 | bool active = false; | 
|  | 3983 | if (args[2] == String16("active")) { | 
|  | 3984 | active = true; | 
|  | 3985 | } else if ((args[2] != String16("idle"))) { | 
|  | 3986 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string()); | 
|  | 3987 | return BAD_VALUE; | 
|  | 3988 | } | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 3989 |  | 
|  | 3990 | int userId = 0; | 
|  | 3991 | if (args.size() >= 5 && args[3] == String16("--user")) { | 
|  | 3992 | userId = atoi(String8(args[4])); | 
|  | 3993 | } | 
|  | 3994 |  | 
|  | 3995 | uid_t uid; | 
|  | 3996 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { | 
|  | 3997 | return BAD_VALUE; | 
|  | 3998 | } | 
|  | 3999 |  | 
|  | 4000 | mUidPolicy->addOverrideUid(uid, packageName, active); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4001 | return NO_ERROR; | 
|  | 4002 | } | 
|  | 4003 |  | 
|  | 4004 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4005 | String16 packageName = args[1]; | 
|  | 4006 |  | 
|  | 4007 | int userId = 0; | 
|  | 4008 | if (args.size() >= 4 && args[2] == String16("--user")) { | 
|  | 4009 | userId = atoi(String8(args[3])); | 
|  | 4010 | } | 
|  | 4011 |  | 
|  | 4012 | uid_t uid; | 
|  | 4013 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4014 | return BAD_VALUE; | 
|  | 4015 | } | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4016 |  | 
|  | 4017 | mUidPolicy->removeOverrideUid(uid, packageName); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4018 | return NO_ERROR; | 
|  | 4019 | } | 
|  | 4020 |  | 
|  | 4021 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4022 | String16 packageName = args[1]; | 
|  | 4023 |  | 
|  | 4024 | int userId = 0; | 
|  | 4025 | if (args.size() >= 4 && args[2] == String16("--user")) { | 
|  | 4026 | userId = atoi(String8(args[3])); | 
|  | 4027 | } | 
|  | 4028 |  | 
|  | 4029 | uid_t uid; | 
|  | 4030 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4031 | return BAD_VALUE; | 
|  | 4032 | } | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4033 |  | 
|  | 4034 | if (mUidPolicy->isUidActive(uid, packageName)) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4035 | return dprintf(out, "active\n"); | 
|  | 4036 | } else { | 
|  | 4037 | return dprintf(out, "idle\n"); | 
|  | 4038 | } | 
|  | 4039 | } | 
|  | 4040 |  | 
| Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4041 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { | 
|  | 4042 | int rotateValue = atoi(String8(args[1])); | 
|  | 4043 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || | 
|  | 4044 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; | 
|  | 4045 | Mutex::Autolock lock(mServiceLock); | 
|  | 4046 |  | 
|  | 4047 | mOverrideRotateAndCropMode = rotateValue; | 
|  | 4048 |  | 
|  | 4049 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; | 
|  | 4050 |  | 
|  | 4051 | const auto clients = mActiveClientManager.getAll(); | 
|  | 4052 | for (auto& current : clients) { | 
|  | 4053 | if (current != nullptr) { | 
|  | 4054 | const auto basicClient = current->getValue(); | 
|  | 4055 | if (basicClient.get() != nullptr) { | 
|  | 4056 | basicClient->setRotateAndCropOverride(rotateValue); | 
|  | 4057 | } | 
|  | 4058 | } | 
|  | 4059 | } | 
|  | 4060 |  | 
|  | 4061 | return OK; | 
|  | 4062 | } | 
|  | 4063 |  | 
|  | 4064 | status_t CameraService::handleGetRotateAndCrop(int out) { | 
|  | 4065 | Mutex::Autolock lock(mServiceLock); | 
|  | 4066 |  | 
|  | 4067 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); | 
|  | 4068 | } | 
|  | 4069 |  | 
| Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4070 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { | 
|  | 4071 | char *endPtr; | 
|  | 4072 | errno = 0; | 
|  | 4073 | String8 maskString8 = String8(args[1]); | 
|  | 4074 | long maskValue = strtol(maskString8.c_str(), &endPtr, 10); | 
|  | 4075 |  | 
|  | 4076 | if (errno != 0) return BAD_VALUE; | 
|  | 4077 | if (endPtr != maskString8.c_str() + maskString8.size()) return BAD_VALUE; | 
|  | 4078 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; | 
|  | 4079 |  | 
|  | 4080 | Mutex::Autolock lock(mServiceLock); | 
|  | 4081 |  | 
|  | 4082 | mImageDumpMask = maskValue; | 
|  | 4083 |  | 
|  | 4084 | return OK; | 
|  | 4085 | } | 
|  | 4086 |  | 
|  | 4087 | status_t CameraService::handleGetImageDumpMask(int out) { | 
|  | 4088 | Mutex::Autolock lock(mServiceLock); | 
|  | 4089 |  | 
|  | 4090 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); | 
|  | 4091 | } | 
|  | 4092 |  | 
| Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 4093 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { | 
|  | 4094 | int muteValue = strtol(String8(args[1]), nullptr, 10); | 
|  | 4095 | if (errno != 0) return BAD_VALUE; | 
|  | 4096 |  | 
|  | 4097 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; | 
|  | 4098 | Mutex::Autolock lock(mServiceLock); | 
|  | 4099 |  | 
|  | 4100 | mOverrideCameraMuteMode = (muteValue == 1); | 
|  | 4101 |  | 
|  | 4102 | const auto clients = mActiveClientManager.getAll(); | 
|  | 4103 | for (auto& current : clients) { | 
|  | 4104 | if (current != nullptr) { | 
|  | 4105 | const auto basicClient = current->getValue(); | 
|  | 4106 | if (basicClient.get() != nullptr) { | 
|  | 4107 | if (basicClient->supportsCameraMute()) { | 
|  | 4108 | basicClient->setCameraMute(mOverrideCameraMuteMode); | 
|  | 4109 | } | 
|  | 4110 | } | 
|  | 4111 | } | 
|  | 4112 | } | 
|  | 4113 |  | 
|  | 4114 | return OK; | 
|  | 4115 | } | 
| Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4116 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4117 | status_t CameraService::printHelp(int out) { | 
|  | 4118 | return dprintf(out, "Camera service commands:\n" | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4119 | "  get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" | 
|  | 4120 | "  set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" | 
|  | 4121 | "  reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n" | 
| Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4122 | "  set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" | 
|  | 4123 | "      Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" | 
|  | 4124 | "  get-rotate-and-crop returns the current override rotate-and-crop value\n" | 
| Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4125 | "  set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" | 
|  | 4126 | "      Valid values 0=OFF, 1=ON for JPEG\n" | 
|  | 4127 | "  get-image-dump-mask returns the current image-dump-mask value\n" | 
| Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 4128 | "  set-camera-mute <0/1> enable or disable camera muting\n" | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4129 | "  help print this message\n"); | 
|  | 4130 | } | 
|  | 4131 |  | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4132 | int32_t CameraService::updateAudioRestriction() { | 
|  | 4133 | Mutex::Autolock lock(mServiceLock); | 
|  | 4134 | return updateAudioRestrictionLocked(); | 
|  | 4135 | } | 
|  | 4136 |  | 
|  | 4137 | int32_t CameraService::updateAudioRestrictionLocked() { | 
|  | 4138 | int32_t mode = 0; | 
|  | 4139 | // iterate through all active client | 
|  | 4140 | for (const auto& i : mActiveClientManager.getAll()) { | 
|  | 4141 | const auto clientSp = i->getValue(); | 
|  | 4142 | mode |= clientSp->getAudioRestriction(); | 
|  | 4143 | } | 
|  | 4144 |  | 
|  | 4145 | bool modeChanged = (mAudioRestriction != mode); | 
|  | 4146 | mAudioRestriction = mode; | 
|  | 4147 | if (modeChanged) { | 
|  | 4148 | mAppOps.setCameraAudioRestriction(mode); | 
|  | 4149 | } | 
|  | 4150 | return mode; | 
|  | 4151 | } | 
|  | 4152 |  | 
| Evan Severson | 190ad25 | 2021-01-07 20:37:15 -0800 | [diff] [blame] | 4153 | bool CameraService::isUserSensorPrivacyEnabledForUid(uid_t uid) { | 
|  | 4154 | userid_t userId = multiuser_get_user_id(uid); | 
|  | 4155 | if (mCameraSensorPrivacyPolicies.find(userId) == mCameraSensorPrivacyPolicies.end()) { | 
|  | 4156 | sp<SensorPrivacyPolicy> userPolicy = new SensorPrivacyPolicy(this); | 
|  | 4157 | if (userPolicy->registerSelfForIndividual(userId) != OK) { | 
|  | 4158 | return false; | 
|  | 4159 | } | 
|  | 4160 | mCameraSensorPrivacyPolicies[userId] = userPolicy; | 
|  | 4161 | } | 
|  | 4162 | return mCameraSensorPrivacyPolicies[userId]->isSensorPrivacyEnabled(); | 
|  | 4163 | } | 
|  | 4164 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4165 | }; // namespace android |