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> |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 27 | #include <iostream> |
| 28 | #include <sstream> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 29 | #include <string> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 30 | #include <sys/types.h> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 31 | #include <inttypes.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 32 | #include <pthread.h> |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 33 | #include <poll.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 34 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 35 | #include <android/hardware/ICamera.h> |
| 36 | #include <android/hardware/ICameraClient.h> |
| 37 | |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 38 | #include <aidl/AidlCameraService.h> |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 39 | #include <android-base/macros.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 40 | #include <android-base/parseint.h> |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 41 | #include <android/permission/PermissionChecker.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 42 | #include <binder/ActivityManager.h> |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 43 | #include <binder/AppOpsManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 44 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 45 | #include <binder/MemoryBase.h> |
| 46 | #include <binder/MemoryHeapBase.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 47 | #include <binder/PermissionController.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 48 | #include <binder/IResultReceiver.h> |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 49 | #include <binderthreadstate/CallerUtils.h> |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 50 | #include <com_android_internal_camera_flags.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 51 | #include <cutils/atomic.h> |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 52 | #include <cutils/properties.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 53 | #include <cutils/misc.h> |
Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 54 | #include <gui/Surface.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 55 | #include <hardware/hardware.h> |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 56 | #include "hidl/HidlCameraService.h" |
| 57 | #include <hidl/HidlTransportSupport.h> |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 58 | #include <hwbinder/IPCThreadState.h> |
Eino-Ville Talvala | d89821e | 2016-04-20 11:23:50 -0700 | [diff] [blame] | 59 | #include <memunreachable/memunreachable.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 60 | #include <media/AudioSystem.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 61 | #include <media/IMediaHTTPService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | #include <media/mediaplayer.h> |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 63 | #include <mediautils/BatteryNotifier.h> |
Steven Moreland | 886d732 | 2021-04-02 04:19:45 +0000 | [diff] [blame] | 64 | #include <processinfo/ProcessInfoService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 65 | #include <utils/Errors.h> |
| 66 | #include <utils/Log.h> |
| 67 | #include <utils/String16.h> |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 68 | #include <utils/SystemClock.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 69 | #include <utils/Trace.h> |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 70 | #include <utils/CallStack.h> |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 71 | #include <private/android_filesystem_config.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 72 | #include <system/camera_vendor_tags.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 73 | #include <system/camera_metadata.h> |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 74 | #include <binder/IServiceManager.h> |
| 75 | #include <binder/IActivityManager.h> |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 76 | #include <camera/StringUtils.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 77 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 78 | #include <system/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 79 | |
| 80 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 81 | #include "api1/Camera2Client.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 82 | #include "api2/CameraDeviceClient.h" |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 83 | #include "utils/CameraTraces.h" |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 84 | #include "utils/TagMonitor.h" |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 85 | #include "utils/CameraThreadState.h" |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 86 | #include "utils/CameraServiceProxyWrapper.h" |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 87 | #include "utils/SessionConfigurationUtils.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 88 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 89 | namespace { |
| 90 | const char* kPermissionServiceName = "permission"; |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 91 | const char* kActivityServiceName = "activity"; |
| 92 | const char* kSensorPrivacyServiceName = "sensor_privacy"; |
| 93 | const char* kAppopsServiceName = "appops"; |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 94 | const char* kProcessInfoServiceName = "processinfo"; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 95 | }; // namespace anonymous |
| 96 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 97 | namespace android { |
| 98 | |
Austin Borger | ea93124 | 2021-12-13 23:10:41 +0000 | [diff] [blame] | 99 | using namespace camera3; |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 100 | using namespace camera3::SessionConfigurationUtils; |
| 101 | |
| 102 | using binder::Status; |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 103 | using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService; |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 104 | using frameworks::cameraservice::service::implementation::AidlCameraService; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 105 | using hardware::ICamera; |
| 106 | using hardware::ICameraClient; |
| 107 | using hardware::ICameraServiceListener; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 108 | using hardware::camera2::ICameraInjectionCallback; |
| 109 | using hardware::camera2::ICameraInjectionSession; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 110 | using hardware::camera2::utils::CameraIdAndSessionConfiguration; |
| 111 | using hardware::camera2::utils::ConcurrentCameraIdCombination; |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 112 | namespace flags = com::android::internal::camera::flags; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 113 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 114 | // ---------------------------------------------------------------------------- |
| 115 | // Logging support -- this is for debugging only |
| 116 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 117 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 118 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 119 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 120 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 121 | |
| 122 | static void setLogLevel(int level) { |
| 123 | android_atomic_write(level, &gLogLevel); |
| 124 | } |
| 125 | |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 126 | int32_t format_as(CameraService::StatusInternal s) { |
| 127 | return fmt::underlying(s); |
| 128 | } |
| 129 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 130 | // ---------------------------------------------------------------------------- |
| 131 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 132 | static const std::string sDumpPermission("android.permission.DUMP"); |
| 133 | static const std::string sManageCameraPermission("android.permission.MANAGE_CAMERA"); |
| 134 | static const std::string sCameraPermission("android.permission.CAMERA"); |
| 135 | static const std::string sSystemCameraPermission("android.permission.SYSTEM_CAMERA"); |
Jyoti Bhayana | 5bdb5a6 | 2023-08-24 14:46:08 -0700 | [diff] [blame] | 136 | static const std::string sCameraHeadlessSystemUserPermission( |
| 137 | "android.permission.CAMERA_HEADLESS_SYSTEM_USER"); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 138 | static const std::string sCameraPrivacyAllowlistPermission( |
| 139 | "android.permission.CAMERA_PRIVACY_ALLOWLIST"); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 140 | static const std::string |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 141 | sCameraSendSystemEventsPermission("android.permission.CAMERA_SEND_SYSTEM_EVENTS"); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 142 | static const std::string sCameraOpenCloseListenerPermission( |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 143 | "android.permission.CAMERA_OPEN_CLOSE_LISTENER"); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 144 | static const std::string |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 145 | sCameraInjectExternalCameraPermission("android.permission.CAMERA_INJECT_EXTERNAL_CAMERA"); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 146 | // Constant integer for FGS Logging, used to denote the API type for logger |
| 147 | static const int LOG_FGS_CAMERA_API = 1; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 148 | const char *sFileName = "lastOpenSessionDumpFile"; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 149 | static constexpr int32_t kSystemNativeClientScore = resource_policy::PERCEPTIBLE_APP_ADJ; |
| 150 | static constexpr int32_t kSystemNativeClientState = |
| 151 | ActivityManager::PROCESS_STATE_PERSISTENT_UI; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 152 | static const std::string kServiceName("cameraserver"); |
Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame] | 153 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 154 | const std::string CameraService::kOfflineDevice("offline-"); |
| 155 | const std::string CameraService::kWatchAllClientsFlag("all"); |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 156 | |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 157 | // Set to keep track of logged service error events. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 158 | static std::set<std::string> sServiceErrorEventSet; |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 159 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 160 | CameraService::CameraService( |
| 161 | std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper) : |
| 162 | mCameraServiceProxyWrapper(cameraServiceProxyWrapper == nullptr ? |
| 163 | std::make_shared<CameraServiceProxyWrapper>() : cameraServiceProxyWrapper), |
Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 164 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 165 | mNumberOfCameras(0), |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 166 | mNumberOfCamerasWithoutSystemCamera(0), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 167 | mSoundRef(0), mInitialized(false), |
| 168 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 169 | ALOGI("CameraService started (pid=%d)", getpid()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 170 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 171 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 172 | if (mMemFd == -1) { |
| 173 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 174 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 177 | // The word 'System' here does not refer to clients only on the system |
| 178 | // partition. They just need to have a android system uid. |
| 179 | static bool doesClientHaveSystemUid() { |
| 180 | return (CameraThreadState::getCallingUid() < AID_APP_START); |
| 181 | } |
| 182 | |
Charles Chen | a7b613c | 2023-01-24 21:57:33 +0000 | [diff] [blame] | 183 | // Enable processes with isolated AID to request the binder |
| 184 | void CameraService::instantiate() { |
| 185 | CameraService::publish(true); |
| 186 | } |
| 187 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 188 | void CameraService::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 189 | if (name != toString16(kAppopsServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 190 | return; |
| 191 | } |
| 192 | |
| 193 | ALOGV("appops service registered. setting camera audio restriction"); |
| 194 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
| 195 | } |
| 196 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 197 | void CameraService::onFirstRef() |
| 198 | { |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 199 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 200 | ALOGI("CameraService process starting"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 201 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 202 | BnCameraService::onFirstRef(); |
| 203 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 204 | // Update battery life tracking if service is restarting |
| 205 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 206 | notifier.noteResetCamera(); |
| 207 | notifier.noteResetFlashlight(); |
| 208 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 209 | status_t res = INVALID_OPERATION; |
Eino-Ville Talvala | 9cbbc83 | 2017-01-23 15:39:53 -0800 | [diff] [blame] | 210 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 211 | res = enumerateProviders(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 212 | if (res == OK) { |
| 213 | mInitialized = true; |
| 214 | } |
| 215 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 216 | mUidPolicy = new UidPolicy(this); |
| 217 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 218 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this); |
| 219 | mSensorPrivacyPolicy->registerSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 220 | mInjectionStatusListener = new InjectionStatusListener(this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 221 | |
| 222 | // appops function setCamerAudioRestriction uses getService which |
| 223 | // is blocking till the appops service is ready. To enable early |
| 224 | // boot availability for cameraservice, use checkService which is |
| 225 | // non blocking and register for notifications |
| 226 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 227 | sp<IBinder> binder = sm->checkService(toString16(kAppopsServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 228 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 229 | sm->registerForNotifications(toString16(kAppopsServiceName), this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 230 | } else { |
| 231 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
| 232 | } |
| 233 | |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 234 | sp<HidlCameraService> hcs = HidlCameraService::getInstance(this); |
| 235 | if (hcs->registerAsService() != android::OK) { |
Devin Moore | a1350e7 | 2023-01-11 23:40:42 +0000 | [diff] [blame] | 236 | // Deprecated, so it will fail to register on newer devices |
| 237 | ALOGW("%s: Did not register default android.frameworks.cameraservice.service@2.2", |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 238 | __FUNCTION__); |
| 239 | } |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 240 | |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 241 | if (!AidlCameraService::registerService(this)) { |
| 242 | ALOGE("%s: Failed to register default AIDL VNDK CameraService", __FUNCTION__); |
| 243 | } |
| 244 | |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 245 | // This needs to be last call in this function, so that it's as close to |
| 246 | // ServiceManager::addService() as possible. |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 247 | mCameraServiceProxyWrapper->pingCameraServiceProxy(); |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 248 | ALOGI("CameraService pinged cameraservice proxy"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 251 | status_t CameraService::enumerateProviders() { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 252 | status_t res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 253 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 254 | std::vector<std::string> deviceIds; |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 255 | std::unordered_map<std::string, std::set<std::string>> unavailPhysicalIds; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 256 | { |
| 257 | Mutex::Autolock l(mServiceLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 258 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 259 | if (nullptr == mCameraProviderManager.get()) { |
| 260 | mCameraProviderManager = new CameraProviderManager(); |
| 261 | res = mCameraProviderManager->initialize(this); |
| 262 | if (res != OK) { |
| 263 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", |
| 264 | __FUNCTION__, strerror(-res), res); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 265 | logServiceError("Unable to initialize camera provider manager", |
| 266 | ERROR_DISCONNECTED); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 267 | return res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 271 | |
| 272 | // Setup vendor tags before we call get_camera_info the first time |
| 273 | // because HAL might need to setup static vendor keys in get_camera_info |
| 274 | // TODO: maybe put this into CameraProviderManager::initialize()? |
| 275 | mCameraProviderManager->setUpVendorTags(); |
| 276 | |
| 277 | if (nullptr == mFlashlight.get()) { |
| 278 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 281 | res = mFlashlight->findFlashUnits(); |
| 282 | if (res != OK) { |
| 283 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); |
| 284 | } |
| 285 | |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 286 | deviceIds = mCameraProviderManager->getCameraDeviceIds(&unavailPhysicalIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | |
| 290 | for (auto& cameraId : deviceIds) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 291 | if (getCameraState(cameraId) == nullptr) { |
| 292 | onDeviceStatusChanged(cameraId, CameraDeviceStatus::PRESENT); |
Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 293 | } |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 294 | if (unavailPhysicalIds.count(cameraId) > 0) { |
| 295 | for (const auto& physicalId : unavailPhysicalIds[cameraId]) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 296 | onDeviceStatusChanged(cameraId, physicalId, CameraDeviceStatus::NOT_PRESENT); |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 301 | // Derive primary rear/front cameras, and filter their charactierstics. |
| 302 | // This needs to be done after all cameras are enumerated and camera ids are sorted. |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 303 | if (SessionConfigurationUtils::IS_PERF_CLASS) { |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 304 | // Assume internal cameras are advertised from the same |
| 305 | // provider. If multiple providers are registered at different time, |
| 306 | // and each provider contains multiple internal color cameras, the current |
| 307 | // logic may filter the characteristics of more than one front/rear color |
| 308 | // cameras. |
| 309 | Mutex::Autolock l(mServiceLock); |
| 310 | filterSPerfClassCharacteristicsLocked(); |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 311 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 312 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 313 | return OK; |
| 314 | } |
| 315 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 316 | void CameraService::broadcastTorchModeStatus(const std::string& cameraId, TorchModeStatus status, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 317 | SystemCameraKind systemCameraKind) { |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 318 | Mutex::Autolock lock(mStatusListenerLock); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 319 | for (auto& i : mListenerList) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 320 | if (shouldSkipStatusUpdates(systemCameraKind, i->isVendorListener(), i->getListenerPid(), |
| 321 | i->getListenerUid())) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 322 | ALOGV("%s: Skipping torch callback for system-only camera device %s", |
| 323 | __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 324 | continue; |
| 325 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 326 | auto ret = i->getListener()->onTorchStatusChanged(mapToInterface(status), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 327 | cameraId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 328 | i->handleBinderStatus(ret, "%s: Failed to trigger onTorchStatusChanged for %d:%d: %d", |
| 329 | __FUNCTION__, i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 330 | // Also trigger the torch callbacks for cameras that were remapped to the current cameraId |
| 331 | // for the specific package that this listener belongs to. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 332 | std::vector<std::string> remappedCameraIds = |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 333 | findOriginalIdsForRemappedCameraId(cameraId, i->getListenerUid()); |
| 334 | for (auto& remappedCameraId : remappedCameraIds) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 335 | ret = i->getListener()->onTorchStatusChanged(mapToInterface(status), remappedCameraId); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 336 | i->handleBinderStatus(ret, "%s: Failed to trigger onTorchStatusChanged for %d:%d: %d", |
| 337 | __FUNCTION__, i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
| 338 | } |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 342 | CameraService::~CameraService() { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 343 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 344 | mUidPolicy->unregisterSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 345 | mSensorPrivacyPolicy->unregisterSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 346 | mInjectionStatusListener->removeListener(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 349 | void CameraService::onNewProviderRegistered() { |
| 350 | enumerateProviders(); |
| 351 | } |
| 352 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 353 | void CameraService::filterAPI1SystemCameraLocked( |
| 354 | const std::vector<std::string> &normalDeviceIds) { |
| 355 | mNormalDeviceIdsWithoutSystemCamera.clear(); |
| 356 | for (auto &deviceId : normalDeviceIds) { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 357 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 358 | if (getSystemCameraKind(deviceId, &deviceKind) != OK) { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 359 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, deviceId.c_str()); |
| 360 | continue; |
| 361 | } |
| 362 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 363 | // All system camera ids will necessarily come after public camera |
| 364 | // device ids as per the HAL interface contract. |
| 365 | break; |
| 366 | } |
| 367 | mNormalDeviceIdsWithoutSystemCamera.push_back(deviceId); |
| 368 | } |
| 369 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, |
| 370 | mNormalDeviceIdsWithoutSystemCamera.size()); |
| 371 | } |
| 372 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 373 | status_t CameraService::getSystemCameraKind(const std::string& cameraId, |
| 374 | SystemCameraKind *kind) const { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 375 | auto state = getCameraState(cameraId); |
| 376 | if (state != nullptr) { |
| 377 | *kind = state->getSystemCameraKind(); |
| 378 | return OK; |
| 379 | } |
| 380 | // Hidden physical camera ids won't have CameraState |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 381 | return mCameraProviderManager->getSystemCameraKind(cameraId, kind); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 384 | void CameraService::updateCameraNumAndIds() { |
| 385 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 386 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); |
| 387 | // Excludes hidden secure cameras |
| 388 | mNumberOfCameras = |
| 389 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; |
| 390 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 391 | mNormalDeviceIds = |
| 392 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 393 | filterAPI1SystemCameraLocked(mNormalDeviceIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 396 | void CameraService::filterSPerfClassCharacteristicsLocked() { |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 397 | // To claim to be S Performance primary cameras, the cameras must be |
| 398 | // backward compatible. So performance class primary camera Ids must be API1 |
| 399 | // compatible. |
| 400 | bool firstRearCameraSeen = false, firstFrontCameraSeen = false; |
| 401 | for (const auto& cameraId : mNormalDeviceIdsWithoutSystemCamera) { |
| 402 | int facing = -1; |
| 403 | int orientation = 0; |
Shuzhen Wang | f221e8d | 2022-12-15 13:26:29 -0800 | [diff] [blame] | 404 | int portraitRotation; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 405 | getDeviceVersion(cameraId, /*overrideToPortrait*/false, /*out*/&portraitRotation, |
Shuzhen Wang | f221e8d | 2022-12-15 13:26:29 -0800 | [diff] [blame] | 406 | /*out*/&facing, /*out*/&orientation); |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 407 | if (facing == -1) { |
| 408 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.c_str()); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | if ((facing == hardware::CAMERA_FACING_BACK && !firstRearCameraSeen) || |
| 413 | (facing == hardware::CAMERA_FACING_FRONT && !firstFrontCameraSeen)) { |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 414 | status_t res = mCameraProviderManager->filterSmallJpegSizes(cameraId); |
| 415 | if (res == OK) { |
| 416 | mPerfClassPrimaryCameraIds.insert(cameraId); |
| 417 | } else { |
| 418 | ALOGE("%s: Failed to filter small JPEG sizes for performance class primary " |
| 419 | "camera %s: %s(%d)", __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
| 420 | break; |
| 421 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 422 | |
| 423 | if (facing == hardware::CAMERA_FACING_BACK) { |
| 424 | firstRearCameraSeen = true; |
| 425 | } |
| 426 | if (facing == hardware::CAMERA_FACING_FRONT) { |
| 427 | firstFrontCameraSeen = true; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | if (firstRearCameraSeen && firstFrontCameraSeen) { |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 437 | void CameraService::addStates(const std::string& cameraId) { |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 438 | CameraResourceCost cost; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 439 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); |
| 440 | if (res != OK) { |
| 441 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); |
| 442 | return; |
| 443 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 444 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 445 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); |
| 446 | if (res != OK) { |
| 447 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); |
| 448 | return; |
| 449 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 450 | std::vector<std::string> physicalCameraIds; |
| 451 | mCameraProviderManager->isLogicalCamera(cameraId, &physicalCameraIds); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 452 | std::set<std::string> conflicting; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 453 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 454 | conflicting.emplace(cost.conflictingDevices[i]); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | { |
| 458 | Mutex::Autolock lock(mCameraStatesLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 459 | mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost.resourceCost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 460 | conflicting, deviceKind, physicalCameraIds)); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 463 | if (mFlashlight->hasFlashUnit(cameraId)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 464 | Mutex::Autolock al(mTorchStatusMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 465 | mTorchStatusMap.add(cameraId, TorchModeStatus::AVAILABLE_OFF); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 466 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 467 | broadcastTorchModeStatus(cameraId, TorchModeStatus::AVAILABLE_OFF, deviceKind); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 468 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 469 | |
| 470 | updateCameraNumAndIds(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 471 | logDeviceAdded(cameraId, "Device added"); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 474 | void CameraService::removeStates(const std::string& cameraId) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 475 | updateCameraNumAndIds(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 476 | if (mFlashlight->hasFlashUnit(cameraId)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 477 | Mutex::Autolock al(mTorchStatusMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 478 | mTorchStatusMap.removeItem(cameraId); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | { |
| 482 | Mutex::Autolock lock(mCameraStatesLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 483 | mCameraStates.erase(cameraId); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 487 | void CameraService::onDeviceStatusChanged(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 488 | CameraDeviceStatus newHalStatus) { |
| 489 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 490 | cameraId.c_str(), newHalStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 491 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 492 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 493 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 494 | std::shared_ptr<CameraState> state = getCameraState(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 495 | |
| 496 | if (state == nullptr) { |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 497 | if (newStatus == StatusInternal::PRESENT) { |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 498 | ALOGI("%s: Unknown camera ID %s, a new camera is added", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 499 | __FUNCTION__, cameraId.c_str()); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 500 | |
| 501 | // First add as absent to make sure clients are notified below |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 502 | addStates(cameraId); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 503 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 504 | updateStatus(newStatus, cameraId); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 505 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 506 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, cameraId.c_str()); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 507 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 508 | return; |
| 509 | } |
| 510 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 511 | StatusInternal oldStatus = state->getStatus(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 512 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 513 | if (oldStatus == newStatus) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 514 | 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] | 515 | return; |
| 516 | } |
| 517 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 518 | if (newStatus == StatusInternal::NOT_PRESENT) { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 519 | logDeviceRemoved(cameraId, fmt::format("Device status changed from {} to {}", |
| 520 | oldStatus, newStatus)); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 521 | |
| 522 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 523 | // to this device until the status changes |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 524 | updateStatus(StatusInternal::NOT_PRESENT, cameraId); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 525 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 526 | sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 527 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 528 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 529 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 530 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 531 | // Remove cached shim parameters |
| 532 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 533 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 534 | // Remove online as well as offline client from the list of active clients, |
| 535 | // if they are present |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 536 | clientToDisconnectOnline = removeClientLocked(cameraId); |
| 537 | clientToDisconnectOffline = removeClientLocked(kOfflineDevice + cameraId); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 538 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 539 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 540 | disconnectClient(cameraId, clientToDisconnectOnline); |
| 541 | disconnectClient(kOfflineDevice + cameraId, clientToDisconnectOffline); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 542 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 543 | removeStates(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 544 | } else { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 545 | if (oldStatus == StatusInternal::NOT_PRESENT) { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 546 | logDeviceAdded(cameraId, fmt::format("Device status changed from {} to {}", |
| 547 | oldStatus, newStatus)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 548 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 549 | updateStatus(newStatus, cameraId); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 550 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 551 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 552 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 553 | void CameraService::onDeviceStatusChanged(const std::string& id, |
| 554 | const std::string& physicalId, |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 555 | CameraDeviceStatus newHalStatus) { |
| 556 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 557 | __FUNCTION__, id.c_str(), physicalId.c_str(), newHalStatus); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 558 | |
| 559 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 560 | |
| 561 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 562 | |
| 563 | if (state == nullptr) { |
| 564 | ALOGE("%s: Physical camera id %s status change on a non-present ID %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 565 | __FUNCTION__, physicalId.c_str(), id.c_str()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 566 | return; |
| 567 | } |
| 568 | |
| 569 | StatusInternal logicalCameraStatus = state->getStatus(); |
| 570 | if (logicalCameraStatus != StatusInternal::PRESENT && |
| 571 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 572 | ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 573 | __FUNCTION__, physicalId.c_str(), newHalStatus, logicalCameraStatus); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
| 577 | bool updated = false; |
| 578 | if (newStatus == StatusInternal::PRESENT) { |
| 579 | updated = state->removeUnavailablePhysicalId(physicalId); |
| 580 | } else { |
| 581 | updated = state->addUnavailablePhysicalId(physicalId); |
| 582 | } |
| 583 | |
| 584 | if (updated) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 585 | std::string idCombo = id + " : " + physicalId; |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 586 | if (newStatus == StatusInternal::PRESENT) { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 587 | logDeviceAdded(idCombo, fmt::format("Device status changed to {}", newStatus)); |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 588 | } else { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 589 | logDeviceRemoved(idCombo, fmt::format("Device status changed to {}", newStatus)); |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 590 | } |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 591 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 592 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 593 | if (getSystemCameraKind(id, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 594 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 595 | return; |
| 596 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 597 | Mutex::Autolock lock(mStatusListenerLock); |
| 598 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 599 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 600 | listener->getListenerPid(), listener->getListenerUid())) { |
| 601 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 602 | id.c_str()); |
| 603 | continue; |
| 604 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 605 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 606 | mapToInterface(newStatus), id, physicalId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 607 | listener->handleBinderStatus(ret, |
| 608 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 609 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 610 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 611 | } |
| 612 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 615 | void CameraService::disconnectClient(const std::string& id, sp<BasicClient> clientToDisconnect) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 616 | if (clientToDisconnect.get() != nullptr) { |
| 617 | ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 618 | __FUNCTION__, id.c_str()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 619 | // Notify the client of disconnection |
| 620 | clientToDisconnect->notifyError( |
| 621 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 622 | CaptureResultExtras{}); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 623 | clientToDisconnect->disconnect(); |
| 624 | } |
| 625 | } |
| 626 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 627 | void CameraService::onTorchStatusChanged(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 628 | TorchModeStatus newStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 629 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 630 | status_t res = getSystemCameraKind(cameraId, &systemCameraKind); |
| 631 | if (res != OK) { |
| 632 | ALOGE("%s: Could not get system camera kind for camera id %s", __FUNCTION__, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 633 | cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 634 | return; |
| 635 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 636 | Mutex::Autolock al(mTorchStatusMutex); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 637 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Jayant Chowdhary | 46ef0f5 | 2021-10-05 14:36:13 -0700 | [diff] [blame] | 640 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 641 | void CameraService::onTorchStatusChanged(const std::string& cameraId, |
Jayant Chowdhary | 46ef0f5 | 2021-10-05 14:36:13 -0700 | [diff] [blame] | 642 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
| 643 | Mutex::Autolock al(mTorchStatusMutex); |
| 644 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
| 645 | } |
| 646 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 647 | void CameraService::broadcastTorchStrengthLevel(const std::string& cameraId, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 648 | int32_t newStrengthLevel) { |
| 649 | Mutex::Autolock lock(mStatusListenerLock); |
| 650 | for (auto& i : mListenerList) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 651 | auto ret = i->getListener()->onTorchStrengthLevelChanged(cameraId, newStrengthLevel); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 652 | i->handleBinderStatus(ret, |
| 653 | "%s: Failed to trigger onTorchStrengthLevelChanged for %d:%d: %d", __FUNCTION__, |
| 654 | i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 658 | void CameraService::onTorchStatusChangedLocked(const std::string& cameraId, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 659 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 660 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 661 | __FUNCTION__, cameraId.c_str(), newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 662 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 663 | TorchModeStatus status; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 664 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 665 | if (res) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 666 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 667 | __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 668 | return; |
| 669 | } |
| 670 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 671 | return; |
| 672 | } |
| 673 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 674 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 675 | if (res) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 676 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, |
| 677 | (uint32_t)newStatus, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 678 | return; |
| 679 | } |
| 680 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 681 | { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 682 | // Update battery life logging for flashlight |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 683 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 684 | auto iter = mTorchUidMap.find(cameraId); |
| 685 | if (iter != mTorchUidMap.end()) { |
| 686 | int oldUid = iter->second.second; |
| 687 | int newUid = iter->second.first; |
| 688 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 689 | if (oldUid != newUid) { |
| 690 | // 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] | 691 | if (status == TorchModeStatus::AVAILABLE_ON) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 692 | notifier.noteFlashlightOff(toString8(cameraId), oldUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 693 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 694 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 695 | notifier.noteFlashlightOn(toString8(cameraId), newUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 696 | } |
| 697 | iter->second.second = newUid; |
| 698 | } else { |
| 699 | // If the UID has not changed, log the status |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 700 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 701 | notifier.noteFlashlightOn(toString8(cameraId), oldUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 702 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 703 | notifier.noteFlashlightOff(toString8(cameraId), oldUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | } |
| 707 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 708 | broadcastTorchModeStatus(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 711 | static bool isAutomotiveDevice() { |
| 712 | // Checks the property ro.hardware.type and returns true if it is |
| 713 | // automotive. |
| 714 | char value[PROPERTY_VALUE_MAX] = {0}; |
| 715 | property_get("ro.hardware.type", value, ""); |
| 716 | return strncmp(value, "automotive", PROPERTY_VALUE_MAX) == 0; |
| 717 | } |
| 718 | |
Jyoti Bhayana | 5bdb5a6 | 2023-08-24 14:46:08 -0700 | [diff] [blame] | 719 | static bool isHeadlessSystemUserMode() { |
| 720 | // Checks if the device is running in headless system user mode |
| 721 | // by checking the property ro.fw.mu.headless_system_user. |
| 722 | char value[PROPERTY_VALUE_MAX] = {0}; |
| 723 | property_get("ro.fw.mu.headless_system_user", value, ""); |
| 724 | return strncmp(value, "true", PROPERTY_VALUE_MAX) == 0; |
| 725 | } |
| 726 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 727 | static bool isAutomotivePrivilegedClient(int32_t uid) { |
| 728 | // Returns false if this is not an automotive device type. |
| 729 | if (!isAutomotiveDevice()) |
| 730 | return false; |
| 731 | |
| 732 | // Returns true if the uid is AID_AUTOMOTIVE_EVS which is a |
| 733 | // privileged client uid used for safety critical use cases such as |
| 734 | // rear view and surround view. |
| 735 | return uid == AID_AUTOMOTIVE_EVS; |
| 736 | } |
| 737 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 738 | bool CameraService::isAutomotiveExteriorSystemCamera(const std::string& cam_id) const{ |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 739 | // Returns false if this is not an automotive device type. |
| 740 | if (!isAutomotiveDevice()) |
| 741 | return false; |
| 742 | |
| 743 | // Returns false if no camera id is provided. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 744 | if (cam_id.empty()) |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 745 | return false; |
| 746 | |
| 747 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 748 | if (getSystemCameraKind(cam_id, &systemCameraKind) != OK) { |
| 749 | // This isn't a known camera ID, so it's not a system camera. |
| 750 | ALOGE("%s: Unknown camera id %s, ", __FUNCTION__, cam_id.c_str()); |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | if (systemCameraKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
| 755 | ALOGE("%s: camera id %s is not a system camera", __FUNCTION__, cam_id.c_str()); |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | CameraMetadata cameraInfo; |
| 760 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 761 | cam_id, false, &cameraInfo, false); |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 762 | if (res != OK){ |
| 763 | ALOGE("%s: Not able to get camera characteristics for camera id %s",__FUNCTION__, |
| 764 | cam_id.c_str()); |
| 765 | return false; |
| 766 | } |
| 767 | |
| 768 | camera_metadata_entry auto_location = cameraInfo.find(ANDROID_AUTOMOTIVE_LOCATION); |
| 769 | if (auto_location.count != 1) |
| 770 | return false; |
| 771 | |
| 772 | uint8_t location = auto_location.data.u8[0]; |
| 773 | if ((location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_FRONT) && |
| 774 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_REAR) && |
| 775 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_LEFT) && |
| 776 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_RIGHT)) { |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | return true; |
| 781 | } |
| 782 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 783 | bool CameraService::checkPermission(const std::string& cameraId, const std::string& permission, |
| 784 | const AttributionSourceState& attributionSource, const std::string& message, |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 785 | int32_t attributedOpCode) const{ |
| 786 | if (isAutomotivePrivilegedClient(attributionSource.uid)) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 787 | // If cameraId is empty, then it means that this check is not used for the |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 788 | // purpose of accessing a specific camera, hence grant permission just |
| 789 | // based on uid to the automotive privileged client. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 790 | if (cameraId.empty()) |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 791 | return true; |
| 792 | // If this call is used for accessing a specific camera then cam_id must be provided. |
| 793 | // In that case, only pre-grants the permission for accessing the exterior system only |
| 794 | // camera. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 795 | return isAutomotiveExteriorSystemCamera(cameraId); |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 798 | permission::PermissionChecker permissionChecker; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 799 | return permissionChecker.checkPermissionForPreflight(toString16(permission), attributionSource, |
| 800 | toString16(message), attributedOpCode) |
| 801 | != permission::PermissionChecker::PERMISSION_HARD_DENIED; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 804 | bool CameraService::hasPermissionsForSystemCamera(const std::string& cameraId, int callingPid, |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 805 | int callingUid) const{ |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 806 | AttributionSourceState attributionSource{}; |
| 807 | attributionSource.pid = callingPid; |
| 808 | attributionSource.uid = callingUid; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 809 | bool checkPermissionForSystemCamera = checkPermission(cameraId, |
| 810 | sSystemCameraPermission, attributionSource, std::string(), AppOpsManager::OP_NONE); |
| 811 | bool checkPermissionForCamera = checkPermission(cameraId, |
| 812 | sCameraPermission, attributionSource, std::string(), AppOpsManager::OP_NONE); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 813 | return checkPermissionForSystemCamera && checkPermissionForCamera; |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Jyoti Bhayana | 5bdb5a6 | 2023-08-24 14:46:08 -0700 | [diff] [blame] | 816 | bool CameraService::hasPermissionsForCameraHeadlessSystemUser(const std::string& cameraId, |
| 817 | int callingPid, int callingUid) const{ |
| 818 | AttributionSourceState attributionSource{}; |
| 819 | attributionSource.pid = callingPid; |
| 820 | attributionSource.uid = callingUid; |
| 821 | return checkPermission(cameraId, sCameraHeadlessSystemUserPermission, attributionSource, |
| 822 | std::string(), AppOpsManager::OP_NONE); |
| 823 | } |
| 824 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 825 | bool CameraService::hasPermissionsForCameraPrivacyAllowlist(int callingPid, int callingUid) const{ |
| 826 | AttributionSourceState attributionSource{}; |
| 827 | attributionSource.pid = callingPid; |
| 828 | attributionSource.uid = callingUid; |
| 829 | return checkPermission(std::string(), sCameraPrivacyAllowlistPermission, attributionSource, |
| 830 | std::string(), AppOpsManager::OP_NONE); |
| 831 | } |
| 832 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 833 | Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 834 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 835 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 836 | bool hasSystemCameraPermissions = |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 837 | hasPermissionsForSystemCamera(std::string(), CameraThreadState::getCallingPid(), |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 838 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 839 | switch (type) { |
| 840 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 841 | if (hasSystemCameraPermissions) { |
| 842 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); |
| 843 | } else { |
| 844 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); |
| 845 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 846 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 847 | case CAMERA_TYPE_ALL: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 848 | if (hasSystemCameraPermissions) { |
| 849 | *numCameras = mNumberOfCameras; |
| 850 | } else { |
| 851 | *numCameras = mNumberOfCamerasWithoutSystemCamera; |
| 852 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 853 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 854 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 855 | ALOGW("%s: Unknown camera type %d", |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 856 | __FUNCTION__, type); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 857 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 858 | "Unknown camera type %d", type); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 859 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 860 | return Status::ok(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 861 | } |
| 862 | |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 863 | Status CameraService::remapCameraIds(const hardware::CameraIdRemapping& cameraIdRemapping) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 864 | if (!checkCallingPermission(toString16(sCameraInjectExternalCameraPermission))) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 865 | const int pid = CameraThreadState::getCallingPid(); |
| 866 | const int uid = CameraThreadState::getCallingUid(); |
| 867 | ALOGE("%s: Permission Denial: can't configure camera ID mapping pid=%d, uid=%d", |
| 868 | __FUNCTION__, pid, uid); |
| 869 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 870 | "Permission Denial: no permission to configure camera id mapping"); |
| 871 | } |
| 872 | TCameraIdRemapping cameraIdRemappingMap{}; |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 873 | binder::Status parseStatus = parseCameraIdRemapping(cameraIdRemapping, &cameraIdRemappingMap); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 874 | if (!parseStatus.isOk()) { |
| 875 | return parseStatus; |
| 876 | } |
| 877 | remapCameraIds(cameraIdRemappingMap); |
| 878 | return Status::ok(); |
| 879 | } |
| 880 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 881 | Status CameraService::createDefaultRequest(const std::string& unresolvedCameraId, int templateId, |
| 882 | /* out */ |
| 883 | hardware::camera2::impl::CameraMetadataNative* request) { |
| 884 | ATRACE_CALL(); |
| 885 | |
| 886 | if (!flags::feature_combination_query()) { |
| 887 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, |
| 888 | "Camera subsystem doesn't support this method!"); |
| 889 | } |
| 890 | if (!mInitialized) { |
| 891 | ALOGE("%s: Camera subsystem is not available", __FUNCTION__); |
| 892 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 893 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 894 | } |
| 895 | |
| 896 | const std::string cameraId = resolveCameraId(unresolvedCameraId, |
| 897 | CameraThreadState::getCallingUid()); |
| 898 | |
| 899 | binder::Status res; |
| 900 | if (request == nullptr) { |
| 901 | res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, |
| 902 | "Camera %s: Error creating default request", cameraId.c_str()); |
| 903 | return res; |
| 904 | } |
| 905 | camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT; |
| 906 | res = SessionConfigurationUtils::mapRequestTemplateFromClient( |
| 907 | cameraId, templateId, &tempId); |
| 908 | if (!res.isOk()) { |
| 909 | ALOGE("%s: Camera %s: failed to map request Template %d", |
| 910 | __FUNCTION__, cameraId.c_str(), templateId); |
| 911 | return res; |
| 912 | } |
| 913 | |
| 914 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 915 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to create default" |
| 916 | "request for system only device %s: ", cameraId.c_str()); |
| 917 | } |
| 918 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 919 | CameraMetadata metadata; |
| 920 | status_t err = mCameraProviderManager->createDefaultRequest(cameraId, tempId, &metadata); |
| 921 | if (err == OK) { |
| 922 | request->swap(metadata); |
| 923 | } else if (err == BAD_VALUE) { |
| 924 | res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT, |
| 925 | "Camera %s: Template ID %d is invalid or not supported: %s (%d)", |
| 926 | cameraId.c_str(), templateId, strerror(-err), err); |
| 927 | } else { |
| 928 | res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, |
| 929 | "Camera %s: Error creating default request for template %d: %s (%d)", |
| 930 | cameraId.c_str(), templateId, strerror(-err), err); |
| 931 | } |
| 932 | return res; |
| 933 | } |
| 934 | |
| 935 | Status CameraService::isSessionConfigurationWithParametersSupported( |
| 936 | const std::string& unresolvedCameraId, |
| 937 | const SessionConfiguration& sessionConfiguration, |
| 938 | /*out*/ |
| 939 | bool* supported) { |
| 940 | ATRACE_CALL(); |
| 941 | |
| 942 | if (!flags::feature_combination_query()) { |
| 943 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, |
| 944 | "Camera subsystem doesn't support this method!"); |
| 945 | } |
| 946 | if (!mInitialized) { |
| 947 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 948 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 949 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 950 | } |
| 951 | |
| 952 | const std::string cameraId = resolveCameraId(unresolvedCameraId, |
| 953 | CameraThreadState::getCallingUid()); |
| 954 | if (supported == nullptr) { |
| 955 | std::string msg = fmt::sprintf("Camera %s: Invalid 'support' input!", |
| 956 | unresolvedCameraId.c_str()); |
| 957 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 958 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 959 | } |
| 960 | |
| 961 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 962 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query " |
| 963 | "session configuration with parameters support for system only device %s: ", |
| 964 | cameraId.c_str()); |
| 965 | } |
| 966 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 967 | *supported = false; |
| 968 | status_t ret = mCameraProviderManager->isSessionConfigurationSupported(cameraId.c_str(), |
| 969 | sessionConfiguration, /*mOverrideForPerfClass*/false, /*checkSessionParams*/true, |
| 970 | supported); |
| 971 | binder::Status res; |
| 972 | switch (ret) { |
| 973 | case OK: |
| 974 | // Expected, do nothing. |
| 975 | break; |
| 976 | case INVALID_OPERATION: { |
| 977 | std::string msg = fmt::sprintf( |
| 978 | "Camera %s: Session configuration query not supported!", |
| 979 | cameraId.c_str()); |
| 980 | ALOGD("%s: %s", __FUNCTION__, msg.c_str()); |
| 981 | res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
| 982 | } |
| 983 | |
| 984 | break; |
| 985 | default: { |
| 986 | std::string msg = fmt::sprintf( "Camera %s: Error: %s (%d)", cameraId.c_str(), |
| 987 | strerror(-ret), ret); |
| 988 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 989 | res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, |
| 990 | msg.c_str()); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | return res; |
| 995 | } |
| 996 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 997 | Status CameraService::getSessionCharacteristics(const std::string& unresolvedCameraId, |
| 998 | int targetSdkVersion, bool overrideToPortrait, |
| 999 | const SessionConfiguration& sessionConfiguration, |
| 1000 | /*out*/ CameraMetadata* outMetadata) { |
| 1001 | ATRACE_CALL(); |
| 1002 | |
| 1003 | if (!mInitialized) { |
| 1004 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 1005 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 1006 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 1007 | } |
| 1008 | |
| 1009 | const std::string cameraId = |
| 1010 | resolveCameraId(unresolvedCameraId, CameraThreadState::getCallingUid()); |
| 1011 | |
| 1012 | if (outMetadata == nullptr) { |
| 1013 | std::string msg = |
| 1014 | fmt::sprintf("Camera %s: Invalid 'outMetadata' input!", unresolvedCameraId.c_str()); |
| 1015 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1016 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1017 | } |
| 1018 | |
| 1019 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
| 1020 | mPerfClassPrimaryCameraIds, cameraId, targetSdkVersion); |
| 1021 | |
| 1022 | status_t ret = mCameraProviderManager->getSessionCharacteristics( |
| 1023 | cameraId, sessionConfiguration, overrideForPerfClass, overrideToPortrait, outMetadata); |
| 1024 | |
| 1025 | // TODO(b/303645857): Remove fingerprintable metadata if the caller process does not have |
| 1026 | // camera access permission. |
| 1027 | |
| 1028 | Status res = Status::ok(); |
| 1029 | switch (ret) { |
| 1030 | case OK: |
| 1031 | // Expected, no handling needed. |
| 1032 | break; |
| 1033 | case INVALID_OPERATION: { |
| 1034 | std::string msg = fmt::sprintf( |
| 1035 | "Camera %s: Session characteristics query not supported!", |
| 1036 | cameraId.c_str()); |
| 1037 | ALOGD("%s: %s", __FUNCTION__, msg.c_str()); |
| 1038 | res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
| 1039 | } |
| 1040 | break; |
| 1041 | default: { |
| 1042 | std::string msg = fmt::sprintf("Camera %s: Error: %s (%d)", cameraId.c_str(), |
| 1043 | strerror(-ret), ret); |
| 1044 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1045 | res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | return res; |
| 1050 | } |
| 1051 | |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1052 | Status CameraService::parseCameraIdRemapping( |
| 1053 | const hardware::CameraIdRemapping& cameraIdRemapping, |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1054 | /* out */ TCameraIdRemapping* cameraIdRemappingMap) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1055 | std::string packageName; |
| 1056 | std::string cameraIdToReplace, updatedCameraId; |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1057 | for(const auto& packageIdRemapping: cameraIdRemapping.packageIdRemappings) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1058 | packageName = packageIdRemapping.packageName; |
malikakash | 0894f5b | 2023-08-10 22:46:47 +0000 | [diff] [blame] | 1059 | if (packageName.empty()) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1060 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1061 | "CameraIdRemapping: Package name cannot be empty"); |
| 1062 | } |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1063 | if (packageIdRemapping.cameraIdsToReplace.size() |
| 1064 | != packageIdRemapping.updatedCameraIds.size()) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1065 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1066 | "CameraIdRemapping: Mismatch in CameraId Remapping lists sizes for package %s", |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1067 | packageName.c_str()); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1068 | } |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1069 | for(size_t i = 0; i < packageIdRemapping.cameraIdsToReplace.size(); i++) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1070 | cameraIdToReplace = packageIdRemapping.cameraIdsToReplace[i]; |
| 1071 | updatedCameraId = packageIdRemapping.updatedCameraIds[i]; |
malikakash | 0894f5b | 2023-08-10 22:46:47 +0000 | [diff] [blame] | 1072 | if (cameraIdToReplace.empty() || updatedCameraId.empty()) { |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1073 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1074 | "CameraIdRemapping: Camera Id cannot be empty for package %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1075 | packageName.c_str()); |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1076 | } |
| 1077 | if (cameraIdToReplace == updatedCameraId) { |
| 1078 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1079 | "CameraIdRemapping: CameraIdToReplace cannot be the same" |
| 1080 | " as updatedCameraId for %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1081 | packageName.c_str()); |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1082 | } |
| 1083 | (*cameraIdRemappingMap)[packageName][cameraIdToReplace] = updatedCameraId; |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | return Status::ok(); |
| 1087 | } |
| 1088 | |
| 1089 | void CameraService::remapCameraIds(const TCameraIdRemapping& cameraIdRemapping) { |
| 1090 | // Acquire mServiceLock and prevent other clients from connecting |
| 1091 | std::unique_ptr<AutoConditionLock> serviceLockWrapper = |
| 1092 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 1093 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1094 | // Collect all existing clients for camera Ids that are being |
| 1095 | // remapped in the new cameraIdRemapping, but only if they were being used by a |
| 1096 | // targeted packageName. |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1097 | std::vector<sp<BasicClient>> clientsToDisconnect; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1098 | std::vector<std::string> cameraIdsToUpdate; |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1099 | for (const auto& [packageName, injectionMap] : cameraIdRemapping) { |
| 1100 | for (auto& [id0, id1] : injectionMap) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1101 | ALOGI("%s: UPDATE:= %s: %s: %s", __FUNCTION__, packageName.c_str(), |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1102 | id0.c_str(), id1.c_str()); |
| 1103 | auto clientDescriptor = mActiveClientManager.get(id0); |
| 1104 | if (clientDescriptor != nullptr) { |
| 1105 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1106 | if (clientSp->getPackageName() == packageName) { |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1107 | // This camera is being used by a targeted packageName and |
| 1108 | // being remapped to a new camera Id. We should disconnect it. |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1109 | clientsToDisconnect.push_back(clientSp); |
| 1110 | cameraIdsToUpdate.push_back(id0); |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1116 | for (auto& clientSp : clientsToDisconnect) { |
malikakash | f4c80f2 | 2023-09-25 21:50:28 +0000 | [diff] [blame] | 1117 | // Notify the clients about the disconnection. |
| 1118 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1119 | CaptureResultExtras{}); |
| 1120 | } |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1121 | |
| 1122 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 1123 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 1124 | mServiceLock.unlock(); |
| 1125 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1126 | // Clear calling identity for disconnect() PID checks. |
| 1127 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 1128 | |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1129 | // Disconnect clients. |
| 1130 | for (auto& clientSp : clientsToDisconnect) { |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1131 | // This also triggers a call to updateStatus() which also reads mCameraIdRemapping |
| 1132 | // and requires mCameraIdRemappingLock. |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1133 | clientSp->disconnect(); |
| 1134 | } |
| 1135 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1136 | // Invoke destructors (which call disconnect()) now while we don't hold the mServiceLock. |
| 1137 | clientsToDisconnect.clear(); |
| 1138 | |
| 1139 | CameraThreadState::restoreCallingIdentity(token); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1140 | mServiceLock.lock(); |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1141 | |
| 1142 | { |
| 1143 | Mutex::Autolock lock(mCameraIdRemappingLock); |
| 1144 | // Update mCameraIdRemapping. |
| 1145 | mCameraIdRemapping.clear(); |
| 1146 | mCameraIdRemapping.insert(cameraIdRemapping.begin(), cameraIdRemapping.end()); |
| 1147 | } |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1150 | Status CameraService::injectSessionParams( |
| 1151 | const std::string& cameraId, |
| 1152 | const CameraMetadata& sessionParams) { |
| 1153 | if (!checkCallingPermission(toString16(sCameraInjectExternalCameraPermission))) { |
| 1154 | const int pid = CameraThreadState::getCallingPid(); |
| 1155 | const int uid = CameraThreadState::getCallingUid(); |
| 1156 | ALOGE("%s: Permission Denial: can't inject session params pid=%d, uid=%d", |
| 1157 | __FUNCTION__, pid, uid); |
| 1158 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 1159 | "Permission Denial: no permission to inject session params"); |
| 1160 | } |
| 1161 | |
| 1162 | std::unique_ptr<AutoConditionLock> serviceLockWrapper = |
| 1163 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 1164 | |
| 1165 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 1166 | if (clientDescriptor == nullptr) { |
| 1167 | ALOGI("%s: No active client for camera id %s", __FUNCTION__, cameraId.c_str()); |
| 1168 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1169 | "No active client for camera id %s", cameraId.c_str()); |
| 1170 | } |
| 1171 | |
| 1172 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1173 | status_t res = clientSp->injectSessionParams(sessionParams); |
| 1174 | |
| 1175 | if (res != OK) { |
| 1176 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1177 | "Error injecting session params into camera \"%s\": %s (%d)", |
| 1178 | cameraId.c_str(), strerror(-res), res); |
| 1179 | } |
| 1180 | return Status::ok(); |
| 1181 | } |
| 1182 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1183 | std::vector<std::string> CameraService::findOriginalIdsForRemappedCameraId( |
| 1184 | const std::string& inputCameraId, int clientUid) { |
| 1185 | std::string packageName = getPackageNameFromUid(clientUid); |
| 1186 | std::vector<std::string> cameraIds; |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1187 | Mutex::Autolock lock(mCameraIdRemappingLock); |
| 1188 | if (auto packageMapIter = mCameraIdRemapping.find(packageName); |
| 1189 | packageMapIter != mCameraIdRemapping.end()) { |
| 1190 | for (auto& [id0, id1]: packageMapIter->second) { |
| 1191 | if (id1 == inputCameraId) { |
| 1192 | cameraIds.push_back(id0); |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | return cameraIds; |
| 1197 | } |
| 1198 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1199 | std::string CameraService::resolveCameraId( |
| 1200 | const std::string& inputCameraId, |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1201 | int clientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1202 | const std::string& packageName) { |
| 1203 | std::string packageNameVal = packageName; |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1204 | if (packageName.empty()) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1205 | packageNameVal = getPackageNameFromUid(clientUid); |
| 1206 | } |
malikakash | 65d2069 | 2023-09-07 01:06:33 +0000 | [diff] [blame] | 1207 | if (clientUid < AID_APP_START || packageNameVal.empty()) { |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1208 | // We shouldn't remap cameras for processes with system/vendor UIDs. |
| 1209 | return inputCameraId; |
| 1210 | } |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1211 | Mutex::Autolock lock(mCameraIdRemappingLock); |
| 1212 | if (auto packageMapIter = mCameraIdRemapping.find(packageNameVal); |
| 1213 | packageMapIter != mCameraIdRemapping.end()) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1214 | auto packageMap = packageMapIter->second; |
| 1215 | if (auto replacementIdIter = packageMap.find(inputCameraId); |
| 1216 | replacementIdIter != packageMap.end()) { |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1217 | ALOGI("%s: resolveCameraId: remapping cameraId %s for %s to %s", |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1218 | __FUNCTION__, inputCameraId.c_str(), |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1219 | packageNameVal.c_str(), |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1220 | replacementIdIter->second.c_str()); |
| 1221 | return replacementIdIter->second; |
| 1222 | } |
| 1223 | } |
| 1224 | return inputCameraId; |
| 1225 | } |
| 1226 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1227 | Status CameraService::getCameraInfo(int cameraId, bool overrideToPortrait, |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1228 | CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1229 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 1230 | Mutex::Autolock l(mServiceLock); |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1231 | std::string unresolvedCameraId = cameraIdIntToStrLocked(cameraId); |
| 1232 | std::string cameraIdStr = resolveCameraId( |
| 1233 | unresolvedCameraId, CameraThreadState::getCallingUid()); |
| 1234 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1235 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1236 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 1237 | "characteristics for system only device %s: ", cameraIdStr.c_str()); |
| 1238 | } |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 1239 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1240 | if (!mInitialized) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1241 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1242 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1243 | "Camera subsystem is not available"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1244 | } |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1245 | bool hasSystemCameraPermissions = hasPermissionsForSystemCamera(std::to_string(cameraId), |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1246 | CameraThreadState::getCallingPid(), CameraThreadState::getCallingUid()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1247 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; |
| 1248 | if (hasSystemCameraPermissions) { |
| 1249 | cameraIdBound = mNumberOfCameras; |
| 1250 | } |
| 1251 | if (cameraId < 0 || cameraId >= cameraIdBound) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1252 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1253 | "CameraId is not valid"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1256 | Status ret = Status::ok(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1257 | int portraitRotation; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1258 | status_t err = mCameraProviderManager->getCameraInfo( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1259 | cameraIdStr, overrideToPortrait, &portraitRotation, cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1260 | if (err != OK) { |
| 1261 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1262 | "Error retrieving camera info from device %d: %s (%d)", cameraId, |
| 1263 | strerror(-err), err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1264 | logServiceError(std::string("Error retrieving camera info from device ") |
| 1265 | + std::to_string(cameraId), ERROR_INVALID_OPERATION); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1266 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1267 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1268 | return ret; |
| 1269 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1270 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1271 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1272 | const std::vector<std::string> *deviceIds = &mNormalDeviceIdsWithoutSystemCamera; |
| 1273 | auto callingPid = CameraThreadState::getCallingPid(); |
| 1274 | auto callingUid = CameraThreadState::getCallingUid(); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1275 | AttributionSourceState attributionSource{}; |
| 1276 | attributionSource.pid = callingPid; |
| 1277 | attributionSource.uid = callingUid; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1278 | bool checkPermissionForSystemCamera = checkPermission(std::to_string(cameraIdInt), |
| 1279 | sSystemCameraPermission, attributionSource, std::string(), |
| 1280 | AppOpsManager::OP_NONE); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1281 | if (checkPermissionForSystemCamera || getpid() == callingPid) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1282 | deviceIds = &mNormalDeviceIds; |
| 1283 | } |
| 1284 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(deviceIds->size())) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1285 | ALOGE("%s: input id %d invalid: valid range (0, %zu)", |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1286 | __FUNCTION__, cameraIdInt, deviceIds->size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1287 | return std::string{}; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1288 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1289 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1290 | return (*deviceIds)[cameraIdInt]; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1293 | std::string CameraService::cameraIdIntToStr(int cameraIdInt) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1294 | Mutex::Autolock lock(mServiceLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1295 | return cameraIdIntToStrLocked(cameraIdInt); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1296 | } |
| 1297 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1298 | Status CameraService::getCameraCharacteristics(const std::string& unresolvedCameraId, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1299 | int targetSdkVersion, bool overrideToPortrait, CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1300 | ATRACE_CALL(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1301 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1302 | const std::string cameraId = resolveCameraId(unresolvedCameraId, |
| 1303 | CameraThreadState::getCallingUid()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1304 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1305 | if (!cameraInfo) { |
| 1306 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1307 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1310 | if (!mInitialized) { |
| 1311 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1312 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1313 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1314 | "Camera subsystem is not available");; |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1317 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1318 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1319 | "characteristics for system only device %s: ", cameraId.c_str()); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1322 | Status ret{}; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1323 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1324 | bool overrideForPerfClass = |
| 1325 | SessionConfigurationUtils::targetPerfClassPrimaryCamera(mPerfClassPrimaryCameraIds, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1326 | cameraId, targetSdkVersion); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1327 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1328 | cameraId, overrideForPerfClass, cameraInfo, overrideToPortrait); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1329 | if (res != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1330 | if (res == NAME_NOT_FOUND) { |
| 1331 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1332 | "characteristics for unknown device %s: %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1333 | strerror(-res), res); |
| 1334 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1335 | logServiceError(fmt::sprintf("Unable to retrieve camera characteristics for device %s.", |
| 1336 | cameraId.c_str()), ERROR_INVALID_OPERATION); |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1337 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1338 | "characteristics for device %s: %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1339 | strerror(-res), res); |
| 1340 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1341 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1342 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1343 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 1344 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1345 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera kind " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1346 | "for device %s", cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1347 | } |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1348 | int callingPid = CameraThreadState::getCallingPid(); |
| 1349 | int callingUid = CameraThreadState::getCallingUid(); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 1350 | std::vector<int32_t> tagsRemoved; |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1351 | // If it's not calling from cameraserver, check the permission only if |
| 1352 | // android.permission.CAMERA is required. If android.permission.SYSTEM_CAMERA was needed, |
| 1353 | // it would've already been checked in shouldRejectSystemCameraConnection. |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1354 | AttributionSourceState attributionSource{}; |
| 1355 | attributionSource.pid = callingPid; |
| 1356 | attributionSource.uid = callingUid; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1357 | bool checkPermissionForCamera = checkPermission(cameraId, sCameraPermission, |
| 1358 | attributionSource, std::string(), AppOpsManager::OP_NONE); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 1359 | if ((callingPid != getpid()) && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1360 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1361 | !checkPermissionForCamera) { |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 1362 | res = cameraInfo->removePermissionEntries( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1363 | mCameraProviderManager->getProviderTagIdLocked(cameraId), |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 1364 | &tagsRemoved); |
| 1365 | if (res != OK) { |
| 1366 | cameraInfo->clear(); |
| 1367 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to remove camera" |
| 1368 | " characteristics needing camera permission for device %s: %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1369 | cameraId.c_str(), strerror(-res), res); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | if (!tagsRemoved.empty()) { |
| 1374 | res = cameraInfo->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, |
| 1375 | tagsRemoved.data(), tagsRemoved.size()); |
| 1376 | if (res != OK) { |
| 1377 | cameraInfo->clear(); |
| 1378 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to insert camera " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1379 | "keys needing permission for device %s: %s (%d)", cameraId.c_str(), |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 1380 | strerror(-res), res); |
| 1381 | } |
| 1382 | } |
| 1383 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1384 | return ret; |
| 1385 | } |
| 1386 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1387 | Status CameraService::getTorchStrengthLevel(const std::string& unresolvedCameraId, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1388 | int32_t* torchStrength) { |
| 1389 | ATRACE_CALL(); |
| 1390 | Mutex::Autolock l(mServiceLock); |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1391 | const std::string cameraId = resolveCameraId( |
malikakash | 65d2069 | 2023-09-07 01:06:33 +0000 | [diff] [blame] | 1392 | unresolvedCameraId, CameraThreadState::getCallingUid()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1393 | if (!mInitialized) { |
| 1394 | ALOGE("%s: Camera HAL couldn't be initialized.", __FUNCTION__); |
| 1395 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera HAL couldn't be initialized."); |
| 1396 | } |
| 1397 | |
| 1398 | if(torchStrength == NULL) { |
| 1399 | ALOGE("%s: strength level must not be null.", __FUNCTION__); |
| 1400 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Strength level should not be null."); |
| 1401 | } |
| 1402 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1403 | status_t res = mCameraProviderManager->getTorchStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1404 | if (res != OK) { |
| 1405 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve torch " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1406 | "strength level for device %s: %s (%d)", cameraId.c_str(), |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1407 | strerror(-res), res); |
| 1408 | } |
| 1409 | ALOGI("%s: Torch strength level is: %d", __FUNCTION__, *torchStrength); |
| 1410 | return Status::ok(); |
| 1411 | } |
| 1412 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1413 | std::string CameraService::getFormattedCurrentTime() { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1414 | time_t now = time(nullptr); |
| 1415 | char formattedTime[64]; |
| 1416 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1417 | return std::string(formattedTime); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1420 | Status CameraService::getCameraVendorTagDescriptor( |
| 1421 | /*out*/ |
| 1422 | hardware::camera2::params::VendorTagDescriptor* desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1423 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1424 | if (!mInitialized) { |
| 1425 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1426 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 1427 | } |
Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 1428 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 1429 | if (globalDescriptor != nullptr) { |
| 1430 | *desc = *(globalDescriptor.get()); |
| 1431 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1432 | return Status::ok(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 1433 | } |
| 1434 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 1435 | Status CameraService::getCameraVendorTagCache( |
| 1436 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { |
| 1437 | ATRACE_CALL(); |
| 1438 | if (!mInitialized) { |
| 1439 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 1440 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1441 | "Camera subsystem not available"); |
| 1442 | } |
| 1443 | sp<VendorTagDescriptorCache> globalCache = |
| 1444 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 1445 | if (globalCache != nullptr) { |
| 1446 | *cache = *(globalCache.get()); |
| 1447 | } |
| 1448 | return Status::ok(); |
| 1449 | } |
| 1450 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 1451 | void CameraService::clearCachedVariables() { |
| 1452 | BasicClient::BasicClient::sCameraService = nullptr; |
| 1453 | } |
| 1454 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1455 | std::pair<int, IPCTransport> CameraService::getDeviceVersion(const std::string& cameraId, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1456 | bool overrideToPortrait, int* portraitRotation, int* facing, int* orientation) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1457 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1458 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1459 | int deviceVersion = 0; |
| 1460 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1461 | status_t res; |
| 1462 | hardware::hidl_version maxVersion{0,0}; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1463 | IPCTransport transport = IPCTransport::INVALID; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1464 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId, &maxVersion, &transport); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1465 | if (res != OK || transport == IPCTransport::INVALID) { |
| 1466 | ALOGE("%s: Unable to get highest supported version for camera id %s", __FUNCTION__, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1467 | cameraId.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1468 | return std::make_pair(-1, IPCTransport::INVALID) ; |
| 1469 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1470 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1471 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1472 | hardware::CameraInfo info; |
| 1473 | if (facing) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1474 | res = mCameraProviderManager->getCameraInfo(cameraId, overrideToPortrait, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1475 | portraitRotation, &info); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1476 | if (res != OK) { |
| 1477 | return std::make_pair(-1, IPCTransport::INVALID); |
| 1478 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1479 | *facing = info.facing; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1480 | if (orientation) { |
| 1481 | *orientation = info.orientation; |
| 1482 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1483 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1484 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1485 | return std::make_pair(deviceVersion, transport); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1488 | Status CameraService::filterGetInfoErrorCode(status_t err) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1489 | switch(err) { |
| 1490 | case NO_ERROR: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1491 | return Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1492 | case BAD_VALUE: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1493 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1494 | "CameraId is not valid for HAL module"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1495 | case NO_INIT: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1496 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1497 | "Camera device not available"); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1498 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1499 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1500 | "Camera HAL encountered error %d: %s", |
| 1501 | err, strerror(-err)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1502 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1503 | } |
| 1504 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1505 | Status CameraService::makeClient(const sp<CameraService>& cameraService, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1506 | const sp<IInterface>& cameraCb, const std::string& packageName, bool systemNativeClient, |
| 1507 | const std::optional<std::string>& featureId, const std::string& cameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 1508 | int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid, |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1509 | int servicePid, std::pair<int, IPCTransport> deviceVersionAndTransport, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1510 | apiLevel effectiveApiLevel, bool overrideForPerfClass, bool overrideToPortrait, |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1511 | bool forceSlowJpegMode, const std::string& originalCameraId, |
| 1512 | /*out*/sp<BasicClient>* client) { |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1513 | // For HIDL devices |
| 1514 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 1515 | // Create CameraClient based on device version reported by the HAL. |
| 1516 | int deviceVersion = deviceVersionAndTransport.first; |
| 1517 | switch(deviceVersion) { |
| 1518 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 1519 | ALOGE("Camera using old HAL version: %d", deviceVersion); |
| 1520 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, |
| 1521 | "Camera device \"%s\" HAL version %d no longer supported", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1522 | cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1523 | break; |
| 1524 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 1525 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 1526 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 1527 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 1528 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 1529 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 1530 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 1531 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 1532 | break; |
| 1533 | default: |
| 1534 | // Should not be reachable |
| 1535 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 1536 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1537 | "Camera device \"%s\" has unknown HAL version %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1538 | cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1539 | } |
| 1540 | } |
| 1541 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 1542 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1543 | *client = new Camera2Client(cameraService, tmp, cameraService->mCameraServiceProxyWrapper, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1544 | packageName, featureId, cameraId, |
| 1545 | api1CameraId, facing, sensorOrientation, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1546 | clientPid, clientUid, servicePid, overrideForPerfClass, overrideToPortrait, |
| 1547 | forceSlowJpegMode); |
| 1548 | ALOGI("%s: Camera1 API (legacy), override to portrait %d, forceSlowJpegMode %d", |
| 1549 | __FUNCTION__, overrideToPortrait, forceSlowJpegMode); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1550 | } else { // Camera2 API route |
| 1551 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = |
| 1552 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1553 | *client = new CameraDeviceClient(cameraService, tmp, |
| 1554 | cameraService->mCameraServiceProxyWrapper, packageName, systemNativeClient, |
| 1555 | featureId, cameraId, facing, sensorOrientation, clientPid, clientUid, servicePid, |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1556 | overrideForPerfClass, overrideToPortrait, originalCameraId); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1557 | ALOGI("%s: Camera2 API, override to portrait %d", __FUNCTION__, overrideToPortrait); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1558 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1559 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1560 | } |
| 1561 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1562 | std::string CameraService::toString(std::set<userid_t> intSet) { |
| 1563 | std::ostringstream s; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1564 | bool first = true; |
| 1565 | for (userid_t i : intSet) { |
| 1566 | if (first) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1567 | s << std::to_string(i); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1568 | first = false; |
| 1569 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1570 | s << ", " << std::to_string(i); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1571 | } |
| 1572 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1573 | return std::move(s.str()); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1576 | int32_t CameraService::mapToInterface(TorchModeStatus status) { |
| 1577 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1578 | switch (status) { |
| 1579 | case TorchModeStatus::NOT_AVAILABLE: |
| 1580 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1581 | break; |
| 1582 | case TorchModeStatus::AVAILABLE_OFF: |
| 1583 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 1584 | break; |
| 1585 | case TorchModeStatus::AVAILABLE_ON: |
| 1586 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
| 1587 | break; |
| 1588 | default: |
| 1589 | ALOGW("Unknown new flash status: %d", status); |
| 1590 | } |
| 1591 | return serviceStatus; |
| 1592 | } |
| 1593 | |
| 1594 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { |
| 1595 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; |
| 1596 | switch (status) { |
| 1597 | case CameraDeviceStatus::NOT_PRESENT: |
| 1598 | serviceStatus = StatusInternal::NOT_PRESENT; |
| 1599 | break; |
| 1600 | case CameraDeviceStatus::PRESENT: |
| 1601 | serviceStatus = StatusInternal::PRESENT; |
| 1602 | break; |
| 1603 | case CameraDeviceStatus::ENUMERATING: |
| 1604 | serviceStatus = StatusInternal::ENUMERATING; |
| 1605 | break; |
| 1606 | default: |
| 1607 | ALOGW("Unknown new HAL device status: %d", status); |
| 1608 | } |
| 1609 | return serviceStatus; |
| 1610 | } |
| 1611 | |
| 1612 | int32_t CameraService::mapToInterface(StatusInternal status) { |
| 1613 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1614 | switch (status) { |
| 1615 | case StatusInternal::NOT_PRESENT: |
| 1616 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1617 | break; |
| 1618 | case StatusInternal::PRESENT: |
| 1619 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; |
| 1620 | break; |
| 1621 | case StatusInternal::ENUMERATING: |
| 1622 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; |
| 1623 | break; |
| 1624 | case StatusInternal::NOT_AVAILABLE: |
| 1625 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; |
| 1626 | break; |
| 1627 | case StatusInternal::UNKNOWN: |
| 1628 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; |
| 1629 | break; |
| 1630 | default: |
| 1631 | ALOGW("Unknown new internal device status: %d", status); |
| 1632 | } |
| 1633 | return serviceStatus; |
| 1634 | } |
| 1635 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1636 | Status CameraService::initializeShimMetadata(int cameraId) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1637 | int uid = CameraThreadState::getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1638 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1639 | std::string cameraIdStr = std::to_string(cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1640 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1641 | sp<Client> tmp = nullptr; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1642 | if (!(ret = connectHelper<ICameraClient,Client>( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1643 | sp<ICameraClient>{nullptr}, cameraIdStr, cameraId, |
| 1644 | kServiceName, /*systemNativeClient*/ false, {}, uid, USE_CALLING_PID, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1645 | API_1, /*shimUpdateOnly*/ true, /*oomScoreOffset*/ 0, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1646 | /*targetSdkVersion*/ __ANDROID_API_FUTURE__, /*overrideToPortrait*/ true, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1647 | /*forceSlowJpegMode*/false, cameraIdStr, /*out*/ tmp) |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1648 | ).isOk()) { |
Tomasz Wasilczyk | 12b04a5 | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 1649 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().c_str()); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1650 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1651 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1654 | Status CameraService::getLegacyParametersLazy(int cameraId, |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1655 | /*out*/ |
| 1656 | CameraParameters* parameters) { |
| 1657 | |
| 1658 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 1659 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1660 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1661 | |
| 1662 | if (parameters == NULL) { |
| 1663 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1664 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1667 | std::string unresolvedCameraId = std::to_string(cameraId); |
| 1668 | std::string cameraIdStr = resolveCameraId(unresolvedCameraId, |
| 1669 | CameraThreadState::getCallingUid()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1670 | |
| 1671 | // Check if we already have parameters |
| 1672 | { |
| 1673 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1674 | Mutex::Autolock lock(mServiceLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1675 | auto cameraState = getCameraState(cameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1676 | if (cameraState == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1677 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, cameraIdStr.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1678 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1679 | "Invalid camera ID: %s", cameraIdStr.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1680 | } |
| 1681 | CameraParameters p = cameraState->getShimParams(); |
| 1682 | if (!p.isEmpty()) { |
| 1683 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1684 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1685 | } |
| 1686 | } |
| 1687 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1688 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1689 | ret = initializeShimMetadata(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1690 | CameraThreadState::restoreCallingIdentity(token); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1691 | if (!ret.isOk()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1692 | // Error already logged by callee |
| 1693 | return ret; |
| 1694 | } |
| 1695 | |
| 1696 | // Check for parameters again |
| 1697 | { |
| 1698 | // Scope for service lock |
| 1699 | Mutex::Autolock lock(mServiceLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1700 | auto cameraState = getCameraState(cameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1701 | if (cameraState == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1702 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, cameraIdStr.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1703 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1704 | "Invalid camera ID: %s", cameraIdStr.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1705 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1706 | CameraParameters p = cameraState->getShimParams(); |
| 1707 | if (!p.isEmpty()) { |
| 1708 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1709 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1713 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 1714 | __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1715 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1718 | // Can camera service trust the caller based on the calling UID? |
| 1719 | static bool isTrustedCallingUid(uid_t uid) { |
| 1720 | switch (uid) { |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1721 | case AID_MEDIA: // mediaserver |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1722 | case AID_CAMERASERVER: // cameraserver |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1723 | case AID_RADIO: // telephony |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1724 | return true; |
| 1725 | default: |
| 1726 | return false; |
| 1727 | } |
| 1728 | } |
| 1729 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1730 | static status_t getUidForPackage(const std::string &packageName, int userId, /*inout*/uid_t& uid, |
| 1731 | int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 1732 | PermissionController pc; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1733 | uid = pc.getPackageUid(toString16(packageName), 0); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 1734 | if (uid <= 0) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1735 | ALOGE("Unknown package: '%s'", packageName.c_str()); |
| 1736 | dprintf(err, "Unknown package: '%s'\n", packageName.c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 1737 | return BAD_VALUE; |
| 1738 | } |
| 1739 | |
| 1740 | if (userId < 0) { |
| 1741 | ALOGE("Invalid user: %d", userId); |
| 1742 | dprintf(err, "Invalid user: %d\n", userId); |
| 1743 | return BAD_VALUE; |
| 1744 | } |
| 1745 | |
| 1746 | uid = multiuser_get_uid(userId, uid); |
| 1747 | return NO_ERROR; |
| 1748 | } |
| 1749 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1750 | Status CameraService::validateConnectLocked(const std::string& cameraId, |
| 1751 | const std::string& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1752 | /*out*/int& originalClientPid) const { |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1753 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1754 | #ifdef __BRILLO__ |
| 1755 | UNUSED(clientName8); |
| 1756 | UNUSED(clientUid); |
| 1757 | UNUSED(clientPid); |
| 1758 | UNUSED(originalClientPid); |
| 1759 | #else |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1760 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, |
| 1761 | originalClientPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1762 | if (!allowed.isOk()) { |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1763 | return allowed; |
| 1764 | } |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1765 | #endif // __BRILLO__ |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1766 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1767 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1768 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1769 | if (!mInitialized) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1770 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 1771 | callingPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1772 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1773 | "No camera HAL module available to open camera device \"%s\"", cameraId.c_str()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1776 | if (getCameraState(cameraId) == nullptr) { |
| 1777 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1778 | cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1779 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1780 | "No camera device with ID \"%s\" available", cameraId.c_str()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1783 | status_t err = checkIfDeviceIsUsable(cameraId); |
| 1784 | if (err != NO_ERROR) { |
| 1785 | switch(err) { |
| 1786 | case -ENODEV: |
| 1787 | case -EBUSY: |
| 1788 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1789 | "No camera device with ID \"%s\" currently available", cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1790 | default: |
| 1791 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1792 | "Unknown error connecting to ID \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | return Status::ok(); |
Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1796 | } |
| 1797 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1798 | Status CameraService::validateClientPermissionsLocked(const std::string& cameraId, |
| 1799 | const std::string& clientName, int& clientUid, int& clientPid, |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1800 | /*out*/int& originalClientPid) const { |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1801 | AttributionSourceState attributionSource{}; |
| 1802 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1803 | int callingPid = CameraThreadState::getCallingPid(); |
| 1804 | int callingUid = CameraThreadState::getCallingUid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1805 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1806 | // Check if we can trust clientUid |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1807 | if (clientUid == USE_CALLING_UID) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1808 | clientUid = callingUid; |
| 1809 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1810 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1811 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1812 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1813 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1814 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1815 | callingPid, callingUid, cameraId.c_str(), |
| 1816 | clientName.c_str(), clientUid, clientPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1819 | // Check if we can trust clientPid |
| 1820 | if (clientPid == USE_CALLING_PID) { |
| 1821 | clientPid = callingPid; |
| 1822 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1823 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1824 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1825 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1826 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1827 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1828 | callingPid, callingUid, cameraId.c_str(), |
| 1829 | clientName.c_str(), clientUid, clientPid); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1830 | } |
| 1831 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1832 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1833 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", |
| 1834 | cameraId.c_str()); |
| 1835 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, "No camera device with ID \"%s\" is" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1836 | "available", cameraId.c_str()); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1837 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1838 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1839 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1840 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1841 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "No camera device with ID \"%s\"" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1842 | "found while trying to query device kind", cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1843 | |
| 1844 | } |
| 1845 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1846 | // If it's not calling from cameraserver, check the permission if the |
| 1847 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for |
| 1848 | // android.permission.SYSTEM_CAMERA for system only camera devices). |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1849 | attributionSource.pid = clientPid; |
| 1850 | attributionSource.uid = clientUid; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1851 | attributionSource.packageName = clientName; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1852 | bool checkPermissionForCamera = checkPermission(cameraId, sCameraPermission, attributionSource, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1853 | std::string(), AppOpsManager::OP_NONE); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1854 | if (callingPid != getpid() && |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1855 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && !checkPermissionForCamera) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1856 | 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] | 1857 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1858 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1859 | clientName.c_str(), clientUid, clientPid, cameraId.c_str()); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1860 | } |
| 1861 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1862 | // Make sure the UID is in an active state to use the camera |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1863 | if (!mUidPolicy->isUidActive(callingUid, clientName)) { |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1864 | int32_t procState = mUidPolicy->getProcState(callingUid); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1865 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", |
| 1866 | clientPid, clientUid); |
| 1867 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1868 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" |
| 1869 | "calling UID %d proc state %" PRId32 ")", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1870 | clientName.c_str(), clientUid, clientPid, cameraId.c_str(), |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1871 | callingUid, procState); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1872 | } |
| 1873 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1874 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use cases |
| 1875 | // such as rear view and surround view cannot be disabled and are exempt from sensor privacy |
| 1876 | // policy. In all other cases,if sensor privacy is enabled then prevent access to the camera. |
| 1877 | if ((!isAutomotivePrivilegedClient(callingUid) || |
| 1878 | !isAutomotiveExteriorSystemCamera(cameraId)) && |
| 1879 | mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1880 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); |
| 1881 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1882 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" when sensor privacy " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1883 | "is enabled", clientName.c_str(), clientUid, clientPid, cameraId.c_str()); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1884 | } |
| 1885 | |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1886 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's |
| 1887 | // connected to camera service directly. |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1888 | originalClientPid = clientPid; |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1889 | clientPid = callingPid; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1890 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1891 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1892 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1893 | // For non-system clients : Only allow clients who are being used by the current foreground |
| 1894 | // device user, unless calling from our own process. |
| 1895 | if (!doesClientHaveSystemUid() && callingPid != getpid() && |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1896 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1897 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 1898 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1899 | toString(mAllowedUsers).c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1900 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1901 | "Callers from device user %d are not currently allowed to connect to camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1902 | clientUserId, cameraId.c_str()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 1905 | if (flags::camera_hsum_permission()) { |
| 1906 | // If the System User tries to access the camera when the device is running in |
| 1907 | // headless system user mode, ensure that client has the required permission |
| 1908 | // CAMERA_HEADLESS_SYSTEM_USER. |
| 1909 | if (isHeadlessSystemUserMode() && (clientUserId == USER_SYSTEM) && |
| 1910 | !hasPermissionsForCameraHeadlessSystemUser(cameraId, callingPid, callingUid)) { |
| 1911 | ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid); |
| 1912 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1913 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" as Headless System \ |
| 1914 | User without camera headless system user permission", |
| 1915 | clientName.c_str(), clientUid, clientPid, cameraId.c_str()); |
| 1916 | } |
Jyoti Bhayana | 5bdb5a6 | 2023-08-24 14:46:08 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1919 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1920 | } |
| 1921 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1922 | status_t CameraService::checkIfDeviceIsUsable(const std::string& cameraId) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1923 | auto cameraState = getCameraState(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1924 | int callingPid = CameraThreadState::getCallingPid(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1925 | if (cameraState == nullptr) { |
| 1926 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1927 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1928 | return -ENODEV; |
| 1929 | } |
| 1930 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1931 | StatusInternal currentStatus = cameraState->getStatus(); |
| 1932 | if (currentStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1933 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1934 | callingPid, cameraId.c_str()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1935 | return -ENODEV; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1936 | } else if (currentStatus == StatusInternal::ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1937 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1938 | callingPid, cameraId.c_str()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1939 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1940 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1941 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1942 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1943 | } |
| 1944 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1945 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1946 | const CameraService::DescriptorPtr& desc, int oomScoreOffset, bool systemNativeClient) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1947 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1948 | // Make a descriptor for the incoming client |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1949 | auto clientDescriptor = |
| 1950 | CameraService::CameraClientManager::makeClientDescriptor(client, desc, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1951 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1952 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 1953 | |
| 1954 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1955 | client->getPackageName()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1956 | |
| 1957 | if (evicted.size() > 0) { |
| 1958 | // This should never happen - clients should already have been removed in disconnect |
| 1959 | for (auto& i : evicted) { |
| 1960 | ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1961 | __FUNCTION__, i->getKey().c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 1965 | __FUNCTION__); |
| 1966 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1967 | |
| 1968 | // And register a death notification for the client callback. Do |
| 1969 | // this last to avoid Binder policy where a nested Binder |
| 1970 | // transaction might be pre-empted to service the client death |
| 1971 | // notification if the client process dies before linkToDeath is |
| 1972 | // invoked. |
| 1973 | sp<IBinder> remoteCallback = client->getRemote(); |
| 1974 | if (remoteCallback != nullptr) { |
| 1975 | remoteCallback->linkToDeath(this); |
| 1976 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1977 | } |
| 1978 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1979 | status_t CameraService::handleEvictionsLocked(const std::string& cameraId, int clientPid, |
| 1980 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, |
| 1981 | const std::string& packageName, int oomScoreOffset, bool systemNativeClient, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1982 | /*out*/ |
| 1983 | sp<BasicClient>* client, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1984 | std::shared_ptr<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1985 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1986 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1987 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1988 | DescriptorPtr clientDescriptor; |
| 1989 | { |
| 1990 | if (effectiveApiLevel == API_1) { |
| 1991 | // If we are using API1, any existing client for this camera ID with the same remote |
| 1992 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 1993 | |
| 1994 | auto current = mActiveClientManager.get(cameraId); |
| 1995 | if (current != nullptr) { |
| 1996 | auto clientSp = current->getValue(); |
| 1997 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1998 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
Shuzhen Wang | b2d43f6 | 2021-08-25 14:01:11 -0700 | [diff] [blame] | 1999 | ALOGW("CameraService connect called with a different" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2000 | " API level, evicting prior client..."); |
| 2001 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2002 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2003 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2004 | *client = clientSp; |
| 2005 | return NO_ERROR; |
| 2006 | } |
| 2007 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 2008 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 2009 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 2010 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2011 | // Get state for the given cameraId |
| 2012 | auto state = getCameraState(cameraId); |
| 2013 | if (state == nullptr) { |
| 2014 | ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2015 | clientPid, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2016 | // Should never get here because validateConnectLocked should have errored out |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2017 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2018 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2019 | |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 2020 | sp<IServiceManager> sm = defaultServiceManager(); |
| 2021 | sp<IBinder> binder = sm->checkService(String16(kProcessInfoServiceName)); |
| 2022 | if (!binder && isAutomotivePrivilegedClient(CameraThreadState::getCallingUid())) { |
| 2023 | // If processinfo service is not available and the client is automotive privileged |
| 2024 | // client used for safety critical uses cases such as rear-view and surround-view which |
| 2025 | // needs to be available before android boot completes, then use the hardcoded values |
| 2026 | // for the process state and priority score. As this scenario is before android system |
| 2027 | // services are up and client is native client, hence using NATIVE_ADJ as the priority |
| 2028 | // score and state as PROCESS_STATE_BOUND_TOP as such automotive apps need to be |
| 2029 | // visible on the top. |
| 2030 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 2031 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 2032 | state->getConflicting(), resource_policy::NATIVE_ADJ, clientPid, |
| 2033 | ActivityManager::PROCESS_STATE_BOUND_TOP, oomScoreOffset, systemNativeClient); |
| 2034 | } else { |
| 2035 | // Get current active client PIDs |
| 2036 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 2037 | ownerPids.push_back(clientPid); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2038 | |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 2039 | std::vector<int> priorityScores(ownerPids.size()); |
| 2040 | std::vector<int> states(ownerPids.size()); |
| 2041 | |
| 2042 | // Get priority scores of all active PIDs |
| 2043 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids(ownerPids.size(), |
| 2044 | &ownerPids[0], /*out*/&states[0], /*out*/&priorityScores[0]); |
| 2045 | if (err != OK) { |
| 2046 | ALOGE("%s: Priority score query failed: %d", __FUNCTION__, err); |
| 2047 | return err; |
| 2048 | } |
| 2049 | |
| 2050 | // Update all active clients' priorities |
| 2051 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; |
| 2052 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
| 2053 | pidToPriorityMap.emplace(ownerPids[i], |
| 2054 | resource_policy::ClientPriority(priorityScores[i], states[i], |
| 2055 | /* isVendorClient won't get copied over*/ false, |
| 2056 | /* oomScoreOffset won't get copied over*/ 0)); |
| 2057 | } |
| 2058 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
| 2059 | |
| 2060 | int32_t actualScore = priorityScores[priorityScores.size() - 1]; |
| 2061 | int32_t actualState = states[states.size() - 1]; |
| 2062 | |
| 2063 | // Make descriptor for incoming client. We store the oomScoreOffset |
| 2064 | // since we might need it later on new handleEvictionsLocked and |
| 2065 | // ProcessInfoService would not take that into account. |
| 2066 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 2067 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 2068 | state->getConflicting(), actualScore, clientPid, actualState, |
| 2069 | oomScoreOffset, systemNativeClient); |
| 2070 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2071 | |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2072 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); |
| 2073 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2074 | // Find clients that would be evicted |
| 2075 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 2076 | |
| 2077 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 2078 | // background, so we cannot do evictions |
| 2079 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 2080 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 2081 | " priority).", clientPid); |
| 2082 | |
| 2083 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2084 | std::string curTime = getFormattedCurrentTime(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2085 | auto incompatibleClients = |
| 2086 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 2087 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2088 | std::string msg = fmt::sprintf("%s : DENIED connect device %s client for package %s " |
| 2089 | "(PID %d, score %d state %d) due to eviction policy", curTime.c_str(), |
| 2090 | cameraId.c_str(), packageName.c_str(), clientPid, |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2091 | clientPriority.getScore(), clientPriority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2092 | |
| 2093 | for (auto& i : incompatibleClients) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2094 | msg += fmt::sprintf("\n - Blocked by existing device %s client for package %s" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2095 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2096 | i->getKey().c_str(), |
| 2097 | i->getValue()->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2098 | i->getOwnerId(), i->getPriority().getScore(), |
| 2099 | i->getPriority().getState()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 2100 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2101 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().c_str(), |
| 2102 | i->getValue()->getPackageName().c_str(), i->getOwnerId(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2103 | i->getPriority().getScore(), i->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2107 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2108 | mEventLog.add(msg); |
| 2109 | |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 2110 | auto current = mActiveClientManager.get(cameraId); |
| 2111 | if (current != nullptr) { |
| 2112 | return -EBUSY; // CAMERA_IN_USE |
| 2113 | } else { |
| 2114 | return -EUSERS; // MAX_CAMERAS_IN_USE |
| 2115 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | for (auto& i : evicted) { |
| 2119 | sp<BasicClient> clientSp = i->getValue(); |
| 2120 | if (clientSp.get() == nullptr) { |
| 2121 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 2122 | |
| 2123 | // TODO: Remove this |
| 2124 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 2125 | __FUNCTION__); |
| 2126 | mActiveClientManager.remove(i); |
| 2127 | continue; |
| 2128 | } |
| 2129 | |
| 2130 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2131 | i->getKey().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2132 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2133 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2134 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2135 | logEvent(fmt::sprintf("EVICT device %s client held by package %s (PID" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2136 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" |
| 2137 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2138 | i->getKey().c_str(), clientSp->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2139 | i->getOwnerId(), i->getPriority().getScore(), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2140 | i->getPriority().getState(), cameraId.c_str(), |
| 2141 | packageName.c_str(), clientPid, clientPriority.getScore(), |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2142 | clientPriority.getState())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2143 | |
| 2144 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2145 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2146 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2147 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2150 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 2151 | // other clients from connecting in mServiceLockWrapper if held |
| 2152 | mServiceLock.unlock(); |
| 2153 | |
| 2154 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2155 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2156 | |
| 2157 | // Destroy evicted clients |
| 2158 | for (auto& i : evictedClients) { |
| 2159 | // 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] | 2160 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2161 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2162 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2163 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2164 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2165 | for (const auto& i : evictedClients) { |
| 2166 | ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2167 | __FUNCTION__, i->getKey().c_str(), i->getOwnerId()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2168 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 2169 | if (ret == TIMED_OUT) { |
| 2170 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2171 | "current clients:\n%s", __FUNCTION__, i->getKey().c_str(), |
| 2172 | mActiveClientManager.toString().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2173 | return -EBUSY; |
| 2174 | } |
| 2175 | if (ret != NO_ERROR) { |
| 2176 | ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2177 | "current clients:\n%s", __FUNCTION__, i->getKey().c_str(), strerror(-ret), |
| 2178 | ret, mActiveClientManager.toString().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2179 | return ret; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | evictedClients.clear(); |
| 2184 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2185 | // Once clients have been disconnected, relock |
| 2186 | mServiceLock.lock(); |
| 2187 | |
| 2188 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 2189 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 2190 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2191 | } |
| 2192 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2193 | *partial = clientDescriptor; |
| 2194 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2197 | Status CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2198 | const sp<ICameraClient>& cameraClient, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2199 | int api1CameraId, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2200 | const std::string& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 2201 | int clientUid, |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 2202 | int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2203 | int targetSdkVersion, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2204 | bool overrideToPortrait, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2205 | bool forceSlowJpegMode, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 2206 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2207 | sp<ICamera>* device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2208 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2209 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2210 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2211 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 2212 | std::string unresolvedCameraId = cameraIdIntToStr(api1CameraId); |
| 2213 | std::string cameraIdStr = resolveCameraId(unresolvedCameraId, |
| 2214 | CameraThreadState::getCallingUid()); |
| 2215 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2216 | sp<Client> client = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2217 | ret = connectHelper<ICameraClient,Client>(cameraClient, cameraIdStr, api1CameraId, |
| 2218 | clientPackageName, /*systemNativeClient*/ false, {}, clientUid, clientPid, API_1, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2219 | /*shimUpdateOnly*/ false, /*oomScoreOffset*/ 0, targetSdkVersion, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2220 | overrideToPortrait, forceSlowJpegMode, cameraIdStr, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2221 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2222 | if(!ret.isOk()) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2223 | logRejected(cameraIdStr, CameraThreadState::getCallingPid(), clientPackageName, |
| 2224 | toStdString(ret.toString8())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2225 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2228 | *device = client; |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2229 | |
| 2230 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 2231 | const auto& mActivityManager = getActivityManager(); |
| 2232 | if (mActivityManager) { |
| 2233 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
| 2234 | CameraThreadState::getCallingUid(), |
| 2235 | CameraThreadState::getCallingPid()); |
| 2236 | } |
| 2237 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2238 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2239 | } |
| 2240 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2241 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, |
| 2242 | bool isVendorListener, int clientPid, int clientUid) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2243 | // If the client is not a vendor client, don't add listener if |
| 2244 | // a) the camera is a publicly hidden secure camera OR |
| 2245 | // b) the camera is a system only camera and the client doesn't |
| 2246 | // have android.permission.SYSTEM_CAMERA permissions. |
| 2247 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || |
| 2248 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2249 | !hasPermissionsForSystemCamera(std::string(), clientPid, clientUid)))) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2250 | return true; |
| 2251 | } |
| 2252 | return false; |
| 2253 | } |
| 2254 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2255 | bool CameraService::shouldRejectSystemCameraConnection(const std::string& cameraId) const { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2256 | // Rules for rejection: |
| 2257 | // 1) If cameraserver tries to access this camera device, accept the |
| 2258 | // connection. |
| 2259 | // 2) The camera device is a publicly hidden secure camera device AND some |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2260 | // non system component is trying to access it. |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2261 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY |
| 2262 | // and the serving thread is a non hwbinder thread, the client must have |
| 2263 | // android.permission.SYSTEM_CAMERA permissions to connect. |
| 2264 | |
| 2265 | int cPid = CameraThreadState::getCallingPid(); |
| 2266 | int cUid = CameraThreadState::getCallingUid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2267 | bool systemClient = doesClientHaveSystemUid(); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2268 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 2269 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 2270 | // This isn't a known camera ID, so it's not a system camera |
| 2271 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); |
| 2272 | return false; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2273 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2274 | |
| 2275 | // (1) Cameraserver trying to connect, accept. |
| 2276 | if (CameraThreadState::getCallingPid() == getpid()) { |
| 2277 | return false; |
| 2278 | } |
| 2279 | // (2) |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2280 | if (!systemClient && systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2281 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); |
| 2282 | return true; |
| 2283 | } |
| 2284 | // (3) Here we only check for permissions if it is a system only camera device. This is since |
| 2285 | // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some |
| 2286 | // characteristics) even if clients don't have android.permission.CAMERA. We do not want the |
| 2287 | // same behavior for system camera devices. |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2288 | if (!systemClient && systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2289 | !hasPermissionsForSystemCamera(cameraId, cPid, cUid)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2290 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", |
| 2291 | cameraId.c_str()); |
| 2292 | return true; |
| 2293 | } |
| 2294 | |
| 2295 | return false; |
| 2296 | } |
| 2297 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2298 | Status CameraService::connectDevice( |
| 2299 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2300 | const std::string& unresolvedCameraId, |
| 2301 | const std::string& clientPackageName, |
| 2302 | const std::optional<std::string>& clientFeatureId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2303 | int clientUid, int oomScoreOffset, int targetSdkVersion, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2304 | bool overrideToPortrait, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2305 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2306 | sp<hardware::camera2::ICameraDeviceUser>* device) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2307 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2308 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2309 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2310 | sp<CameraDeviceClient> client = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2311 | std::string clientPackageNameAdj = clientPackageName; |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2312 | int callingPid = CameraThreadState::getCallingPid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2313 | bool systemNativeClient = false; |
| 2314 | if (doesClientHaveSystemUid() && (clientPackageNameAdj.size() == 0)) { |
| 2315 | std::string systemClient = |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2316 | fmt::sprintf("client.pid<%d>", CameraThreadState::getCallingPid()); |
| 2317 | clientPackageNameAdj = systemClient; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2318 | systemNativeClient = true; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2319 | } |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 2320 | const std::string cameraId = resolveCameraId( |
| 2321 | unresolvedCameraId, |
| 2322 | CameraThreadState::getCallingUid(), |
| 2323 | clientPackageNameAdj); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2324 | |
| 2325 | if (oomScoreOffset < 0) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2326 | std::string msg = |
| 2327 | fmt::sprintf("Cannot increase the priority of a client %s pid %d for " |
| 2328 | "camera id %s", clientPackageNameAdj.c_str(), callingPid, |
| 2329 | cameraId.c_str()); |
| 2330 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2331 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 2334 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 2335 | int callingUid = CameraThreadState::getCallingUid(); |
| 2336 | if (clientUid == USE_CALLING_UID) { |
| 2337 | clientUserId = multiuser_get_user_id(callingUid); |
| 2338 | } |
| 2339 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2340 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use cases |
| 2341 | // such as rear view and surround view cannot be disabled. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2342 | if ((!isAutomotivePrivilegedClient(callingUid) || !isAutomotiveExteriorSystemCamera(cameraId)) |
| 2343 | && mCameraServiceProxyWrapper->isCameraDisabled(clientUserId)) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2344 | std::string msg = "Camera disabled by device policy"; |
| 2345 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2346 | return STATUS_ERROR(ERROR_DISABLED, msg.c_str()); |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2349 | // enforce system camera permissions |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2350 | if (oomScoreOffset > 0 |
| 2351 | && !hasPermissionsForSystemCamera(cameraId, callingPid, |
| 2352 | CameraThreadState::getCallingUid()) |
| 2353 | && !isTrustedCallingUid(CameraThreadState::getCallingUid())) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2354 | std::string msg = fmt::sprintf("Cannot change the priority of a client %s pid %d for " |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2355 | "camera id %s without SYSTEM_CAMERA permissions", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2356 | clientPackageNameAdj.c_str(), callingPid, cameraId.c_str()); |
| 2357 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2358 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, msg.c_str()); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2361 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, |
| 2362 | cameraId, /*api1CameraId*/-1, clientPackageNameAdj, systemNativeClient, clientFeatureId, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2363 | clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, oomScoreOffset, |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 2364 | targetSdkVersion, overrideToPortrait, /*forceSlowJpegMode*/false, unresolvedCameraId, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2365 | /*out*/client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2366 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2367 | if(!ret.isOk()) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2368 | logRejected(cameraId, callingPid, clientPackageNameAdj, toStdString(ret.toString8())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2369 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2370 | } |
| 2371 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2372 | *device = client; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 2373 | Mutex::Autolock lock(mServiceLock); |
| 2374 | |
| 2375 | // Clear the previous cached logs and reposition the |
| 2376 | // file offset to beginning of the file to log new data. |
| 2377 | // If either truncate or lseek fails, close the previous file and create a new one. |
| 2378 | if ((ftruncate(mMemFd, 0) == -1) || (lseek(mMemFd, 0, SEEK_SET) == -1)) { |
| 2379 | ALOGE("%s: Error while truncating the file: %s", __FUNCTION__, sFileName); |
| 2380 | // Close the previous memfd. |
| 2381 | close(mMemFd); |
| 2382 | // If failure to wipe the data, then create a new file and |
| 2383 | // assign the new value to mMemFd. |
| 2384 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 2385 | if (mMemFd == -1) { |
| 2386 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 2387 | } |
| 2388 | } |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2389 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 2390 | const auto& mActivityManager = getActivityManager(); |
| 2391 | if (mActivityManager) { |
| 2392 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
| 2393 | CameraThreadState::getCallingUid(), |
| 2394 | CameraThreadState::getCallingPid()); |
| 2395 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2396 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2397 | } |
| 2398 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2399 | bool CameraService::isCameraPrivacyEnabled(const String16& packageName, const std::string& cam_id, |
| 2400 | int callingPid, int callingUid) { |
| 2401 | if (!isAutomotiveDevice()) { |
| 2402 | return mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
| 2403 | } |
| 2404 | |
| 2405 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for |
| 2406 | // safety-critical use cases cannot be disabled and are exempt from camera privacy policy. |
| 2407 | if ((isAutomotivePrivilegedClient(callingUid) && isAutomotiveExteriorSystemCamera(cam_id))) { |
| 2408 | ALOGI("Camera privacy cannot be enabled for automotive privileged client %d " |
| 2409 | "using camera %s", callingUid, cam_id.c_str()); |
| 2410 | return false; |
| 2411 | } |
| 2412 | |
| 2413 | if (mSensorPrivacyPolicy->isCameraPrivacyEnabled(packageName)) { |
| 2414 | return true; |
| 2415 | } else if (mSensorPrivacyPolicy->getCameraPrivacyState() == SensorPrivacyManager::DISABLED) { |
| 2416 | return false; |
Jyoti Bhayana | 54a4b00 | 2024-02-27 15:36:09 -0800 | [diff] [blame^] | 2417 | } else if (mSensorPrivacyPolicy->getCameraPrivacyState() |
| 2418 | == SensorPrivacyManager::ENABLED_EXCEPT_ALLOWLISTED_APPS) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2419 | if (hasPermissionsForCameraPrivacyAllowlist(callingPid, callingUid)) { |
| 2420 | return false; |
| 2421 | } else { |
| 2422 | return true; |
| 2423 | } |
| 2424 | } |
| 2425 | return false; |
| 2426 | } |
| 2427 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2428 | std::string CameraService::getPackageNameFromUid(int clientUid) { |
| 2429 | std::string packageName(""); |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2430 | |
| 2431 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2432 | sp<IBinder> binder = sm->getService(toString16(kPermissionServiceName)); |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2433 | if (binder == 0) { |
| 2434 | ALOGE("Cannot get permission service"); |
| 2435 | // Return empty package name and the further interaction |
| 2436 | // with camera will likely fail |
| 2437 | return packageName; |
| 2438 | } |
| 2439 | |
| 2440 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 2441 | Vector<String16> packages; |
| 2442 | |
| 2443 | permCtrl->getPackagesForUid(clientUid, packages); |
| 2444 | |
| 2445 | if (packages.isEmpty()) { |
| 2446 | ALOGE("No packages for calling UID %d", clientUid); |
| 2447 | // Return empty package name and the further interaction |
| 2448 | // with camera will likely fail |
| 2449 | return packageName; |
| 2450 | } |
| 2451 | |
| 2452 | // Arbitrarily pick the first name in the list |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2453 | packageName = toStdString(packages[0]); |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2454 | |
| 2455 | return packageName; |
| 2456 | } |
| 2457 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2458 | template<class CALLBACK, class CLIENT> |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2459 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const std::string& cameraId, |
| 2460 | int api1CameraId, const std::string& clientPackageNameMaybe, bool systemNativeClient, |
| 2461 | const std::optional<std::string>& clientFeatureId, int clientUid, int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2462 | apiLevel effectiveApiLevel, bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2463 | bool overrideToPortrait, bool forceSlowJpegMode, const std::string& originalCameraId, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2464 | /*out*/sp<CLIENT>& device) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2465 | binder::Status ret = binder::Status::ok(); |
| 2466 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2467 | bool isNonSystemNdk = false; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2468 | std::string clientPackageName; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2469 | int packageUid = (clientUid == USE_CALLING_UID) ? |
| 2470 | CameraThreadState::getCallingUid() : clientUid; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2471 | if (clientPackageNameMaybe.size() <= 0) { |
| 2472 | // NDK calls don't come with package names, but we need one for various cases. |
| 2473 | // Generally, there's a 1:1 mapping between UID and package name, but shared UIDs |
| 2474 | // do exist. For all authentication cases, all packages under the same UID get the |
| 2475 | // same permissions, so picking any associated package name is sufficient. For some |
| 2476 | // other cases, this may give inaccurate names for clients in logs. |
| 2477 | isNonSystemNdk = true; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2478 | clientPackageName = getPackageNameFromUid(packageUid); |
| 2479 | } else { |
| 2480 | clientPackageName = clientPackageNameMaybe; |
| 2481 | } |
| 2482 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2483 | int originalClientPid = 0; |
| 2484 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2485 | int packagePid = (clientPid == USE_CALLING_PID) ? |
| 2486 | CameraThreadState::getCallingPid() : clientPid; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 2487 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2488 | "Camera API version %d", packagePid, clientPackageName.c_str(), cameraId.c_str(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2489 | static_cast<int>(effectiveApiLevel)); |
| 2490 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2491 | nsecs_t openTimeNs = systemTime(); |
| 2492 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2493 | sp<CLIENT> client = nullptr; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2494 | int facing = -1; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 2495 | int orientation = 0; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2496 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2497 | { |
| 2498 | // Acquire mServiceLock and prevent other clients from connecting |
| 2499 | std::unique_ptr<AutoConditionLock> lock = |
| 2500 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2501 | |
| 2502 | if (lock == nullptr) { |
| 2503 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." |
| 2504 | , clientPid); |
| 2505 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2506 | "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2507 | cameraId.c_str(), clientPackageName.c_str(), clientPid); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2508 | } |
| 2509 | |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 2510 | // Enforce client permissions and do basic validity checks |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2511 | if(!(ret = validateConnectLocked(cameraId, clientPackageName, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2512 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { |
| 2513 | return ret; |
| 2514 | } |
| 2515 | |
| 2516 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 2517 | // we were doing a shim update, return immediately |
| 2518 | if (shimUpdateOnly) { |
| 2519 | auto cameraState = getCameraState(cameraId); |
| 2520 | if (cameraState != nullptr) { |
| 2521 | if (!cameraState->getShimParams().isEmpty()) return ret; |
| 2522 | } |
| 2523 | } |
| 2524 | |
| 2525 | status_t err; |
| 2526 | |
| 2527 | sp<BasicClient> clientTmp = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2528 | std::shared_ptr<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>> partial; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2529 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2530 | IInterface::asBinder(cameraCb), clientPackageName, oomScoreOffset, |
| 2531 | systemNativeClient, /*out*/&clientTmp, /*out*/&partial)) != NO_ERROR) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2532 | switch (err) { |
| 2533 | case -ENODEV: |
| 2534 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2535 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2536 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2537 | case -EBUSY: |
| 2538 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2539 | "Higher-priority client using camera, ID \"%s\" currently unavailable", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2540 | cameraId.c_str()); |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 2541 | case -EUSERS: |
| 2542 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2543 | "Too many cameras already open, cannot open camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2544 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2545 | default: |
| 2546 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2547 | "Unexpected error %s (%d) opening camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2548 | strerror(-err), err, cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | if (clientTmp.get() != nullptr) { |
| 2553 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 2554 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 2555 | return ret; |
| 2556 | } |
| 2557 | |
| 2558 | // give flashlight a chance to close devices if necessary. |
| 2559 | mFlashlight->prepareDeviceOpen(cameraId); |
| 2560 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2561 | int portraitRotation; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2562 | auto deviceVersionAndTransport = |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2563 | getDeviceVersion(cameraId, overrideToPortrait, /*out*/&portraitRotation, |
| 2564 | /*out*/&facing, /*out*/&orientation); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2565 | if (facing == -1) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2566 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2567 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2568 | "Unable to get camera device \"%s\" facing", cameraId.c_str()); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2569 | } |
| 2570 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2571 | sp<BasicClient> tmp = nullptr; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2572 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2573 | mPerfClassPrimaryCameraIds, cameraId, targetSdkVersion); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2574 | if(!(ret = makeClient(this, cameraCb, clientPackageName, systemNativeClient, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2575 | clientFeatureId, cameraId, api1CameraId, facing, |
| 2576 | orientation, clientPid, clientUid, getpid(), |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2577 | deviceVersionAndTransport, effectiveApiLevel, overrideForPerfClass, |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 2578 | overrideToPortrait, forceSlowJpegMode, originalCameraId, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2579 | /*out*/&tmp)).isOk()) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2580 | return ret; |
| 2581 | } |
| 2582 | client = static_cast<CLIENT*>(tmp.get()); |
| 2583 | |
| 2584 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 2585 | __FUNCTION__); |
| 2586 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2587 | std::string monitorTags = isClientWatched(client.get()) ? mMonitorTags : std::string(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2588 | err = client->initialize(mCameraProviderManager, monitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2589 | if (err != OK) { |
| 2590 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2591 | // Errors could be from the HAL module open call or from AppOpsManager |
Kwangkyu Park | b1aaf9a | 2023-07-08 00:42:03 +0900 | [diff] [blame] | 2592 | mServiceLock.unlock(); |
| 2593 | client->disconnect(); |
| 2594 | mServiceLock.lock(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2595 | switch(err) { |
| 2596 | case BAD_VALUE: |
| 2597 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2598 | "Illegal argument to HAL module for camera \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2599 | case -EBUSY: |
| 2600 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2601 | "Camera \"%s\" is already open", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2602 | case -EUSERS: |
| 2603 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2604 | "Too many cameras already open, cannot open camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2605 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2606 | case PERMISSION_DENIED: |
| 2607 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2608 | "No permission to open camera \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2609 | case -EACCES: |
| 2610 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2611 | "Camera \"%s\" disabled by policy", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2612 | case -ENODEV: |
| 2613 | default: |
| 2614 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2615 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2616 | strerror(-err), err); |
| 2617 | } |
| 2618 | } |
| 2619 | |
| 2620 | // Update shim paremeters for legacy clients |
| 2621 | if (effectiveApiLevel == API_1) { |
| 2622 | // Assume we have always received a Client subclass for API1 |
| 2623 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 2624 | String8 rawParams = shimClient->getParameters(); |
| 2625 | CameraParameters params(rawParams); |
| 2626 | |
| 2627 | auto cameraState = getCameraState(cameraId); |
| 2628 | if (cameraState != nullptr) { |
| 2629 | cameraState->setShimParams(params); |
| 2630 | } else { |
| 2631 | ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2632 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2633 | } |
| 2634 | } |
| 2635 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 2636 | // Enable/disable camera service watchdog |
| 2637 | client->setCameraServiceWatchdog(mCameraServiceWatchdogEnabled); |
| 2638 | |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2639 | CameraMetadata chars; |
| 2640 | bool rotateAndCropSupported = true; |
| 2641 | err = mCameraProviderManager->getCameraCharacteristics(cameraId, overrideForPerfClass, |
| 2642 | &chars, overrideToPortrait); |
| 2643 | if (err == OK) { |
| 2644 | auto availableRotateCropEntry = chars.find( |
| 2645 | ANDROID_SCALER_AVAILABLE_ROTATE_AND_CROP_MODES); |
| 2646 | if (availableRotateCropEntry.count <= 1) { |
| 2647 | rotateAndCropSupported = false; |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2648 | } |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame] | 2649 | } else { |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2650 | ALOGE("%s: Unable to query static metadata for camera %s: %s (%d)", __FUNCTION__, |
| 2651 | cameraId.c_str(), strerror(-err), err); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2654 | if (rotateAndCropSupported) { |
| 2655 | // Set rotate-and-crop override behavior |
| 2656 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { |
| 2657 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); |
| 2658 | } else if (overrideToPortrait && portraitRotation != 0) { |
| 2659 | uint8_t rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_AUTO; |
| 2660 | switch (portraitRotation) { |
| 2661 | case 90: |
| 2662 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_90; |
| 2663 | break; |
| 2664 | case 180: |
| 2665 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_180; |
| 2666 | break; |
| 2667 | case 270: |
| 2668 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_270; |
| 2669 | break; |
| 2670 | default: |
| 2671 | ALOGE("Unexpected portrait rotation: %d", portraitRotation); |
| 2672 | break; |
| 2673 | } |
| 2674 | client->setRotateAndCropOverride(rotateAndCropMode); |
| 2675 | } else { |
| 2676 | client->setRotateAndCropOverride( |
| 2677 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 2678 | clientPackageName, facing, multiuser_get_user_id(clientUid))); |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | bool autoframingSupported = true; |
| 2683 | auto availableAutoframingEntry = chars.find(ANDROID_CONTROL_AUTOFRAMING_AVAILABLE); |
| 2684 | if ((availableAutoframingEntry.count == 1) && (availableAutoframingEntry.data.u8[0] == |
| 2685 | ANDROID_CONTROL_AUTOFRAMING_AVAILABLE_FALSE)) { |
| 2686 | autoframingSupported = false; |
| 2687 | } |
| 2688 | |
| 2689 | if (autoframingSupported) { |
| 2690 | // Set autoframing override behaviour |
| 2691 | if (mOverrideAutoframingMode != ANDROID_CONTROL_AUTOFRAMING_AUTO) { |
| 2692 | client->setAutoframingOverride(mOverrideAutoframingMode); |
| 2693 | } else { |
| 2694 | client->setAutoframingOverride( |
| 2695 | mCameraServiceProxyWrapper->getAutoframingOverride( |
| 2696 | clientPackageName)); |
| 2697 | } |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 2698 | } |
| 2699 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2700 | bool isCameraPrivacyEnabled; |
| 2701 | if (flags::camera_privacy_allowlist()) { |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2702 | // Set camera muting behavior. |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2703 | isCameraPrivacyEnabled = this->isCameraPrivacyEnabled( |
| 2704 | toString16(client->getPackageName()), cameraId, packagePid, packageUid); |
| 2705 | } else { |
| 2706 | isCameraPrivacyEnabled = |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2707 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2708 | } |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 2709 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2710 | if (client->supportsCameraMute()) { |
| 2711 | client->setCameraMute( |
| 2712 | mOverrideCameraMuteMode || isCameraPrivacyEnabled); |
| 2713 | } else if (isCameraPrivacyEnabled) { |
| 2714 | // no camera mute supported, but privacy is on! => disconnect |
| 2715 | ALOGI("Camera mute not supported for package: %s, camera id: %s", |
| 2716 | client->getPackageName().c_str(), cameraId.c_str()); |
| 2717 | // Do not hold mServiceLock while disconnecting clients, but |
| 2718 | // retain the condition blocking other clients from connecting |
| 2719 | // in mServiceLockWrapper if held. |
| 2720 | mServiceLock.unlock(); |
| 2721 | // Clear caller identity temporarily so client disconnect PID |
| 2722 | // checks work correctly |
| 2723 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 2724 | // Note AppOp to trigger the "Unblock" dialog |
| 2725 | client->noteAppOp(); |
| 2726 | client->disconnect(); |
| 2727 | CameraThreadState::restoreCallingIdentity(token); |
| 2728 | // Reacquire mServiceLock |
| 2729 | mServiceLock.lock(); |
| 2730 | |
| 2731 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 2732 | "Camera \"%s\" disabled due to camera mute", cameraId.c_str()); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2733 | } |
| 2734 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2735 | if (shimUpdateOnly) { |
| 2736 | // If only updating legacy shim parameters, immediately disconnect client |
| 2737 | mServiceLock.unlock(); |
| 2738 | client->disconnect(); |
| 2739 | mServiceLock.lock(); |
| 2740 | } else { |
| 2741 | // Otherwise, add client to active clients list |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2742 | finishConnectLocked(client, partial, oomScoreOffset, systemNativeClient); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2743 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 2744 | |
| 2745 | client->setImageDumpMask(mImageDumpMask); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 2746 | client->setStreamUseCaseOverrides(mStreamUseCaseOverrides); |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 2747 | client->setZoomOverride(mZoomOverrideValue); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2748 | } // lock is destroyed, allow further connect calls |
| 2749 | |
| 2750 | // Important: release the mutex here so the client can call back into the service from its |
| 2751 | // destructor (can be at the end of the call) |
| 2752 | device = client; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2753 | |
| 2754 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 2755 | mCameraServiceProxyWrapper->logOpen(cameraId, facing, clientPackageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2756 | effectiveApiLevel, isNonSystemNdk, openLatencyMs); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2757 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2758 | { |
| 2759 | Mutex::Autolock lock(mInjectionParametersLock); |
| 2760 | if (cameraId == mInjectionInternalCamId && mInjectionInitPending) { |
| 2761 | mInjectionInitPending = false; |
| 2762 | status_t res = NO_ERROR; |
| 2763 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 2764 | if (clientDescriptor != nullptr) { |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2765 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 2766 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 2767 | if(res != OK) { |
| 2768 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2769 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2770 | mInjectionExternalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2771 | } |
| 2772 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2773 | if (res != OK) { |
| 2774 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2775 | } |
| 2776 | } else { |
| 2777 | ALOGE("%s: Internal camera ID = %s 's client does not exist!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2778 | __FUNCTION__, mInjectionInternalCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2779 | res = NO_INIT; |
| 2780 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2781 | } |
| 2782 | } |
| 2783 | } |
| 2784 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2785 | return ret; |
| 2786 | } |
| 2787 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2788 | status_t CameraService::addOfflineClient(const std::string &cameraId, |
| 2789 | sp<BasicClient> offlineClient) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2790 | if (offlineClient.get() == nullptr) { |
| 2791 | return BAD_VALUE; |
| 2792 | } |
| 2793 | |
| 2794 | { |
| 2795 | // Acquire mServiceLock and prevent other clients from connecting |
| 2796 | std::unique_ptr<AutoConditionLock> lock = |
| 2797 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2798 | |
| 2799 | if (lock == nullptr) { |
| 2800 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." |
| 2801 | , __FUNCTION__, offlineClient->getClientPid()); |
| 2802 | return TIMED_OUT; |
| 2803 | } |
| 2804 | |
| 2805 | auto onlineClientDesc = mActiveClientManager.get(cameraId); |
| 2806 | if (onlineClientDesc.get() == nullptr) { |
| 2807 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, |
| 2808 | cameraId.c_str()); |
| 2809 | return BAD_VALUE; |
| 2810 | } |
| 2811 | |
| 2812 | // Offline clients do not evict or conflict with other online devices. Resource sharing |
| 2813 | // conflicts are handled by the camera provider which will either succeed or fail before |
| 2814 | // reaching this method. |
| 2815 | const auto& onlinePriority = onlineClientDesc->getPriority(); |
| 2816 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( |
| 2817 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2818 | /*conflictingKeys*/ std::set<std::string>(), onlinePriority.getScore(), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2819 | onlineClientDesc->getOwnerId(), onlinePriority.getState(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2820 | // native clients don't have offline processing support. |
| 2821 | /*ommScoreOffset*/ 0, /*systemNativeClient*/false); |
Guillaume Bailey | 417e43d | 2022-11-02 15:30:24 +0100 | [diff] [blame] | 2822 | if (offlineClientDesc == nullptr) { |
| 2823 | ALOGE("%s: Offline client descriptor was NULL", __FUNCTION__); |
| 2824 | return BAD_VALUE; |
| 2825 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2826 | |
| 2827 | // Allow only one offline device per camera |
| 2828 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); |
| 2829 | if (!incompatibleClients.empty()) { |
| 2830 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); |
| 2831 | return BAD_VALUE; |
| 2832 | } |
| 2833 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2834 | std::string monitorTags = isClientWatched(offlineClient.get()) |
| 2835 | ? mMonitorTags : std::string(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2836 | auto err = offlineClient->initialize(mCameraProviderManager, monitorTags); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2837 | if (err != OK) { |
| 2838 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); |
| 2839 | return err; |
| 2840 | } |
| 2841 | |
| 2842 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); |
| 2843 | if (evicted.size() > 0) { |
| 2844 | for (auto& i : evicted) { |
| 2845 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2846 | __FUNCTION__, i->getKey().c_str()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2847 | } |
| 2848 | |
| 2849 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " |
| 2850 | "properly", __FUNCTION__); |
| 2851 | |
| 2852 | return BAD_VALUE; |
| 2853 | } |
| 2854 | |
| 2855 | logConnectedOffline(offlineClientDesc->getKey(), |
| 2856 | static_cast<int>(offlineClientDesc->getOwnerId()), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2857 | offlineClient->getPackageName()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2858 | |
| 2859 | sp<IBinder> remoteCallback = offlineClient->getRemote(); |
| 2860 | if (remoteCallback != nullptr) { |
| 2861 | remoteCallback->linkToDeath(this); |
| 2862 | } |
| 2863 | } // lock is destroyed, allow further connect calls |
| 2864 | |
| 2865 | return OK; |
| 2866 | } |
| 2867 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2868 | Status CameraService::turnOnTorchWithStrengthLevel(const std::string& unresolvedCameraId, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2869 | int32_t torchStrength, const sp<IBinder>& clientBinder) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2870 | Mutex::Autolock lock(mServiceLock); |
| 2871 | |
| 2872 | ATRACE_CALL(); |
| 2873 | if (clientBinder == nullptr) { |
| 2874 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
| 2875 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2876 | "Torch client binder in null."); |
| 2877 | } |
| 2878 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2879 | int uid = CameraThreadState::getCallingUid(); |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 2880 | const std::string cameraId = resolveCameraId(unresolvedCameraId, uid); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2881 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2882 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to change the strength level" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2883 | "for system only device %s: ", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2884 | } |
| 2885 | |
| 2886 | // verify id is valid |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2887 | auto state = getCameraState(cameraId); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2888 | if (state == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2889 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2890 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2891 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | StatusInternal cameraStatus = state->getStatus(); |
| 2895 | if (cameraStatus != StatusInternal::NOT_AVAILABLE && |
| 2896 | cameraStatus != StatusInternal::PRESENT) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2897 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, cameraId.c_str(), |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2898 | (int)cameraStatus); |
| 2899 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2900 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | { |
| 2904 | Mutex::Autolock al(mTorchStatusMutex); |
| 2905 | TorchModeStatus status; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2906 | status_t err = getTorchStatusLocked(cameraId, &status); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2907 | if (err != OK) { |
| 2908 | if (err == NAME_NOT_FOUND) { |
| 2909 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2910 | "Camera \"%s\" does not have a flash unit", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2911 | } |
| 2912 | ALOGE("%s: getting current torch status failed for camera %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2913 | __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2914 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2915 | "Error changing torch strength level for camera \"%s\": %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2916 | cameraId.c_str(), strerror(-err), err); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
| 2920 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
| 2921 | ALOGE("%s: torch mode of camera %s is not available because " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2922 | "camera is in use.", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2923 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2924 | "Torch for camera \"%s\" is not available due to an existing camera user", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2925 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2926 | } else { |
| 2927 | ALOGE("%s: torch mode of camera %s is not available due to " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2928 | "insufficient resources", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2929 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2930 | "Torch for camera \"%s\" is not available due to insufficient resources", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2931 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2932 | } |
| 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | { |
| 2937 | Mutex::Autolock al(mTorchUidMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2938 | updateTorchUidMapLocked(cameraId, uid); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2939 | } |
| 2940 | // Check if the current torch strength level is same as the new one. |
| 2941 | bool shouldSkipTorchStrengthUpdates = mCameraProviderManager->shouldSkipTorchStrengthUpdate( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2942 | cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2943 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2944 | status_t err = mFlashlight->turnOnTorchWithStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2945 | |
| 2946 | if (err != OK) { |
| 2947 | int32_t errorCode; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2948 | std::string msg; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2949 | switch (err) { |
| 2950 | case -ENOSYS: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2951 | msg = fmt::sprintf("Camera \"%s\" has no flashlight.", |
| 2952 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2953 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2954 | break; |
| 2955 | case -EBUSY: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2956 | msg = fmt::sprintf("Camera \"%s\" is in use", |
| 2957 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2958 | errorCode = ERROR_CAMERA_IN_USE; |
| 2959 | break; |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 2960 | case -EINVAL: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2961 | msg = fmt::sprintf("Torch strength level %d is not within the " |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 2962 | "valid range.", torchStrength); |
| 2963 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2964 | break; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2965 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2966 | msg = "Changing torch strength level failed."; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2967 | errorCode = ERROR_INVALID_OPERATION; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2968 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2969 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2970 | return STATUS_ERROR(errorCode, msg.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | { |
| 2974 | // update the link to client's death |
| 2975 | // Store the last client that turns on each camera's torch mode. |
| 2976 | Mutex::Autolock al(mTorchClientMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2977 | ssize_t index = mTorchClientMap.indexOfKey(cameraId); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2978 | if (index == NAME_NOT_FOUND) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2979 | mTorchClientMap.add(cameraId, clientBinder); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2980 | } else { |
| 2981 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
| 2982 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2983 | } |
| 2984 | clientBinder->linkToDeath(this); |
| 2985 | } |
| 2986 | |
| 2987 | int clientPid = CameraThreadState::getCallingPid(); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2988 | ALOGI("%s: Torch strength for camera id %s changed to %d for client PID %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2989 | __FUNCTION__, cameraId.c_str(), torchStrength, clientPid); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2990 | if (!shouldSkipTorchStrengthUpdates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2991 | broadcastTorchStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2992 | } |
| 2993 | return Status::ok(); |
| 2994 | } |
| 2995 | |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 2996 | Status CameraService::setTorchMode(const std::string& unresolvedCameraId, bool enabled, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2997 | const sp<IBinder>& clientBinder) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2998 | Mutex::Autolock lock(mServiceLock); |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2999 | |
| 3000 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3001 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3002 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3003 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 3004 | "Torch client Binder is null"); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3005 | } |
| 3006 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3007 | int uid = CameraThreadState::getCallingUid(); |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 3008 | const std::string cameraId = resolveCameraId(unresolvedCameraId, uid); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3009 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3010 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3011 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3012 | " for system only device %s: ", cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3013 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3014 | // verify id is valid. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3015 | auto state = getCameraState(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3016 | if (state == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3017 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3018 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3019 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3020 | } |
| 3021 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3022 | StatusInternal cameraStatus = state->getStatus(); |
| 3023 | if (cameraStatus != StatusInternal::PRESENT && |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 3024 | cameraStatus != StatusInternal::NOT_AVAILABLE) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3025 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, cameraId.c_str(), |
| 3026 | (int)cameraStatus); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3027 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3028 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | { |
| 3032 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3033 | TorchModeStatus status; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3034 | status_t err = getTorchStatusLocked(cameraId, &status); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3035 | if (err != OK) { |
| 3036 | if (err == NAME_NOT_FOUND) { |
| 3037 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3038 | "Camera \"%s\" does not have a flash unit", cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3039 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3040 | ALOGE("%s: getting current torch status failed for camera %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3041 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3042 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3043 | "Error updating torch status for camera \"%s\": %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3044 | strerror(-err), err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3045 | } |
| 3046 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3047 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 3048 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3049 | ALOGE("%s: torch mode of camera %s is not available because " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3050 | "camera is in use", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3051 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 3052 | "Torch for camera \"%s\" is not available due to an existing camera user", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3053 | cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3054 | } else { |
| 3055 | ALOGE("%s: torch mode of camera %s is not available due to " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3056 | "insufficient resources", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3057 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 3058 | "Torch for camera \"%s\" is not available due to insufficient resources", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3059 | cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3060 | } |
| 3061 | } |
| 3062 | } |
| 3063 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3064 | { |
| 3065 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 3066 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 3067 | Mutex::Autolock al(mTorchUidMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3068 | updateTorchUidMapLocked(cameraId, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3069 | } |
| 3070 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3071 | status_t err = mFlashlight->setTorchMode(cameraId, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3072 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3073 | if (err != OK) { |
| 3074 | int32_t errorCode; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3075 | std::string msg; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3076 | switch (err) { |
| 3077 | case -ENOSYS: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3078 | msg = fmt::sprintf("Camera \"%s\" has no flashlight", |
| 3079 | cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3080 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 3081 | break; |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 3082 | case -EBUSY: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3083 | msg = fmt::sprintf("Camera \"%s\" is in use", |
| 3084 | cameraId.c_str()); |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 3085 | errorCode = ERROR_CAMERA_IN_USE; |
| 3086 | break; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3087 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3088 | msg = fmt::sprintf( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3089 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3090 | cameraId.c_str(), enabled, strerror(-err), err); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3091 | errorCode = ERROR_INVALID_OPERATION; |
| 3092 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3093 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3094 | logServiceError(msg, errorCode); |
| 3095 | return STATUS_ERROR(errorCode, msg.c_str()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3096 | } |
| 3097 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3098 | { |
| 3099 | // update the link to client's death |
| 3100 | Mutex::Autolock al(mTorchClientMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3101 | ssize_t index = mTorchClientMap.indexOfKey(cameraId); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3102 | if (enabled) { |
| 3103 | if (index == NAME_NOT_FOUND) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3104 | mTorchClientMap.add(cameraId, clientBinder); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3105 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3106 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3107 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 3108 | } |
| 3109 | clientBinder->linkToDeath(this); |
| 3110 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3111 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3112 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3113 | } |
| 3114 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3115 | int clientPid = CameraThreadState::getCallingPid(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3116 | std::string torchState = enabled ? "on" : "off"; |
| 3117 | ALOGI("Torch for camera id %s turned %s for client PID %d", cameraId.c_str(), |
| 3118 | torchState.c_str(), clientPid); |
| 3119 | logTorchEvent(cameraId, torchState, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3120 | return Status::ok(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3121 | } |
| 3122 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3123 | void CameraService::updateTorchUidMapLocked(const std::string& cameraId, int uid) { |
| 3124 | if (mTorchUidMap.find(cameraId) == mTorchUidMap.end()) { |
| 3125 | mTorchUidMap[cameraId].first = uid; |
| 3126 | mTorchUidMap[cameraId].second = uid; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3127 | } else { |
| 3128 | // Set the pending UID |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3129 | mTorchUidMap[cameraId].first = uid; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3130 | } |
| 3131 | } |
| 3132 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3133 | Status CameraService::notifySystemEvent(int32_t eventId, |
| 3134 | const std::vector<int32_t>& args) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3135 | const int pid = CameraThreadState::getCallingPid(); |
| 3136 | const int selfPid = getpid(); |
| 3137 | |
| 3138 | // Permission checks |
| 3139 | if (pid != selfPid) { |
| 3140 | // Ensure we're being called by system_server, or similar process with |
| 3141 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3142 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3143 | const int uid = CameraThreadState::getCallingUid(); |
| 3144 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 3145 | " events from pid=%d, uid=%d", pid, uid); |
| 3146 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 3147 | "No permission to send updates to camera service about system events" |
| 3148 | " from pid=%d, uid=%d", pid, uid); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3149 | } |
| 3150 | } |
| 3151 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3152 | ATRACE_CALL(); |
| 3153 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3154 | switch(eventId) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3155 | case ICameraService::EVENT_USER_SWITCHED: { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3156 | // 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] | 3157 | // from a system server crash |
| 3158 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3159 | mSensorPrivacyPolicy->registerSelf(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3160 | doUserSwitch(/*newUserIds*/ args); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3161 | break; |
| 3162 | } |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 3163 | case ICameraService::EVENT_USB_DEVICE_ATTACHED: |
| 3164 | case ICameraService::EVENT_USB_DEVICE_DETACHED: { |
Pawan Wagh | 99f44e2 | 2023-07-05 21:26:43 +0000 | [diff] [blame] | 3165 | if (args.size() != 1) { |
| 3166 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, |
| 3167 | "USB Device Event requires 1 argument"); |
| 3168 | } |
| 3169 | |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 3170 | // Notify CameraProviderManager for lazy HALs |
| 3171 | mCameraProviderManager->notifyUsbDeviceEvent(eventId, |
| 3172 | std::to_string(args[0])); |
| 3173 | break; |
| 3174 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3175 | case ICameraService::EVENT_NONE: |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3176 | default: { |
| 3177 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 3178 | eventId); |
| 3179 | break; |
| 3180 | } |
| 3181 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3182 | return Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3183 | } |
| 3184 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3185 | void CameraService::notifyMonitoredUids() { |
| 3186 | Mutex::Autolock lock(mStatusListenerLock); |
| 3187 | |
| 3188 | for (const auto& it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3189 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 3190 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 3191 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3192 | } |
| 3193 | } |
| 3194 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3195 | void CameraService::notifyMonitoredUids(const std::unordered_set<uid_t> ¬ifyUidSet) { |
| 3196 | Mutex::Autolock lock(mStatusListenerLock); |
| 3197 | |
| 3198 | for (const auto& it : mListenerList) { |
| 3199 | if (notifyUidSet.find(it->getListenerUid()) != notifyUidSet.end()) { |
| 3200 | ALOGV("%s: notifying uid %d", __FUNCTION__, it->getListenerUid()); |
| 3201 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
| 3202 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 3203 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
| 3204 | } |
| 3205 | } |
| 3206 | } |
| 3207 | |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3208 | Status CameraService::notifyDeviceStateChange(int64_t newState) { |
| 3209 | const int pid = CameraThreadState::getCallingPid(); |
| 3210 | const int selfPid = getpid(); |
| 3211 | |
| 3212 | // Permission checks |
| 3213 | if (pid != selfPid) { |
| 3214 | // Ensure we're being called by system_server, or similar process with |
| 3215 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3216 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3217 | const int uid = CameraThreadState::getCallingUid(); |
| 3218 | ALOGE("Permission Denial: cannot send updates to camera service about device" |
| 3219 | " state changes from pid=%d, uid=%d", pid, uid); |
| 3220 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 3221 | "No permission to send updates to camera service about device state" |
| 3222 | " changes from pid=%d, uid=%d", pid, uid); |
| 3223 | } |
| 3224 | } |
| 3225 | |
| 3226 | ATRACE_CALL(); |
| 3227 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3228 | { |
| 3229 | Mutex::Autolock lock(mServiceLock); |
| 3230 | mDeviceState = newState; |
| 3231 | } |
| 3232 | |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 3233 | mCameraProviderManager->notifyDeviceStateChange(newState); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3234 | |
| 3235 | return Status::ok(); |
| 3236 | } |
| 3237 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3238 | Status CameraService::notifyDisplayConfigurationChange() { |
| 3239 | ATRACE_CALL(); |
| 3240 | const int callingPid = CameraThreadState::getCallingPid(); |
| 3241 | const int selfPid = getpid(); |
| 3242 | |
| 3243 | // Permission checks |
| 3244 | if (callingPid != selfPid) { |
| 3245 | // Ensure we're being called by system_server, or similar process with |
| 3246 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3247 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3248 | const int uid = CameraThreadState::getCallingUid(); |
| 3249 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" |
| 3250 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 3251 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 3252 | "No permission to send updates to camera service about orientation" |
| 3253 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 3254 | } |
| 3255 | } |
| 3256 | |
| 3257 | Mutex::Autolock lock(mServiceLock); |
| 3258 | |
| 3259 | // Don't do anything if rotate-and-crop override via cmd is active |
| 3260 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); |
| 3261 | |
| 3262 | const auto clients = mActiveClientManager.getAll(); |
| 3263 | for (auto& current : clients) { |
| 3264 | if (current != nullptr) { |
| 3265 | const auto basicClient = current->getValue(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3266 | if (basicClient.get() != nullptr && !basicClient->getOverrideToPortrait()) { |
| 3267 | basicClient->setRotateAndCropOverride( |
| 3268 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 3269 | basicClient->getPackageName(), |
| 3270 | basicClient->getCameraFacing(), |
| 3271 | multiuser_get_user_id(basicClient->getClientUid()))); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3272 | } |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | return Status::ok(); |
| 3277 | } |
| 3278 | |
| 3279 | Status CameraService::getConcurrentCameraIds( |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3280 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { |
| 3281 | ATRACE_CALL(); |
| 3282 | if (!concurrentCameraIds) { |
| 3283 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); |
| 3284 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); |
| 3285 | } |
| 3286 | |
| 3287 | if (!mInitialized) { |
| 3288 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3289 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3290 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 3291 | "Camera subsystem is not available"); |
| 3292 | } |
| 3293 | // First call into the provider and get the set of concurrent camera |
| 3294 | // combinations |
| 3295 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = |
Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 3296 | mCameraProviderManager->getConcurrentCameraIds(); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3297 | for (auto &combination : concurrentCameraCombinations) { |
| 3298 | std::vector<std::string> validCombination; |
| 3299 | for (auto &cameraId : combination) { |
| 3300 | // if the camera state is not present, skip |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3301 | auto state = getCameraState(cameraId); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3302 | if (state == nullptr) { |
| 3303 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); |
| 3304 | continue; |
| 3305 | } |
| 3306 | StatusInternal status = state->getStatus(); |
| 3307 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { |
| 3308 | continue; |
| 3309 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3310 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3311 | continue; |
| 3312 | } |
| 3313 | validCombination.push_back(cameraId); |
| 3314 | } |
| 3315 | if (validCombination.size() != 0) { |
| 3316 | concurrentCameraIds->push_back(std::move(validCombination)); |
| 3317 | } |
| 3318 | } |
| 3319 | return Status::ok(); |
| 3320 | } |
| 3321 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 3322 | bool CameraService::hasCameraPermissions() const { |
| 3323 | int callingPid = CameraThreadState::getCallingPid(); |
| 3324 | int callingUid = CameraThreadState::getCallingUid(); |
| 3325 | AttributionSourceState attributionSource{}; |
| 3326 | attributionSource.pid = callingPid; |
| 3327 | attributionSource.uid = callingUid; |
| 3328 | bool res = checkPermission(std::string(), sCameraPermission, |
| 3329 | attributionSource, std::string(), AppOpsManager::OP_NONE); |
| 3330 | |
| 3331 | bool hasPermission = ((callingPid == getpid()) || res); |
| 3332 | if (!hasPermission) { |
| 3333 | ALOGE("%s: pid %d doesn't have camera permissions", __FUNCTION__, callingPid); |
| 3334 | } |
| 3335 | return hasPermission; |
| 3336 | } |
| 3337 | |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3338 | Status CameraService::isConcurrentSessionConfigurationSupported( |
| 3339 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 3340 | int targetSdkVersion, /*out*/bool* isSupported) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3341 | if (!isSupported) { |
| 3342 | ALOGE("%s: isSupported is NULL", __FUNCTION__); |
| 3343 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); |
| 3344 | } |
| 3345 | |
| 3346 | if (!mInitialized) { |
| 3347 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 3348 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 3349 | "Camera subsystem is not available"); |
| 3350 | } |
| 3351 | |
| 3352 | // Check for camera permissions |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 3353 | if (!hasCameraPermissions()) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3354 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 3355 | "android.permission.CAMERA needed to call" |
| 3356 | "isConcurrentSessionConfigurationSupported"); |
| 3357 | } |
| 3358 | |
| 3359 | status_t res = |
| 3360 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 3361 | cameraIdsAndSessionConfigurations, mPerfClassPrimaryCameraIds, |
| 3362 | targetSdkVersion, isSupported); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3363 | if (res != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3364 | logServiceError("Unable to query session configuration support", |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3365 | ERROR_INVALID_OPERATION); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3366 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " |
| 3367 | "support %s (%d)", strerror(-res), res); |
| 3368 | } |
| 3369 | return Status::ok(); |
| 3370 | } |
| 3371 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3372 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, |
| 3373 | /*out*/ |
| 3374 | std::vector<hardware::CameraStatus> *cameraStatuses) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3375 | return addListenerHelper(listener, cameraStatuses); |
| 3376 | } |
| 3377 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3378 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, |
| 3379 | std::vector<hardware::CameraStatus>* cameraStatuses) { |
| 3380 | return addListenerHelper(listener, cameraStatuses, false, true); |
| 3381 | } |
| 3382 | |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3383 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, |
| 3384 | /*out*/ |
| 3385 | std::vector<hardware::CameraStatus> *cameraStatuses, |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3386 | bool isVendorListener, bool isProcessLocalTest) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3387 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3388 | ATRACE_CALL(); |
| 3389 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3390 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3391 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 3392 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3393 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3394 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3395 | } |
| 3396 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3397 | auto clientUid = CameraThreadState::getCallingUid(); |
| 3398 | auto clientPid = CameraThreadState::getCallingPid(); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 3399 | AttributionSourceState attributionSource{}; |
| 3400 | attributionSource.uid = clientUid; |
| 3401 | attributionSource.pid = clientPid; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 3402 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3403 | bool openCloseCallbackAllowed = checkPermission(std::string(), |
| 3404 | sCameraOpenCloseListenerPermission, attributionSource, std::string(), |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 3405 | AppOpsManager::OP_NONE); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3406 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3407 | Mutex::Autolock lock(mServiceLock); |
| 3408 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3409 | { |
| 3410 | Mutex::Autolock lock(mStatusListenerLock); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3411 | for (const auto &it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3412 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3413 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 3414 | __FUNCTION__, listener.get()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3415 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3416 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3417 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3418 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3419 | sp<ServiceListener> serviceListener = |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3420 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, |
| 3421 | openCloseCallbackAllowed); |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3422 | auto ret = serviceListener->initialize(isProcessLocalTest); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3423 | if (ret != NO_ERROR) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3424 | std::string msg = fmt::sprintf("Failed to initialize service listener: %s (%d)", |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3425 | strerror(-ret), ret); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3426 | logServiceError(msg, ERROR_ILLEGAL_ARGUMENT); |
| 3427 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3428 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3429 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3430 | // The listener still needs to be added to the list of listeners, regardless of what |
| 3431 | // permissions the listener process has / whether it is a vendor listener. Since it might be |
| 3432 | // eligible to listen to other camera ids. |
| 3433 | mListenerList.emplace_back(serviceListener); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3434 | mUidPolicy->registerMonitorUid(clientUid, /*openCamera*/false); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3435 | } |
| 3436 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3437 | /* Collect current devices and status */ |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3438 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3439 | Mutex::Autolock lock(mCameraStatesLock); |
| 3440 | for (auto& i : mCameraStates) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3441 | cameraStatuses->emplace_back(i.first, |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 3442 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds(), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3443 | openCloseCallbackAllowed ? i.second->getClientPackage() : std::string()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3444 | } |
| 3445 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3446 | // Remove the camera statuses that should be hidden from the client, we do |
| 3447 | // this after collecting the states in order to avoid holding |
| 3448 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at |
| 3449 | // the same time. |
| 3450 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), |
| 3451 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { |
| 3452 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 3453 | if (getSystemCameraKind(s.cameraId, &deviceKind) != OK) { |
| 3454 | ALOGE("%s: Invalid camera id %s, skipping status update", |
| 3455 | __FUNCTION__, s.cameraId.c_str()); |
| 3456 | return true; |
| 3457 | } |
| 3458 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, |
| 3459 | clientUid);}), cameraStatuses->end()); |
| 3460 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3461 | //cameraStatuses will have non-eligible camera ids removed. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3462 | std::set<std::string> idsChosenForCallback; |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3463 | for (const auto &s : *cameraStatuses) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3464 | idsChosenForCallback.insert(s.cameraId); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3465 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3466 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3467 | /* |
| 3468 | * Immediately signal current torch status to this listener only |
| 3469 | * This may be a subset of all the devices, so don't include it in the response directly |
| 3470 | */ |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3471 | { |
| 3472 | Mutex::Autolock al(mTorchStatusMutex); |
| 3473 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3474 | const std::string &id = mTorchStatusMap.keyAt(i); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3475 | // The camera id is visible to the client. Fine to send torch |
| 3476 | // callback. |
| 3477 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { |
| 3478 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id); |
| 3479 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3480 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3481 | } |
| 3482 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3483 | return Status::ok(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3484 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3485 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3486 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3487 | ATRACE_CALL(); |
| 3488 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3489 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 3490 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3491 | if (listener == 0) { |
| 3492 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3493 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3494 | } |
| 3495 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3496 | Mutex::Autolock lock(mServiceLock); |
| 3497 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3498 | { |
| 3499 | Mutex::Autolock lock(mStatusListenerLock); |
| 3500 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3501 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3502 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid(), /*closeCamera*/false); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3503 | IInterface::asBinder(listener)->unlinkToDeath(*it); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3504 | mListenerList.erase(it); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3505 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3506 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3507 | } |
| 3508 | } |
| 3509 | |
| 3510 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 3511 | __FUNCTION__, listener.get()); |
| 3512 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3513 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3514 | } |
| 3515 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3516 | Status CameraService::getLegacyParameters(int cameraId, /*out*/std::string* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3517 | |
| 3518 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3519 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 3520 | |
| 3521 | if (parameters == NULL) { |
| 3522 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3523 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3524 | } |
| 3525 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3526 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3527 | |
| 3528 | CameraParameters shimParams; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3529 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3530 | // Error logged by caller |
| 3531 | return ret; |
| 3532 | } |
| 3533 | |
| 3534 | String8 shimParamsString8 = shimParams.flatten(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3535 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3536 | *parameters = toStdString(shimParamsString8); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3537 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3538 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3539 | } |
| 3540 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3541 | Status CameraService::supportsCameraApi(const std::string& unresolvedCameraId, int apiVersion, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3542 | /*out*/ bool *isSupported) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3543 | ATRACE_CALL(); |
| 3544 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 3545 | const std::string cameraId = resolveCameraId( |
| 3546 | unresolvedCameraId, CameraThreadState::getCallingUid()); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 3547 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3548 | ALOGV("%s: for camera ID = %s", __FUNCTION__, cameraId.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3549 | |
| 3550 | switch (apiVersion) { |
| 3551 | case API_VERSION_1: |
| 3552 | case API_VERSION_2: |
| 3553 | break; |
| 3554 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3555 | std::string msg = fmt::sprintf("Unknown API version %d", apiVersion); |
| 3556 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3557 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3558 | } |
| 3559 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3560 | int portraitRotation; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3561 | auto deviceVersionAndTransport = getDeviceVersion(cameraId, false, &portraitRotation); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3562 | if (deviceVersionAndTransport.first == -1) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3563 | std::string msg = fmt::sprintf("Unknown camera ID %s", cameraId.c_str()); |
| 3564 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3565 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3566 | } |
| 3567 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 3568 | int deviceVersion = deviceVersionAndTransport.first; |
| 3569 | switch (deviceVersion) { |
| 3570 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 3571 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 3572 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 3573 | if (apiVersion == API_VERSION_2) { |
| 3574 | ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3575 | "shim", __FUNCTION__, cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3576 | *isSupported = false; |
| 3577 | } else { // if (apiVersion == API_VERSION_1) { |
| 3578 | ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3579 | "supported", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3580 | *isSupported = true; |
| 3581 | } |
| 3582 | break; |
| 3583 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 3584 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 3585 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 3586 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 3587 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 3588 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 3589 | ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3590 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3591 | *isSupported = true; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3592 | break; |
| 3593 | default: { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3594 | std::string msg = fmt::sprintf("Unknown device version %x for device %s", |
| 3595 | deviceVersion, cameraId.c_str()); |
| 3596 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3597 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3598 | } |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3599 | } |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3600 | } else { |
| 3601 | *isSupported = true; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3602 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3603 | return Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3604 | } |
| 3605 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3606 | Status CameraService::isHiddenPhysicalCamera(const std::string& unresolvedCameraId, |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3607 | /*out*/ bool *isSupported) { |
| 3608 | ATRACE_CALL(); |
| 3609 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 3610 | const std::string cameraId = resolveCameraId(unresolvedCameraId, |
| 3611 | CameraThreadState::getCallingUid()); |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3612 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3613 | ALOGV("%s: for camera ID = %s", __FUNCTION__, cameraId.c_str()); |
| 3614 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(cameraId); |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3615 | |
| 3616 | return Status::ok(); |
| 3617 | } |
| 3618 | |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3619 | Status CameraService::injectCamera( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3620 | const std::string& packageName, const std::string& internalCamId, |
| 3621 | const std::string& externalCamId, |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3622 | const sp<ICameraInjectionCallback>& callback, |
| 3623 | /*out*/ |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3624 | sp<ICameraInjectionSession>* cameraInjectionSession) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3625 | ATRACE_CALL(); |
| 3626 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3627 | if (!checkCallingPermission(toString16(sCameraInjectExternalCameraPermission))) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3628 | const int pid = CameraThreadState::getCallingPid(); |
| 3629 | const int uid = CameraThreadState::getCallingUid(); |
| 3630 | ALOGE("Permission Denial: can't inject camera pid=%d, uid=%d", pid, uid); |
| 3631 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 3632 | "Permission Denial: no permission to inject camera"); |
| 3633 | } |
| 3634 | |
| 3635 | ALOGV( |
| 3636 | "%s: Package name = %s, Internal camera ID = %s, External camera ID = " |
| 3637 | "%s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3638 | __FUNCTION__, packageName.c_str(), |
| 3639 | internalCamId.c_str(), externalCamId.c_str()); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3640 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3641 | { |
| 3642 | Mutex::Autolock lock(mInjectionParametersLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3643 | mInjectionInternalCamId = internalCamId; |
| 3644 | mInjectionExternalCamId = externalCamId; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3645 | mInjectionStatusListener->addListener(callback); |
| 3646 | *cameraInjectionSession = new CameraInjectionSession(this); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3647 | status_t res = NO_ERROR; |
| 3648 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 3649 | // If the client already exists, we can directly connect to the camera device through the |
| 3650 | // client's injectCamera(), otherwise we need to wait until the client is established |
| 3651 | // (execute connectHelper()) before injecting the camera to the camera device. |
| 3652 | if (clientDescriptor != nullptr) { |
| 3653 | mInjectionInitPending = false; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3654 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 3655 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 3656 | if(res != OK) { |
| 3657 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 3658 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3659 | mInjectionExternalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3660 | } |
| 3661 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3662 | if(res != OK) { |
| 3663 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 3664 | } |
| 3665 | } else { |
| 3666 | mInjectionInitPending = true; |
| 3667 | } |
| 3668 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3669 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3670 | return binder::Status::ok(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3671 | } |
| 3672 | |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 3673 | Status CameraService::reportExtensionSessionStats( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3674 | const hardware::CameraExtensionSessionStats& stats, std::string* sessionKey /*out*/) { |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 3675 | ALOGV("%s: reported %s", __FUNCTION__, stats.toString().c_str()); |
| 3676 | *sessionKey = mCameraServiceProxyWrapper->updateExtensionStats(stats); |
| 3677 | return Status::ok(); |
| 3678 | } |
| 3679 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3680 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3681 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3682 | for (auto& i : mActiveClientManager.getAll()) { |
| 3683 | auto clientSp = i->getValue(); |
| 3684 | if (clientSp.get() == client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3685 | cacheClientTagDumpIfNeeded(client->mCameraIdStr, clientSp.get()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3686 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3687 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3688 | } |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3689 | updateAudioRestrictionLocked(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3690 | } |
| 3691 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3692 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3693 | bool ret = false; |
| 3694 | { |
| 3695 | // Acquire mServiceLock and prevent other clients from connecting |
| 3696 | std::unique_ptr<AutoConditionLock> lock = |
| 3697 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3698 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3699 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3700 | std::vector<sp<BasicClient>> evicted; |
| 3701 | for (auto& i : mActiveClientManager.getAll()) { |
| 3702 | auto clientSp = i->getValue(); |
| 3703 | if (clientSp.get() == nullptr) { |
| 3704 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3705 | mActiveClientManager.remove(i); |
| 3706 | continue; |
| 3707 | } |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 3708 | if (remote == clientSp->getRemote()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3709 | mActiveClientManager.remove(i); |
| 3710 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3711 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3712 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3713 | clientSp->notifyError( |
| 3714 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3715 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3716 | } |
| 3717 | } |
| 3718 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3719 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 3720 | // other clients from connecting in mServiceLockWrapper if held |
| 3721 | mServiceLock.unlock(); |
| 3722 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3723 | // Do not clear caller identity, remote caller should be client proccess |
| 3724 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3725 | for (auto& i : evicted) { |
| 3726 | if (i.get() != nullptr) { |
| 3727 | i->disconnect(); |
| 3728 | ret = true; |
| 3729 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3730 | } |
| 3731 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3732 | // Reacquire mServiceLock |
| 3733 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3734 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3735 | } // lock is destroyed, allow further connect calls |
| 3736 | |
| 3737 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3738 | } |
| 3739 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3740 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3741 | const std::string& cameraId) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3742 | std::shared_ptr<CameraState> state; |
| 3743 | { |
| 3744 | Mutex::Autolock lock(mCameraStatesLock); |
| 3745 | auto iter = mCameraStates.find(cameraId); |
| 3746 | if (iter != mCameraStates.end()) { |
| 3747 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3748 | } |
| 3749 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3750 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3751 | } |
| 3752 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3753 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const std::string& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3754 | // Remove from active clients list |
| 3755 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 3756 | if (clientDescriptorPtr == nullptr) { |
| 3757 | ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3758 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3759 | return sp<BasicClient>{nullptr}; |
| 3760 | } |
| 3761 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3762 | sp<BasicClient> client = clientDescriptorPtr->getValue(); |
| 3763 | if (client.get() != nullptr) { |
| 3764 | cacheClientTagDumpIfNeeded(clientDescriptorPtr->getKey(), client.get()); |
| 3765 | } |
| 3766 | return client; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 3767 | } |
| 3768 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3769 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3770 | // Acquire mServiceLock and prevent other clients from connecting |
| 3771 | std::unique_ptr<AutoConditionLock> lock = |
| 3772 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 3773 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3774 | std::set<userid_t> newAllowedUsers; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3775 | for (size_t i = 0; i < newUserIds.size(); i++) { |
| 3776 | if (newUserIds[i] < 0) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3777 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3778 | __FUNCTION__, newUserIds[i]); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3779 | return; |
| 3780 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3781 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3782 | } |
| 3783 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3784 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3785 | if (newAllowedUsers == mAllowedUsers) { |
| 3786 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 3787 | return; |
| 3788 | } |
| 3789 | |
| 3790 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 3791 | |
| 3792 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3793 | |
| 3794 | // Current user has switched, evict all current clients. |
| 3795 | std::vector<sp<BasicClient>> evicted; |
| 3796 | for (auto& i : mActiveClientManager.getAll()) { |
| 3797 | auto clientSp = i->getValue(); |
| 3798 | |
| 3799 | if (clientSp.get() == nullptr) { |
| 3800 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3801 | continue; |
| 3802 | } |
| 3803 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3804 | // Don't evict clients that are still allowed. |
| 3805 | uid_t clientUid = clientSp->getClientUid(); |
| 3806 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 3807 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 3808 | continue; |
| 3809 | } |
| 3810 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3811 | evicted.push_back(clientSp); |
| 3812 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3813 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3814 | i->getKey().c_str()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3815 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3816 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3817 | logEvent(fmt::sprintf("EVICT device %s client held by package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3818 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3819 | " to user switch.", i->getKey().c_str(), |
| 3820 | clientSp->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3821 | i->getOwnerId(), i->getPriority().getScore(), |
| 3822 | i->getPriority().getState())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3823 | |
| 3824 | } |
| 3825 | |
| 3826 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 3827 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 3828 | mServiceLock.unlock(); |
| 3829 | |
| 3830 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3831 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3832 | |
| 3833 | for (auto& i : evicted) { |
| 3834 | i->disconnect(); |
| 3835 | } |
| 3836 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3837 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3838 | |
| 3839 | // Reacquire mServiceLock |
| 3840 | mServiceLock.lock(); |
| 3841 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3842 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3843 | void CameraService::logEvent(const std::string &event) { |
| 3844 | std::string curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3845 | Mutex::Autolock l(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3846 | std::string msg = curTime + " : " + event; |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3847 | // For service error events, print the msg only once. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3848 | if (msg.find("SERVICE ERROR") != std::string::npos) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3849 | mEventLog.add(msg); |
| 3850 | } else if(sServiceErrorEventSet.find(msg) == sServiceErrorEventSet.end()) { |
| 3851 | // Error event not added to the dumpsys log before |
| 3852 | mEventLog.add(msg); |
| 3853 | sServiceErrorEventSet.insert(msg); |
| 3854 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3855 | } |
| 3856 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3857 | void CameraService::logDisconnected(const std::string &cameraId, int clientPid, |
| 3858 | const std::string &clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3859 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3860 | logEvent(fmt::sprintf("DISCONNECT device %s client for package %s (PID %d)", cameraId.c_str(), |
| 3861 | clientPackage.c_str(), clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3862 | } |
| 3863 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3864 | void CameraService::logDisconnectedOffline(const std::string &cameraId, int clientPid, |
| 3865 | const std::string &clientPackage) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3866 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3867 | logEvent(fmt::sprintf("DISCONNECT offline device %s client for package %s (PID %d)", |
| 3868 | cameraId.c_str(), clientPackage.c_str(), clientPid)); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3869 | } |
| 3870 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3871 | void CameraService::logConnected(const std::string &cameraId, int clientPid, |
| 3872 | const std::string &clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3873 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3874 | logEvent(fmt::sprintf("CONNECT device %s client for package %s (PID %d)", cameraId.c_str(), |
| 3875 | clientPackage.c_str(), clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3876 | } |
| 3877 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3878 | void CameraService::logConnectedOffline(const std::string &cameraId, int clientPid, |
| 3879 | const std::string &clientPackage) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3880 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3881 | logEvent(fmt::sprintf("CONNECT offline device %s client for package %s (PID %d)", |
| 3882 | cameraId.c_str(), clientPackage.c_str(), clientPid)); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3883 | } |
| 3884 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3885 | void CameraService::logRejected(const std::string &cameraId, int clientPid, |
| 3886 | const std::string &clientPackage, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3887 | // Log the client rejected |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3888 | logEvent(fmt::sprintf("REJECT device %s client for package %s (PID %d), reason: (%s)", |
| 3889 | cameraId.c_str(), clientPackage.c_str(), clientPid, reason.c_str())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3890 | } |
| 3891 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3892 | void CameraService::logTorchEvent(const std::string &cameraId, const std::string &torchState, |
| 3893 | int clientPid) { |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3894 | // Log torch event |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3895 | logEvent(fmt::sprintf("Torch for camera id %s turned %s for client PID %d", cameraId.c_str(), |
| 3896 | torchState.c_str(), clientPid)); |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3897 | } |
| 3898 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3899 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 3900 | const std::set<userid_t>& newUserIds) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3901 | std::string newUsers = toString(newUserIds); |
| 3902 | std::string oldUsers = toString(oldUserIds); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3903 | if (oldUsers.size() == 0) { |
| 3904 | oldUsers = "<None>"; |
| 3905 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3906 | // Log the new and old users |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3907 | logEvent(fmt::sprintf("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s", |
| 3908 | oldUsers.c_str(), newUsers.c_str())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3909 | } |
| 3910 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3911 | void CameraService::logDeviceRemoved(const std::string &cameraId, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3912 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3913 | logEvent(fmt::sprintf("REMOVE device %s, reason: (%s)", cameraId.c_str(), reason.c_str())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3914 | } |
| 3915 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3916 | void CameraService::logDeviceAdded(const std::string &cameraId, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3917 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3918 | logEvent(fmt::sprintf("ADD device %s, reason: (%s)", cameraId.c_str(), reason.c_str())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3919 | } |
| 3920 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3921 | void CameraService::logClientDied(int clientPid, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3922 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3923 | logEvent(fmt::sprintf("DIED client(s) with PID %d, reason: (%s)", clientPid, reason.c_str())); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3924 | } |
| 3925 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3926 | void CameraService::logServiceError(const std::string &msg, int errorCode) { |
| 3927 | logEvent(fmt::sprintf("SERVICE ERROR: %s : %d (%s)", msg.c_str(), errorCode, |
| 3928 | strerror(-errorCode))); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3929 | } |
| 3930 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3931 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 3932 | uint32_t flags) { |
| 3933 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3934 | // Permission checks |
| 3935 | switch (code) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3936 | case SHELL_COMMAND_TRANSACTION: { |
| 3937 | int in = data.readFileDescriptor(); |
| 3938 | int out = data.readFileDescriptor(); |
| 3939 | int err = data.readFileDescriptor(); |
| 3940 | int argc = data.readInt32(); |
| 3941 | Vector<String16> args; |
| 3942 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 3943 | args.add(data.readString16()); |
| 3944 | } |
| 3945 | sp<IBinder> unusedCallback; |
| 3946 | sp<IResultReceiver> resultReceiver; |
| 3947 | status_t status; |
| 3948 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 3949 | return status; |
| 3950 | } |
| 3951 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 3952 | return status; |
| 3953 | } |
| 3954 | status = shellCommand(in, out, err, args); |
| 3955 | if (resultReceiver != nullptr) { |
| 3956 | resultReceiver->send(status); |
| 3957 | } |
| 3958 | return NO_ERROR; |
| 3959 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | return BnCameraService::onTransact(code, data, reply, flags); |
| 3963 | } |
| 3964 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3965 | // We share the media players for shutter and recording sound for all clients. |
| 3966 | // A reference count is kept to determine when we will actually release the |
| 3967 | // media players. |
| 3968 | |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3969 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { |
| 3970 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 3971 | status_t error; |
| 3972 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 3973 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3974 | error = mp->prepare(); |
| 3975 | } |
| 3976 | if (error != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3977 | ALOGE("Failed to load CameraService sounds: %s", file); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3978 | mp->disconnect(); |
| 3979 | mp.clear(); |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3980 | return nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3981 | } |
| 3982 | return mp; |
| 3983 | } |
| 3984 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3985 | void CameraService::increaseSoundRef() { |
| 3986 | Mutex::Autolock lock(mSoundLock); |
| 3987 | mSoundRef++; |
| 3988 | } |
| 3989 | |
| 3990 | void CameraService::loadSoundLocked(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3991 | ATRACE_CALL(); |
| 3992 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3993 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); |
| 3994 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { |
| 3995 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); |
| 3996 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { |
| 3997 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 3998 | } |
| 3999 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) { |
| 4000 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); |
| 4001 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { |
| 4002 | mSoundPlayer[SOUND_RECORDING_START] = |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4003 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4004 | } |
| 4005 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { |
| 4006 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); |
| 4007 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { |
| 4008 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
| 4009 | } |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4010 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4011 | } |
| 4012 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4013 | void CameraService::decreaseSoundRef() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4014 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4015 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4016 | if (--mSoundRef) return; |
| 4017 | |
| 4018 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 4019 | if (mSoundPlayer[i] != 0) { |
| 4020 | mSoundPlayer[i]->disconnect(); |
| 4021 | mSoundPlayer[i].clear(); |
| 4022 | } |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4027 | ATRACE_CALL(); |
| 4028 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4029 | LOG1("playSound(%d)", kind); |
Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame] | 4030 | if (kind < 0 || kind >= NUM_SOUNDS) { |
| 4031 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); |
| 4032 | return; |
| 4033 | } |
| 4034 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4035 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4036 | loadSoundLocked(kind); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4037 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 4038 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 4039 | player->seekTo(0); |
| 4040 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4041 | } |
| 4042 | } |
| 4043 | |
| 4044 | // ---------------------------------------------------------------------------- |
| 4045 | |
| 4046 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 4047 | const sp<ICameraClient>& cameraClient, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4048 | const std::string& clientPackageName, bool systemNativeClient, |
| 4049 | const std::optional<std::string>& clientFeatureId, |
| 4050 | const std::string& cameraIdStr, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4051 | int api1CameraId, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4052 | int clientPid, uid_t clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 4053 | int servicePid, bool overrideToPortrait) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 4054 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 4055 | IInterface::asBinder(cameraClient), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4056 | clientPackageName, systemNativeClient, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4057 | cameraIdStr, cameraFacing, sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4058 | clientPid, clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 4059 | servicePid, overrideToPortrait), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4060 | mCameraId(api1CameraId) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4061 | { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 4062 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4063 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4064 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 4065 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4066 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4067 | cameraService->increaseSoundRef(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4068 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4069 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4070 | } |
| 4071 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4072 | // tear down the client |
| 4073 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4074 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4075 | mDestructionStarted = true; |
| 4076 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4077 | sCameraService->decreaseSoundRef(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 4078 | // unconditionally disconnect. function is idempotent |
| 4079 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4080 | } |
| 4081 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4082 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; |
| 4083 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4084 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4085 | const sp<IBinder>& remoteCallback, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4086 | const std::string& clientPackageName, bool nativeClient, |
| 4087 | const std::optional<std::string>& clientFeatureId, const std::string& cameraIdStr, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4088 | int cameraFacing, int sensorOrientation, int clientPid, uid_t clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 4089 | int servicePid, bool overrideToPortrait): |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4090 | mDestructionStarted(false), |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4091 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), mOrientation(sensorOrientation), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4092 | mClientPackageName(clientPackageName), mSystemNativeClient(nativeClient), |
| 4093 | mClientFeatureId(clientFeatureId), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 4094 | mClientPid(clientPid), mClientUid(clientUid), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4095 | mServicePid(servicePid), |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 4096 | mDisconnected(false), mUidIsTrusted(false), |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 4097 | mOverrideToPortrait(overrideToPortrait), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4098 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4099 | mRemoteBinder(remoteCallback), |
| 4100 | mOpsActive(false), |
| 4101 | mOpsStreaming(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4102 | { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4103 | if (sCameraService == nullptr) { |
| 4104 | sCameraService = cameraService; |
| 4105 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 4106 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4107 | // There are 2 scenarios in which a client won't have AppOps operations |
| 4108 | // (both scenarios : native clients) |
| 4109 | // 1) It's an system native client*, the package name will be empty |
| 4110 | // and it will return from this function in the previous if condition |
| 4111 | // (This is the same as the previously existing behavior). |
| 4112 | // 2) It is a system native client, but its package name has been |
| 4113 | // modified for debugging, however it still must not use AppOps since |
| 4114 | // the package name is not a real one. |
| 4115 | // |
| 4116 | // * system native client - native client with UID < AID_APP_START. It |
| 4117 | // doesn't exclude clients not on the system partition. |
| 4118 | if (!mSystemNativeClient) { |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4119 | mAppOpsManager = std::make_unique<AppOpsManager>(); |
| 4120 | } |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 4121 | |
| 4122 | mUidIsTrusted = isTrustedCallingUid(mClientUid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4126 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4127 | mDestructionStarted = true; |
| 4128 | } |
| 4129 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4130 | binder::Status CameraService::BasicClient::disconnect() { |
| 4131 | binder::Status res = Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4132 | if (mDisconnected) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4133 | return res; |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4134 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 4135 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4136 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4137 | sCameraService->removeByClient(this); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4138 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, mClientPackageName); |
Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 4139 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4140 | mCameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4141 | |
| 4142 | sp<IBinder> remote = getRemote(); |
| 4143 | if (remote != nullptr) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4144 | remote->unlinkToDeath(sCameraService); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4145 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4146 | |
| 4147 | finishCameraOps(); |
Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 4148 | // Notify flashlight that a camera device is closed. |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4149 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4150 | ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.c_str(), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4151 | mClientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4152 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4153 | // client shouldn't be able to call into us anymore |
| 4154 | mClientPid = 0; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4155 | |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 4156 | const auto& mActivityManager = getActivityManager(); |
| 4157 | if (mActivityManager) { |
| 4158 | mActivityManager->logFgsApiEnd(LOG_FGS_CAMERA_API, |
| 4159 | CameraThreadState::getCallingUid(), |
| 4160 | CameraThreadState::getCallingPid()); |
| 4161 | } |
| 4162 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4163 | return res; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4164 | } |
| 4165 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 4166 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 4167 | // No dumping of clients directly over Binder, |
| 4168 | // must go through CameraService::dump |
| 4169 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 4170 | CameraThreadState::getCallingUid(), NULL, 0); |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 4171 | return OK; |
| 4172 | } |
| 4173 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4174 | status_t CameraService::BasicClient::startWatchingTags(const std::string&, int) { |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4175 | // Can't watch tags directly, must go through CameraService::startWatchingTags |
| 4176 | return OK; |
| 4177 | } |
| 4178 | |
| 4179 | status_t CameraService::BasicClient::stopWatchingTags(int) { |
| 4180 | // Can't watch tags directly, must go through CameraService::stopWatchingTags |
| 4181 | return OK; |
| 4182 | } |
| 4183 | |
| 4184 | status_t CameraService::BasicClient::dumpWatchedEventsToVector(std::vector<std::string> &) { |
| 4185 | // Can't watch tags directly, must go through CameraService::dumpWatchedEventsToVector |
| 4186 | return OK; |
| 4187 | } |
| 4188 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4189 | std::string CameraService::BasicClient::getPackageName() const { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 4190 | return mClientPackageName; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4191 | } |
| 4192 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4193 | int CameraService::BasicClient::getCameraFacing() const { |
| 4194 | return mCameraFacing; |
| 4195 | } |
| 4196 | |
| 4197 | int CameraService::BasicClient::getCameraOrientation() const { |
| 4198 | return mOrientation; |
| 4199 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4200 | |
| 4201 | int CameraService::BasicClient::getClientPid() const { |
| 4202 | return mClientPid; |
| 4203 | } |
| 4204 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4205 | uid_t CameraService::BasicClient::getClientUid() const { |
| 4206 | return mClientUid; |
| 4207 | } |
| 4208 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 4209 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 4210 | // Defaults to API2. |
| 4211 | return level == API_2; |
| 4212 | } |
| 4213 | |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 4214 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4215 | { |
| 4216 | Mutex::Autolock l(mAudioRestrictionLock); |
| 4217 | mAudioRestriction = mode; |
| 4218 | } |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 4219 | sCameraService->updateAudioRestriction(); |
| 4220 | return OK; |
| 4221 | } |
| 4222 | |
| 4223 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4224 | return sCameraService->updateAudioRestriction(); |
| 4225 | } |
| 4226 | |
| 4227 | int32_t CameraService::BasicClient::getAudioRestriction() const { |
| 4228 | Mutex::Autolock l(mAudioRestrictionLock); |
| 4229 | return mAudioRestriction; |
| 4230 | } |
| 4231 | |
| 4232 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { |
| 4233 | switch (mode) { |
| 4234 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: |
| 4235 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: |
| 4236 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: |
| 4237 | return true; |
| 4238 | default: |
| 4239 | return false; |
| 4240 | } |
| 4241 | } |
| 4242 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4243 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { |
| 4244 | if (mode == AppOpsManager::MODE_ERRORED) { |
| 4245 | ALOGI("Camera %s: Access for \"%s\" has been revoked", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4246 | mCameraIdStr.c_str(), mClientPackageName.c_str()); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4247 | return PERMISSION_DENIED; |
| 4248 | } else if (!mUidIsTrusted && mode == AppOpsManager::MODE_IGNORED) { |
| 4249 | // If the calling Uid is trusted (a native service), the AppOpsManager could |
| 4250 | // return MODE_IGNORED. Do not treat such case as error. |
| 4251 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, |
| 4252 | mClientPackageName); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4253 | |
| 4254 | bool isCameraPrivacyEnabled; |
| 4255 | if (flags::camera_privacy_allowlist()) { |
| 4256 | isCameraPrivacyEnabled = sCameraService->isCameraPrivacyEnabled( |
| 4257 | toString16(mClientPackageName), std::string(), mClientPid, mClientUid); |
| 4258 | } else { |
| 4259 | isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4260 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4261 | } |
Jyoti Bhayana | 8143e57 | 2023-01-09 08:46:49 -0800 | [diff] [blame] | 4262 | // We don't want to return EACCESS if the CameraPrivacy is enabled. |
| 4263 | // We prefer to successfully open the camera and perform camera muting |
| 4264 | // or blocking in connectHelper as handleAppOpMode can be called before the |
| 4265 | // connection has been fully established and at that time camera muting |
| 4266 | // capabilities are unknown. |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4267 | if (!isUidActive || !isCameraPrivacyEnabled) { |
| 4268 | ALOGI("Camera %s: Access for \"%s\" has been restricted", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4269 | mCameraIdStr.c_str(), mClientPackageName.c_str()); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4270 | // Return the same error as for device policy manager rejection |
| 4271 | return -EACCES; |
| 4272 | } |
| 4273 | } |
| 4274 | return OK; |
| 4275 | } |
| 4276 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4277 | status_t CameraService::BasicClient::startCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4278 | ATRACE_CALL(); |
| 4279 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4280 | { |
| 4281 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4282 | __FUNCTION__, mClientPackageName.c_str(), mClientUid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4283 | } |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4284 | if (mAppOpsManager != nullptr) { |
| 4285 | // Notify app ops that the camera is not available |
| 4286 | mOpsCallback = new OpsCallback(this); |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4287 | |
| 4288 | if (flags::watch_foreground_changes()) { |
| 4289 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 4290 | toString16(mClientPackageName), |
| 4291 | AppOpsManager::WATCH_FOREGROUND_CHANGES, mOpsCallback); |
| 4292 | } else { |
| 4293 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4294 | toString16(mClientPackageName), mOpsCallback); |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4295 | } |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4296 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4297 | // Just check for camera acccess here on open - delay startOp until |
| 4298 | // camera frames start streaming in startCameraStreamingOps |
| 4299 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4300 | toString16(mClientPackageName)); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4301 | status_t res = handleAppOpMode(mode); |
| 4302 | if (res != OK) { |
| 4303 | return res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4304 | } |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 4305 | } |
| 4306 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4307 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4308 | |
| 4309 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4310 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4311 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4312 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid, /*openCamera*/true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4313 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4314 | // Notify listeners of camera open/close status |
| 4315 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); |
| 4316 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4317 | return OK; |
| 4318 | } |
| 4319 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4320 | status_t CameraService::BasicClient::startCameraStreamingOps() { |
| 4321 | ATRACE_CALL(); |
| 4322 | |
| 4323 | if (!mOpsActive) { |
| 4324 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 4325 | return INVALID_OPERATION; |
| 4326 | } |
| 4327 | if (mOpsStreaming) { |
| 4328 | ALOGV("%s: Streaming already active!", __FUNCTION__); |
| 4329 | return OK; |
| 4330 | } |
| 4331 | |
| 4332 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4333 | __FUNCTION__, mClientPackageName.c_str(), mClientUid); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4334 | |
| 4335 | if (mAppOpsManager != nullptr) { |
| 4336 | int32_t mode = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4337 | toString16(mClientPackageName), /*startIfModeDefault*/ false, |
| 4338 | toString16(mClientFeatureId), |
| 4339 | toString16("start camera ") + toString16(mCameraIdStr)); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4340 | status_t res = handleAppOpMode(mode); |
| 4341 | if (res != OK) { |
| 4342 | return res; |
| 4343 | } |
| 4344 | } |
| 4345 | |
| 4346 | mOpsStreaming = true; |
| 4347 | |
| 4348 | return OK; |
| 4349 | } |
| 4350 | |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4351 | status_t CameraService::BasicClient::noteAppOp() { |
| 4352 | ATRACE_CALL(); |
| 4353 | |
| 4354 | ALOGV("%s: Start camera noteAppOp, package name = %s, client UID = %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4355 | __FUNCTION__, mClientPackageName.c_str(), mClientUid); |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4356 | |
| 4357 | // noteAppOp is only used for when camera mute is not supported, in order |
| 4358 | // to trigger the sensor privacy "Unblock" dialog |
| 4359 | if (mAppOpsManager != nullptr) { |
| 4360 | int32_t mode = mAppOpsManager->noteOp(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4361 | toString16(mClientPackageName), toString16(mClientFeatureId), |
| 4362 | toString16("start camera ") + toString16(mCameraIdStr)); |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4363 | status_t res = handleAppOpMode(mode); |
| 4364 | if (res != OK) { |
| 4365 | return res; |
| 4366 | } |
| 4367 | } |
| 4368 | |
| 4369 | return OK; |
| 4370 | } |
| 4371 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4372 | status_t CameraService::BasicClient::finishCameraStreamingOps() { |
| 4373 | ATRACE_CALL(); |
| 4374 | |
| 4375 | if (!mOpsActive) { |
| 4376 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 4377 | return INVALID_OPERATION; |
| 4378 | } |
| 4379 | if (!mOpsStreaming) { |
| 4380 | ALOGV("%s: Streaming not active!", __FUNCTION__); |
| 4381 | return OK; |
| 4382 | } |
| 4383 | |
| 4384 | if (mAppOpsManager != nullptr) { |
| 4385 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4386 | toString16(mClientPackageName), toString16(mClientFeatureId)); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4387 | mOpsStreaming = false; |
| 4388 | } |
| 4389 | |
| 4390 | return OK; |
| 4391 | } |
| 4392 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4393 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4394 | ATRACE_CALL(); |
| 4395 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4396 | if (mOpsStreaming) { |
| 4397 | // Make sure we've notified everyone about camera stopping |
| 4398 | finishCameraStreamingOps(); |
| 4399 | } |
| 4400 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4401 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4402 | if (mOpsActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4403 | mOpsActive = false; |
| 4404 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 4405 | // This function is called when a client disconnects. This should |
| 4406 | // release the camera, but actually only if it was in a proper |
| 4407 | // functional state, i.e. with status NOT_AVAILABLE |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4408 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 4409 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4410 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4411 | // 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] | 4412 | sCameraService->updateStatus(StatusInternal::PRESENT, |
| 4413 | mCameraIdStr, rejected); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4414 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4415 | // Always stop watching, even if no camera op is active |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4416 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 4417 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 4418 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4419 | mOpsCallback.clear(); |
| 4420 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4421 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid, /*closeCamera*/true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4422 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4423 | // Notify listeners of camera open/close status |
| 4424 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); |
| 4425 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4426 | return OK; |
| 4427 | } |
| 4428 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4429 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4430 | ATRACE_CALL(); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4431 | if (mAppOpsManager == nullptr) { |
| 4432 | return; |
| 4433 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 4434 | // TODO : add offline camera session case |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4435 | if (op != AppOpsManager::OP_CAMERA) { |
| 4436 | ALOGW("Unexpected app ops notification received: %d", op); |
| 4437 | return; |
| 4438 | } |
| 4439 | |
| 4440 | int32_t res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4441 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4442 | mClientUid, toString16(mClientPackageName)); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4443 | ALOGV("checkOp returns: %d, %s ", res, |
| 4444 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 4445 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 4446 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 4447 | "UNKNOWN"); |
| 4448 | |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4449 | if (res == AppOpsManager::MODE_ERRORED) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4450 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.c_str(), |
| 4451 | mClientPackageName.c_str()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4452 | block(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4453 | } else if (res == AppOpsManager::MODE_IGNORED) { |
| 4454 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4455 | |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4456 | // Uid may be active, but not visible to the user (e.g. PROCESS_STATE_FOREGROUND_SERVICE). |
| 4457 | // If not visible, but still active, then we want to block instead of muting the camera. |
| 4458 | int32_t procState = sCameraService->mUidPolicy->getProcState(mClientUid); |
| 4459 | bool isUidVisible = (procState <= ActivityManager::PROCESS_STATE_BOUND_TOP); |
| 4460 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4461 | bool isCameraPrivacyEnabled; |
| 4462 | if (flags::camera_privacy_allowlist()) { |
| 4463 | isCameraPrivacyEnabled = sCameraService->isCameraPrivacyEnabled( |
| 4464 | toString16(mClientPackageName),std::string(),mClientPid,mClientUid); |
| 4465 | } else { |
| 4466 | isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4467 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
| 4470 | ALOGI("Camera %s: Access for \"%s\" has been restricted, isUidTrusted %d, isUidActive %d" |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4471 | " isUidVisible %d, isCameraPrivacyEnabled %d", mCameraIdStr.c_str(), |
| 4472 | mClientPackageName.c_str(), mUidIsTrusted, isUidActive, isUidVisible, |
| 4473 | isCameraPrivacyEnabled); |
| 4474 | // If the calling Uid is trusted (a native service), or the client Uid is active / visible |
| 4475 | // (WAR for b/175320666)the AppOpsManager could return MODE_IGNORED. Do not treat such |
| 4476 | // cases as error. |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4477 | if (!mUidIsTrusted) { |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4478 | if (flags::watch_foreground_changes()) { |
| 4479 | if (isUidVisible && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 4480 | setCameraMute(true); |
| 4481 | } else { |
| 4482 | block(); |
| 4483 | } |
| 4484 | } else { |
| 4485 | if (isUidActive && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 4486 | setCameraMute(true); |
| 4487 | } else if (!isUidActive |
| 4488 | || (isCameraPrivacyEnabled && !supportsCameraMute())) { |
| 4489 | block(); |
| 4490 | } |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4491 | } |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4492 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4493 | } else if (res == AppOpsManager::MODE_ALLOWED) { |
| 4494 | setCameraMute(sCameraService->mOverrideCameraMuteMode); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4495 | } |
| 4496 | } |
| 4497 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4498 | void CameraService::BasicClient::block() { |
| 4499 | ATRACE_CALL(); |
| 4500 | |
| 4501 | // Reset the client PID to allow server-initiated disconnect, |
| 4502 | // and to prevent further calls by client. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 4503 | mClientPid = CameraThreadState::getCallingPid(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4504 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 4505 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); |
| 4506 | disconnect(); |
| 4507 | } |
| 4508 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4509 | // ---------------------------------------------------------------------------- |
| 4510 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4511 | void CameraService::Client::notifyError(int32_t errorCode, |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 4512 | [[maybe_unused]] const CaptureResultExtras& resultExtras) { |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 4513 | if (mRemoteCallback != NULL) { |
Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 4514 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; |
| 4515 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { |
| 4516 | api1ErrorCode = CAMERA_ERROR_DISABLED; |
| 4517 | } |
| 4518 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 4519 | } else { |
| 4520 | ALOGE("mRemoteCallback is NULL!!"); |
| 4521 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4522 | } |
| 4523 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 4524 | // NOTE: function is idempotent |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4525 | binder::Status CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4526 | ALOGV("Client::disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4527 | return BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 4528 | } |
| 4529 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 4530 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 4531 | return level == API_1; |
| 4532 | } |
| 4533 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4534 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 4535 | mClient(client) { |
| 4536 | } |
| 4537 | |
| 4538 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 4539 | const String16& packageName) { |
| 4540 | sp<BasicClient> client = mClient.promote(); |
| 4541 | if (client != NULL) { |
| 4542 | client->opChanged(op, packageName); |
| 4543 | } |
| 4544 | } |
| 4545 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4546 | // ---------------------------------------------------------------------------- |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4547 | // UidPolicy |
| 4548 | // ---------------------------------------------------------------------------- |
| 4549 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4550 | void CameraService::UidPolicy::registerWithActivityManager() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4551 | Mutex::Autolock _l(mUidLock); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4552 | int32_t emptyUidArray[] = { }; |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4553 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4554 | if (mRegistered) return; |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 4555 | status_t res = mAm.linkToDeath(this); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4556 | mAm.registerUidObserverForUids(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4557 | | ActivityManager::UID_OBSERVER_IDLE |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4558 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE |
| 4559 | | ActivityManager::UID_OBSERVER_PROC_OOM_ADJ, |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4560 | ActivityManager::PROCESS_STATE_UNKNOWN, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4561 | toString16(kServiceName), emptyUidArray, 0, mObserverToken); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4562 | if (res == OK) { |
| 4563 | mRegistered = true; |
| 4564 | ALOGV("UidPolicy: Registered with ActivityManager"); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4565 | } else { |
| 4566 | ALOGE("UidPolicy: Failed to register with ActivityManager: 0x%08x", res); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4567 | } |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4568 | } |
| 4569 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4570 | void CameraService::UidPolicy::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4571 | if (name != toString16(kActivityServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4572 | return; |
| 4573 | } |
| 4574 | |
| 4575 | registerWithActivityManager(); |
| 4576 | } |
| 4577 | |
| 4578 | void CameraService::UidPolicy::registerSelf() { |
| 4579 | // Use check service to see if the activity service is available |
| 4580 | // If not available then register for notifications, instead of blocking |
| 4581 | // till the service is ready |
| 4582 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4583 | sp<IBinder> binder = sm->checkService(toString16(kActivityServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4584 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4585 | sm->registerForNotifications(toString16(kActivityServiceName), this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4586 | } else { |
| 4587 | registerWithActivityManager(); |
| 4588 | } |
| 4589 | } |
| 4590 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4591 | void CameraService::UidPolicy::unregisterSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4592 | Mutex::Autolock _l(mUidLock); |
| 4593 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 4594 | mAm.unregisterUidObserver(this); |
| 4595 | mAm.unlinkToDeath(this); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4596 | mRegistered = false; |
| 4597 | mActiveUids.clear(); |
| 4598 | ALOGV("UidPolicy: Unregistered with ActivityManager"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { |
| 4602 | onUidIdle(uid, disabled); |
| 4603 | } |
| 4604 | |
| 4605 | void CameraService::UidPolicy::onUidActive(uid_t uid) { |
| 4606 | Mutex::Autolock _l(mUidLock); |
| 4607 | mActiveUids.insert(uid); |
| 4608 | } |
| 4609 | |
| 4610 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { |
| 4611 | bool deleted = false; |
| 4612 | { |
| 4613 | Mutex::Autolock _l(mUidLock); |
| 4614 | if (mActiveUids.erase(uid) > 0) { |
| 4615 | deleted = true; |
| 4616 | } |
| 4617 | } |
| 4618 | if (deleted) { |
| 4619 | sp<CameraService> service = mService.promote(); |
| 4620 | if (service != nullptr) { |
| 4621 | service->blockClientsForUid(uid); |
| 4622 | } |
| 4623 | } |
| 4624 | } |
| 4625 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4626 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 4627 | int64_t procStateSeq __unused, int32_t capability __unused) { |
Austin Borger | c5585dc | 2022-05-12 00:48:17 +0000 | [diff] [blame] | 4628 | bool procStateChange = false; |
| 4629 | { |
| 4630 | Mutex::Autolock _l(mUidLock); |
| 4631 | if (mMonitoredUids.find(uid) != mMonitoredUids.end() && |
| 4632 | mMonitoredUids[uid].procState != procState) { |
| 4633 | mMonitoredUids[uid].procState = procState; |
| 4634 | procStateChange = true; |
| 4635 | } |
| 4636 | } |
| 4637 | |
| 4638 | if (procStateChange) { |
| 4639 | sp<CameraService> service = mService.promote(); |
| 4640 | if (service != nullptr) { |
| 4641 | service->notifyMonitoredUids(); |
| 4642 | } |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4643 | } |
| 4644 | } |
| 4645 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4646 | /** |
| 4647 | * When the OOM adj of the uid owning the camera changes, a different uid waiting on camera |
| 4648 | * privileges may take precedence if the owner's new OOM adj is greater than the waiting package. |
| 4649 | * Here, we track which monitoredUid has the camera, and track its adj relative to other |
| 4650 | * monitoredUids. If it is revised above some other monitoredUid, signal |
| 4651 | * onCameraAccessPrioritiesChanged. This only needs to capture the case where there are two |
| 4652 | * foreground apps in split screen - state changes will capture all other cases. |
| 4653 | */ |
| 4654 | void CameraService::UidPolicy::onUidProcAdjChanged(uid_t uid, int32_t adj) { |
| 4655 | std::unordered_set<uid_t> notifyUidSet; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4656 | { |
| 4657 | Mutex::Autolock _l(mUidLock); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4658 | auto it = mMonitoredUids.find(uid); |
| 4659 | |
| 4660 | if (it != mMonitoredUids.end()) { |
| 4661 | if (it->second.hasCamera) { |
| 4662 | for (auto &monitoredUid : mMonitoredUids) { |
| 4663 | if (monitoredUid.first != uid && adj > monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4664 | ALOGV("%s: notify uid %d", __FUNCTION__, monitoredUid.first); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4665 | notifyUidSet.emplace(monitoredUid.first); |
| 4666 | } |
| 4667 | } |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4668 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4669 | notifyUidSet.emplace(uid); |
| 4670 | } else { |
| 4671 | for (auto &monitoredUid : mMonitoredUids) { |
| 4672 | if (monitoredUid.second.hasCamera && adj < monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4673 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4674 | notifyUidSet.emplace(uid); |
| 4675 | } |
| 4676 | } |
| 4677 | } |
| 4678 | it->second.procAdj = adj; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4679 | } |
| 4680 | } |
| 4681 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4682 | if (notifyUidSet.size() > 0) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4683 | sp<CameraService> service = mService.promote(); |
| 4684 | if (service != nullptr) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4685 | service->notifyMonitoredUids(notifyUidSet); |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4686 | } |
| 4687 | } |
| 4688 | } |
| 4689 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4690 | /** |
| 4691 | * Register a uid for monitoring, and note whether it owns a camera. |
| 4692 | */ |
| 4693 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid, bool openCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4694 | Mutex::Autolock _l(mUidLock); |
| 4695 | auto it = mMonitoredUids.find(uid); |
| 4696 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4697 | it->second.refCount++; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4698 | } else { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4699 | MonitoredUid monitoredUid; |
| 4700 | monitoredUid.procState = ActivityManager::PROCESS_STATE_NONEXISTENT; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4701 | monitoredUid.procAdj = resource_policy::UNKNOWN_ADJ; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4702 | monitoredUid.refCount = 1; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4703 | it = mMonitoredUids.emplace(std::pair<uid_t, MonitoredUid>(uid, monitoredUid)).first; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4704 | status_t res = mAm.addUidToObserver(mObserverToken, toString16(kServiceName), uid); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4705 | if (res != OK) { |
| 4706 | ALOGE("UidPolicy: Failed to add uid to observer: 0x%08x", res); |
| 4707 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | if (openCamera) { |
| 4711 | it->second.hasCamera = true; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4712 | } |
| 4713 | } |
| 4714 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4715 | /** |
| 4716 | * Unregister a uid for monitoring, and note whether it lost ownership of a camera. |
| 4717 | */ |
| 4718 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid, bool closeCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4719 | Mutex::Autolock _l(mUidLock); |
| 4720 | auto it = mMonitoredUids.find(uid); |
| 4721 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4722 | it->second.refCount--; |
| 4723 | if (it->second.refCount == 0) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4724 | mMonitoredUids.erase(it); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4725 | status_t res = mAm.removeUidFromObserver(mObserverToken, toString16(kServiceName), uid); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4726 | if (res != OK) { |
| 4727 | ALOGE("UidPolicy: Failed to remove uid from observer: 0x%08x", res); |
| 4728 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4729 | } else if (closeCamera) { |
| 4730 | it->second.hasCamera = false; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4731 | } |
| 4732 | } else { |
| 4733 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 4734 | } |
| 4735 | } |
| 4736 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4737 | bool CameraService::UidPolicy::isUidActive(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4738 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4739 | return isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4740 | } |
| 4741 | |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4742 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; |
| 4743 | static const int64_t kPollUidActiveTimeoutMillis = 50; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4744 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4745 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4746 | // Non-app UIDs are considered always active |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4747 | // If activity manager is unreachable, assume everything is active |
| 4748 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4749 | return true; |
| 4750 | } |
| 4751 | auto it = mOverrideUids.find(uid); |
| 4752 | if (it != mOverrideUids.end()) { |
| 4753 | return it->second; |
| 4754 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4755 | bool active = mActiveUids.find(uid) != mActiveUids.end(); |
| 4756 | if (!active) { |
| 4757 | // We want active UIDs to always access camera with their first attempt since |
| 4758 | // there is no guarantee the app is robustly written and would retry getting |
| 4759 | // the camera on failure. The inverse case is not a problem as we would take |
| 4760 | // camera away soon once we get the callback that the uid is no longer active. |
| 4761 | ActivityManager am; |
| 4762 | // Okay to access with a lock held as UID changes are dispatched without |
| 4763 | // a lock and we are a higher level component. |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4764 | int64_t startTimeMillis = 0; |
| 4765 | do { |
| 4766 | // TODO: Fix this b/109950150! |
| 4767 | // Okay this is a hack. There is a race between the UID turning active and |
| 4768 | // activity being resumed. The proper fix is very risky, so we temporary add |
| 4769 | // some polling which should happen pretty rarely anyway as the race is hard |
| 4770 | // to hit. |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4771 | active = mActiveUids.find(uid) != mActiveUids.end(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4772 | if (!active) active = am.isUidActive(uid, toString16(callingPackage)); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4773 | if (active) { |
| 4774 | break; |
| 4775 | } |
| 4776 | if (startTimeMillis <= 0) { |
| 4777 | startTimeMillis = uptimeMillis(); |
| 4778 | } |
| 4779 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4780 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4781 | if (remainingTimeMillis <= 0) { |
| 4782 | break; |
| 4783 | } |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4784 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); |
| 4785 | |
| 4786 | mUidLock.unlock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4787 | usleep(remainingTimeMillis * 1000); |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4788 | mUidLock.lock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4789 | } while (true); |
| 4790 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4791 | if (active) { |
| 4792 | // Now that we found out the UID is actually active, cache that |
| 4793 | mActiveUids.insert(uid); |
| 4794 | } |
| 4795 | } |
| 4796 | return active; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4797 | } |
| 4798 | |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4799 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { |
| 4800 | Mutex::Autolock _l(mUidLock); |
| 4801 | return getProcStateLocked(uid); |
| 4802 | } |
| 4803 | |
| 4804 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { |
| 4805 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 4806 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4807 | procState = mMonitoredUids[uid].procState; |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4808 | } |
| 4809 | return procState; |
| 4810 | } |
| 4811 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4812 | void CameraService::UidPolicy::addOverrideUid(uid_t uid, |
| 4813 | const std::string &callingPackage, bool active) { |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4814 | updateOverrideUid(uid, callingPackage, active, true); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4815 | } |
| 4816 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4817 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4818 | updateOverrideUid(uid, callingPackage, false, false); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4819 | } |
| 4820 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4821 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 4822 | Mutex::Autolock _l(mUidLock); |
| 4823 | ALOGV("UidPolicy: ActivityManager has died"); |
| 4824 | mRegistered = false; |
| 4825 | mActiveUids.clear(); |
| 4826 | } |
| 4827 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4828 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, const std::string &callingPackage, |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4829 | bool active, bool insert) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4830 | bool wasActive = false; |
| 4831 | bool isActive = false; |
| 4832 | { |
| 4833 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4834 | wasActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4835 | mOverrideUids.erase(uid); |
| 4836 | if (insert) { |
| 4837 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 4838 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4839 | isActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4840 | } |
| 4841 | if (wasActive != isActive && !isActive) { |
| 4842 | sp<CameraService> service = mService.promote(); |
| 4843 | if (service != nullptr) { |
| 4844 | service->blockClientsForUid(uid); |
| 4845 | } |
| 4846 | } |
| 4847 | } |
| 4848 | |
| 4849 | // ---------------------------------------------------------------------------- |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4850 | // SensorPrivacyPolicy |
| 4851 | // ---------------------------------------------------------------------------- |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4852 | |
| 4853 | void CameraService::SensorPrivacyPolicy::registerWithSensorPrivacyManager() |
| 4854 | { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4855 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4856 | if (mRegistered) { |
| 4857 | return; |
| 4858 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4859 | hasCameraPrivacyFeature(); // Called so the result is cached |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4860 | mSpm.addSensorPrivacyListener(this); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4861 | if (isAutomotiveDevice()) { |
| 4862 | mSpm.addToggleSensorPrivacyListener(this); |
| 4863 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4864 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4865 | if (flags::camera_privacy_allowlist()) { |
| 4866 | mCameraPrivacyState = mSpm.getToggleSensorPrivacyState( |
| 4867 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, |
| 4868 | SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 4869 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4870 | status_t res = mSpm.linkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4871 | if (res == OK) { |
| 4872 | mRegistered = true; |
| 4873 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); |
| 4874 | } |
| 4875 | } |
| 4876 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4877 | void CameraService::SensorPrivacyPolicy::onServiceRegistration(const String16& name, |
| 4878 | const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4879 | if (name != toString16(kSensorPrivacyServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4880 | return; |
| 4881 | } |
| 4882 | |
| 4883 | registerWithSensorPrivacyManager(); |
| 4884 | } |
| 4885 | |
| 4886 | void CameraService::SensorPrivacyPolicy::registerSelf() { |
| 4887 | // Use checkservice to see if the sensor_privacy service is available |
| 4888 | // If service is not available then register for notification |
| 4889 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4890 | sp<IBinder> binder = sm->checkService(toString16(kSensorPrivacyServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4891 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4892 | sm->registerForNotifications(toString16(kSensorPrivacyServiceName),this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4893 | } else { |
| 4894 | registerWithSensorPrivacyManager(); |
| 4895 | } |
| 4896 | } |
| 4897 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4898 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { |
| 4899 | Mutex::Autolock _l(mSensorPrivacyLock); |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4900 | mSpm.removeSensorPrivacyListener(this); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4901 | if (isAutomotiveDevice()) { |
| 4902 | mSpm.removeToggleSensorPrivacyListener(this); |
| 4903 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4904 | mSpm.unlinkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4905 | mRegistered = false; |
| 4906 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); |
| 4907 | } |
| 4908 | |
| 4909 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4910 | if (!mRegistered) { |
| 4911 | registerWithSensorPrivacyManager(); |
| 4912 | } |
| 4913 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4914 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4915 | return mSensorPrivacyEnabled; |
| 4916 | } |
| 4917 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4918 | int CameraService::SensorPrivacyPolicy::getCameraPrivacyState() { |
| 4919 | if (!mRegistered) { |
| 4920 | registerWithSensorPrivacyManager(); |
| 4921 | } |
| 4922 | |
| 4923 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4924 | return mCameraPrivacyState; |
| 4925 | } |
| 4926 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4927 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled() { |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4928 | if (!hasCameraPrivacyFeature()) { |
| 4929 | return false; |
| 4930 | } |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4931 | return mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4932 | } |
| 4933 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4934 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled(const String16& packageName) { |
| 4935 | if (!hasCameraPrivacyFeature()) { |
| 4936 | return SensorPrivacyManager::DISABLED; |
| 4937 | } |
| 4938 | return mSpm.isCameraPrivacyEnabled(packageName); |
| 4939 | } |
| 4940 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4941 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged( |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4942 | int toggleType, int sensor, bool enabled) { |
| 4943 | if ((toggleType == SensorPrivacyManager::TOGGLE_TYPE_UNKNOWN) |
| 4944 | && (sensor == SensorPrivacyManager::TOGGLE_SENSOR_UNKNOWN)) { |
| 4945 | { |
| 4946 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4947 | mSensorPrivacyEnabled = enabled; |
| 4948 | } |
| 4949 | // if sensor privacy is enabled then block all clients from accessing the camera |
| 4950 | if (enabled) { |
| 4951 | sp<CameraService> service = mService.promote(); |
| 4952 | if (service != nullptr) { |
| 4953 | service->blockAllClients(); |
| 4954 | } |
| 4955 | } |
| 4956 | } |
| 4957 | return binder::Status::ok(); |
| 4958 | } |
| 4959 | |
| 4960 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyStateChanged( |
| 4961 | int, int sensor, int state) { |
| 4962 | if (!flags::camera_privacy_allowlist() |
| 4963 | || (sensor != SensorPrivacyManager::TOGGLE_SENSOR_CAMERA)) { |
| 4964 | return binder::Status::ok(); |
| 4965 | } |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4966 | { |
| 4967 | Mutex::Autolock _l(mSensorPrivacyLock); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4968 | mCameraPrivacyState = state; |
| 4969 | } |
| 4970 | sp<CameraService> service = mService.promote(); |
| 4971 | if (!service) { |
| 4972 | return binder::Status::ok(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4973 | } |
| 4974 | // if sensor privacy is enabled then block all clients from accessing the camera |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4975 | if (state == SensorPrivacyManager::ENABLED) { |
| 4976 | service->blockAllClients(); |
Jyoti Bhayana | 54a4b00 | 2024-02-27 15:36:09 -0800 | [diff] [blame^] | 4977 | } else if (state == SensorPrivacyManager::ENABLED_EXCEPT_ALLOWLISTED_APPS) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4978 | service->blockPrivacyEnabledClients(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4979 | } |
| 4980 | return binder::Status::ok(); |
| 4981 | } |
| 4982 | |
| 4983 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 4984 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4985 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); |
| 4986 | mRegistered = false; |
| 4987 | } |
| 4988 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4989 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4990 | bool supportsSoftwareToggle = mSpm.supportsSensorToggle( |
| 4991 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 4992 | bool supportsHardwareToggle = mSpm.supportsSensorToggle( |
| 4993 | SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 4994 | return supportsSoftwareToggle || supportsHardwareToggle; |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4995 | } |
| 4996 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4997 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4998 | // CameraState |
| 4999 | // ---------------------------------------------------------------------------- |
| 5000 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5001 | CameraService::CameraState::CameraState(const std::string& id, int cost, |
| 5002 | const std::set<std::string>& conflicting, SystemCameraKind systemCameraKind, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5003 | const std::vector<std::string>& physicalCameras) : mId(id), |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5004 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5005 | mSystemCameraKind(systemCameraKind), mPhysicalCameras(physicalCameras) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5006 | |
| 5007 | CameraService::CameraState::~CameraState() {} |
| 5008 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5009 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5010 | Mutex::Autolock lock(mStatusLock); |
| 5011 | return mStatus; |
| 5012 | } |
| 5013 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5014 | std::vector<std::string> CameraService::CameraState::getUnavailablePhysicalIds() const { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5015 | Mutex::Autolock lock(mStatusLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5016 | std::vector<std::string> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5017 | return res; |
| 5018 | } |
| 5019 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5020 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 5021 | return mShimParams; |
| 5022 | } |
| 5023 | |
| 5024 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 5025 | mShimParams = params; |
| 5026 | } |
| 5027 | |
| 5028 | int CameraService::CameraState::getCost() const { |
| 5029 | return mCost; |
| 5030 | } |
| 5031 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5032 | std::set<std::string> CameraService::CameraState::getConflicting() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5033 | return mConflicting; |
| 5034 | } |
| 5035 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5036 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { |
| 5037 | return mSystemCameraKind; |
| 5038 | } |
| 5039 | |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5040 | bool CameraService::CameraState::containsPhysicalCamera(const std::string& physicalCameraId) const { |
| 5041 | return std::find(mPhysicalCameras.begin(), mPhysicalCameras.end(), physicalCameraId) |
| 5042 | != mPhysicalCameras.end(); |
| 5043 | } |
| 5044 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5045 | bool CameraService::CameraState::addUnavailablePhysicalId(const std::string& physicalId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5046 | Mutex::Autolock lock(mStatusLock); |
| 5047 | auto result = mUnavailablePhysicalIds.insert(physicalId); |
| 5048 | return result.second; |
| 5049 | } |
| 5050 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5051 | bool CameraService::CameraState::removeUnavailablePhysicalId(const std::string& physicalId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5052 | Mutex::Autolock lock(mStatusLock); |
| 5053 | auto count = mUnavailablePhysicalIds.erase(physicalId); |
| 5054 | return count > 0; |
| 5055 | } |
| 5056 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5057 | void CameraService::CameraState::setClientPackage(const std::string& clientPackage) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5058 | Mutex::Autolock lock(mStatusLock); |
| 5059 | mClientPackage = clientPackage; |
| 5060 | } |
| 5061 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5062 | std::string CameraService::CameraState::getClientPackage() const { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5063 | Mutex::Autolock lock(mStatusLock); |
| 5064 | return mClientPackage; |
| 5065 | } |
| 5066 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5067 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5068 | // ClientEventListener |
| 5069 | // ---------------------------------------------------------------------------- |
| 5070 | |
| 5071 | void CameraService::ClientEventListener::onClientAdded( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5072 | const resource_policy::ClientDescriptor<std::string, |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5073 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 5074 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5075 | if (basicClient.get() != nullptr) { |
| 5076 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5077 | notifier.noteStartCamera(toString8(descriptor.getKey()), |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5078 | static_cast<int>(basicClient->getClientUid())); |
| 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | void CameraService::ClientEventListener::onClientRemoved( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5083 | const resource_policy::ClientDescriptor<std::string, |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5084 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 5085 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5086 | if (basicClient.get() != nullptr) { |
| 5087 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5088 | notifier.noteStopCamera(toString8(descriptor.getKey()), |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5089 | static_cast<int>(basicClient->getClientUid())); |
| 5090 | } |
| 5091 | } |
| 5092 | |
| 5093 | |
| 5094 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5095 | // CameraClientManager |
| 5096 | // ---------------------------------------------------------------------------- |
| 5097 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5098 | CameraService::CameraClientManager::CameraClientManager() { |
| 5099 | setListener(std::make_shared<ClientEventListener>()); |
| 5100 | } |
| 5101 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5102 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 5103 | |
| 5104 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5105 | const std::string& id) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5106 | auto descriptor = get(id); |
| 5107 | if (descriptor == nullptr) { |
| 5108 | return sp<BasicClient>{nullptr}; |
| 5109 | } |
| 5110 | return descriptor->getValue(); |
| 5111 | } |
| 5112 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5113 | std::string CameraService::CameraClientManager::toString() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5114 | auto all = getAll(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5115 | std::ostringstream ret; |
| 5116 | ret << "["; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5117 | bool hasAny = false; |
| 5118 | for (auto& i : all) { |
| 5119 | hasAny = true; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5120 | std::string key = i->getKey(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5121 | int32_t cost = i->getCost(); |
| 5122 | int32_t pid = i->getOwnerId(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 5123 | int32_t score = i->getPriority().getScore(); |
| 5124 | int32_t state = i->getPriority().getState(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5125 | auto conflicting = i->getConflicting(); |
| 5126 | auto clientSp = i->getValue(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5127 | std::string packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 5128 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5129 | if (clientSp.get() != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5130 | packageName = clientSp->getPackageName(); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5131 | uid_t clientUid = clientSp->getClientUid(); |
| 5132 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5133 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5134 | ret << fmt::sprintf("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" |
| 5135 | PRId32 ", State: %" PRId32, key.c_str(), cost, pid, score, state); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5136 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5137 | if (clientSp.get() != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5138 | ret << fmt::sprintf("User Id: %d, ", clientUserId); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5139 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5140 | if (packageName.size() != 0) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5141 | ret << fmt::sprintf("Client Package Name: %s", packageName.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5142 | } |
| 5143 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5144 | ret << ", Conflicting Client Devices: {"; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5145 | for (auto& j : conflicting) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5146 | ret << fmt::sprintf("%s, ", j.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5147 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5148 | ret << "})"; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5149 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5150 | if (hasAny) ret << "\n"; |
| 5151 | ret << "]\n"; |
| 5152 | return std::move(ret.str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5153 | } |
| 5154 | |
| 5155 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5156 | const std::string& key, const sp<BasicClient>& value, int32_t cost, |
| 5157 | const std::set<std::string>& conflictingKeys, int32_t score, int32_t ownerId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5158 | int32_t state, int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5159 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5160 | int32_t score_adj = systemNativeClient ? kSystemNativeClientScore : score; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5161 | int32_t state_adj = systemNativeClient ? kSystemNativeClientState : state; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 5162 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5163 | return std::make_shared<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>>( |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5164 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, |
| 5165 | systemNativeClient, oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 5169 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5170 | int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5171 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 5172 | partial->getConflicting(), partial->getPriority().getScore(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5173 | partial->getOwnerId(), partial->getPriority().getState(), oomScoreOffset, |
| 5174 | systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5175 | } |
| 5176 | |
| 5177 | // ---------------------------------------------------------------------------- |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5178 | // InjectionStatusListener |
| 5179 | // ---------------------------------------------------------------------------- |
| 5180 | |
| 5181 | void CameraService::InjectionStatusListener::addListener( |
| 5182 | const sp<ICameraInjectionCallback>& callback) { |
| 5183 | Mutex::Autolock lock(mListenerLock); |
| 5184 | if (mCameraInjectionCallback) return; |
| 5185 | status_t res = IInterface::asBinder(callback)->linkToDeath(this); |
| 5186 | if (res == OK) { |
| 5187 | mCameraInjectionCallback = callback; |
| 5188 | } |
| 5189 | } |
| 5190 | |
| 5191 | void CameraService::InjectionStatusListener::removeListener() { |
| 5192 | Mutex::Autolock lock(mListenerLock); |
| 5193 | if (mCameraInjectionCallback == nullptr) { |
| 5194 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 5195 | return; |
| 5196 | } |
| 5197 | IInterface::asBinder(mCameraInjectionCallback)->unlinkToDeath(this); |
| 5198 | mCameraInjectionCallback = nullptr; |
| 5199 | } |
| 5200 | |
| 5201 | void CameraService::InjectionStatusListener::notifyInjectionError( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5202 | const std::string &injectedCamId, status_t err) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5203 | if (mCameraInjectionCallback == nullptr) { |
| 5204 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 5205 | return; |
| 5206 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5207 | |
| 5208 | switch (err) { |
| 5209 | case -ENODEV: |
| 5210 | mCameraInjectionCallback->onInjectionError( |
| 5211 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5212 | ALOGE("No camera device with ID \"%s\" currently available!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5213 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5214 | break; |
| 5215 | case -EBUSY: |
| 5216 | mCameraInjectionCallback->onInjectionError( |
| 5217 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5218 | ALOGE("Higher-priority client using camera, ID \"%s\" currently unavailable!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5219 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5220 | break; |
| 5221 | case DEAD_OBJECT: |
| 5222 | mCameraInjectionCallback->onInjectionError( |
| 5223 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5224 | ALOGE("Camera ID \"%s\" object is dead!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5225 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5226 | break; |
| 5227 | case INVALID_OPERATION: |
| 5228 | mCameraInjectionCallback->onInjectionError( |
| 5229 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5230 | ALOGE("Camera ID \"%s\" encountered an operating or internal error!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5231 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5232 | break; |
| 5233 | case UNKNOWN_TRANSACTION: |
| 5234 | mCameraInjectionCallback->onInjectionError( |
| 5235 | ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED); |
| 5236 | ALOGE("Camera ID \"%s\" method doesn't support!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5237 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5238 | break; |
| 5239 | default: |
| 5240 | mCameraInjectionCallback->onInjectionError( |
| 5241 | ICameraInjectionCallback::ERROR_INJECTION_INVALID_ERROR); |
| 5242 | ALOGE("Unexpected error %s (%d) opening camera \"%s\"!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5243 | strerror(-err), err, injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5244 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | void CameraService::InjectionStatusListener::binderDied( |
| 5248 | const wp<IBinder>& /*who*/) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5249 | ALOGV("InjectionStatusListener: ICameraInjectionCallback has died"); |
| 5250 | auto parent = mParent.promote(); |
| 5251 | if (parent != nullptr) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5252 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 5253 | if (clientDescriptor != nullptr) { |
| 5254 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 5255 | baseClientPtr->stopInjection(); |
| 5256 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 5257 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | // ---------------------------------------------------------------------------- |
| 5262 | // CameraInjectionSession |
| 5263 | // ---------------------------------------------------------------------------- |
| 5264 | |
| 5265 | binder::Status CameraService::CameraInjectionSession::stopInjection() { |
| 5266 | Mutex::Autolock lock(mInjectionSessionLock); |
| 5267 | auto parent = mParent.promote(); |
| 5268 | if (parent == nullptr) { |
| 5269 | ALOGE("CameraInjectionSession: Parent is gone"); |
| 5270 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SERVICE, |
| 5271 | "Camera service encountered error"); |
| 5272 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5273 | |
| 5274 | status_t res = NO_ERROR; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5275 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 5276 | if (clientDescriptor != nullptr) { |
| 5277 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 5278 | res = baseClientPtr->stopInjection(); |
| 5279 | if (res != OK) { |
| 5280 | ALOGE("CameraInjectionSession: Failed to stop the injection camera!" |
| 5281 | " ret != NO_ERROR: %d", res); |
| 5282 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SESSION, |
| 5283 | "Camera session encountered error"); |
| 5284 | } |
| 5285 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 5286 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5287 | return binder::Status::ok(); |
| 5288 | } |
| 5289 | |
| 5290 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5291 | |
| 5292 | static const int kDumpLockRetries = 50; |
| 5293 | static const int kDumpLockSleep = 60000; |
| 5294 | |
| 5295 | static bool tryLock(Mutex& mutex) |
| 5296 | { |
| 5297 | bool locked = false; |
| 5298 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 5299 | if (mutex.tryLock() == NO_ERROR) { |
| 5300 | locked = true; |
| 5301 | break; |
| 5302 | } |
| 5303 | usleep(kDumpLockSleep); |
| 5304 | } |
| 5305 | return locked; |
| 5306 | } |
| 5307 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5308 | void CameraService::cacheDump() { |
| 5309 | if (mMemFd != -1) { |
| 5310 | const Vector<String16> args; |
| 5311 | ATRACE_CALL(); |
| 5312 | // Acquiring service lock here will avoid the deadlock since |
| 5313 | // cacheDump will not be called during the second disconnect. |
| 5314 | Mutex::Autolock lock(mServiceLock); |
| 5315 | |
| 5316 | Mutex::Autolock l(mCameraStatesLock); |
| 5317 | // Start collecting the info for open sessions and store it in temp file. |
| 5318 | for (const auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5319 | std::string cameraId = state.first; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5320 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 5321 | if (clientDescriptor != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5322 | dprintf(mMemFd, "== Camera device %s dynamic info: ==\n", cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5323 | // Log the current open session info before device is disconnected. |
| 5324 | dumpOpenSessionClientLogs(mMemFd, args, cameraId); |
| 5325 | } |
| 5326 | } |
| 5327 | } |
| 5328 | } |
| 5329 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5330 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 5331 | ATRACE_CALL(); |
| 5332 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5333 | if (checkCallingPermission(toString16(sDumpPermission)) == false) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5334 | 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] | 5335 | CameraThreadState::getCallingPid(), |
| 5336 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5337 | return NO_ERROR; |
| 5338 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5339 | bool locked = tryLock(mServiceLock); |
| 5340 | // failed to lock - CameraService is probably deadlocked |
| 5341 | if (!locked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5342 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5343 | } |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5344 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5345 | if (!mInitialized) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5346 | dprintf(fd, "!! No camera HAL available !!\n"); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 5347 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5348 | // Dump event log for error information |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5349 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5350 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5351 | if (locked) mServiceLock.unlock(); |
| 5352 | return NO_ERROR; |
| 5353 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5354 | dprintf(fd, "\n== Service global info: ==\n\n"); |
| 5355 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 5356 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 5357 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", |
| 5358 | mNormalDeviceIdsWithoutSystemCamera.size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 5359 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { |
| 5360 | dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); |
| 5361 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5362 | std::string activeClientString = mActiveClientManager.toString(); |
| 5363 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.c_str()); |
| 5364 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).c_str()); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5365 | if (mStreamUseCaseOverrides.size() > 0) { |
| 5366 | dprintf(fd, "Active stream use case overrides:"); |
| 5367 | for (int64_t useCaseOverride : mStreamUseCaseOverrides) { |
| 5368 | dprintf(fd, " %" PRId64, useCaseOverride); |
| 5369 | } |
| 5370 | dprintf(fd, "\n"); |
| 5371 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5372 | |
| 5373 | dumpEventLog(fd); |
| 5374 | |
| 5375 | bool stateLocked = tryLock(mCameraStatesLock); |
| 5376 | if (!stateLocked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5377 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5378 | } |
| 5379 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5380 | int argSize = args.size(); |
| 5381 | for (int i = 0; i < argSize; i++) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5382 | if (args[i] == toString16(TagMonitor::kMonitorOption)) { |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5383 | if (i + 1 < argSize) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5384 | mMonitorTags = toStdString(args[i + 1]); |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5385 | } |
| 5386 | break; |
| 5387 | } |
| 5388 | } |
| 5389 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5390 | for (auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5391 | const std::string &cameraId = state.first; |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5392 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5393 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.c_str()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5394 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5395 | CameraParameters p = state.second->getShimParams(); |
| 5396 | if (!p.isEmpty()) { |
| 5397 | dprintf(fd, " Camera1 API shim is using parameters:\n "); |
| 5398 | p.dump(fd, args); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5399 | } |
| 5400 | |
| 5401 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 5402 | if (clientDescriptor != nullptr) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5403 | // log the current open session info |
| 5404 | dumpOpenSessionClientLogs(fd, args, cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 5405 | } else { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5406 | dumpClosedSessionClientLogs(fd, cameraId); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5407 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5408 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5409 | } |
| 5410 | |
| 5411 | if (stateLocked) mCameraStatesLock.unlock(); |
| 5412 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5413 | if (locked) mServiceLock.unlock(); |
| 5414 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 5415 | mCameraProviderManager->dump(fd, args); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5416 | |
| 5417 | dprintf(fd, "\n== Vendor tags: ==\n\n"); |
| 5418 | |
| 5419 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 5420 | if (desc == NULL) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 5421 | sp<VendorTagDescriptorCache> cache = |
| 5422 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 5423 | if (cache == NULL) { |
| 5424 | dprintf(fd, "No vendor tags.\n"); |
| 5425 | } else { |
| 5426 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 5427 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5428 | } else { |
| 5429 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 5430 | } |
| 5431 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5432 | // Dump camera traces if there were any |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5433 | dprintf(fd, "\n"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5434 | camera3::CameraTraces::dump(fd); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5435 | |
| 5436 | // Process dump arguments, if any |
| 5437 | int n = args.size(); |
| 5438 | String16 verboseOption("-v"); |
| 5439 | String16 unreachableOption("--unreachable"); |
| 5440 | for (int i = 0; i < n; i++) { |
| 5441 | if (args[i] == verboseOption) { |
| 5442 | // change logging level |
| 5443 | if (i + 1 >= n) continue; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5444 | std::string levelStr = toStdString(args[i+1]); |
| 5445 | int level = atoi(levelStr.c_str()); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5446 | dprintf(fd, "\nSetting log level to %d.\n", level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5447 | setLogLevel(level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5448 | } else if (args[i] == unreachableOption) { |
| 5449 | // Dump memory analysis |
| 5450 | // TODO - should limit be an argument parameter? |
| 5451 | UnreachableMemoryInfo info; |
| 5452 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); |
| 5453 | if (!success) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5454 | dprintf(fd, "\n== Unable to dump unreachable memory. " |
| 5455 | "Try disabling SELinux enforcement. ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5456 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5457 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5458 | std::string s = info.ToString(/*log_contents*/ true); |
| 5459 | write(fd, s.c_str(), s.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5460 | } |
| 5461 | } |
| 5462 | } |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5463 | |
| 5464 | bool serviceLocked = tryLock(mServiceLock); |
| 5465 | |
| 5466 | // Dump info from previous open sessions. |
| 5467 | // Reposition the offset to beginning of the file before reading |
| 5468 | |
| 5469 | if ((mMemFd >= 0) && (lseek(mMemFd, 0, SEEK_SET) != -1)) { |
| 5470 | dprintf(fd, "\n**********Dumpsys from previous open session**********\n"); |
| 5471 | ssize_t size_read; |
| 5472 | char buf[4096]; |
| 5473 | while ((size_read = read(mMemFd, buf, (sizeof(buf) - 1))) > 0) { |
| 5474 | // Read data from file to a small buffer and write it to fd. |
| 5475 | write(fd, buf, size_read); |
| 5476 | if (size_read == -1) { |
| 5477 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 5478 | break; |
| 5479 | } |
| 5480 | } |
| 5481 | dprintf(fd, "\n**********End of Dumpsys from previous open session**********\n"); |
| 5482 | } else { |
| 5483 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 5484 | } |
| 5485 | |
| 5486 | if (serviceLocked) mServiceLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5487 | return NO_ERROR; |
| 5488 | } |
| 5489 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5490 | void CameraService::dumpOpenSessionClientLogs(int fd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5491 | const Vector<String16>& args, const std::string& cameraId) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5492 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Rucha Katakwar | 71a0e05 | 2022-04-07 15:44:18 -0700 | [diff] [blame] | 5493 | dprintf(fd, " %s : Device %s is open. Client instance dump:\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5494 | getFormattedCurrentTime().c_str(), |
| 5495 | cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5496 | dprintf(fd, " Client priority score: %d state: %d\n", |
| 5497 | clientDescriptor->getPriority().getScore(), |
| 5498 | clientDescriptor->getPriority().getState()); |
| 5499 | dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 5500 | |
| 5501 | auto client = clientDescriptor->getValue(); |
| 5502 | dprintf(fd, " Client package: %s\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5503 | client->getPackageName().c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5504 | |
| 5505 | client->dumpClient(fd, args); |
| 5506 | } |
| 5507 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5508 | void CameraService::dumpClosedSessionClientLogs(int fd, const std::string& cameraId) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5509 | dprintf(fd, " Device %s is closed, no client instance\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5510 | cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5511 | } |
| 5512 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5513 | void CameraService::dumpEventLog(int fd) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5514 | 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] | 5515 | |
| 5516 | Mutex::Autolock l(mLogLock); |
| 5517 | for (const auto& msg : mEventLog) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5518 | dprintf(fd, " %s\n", msg.c_str()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5519 | } |
| 5520 | |
| 5521 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5522 | dprintf(fd, " ...\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5523 | } else if (mEventLog.size() == 0) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5524 | dprintf(fd, " [no events yet]\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5525 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5526 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5527 | } |
| 5528 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5529 | void CameraService::cacheClientTagDumpIfNeeded(const std::string &cameraId, BasicClient* client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5530 | Mutex::Autolock lock(mLogLock); |
| 5531 | if (!isClientWatchedLocked(client)) { return; } |
| 5532 | |
| 5533 | std::vector<std::string> dumpVector; |
| 5534 | client->dumpWatchedEventsToVector(dumpVector); |
| 5535 | |
| 5536 | if (dumpVector.empty()) { return; } |
| 5537 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5538 | std::ostringstream dumpString; |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 5539 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5540 | std::string currentTime = getFormattedCurrentTime(); |
| 5541 | dumpString << "Cached @ "; |
| 5542 | dumpString << currentTime; |
| 5543 | dumpString << "\n"; // First line is the timestamp of when client is cached. |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 5544 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5545 | size_t i = dumpVector.size(); |
| 5546 | |
| 5547 | // Store the string in reverse order (latest last) |
| 5548 | while (i > 0) { |
| 5549 | i--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5550 | dumpString << cameraId; |
| 5551 | dumpString << ":"; |
| 5552 | dumpString << client->getPackageName(); |
| 5553 | dumpString << " "; |
| 5554 | dumpString << dumpVector[i]; // implicitly ends with '\n' |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5555 | } |
| 5556 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5557 | mWatchedClientsDumpCache[client->getPackageName()] = dumpString.str(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5558 | } |
| 5559 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5560 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5561 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5562 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 5563 | if (mTorchClientMap[i] == who) { |
| 5564 | // turn off the torch mode that was turned on by dead client |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5565 | std::string cameraId = mTorchClientMap.keyAt(i); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5566 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 5567 | if (res) { |
| 5568 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 5569 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 5570 | return; |
| 5571 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5572 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5573 | break; |
| 5574 | } |
| 5575 | } |
| 5576 | } |
| 5577 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5578 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 5579 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 5580 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 5581 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 5582 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 5583 | */ |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 5584 | // PID here is approximate and can be wrong. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5585 | logClientDied(CameraThreadState::getCallingPid(), "Binder died unexpectedly"); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 5586 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5587 | // check torch client |
| 5588 | handleTorchClientBinderDied(who); |
| 5589 | |
| 5590 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5591 | if(!evictClientIdByRemote(who)) { |
| 5592 | 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] | 5593 | return; |
| 5594 | } |
| 5595 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5596 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 5597 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 5598 | } |
| 5599 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5600 | void CameraService::updateStatus(StatusInternal status, const std::string& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5601 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 5602 | } |
| 5603 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5604 | void CameraService::updateStatus(StatusInternal status, const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5605 | std::initializer_list<StatusInternal> rejectSourceStates) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5606 | // Do not lock mServiceLock here or can get into a deadlock from |
| 5607 | // connect() -> disconnect -> updateStatus |
| 5608 | |
| 5609 | auto state = getCameraState(cameraId); |
| 5610 | |
| 5611 | if (state == nullptr) { |
| 5612 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5613 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5614 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 5615 | } |
| 5616 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5617 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 5618 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 5619 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5620 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5621 | return; |
| 5622 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5623 | |
| 5624 | // Collect the logical cameras without holding mStatusLock in updateStatus |
| 5625 | // as that can lead to a deadlock(b/162192331). |
| 5626 | auto logicalCameraIds = getLogicalCameras(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5627 | // 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] | 5628 | // of the listeners with both the mStatusLock and mStatusListenerLock held |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 5629 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5630 | &logicalCameraIds] |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5631 | (const std::string& cameraId, StatusInternal status) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5632 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5633 | if (status != StatusInternal::ENUMERATING) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 5634 | // Update torch status if it has a flash unit. |
| 5635 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5636 | TorchModeStatus torchStatus; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 5637 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 5638 | NAME_NOT_FOUND) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5639 | TorchModeStatus newTorchStatus = |
| 5640 | status == StatusInternal::PRESENT ? |
| 5641 | TorchModeStatus::AVAILABLE_OFF : |
| 5642 | TorchModeStatus::NOT_AVAILABLE; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 5643 | if (torchStatus != newTorchStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 5644 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 5645 | } |
| 5646 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | Mutex::Autolock lock(mStatusListenerLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5650 | notifyPhysicalCameraStatusLocked(mapToInterface(status), cameraId, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5651 | logicalCameraIds, deviceKind); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5652 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5653 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 5654 | bool isVendorListener = listener->isVendorListener(); |
| 5655 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, |
Kwangkyu Park | 01f8443 | 2023-09-15 17:08:17 +0900 | [diff] [blame] | 5656 | listener->getListenerPid(), listener->getListenerUid())) { |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 5657 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 5658 | cameraId.c_str()); |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 5659 | continue; |
| 5660 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5661 | auto ret = listener->getListener()->onStatusChanged(mapToInterface(status), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5662 | cameraId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5663 | listener->handleBinderStatus(ret, |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5664 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5665 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 5666 | ret.exceptionCode()); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5667 | // Also trigger the callbacks for cameras that were remapped to the current |
| 5668 | // cameraId for the specific package that this listener belongs to. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5669 | std::vector<std::string> remappedCameraIds = |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5670 | findOriginalIdsForRemappedCameraId(cameraId, listener->getListenerUid()); |
| 5671 | for (auto& remappedCameraId : remappedCameraIds) { |
| 5672 | ret = listener->getListener()->onStatusChanged( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5673 | mapToInterface(status), remappedCameraId); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5674 | listener->handleBinderStatus(ret, |
| 5675 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", |
| 5676 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 5677 | ret.exceptionCode()); |
| 5678 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5679 | } |
| 5680 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 5681 | } |
| 5682 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5683 | void CameraService::updateOpenCloseStatus(const std::string& cameraId, bool open, |
| 5684 | const std::string& clientPackageName) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5685 | auto state = getCameraState(cameraId); |
| 5686 | if (state == nullptr) { |
| 5687 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5688 | cameraId.c_str()); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5689 | return; |
| 5690 | } |
| 5691 | if (open) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5692 | state->setClientPackage(clientPackageName); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5693 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5694 | state->setClientPackage(std::string()); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5695 | } |
| 5696 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5697 | Mutex::Autolock lock(mStatusListenerLock); |
| 5698 | |
| 5699 | for (const auto& it : mListenerList) { |
| 5700 | if (!it->isOpenCloseCallbackAllowed()) { |
| 5701 | continue; |
| 5702 | } |
| 5703 | |
| 5704 | binder::Status ret; |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5705 | if (open) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5706 | ret = it->getListener()->onCameraOpened(cameraId, clientPackageName); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5707 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5708 | ret = it->getListener()->onCameraClosed(cameraId); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5709 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5710 | |
| 5711 | it->handleBinderStatus(ret, |
| 5712 | "%s: Failed to trigger onCameraOpened/onCameraClosed callback for %d:%d: %d", |
| 5713 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5714 | } |
| 5715 | } |
| 5716 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5717 | template<class Func> |
| 5718 | void CameraService::CameraState::updateStatus(StatusInternal status, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5719 | const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5720 | std::initializer_list<StatusInternal> rejectSourceStates, |
| 5721 | Func onStatusUpdatedLocked) { |
| 5722 | Mutex::Autolock lock(mStatusLock); |
| 5723 | StatusInternal oldStatus = mStatus; |
| 5724 | mStatus = status; |
| 5725 | |
| 5726 | if (oldStatus == status) { |
| 5727 | return; |
| 5728 | } |
| 5729 | |
| 5730 | ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5731 | cameraId.c_str(), oldStatus, status); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5732 | |
| 5733 | if (oldStatus == StatusInternal::NOT_PRESENT && |
| 5734 | (status != StatusInternal::PRESENT && |
| 5735 | status != StatusInternal::ENUMERATING)) { |
| 5736 | |
| 5737 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 5738 | __FUNCTION__); |
| 5739 | mStatus = oldStatus; |
| 5740 | return; |
| 5741 | } |
| 5742 | |
| 5743 | /** |
| 5744 | * Sometimes we want to conditionally do a transition. |
| 5745 | * For example if a client disconnects, we want to go to PRESENT |
| 5746 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 5747 | */ |
| 5748 | for (auto& rejectStatus : rejectSourceStates) { |
| 5749 | if (oldStatus == rejectStatus) { |
| 5750 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5751 | "state was was in one of the bad states.", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5752 | mStatus = oldStatus; |
| 5753 | return; |
| 5754 | } |
| 5755 | } |
| 5756 | |
| 5757 | onStatusUpdatedLocked(cameraId, status); |
| 5758 | } |
| 5759 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5760 | status_t CameraService::getTorchStatusLocked( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5761 | const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5762 | TorchModeStatus *status) const { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5763 | if (!status) { |
| 5764 | return BAD_VALUE; |
| 5765 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5766 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 5767 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5768 | // invalid camera ID or the camera doesn't have a flash unit |
| 5769 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5770 | } |
| 5771 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5772 | *status = mTorchStatusMap.valueAt(index); |
| 5773 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5774 | } |
| 5775 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5776 | status_t CameraService::setTorchStatusLocked(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5777 | TorchModeStatus status) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5778 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 5779 | if (index == NAME_NOT_FOUND) { |
| 5780 | return BAD_VALUE; |
| 5781 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5782 | mTorchStatusMap.editValueAt(index) = status; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5783 | |
| 5784 | return OK; |
| 5785 | } |
| 5786 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5787 | std::list<std::string> CameraService::getLogicalCameras( |
| 5788 | const std::string& physicalCameraId) { |
| 5789 | std::list<std::string> retList; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5790 | Mutex::Autolock lock(mCameraStatesLock); |
| 5791 | for (const auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5792 | if (state.second->containsPhysicalCamera(physicalCameraId)) { |
| 5793 | retList.emplace_back(state.first); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5794 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5795 | } |
| 5796 | return retList; |
| 5797 | } |
| 5798 | |
| 5799 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5800 | const std::string& physicalCameraId, const std::list<std::string>& logicalCameraIds, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5801 | SystemCameraKind deviceKind) { |
| 5802 | // mStatusListenerLock is expected to be locked |
| 5803 | for (const auto& logicalCameraId : logicalCameraIds) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5804 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5805 | // Note: we check only the deviceKind of the physical camera id |
| 5806 | // since, logical camera ids and their physical camera ids are |
| 5807 | // guaranteed to have the same system camera kind. |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 5808 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 5809 | listener->getListenerPid(), listener->getListenerUid())) { |
| 5810 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5811 | physicalCameraId.c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 5812 | continue; |
| 5813 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5814 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged(status, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5815 | logicalCameraId, physicalCameraId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5816 | listener->handleBinderStatus(ret, |
| 5817 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 5818 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 5819 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5820 | } |
| 5821 | } |
| 5822 | } |
| 5823 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5824 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5825 | void CameraService::blockClientsForUid(uid_t uid) { |
| 5826 | const auto clients = mActiveClientManager.getAll(); |
| 5827 | for (auto& current : clients) { |
| 5828 | if (current != nullptr) { |
| 5829 | const auto basicClient = current->getValue(); |
| 5830 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { |
| 5831 | basicClient->block(); |
| 5832 | } |
| 5833 | } |
| 5834 | } |
| 5835 | } |
| 5836 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5837 | void CameraService::blockAllClients() { |
| 5838 | const auto clients = mActiveClientManager.getAll(); |
| 5839 | for (auto& current : clients) { |
| 5840 | if (current != nullptr) { |
| 5841 | const auto basicClient = current->getValue(); |
| 5842 | if (basicClient.get() != nullptr) { |
| 5843 | basicClient->block(); |
| 5844 | } |
| 5845 | } |
| 5846 | } |
| 5847 | } |
| 5848 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5849 | void CameraService::blockPrivacyEnabledClients() { |
| 5850 | const auto clients = mActiveClientManager.getAll(); |
| 5851 | for (auto& current : clients) { |
| 5852 | if (current != nullptr) { |
| 5853 | const auto basicClient = current->getValue(); |
| 5854 | if (basicClient.get() != nullptr) { |
| 5855 | std::string pkgName = basicClient->getPackageName(); |
| 5856 | bool cameraPrivacyEnabled = |
| 5857 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(toString16(pkgName)); |
| 5858 | if (cameraPrivacyEnabled) { |
| 5859 | basicClient->block(); |
| 5860 | } |
| 5861 | } |
| 5862 | } |
| 5863 | } |
| 5864 | } |
| 5865 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5866 | // NOTE: This is a remote API - make sure all args are validated |
| 5867 | status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5868 | if (!checkCallingPermission(toString16(sManageCameraPermission), nullptr, nullptr)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5869 | return PERMISSION_DENIED; |
| 5870 | } |
| 5871 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 5872 | return BAD_VALUE; |
| 5873 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5874 | if (args.size() >= 3 && args[0] == toString16("set-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5875 | return handleSetUidState(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5876 | } else if (args.size() >= 2 && args[0] == toString16("reset-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5877 | return handleResetUidState(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5878 | } else if (args.size() >= 2 && args[0] == toString16("get-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5879 | return handleGetUidState(args, out, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5880 | } else if (args.size() >= 2 && args[0] == toString16("set-rotate-and-crop")) { |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5881 | return handleSetRotateAndCrop(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5882 | } else if (args.size() >= 1 && args[0] == toString16("get-rotate-and-crop")) { |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5883 | return handleGetRotateAndCrop(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5884 | } else if (args.size() >= 2 && args[0] == toString16("set-autoframing")) { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5885 | return handleSetAutoframing(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5886 | } else if (args.size() >= 1 && args[0] == toString16("get-autoframing")) { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5887 | return handleGetAutoframing(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5888 | } else if (args.size() >= 2 && args[0] == toString16("set-image-dump-mask")) { |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5889 | return handleSetImageDumpMask(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5890 | } else if (args.size() >= 1 && args[0] == toString16("get-image-dump-mask")) { |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5891 | return handleGetImageDumpMask(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5892 | } else if (args.size() >= 2 && args[0] == toString16("set-camera-mute")) { |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 5893 | return handleSetCameraMute(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5894 | } else if (args.size() >= 2 && args[0] == toString16("set-stream-use-case-override")) { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5895 | return handleSetStreamUseCaseOverrides(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5896 | } else if (args.size() >= 1 && args[0] == toString16("clear-stream-use-case-override")) { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5897 | handleClearStreamUseCaseOverrides(); |
| 5898 | return OK; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5899 | } else if (args.size() >= 1 && args[0] == toString16("set-zoom-override")) { |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 5900 | return handleSetZoomOverride(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5901 | } else if (args.size() >= 2 && args[0] == toString16("watch")) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5902 | return handleWatchCommand(args, in, out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5903 | } else if (args.size() >= 2 && args[0] == toString16("set-watchdog")) { |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 5904 | return handleSetCameraServiceWatchdog(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5905 | } else if (args.size() >= 4 && args[0] == toString16("remap-camera-id")) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5906 | return handleCameraIdRemapping(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5907 | } else if (args.size() == 1 && args[0] == toString16("help")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5908 | printHelp(out); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5909 | return OK; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5910 | } |
| 5911 | printHelp(err); |
| 5912 | return BAD_VALUE; |
| 5913 | } |
| 5914 | |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5915 | status_t CameraService::handleCameraIdRemapping(const Vector<String16>& args, int err) { |
| 5916 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 5917 | if (uid != AID_ROOT) { |
| 5918 | dprintf(err, "Must be adb root\n"); |
| 5919 | return PERMISSION_DENIED; |
| 5920 | } |
| 5921 | if (args.size() != 4) { |
| 5922 | dprintf(err, "Expected format: remap-camera-id <PACKAGE> <Id0> <Id1>\n"); |
| 5923 | return BAD_VALUE; |
| 5924 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5925 | std::string packageName = toStdString(args[1]); |
| 5926 | std::string cameraIdToReplace = toStdString(args[2]); |
| 5927 | std::string cameraIdNew = toStdString(args[3]); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5928 | remapCameraIds({{packageName, {{cameraIdToReplace, cameraIdNew}}}}); |
| 5929 | return OK; |
| 5930 | } |
| 5931 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5932 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5933 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5934 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5935 | bool active = false; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5936 | if (args[2] == toString16("active")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5937 | active = true; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5938 | } else if ((args[2] != toString16("idle"))) { |
| 5939 | ALOGE("Expected active or idle but got: '%s'", toStdString(args[2]).c_str()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5940 | return BAD_VALUE; |
| 5941 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5942 | |
| 5943 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5944 | if (args.size() >= 5 && args[3] == toString16("--user")) { |
| 5945 | userId = atoi(toStdString(args[4]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5946 | } |
| 5947 | |
| 5948 | uid_t uid; |
| 5949 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
| 5950 | return BAD_VALUE; |
| 5951 | } |
| 5952 | |
| 5953 | mUidPolicy->addOverrideUid(uid, packageName, active); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5954 | return NO_ERROR; |
| 5955 | } |
| 5956 | |
| 5957 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5958 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5959 | |
| 5960 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5961 | if (args.size() >= 4 && args[2] == toString16("--user")) { |
| 5962 | userId = atoi(toStdString(args[3]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5963 | } |
| 5964 | |
| 5965 | uid_t uid; |
| 5966 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5967 | return BAD_VALUE; |
| 5968 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5969 | |
| 5970 | mUidPolicy->removeOverrideUid(uid, packageName); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5971 | return NO_ERROR; |
| 5972 | } |
| 5973 | |
| 5974 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5975 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5976 | |
| 5977 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5978 | if (args.size() >= 4 && args[2] == toString16("--user")) { |
| 5979 | userId = atoi(toStdString(args[3]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5980 | } |
| 5981 | |
| 5982 | uid_t uid; |
| 5983 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5984 | return BAD_VALUE; |
| 5985 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5986 | |
| 5987 | if (mUidPolicy->isUidActive(uid, packageName)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5988 | return dprintf(out, "active\n"); |
| 5989 | } else { |
| 5990 | return dprintf(out, "idle\n"); |
| 5991 | } |
| 5992 | } |
| 5993 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5994 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5995 | int rotateValue = atoi(toStdString(args[1]).c_str()); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5996 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || |
| 5997 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; |
| 5998 | Mutex::Autolock lock(mServiceLock); |
| 5999 | |
| 6000 | mOverrideRotateAndCropMode = rotateValue; |
| 6001 | |
| 6002 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; |
| 6003 | |
| 6004 | const auto clients = mActiveClientManager.getAll(); |
| 6005 | for (auto& current : clients) { |
| 6006 | if (current != nullptr) { |
| 6007 | const auto basicClient = current->getValue(); |
| 6008 | if (basicClient.get() != nullptr) { |
| 6009 | basicClient->setRotateAndCropOverride(rotateValue); |
| 6010 | } |
| 6011 | } |
| 6012 | } |
| 6013 | |
| 6014 | return OK; |
| 6015 | } |
| 6016 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6017 | status_t CameraService::handleSetAutoframing(const Vector<String16>& args) { |
| 6018 | char* end; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6019 | int autoframingValue = (int) strtol(toStdString(args[1]).c_str(), &end, /*base=*/10); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6020 | if ((*end != '\0') || |
| 6021 | (autoframingValue != ANDROID_CONTROL_AUTOFRAMING_OFF && |
| 6022 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_ON && |
| 6023 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_AUTO)) { |
| 6024 | return BAD_VALUE; |
| 6025 | } |
| 6026 | |
| 6027 | Mutex::Autolock lock(mServiceLock); |
| 6028 | mOverrideAutoframingMode = autoframingValue; |
| 6029 | |
| 6030 | if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) return OK; |
| 6031 | |
| 6032 | const auto clients = mActiveClientManager.getAll(); |
| 6033 | for (auto& current : clients) { |
| 6034 | if (current != nullptr) { |
| 6035 | const auto basicClient = current->getValue(); |
| 6036 | if (basicClient.get() != nullptr) { |
| 6037 | basicClient->setAutoframingOverride(autoframingValue); |
| 6038 | } |
| 6039 | } |
| 6040 | } |
| 6041 | |
| 6042 | return OK; |
| 6043 | } |
| 6044 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6045 | status_t CameraService::handleSetCameraServiceWatchdog(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6046 | int enableWatchdog = atoi(toStdString(args[1]).c_str()); |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6047 | |
| 6048 | if (enableWatchdog < 0 || enableWatchdog > 1) return BAD_VALUE; |
| 6049 | |
| 6050 | Mutex::Autolock lock(mServiceLock); |
| 6051 | |
| 6052 | mCameraServiceWatchdogEnabled = enableWatchdog; |
| 6053 | |
| 6054 | const auto clients = mActiveClientManager.getAll(); |
| 6055 | for (auto& current : clients) { |
| 6056 | if (current != nullptr) { |
| 6057 | const auto basicClient = current->getValue(); |
| 6058 | if (basicClient.get() != nullptr) { |
| 6059 | basicClient->setCameraServiceWatchdog(enableWatchdog); |
| 6060 | } |
| 6061 | } |
| 6062 | } |
| 6063 | |
| 6064 | return OK; |
| 6065 | } |
| 6066 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6067 | status_t CameraService::handleGetRotateAndCrop(int out) { |
| 6068 | Mutex::Autolock lock(mServiceLock); |
| 6069 | |
| 6070 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); |
| 6071 | } |
| 6072 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6073 | status_t CameraService::handleGetAutoframing(int out) { |
| 6074 | Mutex::Autolock lock(mServiceLock); |
| 6075 | |
| 6076 | return dprintf(out, "autoframing override: %d\n", mOverrideAutoframingMode); |
| 6077 | } |
| 6078 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6079 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { |
| 6080 | char *endPtr; |
| 6081 | errno = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6082 | std::string maskString = toStdString(args[1]); |
| 6083 | long maskValue = strtol(maskString.c_str(), &endPtr, 10); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6084 | |
| 6085 | if (errno != 0) return BAD_VALUE; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6086 | if (endPtr != maskString.c_str() + maskString.size()) return BAD_VALUE; |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6087 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; |
| 6088 | |
| 6089 | Mutex::Autolock lock(mServiceLock); |
| 6090 | |
| 6091 | mImageDumpMask = maskValue; |
| 6092 | |
| 6093 | return OK; |
| 6094 | } |
| 6095 | |
| 6096 | status_t CameraService::handleGetImageDumpMask(int out) { |
| 6097 | Mutex::Autolock lock(mServiceLock); |
| 6098 | |
| 6099 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); |
| 6100 | } |
| 6101 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6102 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6103 | int muteValue = strtol(toStdString(args[1]).c_str(), nullptr, 10); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6104 | if (errno != 0) return BAD_VALUE; |
| 6105 | |
| 6106 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; |
| 6107 | Mutex::Autolock lock(mServiceLock); |
| 6108 | |
| 6109 | mOverrideCameraMuteMode = (muteValue == 1); |
| 6110 | |
| 6111 | const auto clients = mActiveClientManager.getAll(); |
| 6112 | for (auto& current : clients) { |
| 6113 | if (current != nullptr) { |
| 6114 | const auto basicClient = current->getValue(); |
| 6115 | if (basicClient.get() != nullptr) { |
| 6116 | if (basicClient->supportsCameraMute()) { |
| 6117 | basicClient->setCameraMute(mOverrideCameraMuteMode); |
| 6118 | } |
| 6119 | } |
| 6120 | } |
| 6121 | } |
| 6122 | |
| 6123 | return OK; |
| 6124 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6125 | |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6126 | status_t CameraService::handleSetStreamUseCaseOverrides(const Vector<String16>& args) { |
| 6127 | std::vector<int64_t> useCasesOverride; |
| 6128 | for (size_t i = 1; i < args.size(); i++) { |
| 6129 | int64_t useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6130 | std::string arg = toStdString(args[i]); |
| 6131 | if (arg == "DEFAULT") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6132 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6133 | } else if (arg == "PREVIEW") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6134 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6135 | } else if (arg == "STILL_CAPTURE") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6136 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_STILL_CAPTURE; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6137 | } else if (arg == "VIDEO_RECORD") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6138 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_RECORD; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6139 | } else if (arg == "PREVIEW_VIDEO_STILL") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6140 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW_VIDEO_STILL; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6141 | } else if (arg == "VIDEO_CALL") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6142 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6143 | } else if (arg == "CROPPED_RAW") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6144 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_CROPPED_RAW; |
| 6145 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6146 | ALOGE("%s: Invalid stream use case %s", __FUNCTION__, arg.c_str()); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6147 | return BAD_VALUE; |
| 6148 | } |
| 6149 | useCasesOverride.push_back(useCase); |
| 6150 | } |
| 6151 | |
| 6152 | Mutex::Autolock lock(mServiceLock); |
| 6153 | mStreamUseCaseOverrides = std::move(useCasesOverride); |
| 6154 | |
| 6155 | return OK; |
| 6156 | } |
| 6157 | |
| 6158 | void CameraService::handleClearStreamUseCaseOverrides() { |
| 6159 | Mutex::Autolock lock(mServiceLock); |
| 6160 | mStreamUseCaseOverrides.clear(); |
| 6161 | } |
| 6162 | |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6163 | status_t CameraService::handleSetZoomOverride(const Vector<String16>& args) { |
| 6164 | char* end; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6165 | int zoomOverrideValue = strtol(toStdString(args[1]).c_str(), &end, /*base=*/10); |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6166 | if ((*end != '\0') || |
| 6167 | (zoomOverrideValue != -1 && |
| 6168 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF && |
| 6169 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM)) { |
| 6170 | return BAD_VALUE; |
| 6171 | } |
| 6172 | |
| 6173 | Mutex::Autolock lock(mServiceLock); |
| 6174 | mZoomOverrideValue = zoomOverrideValue; |
| 6175 | |
| 6176 | const auto clients = mActiveClientManager.getAll(); |
| 6177 | for (auto& current : clients) { |
| 6178 | if (current != nullptr) { |
| 6179 | const auto basicClient = current->getValue(); |
| 6180 | if (basicClient.get() != nullptr) { |
| 6181 | if (basicClient->supportsZoomOverride()) { |
| 6182 | basicClient->setZoomOverride(mZoomOverrideValue); |
| 6183 | } |
| 6184 | } |
| 6185 | } |
| 6186 | } |
| 6187 | |
| 6188 | return OK; |
| 6189 | } |
| 6190 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6191 | status_t CameraService::handleWatchCommand(const Vector<String16>& args, int inFd, int outFd) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6192 | if (args.size() >= 3 && args[1] == toString16("start")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6193 | return startWatchingTags(args, outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6194 | } else if (args.size() == 2 && args[1] == toString16("stop")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6195 | return stopWatchingTags(outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6196 | } else if (args.size() == 2 && args[1] == toString16("dump")) { |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6197 | return printWatchedTags(outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6198 | } else if (args.size() >= 2 && args[1] == toString16("live")) { |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6199 | return printWatchedTagsUntilInterrupt(args, inFd, outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6200 | } else if (args.size() == 2 && args[1] == toString16("clear")) { |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6201 | return clearCachedMonitoredTagDumps(outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6202 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6203 | dprintf(outFd, "Camera service watch commands:\n" |
| 6204 | " start -m <comma_separated_tag_list> [-c <comma_separated_client_list>]\n" |
| 6205 | " starts watching the provided tags for clients with provided package\n" |
| 6206 | " recognizes tag shorthands like '3a'\n" |
| 6207 | " watches all clients if no client is passed, or if 'all' is listed\n" |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6208 | " dump dumps the monitoring information and exits\n" |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6209 | " stop stops watching all tags\n" |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6210 | " live [-n <refresh_interval_ms>]\n" |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6211 | " prints the monitored information in real time\n" |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6212 | " Hit return to exit\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6213 | " clear clears all buffers storing information for watch command"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6214 | return BAD_VALUE; |
| 6215 | } |
| 6216 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6217 | status_t CameraService::startWatchingTags(const Vector<String16> &args, int outFd) { |
| 6218 | Mutex::Autolock lock(mLogLock); |
| 6219 | size_t tagsIdx; // index of '-m' |
| 6220 | String16 tags(""); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6221 | for (tagsIdx = 2; tagsIdx < args.size() && args[tagsIdx] != toString16("-m"); tagsIdx++); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6222 | if (tagsIdx < args.size() - 1) { |
| 6223 | tags = args[tagsIdx + 1]; |
| 6224 | } else { |
| 6225 | dprintf(outFd, "No tags provided.\n"); |
| 6226 | return BAD_VALUE; |
| 6227 | } |
| 6228 | |
| 6229 | size_t clientsIdx; // index of '-c' |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6230 | // watch all clients if no clients are provided |
| 6231 | String16 clients = toString16(kWatchAllClientsFlag); |
| 6232 | for (clientsIdx = 2; clientsIdx < args.size() && args[clientsIdx] != toString16("-c"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6233 | clientsIdx++); |
| 6234 | if (clientsIdx < args.size() - 1) { |
| 6235 | clients = args[clientsIdx + 1]; |
| 6236 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6237 | parseClientsToWatchLocked(toStdString(clients)); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6238 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6239 | // track tags to initialize future clients with the monitoring information |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6240 | mMonitorTags = toStdString(tags); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6241 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6242 | bool serviceLock = tryLock(mServiceLock); |
| 6243 | int numWatchedClients = 0; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6244 | auto cameraClients = mActiveClientManager.getAll(); |
| 6245 | for (const auto &clientDescriptor: cameraClients) { |
| 6246 | if (clientDescriptor == nullptr) { continue; } |
| 6247 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6248 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6249 | |
| 6250 | if (isClientWatchedLocked(client.get())) { |
| 6251 | client->startWatchingTags(mMonitorTags, outFd); |
| 6252 | numWatchedClients++; |
| 6253 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6254 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6255 | dprintf(outFd, "Started watching %d active clients\n", numWatchedClients); |
| 6256 | |
| 6257 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6258 | return OK; |
| 6259 | } |
| 6260 | |
| 6261 | status_t CameraService::stopWatchingTags(int outFd) { |
| 6262 | // clear mMonitorTags to prevent new clients from monitoring tags at initialization |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6263 | Mutex::Autolock lock(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6264 | mMonitorTags = ""; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6265 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6266 | mWatchedClientPackages.clear(); |
| 6267 | mWatchedClientsDumpCache.clear(); |
| 6268 | |
| 6269 | bool serviceLock = tryLock(mServiceLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6270 | auto cameraClients = mActiveClientManager.getAll(); |
| 6271 | for (const auto &clientDescriptor : cameraClients) { |
| 6272 | if (clientDescriptor == nullptr) { continue; } |
| 6273 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6274 | if (client.get() == nullptr) { continue; } |
| 6275 | client->stopWatchingTags(outFd); |
| 6276 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6277 | dprintf(outFd, "Stopped watching all clients.\n"); |
| 6278 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6279 | return OK; |
| 6280 | } |
| 6281 | |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6282 | status_t CameraService::clearCachedMonitoredTagDumps(int outFd) { |
| 6283 | Mutex::Autolock lock(mLogLock); |
| 6284 | size_t clearedSize = mWatchedClientsDumpCache.size(); |
| 6285 | mWatchedClientsDumpCache.clear(); |
| 6286 | dprintf(outFd, "Cleared tag information of %zu cached clients.\n", clearedSize); |
| 6287 | return OK; |
| 6288 | } |
| 6289 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6290 | status_t CameraService::printWatchedTags(int outFd) { |
| 6291 | Mutex::Autolock logLock(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6292 | std::set<std::string> connectedMonitoredClients; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6293 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6294 | bool printedSomething = false; // tracks if any monitoring information was printed |
| 6295 | // (from either cached or active clients) |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6296 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6297 | bool serviceLock = tryLock(mServiceLock); |
| 6298 | // get all watched clients that are currently connected |
| 6299 | for (const auto &clientDescriptor: mActiveClientManager.getAll()) { |
| 6300 | if (clientDescriptor == nullptr) { continue; } |
| 6301 | |
| 6302 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6303 | if (client.get() == nullptr) { continue; } |
| 6304 | if (!isClientWatchedLocked(client.get())) { continue; } |
| 6305 | |
| 6306 | std::vector<std::string> dumpVector; |
| 6307 | client->dumpWatchedEventsToVector(dumpVector); |
| 6308 | |
| 6309 | size_t printIdx = dumpVector.size(); |
| 6310 | if (printIdx == 0) { |
| 6311 | continue; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6312 | } |
| 6313 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6314 | // Print tag dumps for active client |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6315 | const std::string &cameraId = clientDescriptor->getKey(); |
| 6316 | dprintf(outFd, "Client: %s (active)\n", client->getPackageName().c_str()); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6317 | while(printIdx > 0) { |
| 6318 | printIdx--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6319 | dprintf(outFd, "%s:%s %s", cameraId.c_str(), client->getPackageName().c_str(), |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6320 | dumpVector[printIdx].c_str()); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6321 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6322 | dprintf(outFd, "\n"); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6323 | printedSomething = true; |
| 6324 | |
| 6325 | connectedMonitoredClients.emplace(client->getPackageName()); |
| 6326 | } |
| 6327 | if (serviceLock) { mServiceLock.unlock(); } |
| 6328 | |
| 6329 | // Print entries in mWatchedClientsDumpCache for clients that are not connected |
| 6330 | for (const auto &kv: mWatchedClientsDumpCache) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6331 | const std::string &package = kv.first; |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6332 | if (connectedMonitoredClients.find(package) != connectedMonitoredClients.end()) { |
| 6333 | continue; |
| 6334 | } |
| 6335 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6336 | dprintf(outFd, "Client: %s (cached)\n", package.c_str()); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6337 | dprintf(outFd, "%s\n", kv.second.c_str()); |
| 6338 | printedSomething = true; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6339 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6340 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6341 | if (!printedSomething) { |
| 6342 | dprintf(outFd, "No monitoring information to print.\n"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6343 | } |
| 6344 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6345 | return OK; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6346 | } |
| 6347 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6348 | // Print all events in vector `events' that came after lastPrintedEvent |
| 6349 | void printNewWatchedEvents(int outFd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6350 | const std::string &cameraId, |
| 6351 | const std::string &packageName, |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6352 | const std::vector<std::string> &events, |
| 6353 | const std::string &lastPrintedEvent) { |
| 6354 | if (events.empty()) { return; } |
| 6355 | |
| 6356 | // index of lastPrintedEvent in events. |
| 6357 | // lastPrintedIdx = events.size() if lastPrintedEvent is not in events |
| 6358 | size_t lastPrintedIdx; |
| 6359 | for (lastPrintedIdx = 0; |
| 6360 | lastPrintedIdx < events.size() && lastPrintedEvent != events[lastPrintedIdx]; |
| 6361 | lastPrintedIdx++); |
| 6362 | |
| 6363 | if (lastPrintedIdx == 0) { return; } // early exit if no new event in `events` |
| 6364 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6365 | // print events in chronological order (latest event last) |
| 6366 | size_t idxToPrint = lastPrintedIdx; |
| 6367 | do { |
| 6368 | idxToPrint--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6369 | dprintf(outFd, "%s:%s %s", cameraId.c_str(), packageName.c_str(), |
| 6370 | events[idxToPrint].c_str()); |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6371 | } while (idxToPrint != 0); |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6372 | } |
| 6373 | |
| 6374 | // Returns true if adb shell cmd watch should be interrupted based on data in inFd. The watch |
| 6375 | // command should be interrupted if the user presses the return key, or if user loses any way to |
| 6376 | // signal interrupt. |
| 6377 | // If timeoutMs == 0, this function will always return false |
| 6378 | bool shouldInterruptWatchCommand(int inFd, int outFd, long timeoutMs) { |
| 6379 | struct timeval startTime; |
| 6380 | int startTimeError = gettimeofday(&startTime, nullptr); |
| 6381 | if (startTimeError) { |
| 6382 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 6383 | return true; |
| 6384 | } |
| 6385 | |
| 6386 | const nfds_t numFds = 1; |
| 6387 | struct pollfd pollFd = { .fd = inFd, .events = POLLIN, .revents = 0 }; |
| 6388 | |
| 6389 | struct timeval currTime; |
| 6390 | char buffer[2]; |
| 6391 | while(true) { |
| 6392 | int currTimeError = gettimeofday(&currTime, nullptr); |
| 6393 | if (currTimeError) { |
| 6394 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 6395 | return true; |
| 6396 | } |
| 6397 | |
| 6398 | long elapsedTimeMs = ((currTime.tv_sec - startTime.tv_sec) * 1000L) |
| 6399 | + ((currTime.tv_usec - startTime.tv_usec) / 1000L); |
| 6400 | int remainingTimeMs = (int) (timeoutMs - elapsedTimeMs); |
| 6401 | |
| 6402 | if (remainingTimeMs <= 0) { |
| 6403 | // No user interrupt within timeoutMs, don't interrupt watch command |
| 6404 | return false; |
| 6405 | } |
| 6406 | |
| 6407 | int numFdsUpdated = poll(&pollFd, numFds, remainingTimeMs); |
| 6408 | if (numFdsUpdated < 0) { |
| 6409 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 6410 | return true; |
| 6411 | } |
| 6412 | |
| 6413 | if (numFdsUpdated == 0) { |
| 6414 | // No user input within timeoutMs, don't interrupt watch command |
| 6415 | return false; |
| 6416 | } |
| 6417 | |
| 6418 | if (!(pollFd.revents & POLLIN)) { |
| 6419 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 6420 | return true; |
| 6421 | } |
| 6422 | |
| 6423 | ssize_t sizeRead = read(inFd, buffer, sizeof(buffer) - 1); |
| 6424 | if (sizeRead < 0) { |
| 6425 | dprintf(outFd, "Error reading user input. Exiting.\n"); |
| 6426 | return true; |
| 6427 | } |
| 6428 | |
| 6429 | if (sizeRead == 0) { |
| 6430 | // Reached end of input fd (can happen if input is piped) |
| 6431 | // User has no way to signal an interrupt, so interrupt here |
| 6432 | return true; |
| 6433 | } |
| 6434 | |
| 6435 | if (buffer[0] == '\n') { |
| 6436 | // User pressed return, interrupt watch command. |
| 6437 | return true; |
| 6438 | } |
| 6439 | } |
| 6440 | } |
| 6441 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6442 | status_t CameraService::printWatchedTagsUntilInterrupt(const Vector<String16> &args, |
| 6443 | int inFd, int outFd) { |
| 6444 | // Figure out refresh interval, if present in args |
| 6445 | long refreshTimeoutMs = 1000L; // refresh every 1s by default |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6446 | if (args.size() > 2) { |
| 6447 | size_t intervalIdx; // index of '-n' |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6448 | for (intervalIdx = 2; intervalIdx < args.size() && toString16("-n") != args[intervalIdx]; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6449 | intervalIdx++); |
| 6450 | |
| 6451 | size_t intervalValIdx = intervalIdx + 1; |
| 6452 | if (intervalValIdx < args.size()) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6453 | refreshTimeoutMs = strtol(toStdString(args[intervalValIdx]).c_str(), nullptr, 10); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6454 | if (errno) { return BAD_VALUE; } |
| 6455 | } |
| 6456 | } |
| 6457 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6458 | // Set min timeout of 10ms. This prevents edge cases in polling when timeout of 0 is passed. |
| 6459 | refreshTimeoutMs = refreshTimeoutMs < 10 ? 10 : refreshTimeoutMs; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6460 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6461 | dprintf(outFd, "Press return to exit...\n\n"); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6462 | std::map<std::string, std::string> packageNameToLastEvent; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6463 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6464 | while (true) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6465 | bool serviceLock = tryLock(mServiceLock); |
| 6466 | auto cameraClients = mActiveClientManager.getAll(); |
| 6467 | if (serviceLock) { mServiceLock.unlock(); } |
| 6468 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6469 | for (const auto& clientDescriptor : cameraClients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6470 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6471 | if (clientDescriptor == nullptr) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6472 | |
| 6473 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6474 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6475 | if (!isClientWatchedLocked(client.get())) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6476 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6477 | const std::string &packageName = client->getPackageName(); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6478 | // This also initializes the map entries with an empty string |
| 6479 | const std::string& lastPrintedEvent = packageNameToLastEvent[packageName]; |
| 6480 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6481 | std::vector<std::string> latestEvents; |
| 6482 | client->dumpWatchedEventsToVector(latestEvents); |
| 6483 | |
| 6484 | if (!latestEvents.empty()) { |
| 6485 | printNewWatchedEvents(outFd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6486 | clientDescriptor->getKey(), |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6487 | packageName, |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6488 | latestEvents, |
| 6489 | lastPrintedEvent); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6490 | packageNameToLastEvent[packageName] = latestEvents[0]; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6491 | } |
| 6492 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6493 | if (shouldInterruptWatchCommand(inFd, outFd, refreshTimeoutMs)) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6494 | break; |
| 6495 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6496 | } |
| 6497 | return OK; |
| 6498 | } |
| 6499 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6500 | void CameraService::parseClientsToWatchLocked(const std::string &clients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6501 | mWatchedClientPackages.clear(); |
| 6502 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6503 | std::istringstream iss(clients); |
| 6504 | std::string nextClient; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6505 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6506 | while (std::getline(iss, nextClient, ',')) { |
| 6507 | if (nextClient == kWatchAllClientsFlag) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6508 | // Don't need to track any other package if 'all' is present |
| 6509 | mWatchedClientPackages.clear(); |
| 6510 | mWatchedClientPackages.emplace(kWatchAllClientsFlag); |
| 6511 | break; |
| 6512 | } |
| 6513 | |
| 6514 | // track package names |
| 6515 | mWatchedClientPackages.emplace(nextClient); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6516 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6517 | } |
| 6518 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6519 | status_t CameraService::printHelp(int out) { |
| 6520 | return dprintf(out, "Camera service commands:\n" |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6521 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 6522 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 6523 | " 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] | 6524 | " set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" |
| 6525 | " Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" |
| 6526 | " get-rotate-and-crop returns the current override rotate-and-crop value\n" |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6527 | " set-autoframing <VALUE> overrides the autoframing value for AUTO\n" |
| 6528 | " Valid values 0=false, 1=true, 2=auto\n" |
| 6529 | " get-autoframing returns the current override autoframing value\n" |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6530 | " set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" |
| 6531 | " Valid values 0=OFF, 1=ON for JPEG\n" |
| 6532 | " 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] | 6533 | " set-camera-mute <0/1> enable or disable camera muting\n" |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6534 | " set-stream-use-case-override <usecase1> <usecase2> ... override stream use cases\n" |
| 6535 | " Use cases applied in descending resolutions. So usecase1 is assigned to the\n" |
| 6536 | " largest resolution, usecase2 is assigned to the 2nd largest resolution, and so\n" |
| 6537 | " on. In case the number of usecases is smaller than the number of streams, the\n" |
| 6538 | " last use case is assigned to all the remaining streams. In case of multiple\n" |
| 6539 | " streams with the same resolution, the tie-breaker is (JPEG, RAW, YUV, and PRIV)\n" |
| 6540 | " Valid values are (case sensitive): DEFAULT, PREVIEW, STILL_CAPTURE, VIDEO_RECORD,\n" |
| 6541 | " PREVIEW_VIDEO_STILL, VIDEO_CALL, CROPPED_RAW\n" |
| 6542 | " clear-stream-use-case-override clear the stream use case override\n" |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6543 | " set-zoom-override <-1/0/1> enable or disable zoom override\n" |
| 6544 | " Valid values -1: do not override, 0: override to OFF, 1: override to ZOOM\n" |
Ravneet Dhanjal | ad99ff5 | 2023-07-24 05:21:07 +0000 | [diff] [blame] | 6545 | " set-watchdog <VALUE> enables or disables the camera service watchdog\n" |
| 6546 | " Valid values 0=disable, 1=enable\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6547 | " watch <start|stop|dump|print|clear> manages tag monitoring in connected clients\n" |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 6548 | " remap-camera-id <PACKAGE> <Id0> <Id1> remaps camera ids. Must use adb root\n" |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6549 | " help print this message\n"); |
| 6550 | } |
| 6551 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6552 | bool CameraService::isClientWatched(const BasicClient *client) { |
| 6553 | Mutex::Autolock lock(mLogLock); |
| 6554 | return isClientWatchedLocked(client); |
| 6555 | } |
| 6556 | |
| 6557 | bool CameraService::isClientWatchedLocked(const BasicClient *client) { |
| 6558 | return mWatchedClientPackages.find(kWatchAllClientsFlag) != mWatchedClientPackages.end() || |
| 6559 | mWatchedClientPackages.find(client->getPackageName()) != mWatchedClientPackages.end(); |
| 6560 | } |
| 6561 | |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 6562 | int32_t CameraService::updateAudioRestriction() { |
| 6563 | Mutex::Autolock lock(mServiceLock); |
| 6564 | return updateAudioRestrictionLocked(); |
| 6565 | } |
| 6566 | |
| 6567 | int32_t CameraService::updateAudioRestrictionLocked() { |
| 6568 | int32_t mode = 0; |
| 6569 | // iterate through all active client |
| 6570 | for (const auto& i : mActiveClientManager.getAll()) { |
| 6571 | const auto clientSp = i->getValue(); |
| 6572 | mode |= clientSp->getAudioRestriction(); |
| 6573 | } |
| 6574 | |
| 6575 | bool modeChanged = (mAudioRestriction != mode); |
| 6576 | mAudioRestriction = mode; |
| 6577 | if (modeChanged) { |
| 6578 | mAppOps.setCameraAudioRestriction(mode); |
| 6579 | } |
| 6580 | return mode; |
| 6581 | } |
| 6582 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6583 | status_t CameraService::checkIfInjectionCameraIsPresent(const std::string& externalCamId, |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6584 | sp<BasicClient> clientSp) { |
| 6585 | std::unique_ptr<AutoConditionLock> lock = |
| 6586 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 6587 | status_t res = NO_ERROR; |
| 6588 | if ((res = checkIfDeviceIsUsable(externalCamId)) != NO_ERROR) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6589 | ALOGW("Device %s is not usable!", externalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6590 | mInjectionStatusListener->notifyInjectionError( |
| 6591 | externalCamId, UNKNOWN_TRANSACTION); |
| 6592 | clientSp->notifyError( |
| 6593 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 6594 | CaptureResultExtras()); |
| 6595 | |
| 6596 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 6597 | // other clients from connecting in mServiceLockWrapper if held |
| 6598 | mServiceLock.unlock(); |
| 6599 | |
| 6600 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
| 6601 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 6602 | clientSp->disconnect(); |
| 6603 | CameraThreadState::restoreCallingIdentity(token); |
| 6604 | |
| 6605 | // Reacquire mServiceLock |
| 6606 | mServiceLock.lock(); |
| 6607 | } |
| 6608 | |
| 6609 | return res; |
| 6610 | } |
| 6611 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 6612 | void CameraService::clearInjectionParameters() { |
| 6613 | { |
| 6614 | Mutex::Autolock lock(mInjectionParametersLock); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6615 | mInjectionInitPending = false; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 6616 | mInjectionInternalCamId = ""; |
| 6617 | } |
| 6618 | mInjectionExternalCamId = ""; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 6619 | mInjectionStatusListener->removeListener(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 6620 | } |
| 6621 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6622 | }; // namespace android |