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