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> |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 41 | #include <android_companion_virtualdevice_flags.h> |
| 42 | #include <android/companion/virtualnative/IVirtualDeviceManagerNative.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 43 | #include <binder/ActivityManager.h> |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 44 | #include <binder/AppOpsManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 45 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 46 | #include <binder/MemoryBase.h> |
| 47 | #include <binder/MemoryHeapBase.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 48 | #include <binder/PermissionController.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 49 | #include <binder/IResultReceiver.h> |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 50 | #include <binderthreadstate/CallerUtils.h> |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 51 | #include <com_android_internal_camera_flags.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | #include <cutils/atomic.h> |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 53 | #include <cutils/properties.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 54 | #include <cutils/misc.h> |
Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 55 | #include <gui/Surface.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 56 | #include <hardware/hardware.h> |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 57 | #include "hidl/HidlCameraService.h" |
| 58 | #include <hidl/HidlTransportSupport.h> |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 59 | #include <hwbinder/IPCThreadState.h> |
Eino-Ville Talvala | d89821e | 2016-04-20 11:23:50 -0700 | [diff] [blame] | 60 | #include <memunreachable/memunreachable.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 61 | #include <media/AudioSystem.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 62 | #include <media/IMediaHTTPService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 63 | #include <media/mediaplayer.h> |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 64 | #include <mediautils/BatteryNotifier.h> |
Steven Moreland | 886d732 | 2021-04-02 04:19:45 +0000 | [diff] [blame] | 65 | #include <processinfo/ProcessInfoService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 66 | #include <utils/Errors.h> |
| 67 | #include <utils/Log.h> |
| 68 | #include <utils/String16.h> |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 69 | #include <utils/SystemClock.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 70 | #include <utils/Trace.h> |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 71 | #include <utils/CallStack.h> |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 72 | #include <private/android_filesystem_config.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 73 | #include <system/camera_vendor_tags.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 74 | #include <system/camera_metadata.h> |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 75 | #include <binder/IServiceManager.h> |
| 76 | #include <binder/IActivityManager.h> |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 77 | #include <camera/CameraUtils.h> |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 78 | #include <camera/StringUtils.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 79 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 80 | #include <system/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 81 | |
| 82 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 83 | #include "api1/Camera2Client.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 84 | #include "api2/CameraDeviceClient.h" |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 85 | #include "utils/CameraTraces.h" |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 86 | #include "utils/TagMonitor.h" |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 87 | #include "utils/CameraServiceProxyWrapper.h" |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 88 | #include "utils/SessionConfigurationUtils.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 89 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 90 | namespace { |
| 91 | const char* kPermissionServiceName = "permission"; |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 92 | const char* kActivityServiceName = "activity"; |
| 93 | const char* kSensorPrivacyServiceName = "sensor_privacy"; |
| 94 | const char* kAppopsServiceName = "appops"; |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 95 | const char* kProcessInfoServiceName = "processinfo"; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 96 | const char* kVirtualDeviceBackCameraId = "0"; |
| 97 | const char* kVirtualDeviceFrontCameraId = "1"; |
| 98 | |
| 99 | int32_t getDeviceId(const android::CameraMetadata& cameraInfo) { |
| 100 | if (!cameraInfo.exists(ANDROID_INFO_DEVICE_ID)) { |
| 101 | return android::kDefaultDeviceId; |
| 102 | } |
| 103 | |
| 104 | const auto &deviceIdEntry = cameraInfo.find(ANDROID_INFO_DEVICE_ID); |
| 105 | return deviceIdEntry.data.i32[0]; |
| 106 | } |
| 107 | } // namespace anonymous |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 108 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 109 | namespace android { |
| 110 | |
Austin Borger | ea93124 | 2021-12-13 23:10:41 +0000 | [diff] [blame] | 111 | using namespace camera3; |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 112 | using namespace camera3::SessionConfigurationUtils; |
| 113 | |
| 114 | using binder::Status; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 115 | using companion::virtualnative::IVirtualDeviceManagerNative; |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 116 | using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService; |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 117 | using frameworks::cameraservice::service::implementation::AidlCameraService; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 118 | using hardware::ICamera; |
| 119 | using hardware::ICameraClient; |
| 120 | using hardware::ICameraServiceListener; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 121 | using hardware::camera2::ICameraInjectionCallback; |
| 122 | using hardware::camera2::ICameraInjectionSession; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 123 | using hardware::camera2::utils::CameraIdAndSessionConfiguration; |
| 124 | using hardware::camera2::utils::ConcurrentCameraIdCombination; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 125 | |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 126 | namespace flags = com::android::internal::camera::flags; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 127 | namespace vd_flags = android::companion::virtualdevice::flags; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 128 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 129 | // ---------------------------------------------------------------------------- |
| 130 | // Logging support -- this is for debugging only |
| 131 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 132 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 133 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 134 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 135 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 136 | |
| 137 | static void setLogLevel(int level) { |
| 138 | android_atomic_write(level, &gLogLevel); |
| 139 | } |
| 140 | |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 141 | int32_t format_as(CameraService::StatusInternal s) { |
| 142 | return fmt::underlying(s); |
| 143 | } |
| 144 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 145 | // ---------------------------------------------------------------------------- |
| 146 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 147 | // Permission strings (references to AttributionAndPermissionUtils for brevity) |
| 148 | static const std::string &sDumpPermission = |
| 149 | AttributionAndPermissionUtils::sDumpPermission; |
| 150 | static const std::string &sManageCameraPermission = |
| 151 | AttributionAndPermissionUtils::sManageCameraPermission; |
| 152 | static const std::string &sCameraSendSystemEventsPermission = |
| 153 | AttributionAndPermissionUtils::sCameraSendSystemEventsPermission; |
| 154 | static const std::string &sCameraInjectExternalCameraPermission = |
| 155 | AttributionAndPermissionUtils::sCameraInjectExternalCameraPermission; |
| 156 | |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 157 | // Constant integer for FGS Logging, used to denote the API type for logger |
| 158 | static const int LOG_FGS_CAMERA_API = 1; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 159 | const char *sFileName = "lastOpenSessionDumpFile"; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 160 | static constexpr int32_t kSystemNativeClientScore = resource_policy::PERCEPTIBLE_APP_ADJ; |
| 161 | static constexpr int32_t kSystemNativeClientState = |
| 162 | ActivityManager::PROCESS_STATE_PERSISTENT_UI; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 163 | static const std::string kServiceName("cameraserver"); |
Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame] | 164 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 165 | const std::string CameraService::kOfflineDevice("offline-"); |
| 166 | const std::string CameraService::kWatchAllClientsFlag("all"); |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 167 | |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 168 | // Set to keep track of logged service error events. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 169 | static std::set<std::string> sServiceErrorEventSet; |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 170 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 171 | CameraService::CameraService( |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 172 | std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper, |
| 173 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils) : |
| 174 | AttributionAndPermissionUtilsEncapsulator(attributionAndPermissionUtils == nullptr ? |
| 175 | std::make_shared<AttributionAndPermissionUtils>()\ |
| 176 | : attributionAndPermissionUtils), |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 177 | mCameraServiceProxyWrapper(cameraServiceProxyWrapper == nullptr ? |
| 178 | std::make_shared<CameraServiceProxyWrapper>() : cameraServiceProxyWrapper), |
Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 179 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 180 | mNumberOfCameras(0), |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 181 | mNumberOfCamerasWithoutSystemCamera(0), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 182 | mSoundRef(0), mInitialized(false), |
| 183 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 184 | ALOGI("CameraService started (pid=%d)", getpid()); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 185 | mAttributionAndPermissionUtils->setCameraService(this); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 186 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 187 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 188 | if (mMemFd == -1) { |
| 189 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 190 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Charles Chen | a7b613c | 2023-01-24 21:57:33 +0000 | [diff] [blame] | 193 | // Enable processes with isolated AID to request the binder |
| 194 | void CameraService::instantiate() { |
| 195 | CameraService::publish(true); |
| 196 | } |
| 197 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 198 | void CameraService::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 199 | if (name != toString16(kAppopsServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | |
| 203 | ALOGV("appops service registered. setting camera audio restriction"); |
| 204 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
| 205 | } |
| 206 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 207 | void CameraService::onFirstRef() |
| 208 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 209 | ALOGI("CameraService process starting"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 210 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 211 | BnCameraService::onFirstRef(); |
| 212 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 213 | // Update battery life tracking if service is restarting |
| 214 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 215 | notifier.noteResetCamera(); |
| 216 | notifier.noteResetFlashlight(); |
| 217 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 218 | status_t res = INVALID_OPERATION; |
Eino-Ville Talvala | 9cbbc83 | 2017-01-23 15:39:53 -0800 | [diff] [blame] | 219 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 220 | res = enumerateProviders(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 221 | if (res == OK) { |
| 222 | mInitialized = true; |
| 223 | } |
| 224 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 225 | mUidPolicy = new UidPolicy(this); |
| 226 | mUidPolicy->registerSelf(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 227 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this, mAttributionAndPermissionUtils); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 228 | mSensorPrivacyPolicy->registerSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 229 | mInjectionStatusListener = new InjectionStatusListener(this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 230 | |
| 231 | // appops function setCamerAudioRestriction uses getService which |
| 232 | // is blocking till the appops service is ready. To enable early |
| 233 | // boot availability for cameraservice, use checkService which is |
| 234 | // non blocking and register for notifications |
| 235 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 236 | sp<IBinder> binder = sm->checkService(toString16(kAppopsServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 237 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 238 | sm->registerForNotifications(toString16(kAppopsServiceName), this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 239 | } else { |
| 240 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
| 241 | } |
| 242 | |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 243 | sp<HidlCameraService> hcs = HidlCameraService::getInstance(this); |
| 244 | if (hcs->registerAsService() != android::OK) { |
Devin Moore | a1350e7 | 2023-01-11 23:40:42 +0000 | [diff] [blame] | 245 | // Deprecated, so it will fail to register on newer devices |
| 246 | ALOGW("%s: Did not register default android.frameworks.cameraservice.service@2.2", |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 247 | __FUNCTION__); |
| 248 | } |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 249 | |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 250 | if (!AidlCameraService::registerService(this)) { |
| 251 | ALOGE("%s: Failed to register default AIDL VNDK CameraService", __FUNCTION__); |
| 252 | } |
| 253 | |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 254 | // This needs to be last call in this function, so that it's as close to |
| 255 | // ServiceManager::addService() as possible. |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 256 | mCameraServiceProxyWrapper->pingCameraServiceProxy(); |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 257 | ALOGI("CameraService pinged cameraservice proxy"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 260 | status_t CameraService::enumerateProviders() { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 261 | status_t res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 262 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 263 | std::vector<std::string> deviceIds; |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 264 | std::unordered_map<std::string, std::set<std::string>> unavailPhysicalIds; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 265 | { |
| 266 | Mutex::Autolock l(mServiceLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 267 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 268 | if (nullptr == mCameraProviderManager.get()) { |
| 269 | mCameraProviderManager = new CameraProviderManager(); |
| 270 | res = mCameraProviderManager->initialize(this); |
| 271 | if (res != OK) { |
| 272 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", |
| 273 | __FUNCTION__, strerror(-res), res); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 274 | logServiceError("Unable to initialize camera provider manager", |
| 275 | ERROR_DISCONNECTED); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 276 | return res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 280 | // Setup vendor tags before we call get_camera_info the first time |
| 281 | // because HAL might need to setup static vendor keys in get_camera_info |
| 282 | // TODO: maybe put this into CameraProviderManager::initialize()? |
| 283 | mCameraProviderManager->setUpVendorTags(); |
| 284 | |
| 285 | if (nullptr == mFlashlight.get()) { |
| 286 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 289 | res = mFlashlight->findFlashUnits(); |
| 290 | if (res != OK) { |
| 291 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); |
| 292 | } |
| 293 | |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 294 | deviceIds = mCameraProviderManager->getCameraDeviceIds(&unavailPhysicalIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 297 | for (auto& cameraId : deviceIds) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 298 | if (getCameraState(cameraId) == nullptr) { |
| 299 | onDeviceStatusChanged(cameraId, CameraDeviceStatus::PRESENT); |
Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 300 | } |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 301 | if (unavailPhysicalIds.count(cameraId) > 0) { |
| 302 | for (const auto& physicalId : unavailPhysicalIds[cameraId]) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 303 | onDeviceStatusChanged(cameraId, physicalId, CameraDeviceStatus::NOT_PRESENT); |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 308 | // Derive primary rear/front cameras, and filter their charactierstics. |
| 309 | // 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] | 310 | if (SessionConfigurationUtils::IS_PERF_CLASS) { |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 311 | // Assume internal cameras are advertised from the same |
| 312 | // provider. If multiple providers are registered at different time, |
| 313 | // and each provider contains multiple internal color cameras, the current |
| 314 | // logic may filter the characteristics of more than one front/rear color |
| 315 | // cameras. |
| 316 | Mutex::Autolock l(mServiceLock); |
| 317 | filterSPerfClassCharacteristicsLocked(); |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 318 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 319 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 320 | return OK; |
| 321 | } |
| 322 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 323 | void CameraService::broadcastTorchModeStatus(const std::string& cameraId, TorchModeStatus status, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 324 | SystemCameraKind systemCameraKind) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 325 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 326 | auto [deviceId, mappedCameraId] = |
| 327 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 328 | |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 329 | Mutex::Autolock lock(mStatusListenerLock); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 330 | for (auto& i : mListenerList) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 331 | if (shouldSkipStatusUpdates(systemCameraKind, i->isVendorListener(), i->getListenerPid(), |
| 332 | i->getListenerUid())) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 333 | ALOGV("%s: Skipping torch callback for system-only camera device %s", |
| 334 | __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 335 | continue; |
| 336 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 337 | |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 338 | auto ret = i->getListener()->onTorchStatusChanged(mapToInterface(status), |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 339 | mappedCameraId, deviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 340 | i->handleBinderStatus(ret, "%s: Failed to trigger onTorchStatusChanged for %d:%d: %d", |
| 341 | __FUNCTION__, i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 342 | |
| 343 | // Only cameras of the default device can be remapped to a different camera (using |
| 344 | // remapCameraIds method), so do the following only if the camera is associated with the |
| 345 | // default device. |
| 346 | if (deviceId == kDefaultDeviceId) { |
| 347 | // For the default device, also trigger the torch callbacks for cameras that were |
| 348 | // remapped to the current cameraId for the specific package that this listener belongs |
| 349 | // to. |
| 350 | std::vector<std::string> remappedCameraIds = |
| 351 | findOriginalIdsForRemappedCameraId(cameraId, i->getListenerUid()); |
| 352 | for (auto &remappedCameraId: remappedCameraIds) { |
| 353 | ret = i->getListener()->onTorchStatusChanged(mapToInterface(status), |
| 354 | remappedCameraId, kDefaultDeviceId); |
| 355 | i->handleBinderStatus(ret, |
| 356 | "%s: Failed to trigger onTorchStatusChanged for %d:%d: %d", |
| 357 | __FUNCTION__, i->getListenerUid(), i->getListenerPid(), |
| 358 | ret.exceptionCode()); |
| 359 | } |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 360 | } |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 364 | CameraService::~CameraService() { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 365 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 366 | mUidPolicy->unregisterSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 367 | mSensorPrivacyPolicy->unregisterSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 368 | mInjectionStatusListener->removeListener(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 371 | void CameraService::onNewProviderRegistered() { |
| 372 | enumerateProviders(); |
| 373 | } |
| 374 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 375 | void CameraService::filterAPI1SystemCameraLocked( |
| 376 | const std::vector<std::string> &normalDeviceIds) { |
| 377 | mNormalDeviceIdsWithoutSystemCamera.clear(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 378 | for (auto &cameraId : normalDeviceIds) { |
| 379 | if (vd_flags::camera_device_awareness()) { |
| 380 | CameraMetadata cameraInfo; |
| 381 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
| 382 | cameraId, false, &cameraInfo, false); |
| 383 | int32_t deviceId = kDefaultDeviceId; |
| 384 | if (res != OK) { |
| 385 | ALOGW("%s: Not able to get camera characteristics for camera id %s", |
| 386 | __FUNCTION__, cameraId.c_str()); |
| 387 | } else { |
| 388 | deviceId = getDeviceId(cameraInfo); |
| 389 | } |
| 390 | // Cameras associated with non-default device id's (i.e., virtual cameras) can never be |
| 391 | // system cameras, so skip for non-default device id's. |
| 392 | if (deviceId != kDefaultDeviceId) { |
| 393 | continue; |
| 394 | } |
| 395 | } |
| 396 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 397 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 398 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 399 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 400 | continue; |
| 401 | } |
| 402 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 403 | // All system camera ids will necessarily come after public camera |
| 404 | // device ids as per the HAL interface contract. |
| 405 | break; |
| 406 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 407 | mNormalDeviceIdsWithoutSystemCamera.push_back(cameraId); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 408 | } |
| 409 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, |
| 410 | mNormalDeviceIdsWithoutSystemCamera.size()); |
| 411 | } |
| 412 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 413 | status_t CameraService::getSystemCameraKind(const std::string& cameraId, |
| 414 | SystemCameraKind *kind) const { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 415 | auto state = getCameraState(cameraId); |
| 416 | if (state != nullptr) { |
| 417 | *kind = state->getSystemCameraKind(); |
| 418 | return OK; |
| 419 | } |
| 420 | // Hidden physical camera ids won't have CameraState |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 421 | return mCameraProviderManager->getSystemCameraKind(cameraId, kind); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 424 | void CameraService::updateCameraNumAndIds() { |
| 425 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 426 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); |
| 427 | // Excludes hidden secure cameras |
| 428 | mNumberOfCameras = |
| 429 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; |
| 430 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 431 | mNormalDeviceIds = |
| 432 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 433 | filterAPI1SystemCameraLocked(mNormalDeviceIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 436 | void CameraService::filterSPerfClassCharacteristicsLocked() { |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 437 | // To claim to be S Performance primary cameras, the cameras must be |
| 438 | // backward compatible. So performance class primary camera Ids must be API1 |
| 439 | // compatible. |
| 440 | bool firstRearCameraSeen = false, firstFrontCameraSeen = false; |
| 441 | for (const auto& cameraId : mNormalDeviceIdsWithoutSystemCamera) { |
| 442 | int facing = -1; |
| 443 | int orientation = 0; |
Shuzhen Wang | f221e8d | 2022-12-15 13:26:29 -0800 | [diff] [blame] | 444 | int portraitRotation; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 445 | getDeviceVersion(cameraId, /*overrideToPortrait*/false, /*out*/&portraitRotation, |
Shuzhen Wang | f221e8d | 2022-12-15 13:26:29 -0800 | [diff] [blame] | 446 | /*out*/&facing, /*out*/&orientation); |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 447 | if (facing == -1) { |
| 448 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.c_str()); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | if ((facing == hardware::CAMERA_FACING_BACK && !firstRearCameraSeen) || |
| 453 | (facing == hardware::CAMERA_FACING_FRONT && !firstFrontCameraSeen)) { |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 454 | status_t res = mCameraProviderManager->filterSmallJpegSizes(cameraId); |
| 455 | if (res == OK) { |
| 456 | mPerfClassPrimaryCameraIds.insert(cameraId); |
| 457 | } else { |
| 458 | ALOGE("%s: Failed to filter small JPEG sizes for performance class primary " |
| 459 | "camera %s: %s(%d)", __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
| 460 | break; |
| 461 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 462 | |
| 463 | if (facing == hardware::CAMERA_FACING_BACK) { |
| 464 | firstRearCameraSeen = true; |
| 465 | } |
| 466 | if (facing == hardware::CAMERA_FACING_FRONT) { |
| 467 | firstFrontCameraSeen = true; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | if (firstRearCameraSeen && firstFrontCameraSeen) { |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 477 | void CameraService::addStates(const std::string& cameraId) { |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 478 | CameraResourceCost cost; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 479 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); |
| 480 | if (res != OK) { |
| 481 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); |
| 482 | return; |
| 483 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 484 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 485 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); |
| 486 | if (res != OK) { |
| 487 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); |
| 488 | return; |
| 489 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 490 | std::vector<std::string> physicalCameraIds; |
| 491 | mCameraProviderManager->isLogicalCamera(cameraId, &physicalCameraIds); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 492 | std::set<std::string> conflicting; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 493 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 494 | conflicting.emplace(cost.conflictingDevices[i]); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | { |
| 498 | Mutex::Autolock lock(mCameraStatesLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 499 | mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost.resourceCost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 500 | conflicting, deviceKind, physicalCameraIds)); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 503 | if (mFlashlight->hasFlashUnit(cameraId)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 504 | Mutex::Autolock al(mTorchStatusMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 505 | mTorchStatusMap.add(cameraId, TorchModeStatus::AVAILABLE_OFF); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 506 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 507 | broadcastTorchModeStatus(cameraId, TorchModeStatus::AVAILABLE_OFF, deviceKind); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 508 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 509 | |
| 510 | updateCameraNumAndIds(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 511 | logDeviceAdded(cameraId, "Device added"); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 512 | } |
| 513 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 514 | void CameraService::removeStates(const std::string& cameraId) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 515 | updateCameraNumAndIds(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 516 | if (mFlashlight->hasFlashUnit(cameraId)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 517 | Mutex::Autolock al(mTorchStatusMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 518 | mTorchStatusMap.removeItem(cameraId); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | { |
| 522 | Mutex::Autolock lock(mCameraStatesLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 523 | mCameraStates.erase(cameraId); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 527 | void CameraService::onDeviceStatusChanged(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 528 | CameraDeviceStatus newHalStatus) { |
| 529 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 530 | cameraId.c_str(), newHalStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 531 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 532 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 533 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 534 | std::shared_ptr<CameraState> state = getCameraState(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 535 | |
| 536 | if (state == nullptr) { |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 537 | if (newStatus == StatusInternal::PRESENT) { |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 538 | ALOGI("%s: Unknown camera ID %s, a new camera is added", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 539 | __FUNCTION__, cameraId.c_str()); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 540 | |
| 541 | // First add as absent to make sure clients are notified below |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 542 | addStates(cameraId); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 543 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 544 | updateStatus(newStatus, cameraId); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 545 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 546 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, cameraId.c_str()); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 547 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 548 | return; |
| 549 | } |
| 550 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 551 | StatusInternal oldStatus = state->getStatus(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 552 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 553 | if (oldStatus == newStatus) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 554 | 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] | 555 | return; |
| 556 | } |
| 557 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 558 | if (newStatus == StatusInternal::NOT_PRESENT) { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 559 | logDeviceRemoved(cameraId, fmt::format("Device status changed from {} to {}", |
| 560 | oldStatus, newStatus)); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 561 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 562 | // to this device until the status changes |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 563 | updateStatus(StatusInternal::NOT_PRESENT, cameraId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 564 | mVirtualDeviceCameraIdMapper.removeCamera(cameraId); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 565 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 566 | sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 567 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 568 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 569 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 570 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 571 | // Remove cached shim parameters |
| 572 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 573 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 574 | // Remove online as well as offline client from the list of active clients, |
| 575 | // if they are present |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 576 | clientToDisconnectOnline = removeClientLocked(cameraId); |
| 577 | clientToDisconnectOffline = removeClientLocked(kOfflineDevice + cameraId); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 578 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 579 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 580 | disconnectClient(cameraId, clientToDisconnectOnline); |
| 581 | disconnectClient(kOfflineDevice + cameraId, clientToDisconnectOffline); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 582 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 583 | removeStates(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 584 | } else { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 585 | if (oldStatus == StatusInternal::NOT_PRESENT) { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 586 | logDeviceAdded(cameraId, fmt::format("Device status changed from {} to {}", |
| 587 | oldStatus, newStatus)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 588 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 589 | updateStatus(newStatus, cameraId); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 590 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 591 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 592 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 593 | void CameraService::onDeviceStatusChanged(const std::string& id, |
| 594 | const std::string& physicalId, |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 595 | CameraDeviceStatus newHalStatus) { |
| 596 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 597 | __FUNCTION__, id.c_str(), physicalId.c_str(), newHalStatus); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 598 | |
| 599 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 600 | |
| 601 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 602 | |
| 603 | if (state == nullptr) { |
| 604 | 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] | 605 | __FUNCTION__, physicalId.c_str(), id.c_str()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 606 | return; |
| 607 | } |
| 608 | |
| 609 | StatusInternal logicalCameraStatus = state->getStatus(); |
| 610 | if (logicalCameraStatus != StatusInternal::PRESENT && |
| 611 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 612 | 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] | 613 | __FUNCTION__, physicalId.c_str(), newHalStatus, logicalCameraStatus); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | |
| 617 | bool updated = false; |
| 618 | if (newStatus == StatusInternal::PRESENT) { |
| 619 | updated = state->removeUnavailablePhysicalId(physicalId); |
| 620 | } else { |
| 621 | updated = state->addUnavailablePhysicalId(physicalId); |
| 622 | } |
| 623 | |
| 624 | if (updated) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 625 | std::string idCombo = id + " : " + physicalId; |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 626 | if (newStatus == StatusInternal::PRESENT) { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 627 | logDeviceAdded(idCombo, fmt::format("Device status changed to {}", newStatus)); |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 628 | } else { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 629 | logDeviceRemoved(idCombo, fmt::format("Device status changed to {}", newStatus)); |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 630 | } |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 631 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 632 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 633 | if (getSystemCameraKind(id, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 634 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 635 | return; |
| 636 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 637 | Mutex::Autolock lock(mStatusListenerLock); |
| 638 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 639 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 640 | listener->getListenerPid(), listener->getListenerUid())) { |
| 641 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 642 | id.c_str()); |
| 643 | continue; |
| 644 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 645 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged( |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 646 | mapToInterface(newStatus), id, physicalId, kDefaultDeviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 647 | listener->handleBinderStatus(ret, |
| 648 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 649 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 650 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 651 | } |
| 652 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 655 | void CameraService::disconnectClient(const std::string& id, sp<BasicClient> clientToDisconnect) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 656 | if (clientToDisconnect.get() != nullptr) { |
| 657 | 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] | 658 | __FUNCTION__, id.c_str()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 659 | // Notify the client of disconnection |
| 660 | clientToDisconnect->notifyError( |
| 661 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 662 | CaptureResultExtras{}); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 663 | clientToDisconnect->disconnect(); |
| 664 | } |
| 665 | } |
| 666 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 667 | void CameraService::onTorchStatusChanged(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 668 | TorchModeStatus newStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 669 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 670 | status_t res = getSystemCameraKind(cameraId, &systemCameraKind); |
| 671 | if (res != OK) { |
| 672 | 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] | 673 | cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 674 | return; |
| 675 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 676 | Mutex::Autolock al(mTorchStatusMutex); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 677 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 680 | void CameraService::onTorchStatusChanged(const std::string& cameraId, |
Jayant Chowdhary | 46ef0f5 | 2021-10-05 14:36:13 -0700 | [diff] [blame] | 681 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
| 682 | Mutex::Autolock al(mTorchStatusMutex); |
| 683 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
| 684 | } |
| 685 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 686 | void CameraService::broadcastTorchStrengthLevel(const std::string& cameraId, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 687 | int32_t newStrengthLevel) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 688 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 689 | auto [deviceId, mappedCameraId] = |
| 690 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 691 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 692 | Mutex::Autolock lock(mStatusListenerLock); |
| 693 | for (auto& i : mListenerList) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 694 | auto ret = i->getListener()->onTorchStrengthLevelChanged(mappedCameraId, |
| 695 | newStrengthLevel, deviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 696 | i->handleBinderStatus(ret, |
| 697 | "%s: Failed to trigger onTorchStrengthLevelChanged for %d:%d: %d", __FUNCTION__, |
| 698 | i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 702 | void CameraService::onTorchStatusChangedLocked(const std::string& cameraId, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 703 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 704 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 705 | __FUNCTION__, cameraId.c_str(), newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 706 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 707 | TorchModeStatus status; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 708 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 709 | if (res) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 710 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 711 | __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 712 | return; |
| 713 | } |
| 714 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 715 | return; |
| 716 | } |
| 717 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 718 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 719 | if (res) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 720 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, |
| 721 | (uint32_t)newStatus, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 722 | return; |
| 723 | } |
| 724 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 725 | { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 726 | // Update battery life logging for flashlight |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 727 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 728 | auto iter = mTorchUidMap.find(cameraId); |
| 729 | if (iter != mTorchUidMap.end()) { |
| 730 | int oldUid = iter->second.second; |
| 731 | int newUid = iter->second.first; |
| 732 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 733 | if (oldUid != newUid) { |
| 734 | // 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] | 735 | if (status == TorchModeStatus::AVAILABLE_ON) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 736 | notifier.noteFlashlightOff(toString8(cameraId), oldUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 737 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 738 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 739 | notifier.noteFlashlightOn(toString8(cameraId), newUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 740 | } |
| 741 | iter->second.second = newUid; |
| 742 | } else { |
| 743 | // If the UID has not changed, log the status |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 744 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 745 | notifier.noteFlashlightOn(toString8(cameraId), oldUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 746 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 747 | notifier.noteFlashlightOff(toString8(cameraId), oldUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | } |
| 751 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 752 | broadcastTorchModeStatus(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 753 | } |
| 754 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 755 | bool CameraService::isAutomotiveExteriorSystemCamera(const std::string& cam_id) const { |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 756 | // Returns false if this is not an automotive device type. |
| 757 | if (!isAutomotiveDevice()) |
| 758 | return false; |
| 759 | |
| 760 | // Returns false if no camera id is provided. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 761 | if (cam_id.empty()) |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 762 | return false; |
| 763 | |
| 764 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 765 | if (getSystemCameraKind(cam_id, &systemCameraKind) != OK) { |
| 766 | // This isn't a known camera ID, so it's not a system camera. |
| 767 | ALOGE("%s: Unknown camera id %s, ", __FUNCTION__, cam_id.c_str()); |
| 768 | return false; |
| 769 | } |
| 770 | |
| 771 | if (systemCameraKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
| 772 | ALOGE("%s: camera id %s is not a system camera", __FUNCTION__, cam_id.c_str()); |
| 773 | return false; |
| 774 | } |
| 775 | |
| 776 | CameraMetadata cameraInfo; |
| 777 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 778 | cam_id, false, &cameraInfo, false); |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 779 | if (res != OK){ |
| 780 | ALOGE("%s: Not able to get camera characteristics for camera id %s",__FUNCTION__, |
| 781 | cam_id.c_str()); |
| 782 | return false; |
| 783 | } |
| 784 | |
| 785 | camera_metadata_entry auto_location = cameraInfo.find(ANDROID_AUTOMOTIVE_LOCATION); |
| 786 | if (auto_location.count != 1) |
| 787 | return false; |
| 788 | |
| 789 | uint8_t location = auto_location.data.u8[0]; |
| 790 | if ((location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_FRONT) && |
| 791 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_REAR) && |
| 792 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_LEFT) && |
| 793 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_RIGHT)) { |
| 794 | return false; |
| 795 | } |
| 796 | |
| 797 | return true; |
| 798 | } |
| 799 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 800 | Status CameraService::getNumberOfCameras(int32_t type, int32_t deviceId, int32_t devicePolicy, |
| 801 | int32_t* numCameras) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 802 | ATRACE_CALL(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 803 | if (vd_flags::camera_device_awareness() && (deviceId != kDefaultDeviceId) |
| 804 | && (devicePolicy != IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT)) { |
| 805 | *numCameras = mVirtualDeviceCameraIdMapper.getNumberOfCameras(deviceId); |
| 806 | return Status::ok(); |
| 807 | } |
| 808 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 809 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 810 | bool hasSystemCameraPermissions = |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 811 | hasPermissionsForSystemCamera(std::string(), getCallingPid(), |
| 812 | getCallingUid()); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 813 | switch (type) { |
| 814 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 815 | if (hasSystemCameraPermissions) { |
| 816 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); |
| 817 | } else { |
| 818 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); |
| 819 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 820 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 821 | case CAMERA_TYPE_ALL: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 822 | if (hasSystemCameraPermissions) { |
| 823 | *numCameras = mNumberOfCameras; |
| 824 | } else { |
| 825 | *numCameras = mNumberOfCamerasWithoutSystemCamera; |
| 826 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 827 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 828 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 829 | ALOGW("%s: Unknown camera type %d", |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 830 | __FUNCTION__, type); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 831 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 832 | "Unknown camera type %d", type); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 833 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 834 | return Status::ok(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 835 | } |
| 836 | |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 837 | Status CameraService::remapCameraIds(const hardware::CameraIdRemapping& cameraIdRemapping) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 838 | if (!checkCallingPermission(toString16(sCameraInjectExternalCameraPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 839 | const int pid = getCallingPid(); |
| 840 | const int uid = getCallingUid(); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 841 | ALOGE("%s: Permission Denial: can't configure camera ID mapping pid=%d, uid=%d", |
| 842 | __FUNCTION__, pid, uid); |
| 843 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 844 | "Permission Denial: no permission to configure camera id mapping"); |
| 845 | } |
| 846 | TCameraIdRemapping cameraIdRemappingMap{}; |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 847 | binder::Status parseStatus = parseCameraIdRemapping(cameraIdRemapping, &cameraIdRemappingMap); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 848 | if (!parseStatus.isOk()) { |
| 849 | return parseStatus; |
| 850 | } |
| 851 | remapCameraIds(cameraIdRemappingMap); |
| 852 | return Status::ok(); |
| 853 | } |
| 854 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 855 | Status CameraService::createDefaultRequest(const std::string& unresolvedCameraId, int templateId, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 856 | int32_t deviceId, int32_t devicePolicy, |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 857 | /* out */ |
| 858 | hardware::camera2::impl::CameraMetadataNative* request) { |
| 859 | ATRACE_CALL(); |
| 860 | |
| 861 | if (!flags::feature_combination_query()) { |
| 862 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, |
| 863 | "Camera subsystem doesn't support this method!"); |
| 864 | } |
| 865 | if (!mInitialized) { |
| 866 | ALOGE("%s: Camera subsystem is not available", __FUNCTION__); |
| 867 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 868 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 869 | } |
| 870 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 871 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
| 872 | devicePolicy, getCallingUid()); |
| 873 | if (!cameraIdOptional.has_value()) { |
| 874 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 875 | unresolvedCameraId.c_str(), deviceId); |
| 876 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 877 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 878 | } |
| 879 | std::string cameraId = cameraIdOptional.value(); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 880 | |
| 881 | binder::Status res; |
| 882 | if (request == nullptr) { |
| 883 | res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, |
| 884 | "Camera %s: Error creating default request", cameraId.c_str()); |
| 885 | return res; |
| 886 | } |
| 887 | camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT; |
| 888 | res = SessionConfigurationUtils::mapRequestTemplateFromClient( |
| 889 | cameraId, templateId, &tempId); |
| 890 | if (!res.isOk()) { |
| 891 | ALOGE("%s: Camera %s: failed to map request Template %d", |
| 892 | __FUNCTION__, cameraId.c_str(), templateId); |
| 893 | return res; |
| 894 | } |
| 895 | |
| 896 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 897 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to create default" |
| 898 | "request for system only device %s: ", cameraId.c_str()); |
| 899 | } |
| 900 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 901 | CameraMetadata metadata; |
| 902 | status_t err = mCameraProviderManager->createDefaultRequest(cameraId, tempId, &metadata); |
| 903 | if (err == OK) { |
| 904 | request->swap(metadata); |
| 905 | } else if (err == BAD_VALUE) { |
| 906 | res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT, |
| 907 | "Camera %s: Template ID %d is invalid or not supported: %s (%d)", |
| 908 | cameraId.c_str(), templateId, strerror(-err), err); |
| 909 | } else { |
| 910 | res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, |
| 911 | "Camera %s: Error creating default request for template %d: %s (%d)", |
| 912 | cameraId.c_str(), templateId, strerror(-err), err); |
| 913 | } |
| 914 | return res; |
| 915 | } |
| 916 | |
| 917 | Status CameraService::isSessionConfigurationWithParametersSupported( |
| 918 | const std::string& unresolvedCameraId, |
| 919 | const SessionConfiguration& sessionConfiguration, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 920 | int32_t deviceId, int32_t devicePolicy, |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 921 | /*out*/ |
| 922 | bool* supported) { |
| 923 | ATRACE_CALL(); |
| 924 | |
| 925 | if (!flags::feature_combination_query()) { |
| 926 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, |
| 927 | "Camera subsystem doesn't support this method!"); |
| 928 | } |
| 929 | if (!mInitialized) { |
| 930 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 931 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 932 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 933 | } |
| 934 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 935 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
| 936 | devicePolicy, getCallingUid()); |
| 937 | if (!cameraIdOptional.has_value()) { |
| 938 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 939 | unresolvedCameraId.c_str(), deviceId); |
| 940 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 941 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 942 | } |
| 943 | std::string cameraId = cameraIdOptional.value(); |
| 944 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 945 | if (supported == nullptr) { |
| 946 | std::string msg = fmt::sprintf("Camera %s: Invalid 'support' input!", |
| 947 | unresolvedCameraId.c_str()); |
| 948 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 949 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 950 | } |
| 951 | |
| 952 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 953 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query " |
| 954 | "session configuration with parameters support for system only device %s: ", |
| 955 | cameraId.c_str()); |
| 956 | } |
| 957 | |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame^] | 958 | return isSessionConfigurationWithParametersSupportedUnsafe(cameraId, sessionConfiguration, |
| 959 | /*overrideForPerfClass=*/false, |
| 960 | supported); |
| 961 | } |
| 962 | |
| 963 | Status CameraService::isSessionConfigurationWithParametersSupportedUnsafe( |
| 964 | const std::string& cameraId, const SessionConfiguration& sessionConfiguration, |
| 965 | bool overrideForPerfClass, /*out*/ bool* supported) { |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 966 | *supported = false; |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame^] | 967 | status_t ret = mCameraProviderManager->isSessionConfigurationSupported( |
| 968 | cameraId, sessionConfiguration, overrideForPerfClass, |
| 969 | /*checkSessionParams=*/true, supported); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 970 | binder::Status res; |
| 971 | switch (ret) { |
| 972 | case OK: |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame^] | 973 | // Expected. Do Nothing. |
| 974 | return Status::ok(); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 975 | case INVALID_OPERATION: { |
| 976 | std::string msg = fmt::sprintf( |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame^] | 977 | "Camera %s: Session configuration with parameters supported query not " |
| 978 | "supported!", |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 979 | cameraId.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame^] | 980 | ALOGW("%s: %s", __FUNCTION__, msg.c_str()); |
| 981 | logServiceError(msg, CameraService::ERROR_INVALID_OPERATION); |
| 982 | *supported = false; |
| 983 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 984 | } |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame^] | 985 | break; |
| 986 | case NAME_NOT_FOUND: { |
| 987 | std::string msg = fmt::sprintf("Camera %s: Unknown camera ID.", cameraId.c_str()); |
| 988 | ALOGW("%s: %s", __FUNCTION__, msg.c_str()); |
| 989 | logServiceError(msg, CameraService::ERROR_ILLEGAL_ARGUMENT); |
| 990 | *supported = false; |
| 991 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 992 | } |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 993 | break; |
| 994 | default: { |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame^] | 995 | std::string msg = fmt::sprintf( |
| 996 | "Unable to retrieve session configuration support for camera " |
| 997 | "device %s: Error: %s (%d)", |
| 998 | cameraId.c_str(), strerror(-ret), ret); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 999 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame^] | 1000 | logServiceError(msg, CameraService::ERROR_ILLEGAL_ARGUMENT); |
| 1001 | *supported = false; |
| 1002 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 1003 | } |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame^] | 1004 | break; |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 1005 | } |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1008 | Status CameraService::getSessionCharacteristics(const std::string& unresolvedCameraId, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1009 | int targetSdkVersion, bool overrideToPortrait, |
| 1010 | const SessionConfiguration& sessionConfiguration, int32_t deviceId, int32_t devicePolicy, |
| 1011 | /*out*/ CameraMetadata* outMetadata) { |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1012 | ATRACE_CALL(); |
| 1013 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1014 | if (outMetadata == nullptr) { |
| 1015 | std::string msg = |
| 1016 | fmt::sprintf("Camera %s: Invalid 'outMetadata' input!", unresolvedCameraId.c_str()); |
| 1017 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1018 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1019 | } |
| 1020 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1021 | if (!mInitialized) { |
| 1022 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 1023 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 1024 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 1025 | } |
| 1026 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1027 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1028 | devicePolicy, getCallingUid()); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1029 | if (!cameraIdOptional.has_value()) { |
| 1030 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1031 | unresolvedCameraId.c_str(), deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1032 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1033 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1034 | } |
| 1035 | std::string cameraId = cameraIdOptional.value(); |
| 1036 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1037 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1038 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1039 | "Unable to retrieve camera" |
| 1040 | "characteristics for system only device %s: ", |
| 1041 | cameraId.c_str()); |
| 1042 | } |
| 1043 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1044 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
| 1045 | mPerfClassPrimaryCameraIds, cameraId, targetSdkVersion); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame^] | 1046 | if (flags::check_session_support_before_session_char()) { |
| 1047 | bool sessionConfigSupported; |
| 1048 | Status res = isSessionConfigurationWithParametersSupportedUnsafe( |
| 1049 | cameraId, sessionConfiguration, overrideForPerfClass, &sessionConfigSupported); |
| 1050 | if (!res.isOk()) { |
| 1051 | // isSessionConfigurationWithParametersSupportedUnsafe should log what went wrong and |
| 1052 | // report the correct Status to send to the client. Simply forward the error to |
| 1053 | // the client. |
| 1054 | outMetadata->clear(); |
| 1055 | return res; |
| 1056 | } |
| 1057 | if (!sessionConfigSupported) { |
| 1058 | std::string msg = fmt::sprintf( |
| 1059 | "Session configuration not supported for camera device %s.", cameraId.c_str()); |
| 1060 | outMetadata->clear(); |
| 1061 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1062 | } |
| 1063 | } |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1064 | |
| 1065 | status_t ret = mCameraProviderManager->getSessionCharacteristics( |
| 1066 | cameraId, sessionConfiguration, overrideForPerfClass, overrideToPortrait, outMetadata); |
| 1067 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1068 | switch (ret) { |
| 1069 | case OK: |
| 1070 | // Expected, no handling needed. |
| 1071 | break; |
| 1072 | case INVALID_OPERATION: { |
| 1073 | std::string msg = fmt::sprintf( |
| 1074 | "Camera %s: Session characteristics query not supported!", |
| 1075 | cameraId.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame^] | 1076 | ALOGW("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1077 | logServiceError(msg, CameraService::ERROR_INVALID_OPERATION); |
| 1078 | outMetadata->clear(); |
| 1079 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
| 1080 | } |
| 1081 | break; |
| 1082 | case NAME_NOT_FOUND: { |
| 1083 | std::string msg = fmt::sprintf( |
| 1084 | "Camera %s: Unknown camera ID.", |
| 1085 | cameraId.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame^] | 1086 | ALOGW("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1087 | logServiceError(msg, CameraService::ERROR_ILLEGAL_ARGUMENT); |
| 1088 | outMetadata->clear(); |
| 1089 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1090 | } |
| 1091 | break; |
| 1092 | default: { |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1093 | std::string msg = fmt::sprintf( |
| 1094 | "Unable to retrieve session characteristics for camera device %s: " |
| 1095 | "Error: %s (%d)", |
| 1096 | cameraId.c_str(), strerror(-ret), ret); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1097 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1098 | logServiceError(msg, CameraService::ERROR_INVALID_OPERATION); |
| 1099 | outMetadata->clear(); |
| 1100 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1104 | return filterSensitiveMetadataIfNeeded(cameraId, outMetadata); |
| 1105 | } |
| 1106 | |
| 1107 | Status CameraService::filterSensitiveMetadataIfNeeded( |
| 1108 | const std::string& cameraId, CameraMetadata* metadata) { |
| 1109 | int callingPid = getCallingPid(); |
| 1110 | int callingUid = getCallingUid(); |
| 1111 | |
| 1112 | if (callingPid == getpid()) { |
| 1113 | // Caller is cameraserver; no need to remove keys |
| 1114 | return Status::ok(); |
| 1115 | } |
| 1116 | |
| 1117 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1118 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 1119 | ALOGE("%s: Couldn't get camera kind for camera id %s", __FUNCTION__, cameraId.c_str()); |
| 1120 | metadata->clear(); |
| 1121 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1122 | "Unable to retrieve camera kind for device %s", cameraId.c_str()); |
| 1123 | } |
| 1124 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
| 1125 | // Attempting to query system only camera without system camera permission would have |
| 1126 | // failed the shouldRejectSystemCameraConnection in the caller. So if we get here |
| 1127 | // for a system only camera, then the caller has the required permission. |
| 1128 | // No need to remove keys |
| 1129 | return Status::ok(); |
| 1130 | } |
| 1131 | |
| 1132 | std::vector<int32_t> tagsRemoved; |
| 1133 | bool hasCameraPermission = hasPermissionsForCamera(cameraId, callingPid, callingUid); |
| 1134 | if (hasCameraPermission) { |
| 1135 | // Caller has camera permission; no need to remove keys |
| 1136 | return Status::ok(); |
| 1137 | } |
| 1138 | |
| 1139 | status_t ret = metadata->removePermissionEntries( |
| 1140 | mCameraProviderManager->getProviderTagIdLocked(cameraId), &tagsRemoved); |
| 1141 | if (ret != OK) { |
| 1142 | metadata->clear(); |
| 1143 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1144 | "Failed to remove camera characteristics needing camera permission " |
| 1145 | "for device %s:%s (%d)", |
| 1146 | cameraId.c_str(), strerror(-ret), ret); |
| 1147 | } |
| 1148 | |
| 1149 | if (!tagsRemoved.empty()) { |
| 1150 | ret = metadata->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, |
| 1151 | tagsRemoved.data(), tagsRemoved.size()); |
| 1152 | if (ret != OK) { |
| 1153 | metadata->clear(); |
| 1154 | return STATUS_ERROR_FMT( |
| 1155 | ERROR_INVALID_OPERATION, |
| 1156 | "Failed to insert camera keys needing permission for device %s: %s (%d)", |
| 1157 | cameraId.c_str(), strerror(-ret), ret); |
| 1158 | } |
| 1159 | } |
| 1160 | return Status::ok(); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1163 | Status CameraService::parseCameraIdRemapping( |
| 1164 | const hardware::CameraIdRemapping& cameraIdRemapping, |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1165 | /* out */ TCameraIdRemapping* cameraIdRemappingMap) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1166 | std::string packageName; |
| 1167 | std::string cameraIdToReplace, updatedCameraId; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1168 | for (const auto& packageIdRemapping: cameraIdRemapping.packageIdRemappings) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1169 | packageName = packageIdRemapping.packageName; |
malikakash | 0894f5b | 2023-08-10 22:46:47 +0000 | [diff] [blame] | 1170 | if (packageName.empty()) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1171 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1172 | "CameraIdRemapping: Package name cannot be empty"); |
| 1173 | } |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1174 | if (packageIdRemapping.cameraIdsToReplace.size() |
| 1175 | != packageIdRemapping.updatedCameraIds.size()) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1176 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1177 | "CameraIdRemapping: Mismatch in CameraId Remapping lists sizes for package %s", |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1178 | packageName.c_str()); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1179 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1180 | for (size_t i = 0; i < packageIdRemapping.cameraIdsToReplace.size(); i++) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1181 | cameraIdToReplace = packageIdRemapping.cameraIdsToReplace[i]; |
| 1182 | updatedCameraId = packageIdRemapping.updatedCameraIds[i]; |
malikakash | 0894f5b | 2023-08-10 22:46:47 +0000 | [diff] [blame] | 1183 | if (cameraIdToReplace.empty() || updatedCameraId.empty()) { |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1184 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1185 | "CameraIdRemapping: Camera Id cannot be empty for package %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1186 | packageName.c_str()); |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1187 | } |
| 1188 | if (cameraIdToReplace == updatedCameraId) { |
| 1189 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1190 | "CameraIdRemapping: CameraIdToReplace cannot be the same" |
| 1191 | " as updatedCameraId for %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1192 | packageName.c_str()); |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1193 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1194 | |
| 1195 | // Do not allow any camera remapping that involves a virtual camera. |
| 1196 | auto [deviceIdForCameraToReplace, _] = |
| 1197 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair( |
| 1198 | cameraIdToReplace); |
| 1199 | if (deviceIdForCameraToReplace != kDefaultDeviceId) { |
| 1200 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1201 | "CameraIdRemapping: CameraIdToReplace cannot be a virtual camera"); |
| 1202 | } |
| 1203 | [[maybe_unused]] auto [deviceIdForUpdatedCamera, unusedMappedCameraId] = |
| 1204 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(updatedCameraId); |
| 1205 | if (deviceIdForUpdatedCamera != kDefaultDeviceId) { |
| 1206 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1207 | "CameraIdRemapping: UpdatedCameraId cannot be a virtual camera"); |
| 1208 | } |
| 1209 | |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1210 | (*cameraIdRemappingMap)[packageName][cameraIdToReplace] = updatedCameraId; |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | return Status::ok(); |
| 1214 | } |
| 1215 | |
| 1216 | void CameraService::remapCameraIds(const TCameraIdRemapping& cameraIdRemapping) { |
| 1217 | // Acquire mServiceLock and prevent other clients from connecting |
| 1218 | std::unique_ptr<AutoConditionLock> serviceLockWrapper = |
| 1219 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 1220 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1221 | // Collect all existing clients for camera Ids that are being |
| 1222 | // remapped in the new cameraIdRemapping, but only if they were being used by a |
| 1223 | // targeted packageName. |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1224 | std::vector<sp<BasicClient>> clientsToDisconnect; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1225 | std::vector<std::string> cameraIdsToUpdate; |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1226 | for (const auto& [packageName, injectionMap] : cameraIdRemapping) { |
| 1227 | for (auto& [id0, id1] : injectionMap) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1228 | ALOGI("%s: UPDATE:= %s: %s: %s", __FUNCTION__, packageName.c_str(), |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1229 | id0.c_str(), id1.c_str()); |
| 1230 | auto clientDescriptor = mActiveClientManager.get(id0); |
| 1231 | if (clientDescriptor != nullptr) { |
| 1232 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1233 | if (clientSp->getPackageName() == packageName) { |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1234 | // This camera is being used by a targeted packageName and |
| 1235 | // being remapped to a new camera Id. We should disconnect it. |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1236 | clientsToDisconnect.push_back(clientSp); |
| 1237 | cameraIdsToUpdate.push_back(id0); |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1243 | for (auto& clientSp : clientsToDisconnect) { |
malikakash | f4c80f2 | 2023-09-25 21:50:28 +0000 | [diff] [blame] | 1244 | // Notify the clients about the disconnection. |
| 1245 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1246 | CaptureResultExtras{}); |
| 1247 | } |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1248 | |
| 1249 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 1250 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 1251 | mServiceLock.unlock(); |
| 1252 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1253 | // Clear calling identity for disconnect() PID checks. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1254 | int64_t token = clearCallingIdentity(); |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1255 | |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1256 | // Disconnect clients. |
| 1257 | for (auto& clientSp : clientsToDisconnect) { |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1258 | // This also triggers a call to updateStatus() which also reads mCameraIdRemapping |
| 1259 | // and requires mCameraIdRemappingLock. |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1260 | clientSp->disconnect(); |
| 1261 | } |
| 1262 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1263 | // Invoke destructors (which call disconnect()) now while we don't hold the mServiceLock. |
| 1264 | clientsToDisconnect.clear(); |
| 1265 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1266 | restoreCallingIdentity(token); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1267 | mServiceLock.lock(); |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1268 | |
| 1269 | { |
| 1270 | Mutex::Autolock lock(mCameraIdRemappingLock); |
| 1271 | // Update mCameraIdRemapping. |
| 1272 | mCameraIdRemapping.clear(); |
| 1273 | mCameraIdRemapping.insert(cameraIdRemapping.begin(), cameraIdRemapping.end()); |
| 1274 | } |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1277 | Status CameraService::injectSessionParams( |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1278 | const std::string& cameraId, |
| 1279 | const CameraMetadata& sessionParams) { |
| 1280 | if (!checkCallingPermission(toString16(sCameraInjectExternalCameraPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1281 | const int pid = getCallingPid(); |
| 1282 | const int uid = getCallingUid(); |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1283 | ALOGE("%s: Permission Denial: can't inject session params pid=%d, uid=%d", |
| 1284 | __FUNCTION__, pid, uid); |
| 1285 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 1286 | "Permission Denial: no permission to inject session params"); |
| 1287 | } |
| 1288 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1289 | // Do not allow session params injection for a virtual camera. |
| 1290 | auto [deviceId, _] = mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 1291 | if (deviceId != kDefaultDeviceId) { |
| 1292 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1293 | "Cannot inject session params for a virtual camera"); |
| 1294 | } |
| 1295 | |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1296 | std::unique_ptr<AutoConditionLock> serviceLockWrapper = |
| 1297 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 1298 | |
| 1299 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 1300 | if (clientDescriptor == nullptr) { |
| 1301 | ALOGI("%s: No active client for camera id %s", __FUNCTION__, cameraId.c_str()); |
| 1302 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1303 | "No active client for camera id %s", cameraId.c_str()); |
| 1304 | } |
| 1305 | |
| 1306 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1307 | status_t res = clientSp->injectSessionParams(sessionParams); |
| 1308 | |
| 1309 | if (res != OK) { |
| 1310 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1311 | "Error injecting session params into camera \"%s\": %s (%d)", |
| 1312 | cameraId.c_str(), strerror(-res), res); |
| 1313 | } |
| 1314 | return Status::ok(); |
| 1315 | } |
| 1316 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1317 | std::vector<std::string> CameraService::findOriginalIdsForRemappedCameraId( |
| 1318 | const std::string& inputCameraId, int clientUid) { |
| 1319 | std::string packageName = getPackageNameFromUid(clientUid); |
| 1320 | std::vector<std::string> cameraIds; |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1321 | Mutex::Autolock lock(mCameraIdRemappingLock); |
| 1322 | if (auto packageMapIter = mCameraIdRemapping.find(packageName); |
| 1323 | packageMapIter != mCameraIdRemapping.end()) { |
| 1324 | for (auto& [id0, id1]: packageMapIter->second) { |
| 1325 | if (id1 == inputCameraId) { |
| 1326 | cameraIds.push_back(id0); |
| 1327 | } |
| 1328 | } |
| 1329 | } |
| 1330 | return cameraIds; |
| 1331 | } |
| 1332 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1333 | std::string CameraService::resolveCameraId( |
| 1334 | const std::string& inputCameraId, |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1335 | int clientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1336 | const std::string& packageName) { |
| 1337 | std::string packageNameVal = packageName; |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1338 | if (packageName.empty()) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1339 | packageNameVal = getPackageNameFromUid(clientUid); |
| 1340 | } |
malikakash | 65d2069 | 2023-09-07 01:06:33 +0000 | [diff] [blame] | 1341 | if (clientUid < AID_APP_START || packageNameVal.empty()) { |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1342 | // We shouldn't remap cameras for processes with system/vendor UIDs. |
| 1343 | return inputCameraId; |
| 1344 | } |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1345 | Mutex::Autolock lock(mCameraIdRemappingLock); |
| 1346 | if (auto packageMapIter = mCameraIdRemapping.find(packageNameVal); |
| 1347 | packageMapIter != mCameraIdRemapping.end()) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1348 | auto packageMap = packageMapIter->second; |
| 1349 | if (auto replacementIdIter = packageMap.find(inputCameraId); |
| 1350 | replacementIdIter != packageMap.end()) { |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1351 | ALOGI("%s: resolveCameraId: remapping cameraId %s for %s to %s", |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1352 | __FUNCTION__, inputCameraId.c_str(), |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1353 | packageNameVal.c_str(), |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1354 | replacementIdIter->second.c_str()); |
| 1355 | return replacementIdIter->second; |
| 1356 | } |
| 1357 | } |
| 1358 | return inputCameraId; |
| 1359 | } |
| 1360 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1361 | std::optional<std::string> CameraService::resolveCameraId( |
| 1362 | const std::string& inputCameraId, |
| 1363 | int32_t deviceId, |
| 1364 | int32_t devicePolicy, |
| 1365 | int clientUid, |
| 1366 | const std::string& packageName) { |
| 1367 | if ((deviceId == kDefaultDeviceId) |
| 1368 | || (devicePolicy == IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT)) { |
| 1369 | auto [storedDeviceId, _] = |
| 1370 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(inputCameraId); |
| 1371 | if (storedDeviceId != kDefaultDeviceId) { |
| 1372 | // Trying to access a virtual camera from default-policy device context, we should fail. |
| 1373 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 1374 | inputCameraId.c_str(), deviceId); |
| 1375 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1376 | return std::nullopt; |
| 1377 | } |
| 1378 | return resolveCameraId(inputCameraId, clientUid, packageName); |
| 1379 | } |
| 1380 | |
| 1381 | return mVirtualDeviceCameraIdMapper.getActualCameraId(deviceId, inputCameraId); |
| 1382 | } |
| 1383 | |
| 1384 | Status CameraService::getCameraInfo(int cameraId, bool overrideToPortrait, int32_t deviceId, |
| 1385 | int32_t devicePolicy, CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1386 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 1387 | Mutex::Autolock l(mServiceLock); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1388 | std::string cameraIdStr = cameraIdIntToStrLocked(cameraId, deviceId, devicePolicy); |
| 1389 | if (cameraIdStr.empty()) { |
| 1390 | std::string msg = fmt::sprintf("Camera %d: Invalid camera id for device id %d", |
| 1391 | cameraId, deviceId); |
| 1392 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1393 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1394 | } |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1395 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1396 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1397 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 1398 | "characteristics for system only device %s: ", cameraIdStr.c_str()); |
| 1399 | } |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 1400 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1401 | if (!mInitialized) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1402 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1403 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1404 | "Camera subsystem is not available"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1405 | } |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1406 | bool hasSystemCameraPermissions = hasPermissionsForSystemCamera(std::to_string(cameraId), |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1407 | getCallingPid(), getCallingUid()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1408 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; |
| 1409 | if (hasSystemCameraPermissions) { |
| 1410 | cameraIdBound = mNumberOfCameras; |
| 1411 | } |
| 1412 | if (cameraId < 0 || cameraId >= cameraIdBound) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1413 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1414 | "CameraId is not valid"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1417 | Status ret = Status::ok(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1418 | int portraitRotation; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1419 | status_t err = mCameraProviderManager->getCameraInfo( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1420 | cameraIdStr, overrideToPortrait, &portraitRotation, cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1421 | if (err != OK) { |
| 1422 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1423 | "Error retrieving camera info from device %d: %s (%d)", cameraId, |
| 1424 | strerror(-err), err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1425 | logServiceError(std::string("Error retrieving camera info from device ") |
| 1426 | + std::to_string(cameraId), ERROR_INVALID_OPERATION); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1427 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1428 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1429 | return ret; |
| 1430 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1431 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1432 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt, |
| 1433 | int32_t deviceId, int32_t devicePolicy) { |
| 1434 | if (vd_flags::camera_device_awareness() && (deviceId != kDefaultDeviceId) |
| 1435 | && (devicePolicy != IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT)) { |
| 1436 | std::optional<std::string> cameraIdOptional = |
| 1437 | mVirtualDeviceCameraIdMapper.getActualCameraId(cameraIdInt, deviceId); |
| 1438 | return cameraIdOptional.has_value() ? cameraIdOptional.value() : std::string{}; |
| 1439 | } |
| 1440 | |
| 1441 | const std::vector<std::string> *cameraIds = &mNormalDeviceIdsWithoutSystemCamera; |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1442 | auto callingPid = getCallingPid(); |
| 1443 | auto callingUid = getCallingUid(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1444 | bool systemCameraPermissions = hasPermissionsForSystemCamera(std::to_string(cameraIdInt), |
| 1445 | callingPid, callingUid, /* checkCameraPermissions= */ false); |
| 1446 | if (systemCameraPermissions || getpid() == callingPid) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1447 | cameraIds = &mNormalDeviceIds; |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1448 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1449 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(cameraIds->size())) { |
| 1450 | ALOGE("%s: input id %d invalid: valid range (0, %zu)", |
| 1451 | __FUNCTION__, cameraIdInt, cameraIds->size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1452 | return std::string{}; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1453 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1454 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1455 | std::string unresolvedCameraId = (*cameraIds)[cameraIdInt]; |
| 1456 | return resolveCameraId(unresolvedCameraId, getCallingUid()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1457 | } |
| 1458 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1459 | std::string CameraService::cameraIdIntToStr(int cameraIdInt, int32_t deviceId, |
| 1460 | int32_t devicePolicy) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1461 | Mutex::Autolock lock(mServiceLock); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1462 | return cameraIdIntToStrLocked(cameraIdInt, deviceId, devicePolicy); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1463 | } |
| 1464 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1465 | Status CameraService::getCameraCharacteristics(const std::string& unresolvedCameraId, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1466 | int targetSdkVersion, bool overrideToPortrait, int32_t deviceId, int32_t devicePolicy, |
| 1467 | CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1468 | ATRACE_CALL(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1469 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1470 | if (!cameraInfo) { |
| 1471 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1472 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1473 | } |
| 1474 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1475 | if (!mInitialized) { |
| 1476 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1477 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1478 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1479 | "Camera subsystem is not available");; |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1482 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
| 1483 | devicePolicy, getCallingUid()); |
| 1484 | if (!cameraIdOptional.has_value()) { |
| 1485 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 1486 | unresolvedCameraId.c_str(), deviceId); |
| 1487 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1488 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1489 | } |
| 1490 | std::string cameraId = cameraIdOptional.value(); |
| 1491 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1492 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1493 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1494 | "characteristics for system only device %s: ", cameraId.c_str()); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1495 | } |
| 1496 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1497 | bool overrideForPerfClass = |
| 1498 | SessionConfigurationUtils::targetPerfClassPrimaryCamera(mPerfClassPrimaryCameraIds, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1499 | cameraId, targetSdkVersion); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1500 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1501 | cameraId, overrideForPerfClass, cameraInfo, overrideToPortrait); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1502 | if (res != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1503 | if (res == NAME_NOT_FOUND) { |
| 1504 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1505 | "characteristics for unknown device %s: %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1506 | strerror(-res), res); |
| 1507 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1508 | logServiceError(fmt::sprintf("Unable to retrieve camera characteristics for device %s.", |
| 1509 | cameraId.c_str()), ERROR_INVALID_OPERATION); |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1510 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1511 | "characteristics for device %s: %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1512 | strerror(-res), res); |
| 1513 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1514 | } |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 1515 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1516 | return filterSensitiveMetadataIfNeeded(cameraId, cameraInfo); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1519 | Status CameraService::getTorchStrengthLevel(const std::string& unresolvedCameraId, int32_t deviceId, |
| 1520 | int32_t devicePolicy, int32_t* torchStrength) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1521 | ATRACE_CALL(); |
| 1522 | Mutex::Autolock l(mServiceLock); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1523 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1524 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
| 1525 | devicePolicy, getCallingUid()); |
| 1526 | if (!cameraIdOptional.has_value()) { |
| 1527 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 1528 | unresolvedCameraId.c_str(), deviceId); |
| 1529 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1530 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1531 | } |
| 1532 | std::string cameraId = cameraIdOptional.value(); |
| 1533 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1534 | if (!mInitialized) { |
| 1535 | ALOGE("%s: Camera HAL couldn't be initialized.", __FUNCTION__); |
| 1536 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera HAL couldn't be initialized."); |
| 1537 | } |
| 1538 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1539 | if (torchStrength == NULL) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1540 | ALOGE("%s: strength level must not be null.", __FUNCTION__); |
| 1541 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Strength level should not be null."); |
| 1542 | } |
| 1543 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1544 | status_t res = mCameraProviderManager->getTorchStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1545 | if (res != OK) { |
| 1546 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve torch " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1547 | "strength level for device %s: %s (%d)", cameraId.c_str(), |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1548 | strerror(-res), res); |
| 1549 | } |
| 1550 | ALOGI("%s: Torch strength level is: %d", __FUNCTION__, *torchStrength); |
| 1551 | return Status::ok(); |
| 1552 | } |
| 1553 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1554 | std::string CameraService::getFormattedCurrentTime() { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1555 | time_t now = time(nullptr); |
| 1556 | char formattedTime[64]; |
| 1557 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1558 | return std::string(formattedTime); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1559 | } |
| 1560 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1561 | Status CameraService::getCameraVendorTagDescriptor( |
| 1562 | /*out*/ |
| 1563 | hardware::camera2::params::VendorTagDescriptor* desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1564 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1565 | if (!mInitialized) { |
| 1566 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1567 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 1568 | } |
Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 1569 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 1570 | if (globalDescriptor != nullptr) { |
| 1571 | *desc = *(globalDescriptor.get()); |
| 1572 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1573 | return Status::ok(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 1574 | } |
| 1575 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 1576 | Status CameraService::getCameraVendorTagCache( |
| 1577 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { |
| 1578 | ATRACE_CALL(); |
| 1579 | if (!mInitialized) { |
| 1580 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 1581 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1582 | "Camera subsystem not available"); |
| 1583 | } |
| 1584 | sp<VendorTagDescriptorCache> globalCache = |
| 1585 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 1586 | if (globalCache != nullptr) { |
| 1587 | *cache = *(globalCache.get()); |
| 1588 | } |
| 1589 | return Status::ok(); |
| 1590 | } |
| 1591 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 1592 | void CameraService::clearCachedVariables() { |
| 1593 | BasicClient::BasicClient::sCameraService = nullptr; |
| 1594 | } |
| 1595 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1596 | std::pair<int, IPCTransport> CameraService::getDeviceVersion(const std::string& cameraId, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1597 | bool overrideToPortrait, int* portraitRotation, int* facing, int* orientation) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1598 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1599 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1600 | int deviceVersion = 0; |
| 1601 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1602 | status_t res; |
| 1603 | hardware::hidl_version maxVersion{0,0}; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1604 | IPCTransport transport = IPCTransport::INVALID; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1605 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId, &maxVersion, &transport); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1606 | if (res != OK || transport == IPCTransport::INVALID) { |
| 1607 | 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] | 1608 | cameraId.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1609 | return std::make_pair(-1, IPCTransport::INVALID) ; |
| 1610 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1611 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1612 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1613 | hardware::CameraInfo info; |
| 1614 | if (facing) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1615 | res = mCameraProviderManager->getCameraInfo(cameraId, overrideToPortrait, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1616 | portraitRotation, &info); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1617 | if (res != OK) { |
| 1618 | return std::make_pair(-1, IPCTransport::INVALID); |
| 1619 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1620 | *facing = info.facing; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1621 | if (orientation) { |
| 1622 | *orientation = info.orientation; |
| 1623 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1624 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1625 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1626 | return std::make_pair(deviceVersion, transport); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1627 | } |
| 1628 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1629 | Status CameraService::filterGetInfoErrorCode(status_t err) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1630 | switch(err) { |
| 1631 | case NO_ERROR: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1632 | return Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1633 | case BAD_VALUE: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1634 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1635 | "CameraId is not valid for HAL module"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1636 | case NO_INIT: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1637 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1638 | "Camera device not available"); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1639 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1640 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1641 | "Camera HAL encountered error %d: %s", |
| 1642 | err, strerror(-err)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1643 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1646 | Status CameraService::makeClient(const sp<CameraService>& cameraService, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1647 | const sp<IInterface>& cameraCb, const std::string& packageName, bool systemNativeClient, |
| 1648 | const std::optional<std::string>& featureId, const std::string& cameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 1649 | int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid, |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1650 | int servicePid, std::pair<int, IPCTransport> deviceVersionAndTransport, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1651 | apiLevel effectiveApiLevel, bool overrideForPerfClass, bool overrideToPortrait, |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1652 | bool forceSlowJpegMode, const std::string& originalCameraId, |
| 1653 | /*out*/sp<BasicClient>* client) { |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1654 | // For HIDL devices |
| 1655 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 1656 | // Create CameraClient based on device version reported by the HAL. |
| 1657 | int deviceVersion = deviceVersionAndTransport.first; |
| 1658 | switch(deviceVersion) { |
| 1659 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 1660 | ALOGE("Camera using old HAL version: %d", deviceVersion); |
| 1661 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, |
| 1662 | "Camera device \"%s\" HAL version %d no longer supported", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1663 | cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1664 | break; |
| 1665 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 1666 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 1667 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 1668 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 1669 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 1670 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 1671 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 1672 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 1673 | break; |
| 1674 | default: |
| 1675 | // Should not be reachable |
| 1676 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 1677 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1678 | "Camera device \"%s\" has unknown HAL version %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1679 | cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1680 | } |
| 1681 | } |
| 1682 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 1683 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1684 | *client = new Camera2Client(cameraService, tmp, cameraService->mCameraServiceProxyWrapper, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1685 | cameraService->mAttributionAndPermissionUtils, packageName, featureId, cameraId, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1686 | api1CameraId, facing, sensorOrientation, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1687 | clientPid, clientUid, servicePid, overrideForPerfClass, overrideToPortrait, |
| 1688 | forceSlowJpegMode); |
| 1689 | ALOGI("%s: Camera1 API (legacy), override to portrait %d, forceSlowJpegMode %d", |
| 1690 | __FUNCTION__, overrideToPortrait, forceSlowJpegMode); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1691 | } else { // Camera2 API route |
| 1692 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = |
| 1693 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1694 | *client = new CameraDeviceClient(cameraService, tmp, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1695 | cameraService->mCameraServiceProxyWrapper, |
| 1696 | cameraService->mAttributionAndPermissionUtils, packageName, systemNativeClient, |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1697 | featureId, cameraId, facing, sensorOrientation, clientPid, clientUid, servicePid, |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1698 | overrideForPerfClass, overrideToPortrait, originalCameraId); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1699 | ALOGI("%s: Camera2 API, override to portrait %d", __FUNCTION__, overrideToPortrait); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1700 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1701 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1702 | } |
| 1703 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1704 | std::string CameraService::toString(std::set<userid_t> intSet) { |
| 1705 | std::ostringstream s; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1706 | bool first = true; |
| 1707 | for (userid_t i : intSet) { |
| 1708 | if (first) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1709 | s << std::to_string(i); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1710 | first = false; |
| 1711 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1712 | s << ", " << std::to_string(i); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1713 | } |
| 1714 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1715 | return std::move(s.str()); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1718 | int32_t CameraService::mapToInterface(TorchModeStatus status) { |
| 1719 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1720 | switch (status) { |
| 1721 | case TorchModeStatus::NOT_AVAILABLE: |
| 1722 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1723 | break; |
| 1724 | case TorchModeStatus::AVAILABLE_OFF: |
| 1725 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 1726 | break; |
| 1727 | case TorchModeStatus::AVAILABLE_ON: |
| 1728 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
| 1729 | break; |
| 1730 | default: |
| 1731 | ALOGW("Unknown new flash status: %d", status); |
| 1732 | } |
| 1733 | return serviceStatus; |
| 1734 | } |
| 1735 | |
| 1736 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { |
| 1737 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; |
| 1738 | switch (status) { |
| 1739 | case CameraDeviceStatus::NOT_PRESENT: |
| 1740 | serviceStatus = StatusInternal::NOT_PRESENT; |
| 1741 | break; |
| 1742 | case CameraDeviceStatus::PRESENT: |
| 1743 | serviceStatus = StatusInternal::PRESENT; |
| 1744 | break; |
| 1745 | case CameraDeviceStatus::ENUMERATING: |
| 1746 | serviceStatus = StatusInternal::ENUMERATING; |
| 1747 | break; |
| 1748 | default: |
| 1749 | ALOGW("Unknown new HAL device status: %d", status); |
| 1750 | } |
| 1751 | return serviceStatus; |
| 1752 | } |
| 1753 | |
| 1754 | int32_t CameraService::mapToInterface(StatusInternal status) { |
| 1755 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1756 | switch (status) { |
| 1757 | case StatusInternal::NOT_PRESENT: |
| 1758 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1759 | break; |
| 1760 | case StatusInternal::PRESENT: |
| 1761 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; |
| 1762 | break; |
| 1763 | case StatusInternal::ENUMERATING: |
| 1764 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; |
| 1765 | break; |
| 1766 | case StatusInternal::NOT_AVAILABLE: |
| 1767 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; |
| 1768 | break; |
| 1769 | case StatusInternal::UNKNOWN: |
| 1770 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; |
| 1771 | break; |
| 1772 | default: |
| 1773 | ALOGW("Unknown new internal device status: %d", status); |
| 1774 | } |
| 1775 | return serviceStatus; |
| 1776 | } |
| 1777 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1778 | Status CameraService::initializeShimMetadata(int cameraId) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1779 | int uid = getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1780 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1781 | std::string cameraIdStr = std::to_string(cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1782 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1783 | sp<Client> tmp = nullptr; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1784 | if (!(ret = connectHelper<ICameraClient,Client>( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1785 | sp<ICameraClient>{nullptr}, cameraIdStr, cameraId, |
| 1786 | kServiceName, /*systemNativeClient*/ false, {}, uid, USE_CALLING_PID, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1787 | API_1, /*shimUpdateOnly*/ true, /*oomScoreOffset*/ 0, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1788 | /*targetSdkVersion*/ __ANDROID_API_FUTURE__, /*overrideToPortrait*/ true, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1789 | /*forceSlowJpegMode*/false, cameraIdStr, /*out*/ tmp) |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1790 | ).isOk()) { |
Tomasz Wasilczyk | 12b04a5 | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 1791 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().c_str()); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1792 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1793 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1796 | Status CameraService::getLegacyParametersLazy(int cameraId, |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1797 | /*out*/ |
| 1798 | CameraParameters* parameters) { |
| 1799 | |
| 1800 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 1801 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1802 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1803 | |
| 1804 | if (parameters == NULL) { |
| 1805 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1806 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1809 | std::string unresolvedCameraId = std::to_string(cameraId); |
| 1810 | std::string cameraIdStr = resolveCameraId(unresolvedCameraId, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1811 | getCallingUid()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1812 | |
| 1813 | // Check if we already have parameters |
| 1814 | { |
| 1815 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1816 | Mutex::Autolock lock(mServiceLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1817 | auto cameraState = getCameraState(cameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1818 | if (cameraState == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1819 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, cameraIdStr.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1820 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1821 | "Invalid camera ID: %s", cameraIdStr.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1822 | } |
| 1823 | CameraParameters p = cameraState->getShimParams(); |
| 1824 | if (!p.isEmpty()) { |
| 1825 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1826 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1830 | int64_t token = clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1831 | ret = initializeShimMetadata(cameraId); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1832 | restoreCallingIdentity(token); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1833 | if (!ret.isOk()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1834 | // Error already logged by callee |
| 1835 | return ret; |
| 1836 | } |
| 1837 | |
| 1838 | // Check for parameters again |
| 1839 | { |
| 1840 | // Scope for service lock |
| 1841 | Mutex::Autolock lock(mServiceLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1842 | auto cameraState = getCameraState(cameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1843 | if (cameraState == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1844 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, cameraIdStr.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1845 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1846 | "Invalid camera ID: %s", cameraIdStr.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1847 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1848 | CameraParameters p = cameraState->getShimParams(); |
| 1849 | if (!p.isEmpty()) { |
| 1850 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1851 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1852 | } |
| 1853 | } |
| 1854 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1855 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 1856 | __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1857 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1860 | Status CameraService::validateConnectLocked(const std::string& cameraId, |
| 1861 | const std::string& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1862 | /*out*/int& originalClientPid) const { |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1863 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1864 | #ifdef __BRILLO__ |
| 1865 | UNUSED(clientName8); |
| 1866 | UNUSED(clientUid); |
| 1867 | UNUSED(clientPid); |
| 1868 | UNUSED(originalClientPid); |
| 1869 | #else |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1870 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, |
| 1871 | originalClientPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1872 | if (!allowed.isOk()) { |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1873 | return allowed; |
| 1874 | } |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1875 | #endif // __BRILLO__ |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1876 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1877 | int callingPid = getCallingPid(); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1878 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1879 | if (!mInitialized) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1880 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 1881 | callingPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1882 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1883 | "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] | 1884 | } |
| 1885 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1886 | if (getCameraState(cameraId) == nullptr) { |
| 1887 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1888 | cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1889 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1890 | "No camera device with ID \"%s\" available", cameraId.c_str()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1893 | status_t err = checkIfDeviceIsUsable(cameraId); |
| 1894 | if (err != NO_ERROR) { |
| 1895 | switch(err) { |
| 1896 | case -ENODEV: |
| 1897 | case -EBUSY: |
| 1898 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1899 | "No camera device with ID \"%s\" currently available", cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1900 | default: |
| 1901 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1902 | "Unknown error connecting to ID \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1903 | } |
| 1904 | } |
| 1905 | return Status::ok(); |
Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1906 | } |
| 1907 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1908 | Status CameraService::validateClientPermissionsLocked(const std::string& cameraId, |
| 1909 | const std::string& clientName, int& clientUid, int& clientPid, |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1910 | /*out*/int& originalClientPid) const { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1911 | int callingPid = getCallingPid(); |
| 1912 | int callingUid = getCallingUid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1913 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1914 | // Check if we can trust clientUid |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1915 | if (clientUid == USE_CALLING_UID) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1916 | clientUid = callingUid; |
| 1917 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1918 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1919 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1920 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1921 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1922 | "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] | 1923 | callingPid, callingUid, cameraId.c_str(), |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1924 | clientName.c_str(), clientPid, clientUid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1925 | } |
| 1926 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1927 | // Check if we can trust clientPid |
| 1928 | if (clientPid == USE_CALLING_PID) { |
| 1929 | clientPid = callingPid; |
| 1930 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1931 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1932 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1933 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1934 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1935 | "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] | 1936 | callingPid, callingUid, cameraId.c_str(), |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1937 | clientName.c_str(), clientPid, clientUid); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1938 | } |
| 1939 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1940 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1941 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", |
| 1942 | cameraId.c_str()); |
| 1943 | 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] | 1944 | "available", cameraId.c_str()); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1945 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1946 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1947 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1948 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1949 | 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] | 1950 | "found while trying to query device kind", cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1951 | |
| 1952 | } |
| 1953 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1954 | // If it's not calling from cameraserver, check the permission if the |
| 1955 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for |
| 1956 | // android.permission.SYSTEM_CAMERA for system only camera devices). |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1957 | bool checkPermissionForCamera = |
| 1958 | hasPermissionsForCamera(cameraId, clientPid, clientUid, clientName); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1959 | if (callingPid != getpid() && |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1960 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && !checkPermissionForCamera) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1961 | 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] | 1962 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1963 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission", |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1964 | clientName.c_str(), clientPid, clientUid, cameraId.c_str()); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1965 | } |
| 1966 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1967 | // 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] | 1968 | if (!mUidPolicy->isUidActive(callingUid, clientName)) { |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1969 | int32_t procState = mUidPolicy->getProcState(callingUid); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1970 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", |
| 1971 | clientPid, clientUid); |
| 1972 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1973 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" |
| 1974 | "calling UID %d proc state %" PRId32 ")", |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1975 | clientName.c_str(), clientPid, clientUid, cameraId.c_str(), |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1976 | callingUid, procState); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1977 | } |
| 1978 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1979 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use cases |
| 1980 | // such as rear view and surround view cannot be disabled and are exempt from sensor privacy |
| 1981 | // policy. In all other cases,if sensor privacy is enabled then prevent access to the camera. |
| 1982 | if ((!isAutomotivePrivilegedClient(callingUid) || |
| 1983 | !isAutomotiveExteriorSystemCamera(cameraId)) && |
| 1984 | mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1985 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); |
| 1986 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1987 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" when sensor privacy " |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1988 | "is enabled", clientName.c_str(), clientPid, clientUid, cameraId.c_str()); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1989 | } |
| 1990 | |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1991 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's |
| 1992 | // connected to camera service directly. |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1993 | originalClientPid = clientPid; |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1994 | clientPid = callingPid; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1995 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1996 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1997 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1998 | // For non-system clients : Only allow clients who are being used by the current foreground |
| 1999 | // device user, unless calling from our own process. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2000 | if (!callerHasSystemUid() && callingPid != getpid() && |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 2001 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2002 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 2003 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2004 | toString(mAllowedUsers).c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2005 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2006 | "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] | 2007 | clientUserId, cameraId.c_str()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2008 | } |
| 2009 | |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 2010 | if (flags::camera_hsum_permission()) { |
| 2011 | // If the System User tries to access the camera when the device is running in |
| 2012 | // headless system user mode, ensure that client has the required permission |
| 2013 | // CAMERA_HEADLESS_SYSTEM_USER. |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2014 | if (isHeadlessSystemUserMode() |
| 2015 | && (clientUserId == USER_SYSTEM) |
| 2016 | && !hasPermissionsForCameraHeadlessSystemUser(cameraId, callingPid, callingUid)) { |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 2017 | ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid); |
| 2018 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2019 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" as Headless System \ |
| 2020 | User without camera headless system user permission", |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2021 | clientName.c_str(), clientPid, clientUid, cameraId.c_str()); |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 2022 | } |
Jyoti Bhayana | 5bdb5a6 | 2023-08-24 14:46:08 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2025 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2026 | } |
| 2027 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2028 | status_t CameraService::checkIfDeviceIsUsable(const std::string& cameraId) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2029 | auto cameraState = getCameraState(cameraId); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2030 | int callingPid = getCallingPid(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2031 | if (cameraState == nullptr) { |
| 2032 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2033 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2034 | return -ENODEV; |
| 2035 | } |
| 2036 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2037 | StatusInternal currentStatus = cameraState->getStatus(); |
| 2038 | if (currentStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2039 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2040 | callingPid, cameraId.c_str()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 2041 | return -ENODEV; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2042 | } else if (currentStatus == StatusInternal::ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2043 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2044 | callingPid, cameraId.c_str()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 2045 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2046 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2047 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2048 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2049 | } |
| 2050 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2051 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2052 | const CameraService::DescriptorPtr& desc, int oomScoreOffset, bool systemNativeClient) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2053 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2054 | // Make a descriptor for the incoming client |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2055 | auto clientDescriptor = |
| 2056 | CameraService::CameraClientManager::makeClientDescriptor(client, desc, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2057 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2058 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 2059 | |
| 2060 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2061 | client->getPackageName()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2062 | |
| 2063 | if (evicted.size() > 0) { |
| 2064 | // This should never happen - clients should already have been removed in disconnect |
| 2065 | for (auto& i : evicted) { |
| 2066 | 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] | 2067 | __FUNCTION__, i->getKey().c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 2071 | __FUNCTION__); |
| 2072 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 2073 | |
| 2074 | // And register a death notification for the client callback. Do |
| 2075 | // this last to avoid Binder policy where a nested Binder |
| 2076 | // transaction might be pre-empted to service the client death |
| 2077 | // notification if the client process dies before linkToDeath is |
| 2078 | // invoked. |
| 2079 | sp<IBinder> remoteCallback = client->getRemote(); |
| 2080 | if (remoteCallback != nullptr) { |
| 2081 | remoteCallback->linkToDeath(this); |
| 2082 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2083 | } |
| 2084 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2085 | status_t CameraService::handleEvictionsLocked(const std::string& cameraId, int clientPid, |
| 2086 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, |
| 2087 | const std::string& packageName, int oomScoreOffset, bool systemNativeClient, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2088 | /*out*/ |
| 2089 | sp<BasicClient>* client, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2090 | std::shared_ptr<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2091 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2092 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2093 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2094 | DescriptorPtr clientDescriptor; |
| 2095 | { |
| 2096 | if (effectiveApiLevel == API_1) { |
| 2097 | // If we are using API1, any existing client for this camera ID with the same remote |
| 2098 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 2099 | |
| 2100 | auto current = mActiveClientManager.get(cameraId); |
| 2101 | if (current != nullptr) { |
| 2102 | auto clientSp = current->getValue(); |
| 2103 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2104 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
Shuzhen Wang | b2d43f6 | 2021-08-25 14:01:11 -0700 | [diff] [blame] | 2105 | ALOGW("CameraService connect called with a different" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2106 | " API level, evicting prior client..."); |
| 2107 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2108 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2109 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2110 | *client = clientSp; |
| 2111 | return NO_ERROR; |
| 2112 | } |
| 2113 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 2114 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 2115 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 2116 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2117 | // Get state for the given cameraId |
| 2118 | auto state = getCameraState(cameraId); |
| 2119 | if (state == nullptr) { |
| 2120 | 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] | 2121 | clientPid, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2122 | // Should never get here because validateConnectLocked should have errored out |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2123 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2124 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2125 | |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 2126 | sp<IServiceManager> sm = defaultServiceManager(); |
| 2127 | sp<IBinder> binder = sm->checkService(String16(kProcessInfoServiceName)); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2128 | if (!binder && isAutomotivePrivilegedClient(getCallingUid())) { |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 2129 | // If processinfo service is not available and the client is automotive privileged |
| 2130 | // client used for safety critical uses cases such as rear-view and surround-view which |
| 2131 | // needs to be available before android boot completes, then use the hardcoded values |
| 2132 | // for the process state and priority score. As this scenario is before android system |
| 2133 | // services are up and client is native client, hence using NATIVE_ADJ as the priority |
| 2134 | // score and state as PROCESS_STATE_BOUND_TOP as such automotive apps need to be |
| 2135 | // visible on the top. |
| 2136 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 2137 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 2138 | state->getConflicting(), resource_policy::NATIVE_ADJ, clientPid, |
| 2139 | ActivityManager::PROCESS_STATE_BOUND_TOP, oomScoreOffset, systemNativeClient); |
| 2140 | } else { |
| 2141 | // Get current active client PIDs |
| 2142 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 2143 | ownerPids.push_back(clientPid); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2144 | |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 2145 | std::vector<int> priorityScores(ownerPids.size()); |
| 2146 | std::vector<int> states(ownerPids.size()); |
| 2147 | |
| 2148 | // Get priority scores of all active PIDs |
| 2149 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids(ownerPids.size(), |
| 2150 | &ownerPids[0], /*out*/&states[0], /*out*/&priorityScores[0]); |
| 2151 | if (err != OK) { |
| 2152 | ALOGE("%s: Priority score query failed: %d", __FUNCTION__, err); |
| 2153 | return err; |
| 2154 | } |
| 2155 | |
| 2156 | // Update all active clients' priorities |
| 2157 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; |
| 2158 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
| 2159 | pidToPriorityMap.emplace(ownerPids[i], |
| 2160 | resource_policy::ClientPriority(priorityScores[i], states[i], |
| 2161 | /* isVendorClient won't get copied over*/ false, |
| 2162 | /* oomScoreOffset won't get copied over*/ 0)); |
| 2163 | } |
| 2164 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
| 2165 | |
| 2166 | int32_t actualScore = priorityScores[priorityScores.size() - 1]; |
| 2167 | int32_t actualState = states[states.size() - 1]; |
| 2168 | |
| 2169 | // Make descriptor for incoming client. We store the oomScoreOffset |
| 2170 | // since we might need it later on new handleEvictionsLocked and |
| 2171 | // ProcessInfoService would not take that into account. |
| 2172 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 2173 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 2174 | state->getConflicting(), actualScore, clientPid, actualState, |
| 2175 | oomScoreOffset, systemNativeClient); |
| 2176 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2177 | |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2178 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); |
| 2179 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2180 | // Find clients that would be evicted |
| 2181 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 2182 | |
| 2183 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 2184 | // background, so we cannot do evictions |
| 2185 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 2186 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 2187 | " priority).", clientPid); |
| 2188 | |
| 2189 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2190 | std::string curTime = getFormattedCurrentTime(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2191 | auto incompatibleClients = |
| 2192 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 2193 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2194 | std::string msg = fmt::sprintf("%s : DENIED connect device %s client for package %s " |
| 2195 | "(PID %d, score %d state %d) due to eviction policy", curTime.c_str(), |
| 2196 | cameraId.c_str(), packageName.c_str(), clientPid, |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2197 | clientPriority.getScore(), clientPriority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2198 | |
| 2199 | for (auto& i : incompatibleClients) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2200 | 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] | 2201 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2202 | i->getKey().c_str(), |
| 2203 | i->getValue()->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2204 | i->getOwnerId(), i->getPriority().getScore(), |
| 2205 | i->getPriority().getState()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 2206 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2207 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().c_str(), |
| 2208 | i->getValue()->getPackageName().c_str(), i->getOwnerId(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2209 | i->getPriority().getScore(), i->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2213 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2214 | mEventLog.add(msg); |
| 2215 | |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 2216 | auto current = mActiveClientManager.get(cameraId); |
| 2217 | if (current != nullptr) { |
| 2218 | return -EBUSY; // CAMERA_IN_USE |
| 2219 | } else { |
| 2220 | return -EUSERS; // MAX_CAMERAS_IN_USE |
| 2221 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | for (auto& i : evicted) { |
| 2225 | sp<BasicClient> clientSp = i->getValue(); |
| 2226 | if (clientSp.get() == nullptr) { |
| 2227 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 2228 | |
| 2229 | // TODO: Remove this |
| 2230 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 2231 | __FUNCTION__); |
| 2232 | mActiveClientManager.remove(i); |
| 2233 | continue; |
| 2234 | } |
| 2235 | |
| 2236 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2237 | i->getKey().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2238 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2239 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2240 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2241 | logEvent(fmt::sprintf("EVICT device %s client held by package %s (PID" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2242 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" |
| 2243 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2244 | i->getKey().c_str(), clientSp->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2245 | i->getOwnerId(), i->getPriority().getScore(), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2246 | i->getPriority().getState(), cameraId.c_str(), |
| 2247 | packageName.c_str(), clientPid, clientPriority.getScore(), |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2248 | clientPriority.getState())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2249 | |
| 2250 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2251 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2252 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2253 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2256 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 2257 | // other clients from connecting in mServiceLockWrapper if held |
| 2258 | mServiceLock.unlock(); |
| 2259 | |
| 2260 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2261 | int64_t token = clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2262 | |
| 2263 | // Destroy evicted clients |
| 2264 | for (auto& i : evictedClients) { |
| 2265 | // 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] | 2266 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2267 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2268 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2269 | restoreCallingIdentity(token); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2270 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2271 | for (const auto& i : evictedClients) { |
| 2272 | 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] | 2273 | __FUNCTION__, i->getKey().c_str(), i->getOwnerId()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2274 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 2275 | if (ret == TIMED_OUT) { |
| 2276 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2277 | "current clients:\n%s", __FUNCTION__, i->getKey().c_str(), |
| 2278 | mActiveClientManager.toString().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2279 | return -EBUSY; |
| 2280 | } |
| 2281 | if (ret != NO_ERROR) { |
| 2282 | 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] | 2283 | "current clients:\n%s", __FUNCTION__, i->getKey().c_str(), strerror(-ret), |
| 2284 | ret, mActiveClientManager.toString().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2285 | return ret; |
| 2286 | } |
| 2287 | } |
| 2288 | |
| 2289 | evictedClients.clear(); |
| 2290 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2291 | // Once clients have been disconnected, relock |
| 2292 | mServiceLock.lock(); |
| 2293 | |
| 2294 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 2295 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 2296 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2297 | } |
| 2298 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2299 | *partial = clientDescriptor; |
| 2300 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2301 | } |
| 2302 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2303 | Status CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2304 | const sp<ICameraClient>& cameraClient, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2305 | int api1CameraId, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2306 | const std::string& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 2307 | int clientUid, |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 2308 | int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2309 | int targetSdkVersion, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2310 | bool overrideToPortrait, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2311 | bool forceSlowJpegMode, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2312 | int32_t deviceId, |
| 2313 | int32_t devicePolicy, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 2314 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2315 | sp<ICamera>* device) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2316 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2317 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2318 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2319 | std::string cameraIdStr = cameraIdIntToStr(api1CameraId, deviceId, devicePolicy); |
| 2320 | if (cameraIdStr.empty()) { |
| 2321 | std::string msg = fmt::sprintf("Camera %d: Invalid camera id for device id %d", |
| 2322 | api1CameraId, deviceId); |
| 2323 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2324 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 2325 | } |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 2326 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2327 | sp<Client> client = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2328 | ret = connectHelper<ICameraClient,Client>(cameraClient, cameraIdStr, api1CameraId, |
| 2329 | clientPackageName, /*systemNativeClient*/ false, {}, clientUid, clientPid, API_1, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2330 | /*shimUpdateOnly*/ false, /*oomScoreOffset*/ 0, targetSdkVersion, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2331 | overrideToPortrait, forceSlowJpegMode, cameraIdStr, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2332 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2333 | if (!ret.isOk()) { |
| 2334 | logRejected(cameraIdStr, getCallingPid(), clientPackageName, toStdString(ret.toString8())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2335 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2336 | } |
| 2337 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2338 | *device = client; |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2339 | |
| 2340 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 2341 | const auto& mActivityManager = getActivityManager(); |
| 2342 | if (mActivityManager) { |
| 2343 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2344 | getCallingUid(), |
| 2345 | getCallingPid()); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2348 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2349 | } |
| 2350 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2351 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, |
| 2352 | bool isVendorListener, int clientPid, int clientUid) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2353 | // If the client is not a vendor client, don't add listener if |
| 2354 | // a) the camera is a publicly hidden secure camera OR |
| 2355 | // b) the camera is a system only camera and the client doesn't |
| 2356 | // have android.permission.SYSTEM_CAMERA permissions. |
| 2357 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || |
| 2358 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2359 | !hasPermissionsForSystemCamera(std::string(), clientPid, clientUid)))) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2360 | return true; |
| 2361 | } |
| 2362 | return false; |
| 2363 | } |
| 2364 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2365 | bool CameraService::shouldRejectSystemCameraConnection(const std::string& cameraId) const { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2366 | // Rules for rejection: |
| 2367 | // 1) If cameraserver tries to access this camera device, accept the |
| 2368 | // connection. |
| 2369 | // 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] | 2370 | // non system component is trying to access it. |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2371 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY |
| 2372 | // and the serving thread is a non hwbinder thread, the client must have |
| 2373 | // android.permission.SYSTEM_CAMERA permissions to connect. |
| 2374 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2375 | int cPid = getCallingPid(); |
| 2376 | int cUid = getCallingUid(); |
| 2377 | bool systemClient = callerHasSystemUid(); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2378 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 2379 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 2380 | // This isn't a known camera ID, so it's not a system camera |
| 2381 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); |
| 2382 | return false; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2383 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2384 | |
| 2385 | // (1) Cameraserver trying to connect, accept. |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2386 | if (isCallerCameraServerNotDelegating()) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2387 | return false; |
| 2388 | } |
| 2389 | // (2) |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2390 | if (!systemClient && systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2391 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); |
| 2392 | return true; |
| 2393 | } |
| 2394 | // (3) Here we only check for permissions if it is a system only camera device. This is since |
| 2395 | // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some |
| 2396 | // characteristics) even if clients don't have android.permission.CAMERA. We do not want the |
| 2397 | // same behavior for system camera devices. |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2398 | if (!systemClient && systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2399 | !hasPermissionsForSystemCamera(cameraId, cPid, cUid)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2400 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", |
| 2401 | cameraId.c_str()); |
| 2402 | return true; |
| 2403 | } |
| 2404 | |
| 2405 | return false; |
| 2406 | } |
| 2407 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2408 | Status CameraService::connectDevice( |
| 2409 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2410 | const std::string& unresolvedCameraId, |
| 2411 | const std::string& clientPackageName, |
| 2412 | const std::optional<std::string>& clientFeatureId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2413 | int clientUid, int oomScoreOffset, int targetSdkVersion, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2414 | bool overrideToPortrait, int32_t deviceId, int32_t devicePolicy, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2415 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2416 | sp<hardware::camera2::ICameraDeviceUser>* device) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2417 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2418 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2419 | sp<CameraDeviceClient> client = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2420 | std::string clientPackageNameAdj = clientPackageName; |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2421 | int callingPid = getCallingPid(); |
| 2422 | int callingUid = getCallingUid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2423 | bool systemNativeClient = false; |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2424 | if (callerHasSystemUid() && (clientPackageNameAdj.size() == 0)) { |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2425 | std::string systemClient = fmt::sprintf("client.pid<%d>", callingPid); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2426 | clientPackageNameAdj = systemClient; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2427 | systemNativeClient = true; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2428 | } |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2429 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2430 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
| 2431 | devicePolicy, callingUid, clientPackageNameAdj); |
| 2432 | if (!cameraIdOptional.has_value()) { |
| 2433 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 2434 | unresolvedCameraId.c_str(), deviceId); |
| 2435 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2436 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 2437 | } |
| 2438 | std::string cameraId = cameraIdOptional.value(); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2439 | |
| 2440 | if (oomScoreOffset < 0) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2441 | std::string msg = |
| 2442 | fmt::sprintf("Cannot increase the priority of a client %s pid %d for " |
| 2443 | "camera id %s", clientPackageNameAdj.c_str(), callingPid, |
| 2444 | cameraId.c_str()); |
| 2445 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2446 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 2449 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 2450 | if (clientUid == USE_CALLING_UID) { |
| 2451 | clientUserId = multiuser_get_user_id(callingUid); |
| 2452 | } |
| 2453 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2454 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use cases |
| 2455 | // such as rear view and surround view cannot be disabled. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2456 | if ((!isAutomotivePrivilegedClient(callingUid) || !isAutomotiveExteriorSystemCamera(cameraId)) |
| 2457 | && mCameraServiceProxyWrapper->isCameraDisabled(clientUserId)) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2458 | std::string msg = "Camera disabled by device policy"; |
| 2459 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2460 | return STATUS_ERROR(ERROR_DISABLED, msg.c_str()); |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 2461 | } |
| 2462 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2463 | // enforce system camera permissions |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2464 | if (oomScoreOffset > 0 |
| 2465 | && !hasPermissionsForSystemCamera(cameraId, callingPid, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2466 | callingUid) |
| 2467 | && !isTrustedCallingUid(callingUid)) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2468 | 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] | 2469 | "camera id %s without SYSTEM_CAMERA permissions", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2470 | clientPackageNameAdj.c_str(), callingPid, cameraId.c_str()); |
| 2471 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2472 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, msg.c_str()); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2475 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, |
| 2476 | cameraId, /*api1CameraId*/-1, clientPackageNameAdj, systemNativeClient, clientFeatureId, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2477 | clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, oomScoreOffset, |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 2478 | targetSdkVersion, overrideToPortrait, /*forceSlowJpegMode*/false, unresolvedCameraId, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2479 | /*out*/client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2480 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2481 | if (!ret.isOk()) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2482 | logRejected(cameraId, callingPid, clientPackageNameAdj, toStdString(ret.toString8())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2483 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2484 | } |
| 2485 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2486 | *device = client; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 2487 | Mutex::Autolock lock(mServiceLock); |
| 2488 | |
| 2489 | // Clear the previous cached logs and reposition the |
| 2490 | // file offset to beginning of the file to log new data. |
| 2491 | // If either truncate or lseek fails, close the previous file and create a new one. |
| 2492 | if ((ftruncate(mMemFd, 0) == -1) || (lseek(mMemFd, 0, SEEK_SET) == -1)) { |
| 2493 | ALOGE("%s: Error while truncating the file: %s", __FUNCTION__, sFileName); |
| 2494 | // Close the previous memfd. |
| 2495 | close(mMemFd); |
| 2496 | // If failure to wipe the data, then create a new file and |
| 2497 | // assign the new value to mMemFd. |
| 2498 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 2499 | if (mMemFd == -1) { |
| 2500 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 2501 | } |
| 2502 | } |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2503 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 2504 | const auto& mActivityManager = getActivityManager(); |
| 2505 | if (mActivityManager) { |
| 2506 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2507 | callingUid, |
| 2508 | callingPid); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2509 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2510 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2511 | } |
| 2512 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2513 | bool CameraService::isCameraPrivacyEnabled(const String16& packageName, const std::string& cam_id, |
| 2514 | int callingPid, int callingUid) { |
| 2515 | if (!isAutomotiveDevice()) { |
| 2516 | return mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
| 2517 | } |
| 2518 | |
| 2519 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for |
| 2520 | // safety-critical use cases cannot be disabled and are exempt from camera privacy policy. |
| 2521 | if ((isAutomotivePrivilegedClient(callingUid) && isAutomotiveExteriorSystemCamera(cam_id))) { |
| 2522 | ALOGI("Camera privacy cannot be enabled for automotive privileged client %d " |
| 2523 | "using camera %s", callingUid, cam_id.c_str()); |
| 2524 | return false; |
| 2525 | } |
| 2526 | |
| 2527 | if (mSensorPrivacyPolicy->isCameraPrivacyEnabled(packageName)) { |
| 2528 | return true; |
| 2529 | } else if (mSensorPrivacyPolicy->getCameraPrivacyState() == SensorPrivacyManager::DISABLED) { |
| 2530 | return false; |
Jyoti Bhayana | 54a4b00 | 2024-02-27 15:36:09 -0800 | [diff] [blame] | 2531 | } else if (mSensorPrivacyPolicy->getCameraPrivacyState() |
| 2532 | == SensorPrivacyManager::ENABLED_EXCEPT_ALLOWLISTED_APPS) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2533 | if (hasPermissionsForCameraPrivacyAllowlist(callingPid, callingUid)) { |
| 2534 | return false; |
| 2535 | } else { |
| 2536 | return true; |
| 2537 | } |
| 2538 | } |
| 2539 | return false; |
| 2540 | } |
| 2541 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2542 | std::string CameraService::getPackageNameFromUid(int clientUid) { |
| 2543 | std::string packageName(""); |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2544 | |
Avichal Rakesh | 5788fec | 2024-03-15 14:39:20 -0700 | [diff] [blame] | 2545 | sp<IPermissionController> permCtrl; |
| 2546 | if (flags::cache_permission_services()) { |
| 2547 | permCtrl = getPermissionController(); |
| 2548 | } else { |
| 2549 | sp<IServiceManager> sm = defaultServiceManager(); |
| 2550 | #pragma clang diagnostic push |
| 2551 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 2552 | // Using deprecated function to preserve functionality until the |
| 2553 | // cache_permission_services flag is removed. |
| 2554 | sp<IBinder> binder = sm->getService(toString16(kPermissionServiceName)); |
| 2555 | #pragma clang diagnostic pop |
| 2556 | if (binder == 0) { |
| 2557 | ALOGE("Cannot get permission service"); |
| 2558 | permCtrl = nullptr; |
| 2559 | } else { |
| 2560 | permCtrl = interface_cast<IPermissionController>(binder); |
| 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | if (permCtrl == nullptr) { |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2565 | // Return empty package name and the further interaction |
| 2566 | // with camera will likely fail |
| 2567 | return packageName; |
| 2568 | } |
| 2569 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2570 | Vector<String16> packages; |
| 2571 | |
| 2572 | permCtrl->getPackagesForUid(clientUid, packages); |
| 2573 | |
| 2574 | if (packages.isEmpty()) { |
| 2575 | ALOGE("No packages for calling UID %d", clientUid); |
| 2576 | // Return empty package name and the further interaction |
| 2577 | // with camera will likely fail |
| 2578 | return packageName; |
| 2579 | } |
| 2580 | |
| 2581 | // Arbitrarily pick the first name in the list |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2582 | packageName = toStdString(packages[0]); |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2583 | |
| 2584 | return packageName; |
| 2585 | } |
| 2586 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2587 | template<class CALLBACK, class CLIENT> |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2588 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const std::string& cameraId, |
| 2589 | int api1CameraId, const std::string& clientPackageNameMaybe, bool systemNativeClient, |
| 2590 | const std::optional<std::string>& clientFeatureId, int clientUid, int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2591 | apiLevel effectiveApiLevel, bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2592 | bool overrideToPortrait, bool forceSlowJpegMode, const std::string& originalCameraId, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2593 | /*out*/sp<CLIENT>& device) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2594 | binder::Status ret = binder::Status::ok(); |
| 2595 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2596 | bool isNonSystemNdk = false; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2597 | std::string clientPackageName; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2598 | int packageUid = (clientUid == USE_CALLING_UID) ? |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2599 | getCallingUid() : clientUid; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2600 | if (clientPackageNameMaybe.size() <= 0) { |
| 2601 | // NDK calls don't come with package names, but we need one for various cases. |
| 2602 | // Generally, there's a 1:1 mapping between UID and package name, but shared UIDs |
| 2603 | // do exist. For all authentication cases, all packages under the same UID get the |
| 2604 | // same permissions, so picking any associated package name is sufficient. For some |
| 2605 | // other cases, this may give inaccurate names for clients in logs. |
| 2606 | isNonSystemNdk = true; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2607 | clientPackageName = getPackageNameFromUid(packageUid); |
| 2608 | } else { |
| 2609 | clientPackageName = clientPackageNameMaybe; |
| 2610 | } |
| 2611 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2612 | int originalClientPid = 0; |
| 2613 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2614 | int packagePid = (clientPid == USE_CALLING_PID) ? |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2615 | getCallingPid() : clientPid; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 2616 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2617 | "Camera API version %d", packagePid, clientPackageName.c_str(), cameraId.c_str(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2618 | static_cast<int>(effectiveApiLevel)); |
| 2619 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2620 | nsecs_t openTimeNs = systemTime(); |
| 2621 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2622 | sp<CLIENT> client = nullptr; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2623 | int facing = -1; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 2624 | int orientation = 0; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2625 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2626 | { |
| 2627 | // Acquire mServiceLock and prevent other clients from connecting |
| 2628 | std::unique_ptr<AutoConditionLock> lock = |
| 2629 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2630 | |
| 2631 | if (lock == nullptr) { |
| 2632 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." |
| 2633 | , clientPid); |
| 2634 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2635 | "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] | 2636 | cameraId.c_str(), clientPackageName.c_str(), clientPid); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2637 | } |
| 2638 | |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 2639 | // Enforce client permissions and do basic validity checks |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2640 | if (!(ret = validateConnectLocked(cameraId, clientPackageName, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2641 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { |
| 2642 | return ret; |
| 2643 | } |
| 2644 | |
| 2645 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 2646 | // we were doing a shim update, return immediately |
| 2647 | if (shimUpdateOnly) { |
| 2648 | auto cameraState = getCameraState(cameraId); |
| 2649 | if (cameraState != nullptr) { |
| 2650 | if (!cameraState->getShimParams().isEmpty()) return ret; |
| 2651 | } |
| 2652 | } |
| 2653 | |
| 2654 | status_t err; |
| 2655 | |
| 2656 | sp<BasicClient> clientTmp = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2657 | std::shared_ptr<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>> partial; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2658 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2659 | IInterface::asBinder(cameraCb), clientPackageName, oomScoreOffset, |
| 2660 | systemNativeClient, /*out*/&clientTmp, /*out*/&partial)) != NO_ERROR) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2661 | switch (err) { |
| 2662 | case -ENODEV: |
| 2663 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2664 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2665 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2666 | case -EBUSY: |
| 2667 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2668 | "Higher-priority client using camera, ID \"%s\" currently unavailable", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2669 | cameraId.c_str()); |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 2670 | case -EUSERS: |
| 2671 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2672 | "Too many cameras already open, cannot open camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2673 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2674 | default: |
| 2675 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2676 | "Unexpected error %s (%d) opening camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2677 | strerror(-err), err, cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | if (clientTmp.get() != nullptr) { |
| 2682 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 2683 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 2684 | return ret; |
| 2685 | } |
| 2686 | |
| 2687 | // give flashlight a chance to close devices if necessary. |
| 2688 | mFlashlight->prepareDeviceOpen(cameraId); |
| 2689 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2690 | int portraitRotation; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2691 | auto deviceVersionAndTransport = |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2692 | getDeviceVersion(cameraId, overrideToPortrait, /*out*/&portraitRotation, |
| 2693 | /*out*/&facing, /*out*/&orientation); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2694 | if (facing == -1) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2695 | 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] | 2696 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2697 | "Unable to get camera device \"%s\" facing", cameraId.c_str()); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2698 | } |
| 2699 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2700 | sp<BasicClient> tmp = nullptr; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2701 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2702 | mPerfClassPrimaryCameraIds, cameraId, targetSdkVersion); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2703 | if(!(ret = makeClient(this, cameraCb, clientPackageName, systemNativeClient, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2704 | clientFeatureId, cameraId, api1CameraId, facing, |
| 2705 | orientation, clientPid, clientUid, getpid(), |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2706 | deviceVersionAndTransport, effectiveApiLevel, overrideForPerfClass, |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 2707 | overrideToPortrait, forceSlowJpegMode, originalCameraId, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2708 | /*out*/&tmp)).isOk()) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2709 | return ret; |
| 2710 | } |
| 2711 | client = static_cast<CLIENT*>(tmp.get()); |
| 2712 | |
| 2713 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 2714 | __FUNCTION__); |
| 2715 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2716 | std::string monitorTags = isClientWatched(client.get()) ? mMonitorTags : std::string(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2717 | err = client->initialize(mCameraProviderManager, monitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2718 | if (err != OK) { |
| 2719 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2720 | // Errors could be from the HAL module open call or from AppOpsManager |
Kwangkyu Park | b1aaf9a | 2023-07-08 00:42:03 +0900 | [diff] [blame] | 2721 | mServiceLock.unlock(); |
| 2722 | client->disconnect(); |
| 2723 | mServiceLock.lock(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2724 | switch(err) { |
| 2725 | case BAD_VALUE: |
| 2726 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2727 | "Illegal argument to HAL module for camera \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2728 | case -EBUSY: |
| 2729 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2730 | "Camera \"%s\" is already open", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2731 | case -EUSERS: |
| 2732 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2733 | "Too many cameras already open, cannot open camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2734 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2735 | case PERMISSION_DENIED: |
| 2736 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2737 | "No permission to open camera \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2738 | case -EACCES: |
| 2739 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2740 | "Camera \"%s\" disabled by policy", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2741 | case -ENODEV: |
| 2742 | default: |
| 2743 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2744 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2745 | strerror(-err), err); |
| 2746 | } |
| 2747 | } |
| 2748 | |
| 2749 | // Update shim paremeters for legacy clients |
| 2750 | if (effectiveApiLevel == API_1) { |
| 2751 | // Assume we have always received a Client subclass for API1 |
| 2752 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 2753 | String8 rawParams = shimClient->getParameters(); |
| 2754 | CameraParameters params(rawParams); |
| 2755 | |
| 2756 | auto cameraState = getCameraState(cameraId); |
| 2757 | if (cameraState != nullptr) { |
| 2758 | cameraState->setShimParams(params); |
| 2759 | } else { |
| 2760 | 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] | 2761 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2762 | } |
| 2763 | } |
| 2764 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 2765 | // Enable/disable camera service watchdog |
| 2766 | client->setCameraServiceWatchdog(mCameraServiceWatchdogEnabled); |
| 2767 | |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2768 | CameraMetadata chars; |
| 2769 | bool rotateAndCropSupported = true; |
| 2770 | err = mCameraProviderManager->getCameraCharacteristics(cameraId, overrideForPerfClass, |
| 2771 | &chars, overrideToPortrait); |
| 2772 | if (err == OK) { |
| 2773 | auto availableRotateCropEntry = chars.find( |
| 2774 | ANDROID_SCALER_AVAILABLE_ROTATE_AND_CROP_MODES); |
| 2775 | if (availableRotateCropEntry.count <= 1) { |
| 2776 | rotateAndCropSupported = false; |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2777 | } |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame] | 2778 | } else { |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2779 | ALOGE("%s: Unable to query static metadata for camera %s: %s (%d)", __FUNCTION__, |
| 2780 | cameraId.c_str(), strerror(-err), err); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2783 | if (rotateAndCropSupported) { |
| 2784 | // Set rotate-and-crop override behavior |
| 2785 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { |
| 2786 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); |
| 2787 | } else if (overrideToPortrait && portraitRotation != 0) { |
| 2788 | uint8_t rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_AUTO; |
| 2789 | switch (portraitRotation) { |
| 2790 | case 90: |
| 2791 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_90; |
| 2792 | break; |
| 2793 | case 180: |
| 2794 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_180; |
| 2795 | break; |
| 2796 | case 270: |
| 2797 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_270; |
| 2798 | break; |
| 2799 | default: |
| 2800 | ALOGE("Unexpected portrait rotation: %d", portraitRotation); |
| 2801 | break; |
| 2802 | } |
| 2803 | client->setRotateAndCropOverride(rotateAndCropMode); |
| 2804 | } else { |
| 2805 | client->setRotateAndCropOverride( |
| 2806 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 2807 | clientPackageName, facing, multiuser_get_user_id(clientUid))); |
| 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | bool autoframingSupported = true; |
| 2812 | auto availableAutoframingEntry = chars.find(ANDROID_CONTROL_AUTOFRAMING_AVAILABLE); |
| 2813 | if ((availableAutoframingEntry.count == 1) && (availableAutoframingEntry.data.u8[0] == |
| 2814 | ANDROID_CONTROL_AUTOFRAMING_AVAILABLE_FALSE)) { |
| 2815 | autoframingSupported = false; |
| 2816 | } |
| 2817 | |
| 2818 | if (autoframingSupported) { |
| 2819 | // Set autoframing override behaviour |
| 2820 | if (mOverrideAutoframingMode != ANDROID_CONTROL_AUTOFRAMING_AUTO) { |
| 2821 | client->setAutoframingOverride(mOverrideAutoframingMode); |
| 2822 | } else { |
| 2823 | client->setAutoframingOverride( |
| 2824 | mCameraServiceProxyWrapper->getAutoframingOverride( |
| 2825 | clientPackageName)); |
| 2826 | } |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 2827 | } |
| 2828 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2829 | bool isCameraPrivacyEnabled; |
| 2830 | if (flags::camera_privacy_allowlist()) { |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2831 | // Set camera muting behavior. |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2832 | isCameraPrivacyEnabled = this->isCameraPrivacyEnabled( |
| 2833 | toString16(client->getPackageName()), cameraId, packagePid, packageUid); |
| 2834 | } else { |
| 2835 | isCameraPrivacyEnabled = |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2836 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2837 | } |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 2838 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2839 | if (client->supportsCameraMute()) { |
| 2840 | client->setCameraMute( |
| 2841 | mOverrideCameraMuteMode || isCameraPrivacyEnabled); |
| 2842 | } else if (isCameraPrivacyEnabled) { |
| 2843 | // no camera mute supported, but privacy is on! => disconnect |
| 2844 | ALOGI("Camera mute not supported for package: %s, camera id: %s", |
| 2845 | client->getPackageName().c_str(), cameraId.c_str()); |
| 2846 | // Do not hold mServiceLock while disconnecting clients, but |
| 2847 | // retain the condition blocking other clients from connecting |
| 2848 | // in mServiceLockWrapper if held. |
| 2849 | mServiceLock.unlock(); |
| 2850 | // Clear caller identity temporarily so client disconnect PID |
| 2851 | // checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2852 | int64_t token = clearCallingIdentity(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2853 | // Note AppOp to trigger the "Unblock" dialog |
| 2854 | client->noteAppOp(); |
| 2855 | client->disconnect(); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2856 | restoreCallingIdentity(token); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2857 | // Reacquire mServiceLock |
| 2858 | mServiceLock.lock(); |
| 2859 | |
| 2860 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 2861 | "Camera \"%s\" disabled due to camera mute", cameraId.c_str()); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2862 | } |
| 2863 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2864 | if (shimUpdateOnly) { |
| 2865 | // If only updating legacy shim parameters, immediately disconnect client |
| 2866 | mServiceLock.unlock(); |
| 2867 | client->disconnect(); |
| 2868 | mServiceLock.lock(); |
| 2869 | } else { |
| 2870 | // Otherwise, add client to active clients list |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2871 | finishConnectLocked(client, partial, oomScoreOffset, systemNativeClient); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2872 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 2873 | |
| 2874 | client->setImageDumpMask(mImageDumpMask); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 2875 | client->setStreamUseCaseOverrides(mStreamUseCaseOverrides); |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 2876 | client->setZoomOverride(mZoomOverrideValue); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2877 | } // lock is destroyed, allow further connect calls |
| 2878 | |
| 2879 | // Important: release the mutex here so the client can call back into the service from its |
| 2880 | // destructor (can be at the end of the call) |
| 2881 | device = client; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2882 | |
| 2883 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 2884 | mCameraServiceProxyWrapper->logOpen(cameraId, facing, clientPackageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2885 | effectiveApiLevel, isNonSystemNdk, openLatencyMs); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2886 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2887 | { |
| 2888 | Mutex::Autolock lock(mInjectionParametersLock); |
| 2889 | if (cameraId == mInjectionInternalCamId && mInjectionInitPending) { |
| 2890 | mInjectionInitPending = false; |
| 2891 | status_t res = NO_ERROR; |
| 2892 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 2893 | if (clientDescriptor != nullptr) { |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2894 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 2895 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 2896 | if(res != OK) { |
| 2897 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2898 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2899 | mInjectionExternalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2900 | } |
| 2901 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2902 | if (res != OK) { |
| 2903 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2904 | } |
| 2905 | } else { |
| 2906 | ALOGE("%s: Internal camera ID = %s 's client does not exist!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2907 | __FUNCTION__, mInjectionInternalCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2908 | res = NO_INIT; |
| 2909 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2910 | } |
| 2911 | } |
| 2912 | } |
| 2913 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2914 | return ret; |
| 2915 | } |
| 2916 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2917 | status_t CameraService::addOfflineClient(const std::string &cameraId, |
| 2918 | sp<BasicClient> offlineClient) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2919 | if (offlineClient.get() == nullptr) { |
| 2920 | return BAD_VALUE; |
| 2921 | } |
| 2922 | |
| 2923 | { |
| 2924 | // Acquire mServiceLock and prevent other clients from connecting |
| 2925 | std::unique_ptr<AutoConditionLock> lock = |
| 2926 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2927 | |
| 2928 | if (lock == nullptr) { |
| 2929 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." |
| 2930 | , __FUNCTION__, offlineClient->getClientPid()); |
| 2931 | return TIMED_OUT; |
| 2932 | } |
| 2933 | |
| 2934 | auto onlineClientDesc = mActiveClientManager.get(cameraId); |
| 2935 | if (onlineClientDesc.get() == nullptr) { |
| 2936 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, |
| 2937 | cameraId.c_str()); |
| 2938 | return BAD_VALUE; |
| 2939 | } |
| 2940 | |
| 2941 | // Offline clients do not evict or conflict with other online devices. Resource sharing |
| 2942 | // conflicts are handled by the camera provider which will either succeed or fail before |
| 2943 | // reaching this method. |
| 2944 | const auto& onlinePriority = onlineClientDesc->getPriority(); |
| 2945 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( |
| 2946 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2947 | /*conflictingKeys*/ std::set<std::string>(), onlinePriority.getScore(), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2948 | onlineClientDesc->getOwnerId(), onlinePriority.getState(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2949 | // native clients don't have offline processing support. |
| 2950 | /*ommScoreOffset*/ 0, /*systemNativeClient*/false); |
Guillaume Bailey | 417e43d | 2022-11-02 15:30:24 +0100 | [diff] [blame] | 2951 | if (offlineClientDesc == nullptr) { |
| 2952 | ALOGE("%s: Offline client descriptor was NULL", __FUNCTION__); |
| 2953 | return BAD_VALUE; |
| 2954 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2955 | |
| 2956 | // Allow only one offline device per camera |
| 2957 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); |
| 2958 | if (!incompatibleClients.empty()) { |
| 2959 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); |
| 2960 | return BAD_VALUE; |
| 2961 | } |
| 2962 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2963 | std::string monitorTags = isClientWatched(offlineClient.get()) |
| 2964 | ? mMonitorTags : std::string(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2965 | auto err = offlineClient->initialize(mCameraProviderManager, monitorTags); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2966 | if (err != OK) { |
| 2967 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); |
| 2968 | return err; |
| 2969 | } |
| 2970 | |
| 2971 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); |
| 2972 | if (evicted.size() > 0) { |
| 2973 | for (auto& i : evicted) { |
| 2974 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2975 | __FUNCTION__, i->getKey().c_str()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " |
| 2979 | "properly", __FUNCTION__); |
| 2980 | |
| 2981 | return BAD_VALUE; |
| 2982 | } |
| 2983 | |
| 2984 | logConnectedOffline(offlineClientDesc->getKey(), |
| 2985 | static_cast<int>(offlineClientDesc->getOwnerId()), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2986 | offlineClient->getPackageName()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2987 | |
| 2988 | sp<IBinder> remoteCallback = offlineClient->getRemote(); |
| 2989 | if (remoteCallback != nullptr) { |
| 2990 | remoteCallback->linkToDeath(this); |
| 2991 | } |
| 2992 | } // lock is destroyed, allow further connect calls |
| 2993 | |
| 2994 | return OK; |
| 2995 | } |
| 2996 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2997 | Status CameraService::turnOnTorchWithStrengthLevel(const std::string& unresolvedCameraId, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2998 | int32_t torchStrength, const sp<IBinder>& clientBinder, int32_t deviceId, |
| 2999 | int32_t devicePolicy) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3000 | Mutex::Autolock lock(mServiceLock); |
| 3001 | |
| 3002 | ATRACE_CALL(); |
| 3003 | if (clientBinder == nullptr) { |
| 3004 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
| 3005 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 3006 | "Torch client binder in null."); |
| 3007 | } |
| 3008 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3009 | int uid = getCallingUid(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3010 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
| 3011 | devicePolicy, uid); |
| 3012 | if (!cameraIdOptional.has_value()) { |
| 3013 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 3014 | unresolvedCameraId.c_str(), deviceId); |
| 3015 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3016 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 3017 | } |
| 3018 | std::string cameraId = cameraIdOptional.value(); |
| 3019 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3020 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3021 | 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] | 3022 | "for system only device %s: ", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | // verify id is valid |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3026 | auto state = getCameraState(cameraId); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3027 | if (state == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3028 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3029 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3030 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | StatusInternal cameraStatus = state->getStatus(); |
| 3034 | if (cameraStatus != StatusInternal::NOT_AVAILABLE && |
| 3035 | cameraStatus != StatusInternal::PRESENT) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3036 | 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] | 3037 | (int)cameraStatus); |
| 3038 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3039 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | { |
| 3043 | Mutex::Autolock al(mTorchStatusMutex); |
| 3044 | TorchModeStatus status; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3045 | status_t err = getTorchStatusLocked(cameraId, &status); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3046 | if (err != OK) { |
| 3047 | if (err == NAME_NOT_FOUND) { |
| 3048 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3049 | "Camera \"%s\" does not have a flash unit", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3050 | } |
| 3051 | ALOGE("%s: getting current torch status failed for camera %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3052 | __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3053 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 3054 | "Error changing torch strength level for camera \"%s\": %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3055 | cameraId.c_str(), strerror(-err), err); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
| 3059 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
| 3060 | ALOGE("%s: torch mode of camera %s is not available because " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3061 | "camera is in use.", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3062 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 3063 | "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] | 3064 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3065 | } else { |
| 3066 | ALOGE("%s: torch mode of camera %s is not available due to " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3067 | "insufficient resources", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3068 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 3069 | "Torch for camera \"%s\" is not available due to insufficient resources", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3070 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3071 | } |
| 3072 | } |
| 3073 | } |
| 3074 | |
| 3075 | { |
| 3076 | Mutex::Autolock al(mTorchUidMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3077 | updateTorchUidMapLocked(cameraId, uid); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3078 | } |
| 3079 | // Check if the current torch strength level is same as the new one. |
| 3080 | bool shouldSkipTorchStrengthUpdates = mCameraProviderManager->shouldSkipTorchStrengthUpdate( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3081 | cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3082 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3083 | status_t err = mFlashlight->turnOnTorchWithStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3084 | |
| 3085 | if (err != OK) { |
| 3086 | int32_t errorCode; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3087 | std::string msg; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3088 | switch (err) { |
| 3089 | case -ENOSYS: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3090 | msg = fmt::sprintf("Camera \"%s\" has no flashlight.", |
| 3091 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3092 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 3093 | break; |
| 3094 | case -EBUSY: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3095 | msg = fmt::sprintf("Camera \"%s\" is in use", |
| 3096 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3097 | errorCode = ERROR_CAMERA_IN_USE; |
| 3098 | break; |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 3099 | case -EINVAL: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3100 | msg = fmt::sprintf("Torch strength level %d is not within the " |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 3101 | "valid range.", torchStrength); |
| 3102 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 3103 | break; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3104 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3105 | msg = "Changing torch strength level failed."; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3106 | errorCode = ERROR_INVALID_OPERATION; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3107 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3108 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3109 | return STATUS_ERROR(errorCode, msg.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | { |
| 3113 | // update the link to client's death |
| 3114 | // Store the last client that turns on each camera's torch mode. |
| 3115 | Mutex::Autolock al(mTorchClientMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3116 | ssize_t index = mTorchClientMap.indexOfKey(cameraId); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3117 | if (index == NAME_NOT_FOUND) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3118 | mTorchClientMap.add(cameraId, clientBinder); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3119 | } else { |
| 3120 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
| 3121 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 3122 | } |
| 3123 | clientBinder->linkToDeath(this); |
| 3124 | } |
| 3125 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3126 | int clientPid = getCallingPid(); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3127 | 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] | 3128 | __FUNCTION__, cameraId.c_str(), torchStrength, clientPid); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3129 | if (!shouldSkipTorchStrengthUpdates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3130 | broadcastTorchStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3131 | } |
| 3132 | return Status::ok(); |
| 3133 | } |
| 3134 | |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 3135 | Status CameraService::setTorchMode(const std::string& unresolvedCameraId, bool enabled, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3136 | const sp<IBinder>& clientBinder, int32_t deviceId, int32_t devicePolicy) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3137 | Mutex::Autolock lock(mServiceLock); |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3138 | |
| 3139 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3140 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3141 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3142 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 3143 | "Torch client Binder is null"); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3144 | } |
| 3145 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3146 | int uid = getCallingUid(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3147 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
| 3148 | devicePolicy, uid); |
| 3149 | if (!cameraIdOptional.has_value()) { |
| 3150 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 3151 | unresolvedCameraId.c_str(), deviceId); |
| 3152 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3153 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 3154 | } |
| 3155 | std::string cameraId = cameraIdOptional.value(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3156 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3157 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3158 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3159 | " for system only device %s: ", cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3160 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3161 | // verify id is valid. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3162 | auto state = getCameraState(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3163 | if (state == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3164 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3165 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3166 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3167 | } |
| 3168 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3169 | StatusInternal cameraStatus = state->getStatus(); |
| 3170 | if (cameraStatus != StatusInternal::PRESENT && |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 3171 | cameraStatus != StatusInternal::NOT_AVAILABLE) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3172 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, cameraId.c_str(), |
| 3173 | (int)cameraStatus); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3174 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3175 | "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] | 3176 | } |
| 3177 | |
| 3178 | { |
| 3179 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3180 | TorchModeStatus status; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3181 | status_t err = getTorchStatusLocked(cameraId, &status); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3182 | if (err != OK) { |
| 3183 | if (err == NAME_NOT_FOUND) { |
| 3184 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3185 | "Camera \"%s\" does not have a flash unit", cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3186 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3187 | ALOGE("%s: getting current torch status failed for camera %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3188 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3189 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3190 | "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] | 3191 | strerror(-err), err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3192 | } |
| 3193 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3194 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 3195 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3196 | ALOGE("%s: torch mode of camera %s is not available because " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3197 | "camera is in use", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3198 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 3199 | "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] | 3200 | cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3201 | } else { |
| 3202 | ALOGE("%s: torch mode of camera %s is not available due to " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3203 | "insufficient resources", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3204 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 3205 | "Torch for camera \"%s\" is not available due to insufficient resources", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3206 | cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3207 | } |
| 3208 | } |
| 3209 | } |
| 3210 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3211 | { |
| 3212 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 3213 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 3214 | Mutex::Autolock al(mTorchUidMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3215 | updateTorchUidMapLocked(cameraId, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3216 | } |
| 3217 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3218 | status_t err = mFlashlight->setTorchMode(cameraId, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3219 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3220 | if (err != OK) { |
| 3221 | int32_t errorCode; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3222 | std::string msg; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3223 | switch (err) { |
| 3224 | case -ENOSYS: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3225 | msg = fmt::sprintf("Camera \"%s\" has no flashlight", |
| 3226 | cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3227 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 3228 | break; |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 3229 | case -EBUSY: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3230 | msg = fmt::sprintf("Camera \"%s\" is in use", |
| 3231 | cameraId.c_str()); |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 3232 | errorCode = ERROR_CAMERA_IN_USE; |
| 3233 | break; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3234 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3235 | msg = fmt::sprintf( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3236 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3237 | cameraId.c_str(), enabled, strerror(-err), err); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3238 | errorCode = ERROR_INVALID_OPERATION; |
| 3239 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3240 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3241 | logServiceError(msg, errorCode); |
| 3242 | return STATUS_ERROR(errorCode, msg.c_str()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3243 | } |
| 3244 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3245 | { |
| 3246 | // update the link to client's death |
| 3247 | Mutex::Autolock al(mTorchClientMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3248 | ssize_t index = mTorchClientMap.indexOfKey(cameraId); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3249 | if (enabled) { |
| 3250 | if (index == NAME_NOT_FOUND) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3251 | mTorchClientMap.add(cameraId, clientBinder); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3252 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3253 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3254 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 3255 | } |
| 3256 | clientBinder->linkToDeath(this); |
| 3257 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3258 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3259 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3260 | } |
| 3261 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3262 | int clientPid = getCallingPid(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3263 | std::string torchState = enabled ? "on" : "off"; |
| 3264 | ALOGI("Torch for camera id %s turned %s for client PID %d", cameraId.c_str(), |
| 3265 | torchState.c_str(), clientPid); |
| 3266 | logTorchEvent(cameraId, torchState, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3267 | return Status::ok(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3268 | } |
| 3269 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3270 | void CameraService::updateTorchUidMapLocked(const std::string& cameraId, int uid) { |
| 3271 | if (mTorchUidMap.find(cameraId) == mTorchUidMap.end()) { |
| 3272 | mTorchUidMap[cameraId].first = uid; |
| 3273 | mTorchUidMap[cameraId].second = uid; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3274 | } else { |
| 3275 | // Set the pending UID |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3276 | mTorchUidMap[cameraId].first = uid; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3277 | } |
| 3278 | } |
| 3279 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3280 | Status CameraService::notifySystemEvent(int32_t eventId, |
| 3281 | const std::vector<int32_t>& args) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3282 | const int pid = getCallingPid(); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3283 | const int selfPid = getpid(); |
| 3284 | |
| 3285 | // Permission checks |
| 3286 | if (pid != selfPid) { |
| 3287 | // Ensure we're being called by system_server, or similar process with |
| 3288 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3289 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3290 | const int uid = getCallingUid(); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3291 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 3292 | " events from pid=%d, uid=%d", pid, uid); |
| 3293 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 3294 | "No permission to send updates to camera service about system events" |
| 3295 | " from pid=%d, uid=%d", pid, uid); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3296 | } |
| 3297 | } |
| 3298 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3299 | ATRACE_CALL(); |
| 3300 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3301 | switch(eventId) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3302 | case ICameraService::EVENT_USER_SWITCHED: { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3303 | // 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] | 3304 | // from a system server crash |
| 3305 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3306 | mSensorPrivacyPolicy->registerSelf(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3307 | doUserSwitch(/*newUserIds*/ args); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3308 | break; |
| 3309 | } |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 3310 | case ICameraService::EVENT_USB_DEVICE_ATTACHED: |
| 3311 | case ICameraService::EVENT_USB_DEVICE_DETACHED: { |
Pawan Wagh | 99f44e2 | 2023-07-05 21:26:43 +0000 | [diff] [blame] | 3312 | if (args.size() != 1) { |
| 3313 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, |
| 3314 | "USB Device Event requires 1 argument"); |
| 3315 | } |
| 3316 | |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 3317 | // Notify CameraProviderManager for lazy HALs |
| 3318 | mCameraProviderManager->notifyUsbDeviceEvent(eventId, |
| 3319 | std::to_string(args[0])); |
| 3320 | break; |
| 3321 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3322 | case ICameraService::EVENT_NONE: |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3323 | default: { |
| 3324 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 3325 | eventId); |
| 3326 | break; |
| 3327 | } |
| 3328 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3329 | return Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3330 | } |
| 3331 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3332 | void CameraService::notifyMonitoredUids() { |
| 3333 | Mutex::Autolock lock(mStatusListenerLock); |
| 3334 | |
| 3335 | for (const auto& it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3336 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 3337 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 3338 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3339 | } |
| 3340 | } |
| 3341 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3342 | void CameraService::notifyMonitoredUids(const std::unordered_set<uid_t> ¬ifyUidSet) { |
| 3343 | Mutex::Autolock lock(mStatusListenerLock); |
| 3344 | |
| 3345 | for (const auto& it : mListenerList) { |
| 3346 | if (notifyUidSet.find(it->getListenerUid()) != notifyUidSet.end()) { |
| 3347 | ALOGV("%s: notifying uid %d", __FUNCTION__, it->getListenerUid()); |
| 3348 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
| 3349 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 3350 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
| 3351 | } |
| 3352 | } |
| 3353 | } |
| 3354 | |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3355 | Status CameraService::notifyDeviceStateChange(int64_t newState) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3356 | const int pid = getCallingPid(); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3357 | const int selfPid = getpid(); |
| 3358 | |
| 3359 | // Permission checks |
| 3360 | if (pid != selfPid) { |
| 3361 | // Ensure we're being called by system_server, or similar process with |
| 3362 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3363 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3364 | const int uid = getCallingUid(); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3365 | ALOGE("Permission Denial: cannot send updates to camera service about device" |
| 3366 | " state changes from pid=%d, uid=%d", pid, uid); |
| 3367 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 3368 | "No permission to send updates to camera service about device state" |
| 3369 | " changes from pid=%d, uid=%d", pid, uid); |
| 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | ATRACE_CALL(); |
| 3374 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3375 | { |
| 3376 | Mutex::Autolock lock(mServiceLock); |
| 3377 | mDeviceState = newState; |
| 3378 | } |
| 3379 | |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 3380 | mCameraProviderManager->notifyDeviceStateChange(newState); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3381 | |
| 3382 | return Status::ok(); |
| 3383 | } |
| 3384 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3385 | Status CameraService::notifyDisplayConfigurationChange() { |
| 3386 | ATRACE_CALL(); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3387 | const int callingPid = getCallingPid(); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3388 | const int selfPid = getpid(); |
| 3389 | |
| 3390 | // Permission checks |
| 3391 | if (callingPid != selfPid) { |
| 3392 | // Ensure we're being called by system_server, or similar process with |
| 3393 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3394 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3395 | const int uid = getCallingUid(); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3396 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" |
| 3397 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 3398 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 3399 | "No permission to send updates to camera service about orientation" |
| 3400 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | Mutex::Autolock lock(mServiceLock); |
| 3405 | |
| 3406 | // Don't do anything if rotate-and-crop override via cmd is active |
| 3407 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); |
| 3408 | |
| 3409 | const auto clients = mActiveClientManager.getAll(); |
| 3410 | for (auto& current : clients) { |
| 3411 | if (current != nullptr) { |
| 3412 | const auto basicClient = current->getValue(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3413 | if (basicClient.get() != nullptr && !basicClient->getOverrideToPortrait()) { |
| 3414 | basicClient->setRotateAndCropOverride( |
| 3415 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 3416 | basicClient->getPackageName(), |
| 3417 | basicClient->getCameraFacing(), |
| 3418 | multiuser_get_user_id(basicClient->getClientUid()))); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3419 | } |
| 3420 | } |
| 3421 | } |
| 3422 | |
| 3423 | return Status::ok(); |
| 3424 | } |
| 3425 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3426 | // TODO(b/291736219): This to be made device-aware. |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3427 | Status CameraService::getConcurrentCameraIds( |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3428 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { |
| 3429 | ATRACE_CALL(); |
| 3430 | if (!concurrentCameraIds) { |
| 3431 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); |
| 3432 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); |
| 3433 | } |
| 3434 | |
| 3435 | if (!mInitialized) { |
| 3436 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3437 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3438 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 3439 | "Camera subsystem is not available"); |
| 3440 | } |
| 3441 | // First call into the provider and get the set of concurrent camera |
| 3442 | // combinations |
| 3443 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = |
Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 3444 | mCameraProviderManager->getConcurrentCameraIds(); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3445 | for (auto &combination : concurrentCameraCombinations) { |
| 3446 | std::vector<std::string> validCombination; |
| 3447 | for (auto &cameraId : combination) { |
| 3448 | // if the camera state is not present, skip |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3449 | auto state = getCameraState(cameraId); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3450 | if (state == nullptr) { |
| 3451 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); |
| 3452 | continue; |
| 3453 | } |
| 3454 | StatusInternal status = state->getStatus(); |
| 3455 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { |
| 3456 | continue; |
| 3457 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3458 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3459 | continue; |
| 3460 | } |
| 3461 | validCombination.push_back(cameraId); |
| 3462 | } |
| 3463 | if (validCombination.size() != 0) { |
| 3464 | concurrentCameraIds->push_back(std::move(validCombination)); |
| 3465 | } |
| 3466 | } |
| 3467 | return Status::ok(); |
| 3468 | } |
| 3469 | |
| 3470 | Status CameraService::isConcurrentSessionConfigurationSupported( |
| 3471 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 3472 | int targetSdkVersion, /*out*/bool* isSupported) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3473 | if (!isSupported) { |
| 3474 | ALOGE("%s: isSupported is NULL", __FUNCTION__); |
| 3475 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); |
| 3476 | } |
| 3477 | |
| 3478 | if (!mInitialized) { |
| 3479 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 3480 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 3481 | "Camera subsystem is not available"); |
| 3482 | } |
| 3483 | |
| 3484 | // Check for camera permissions |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3485 | int callingPid = getCallingPid(); |
| 3486 | int callingUid = getCallingUid(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 3487 | bool hasCameraPermission = ((callingPid == getpid()) || |
| 3488 | hasPermissionsForCamera(callingPid, callingUid)); |
| 3489 | if (!hasCameraPermission) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3490 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 3491 | "android.permission.CAMERA needed to call" |
| 3492 | "isConcurrentSessionConfigurationSupported"); |
| 3493 | } |
| 3494 | |
| 3495 | status_t res = |
| 3496 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 3497 | cameraIdsAndSessionConfigurations, mPerfClassPrimaryCameraIds, |
| 3498 | targetSdkVersion, isSupported); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3499 | if (res != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3500 | logServiceError("Unable to query session configuration support", |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3501 | ERROR_INVALID_OPERATION); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3502 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " |
| 3503 | "support %s (%d)", strerror(-res), res); |
| 3504 | } |
| 3505 | return Status::ok(); |
| 3506 | } |
| 3507 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3508 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, |
| 3509 | /*out*/ |
| 3510 | std::vector<hardware::CameraStatus> *cameraStatuses) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3511 | return addListenerHelper(listener, cameraStatuses); |
| 3512 | } |
| 3513 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3514 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, |
| 3515 | std::vector<hardware::CameraStatus>* cameraStatuses) { |
| 3516 | return addListenerHelper(listener, cameraStatuses, false, true); |
| 3517 | } |
| 3518 | |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3519 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, |
| 3520 | /*out*/ |
| 3521 | std::vector<hardware::CameraStatus> *cameraStatuses, |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3522 | bool isVendorListener, bool isProcessLocalTest) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3523 | ATRACE_CALL(); |
| 3524 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3525 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3526 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 3527 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3528 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3529 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3530 | } |
| 3531 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3532 | auto clientPid = getCallingPid(); |
| 3533 | auto clientUid = getCallingUid(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 3534 | bool openCloseCallbackAllowed = hasPermissionsForOpenCloseListener(clientPid, clientUid); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3535 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3536 | Mutex::Autolock lock(mServiceLock); |
| 3537 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3538 | { |
| 3539 | Mutex::Autolock lock(mStatusListenerLock); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3540 | for (const auto &it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3541 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3542 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 3543 | __FUNCTION__, listener.get()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3544 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3545 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3546 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3547 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3548 | sp<ServiceListener> serviceListener = |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3549 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, |
| 3550 | openCloseCallbackAllowed); |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3551 | auto ret = serviceListener->initialize(isProcessLocalTest); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3552 | if (ret != NO_ERROR) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3553 | std::string msg = fmt::sprintf("Failed to initialize service listener: %s (%d)", |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3554 | strerror(-ret), ret); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3555 | logServiceError(msg, ERROR_ILLEGAL_ARGUMENT); |
| 3556 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3557 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3558 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3559 | // The listener still needs to be added to the list of listeners, regardless of what |
| 3560 | // permissions the listener process has / whether it is a vendor listener. Since it might be |
| 3561 | // eligible to listen to other camera ids. |
| 3562 | mListenerList.emplace_back(serviceListener); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3563 | mUidPolicy->registerMonitorUid(clientUid, /*openCamera*/false); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3564 | } |
| 3565 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3566 | /* Collect current devices and status */ |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3567 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3568 | Mutex::Autolock lock(mCameraStatesLock); |
| 3569 | for (auto& i : mCameraStates) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3570 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 3571 | auto [deviceId, mappedCameraId] = |
| 3572 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(i.first); |
| 3573 | |
| 3574 | cameraStatuses->emplace_back(mappedCameraId, |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 3575 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds(), |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3576 | openCloseCallbackAllowed ? i.second->getClientPackage() : std::string(), |
| 3577 | deviceId); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3578 | } |
| 3579 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3580 | // Remove the camera statuses that should be hidden from the client, we do |
| 3581 | // this after collecting the states in order to avoid holding |
| 3582 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at |
| 3583 | // the same time. |
| 3584 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), |
| 3585 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3586 | std::string cameraId = s.cameraId; |
| 3587 | std::optional<std::string> cameraIdOptional = resolveCameraId(s.cameraId, |
| 3588 | s.deviceId, IVirtualDeviceManagerNative::DEVICE_POLICY_CUSTOM, |
| 3589 | clientUid); |
| 3590 | if (!cameraIdOptional.has_value()) { |
| 3591 | std::string msg = |
| 3592 | fmt::sprintf( |
| 3593 | "Camera %s: Invalid camera id for device id %d", |
| 3594 | s.cameraId.c_str(), s.deviceId); |
| 3595 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3596 | return true; |
| 3597 | } |
| 3598 | cameraId = cameraIdOptional.value(); |
| 3599 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 3600 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 3601 | ALOGE("%s: Invalid camera id %s, skipping status update", |
| 3602 | __FUNCTION__, s.cameraId.c_str()); |
| 3603 | return true; |
| 3604 | } |
| 3605 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, |
| 3606 | clientUid); |
| 3607 | }), cameraStatuses->end()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3608 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3609 | // cameraStatuses will have non-eligible camera ids removed. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3610 | std::set<std::string> idsChosenForCallback; |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3611 | for (const auto &s : *cameraStatuses) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3612 | // Add only default device cameras here, as virtual cameras currently don't support torch |
| 3613 | // anyway. Note that this is a simplification of the implementation here, and we should |
| 3614 | // change this when virtual cameras support torch. |
| 3615 | if (s.deviceId == kDefaultDeviceId) { |
| 3616 | idsChosenForCallback.insert(s.cameraId); |
| 3617 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3618 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3619 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3620 | /* |
| 3621 | * Immediately signal current torch status to this listener only |
| 3622 | * This may be a subset of all the devices, so don't include it in the response directly |
| 3623 | */ |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3624 | { |
| 3625 | Mutex::Autolock al(mTorchStatusMutex); |
| 3626 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3627 | const std::string &id = mTorchStatusMap.keyAt(i); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3628 | // The camera id is visible to the client. Fine to send torch |
| 3629 | // callback. |
| 3630 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3631 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id, |
| 3632 | kDefaultDeviceId); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3633 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3634 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3635 | } |
| 3636 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3637 | return Status::ok(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3638 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3639 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3640 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3641 | ATRACE_CALL(); |
| 3642 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3643 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 3644 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3645 | if (listener == 0) { |
| 3646 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3647 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3648 | } |
| 3649 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3650 | Mutex::Autolock lock(mServiceLock); |
| 3651 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3652 | { |
| 3653 | Mutex::Autolock lock(mStatusListenerLock); |
| 3654 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3655 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3656 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid(), /*closeCamera*/false); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3657 | IInterface::asBinder(listener)->unlinkToDeath(*it); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3658 | mListenerList.erase(it); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3659 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3660 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3661 | } |
| 3662 | } |
| 3663 | |
| 3664 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 3665 | __FUNCTION__, listener.get()); |
| 3666 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3667 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3668 | } |
| 3669 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3670 | Status CameraService::getLegacyParameters(int cameraId, /*out*/std::string* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3671 | |
| 3672 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3673 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 3674 | |
| 3675 | if (parameters == NULL) { |
| 3676 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3677 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3678 | } |
| 3679 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3680 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3681 | |
| 3682 | CameraParameters shimParams; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3683 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3684 | // Error logged by caller |
| 3685 | return ret; |
| 3686 | } |
| 3687 | |
| 3688 | String8 shimParamsString8 = shimParams.flatten(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3689 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3690 | *parameters = toStdString(shimParamsString8); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3691 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3692 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3693 | } |
| 3694 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3695 | Status CameraService::supportsCameraApi(const std::string& unresolvedCameraId, int apiVersion, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3696 | /*out*/ bool *isSupported) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3697 | ATRACE_CALL(); |
| 3698 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 3699 | const std::string cameraId = resolveCameraId( |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3700 | unresolvedCameraId, getCallingUid()); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 3701 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3702 | ALOGV("%s: for camera ID = %s", __FUNCTION__, cameraId.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3703 | |
| 3704 | switch (apiVersion) { |
| 3705 | case API_VERSION_1: |
| 3706 | case API_VERSION_2: |
| 3707 | break; |
| 3708 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3709 | std::string msg = fmt::sprintf("Unknown API version %d", apiVersion); |
| 3710 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3711 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3712 | } |
| 3713 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3714 | int portraitRotation; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3715 | auto deviceVersionAndTransport = getDeviceVersion(cameraId, false, &portraitRotation); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3716 | if (deviceVersionAndTransport.first == -1) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3717 | std::string msg = fmt::sprintf("Unknown camera ID %s", cameraId.c_str()); |
| 3718 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3719 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3720 | } |
| 3721 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 3722 | int deviceVersion = deviceVersionAndTransport.first; |
| 3723 | switch (deviceVersion) { |
| 3724 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 3725 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 3726 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 3727 | if (apiVersion == API_VERSION_2) { |
| 3728 | 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] | 3729 | "shim", __FUNCTION__, cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3730 | *isSupported = false; |
| 3731 | } else { // if (apiVersion == API_VERSION_1) { |
| 3732 | 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] | 3733 | "supported", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3734 | *isSupported = true; |
| 3735 | } |
| 3736 | break; |
| 3737 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 3738 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 3739 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 3740 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 3741 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 3742 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 3743 | 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] | 3744 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3745 | *isSupported = true; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3746 | break; |
| 3747 | default: { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3748 | std::string msg = fmt::sprintf("Unknown device version %x for device %s", |
| 3749 | deviceVersion, cameraId.c_str()); |
| 3750 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3751 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3752 | } |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3753 | } |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3754 | } else { |
| 3755 | *isSupported = true; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3756 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3757 | return Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3758 | } |
| 3759 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3760 | Status CameraService::isHiddenPhysicalCamera(const std::string& unresolvedCameraId, |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3761 | /*out*/ bool *isSupported) { |
| 3762 | ATRACE_CALL(); |
| 3763 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 3764 | const std::string cameraId = resolveCameraId(unresolvedCameraId, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3765 | getCallingUid()); |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3766 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3767 | ALOGV("%s: for camera ID = %s", __FUNCTION__, cameraId.c_str()); |
| 3768 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(cameraId); |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3769 | |
| 3770 | return Status::ok(); |
| 3771 | } |
| 3772 | |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3773 | Status CameraService::injectCamera( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3774 | const std::string& packageName, const std::string& internalCamId, |
| 3775 | const std::string& externalCamId, |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3776 | const sp<ICameraInjectionCallback>& callback, |
| 3777 | /*out*/ |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3778 | sp<ICameraInjectionSession>* cameraInjectionSession) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3779 | ATRACE_CALL(); |
| 3780 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3781 | if (!checkCallingPermission(toString16(sCameraInjectExternalCameraPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3782 | const int pid = getCallingPid(); |
| 3783 | const int uid = getCallingUid(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3784 | ALOGE("Permission Denial: can't inject camera pid=%d, uid=%d", pid, uid); |
| 3785 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3786 | "Permission Denial: no permission to inject camera"); |
| 3787 | } |
| 3788 | |
| 3789 | // Do not allow any camera injection that injects or replaces a virtual camera. |
| 3790 | auto [deviceIdForInternalCamera, _] = |
| 3791 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(internalCamId); |
| 3792 | if (deviceIdForInternalCamera != kDefaultDeviceId) { |
| 3793 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED, |
| 3794 | "Cannot replace a virtual camera"); |
| 3795 | } |
| 3796 | [[maybe_unused]] auto [deviceIdForExternalCamera, unusedMappedCameraId] = |
| 3797 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(externalCamId); |
| 3798 | if (deviceIdForExternalCamera != kDefaultDeviceId) { |
| 3799 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED, |
| 3800 | "Cannot inject a virtual camera to replace an internal camera"); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | ALOGV( |
| 3804 | "%s: Package name = %s, Internal camera ID = %s, External camera ID = " |
| 3805 | "%s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3806 | __FUNCTION__, packageName.c_str(), |
| 3807 | internalCamId.c_str(), externalCamId.c_str()); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3808 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3809 | { |
| 3810 | Mutex::Autolock lock(mInjectionParametersLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3811 | mInjectionInternalCamId = internalCamId; |
| 3812 | mInjectionExternalCamId = externalCamId; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3813 | mInjectionStatusListener->addListener(callback); |
| 3814 | *cameraInjectionSession = new CameraInjectionSession(this); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3815 | status_t res = NO_ERROR; |
| 3816 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 3817 | // If the client already exists, we can directly connect to the camera device through the |
| 3818 | // client's injectCamera(), otherwise we need to wait until the client is established |
| 3819 | // (execute connectHelper()) before injecting the camera to the camera device. |
| 3820 | if (clientDescriptor != nullptr) { |
| 3821 | mInjectionInitPending = false; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3822 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 3823 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 3824 | if(res != OK) { |
| 3825 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 3826 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3827 | mInjectionExternalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3828 | } |
| 3829 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3830 | if (res != OK) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3831 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 3832 | } |
| 3833 | } else { |
| 3834 | mInjectionInitPending = true; |
| 3835 | } |
| 3836 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3837 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3838 | return binder::Status::ok(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3839 | } |
| 3840 | |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 3841 | Status CameraService::reportExtensionSessionStats( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3842 | const hardware::CameraExtensionSessionStats& stats, std::string* sessionKey /*out*/) { |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 3843 | ALOGV("%s: reported %s", __FUNCTION__, stats.toString().c_str()); |
| 3844 | *sessionKey = mCameraServiceProxyWrapper->updateExtensionStats(stats); |
| 3845 | return Status::ok(); |
| 3846 | } |
| 3847 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3848 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3849 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3850 | for (auto& i : mActiveClientManager.getAll()) { |
| 3851 | auto clientSp = i->getValue(); |
| 3852 | if (clientSp.get() == client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3853 | cacheClientTagDumpIfNeeded(client->mCameraIdStr, clientSp.get()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3854 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3855 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3856 | } |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3857 | updateAudioRestrictionLocked(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3858 | } |
| 3859 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3860 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3861 | bool ret = false; |
| 3862 | { |
| 3863 | // Acquire mServiceLock and prevent other clients from connecting |
| 3864 | std::unique_ptr<AutoConditionLock> lock = |
| 3865 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3866 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3867 | std::vector<sp<BasicClient>> evicted; |
| 3868 | for (auto& i : mActiveClientManager.getAll()) { |
| 3869 | auto clientSp = i->getValue(); |
| 3870 | if (clientSp.get() == nullptr) { |
| 3871 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3872 | mActiveClientManager.remove(i); |
| 3873 | continue; |
| 3874 | } |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 3875 | if (remote == clientSp->getRemote()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3876 | mActiveClientManager.remove(i); |
| 3877 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3878 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3879 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3880 | clientSp->notifyError( |
| 3881 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3882 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3883 | } |
| 3884 | } |
| 3885 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3886 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 3887 | // other clients from connecting in mServiceLockWrapper if held |
| 3888 | mServiceLock.unlock(); |
| 3889 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3890 | // Do not clear caller identity, remote caller should be client proccess |
| 3891 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3892 | for (auto& i : evicted) { |
| 3893 | if (i.get() != nullptr) { |
| 3894 | i->disconnect(); |
| 3895 | ret = true; |
| 3896 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3897 | } |
| 3898 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3899 | // Reacquire mServiceLock |
| 3900 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3901 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3902 | } // lock is destroyed, allow further connect calls |
| 3903 | |
| 3904 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3905 | } |
| 3906 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3907 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3908 | const std::string& cameraId) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3909 | std::shared_ptr<CameraState> state; |
| 3910 | { |
| 3911 | Mutex::Autolock lock(mCameraStatesLock); |
| 3912 | auto iter = mCameraStates.find(cameraId); |
| 3913 | if (iter != mCameraStates.end()) { |
| 3914 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3915 | } |
| 3916 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3917 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3918 | } |
| 3919 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3920 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const std::string& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3921 | // Remove from active clients list |
| 3922 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 3923 | if (clientDescriptorPtr == nullptr) { |
| 3924 | 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] | 3925 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3926 | return sp<BasicClient>{nullptr}; |
| 3927 | } |
| 3928 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3929 | sp<BasicClient> client = clientDescriptorPtr->getValue(); |
| 3930 | if (client.get() != nullptr) { |
| 3931 | cacheClientTagDumpIfNeeded(clientDescriptorPtr->getKey(), client.get()); |
| 3932 | } |
| 3933 | return client; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 3934 | } |
| 3935 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3936 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3937 | // Acquire mServiceLock and prevent other clients from connecting |
| 3938 | std::unique_ptr<AutoConditionLock> lock = |
| 3939 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 3940 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3941 | std::set<userid_t> newAllowedUsers; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3942 | for (size_t i = 0; i < newUserIds.size(); i++) { |
| 3943 | if (newUserIds[i] < 0) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3944 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3945 | __FUNCTION__, newUserIds[i]); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3946 | return; |
| 3947 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3948 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3949 | } |
| 3950 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3951 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3952 | if (newAllowedUsers == mAllowedUsers) { |
| 3953 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 3954 | return; |
| 3955 | } |
| 3956 | |
| 3957 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 3958 | |
| 3959 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3960 | |
| 3961 | // Current user has switched, evict all current clients. |
| 3962 | std::vector<sp<BasicClient>> evicted; |
| 3963 | for (auto& i : mActiveClientManager.getAll()) { |
| 3964 | auto clientSp = i->getValue(); |
| 3965 | |
| 3966 | if (clientSp.get() == nullptr) { |
| 3967 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3968 | continue; |
| 3969 | } |
| 3970 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3971 | // Don't evict clients that are still allowed. |
| 3972 | uid_t clientUid = clientSp->getClientUid(); |
| 3973 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 3974 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 3975 | continue; |
| 3976 | } |
| 3977 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3978 | evicted.push_back(clientSp); |
| 3979 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3980 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3981 | i->getKey().c_str()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3982 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3983 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3984 | logEvent(fmt::sprintf("EVICT device %s client held by package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3985 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3986 | " to user switch.", i->getKey().c_str(), |
| 3987 | clientSp->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3988 | i->getOwnerId(), i->getPriority().getScore(), |
| 3989 | i->getPriority().getState())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3990 | |
| 3991 | } |
| 3992 | |
| 3993 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 3994 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 3995 | mServiceLock.unlock(); |
| 3996 | |
| 3997 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3998 | int64_t token = clearCallingIdentity(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3999 | |
| 4000 | for (auto& i : evicted) { |
| 4001 | i->disconnect(); |
| 4002 | } |
| 4003 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4004 | restoreCallingIdentity(token); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4005 | |
| 4006 | // Reacquire mServiceLock |
| 4007 | mServiceLock.lock(); |
| 4008 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4009 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4010 | void CameraService::logEvent(const std::string &event) { |
| 4011 | std::string curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4012 | Mutex::Autolock l(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4013 | std::string msg = curTime + " : " + event; |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 4014 | // For service error events, print the msg only once. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4015 | if (msg.find("SERVICE ERROR") != std::string::npos) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 4016 | mEventLog.add(msg); |
| 4017 | } else if(sServiceErrorEventSet.find(msg) == sServiceErrorEventSet.end()) { |
| 4018 | // Error event not added to the dumpsys log before |
| 4019 | mEventLog.add(msg); |
| 4020 | sServiceErrorEventSet.insert(msg); |
| 4021 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4022 | } |
| 4023 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4024 | void CameraService::logDisconnected(const std::string &cameraId, int clientPid, |
| 4025 | const std::string &clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4026 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4027 | logEvent(fmt::sprintf("DISCONNECT device %s client for package %s (PID %d)", cameraId.c_str(), |
| 4028 | clientPackage.c_str(), clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4029 | } |
| 4030 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4031 | void CameraService::logDisconnectedOffline(const std::string &cameraId, int clientPid, |
| 4032 | const std::string &clientPackage) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 4033 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4034 | logEvent(fmt::sprintf("DISCONNECT offline device %s client for package %s (PID %d)", |
| 4035 | cameraId.c_str(), clientPackage.c_str(), clientPid)); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 4036 | } |
| 4037 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4038 | void CameraService::logConnected(const std::string &cameraId, int clientPid, |
| 4039 | const std::string &clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4040 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4041 | logEvent(fmt::sprintf("CONNECT device %s client for package %s (PID %d)", cameraId.c_str(), |
| 4042 | clientPackage.c_str(), clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4043 | } |
| 4044 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4045 | void CameraService::logConnectedOffline(const std::string &cameraId, int clientPid, |
| 4046 | const std::string &clientPackage) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 4047 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4048 | logEvent(fmt::sprintf("CONNECT offline device %s client for package %s (PID %d)", |
| 4049 | cameraId.c_str(), clientPackage.c_str(), clientPid)); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 4050 | } |
| 4051 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4052 | void CameraService::logRejected(const std::string &cameraId, int clientPid, |
| 4053 | const std::string &clientPackage, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4054 | // Log the client rejected |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4055 | logEvent(fmt::sprintf("REJECT device %s client for package %s (PID %d), reason: (%s)", |
| 4056 | cameraId.c_str(), clientPackage.c_str(), clientPid, reason.c_str())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4057 | } |
| 4058 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4059 | void CameraService::logTorchEvent(const std::string &cameraId, const std::string &torchState, |
| 4060 | int clientPid) { |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 4061 | // Log torch event |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4062 | logEvent(fmt::sprintf("Torch for camera id %s turned %s for client PID %d", cameraId.c_str(), |
| 4063 | torchState.c_str(), clientPid)); |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 4064 | } |
| 4065 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4066 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 4067 | const std::set<userid_t>& newUserIds) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4068 | std::string newUsers = toString(newUserIds); |
| 4069 | std::string oldUsers = toString(oldUserIds); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4070 | if (oldUsers.size() == 0) { |
| 4071 | oldUsers = "<None>"; |
| 4072 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4073 | // Log the new and old users |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4074 | logEvent(fmt::sprintf("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s", |
| 4075 | oldUsers.c_str(), newUsers.c_str())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4076 | } |
| 4077 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4078 | void CameraService::logDeviceRemoved(const std::string &cameraId, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4079 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4080 | 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] | 4081 | } |
| 4082 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4083 | void CameraService::logDeviceAdded(const std::string &cameraId, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4084 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4085 | 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] | 4086 | } |
| 4087 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4088 | void CameraService::logClientDied(int clientPid, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4089 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4090 | 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] | 4091 | } |
| 4092 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4093 | void CameraService::logServiceError(const std::string &msg, int errorCode) { |
| 4094 | logEvent(fmt::sprintf("SERVICE ERROR: %s : %d (%s)", msg.c_str(), errorCode, |
| 4095 | strerror(-errorCode))); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4096 | } |
| 4097 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4098 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 4099 | uint32_t flags) { |
| 4100 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4101 | // Permission checks |
| 4102 | switch (code) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4103 | case SHELL_COMMAND_TRANSACTION: { |
| 4104 | int in = data.readFileDescriptor(); |
| 4105 | int out = data.readFileDescriptor(); |
| 4106 | int err = data.readFileDescriptor(); |
| 4107 | int argc = data.readInt32(); |
| 4108 | Vector<String16> args; |
| 4109 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 4110 | args.add(data.readString16()); |
| 4111 | } |
| 4112 | sp<IBinder> unusedCallback; |
| 4113 | sp<IResultReceiver> resultReceiver; |
| 4114 | status_t status; |
| 4115 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 4116 | return status; |
| 4117 | } |
| 4118 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 4119 | return status; |
| 4120 | } |
| 4121 | status = shellCommand(in, out, err, args); |
| 4122 | if (resultReceiver != nullptr) { |
| 4123 | resultReceiver->send(status); |
| 4124 | } |
| 4125 | return NO_ERROR; |
| 4126 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | return BnCameraService::onTransact(code, data, reply, flags); |
| 4130 | } |
| 4131 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4132 | // We share the media players for shutter and recording sound for all clients. |
| 4133 | // A reference count is kept to determine when we will actually release the |
| 4134 | // media players. |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 4135 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { |
| 4136 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 4137 | status_t error; |
| 4138 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 4139 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 4140 | error = mp->prepare(); |
| 4141 | } |
| 4142 | if (error != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4143 | ALOGE("Failed to load CameraService sounds: %s", file); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 4144 | mp->disconnect(); |
| 4145 | mp.clear(); |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4146 | return nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4147 | } |
| 4148 | return mp; |
| 4149 | } |
| 4150 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4151 | void CameraService::increaseSoundRef() { |
| 4152 | Mutex::Autolock lock(mSoundLock); |
| 4153 | mSoundRef++; |
| 4154 | } |
| 4155 | |
| 4156 | void CameraService::loadSoundLocked(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4157 | ATRACE_CALL(); |
| 4158 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4159 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); |
| 4160 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { |
| 4161 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); |
| 4162 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { |
| 4163 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 4164 | } |
| 4165 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) { |
| 4166 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); |
| 4167 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { |
| 4168 | mSoundPlayer[SOUND_RECORDING_START] = |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4169 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4170 | } |
| 4171 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { |
| 4172 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); |
| 4173 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { |
| 4174 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
| 4175 | } |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4176 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4177 | } |
| 4178 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4179 | void CameraService::decreaseSoundRef() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4180 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4181 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4182 | if (--mSoundRef) return; |
| 4183 | |
| 4184 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 4185 | if (mSoundPlayer[i] != 0) { |
| 4186 | mSoundPlayer[i]->disconnect(); |
| 4187 | mSoundPlayer[i].clear(); |
| 4188 | } |
| 4189 | } |
| 4190 | } |
| 4191 | |
| 4192 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4193 | ATRACE_CALL(); |
| 4194 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4195 | LOG1("playSound(%d)", kind); |
Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame] | 4196 | if (kind < 0 || kind >= NUM_SOUNDS) { |
| 4197 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); |
| 4198 | return; |
| 4199 | } |
| 4200 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4201 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4202 | loadSoundLocked(kind); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4203 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 4204 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 4205 | player->seekTo(0); |
| 4206 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4207 | } |
| 4208 | } |
| 4209 | |
| 4210 | // ---------------------------------------------------------------------------- |
| 4211 | |
| 4212 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 4213 | const sp<ICameraClient>& cameraClient, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4214 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4215 | const std::string& clientPackageName, bool systemNativeClient, |
| 4216 | const std::optional<std::string>& clientFeatureId, |
| 4217 | const std::string& cameraIdStr, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4218 | int api1CameraId, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4219 | int clientPid, uid_t clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 4220 | int servicePid, bool overrideToPortrait) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 4221 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 4222 | IInterface::asBinder(cameraClient), |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4223 | attributionAndPermissionUtils, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4224 | clientPackageName, systemNativeClient, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4225 | cameraIdStr, cameraFacing, sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4226 | clientPid, clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 4227 | servicePid, overrideToPortrait), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4228 | mCameraId(api1CameraId) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4229 | { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4230 | int callingPid = getCallingPid(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4231 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4232 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 4233 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4234 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4235 | cameraService->increaseSoundRef(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4236 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4237 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4238 | } |
| 4239 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4240 | // tear down the client |
| 4241 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4242 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4243 | mDestructionStarted = true; |
| 4244 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4245 | sCameraService->decreaseSoundRef(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 4246 | // unconditionally disconnect. function is idempotent |
| 4247 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4248 | } |
| 4249 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4250 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; |
| 4251 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4252 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4253 | const sp<IBinder>& remoteCallback, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4254 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4255 | const std::string& clientPackageName, bool nativeClient, |
| 4256 | const std::optional<std::string>& clientFeatureId, const std::string& cameraIdStr, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4257 | int cameraFacing, int sensorOrientation, int clientPid, uid_t clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 4258 | int servicePid, bool overrideToPortrait): |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4259 | AttributionAndPermissionUtilsEncapsulator(attributionAndPermissionUtils), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4260 | mDestructionStarted(false), |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4261 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), mOrientation(sensorOrientation), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4262 | mClientPackageName(clientPackageName), mSystemNativeClient(nativeClient), |
| 4263 | mClientFeatureId(clientFeatureId), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 4264 | mClientPid(clientPid), mClientUid(clientUid), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4265 | mServicePid(servicePid), |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 4266 | mDisconnected(false), mUidIsTrusted(false), |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 4267 | mOverrideToPortrait(overrideToPortrait), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4268 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4269 | mRemoteBinder(remoteCallback), |
| 4270 | mOpsActive(false), |
| 4271 | mOpsStreaming(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4272 | { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4273 | if (sCameraService == nullptr) { |
| 4274 | sCameraService = cameraService; |
| 4275 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 4276 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4277 | // There are 2 scenarios in which a client won't have AppOps operations |
| 4278 | // (both scenarios : native clients) |
| 4279 | // 1) It's an system native client*, the package name will be empty |
| 4280 | // and it will return from this function in the previous if condition |
| 4281 | // (This is the same as the previously existing behavior). |
| 4282 | // 2) It is a system native client, but its package name has been |
| 4283 | // modified for debugging, however it still must not use AppOps since |
| 4284 | // the package name is not a real one. |
| 4285 | // |
| 4286 | // * system native client - native client with UID < AID_APP_START. It |
| 4287 | // doesn't exclude clients not on the system partition. |
| 4288 | if (!mSystemNativeClient) { |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4289 | mAppOpsManager = std::make_unique<AppOpsManager>(); |
| 4290 | } |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 4291 | |
Charles Chen | f075f08 | 2024-03-04 23:32:55 +0000 | [diff] [blame] | 4292 | mUidIsTrusted = isTrustedCallingUid(mClientUid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4296 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4297 | mDestructionStarted = true; |
| 4298 | } |
| 4299 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4300 | binder::Status CameraService::BasicClient::disconnect() { |
| 4301 | binder::Status res = Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4302 | if (mDisconnected) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4303 | return res; |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4304 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 4305 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4306 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4307 | sCameraService->removeByClient(this); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4308 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, mClientPackageName); |
Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 4309 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4310 | mCameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4311 | |
| 4312 | sp<IBinder> remote = getRemote(); |
| 4313 | if (remote != nullptr) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4314 | remote->unlinkToDeath(sCameraService); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4315 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4316 | |
| 4317 | finishCameraOps(); |
Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 4318 | // Notify flashlight that a camera device is closed. |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4319 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4320 | 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] | 4321 | mClientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4322 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4323 | // client shouldn't be able to call into us anymore |
| 4324 | mClientPid = 0; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4325 | |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 4326 | const auto& mActivityManager = getActivityManager(); |
| 4327 | if (mActivityManager) { |
| 4328 | mActivityManager->logFgsApiEnd(LOG_FGS_CAMERA_API, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4329 | getCallingUid(), |
| 4330 | getCallingPid()); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4333 | return res; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4334 | } |
| 4335 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 4336 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 4337 | // No dumping of clients directly over Binder, |
| 4338 | // must go through CameraService::dump |
| 4339 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4340 | getCallingUid(), NULL, 0); |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 4341 | return OK; |
| 4342 | } |
| 4343 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4344 | status_t CameraService::BasicClient::startWatchingTags(const std::string&, int) { |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4345 | // Can't watch tags directly, must go through CameraService::startWatchingTags |
| 4346 | return OK; |
| 4347 | } |
| 4348 | |
| 4349 | status_t CameraService::BasicClient::stopWatchingTags(int) { |
| 4350 | // Can't watch tags directly, must go through CameraService::stopWatchingTags |
| 4351 | return OK; |
| 4352 | } |
| 4353 | |
| 4354 | status_t CameraService::BasicClient::dumpWatchedEventsToVector(std::vector<std::string> &) { |
| 4355 | // Can't watch tags directly, must go through CameraService::dumpWatchedEventsToVector |
| 4356 | return OK; |
| 4357 | } |
| 4358 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4359 | std::string CameraService::BasicClient::getPackageName() const { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 4360 | return mClientPackageName; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4361 | } |
| 4362 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4363 | int CameraService::BasicClient::getCameraFacing() const { |
| 4364 | return mCameraFacing; |
| 4365 | } |
| 4366 | |
| 4367 | int CameraService::BasicClient::getCameraOrientation() const { |
| 4368 | return mOrientation; |
| 4369 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4370 | |
| 4371 | int CameraService::BasicClient::getClientPid() const { |
| 4372 | return mClientPid; |
| 4373 | } |
| 4374 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4375 | uid_t CameraService::BasicClient::getClientUid() const { |
| 4376 | return mClientUid; |
| 4377 | } |
| 4378 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 4379 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 4380 | // Defaults to API2. |
| 4381 | return level == API_2; |
| 4382 | } |
| 4383 | |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 4384 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4385 | { |
| 4386 | Mutex::Autolock l(mAudioRestrictionLock); |
| 4387 | mAudioRestriction = mode; |
| 4388 | } |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 4389 | sCameraService->updateAudioRestriction(); |
| 4390 | return OK; |
| 4391 | } |
| 4392 | |
| 4393 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4394 | return sCameraService->updateAudioRestriction(); |
| 4395 | } |
| 4396 | |
| 4397 | int32_t CameraService::BasicClient::getAudioRestriction() const { |
| 4398 | Mutex::Autolock l(mAudioRestrictionLock); |
| 4399 | return mAudioRestriction; |
| 4400 | } |
| 4401 | |
| 4402 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { |
| 4403 | switch (mode) { |
| 4404 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: |
| 4405 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: |
| 4406 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: |
| 4407 | return true; |
| 4408 | default: |
| 4409 | return false; |
| 4410 | } |
| 4411 | } |
| 4412 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4413 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { |
| 4414 | if (mode == AppOpsManager::MODE_ERRORED) { |
| 4415 | ALOGI("Camera %s: Access for \"%s\" has been revoked", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4416 | mCameraIdStr.c_str(), mClientPackageName.c_str()); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4417 | return PERMISSION_DENIED; |
| 4418 | } else if (!mUidIsTrusted && mode == AppOpsManager::MODE_IGNORED) { |
| 4419 | // If the calling Uid is trusted (a native service), the AppOpsManager could |
| 4420 | // return MODE_IGNORED. Do not treat such case as error. |
| 4421 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, |
| 4422 | mClientPackageName); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4423 | |
| 4424 | bool isCameraPrivacyEnabled; |
| 4425 | if (flags::camera_privacy_allowlist()) { |
| 4426 | isCameraPrivacyEnabled = sCameraService->isCameraPrivacyEnabled( |
| 4427 | toString16(mClientPackageName), std::string(), mClientPid, mClientUid); |
| 4428 | } else { |
| 4429 | isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4430 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4431 | } |
Jyoti Bhayana | 8143e57 | 2023-01-09 08:46:49 -0800 | [diff] [blame] | 4432 | // We don't want to return EACCESS if the CameraPrivacy is enabled. |
| 4433 | // We prefer to successfully open the camera and perform camera muting |
| 4434 | // or blocking in connectHelper as handleAppOpMode can be called before the |
| 4435 | // connection has been fully established and at that time camera muting |
| 4436 | // capabilities are unknown. |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4437 | if (!isUidActive || !isCameraPrivacyEnabled) { |
| 4438 | ALOGI("Camera %s: Access for \"%s\" has been restricted", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4439 | mCameraIdStr.c_str(), mClientPackageName.c_str()); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4440 | // Return the same error as for device policy manager rejection |
| 4441 | return -EACCES; |
| 4442 | } |
| 4443 | } |
| 4444 | return OK; |
| 4445 | } |
| 4446 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4447 | status_t CameraService::BasicClient::startCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4448 | ATRACE_CALL(); |
| 4449 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4450 | { |
| 4451 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4452 | __FUNCTION__, mClientPackageName.c_str(), mClientUid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4453 | } |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4454 | if (mAppOpsManager != nullptr) { |
| 4455 | // Notify app ops that the camera is not available |
| 4456 | mOpsCallback = new OpsCallback(this); |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4457 | |
| 4458 | if (flags::watch_foreground_changes()) { |
| 4459 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 4460 | toString16(mClientPackageName), |
| 4461 | AppOpsManager::WATCH_FOREGROUND_CHANGES, mOpsCallback); |
| 4462 | } else { |
| 4463 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4464 | toString16(mClientPackageName), mOpsCallback); |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4465 | } |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4466 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4467 | // Just check for camera acccess here on open - delay startOp until |
| 4468 | // camera frames start streaming in startCameraStreamingOps |
| 4469 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4470 | toString16(mClientPackageName)); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4471 | status_t res = handleAppOpMode(mode); |
| 4472 | if (res != OK) { |
| 4473 | return res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4474 | } |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 4475 | } |
| 4476 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4477 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4478 | |
| 4479 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4480 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4481 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4482 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid, /*openCamera*/true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4483 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4484 | // Notify listeners of camera open/close status |
| 4485 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); |
| 4486 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4487 | return OK; |
| 4488 | } |
| 4489 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4490 | status_t CameraService::BasicClient::startCameraStreamingOps() { |
| 4491 | ATRACE_CALL(); |
| 4492 | |
| 4493 | if (!mOpsActive) { |
| 4494 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 4495 | return INVALID_OPERATION; |
| 4496 | } |
| 4497 | if (mOpsStreaming) { |
| 4498 | ALOGV("%s: Streaming already active!", __FUNCTION__); |
| 4499 | return OK; |
| 4500 | } |
| 4501 | |
| 4502 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4503 | __FUNCTION__, mClientPackageName.c_str(), mClientUid); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4504 | |
| 4505 | if (mAppOpsManager != nullptr) { |
| 4506 | int32_t mode = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4507 | toString16(mClientPackageName), /*startIfModeDefault*/ false, |
| 4508 | toString16(mClientFeatureId), |
| 4509 | toString16("start camera ") + toString16(mCameraIdStr)); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4510 | status_t res = handleAppOpMode(mode); |
| 4511 | if (res != OK) { |
| 4512 | return res; |
| 4513 | } |
| 4514 | } |
| 4515 | |
| 4516 | mOpsStreaming = true; |
| 4517 | |
| 4518 | return OK; |
| 4519 | } |
| 4520 | |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4521 | status_t CameraService::BasicClient::noteAppOp() { |
| 4522 | ATRACE_CALL(); |
| 4523 | |
| 4524 | ALOGV("%s: Start camera noteAppOp, package name = %s, client UID = %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4525 | __FUNCTION__, mClientPackageName.c_str(), mClientUid); |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4526 | |
| 4527 | // noteAppOp is only used for when camera mute is not supported, in order |
| 4528 | // to trigger the sensor privacy "Unblock" dialog |
| 4529 | if (mAppOpsManager != nullptr) { |
| 4530 | int32_t mode = mAppOpsManager->noteOp(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4531 | toString16(mClientPackageName), toString16(mClientFeatureId), |
| 4532 | toString16("start camera ") + toString16(mCameraIdStr)); |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4533 | status_t res = handleAppOpMode(mode); |
| 4534 | if (res != OK) { |
| 4535 | return res; |
| 4536 | } |
| 4537 | } |
| 4538 | |
| 4539 | return OK; |
| 4540 | } |
| 4541 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4542 | status_t CameraService::BasicClient::finishCameraStreamingOps() { |
| 4543 | ATRACE_CALL(); |
| 4544 | |
| 4545 | if (!mOpsActive) { |
| 4546 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 4547 | return INVALID_OPERATION; |
| 4548 | } |
| 4549 | if (!mOpsStreaming) { |
| 4550 | ALOGV("%s: Streaming not active!", __FUNCTION__); |
| 4551 | return OK; |
| 4552 | } |
| 4553 | |
| 4554 | if (mAppOpsManager != nullptr) { |
| 4555 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4556 | toString16(mClientPackageName), toString16(mClientFeatureId)); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4557 | mOpsStreaming = false; |
| 4558 | } |
| 4559 | |
| 4560 | return OK; |
| 4561 | } |
| 4562 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4563 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4564 | ATRACE_CALL(); |
| 4565 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4566 | if (mOpsStreaming) { |
| 4567 | // Make sure we've notified everyone about camera stopping |
| 4568 | finishCameraStreamingOps(); |
| 4569 | } |
| 4570 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4571 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4572 | if (mOpsActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4573 | mOpsActive = false; |
| 4574 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 4575 | // This function is called when a client disconnects. This should |
| 4576 | // release the camera, but actually only if it was in a proper |
| 4577 | // functional state, i.e. with status NOT_AVAILABLE |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4578 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 4579 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4580 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4581 | // 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] | 4582 | sCameraService->updateStatus(StatusInternal::PRESENT, |
| 4583 | mCameraIdStr, rejected); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4584 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4585 | // Always stop watching, even if no camera op is active |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4586 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 4587 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 4588 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4589 | mOpsCallback.clear(); |
| 4590 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4591 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid, /*closeCamera*/true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4592 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4593 | // Notify listeners of camera open/close status |
| 4594 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); |
| 4595 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4596 | return OK; |
| 4597 | } |
| 4598 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4599 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4600 | ATRACE_CALL(); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4601 | if (mAppOpsManager == nullptr) { |
| 4602 | return; |
| 4603 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 4604 | // TODO : add offline camera session case |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4605 | if (op != AppOpsManager::OP_CAMERA) { |
| 4606 | ALOGW("Unexpected app ops notification received: %d", op); |
| 4607 | return; |
| 4608 | } |
| 4609 | |
| 4610 | int32_t res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4611 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4612 | mClientUid, toString16(mClientPackageName)); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4613 | ALOGV("checkOp returns: %d, %s ", res, |
| 4614 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 4615 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 4616 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 4617 | "UNKNOWN"); |
| 4618 | |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4619 | if (res == AppOpsManager::MODE_ERRORED) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4620 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.c_str(), |
| 4621 | mClientPackageName.c_str()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4622 | block(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4623 | } else if (res == AppOpsManager::MODE_IGNORED) { |
| 4624 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4625 | |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4626 | // Uid may be active, but not visible to the user (e.g. PROCESS_STATE_FOREGROUND_SERVICE). |
| 4627 | // If not visible, but still active, then we want to block instead of muting the camera. |
| 4628 | int32_t procState = sCameraService->mUidPolicy->getProcState(mClientUid); |
| 4629 | bool isUidVisible = (procState <= ActivityManager::PROCESS_STATE_BOUND_TOP); |
| 4630 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4631 | bool isCameraPrivacyEnabled; |
| 4632 | if (flags::camera_privacy_allowlist()) { |
| 4633 | isCameraPrivacyEnabled = sCameraService->isCameraPrivacyEnabled( |
| 4634 | toString16(mClientPackageName),std::string(),mClientPid,mClientUid); |
| 4635 | } else { |
| 4636 | isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4637 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4638 | } |
| 4639 | |
| 4640 | 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] | 4641 | " isUidVisible %d, isCameraPrivacyEnabled %d", mCameraIdStr.c_str(), |
| 4642 | mClientPackageName.c_str(), mUidIsTrusted, isUidActive, isUidVisible, |
| 4643 | isCameraPrivacyEnabled); |
| 4644 | // If the calling Uid is trusted (a native service), or the client Uid is active / visible |
| 4645 | // (WAR for b/175320666)the AppOpsManager could return MODE_IGNORED. Do not treat such |
| 4646 | // cases as error. |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4647 | if (!mUidIsTrusted) { |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4648 | if (flags::watch_foreground_changes()) { |
| 4649 | if (isUidVisible && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 4650 | setCameraMute(true); |
| 4651 | } else { |
| 4652 | block(); |
| 4653 | } |
| 4654 | } else { |
| 4655 | if (isUidActive && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 4656 | setCameraMute(true); |
| 4657 | } else if (!isUidActive |
| 4658 | || (isCameraPrivacyEnabled && !supportsCameraMute())) { |
| 4659 | block(); |
| 4660 | } |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4661 | } |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4662 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4663 | } else if (res == AppOpsManager::MODE_ALLOWED) { |
| 4664 | setCameraMute(sCameraService->mOverrideCameraMuteMode); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4665 | } |
| 4666 | } |
| 4667 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4668 | void CameraService::BasicClient::block() { |
| 4669 | ATRACE_CALL(); |
| 4670 | |
| 4671 | // Reset the client PID to allow server-initiated disconnect, |
| 4672 | // and to prevent further calls by client. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4673 | mClientPid = getCallingPid(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4674 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 4675 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); |
| 4676 | disconnect(); |
| 4677 | } |
| 4678 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4679 | // ---------------------------------------------------------------------------- |
| 4680 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4681 | void CameraService::Client::notifyError(int32_t errorCode, |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 4682 | [[maybe_unused]] const CaptureResultExtras& resultExtras) { |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 4683 | if (mRemoteCallback != NULL) { |
Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 4684 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; |
| 4685 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { |
| 4686 | api1ErrorCode = CAMERA_ERROR_DISABLED; |
| 4687 | } |
| 4688 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 4689 | } else { |
| 4690 | ALOGE("mRemoteCallback is NULL!!"); |
| 4691 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4692 | } |
| 4693 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 4694 | // NOTE: function is idempotent |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4695 | binder::Status CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4696 | ALOGV("Client::disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4697 | return BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 4698 | } |
| 4699 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 4700 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 4701 | return level == API_1; |
| 4702 | } |
| 4703 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4704 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 4705 | mClient(client) { |
| 4706 | } |
| 4707 | |
| 4708 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 4709 | const String16& packageName) { |
| 4710 | sp<BasicClient> client = mClient.promote(); |
| 4711 | if (client != NULL) { |
| 4712 | client->opChanged(op, packageName); |
| 4713 | } |
| 4714 | } |
| 4715 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4716 | // ---------------------------------------------------------------------------- |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4717 | // UidPolicy |
| 4718 | // ---------------------------------------------------------------------------- |
| 4719 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4720 | void CameraService::UidPolicy::registerWithActivityManager() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4721 | Mutex::Autolock _l(mUidLock); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4722 | int32_t emptyUidArray[] = { }; |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4723 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4724 | if (mRegistered) return; |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 4725 | status_t res = mAm.linkToDeath(this); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4726 | mAm.registerUidObserverForUids(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4727 | | ActivityManager::UID_OBSERVER_IDLE |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4728 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE |
| 4729 | | ActivityManager::UID_OBSERVER_PROC_OOM_ADJ, |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4730 | ActivityManager::PROCESS_STATE_UNKNOWN, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4731 | toString16(kServiceName), emptyUidArray, 0, mObserverToken); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4732 | if (res == OK) { |
| 4733 | mRegistered = true; |
| 4734 | ALOGV("UidPolicy: Registered with ActivityManager"); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4735 | } else { |
| 4736 | ALOGE("UidPolicy: Failed to register with ActivityManager: 0x%08x", res); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4737 | } |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4738 | } |
| 4739 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4740 | void CameraService::UidPolicy::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4741 | if (name != toString16(kActivityServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4742 | return; |
| 4743 | } |
| 4744 | |
| 4745 | registerWithActivityManager(); |
| 4746 | } |
| 4747 | |
| 4748 | void CameraService::UidPolicy::registerSelf() { |
| 4749 | // Use check service to see if the activity service is available |
| 4750 | // If not available then register for notifications, instead of blocking |
| 4751 | // till the service is ready |
| 4752 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4753 | sp<IBinder> binder = sm->checkService(toString16(kActivityServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4754 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4755 | sm->registerForNotifications(toString16(kActivityServiceName), this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4756 | } else { |
| 4757 | registerWithActivityManager(); |
| 4758 | } |
| 4759 | } |
| 4760 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4761 | void CameraService::UidPolicy::unregisterSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4762 | Mutex::Autolock _l(mUidLock); |
| 4763 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 4764 | mAm.unregisterUidObserver(this); |
| 4765 | mAm.unlinkToDeath(this); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4766 | mRegistered = false; |
| 4767 | mActiveUids.clear(); |
| 4768 | ALOGV("UidPolicy: Unregistered with ActivityManager"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { |
| 4772 | onUidIdle(uid, disabled); |
| 4773 | } |
| 4774 | |
| 4775 | void CameraService::UidPolicy::onUidActive(uid_t uid) { |
| 4776 | Mutex::Autolock _l(mUidLock); |
| 4777 | mActiveUids.insert(uid); |
| 4778 | } |
| 4779 | |
| 4780 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { |
| 4781 | bool deleted = false; |
| 4782 | { |
| 4783 | Mutex::Autolock _l(mUidLock); |
| 4784 | if (mActiveUids.erase(uid) > 0) { |
| 4785 | deleted = true; |
| 4786 | } |
| 4787 | } |
| 4788 | if (deleted) { |
| 4789 | sp<CameraService> service = mService.promote(); |
| 4790 | if (service != nullptr) { |
| 4791 | service->blockClientsForUid(uid); |
| 4792 | } |
| 4793 | } |
| 4794 | } |
| 4795 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4796 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 4797 | int64_t procStateSeq __unused, int32_t capability __unused) { |
Austin Borger | c5585dc | 2022-05-12 00:48:17 +0000 | [diff] [blame] | 4798 | bool procStateChange = false; |
| 4799 | { |
| 4800 | Mutex::Autolock _l(mUidLock); |
| 4801 | if (mMonitoredUids.find(uid) != mMonitoredUids.end() && |
| 4802 | mMonitoredUids[uid].procState != procState) { |
| 4803 | mMonitoredUids[uid].procState = procState; |
| 4804 | procStateChange = true; |
| 4805 | } |
| 4806 | } |
| 4807 | |
| 4808 | if (procStateChange) { |
| 4809 | sp<CameraService> service = mService.promote(); |
| 4810 | if (service != nullptr) { |
| 4811 | service->notifyMonitoredUids(); |
| 4812 | } |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4813 | } |
| 4814 | } |
| 4815 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4816 | /** |
| 4817 | * When the OOM adj of the uid owning the camera changes, a different uid waiting on camera |
| 4818 | * privileges may take precedence if the owner's new OOM adj is greater than the waiting package. |
| 4819 | * Here, we track which monitoredUid has the camera, and track its adj relative to other |
| 4820 | * monitoredUids. If it is revised above some other monitoredUid, signal |
| 4821 | * onCameraAccessPrioritiesChanged. This only needs to capture the case where there are two |
| 4822 | * foreground apps in split screen - state changes will capture all other cases. |
| 4823 | */ |
| 4824 | void CameraService::UidPolicy::onUidProcAdjChanged(uid_t uid, int32_t adj) { |
| 4825 | std::unordered_set<uid_t> notifyUidSet; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4826 | { |
| 4827 | Mutex::Autolock _l(mUidLock); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4828 | auto it = mMonitoredUids.find(uid); |
| 4829 | |
| 4830 | if (it != mMonitoredUids.end()) { |
| 4831 | if (it->second.hasCamera) { |
| 4832 | for (auto &monitoredUid : mMonitoredUids) { |
| 4833 | if (monitoredUid.first != uid && adj > monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4834 | ALOGV("%s: notify uid %d", __FUNCTION__, monitoredUid.first); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4835 | notifyUidSet.emplace(monitoredUid.first); |
| 4836 | } |
| 4837 | } |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4838 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4839 | notifyUidSet.emplace(uid); |
| 4840 | } else { |
| 4841 | for (auto &monitoredUid : mMonitoredUids) { |
| 4842 | if (monitoredUid.second.hasCamera && adj < monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4843 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4844 | notifyUidSet.emplace(uid); |
| 4845 | } |
| 4846 | } |
| 4847 | } |
| 4848 | it->second.procAdj = adj; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4849 | } |
| 4850 | } |
| 4851 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4852 | if (notifyUidSet.size() > 0) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4853 | sp<CameraService> service = mService.promote(); |
| 4854 | if (service != nullptr) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4855 | service->notifyMonitoredUids(notifyUidSet); |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4856 | } |
| 4857 | } |
| 4858 | } |
| 4859 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4860 | /** |
| 4861 | * Register a uid for monitoring, and note whether it owns a camera. |
| 4862 | */ |
| 4863 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid, bool openCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4864 | Mutex::Autolock _l(mUidLock); |
| 4865 | auto it = mMonitoredUids.find(uid); |
| 4866 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4867 | it->second.refCount++; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4868 | } else { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4869 | MonitoredUid monitoredUid; |
| 4870 | monitoredUid.procState = ActivityManager::PROCESS_STATE_NONEXISTENT; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4871 | monitoredUid.procAdj = resource_policy::UNKNOWN_ADJ; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4872 | monitoredUid.refCount = 1; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4873 | it = mMonitoredUids.emplace(std::pair<uid_t, MonitoredUid>(uid, monitoredUid)).first; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4874 | status_t res = mAm.addUidToObserver(mObserverToken, toString16(kServiceName), uid); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4875 | if (res != OK) { |
| 4876 | ALOGE("UidPolicy: Failed to add uid to observer: 0x%08x", res); |
| 4877 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4878 | } |
| 4879 | |
| 4880 | if (openCamera) { |
| 4881 | it->second.hasCamera = true; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4882 | } |
| 4883 | } |
| 4884 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4885 | /** |
| 4886 | * Unregister a uid for monitoring, and note whether it lost ownership of a camera. |
| 4887 | */ |
| 4888 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid, bool closeCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4889 | Mutex::Autolock _l(mUidLock); |
| 4890 | auto it = mMonitoredUids.find(uid); |
| 4891 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4892 | it->second.refCount--; |
| 4893 | if (it->second.refCount == 0) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4894 | mMonitoredUids.erase(it); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4895 | status_t res = mAm.removeUidFromObserver(mObserverToken, toString16(kServiceName), uid); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4896 | if (res != OK) { |
| 4897 | ALOGE("UidPolicy: Failed to remove uid from observer: 0x%08x", res); |
| 4898 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4899 | } else if (closeCamera) { |
| 4900 | it->second.hasCamera = false; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4901 | } |
| 4902 | } else { |
| 4903 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 4904 | } |
| 4905 | } |
| 4906 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4907 | bool CameraService::UidPolicy::isUidActive(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4908 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4909 | return isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4910 | } |
| 4911 | |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4912 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; |
| 4913 | static const int64_t kPollUidActiveTimeoutMillis = 50; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4914 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4915 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4916 | // Non-app UIDs are considered always active |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4917 | // If activity manager is unreachable, assume everything is active |
| 4918 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4919 | return true; |
| 4920 | } |
| 4921 | auto it = mOverrideUids.find(uid); |
| 4922 | if (it != mOverrideUids.end()) { |
| 4923 | return it->second; |
| 4924 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4925 | bool active = mActiveUids.find(uid) != mActiveUids.end(); |
| 4926 | if (!active) { |
| 4927 | // We want active UIDs to always access camera with their first attempt since |
| 4928 | // there is no guarantee the app is robustly written and would retry getting |
| 4929 | // the camera on failure. The inverse case is not a problem as we would take |
| 4930 | // camera away soon once we get the callback that the uid is no longer active. |
| 4931 | ActivityManager am; |
| 4932 | // Okay to access with a lock held as UID changes are dispatched without |
| 4933 | // a lock and we are a higher level component. |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4934 | int64_t startTimeMillis = 0; |
| 4935 | do { |
| 4936 | // TODO: Fix this b/109950150! |
| 4937 | // Okay this is a hack. There is a race between the UID turning active and |
| 4938 | // activity being resumed. The proper fix is very risky, so we temporary add |
| 4939 | // some polling which should happen pretty rarely anyway as the race is hard |
| 4940 | // to hit. |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4941 | active = mActiveUids.find(uid) != mActiveUids.end(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4942 | if (!active) active = am.isUidActive(uid, toString16(callingPackage)); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4943 | if (active) { |
| 4944 | break; |
| 4945 | } |
| 4946 | if (startTimeMillis <= 0) { |
| 4947 | startTimeMillis = uptimeMillis(); |
| 4948 | } |
| 4949 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4950 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4951 | if (remainingTimeMillis <= 0) { |
| 4952 | break; |
| 4953 | } |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4954 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); |
| 4955 | |
| 4956 | mUidLock.unlock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4957 | usleep(remainingTimeMillis * 1000); |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4958 | mUidLock.lock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4959 | } while (true); |
| 4960 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4961 | if (active) { |
| 4962 | // Now that we found out the UID is actually active, cache that |
| 4963 | mActiveUids.insert(uid); |
| 4964 | } |
| 4965 | } |
| 4966 | return active; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4967 | } |
| 4968 | |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4969 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { |
| 4970 | Mutex::Autolock _l(mUidLock); |
| 4971 | return getProcStateLocked(uid); |
| 4972 | } |
| 4973 | |
| 4974 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { |
| 4975 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 4976 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4977 | procState = mMonitoredUids[uid].procState; |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4978 | } |
| 4979 | return procState; |
| 4980 | } |
| 4981 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4982 | void CameraService::UidPolicy::addOverrideUid(uid_t uid, |
| 4983 | const std::string &callingPackage, bool active) { |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4984 | updateOverrideUid(uid, callingPackage, active, true); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4985 | } |
| 4986 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4987 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4988 | updateOverrideUid(uid, callingPackage, false, false); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4989 | } |
| 4990 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4991 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 4992 | Mutex::Autolock _l(mUidLock); |
| 4993 | ALOGV("UidPolicy: ActivityManager has died"); |
| 4994 | mRegistered = false; |
| 4995 | mActiveUids.clear(); |
| 4996 | } |
| 4997 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4998 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, const std::string &callingPackage, |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4999 | bool active, bool insert) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5000 | bool wasActive = false; |
| 5001 | bool isActive = false; |
| 5002 | { |
| 5003 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 5004 | wasActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5005 | mOverrideUids.erase(uid); |
| 5006 | if (insert) { |
| 5007 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 5008 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 5009 | isActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5010 | } |
| 5011 | if (wasActive != isActive && !isActive) { |
| 5012 | sp<CameraService> service = mService.promote(); |
| 5013 | if (service != nullptr) { |
| 5014 | service->blockClientsForUid(uid); |
| 5015 | } |
| 5016 | } |
| 5017 | } |
| 5018 | |
| 5019 | // ---------------------------------------------------------------------------- |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5020 | // SensorPrivacyPolicy |
| 5021 | // ---------------------------------------------------------------------------- |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5022 | |
| 5023 | void CameraService::SensorPrivacyPolicy::registerWithSensorPrivacyManager() |
| 5024 | { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5025 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5026 | if (mRegistered) { |
| 5027 | return; |
| 5028 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5029 | hasCameraPrivacyFeature(); // Called so the result is cached |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5030 | mSpm.addSensorPrivacyListener(this); |
Charles Chen | f075f08 | 2024-03-04 23:32:55 +0000 | [diff] [blame] | 5031 | if (isAutomotiveDevice()) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5032 | mSpm.addToggleSensorPrivacyListener(this); |
| 5033 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5034 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5035 | if (flags::camera_privacy_allowlist()) { |
| 5036 | mCameraPrivacyState = mSpm.getToggleSensorPrivacyState( |
| 5037 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, |
| 5038 | SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 5039 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5040 | status_t res = mSpm.linkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5041 | if (res == OK) { |
| 5042 | mRegistered = true; |
| 5043 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); |
| 5044 | } |
| 5045 | } |
| 5046 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5047 | void CameraService::SensorPrivacyPolicy::onServiceRegistration(const String16& name, |
| 5048 | const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5049 | if (name != toString16(kSensorPrivacyServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5050 | return; |
| 5051 | } |
| 5052 | |
| 5053 | registerWithSensorPrivacyManager(); |
| 5054 | } |
| 5055 | |
| 5056 | void CameraService::SensorPrivacyPolicy::registerSelf() { |
| 5057 | // Use checkservice to see if the sensor_privacy service is available |
| 5058 | // If service is not available then register for notification |
| 5059 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5060 | sp<IBinder> binder = sm->checkService(toString16(kSensorPrivacyServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5061 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5062 | sm->registerForNotifications(toString16(kSensorPrivacyServiceName),this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5063 | } else { |
| 5064 | registerWithSensorPrivacyManager(); |
| 5065 | } |
| 5066 | } |
| 5067 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5068 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { |
| 5069 | Mutex::Autolock _l(mSensorPrivacyLock); |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5070 | mSpm.removeSensorPrivacyListener(this); |
Charles Chen | f075f08 | 2024-03-04 23:32:55 +0000 | [diff] [blame] | 5071 | if (isAutomotiveDevice()) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5072 | mSpm.removeToggleSensorPrivacyListener(this); |
| 5073 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5074 | mSpm.unlinkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5075 | mRegistered = false; |
| 5076 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); |
| 5077 | } |
| 5078 | |
| 5079 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5080 | if (!mRegistered) { |
| 5081 | registerWithSensorPrivacyManager(); |
| 5082 | } |
| 5083 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5084 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5085 | return mSensorPrivacyEnabled; |
| 5086 | } |
| 5087 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5088 | int CameraService::SensorPrivacyPolicy::getCameraPrivacyState() { |
| 5089 | if (!mRegistered) { |
| 5090 | registerWithSensorPrivacyManager(); |
| 5091 | } |
| 5092 | |
| 5093 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5094 | return mCameraPrivacyState; |
| 5095 | } |
| 5096 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5097 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled() { |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5098 | if (!hasCameraPrivacyFeature()) { |
| 5099 | return false; |
| 5100 | } |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5101 | return mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5102 | } |
| 5103 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5104 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled(const String16& packageName) { |
| 5105 | if (!hasCameraPrivacyFeature()) { |
| 5106 | return SensorPrivacyManager::DISABLED; |
| 5107 | } |
| 5108 | return mSpm.isCameraPrivacyEnabled(packageName); |
| 5109 | } |
| 5110 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5111 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged( |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5112 | int toggleType, int sensor, bool enabled) { |
| 5113 | if ((toggleType == SensorPrivacyManager::TOGGLE_TYPE_UNKNOWN) |
| 5114 | && (sensor == SensorPrivacyManager::TOGGLE_SENSOR_UNKNOWN)) { |
| 5115 | { |
| 5116 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5117 | mSensorPrivacyEnabled = enabled; |
| 5118 | } |
| 5119 | // if sensor privacy is enabled then block all clients from accessing the camera |
| 5120 | if (enabled) { |
| 5121 | sp<CameraService> service = mService.promote(); |
| 5122 | if (service != nullptr) { |
| 5123 | service->blockAllClients(); |
| 5124 | } |
| 5125 | } |
| 5126 | } |
| 5127 | return binder::Status::ok(); |
| 5128 | } |
| 5129 | |
| 5130 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyStateChanged( |
| 5131 | int, int sensor, int state) { |
| 5132 | if (!flags::camera_privacy_allowlist() |
| 5133 | || (sensor != SensorPrivacyManager::TOGGLE_SENSOR_CAMERA)) { |
| 5134 | return binder::Status::ok(); |
| 5135 | } |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5136 | { |
| 5137 | Mutex::Autolock _l(mSensorPrivacyLock); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5138 | mCameraPrivacyState = state; |
| 5139 | } |
| 5140 | sp<CameraService> service = mService.promote(); |
| 5141 | if (!service) { |
| 5142 | return binder::Status::ok(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5143 | } |
| 5144 | // 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] | 5145 | if (state == SensorPrivacyManager::ENABLED) { |
| 5146 | service->blockAllClients(); |
Jyoti Bhayana | 54a4b00 | 2024-02-27 15:36:09 -0800 | [diff] [blame] | 5147 | } else if (state == SensorPrivacyManager::ENABLED_EXCEPT_ALLOWLISTED_APPS) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5148 | service->blockPrivacyEnabledClients(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5149 | } |
| 5150 | return binder::Status::ok(); |
| 5151 | } |
| 5152 | |
| 5153 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 5154 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5155 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); |
| 5156 | mRegistered = false; |
| 5157 | } |
| 5158 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5159 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5160 | bool supportsSoftwareToggle = mSpm.supportsSensorToggle( |
| 5161 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 5162 | bool supportsHardwareToggle = mSpm.supportsSensorToggle( |
| 5163 | SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 5164 | return supportsSoftwareToggle || supportsHardwareToggle; |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5165 | } |
| 5166 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5167 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5168 | // CameraState |
| 5169 | // ---------------------------------------------------------------------------- |
| 5170 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5171 | CameraService::CameraState::CameraState(const std::string& id, int cost, |
| 5172 | const std::set<std::string>& conflicting, SystemCameraKind systemCameraKind, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5173 | const std::vector<std::string>& physicalCameras) : mId(id), |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5174 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5175 | mSystemCameraKind(systemCameraKind), mPhysicalCameras(physicalCameras) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5176 | |
| 5177 | CameraService::CameraState::~CameraState() {} |
| 5178 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5179 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5180 | Mutex::Autolock lock(mStatusLock); |
| 5181 | return mStatus; |
| 5182 | } |
| 5183 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5184 | std::vector<std::string> CameraService::CameraState::getUnavailablePhysicalIds() const { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5185 | Mutex::Autolock lock(mStatusLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5186 | std::vector<std::string> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5187 | return res; |
| 5188 | } |
| 5189 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5190 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 5191 | return mShimParams; |
| 5192 | } |
| 5193 | |
| 5194 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 5195 | mShimParams = params; |
| 5196 | } |
| 5197 | |
| 5198 | int CameraService::CameraState::getCost() const { |
| 5199 | return mCost; |
| 5200 | } |
| 5201 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5202 | std::set<std::string> CameraService::CameraState::getConflicting() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5203 | return mConflicting; |
| 5204 | } |
| 5205 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5206 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { |
| 5207 | return mSystemCameraKind; |
| 5208 | } |
| 5209 | |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5210 | bool CameraService::CameraState::containsPhysicalCamera(const std::string& physicalCameraId) const { |
| 5211 | return std::find(mPhysicalCameras.begin(), mPhysicalCameras.end(), physicalCameraId) |
| 5212 | != mPhysicalCameras.end(); |
| 5213 | } |
| 5214 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5215 | bool CameraService::CameraState::addUnavailablePhysicalId(const std::string& physicalId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5216 | Mutex::Autolock lock(mStatusLock); |
| 5217 | auto result = mUnavailablePhysicalIds.insert(physicalId); |
| 5218 | return result.second; |
| 5219 | } |
| 5220 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5221 | bool CameraService::CameraState::removeUnavailablePhysicalId(const std::string& physicalId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5222 | Mutex::Autolock lock(mStatusLock); |
| 5223 | auto count = mUnavailablePhysicalIds.erase(physicalId); |
| 5224 | return count > 0; |
| 5225 | } |
| 5226 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5227 | void CameraService::CameraState::setClientPackage(const std::string& clientPackage) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5228 | Mutex::Autolock lock(mStatusLock); |
| 5229 | mClientPackage = clientPackage; |
| 5230 | } |
| 5231 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5232 | std::string CameraService::CameraState::getClientPackage() const { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5233 | Mutex::Autolock lock(mStatusLock); |
| 5234 | return mClientPackage; |
| 5235 | } |
| 5236 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5237 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5238 | // ClientEventListener |
| 5239 | // ---------------------------------------------------------------------------- |
| 5240 | |
| 5241 | void CameraService::ClientEventListener::onClientAdded( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5242 | const resource_policy::ClientDescriptor<std::string, |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5243 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 5244 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5245 | if (basicClient.get() != nullptr) { |
| 5246 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5247 | notifier.noteStartCamera(toString8(descriptor.getKey()), |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5248 | static_cast<int>(basicClient->getClientUid())); |
| 5249 | } |
| 5250 | } |
| 5251 | |
| 5252 | void CameraService::ClientEventListener::onClientRemoved( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5253 | const resource_policy::ClientDescriptor<std::string, |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5254 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 5255 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5256 | if (basicClient.get() != nullptr) { |
| 5257 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5258 | notifier.noteStopCamera(toString8(descriptor.getKey()), |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5259 | static_cast<int>(basicClient->getClientUid())); |
| 5260 | } |
| 5261 | } |
| 5262 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5263 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5264 | // CameraClientManager |
| 5265 | // ---------------------------------------------------------------------------- |
| 5266 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5267 | CameraService::CameraClientManager::CameraClientManager() { |
| 5268 | setListener(std::make_shared<ClientEventListener>()); |
| 5269 | } |
| 5270 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5271 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 5272 | |
| 5273 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5274 | const std::string& id) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5275 | auto descriptor = get(id); |
| 5276 | if (descriptor == nullptr) { |
| 5277 | return sp<BasicClient>{nullptr}; |
| 5278 | } |
| 5279 | return descriptor->getValue(); |
| 5280 | } |
| 5281 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5282 | std::string CameraService::CameraClientManager::toString() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5283 | auto all = getAll(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5284 | std::ostringstream ret; |
| 5285 | ret << "["; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5286 | bool hasAny = false; |
| 5287 | for (auto& i : all) { |
| 5288 | hasAny = true; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5289 | std::string key = i->getKey(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5290 | int32_t cost = i->getCost(); |
| 5291 | int32_t pid = i->getOwnerId(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 5292 | int32_t score = i->getPriority().getScore(); |
| 5293 | int32_t state = i->getPriority().getState(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5294 | auto conflicting = i->getConflicting(); |
| 5295 | auto clientSp = i->getValue(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5296 | std::string packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 5297 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5298 | if (clientSp.get() != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5299 | packageName = clientSp->getPackageName(); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5300 | uid_t clientUid = clientSp->getClientUid(); |
| 5301 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5302 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5303 | ret << fmt::sprintf("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" |
| 5304 | PRId32 ", State: %" PRId32, key.c_str(), cost, pid, score, state); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5305 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5306 | if (clientSp.get() != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5307 | ret << fmt::sprintf("User Id: %d, ", clientUserId); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5308 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5309 | if (packageName.size() != 0) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5310 | ret << fmt::sprintf("Client Package Name: %s", packageName.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5311 | } |
| 5312 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5313 | ret << ", Conflicting Client Devices: {"; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5314 | for (auto& j : conflicting) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5315 | ret << fmt::sprintf("%s, ", j.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5316 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5317 | ret << "})"; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5318 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5319 | if (hasAny) ret << "\n"; |
| 5320 | ret << "]\n"; |
| 5321 | return std::move(ret.str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5322 | } |
| 5323 | |
| 5324 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5325 | const std::string& key, const sp<BasicClient>& value, int32_t cost, |
| 5326 | const std::set<std::string>& conflictingKeys, int32_t score, int32_t ownerId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5327 | int32_t state, int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5328 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5329 | int32_t score_adj = systemNativeClient ? kSystemNativeClientScore : score; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5330 | int32_t state_adj = systemNativeClient ? kSystemNativeClientState : state; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 5331 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5332 | return std::make_shared<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>>( |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5333 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, |
| 5334 | systemNativeClient, oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5335 | } |
| 5336 | |
| 5337 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 5338 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5339 | int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5340 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 5341 | partial->getConflicting(), partial->getPriority().getScore(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5342 | partial->getOwnerId(), partial->getPriority().getState(), oomScoreOffset, |
| 5343 | systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | // ---------------------------------------------------------------------------- |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5347 | // InjectionStatusListener |
| 5348 | // ---------------------------------------------------------------------------- |
| 5349 | |
| 5350 | void CameraService::InjectionStatusListener::addListener( |
| 5351 | const sp<ICameraInjectionCallback>& callback) { |
| 5352 | Mutex::Autolock lock(mListenerLock); |
| 5353 | if (mCameraInjectionCallback) return; |
| 5354 | status_t res = IInterface::asBinder(callback)->linkToDeath(this); |
| 5355 | if (res == OK) { |
| 5356 | mCameraInjectionCallback = callback; |
| 5357 | } |
| 5358 | } |
| 5359 | |
| 5360 | void CameraService::InjectionStatusListener::removeListener() { |
| 5361 | Mutex::Autolock lock(mListenerLock); |
| 5362 | if (mCameraInjectionCallback == nullptr) { |
| 5363 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 5364 | return; |
| 5365 | } |
| 5366 | IInterface::asBinder(mCameraInjectionCallback)->unlinkToDeath(this); |
| 5367 | mCameraInjectionCallback = nullptr; |
| 5368 | } |
| 5369 | |
| 5370 | void CameraService::InjectionStatusListener::notifyInjectionError( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5371 | const std::string &injectedCamId, status_t err) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5372 | if (mCameraInjectionCallback == nullptr) { |
| 5373 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 5374 | return; |
| 5375 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5376 | |
| 5377 | switch (err) { |
| 5378 | case -ENODEV: |
| 5379 | mCameraInjectionCallback->onInjectionError( |
| 5380 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5381 | ALOGE("No camera device with ID \"%s\" currently available!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5382 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5383 | break; |
| 5384 | case -EBUSY: |
| 5385 | mCameraInjectionCallback->onInjectionError( |
| 5386 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5387 | ALOGE("Higher-priority client using camera, ID \"%s\" currently unavailable!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5388 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5389 | break; |
| 5390 | case DEAD_OBJECT: |
| 5391 | mCameraInjectionCallback->onInjectionError( |
| 5392 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5393 | ALOGE("Camera ID \"%s\" object is dead!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5394 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5395 | break; |
| 5396 | case INVALID_OPERATION: |
| 5397 | mCameraInjectionCallback->onInjectionError( |
| 5398 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5399 | ALOGE("Camera ID \"%s\" encountered an operating or internal error!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5400 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5401 | break; |
| 5402 | case UNKNOWN_TRANSACTION: |
| 5403 | mCameraInjectionCallback->onInjectionError( |
| 5404 | ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED); |
| 5405 | ALOGE("Camera ID \"%s\" method doesn't support!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5406 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5407 | break; |
| 5408 | default: |
| 5409 | mCameraInjectionCallback->onInjectionError( |
| 5410 | ICameraInjectionCallback::ERROR_INJECTION_INVALID_ERROR); |
| 5411 | ALOGE("Unexpected error %s (%d) opening camera \"%s\"!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5412 | strerror(-err), err, injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5413 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5414 | } |
| 5415 | |
| 5416 | void CameraService::InjectionStatusListener::binderDied( |
| 5417 | const wp<IBinder>& /*who*/) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5418 | ALOGV("InjectionStatusListener: ICameraInjectionCallback has died"); |
| 5419 | auto parent = mParent.promote(); |
| 5420 | if (parent != nullptr) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5421 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 5422 | if (clientDescriptor != nullptr) { |
| 5423 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 5424 | baseClientPtr->stopInjection(); |
| 5425 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 5426 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5427 | } |
| 5428 | } |
| 5429 | |
| 5430 | // ---------------------------------------------------------------------------- |
| 5431 | // CameraInjectionSession |
| 5432 | // ---------------------------------------------------------------------------- |
| 5433 | |
| 5434 | binder::Status CameraService::CameraInjectionSession::stopInjection() { |
| 5435 | Mutex::Autolock lock(mInjectionSessionLock); |
| 5436 | auto parent = mParent.promote(); |
| 5437 | if (parent == nullptr) { |
| 5438 | ALOGE("CameraInjectionSession: Parent is gone"); |
| 5439 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SERVICE, |
| 5440 | "Camera service encountered error"); |
| 5441 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5442 | |
| 5443 | status_t res = NO_ERROR; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5444 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 5445 | if (clientDescriptor != nullptr) { |
| 5446 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 5447 | res = baseClientPtr->stopInjection(); |
| 5448 | if (res != OK) { |
| 5449 | ALOGE("CameraInjectionSession: Failed to stop the injection camera!" |
| 5450 | " ret != NO_ERROR: %d", res); |
| 5451 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SESSION, |
| 5452 | "Camera session encountered error"); |
| 5453 | } |
| 5454 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 5455 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5456 | return binder::Status::ok(); |
| 5457 | } |
| 5458 | |
| 5459 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5460 | |
| 5461 | static const int kDumpLockRetries = 50; |
| 5462 | static const int kDumpLockSleep = 60000; |
| 5463 | |
| 5464 | static bool tryLock(Mutex& mutex) |
| 5465 | { |
| 5466 | bool locked = false; |
| 5467 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 5468 | if (mutex.tryLock() == NO_ERROR) { |
| 5469 | locked = true; |
| 5470 | break; |
| 5471 | } |
| 5472 | usleep(kDumpLockSleep); |
| 5473 | } |
| 5474 | return locked; |
| 5475 | } |
| 5476 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5477 | void CameraService::cacheDump() { |
| 5478 | if (mMemFd != -1) { |
| 5479 | const Vector<String16> args; |
| 5480 | ATRACE_CALL(); |
| 5481 | // Acquiring service lock here will avoid the deadlock since |
| 5482 | // cacheDump will not be called during the second disconnect. |
| 5483 | Mutex::Autolock lock(mServiceLock); |
| 5484 | |
| 5485 | Mutex::Autolock l(mCameraStatesLock); |
| 5486 | // Start collecting the info for open sessions and store it in temp file. |
| 5487 | for (const auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5488 | std::string cameraId = state.first; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5489 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 5490 | if (clientDescriptor != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5491 | dprintf(mMemFd, "== Camera device %s dynamic info: ==\n", cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5492 | // Log the current open session info before device is disconnected. |
| 5493 | dumpOpenSessionClientLogs(mMemFd, args, cameraId); |
| 5494 | } |
| 5495 | } |
| 5496 | } |
| 5497 | } |
| 5498 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5499 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 5500 | ATRACE_CALL(); |
| 5501 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5502 | if (checkCallingPermission(toString16(sDumpPermission)) == false) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5503 | dprintf(fd, "Permission Denial: can't dump CameraService from pid=%d, uid=%d\n", |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 5504 | getCallingPid(), |
| 5505 | getCallingUid()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5506 | return NO_ERROR; |
| 5507 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5508 | bool locked = tryLock(mServiceLock); |
| 5509 | // failed to lock - CameraService is probably deadlocked |
| 5510 | if (!locked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5511 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5512 | } |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5513 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5514 | if (!mInitialized) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5515 | dprintf(fd, "!! No camera HAL available !!\n"); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 5516 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5517 | // Dump event log for error information |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5518 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5519 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5520 | if (locked) mServiceLock.unlock(); |
| 5521 | return NO_ERROR; |
| 5522 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5523 | dprintf(fd, "\n== Service global info: ==\n\n"); |
| 5524 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 5525 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 5526 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", |
| 5527 | mNormalDeviceIdsWithoutSystemCamera.size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 5528 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { |
| 5529 | dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); |
| 5530 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5531 | std::string activeClientString = mActiveClientManager.toString(); |
| 5532 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.c_str()); |
| 5533 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).c_str()); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5534 | if (mStreamUseCaseOverrides.size() > 0) { |
| 5535 | dprintf(fd, "Active stream use case overrides:"); |
| 5536 | for (int64_t useCaseOverride : mStreamUseCaseOverrides) { |
| 5537 | dprintf(fd, " %" PRId64, useCaseOverride); |
| 5538 | } |
| 5539 | dprintf(fd, "\n"); |
| 5540 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5541 | |
| 5542 | dumpEventLog(fd); |
| 5543 | |
| 5544 | bool stateLocked = tryLock(mCameraStatesLock); |
| 5545 | if (!stateLocked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5546 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5547 | } |
| 5548 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5549 | int argSize = args.size(); |
| 5550 | for (int i = 0; i < argSize; i++) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5551 | if (args[i] == toString16(TagMonitor::kMonitorOption)) { |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5552 | if (i + 1 < argSize) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5553 | mMonitorTags = toStdString(args[i + 1]); |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5554 | } |
| 5555 | break; |
| 5556 | } |
| 5557 | } |
| 5558 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5559 | for (auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5560 | const std::string &cameraId = state.first; |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5561 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5562 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.c_str()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5563 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5564 | CameraParameters p = state.second->getShimParams(); |
| 5565 | if (!p.isEmpty()) { |
| 5566 | dprintf(fd, " Camera1 API shim is using parameters:\n "); |
| 5567 | p.dump(fd, args); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5568 | } |
| 5569 | |
| 5570 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 5571 | if (clientDescriptor != nullptr) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5572 | // log the current open session info |
| 5573 | dumpOpenSessionClientLogs(fd, args, cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 5574 | } else { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5575 | dumpClosedSessionClientLogs(fd, cameraId); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5576 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5577 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | if (stateLocked) mCameraStatesLock.unlock(); |
| 5581 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5582 | if (locked) mServiceLock.unlock(); |
| 5583 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 5584 | mCameraProviderManager->dump(fd, args); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5585 | |
| 5586 | dprintf(fd, "\n== Vendor tags: ==\n\n"); |
| 5587 | |
| 5588 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 5589 | if (desc == NULL) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 5590 | sp<VendorTagDescriptorCache> cache = |
| 5591 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 5592 | if (cache == NULL) { |
| 5593 | dprintf(fd, "No vendor tags.\n"); |
| 5594 | } else { |
| 5595 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 5596 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5597 | } else { |
| 5598 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 5599 | } |
| 5600 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5601 | // Dump camera traces if there were any |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5602 | dprintf(fd, "\n"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5603 | camera3::CameraTraces::dump(fd); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5604 | |
| 5605 | // Process dump arguments, if any |
| 5606 | int n = args.size(); |
| 5607 | String16 verboseOption("-v"); |
| 5608 | String16 unreachableOption("--unreachable"); |
| 5609 | for (int i = 0; i < n; i++) { |
| 5610 | if (args[i] == verboseOption) { |
| 5611 | // change logging level |
| 5612 | if (i + 1 >= n) continue; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5613 | std::string levelStr = toStdString(args[i+1]); |
| 5614 | int level = atoi(levelStr.c_str()); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5615 | dprintf(fd, "\nSetting log level to %d.\n", level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5616 | setLogLevel(level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5617 | } else if (args[i] == unreachableOption) { |
| 5618 | // Dump memory analysis |
| 5619 | // TODO - should limit be an argument parameter? |
| 5620 | UnreachableMemoryInfo info; |
| 5621 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); |
| 5622 | if (!success) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5623 | dprintf(fd, "\n== Unable to dump unreachable memory. " |
| 5624 | "Try disabling SELinux enforcement. ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5625 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5626 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5627 | std::string s = info.ToString(/*log_contents*/ true); |
| 5628 | write(fd, s.c_str(), s.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5629 | } |
| 5630 | } |
| 5631 | } |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5632 | |
| 5633 | bool serviceLocked = tryLock(mServiceLock); |
| 5634 | |
| 5635 | // Dump info from previous open sessions. |
| 5636 | // Reposition the offset to beginning of the file before reading |
| 5637 | |
| 5638 | if ((mMemFd >= 0) && (lseek(mMemFd, 0, SEEK_SET) != -1)) { |
| 5639 | dprintf(fd, "\n**********Dumpsys from previous open session**********\n"); |
| 5640 | ssize_t size_read; |
| 5641 | char buf[4096]; |
| 5642 | while ((size_read = read(mMemFd, buf, (sizeof(buf) - 1))) > 0) { |
| 5643 | // Read data from file to a small buffer and write it to fd. |
| 5644 | write(fd, buf, size_read); |
| 5645 | if (size_read == -1) { |
| 5646 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 5647 | break; |
| 5648 | } |
| 5649 | } |
| 5650 | dprintf(fd, "\n**********End of Dumpsys from previous open session**********\n"); |
| 5651 | } else { |
| 5652 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 5653 | } |
| 5654 | |
| 5655 | if (serviceLocked) mServiceLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5656 | return NO_ERROR; |
| 5657 | } |
| 5658 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5659 | void CameraService::dumpOpenSessionClientLogs(int fd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5660 | const Vector<String16>& args, const std::string& cameraId) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5661 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Rucha Katakwar | 71a0e05 | 2022-04-07 15:44:18 -0700 | [diff] [blame] | 5662 | dprintf(fd, " %s : Device %s is open. Client instance dump:\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5663 | getFormattedCurrentTime().c_str(), |
| 5664 | cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5665 | dprintf(fd, " Client priority score: %d state: %d\n", |
| 5666 | clientDescriptor->getPriority().getScore(), |
| 5667 | clientDescriptor->getPriority().getState()); |
| 5668 | dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 5669 | |
| 5670 | auto client = clientDescriptor->getValue(); |
| 5671 | dprintf(fd, " Client package: %s\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5672 | client->getPackageName().c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5673 | |
| 5674 | client->dumpClient(fd, args); |
| 5675 | } |
| 5676 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5677 | void CameraService::dumpClosedSessionClientLogs(int fd, const std::string& cameraId) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5678 | dprintf(fd, " Device %s is closed, no client instance\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5679 | cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5680 | } |
| 5681 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5682 | void CameraService::dumpEventLog(int fd) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5683 | 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] | 5684 | |
| 5685 | Mutex::Autolock l(mLogLock); |
| 5686 | for (const auto& msg : mEventLog) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5687 | dprintf(fd, " %s\n", msg.c_str()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5691 | dprintf(fd, " ...\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5692 | } else if (mEventLog.size() == 0) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5693 | dprintf(fd, " [no events yet]\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5694 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5695 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5696 | } |
| 5697 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5698 | void CameraService::cacheClientTagDumpIfNeeded(const std::string &cameraId, BasicClient* client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5699 | Mutex::Autolock lock(mLogLock); |
| 5700 | if (!isClientWatchedLocked(client)) { return; } |
| 5701 | |
| 5702 | std::vector<std::string> dumpVector; |
| 5703 | client->dumpWatchedEventsToVector(dumpVector); |
| 5704 | |
| 5705 | if (dumpVector.empty()) { return; } |
| 5706 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5707 | std::ostringstream dumpString; |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 5708 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5709 | std::string currentTime = getFormattedCurrentTime(); |
| 5710 | dumpString << "Cached @ "; |
| 5711 | dumpString << currentTime; |
| 5712 | dumpString << "\n"; // First line is the timestamp of when client is cached. |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 5713 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5714 | size_t i = dumpVector.size(); |
| 5715 | |
| 5716 | // Store the string in reverse order (latest last) |
| 5717 | while (i > 0) { |
| 5718 | i--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5719 | dumpString << cameraId; |
| 5720 | dumpString << ":"; |
| 5721 | dumpString << client->getPackageName(); |
| 5722 | dumpString << " "; |
| 5723 | dumpString << dumpVector[i]; // implicitly ends with '\n' |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5724 | } |
| 5725 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5726 | mWatchedClientsDumpCache[client->getPackageName()] = dumpString.str(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5727 | } |
| 5728 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5729 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5730 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5731 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 5732 | if (mTorchClientMap[i] == who) { |
| 5733 | // turn off the torch mode that was turned on by dead client |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5734 | std::string cameraId = mTorchClientMap.keyAt(i); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5735 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 5736 | if (res) { |
| 5737 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 5738 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 5739 | return; |
| 5740 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5741 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5742 | break; |
| 5743 | } |
| 5744 | } |
| 5745 | } |
| 5746 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5747 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 5748 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 5749 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 5750 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 5751 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 5752 | */ |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 5753 | // PID here is approximate and can be wrong. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 5754 | logClientDied(getCallingPid(), "Binder died unexpectedly"); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 5755 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5756 | // check torch client |
| 5757 | handleTorchClientBinderDied(who); |
| 5758 | |
| 5759 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5760 | if(!evictClientIdByRemote(who)) { |
| 5761 | 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] | 5762 | return; |
| 5763 | } |
| 5764 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5765 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 5766 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 5767 | } |
| 5768 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5769 | void CameraService::updateStatus(StatusInternal status, const std::string& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5770 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 5771 | } |
| 5772 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5773 | void CameraService::updateStatus(StatusInternal status, const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5774 | std::initializer_list<StatusInternal> rejectSourceStates) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5775 | // Do not lock mServiceLock here or can get into a deadlock from |
| 5776 | // connect() -> disconnect -> updateStatus |
| 5777 | |
| 5778 | auto state = getCameraState(cameraId); |
| 5779 | |
| 5780 | if (state == nullptr) { |
| 5781 | 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] | 5782 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5783 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 5784 | } |
| 5785 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5786 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 5787 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 5788 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5789 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5790 | return; |
| 5791 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5792 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5793 | if (vd_flags::camera_device_awareness() && status == StatusInternal::PRESENT) { |
| 5794 | CameraMetadata cameraInfo; |
| 5795 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
| 5796 | cameraId, false, &cameraInfo, false); |
| 5797 | if (res != OK) { |
| 5798 | ALOGW("%s: Not able to get camera characteristics for camera id %s", |
| 5799 | __FUNCTION__, cameraId.c_str()); |
| 5800 | } else { |
| 5801 | int32_t deviceId = getDeviceId(cameraInfo); |
| 5802 | if (deviceId != kDefaultDeviceId) { |
| 5803 | const auto &lensFacingEntry = cameraInfo.find(ANDROID_LENS_FACING); |
| 5804 | camera_metadata_enum_android_lens_facing_t androidLensFacing = |
| 5805 | static_cast<camera_metadata_enum_android_lens_facing_t>( |
| 5806 | lensFacingEntry.data.u8[0]); |
| 5807 | std::string mappedCameraId; |
| 5808 | if (androidLensFacing == ANDROID_LENS_FACING_BACK) { |
| 5809 | mappedCameraId = kVirtualDeviceBackCameraId; |
| 5810 | } else if (androidLensFacing == ANDROID_LENS_FACING_FRONT) { |
| 5811 | mappedCameraId = kVirtualDeviceFrontCameraId; |
| 5812 | } else { |
| 5813 | ALOGD("%s: Not adding entry for an external camera of a virtual device", |
| 5814 | __func__); |
| 5815 | } |
| 5816 | if (!mappedCameraId.empty()) { |
| 5817 | mVirtualDeviceCameraIdMapper.addCamera(cameraId, deviceId, mappedCameraId); |
| 5818 | } |
| 5819 | } |
| 5820 | } |
| 5821 | } |
| 5822 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5823 | // Collect the logical cameras without holding mStatusLock in updateStatus |
| 5824 | // as that can lead to a deadlock(b/162192331). |
| 5825 | auto logicalCameraIds = getLogicalCameras(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5826 | // 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] | 5827 | // of the listeners with both the mStatusLock and mStatusListenerLock held |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 5828 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5829 | &logicalCameraIds] |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5830 | (const std::string& cameraId, StatusInternal status) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5831 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 5832 | auto [deviceId, mappedCameraId] = |
| 5833 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5834 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5835 | if (status != StatusInternal::ENUMERATING) { |
| 5836 | // Update torch status if it has a flash unit. |
| 5837 | Mutex::Autolock al(mTorchStatusMutex); |
| 5838 | TorchModeStatus torchStatus; |
| 5839 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 5840 | NAME_NOT_FOUND) { |
| 5841 | TorchModeStatus newTorchStatus = |
| 5842 | status == StatusInternal::PRESENT ? |
| 5843 | TorchModeStatus::AVAILABLE_OFF : |
| 5844 | TorchModeStatus::NOT_AVAILABLE; |
| 5845 | if (torchStatus != newTorchStatus) { |
| 5846 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); |
| 5847 | } |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 5848 | } |
| 5849 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5850 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5851 | Mutex::Autolock lock(mStatusListenerLock); |
| 5852 | notifyPhysicalCameraStatusLocked(mapToInterface(status), mappedCameraId, |
| 5853 | logicalCameraIds, deviceKind, deviceId); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5854 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5855 | for (auto& listener : mListenerList) { |
| 5856 | bool isVendorListener = listener->isVendorListener(); |
| 5857 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, |
| 5858 | listener->getListenerPid(), listener->getListenerUid())) { |
| 5859 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 5860 | cameraId.c_str()); |
| 5861 | continue; |
| 5862 | } |
| 5863 | |
| 5864 | auto ret = listener->getListener()->onStatusChanged(mapToInterface(status), |
| 5865 | mappedCameraId, deviceId); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5866 | listener->handleBinderStatus(ret, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5867 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5868 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 5869 | ret.exceptionCode()); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5870 | |
| 5871 | // Only cameras of the default device can be remapped to a different camera |
| 5872 | // (using remapCameraIds method), so do the following only if the camera is |
| 5873 | // associated with the default device. |
| 5874 | if (deviceId == kDefaultDeviceId) { |
| 5875 | // For the default device, also trigger the callbacks for cameras that were |
| 5876 | // remapped to the current cameraId for the specific package that this |
| 5877 | // listener belongs to. |
| 5878 | std::vector<std::string> remappedCameraIds = |
| 5879 | findOriginalIdsForRemappedCameraId(cameraId, |
| 5880 | listener->getListenerUid()); |
| 5881 | for (auto &remappedCameraId: remappedCameraIds) { |
| 5882 | ret = listener->getListener()->onStatusChanged( |
| 5883 | mapToInterface(status), remappedCameraId, kDefaultDeviceId); |
| 5884 | listener->handleBinderStatus(ret, |
| 5885 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", |
| 5886 | __FUNCTION__, listener->getListenerUid(), |
| 5887 | listener->getListenerPid(), ret.exceptionCode()); |
| 5888 | } |
| 5889 | } |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5890 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5891 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 5892 | } |
| 5893 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5894 | void CameraService::updateOpenCloseStatus(const std::string& cameraId, bool open, |
| 5895 | const std::string& clientPackageName) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5896 | auto state = getCameraState(cameraId); |
| 5897 | if (state == nullptr) { |
| 5898 | 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] | 5899 | cameraId.c_str()); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5900 | return; |
| 5901 | } |
| 5902 | if (open) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5903 | state->setClientPackage(clientPackageName); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5904 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5905 | state->setClientPackage(std::string()); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5906 | } |
| 5907 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5908 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 5909 | auto [deviceId, mappedCameraId] = |
| 5910 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 5911 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5912 | Mutex::Autolock lock(mStatusListenerLock); |
| 5913 | |
| 5914 | for (const auto& it : mListenerList) { |
| 5915 | if (!it->isOpenCloseCallbackAllowed()) { |
| 5916 | continue; |
| 5917 | } |
| 5918 | |
| 5919 | binder::Status ret; |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5920 | if (open) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5921 | ret = it->getListener()->onCameraOpened(mappedCameraId, clientPackageName, |
| 5922 | deviceId); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5923 | } else { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5924 | ret = it->getListener()->onCameraClosed(mappedCameraId, deviceId); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5925 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5926 | |
| 5927 | it->handleBinderStatus(ret, |
| 5928 | "%s: Failed to trigger onCameraOpened/onCameraClosed callback for %d:%d: %d", |
| 5929 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5930 | } |
| 5931 | } |
| 5932 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5933 | template<class Func> |
| 5934 | void CameraService::CameraState::updateStatus(StatusInternal status, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5935 | const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5936 | std::initializer_list<StatusInternal> rejectSourceStates, |
| 5937 | Func onStatusUpdatedLocked) { |
| 5938 | Mutex::Autolock lock(mStatusLock); |
| 5939 | StatusInternal oldStatus = mStatus; |
| 5940 | mStatus = status; |
| 5941 | |
| 5942 | if (oldStatus == status) { |
| 5943 | return; |
| 5944 | } |
| 5945 | |
| 5946 | 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] | 5947 | cameraId.c_str(), oldStatus, status); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5948 | |
| 5949 | if (oldStatus == StatusInternal::NOT_PRESENT && |
| 5950 | (status != StatusInternal::PRESENT && |
| 5951 | status != StatusInternal::ENUMERATING)) { |
| 5952 | |
| 5953 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 5954 | __FUNCTION__); |
| 5955 | mStatus = oldStatus; |
| 5956 | return; |
| 5957 | } |
| 5958 | |
| 5959 | /** |
| 5960 | * Sometimes we want to conditionally do a transition. |
| 5961 | * For example if a client disconnects, we want to go to PRESENT |
| 5962 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 5963 | */ |
| 5964 | for (auto& rejectStatus : rejectSourceStates) { |
| 5965 | if (oldStatus == rejectStatus) { |
| 5966 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5967 | "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] | 5968 | mStatus = oldStatus; |
| 5969 | return; |
| 5970 | } |
| 5971 | } |
| 5972 | |
| 5973 | onStatusUpdatedLocked(cameraId, status); |
| 5974 | } |
| 5975 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5976 | status_t CameraService::getTorchStatusLocked( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5977 | const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5978 | TorchModeStatus *status) const { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5979 | if (!status) { |
| 5980 | return BAD_VALUE; |
| 5981 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5982 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 5983 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5984 | // invalid camera ID or the camera doesn't have a flash unit |
| 5985 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5986 | } |
| 5987 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5988 | *status = mTorchStatusMap.valueAt(index); |
| 5989 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5990 | } |
| 5991 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5992 | status_t CameraService::setTorchStatusLocked(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5993 | TorchModeStatus status) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5994 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 5995 | if (index == NAME_NOT_FOUND) { |
| 5996 | return BAD_VALUE; |
| 5997 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5998 | mTorchStatusMap.editValueAt(index) = status; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5999 | |
| 6000 | return OK; |
| 6001 | } |
| 6002 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6003 | std::list<std::string> CameraService::getLogicalCameras( |
| 6004 | const std::string& physicalCameraId) { |
| 6005 | std::list<std::string> retList; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 6006 | Mutex::Autolock lock(mCameraStatesLock); |
| 6007 | for (const auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6008 | if (state.second->containsPhysicalCamera(physicalCameraId)) { |
| 6009 | retList.emplace_back(state.first); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 6010 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 6011 | } |
| 6012 | return retList; |
| 6013 | } |
| 6014 | |
| 6015 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6016 | const std::string& physicalCameraId, const std::list<std::string>& logicalCameraIds, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 6017 | SystemCameraKind deviceKind, int32_t deviceId) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 6018 | // mStatusListenerLock is expected to be locked |
| 6019 | for (const auto& logicalCameraId : logicalCameraIds) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 6020 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 6021 | // Note: we check only the deviceKind of the physical camera id |
| 6022 | // since, logical camera ids and their physical camera ids are |
| 6023 | // guaranteed to have the same system camera kind. |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 6024 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 6025 | listener->getListenerPid(), listener->getListenerUid())) { |
| 6026 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6027 | physicalCameraId.c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 6028 | continue; |
| 6029 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 6030 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged(status, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 6031 | logicalCameraId, physicalCameraId, deviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 6032 | listener->handleBinderStatus(ret, |
| 6033 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 6034 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 6035 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 6036 | } |
| 6037 | } |
| 6038 | } |
| 6039 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6040 | void CameraService::blockClientsForUid(uid_t uid) { |
| 6041 | const auto clients = mActiveClientManager.getAll(); |
| 6042 | for (auto& current : clients) { |
| 6043 | if (current != nullptr) { |
| 6044 | const auto basicClient = current->getValue(); |
| 6045 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { |
| 6046 | basicClient->block(); |
| 6047 | } |
| 6048 | } |
| 6049 | } |
| 6050 | } |
| 6051 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 6052 | void CameraService::blockAllClients() { |
| 6053 | const auto clients = mActiveClientManager.getAll(); |
| 6054 | for (auto& current : clients) { |
| 6055 | if (current != nullptr) { |
| 6056 | const auto basicClient = current->getValue(); |
| 6057 | if (basicClient.get() != nullptr) { |
| 6058 | basicClient->block(); |
| 6059 | } |
| 6060 | } |
| 6061 | } |
| 6062 | } |
| 6063 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 6064 | void CameraService::blockPrivacyEnabledClients() { |
| 6065 | const auto clients = mActiveClientManager.getAll(); |
| 6066 | for (auto& current : clients) { |
| 6067 | if (current != nullptr) { |
| 6068 | const auto basicClient = current->getValue(); |
| 6069 | if (basicClient.get() != nullptr) { |
| 6070 | std::string pkgName = basicClient->getPackageName(); |
| 6071 | bool cameraPrivacyEnabled = |
| 6072 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(toString16(pkgName)); |
| 6073 | if (cameraPrivacyEnabled) { |
| 6074 | basicClient->block(); |
| 6075 | } |
| 6076 | } |
| 6077 | } |
| 6078 | } |
| 6079 | } |
| 6080 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6081 | // NOTE: This is a remote API - make sure all args are validated |
| 6082 | 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] | 6083 | if (!checkCallingPermission(toString16(sManageCameraPermission), nullptr, nullptr)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6084 | return PERMISSION_DENIED; |
| 6085 | } |
| 6086 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 6087 | return BAD_VALUE; |
| 6088 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6089 | if (args.size() >= 3 && args[0] == toString16("set-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6090 | return handleSetUidState(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6091 | } else if (args.size() >= 2 && args[0] == toString16("reset-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6092 | return handleResetUidState(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6093 | } else if (args.size() >= 2 && args[0] == toString16("get-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6094 | return handleGetUidState(args, out, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6095 | } 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] | 6096 | return handleSetRotateAndCrop(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6097 | } 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] | 6098 | return handleGetRotateAndCrop(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6099 | } else if (args.size() >= 2 && args[0] == toString16("set-autoframing")) { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6100 | return handleSetAutoframing(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6101 | } else if (args.size() >= 1 && args[0] == toString16("get-autoframing")) { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6102 | return handleGetAutoframing(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6103 | } else if (args.size() >= 2 && args[0] == toString16("set-image-dump-mask")) { |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6104 | return handleSetImageDumpMask(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6105 | } else if (args.size() >= 1 && args[0] == toString16("get-image-dump-mask")) { |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6106 | return handleGetImageDumpMask(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6107 | } else if (args.size() >= 2 && args[0] == toString16("set-camera-mute")) { |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6108 | return handleSetCameraMute(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6109 | } 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] | 6110 | return handleSetStreamUseCaseOverrides(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6111 | } 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] | 6112 | handleClearStreamUseCaseOverrides(); |
| 6113 | return OK; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6114 | } else if (args.size() >= 1 && args[0] == toString16("set-zoom-override")) { |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6115 | return handleSetZoomOverride(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6116 | } else if (args.size() >= 2 && args[0] == toString16("watch")) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6117 | return handleWatchCommand(args, in, out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6118 | } else if (args.size() >= 2 && args[0] == toString16("set-watchdog")) { |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6119 | return handleSetCameraServiceWatchdog(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6120 | } else if (args.size() >= 4 && args[0] == toString16("remap-camera-id")) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 6121 | return handleCameraIdRemapping(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6122 | } else if (args.size() == 1 && args[0] == toString16("help")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6123 | printHelp(out); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6124 | return OK; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6125 | } |
| 6126 | printHelp(err); |
| 6127 | return BAD_VALUE; |
| 6128 | } |
| 6129 | |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 6130 | status_t CameraService::handleCameraIdRemapping(const Vector<String16>& args, int err) { |
| 6131 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 6132 | if (uid != AID_ROOT) { |
| 6133 | dprintf(err, "Must be adb root\n"); |
| 6134 | return PERMISSION_DENIED; |
| 6135 | } |
| 6136 | if (args.size() != 4) { |
| 6137 | dprintf(err, "Expected format: remap-camera-id <PACKAGE> <Id0> <Id1>\n"); |
| 6138 | return BAD_VALUE; |
| 6139 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6140 | std::string packageName = toStdString(args[1]); |
| 6141 | std::string cameraIdToReplace = toStdString(args[2]); |
| 6142 | std::string cameraIdNew = toStdString(args[3]); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 6143 | remapCameraIds({{packageName, {{cameraIdToReplace, cameraIdNew}}}}); |
| 6144 | return OK; |
| 6145 | } |
| 6146 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6147 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6148 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6149 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6150 | bool active = false; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6151 | if (args[2] == toString16("active")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6152 | active = true; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6153 | } else if ((args[2] != toString16("idle"))) { |
| 6154 | 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] | 6155 | return BAD_VALUE; |
| 6156 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6157 | |
| 6158 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6159 | if (args.size() >= 5 && args[3] == toString16("--user")) { |
| 6160 | userId = atoi(toStdString(args[4]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | uid_t uid; |
| 6164 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
| 6165 | return BAD_VALUE; |
| 6166 | } |
| 6167 | |
| 6168 | mUidPolicy->addOverrideUid(uid, packageName, active); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6169 | return NO_ERROR; |
| 6170 | } |
| 6171 | |
| 6172 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6173 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6174 | |
| 6175 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6176 | if (args.size() >= 4 && args[2] == toString16("--user")) { |
| 6177 | userId = atoi(toStdString(args[3]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6178 | } |
| 6179 | |
| 6180 | uid_t uid; |
| 6181 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6182 | return BAD_VALUE; |
| 6183 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6184 | |
| 6185 | mUidPolicy->removeOverrideUid(uid, packageName); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6186 | return NO_ERROR; |
| 6187 | } |
| 6188 | |
| 6189 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6190 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6191 | |
| 6192 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6193 | if (args.size() >= 4 && args[2] == toString16("--user")) { |
| 6194 | userId = atoi(toStdString(args[3]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6195 | } |
| 6196 | |
| 6197 | uid_t uid; |
| 6198 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6199 | return BAD_VALUE; |
| 6200 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6201 | |
| 6202 | if (mUidPolicy->isUidActive(uid, packageName)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6203 | return dprintf(out, "active\n"); |
| 6204 | } else { |
| 6205 | return dprintf(out, "idle\n"); |
| 6206 | } |
| 6207 | } |
| 6208 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6209 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6210 | int rotateValue = atoi(toStdString(args[1]).c_str()); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6211 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || |
| 6212 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; |
| 6213 | Mutex::Autolock lock(mServiceLock); |
| 6214 | |
| 6215 | mOverrideRotateAndCropMode = rotateValue; |
| 6216 | |
| 6217 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; |
| 6218 | |
| 6219 | const auto clients = mActiveClientManager.getAll(); |
| 6220 | for (auto& current : clients) { |
| 6221 | if (current != nullptr) { |
| 6222 | const auto basicClient = current->getValue(); |
| 6223 | if (basicClient.get() != nullptr) { |
| 6224 | basicClient->setRotateAndCropOverride(rotateValue); |
| 6225 | } |
| 6226 | } |
| 6227 | } |
| 6228 | |
| 6229 | return OK; |
| 6230 | } |
| 6231 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6232 | status_t CameraService::handleSetAutoframing(const Vector<String16>& args) { |
| 6233 | char* end; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6234 | int autoframingValue = (int) strtol(toStdString(args[1]).c_str(), &end, /*base=*/10); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6235 | if ((*end != '\0') || |
| 6236 | (autoframingValue != ANDROID_CONTROL_AUTOFRAMING_OFF && |
| 6237 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_ON && |
| 6238 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_AUTO)) { |
| 6239 | return BAD_VALUE; |
| 6240 | } |
| 6241 | |
| 6242 | Mutex::Autolock lock(mServiceLock); |
| 6243 | mOverrideAutoframingMode = autoframingValue; |
| 6244 | |
| 6245 | if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) return OK; |
| 6246 | |
| 6247 | const auto clients = mActiveClientManager.getAll(); |
| 6248 | for (auto& current : clients) { |
| 6249 | if (current != nullptr) { |
| 6250 | const auto basicClient = current->getValue(); |
| 6251 | if (basicClient.get() != nullptr) { |
| 6252 | basicClient->setAutoframingOverride(autoframingValue); |
| 6253 | } |
| 6254 | } |
| 6255 | } |
| 6256 | |
| 6257 | return OK; |
| 6258 | } |
| 6259 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6260 | status_t CameraService::handleSetCameraServiceWatchdog(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6261 | int enableWatchdog = atoi(toStdString(args[1]).c_str()); |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6262 | |
| 6263 | if (enableWatchdog < 0 || enableWatchdog > 1) return BAD_VALUE; |
| 6264 | |
| 6265 | Mutex::Autolock lock(mServiceLock); |
| 6266 | |
| 6267 | mCameraServiceWatchdogEnabled = enableWatchdog; |
| 6268 | |
| 6269 | const auto clients = mActiveClientManager.getAll(); |
| 6270 | for (auto& current : clients) { |
| 6271 | if (current != nullptr) { |
| 6272 | const auto basicClient = current->getValue(); |
| 6273 | if (basicClient.get() != nullptr) { |
| 6274 | basicClient->setCameraServiceWatchdog(enableWatchdog); |
| 6275 | } |
| 6276 | } |
| 6277 | } |
| 6278 | |
| 6279 | return OK; |
| 6280 | } |
| 6281 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6282 | status_t CameraService::handleGetRotateAndCrop(int out) { |
| 6283 | Mutex::Autolock lock(mServiceLock); |
| 6284 | |
| 6285 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); |
| 6286 | } |
| 6287 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6288 | status_t CameraService::handleGetAutoframing(int out) { |
| 6289 | Mutex::Autolock lock(mServiceLock); |
| 6290 | |
| 6291 | return dprintf(out, "autoframing override: %d\n", mOverrideAutoframingMode); |
| 6292 | } |
| 6293 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6294 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { |
| 6295 | char *endPtr; |
| 6296 | errno = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6297 | std::string maskString = toStdString(args[1]); |
| 6298 | long maskValue = strtol(maskString.c_str(), &endPtr, 10); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6299 | |
| 6300 | if (errno != 0) return BAD_VALUE; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6301 | if (endPtr != maskString.c_str() + maskString.size()) return BAD_VALUE; |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6302 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; |
| 6303 | |
| 6304 | Mutex::Autolock lock(mServiceLock); |
| 6305 | |
| 6306 | mImageDumpMask = maskValue; |
| 6307 | |
| 6308 | return OK; |
| 6309 | } |
| 6310 | |
| 6311 | status_t CameraService::handleGetImageDumpMask(int out) { |
| 6312 | Mutex::Autolock lock(mServiceLock); |
| 6313 | |
| 6314 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); |
| 6315 | } |
| 6316 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6317 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6318 | int muteValue = strtol(toStdString(args[1]).c_str(), nullptr, 10); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6319 | if (errno != 0) return BAD_VALUE; |
| 6320 | |
| 6321 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; |
| 6322 | Mutex::Autolock lock(mServiceLock); |
| 6323 | |
| 6324 | mOverrideCameraMuteMode = (muteValue == 1); |
| 6325 | |
| 6326 | const auto clients = mActiveClientManager.getAll(); |
| 6327 | for (auto& current : clients) { |
| 6328 | if (current != nullptr) { |
| 6329 | const auto basicClient = current->getValue(); |
| 6330 | if (basicClient.get() != nullptr) { |
| 6331 | if (basicClient->supportsCameraMute()) { |
| 6332 | basicClient->setCameraMute(mOverrideCameraMuteMode); |
| 6333 | } |
| 6334 | } |
| 6335 | } |
| 6336 | } |
| 6337 | |
| 6338 | return OK; |
| 6339 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6340 | |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6341 | status_t CameraService::handleSetStreamUseCaseOverrides(const Vector<String16>& args) { |
| 6342 | std::vector<int64_t> useCasesOverride; |
| 6343 | for (size_t i = 1; i < args.size(); i++) { |
| 6344 | int64_t useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6345 | std::string arg = toStdString(args[i]); |
| 6346 | if (arg == "DEFAULT") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6347 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6348 | } else if (arg == "PREVIEW") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6349 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6350 | } else if (arg == "STILL_CAPTURE") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6351 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_STILL_CAPTURE; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6352 | } else if (arg == "VIDEO_RECORD") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6353 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_RECORD; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6354 | } else if (arg == "PREVIEW_VIDEO_STILL") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6355 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW_VIDEO_STILL; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6356 | } else if (arg == "VIDEO_CALL") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6357 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6358 | } else if (arg == "CROPPED_RAW") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6359 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_CROPPED_RAW; |
| 6360 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6361 | ALOGE("%s: Invalid stream use case %s", __FUNCTION__, arg.c_str()); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6362 | return BAD_VALUE; |
| 6363 | } |
| 6364 | useCasesOverride.push_back(useCase); |
| 6365 | } |
| 6366 | |
| 6367 | Mutex::Autolock lock(mServiceLock); |
| 6368 | mStreamUseCaseOverrides = std::move(useCasesOverride); |
| 6369 | |
| 6370 | return OK; |
| 6371 | } |
| 6372 | |
| 6373 | void CameraService::handleClearStreamUseCaseOverrides() { |
| 6374 | Mutex::Autolock lock(mServiceLock); |
| 6375 | mStreamUseCaseOverrides.clear(); |
| 6376 | } |
| 6377 | |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6378 | status_t CameraService::handleSetZoomOverride(const Vector<String16>& args) { |
| 6379 | char* end; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6380 | int zoomOverrideValue = strtol(toStdString(args[1]).c_str(), &end, /*base=*/10); |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6381 | if ((*end != '\0') || |
| 6382 | (zoomOverrideValue != -1 && |
| 6383 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF && |
| 6384 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM)) { |
| 6385 | return BAD_VALUE; |
| 6386 | } |
| 6387 | |
| 6388 | Mutex::Autolock lock(mServiceLock); |
| 6389 | mZoomOverrideValue = zoomOverrideValue; |
| 6390 | |
| 6391 | const auto clients = mActiveClientManager.getAll(); |
| 6392 | for (auto& current : clients) { |
| 6393 | if (current != nullptr) { |
| 6394 | const auto basicClient = current->getValue(); |
| 6395 | if (basicClient.get() != nullptr) { |
| 6396 | if (basicClient->supportsZoomOverride()) { |
| 6397 | basicClient->setZoomOverride(mZoomOverrideValue); |
| 6398 | } |
| 6399 | } |
| 6400 | } |
| 6401 | } |
| 6402 | |
| 6403 | return OK; |
| 6404 | } |
| 6405 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6406 | status_t CameraService::handleWatchCommand(const Vector<String16>& args, int inFd, int outFd) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6407 | if (args.size() >= 3 && args[1] == toString16("start")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6408 | return startWatchingTags(args, outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6409 | } else if (args.size() == 2 && args[1] == toString16("stop")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6410 | return stopWatchingTags(outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6411 | } else if (args.size() == 2 && args[1] == toString16("dump")) { |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6412 | return printWatchedTags(outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6413 | } else if (args.size() >= 2 && args[1] == toString16("live")) { |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6414 | return printWatchedTagsUntilInterrupt(args, inFd, outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6415 | } else if (args.size() == 2 && args[1] == toString16("clear")) { |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6416 | return clearCachedMonitoredTagDumps(outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6417 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6418 | dprintf(outFd, "Camera service watch commands:\n" |
| 6419 | " start -m <comma_separated_tag_list> [-c <comma_separated_client_list>]\n" |
| 6420 | " starts watching the provided tags for clients with provided package\n" |
| 6421 | " recognizes tag shorthands like '3a'\n" |
| 6422 | " 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] | 6423 | " dump dumps the monitoring information and exits\n" |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6424 | " stop stops watching all tags\n" |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6425 | " live [-n <refresh_interval_ms>]\n" |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6426 | " prints the monitored information in real time\n" |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6427 | " Hit return to exit\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6428 | " clear clears all buffers storing information for watch command"); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 6429 | return BAD_VALUE; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6430 | } |
| 6431 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6432 | status_t CameraService::startWatchingTags(const Vector<String16> &args, int outFd) { |
| 6433 | Mutex::Autolock lock(mLogLock); |
| 6434 | size_t tagsIdx; // index of '-m' |
| 6435 | String16 tags(""); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6436 | for (tagsIdx = 2; tagsIdx < args.size() && args[tagsIdx] != toString16("-m"); tagsIdx++); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6437 | if (tagsIdx < args.size() - 1) { |
| 6438 | tags = args[tagsIdx + 1]; |
| 6439 | } else { |
| 6440 | dprintf(outFd, "No tags provided.\n"); |
| 6441 | return BAD_VALUE; |
| 6442 | } |
| 6443 | |
| 6444 | size_t clientsIdx; // index of '-c' |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6445 | // watch all clients if no clients are provided |
| 6446 | String16 clients = toString16(kWatchAllClientsFlag); |
| 6447 | for (clientsIdx = 2; clientsIdx < args.size() && args[clientsIdx] != toString16("-c"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6448 | clientsIdx++); |
| 6449 | if (clientsIdx < args.size() - 1) { |
| 6450 | clients = args[clientsIdx + 1]; |
| 6451 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6452 | parseClientsToWatchLocked(toStdString(clients)); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6453 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6454 | // track tags to initialize future clients with the monitoring information |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6455 | mMonitorTags = toStdString(tags); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6456 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6457 | bool serviceLock = tryLock(mServiceLock); |
| 6458 | int numWatchedClients = 0; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6459 | auto cameraClients = mActiveClientManager.getAll(); |
| 6460 | for (const auto &clientDescriptor: cameraClients) { |
| 6461 | if (clientDescriptor == nullptr) { continue; } |
| 6462 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6463 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6464 | |
| 6465 | if (isClientWatchedLocked(client.get())) { |
| 6466 | client->startWatchingTags(mMonitorTags, outFd); |
| 6467 | numWatchedClients++; |
| 6468 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6469 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6470 | dprintf(outFd, "Started watching %d active clients\n", numWatchedClients); |
| 6471 | |
| 6472 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6473 | return OK; |
| 6474 | } |
| 6475 | |
| 6476 | status_t CameraService::stopWatchingTags(int outFd) { |
| 6477 | // clear mMonitorTags to prevent new clients from monitoring tags at initialization |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6478 | Mutex::Autolock lock(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6479 | mMonitorTags = ""; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6480 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6481 | mWatchedClientPackages.clear(); |
| 6482 | mWatchedClientsDumpCache.clear(); |
| 6483 | |
| 6484 | bool serviceLock = tryLock(mServiceLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6485 | auto cameraClients = mActiveClientManager.getAll(); |
| 6486 | for (const auto &clientDescriptor : cameraClients) { |
| 6487 | if (clientDescriptor == nullptr) { continue; } |
| 6488 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6489 | if (client.get() == nullptr) { continue; } |
| 6490 | client->stopWatchingTags(outFd); |
| 6491 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6492 | dprintf(outFd, "Stopped watching all clients.\n"); |
| 6493 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6494 | return OK; |
| 6495 | } |
| 6496 | |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6497 | status_t CameraService::clearCachedMonitoredTagDumps(int outFd) { |
| 6498 | Mutex::Autolock lock(mLogLock); |
| 6499 | size_t clearedSize = mWatchedClientsDumpCache.size(); |
| 6500 | mWatchedClientsDumpCache.clear(); |
| 6501 | dprintf(outFd, "Cleared tag information of %zu cached clients.\n", clearedSize); |
| 6502 | return OK; |
| 6503 | } |
| 6504 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6505 | status_t CameraService::printWatchedTags(int outFd) { |
| 6506 | Mutex::Autolock logLock(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6507 | std::set<std::string> connectedMonitoredClients; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6508 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6509 | bool printedSomething = false; // tracks if any monitoring information was printed |
| 6510 | // (from either cached or active clients) |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6511 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6512 | bool serviceLock = tryLock(mServiceLock); |
| 6513 | // get all watched clients that are currently connected |
| 6514 | for (const auto &clientDescriptor: mActiveClientManager.getAll()) { |
| 6515 | if (clientDescriptor == nullptr) { continue; } |
| 6516 | |
| 6517 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6518 | if (client.get() == nullptr) { continue; } |
| 6519 | if (!isClientWatchedLocked(client.get())) { continue; } |
| 6520 | |
| 6521 | std::vector<std::string> dumpVector; |
| 6522 | client->dumpWatchedEventsToVector(dumpVector); |
| 6523 | |
| 6524 | size_t printIdx = dumpVector.size(); |
| 6525 | if (printIdx == 0) { |
| 6526 | continue; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6527 | } |
| 6528 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6529 | // Print tag dumps for active client |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6530 | const std::string &cameraId = clientDescriptor->getKey(); |
| 6531 | dprintf(outFd, "Client: %s (active)\n", client->getPackageName().c_str()); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6532 | while(printIdx > 0) { |
| 6533 | printIdx--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6534 | dprintf(outFd, "%s:%s %s", cameraId.c_str(), client->getPackageName().c_str(), |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6535 | dumpVector[printIdx].c_str()); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6536 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6537 | dprintf(outFd, "\n"); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6538 | printedSomething = true; |
| 6539 | |
| 6540 | connectedMonitoredClients.emplace(client->getPackageName()); |
| 6541 | } |
| 6542 | if (serviceLock) { mServiceLock.unlock(); } |
| 6543 | |
| 6544 | // Print entries in mWatchedClientsDumpCache for clients that are not connected |
| 6545 | for (const auto &kv: mWatchedClientsDumpCache) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6546 | const std::string &package = kv.first; |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6547 | if (connectedMonitoredClients.find(package) != connectedMonitoredClients.end()) { |
| 6548 | continue; |
| 6549 | } |
| 6550 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6551 | dprintf(outFd, "Client: %s (cached)\n", package.c_str()); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6552 | dprintf(outFd, "%s\n", kv.second.c_str()); |
| 6553 | printedSomething = true; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6554 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6555 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6556 | if (!printedSomething) { |
| 6557 | dprintf(outFd, "No monitoring information to print.\n"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6558 | } |
| 6559 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6560 | return OK; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6561 | } |
| 6562 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6563 | // Print all events in vector `events' that came after lastPrintedEvent |
| 6564 | void printNewWatchedEvents(int outFd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6565 | const std::string &cameraId, |
| 6566 | const std::string &packageName, |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6567 | const std::vector<std::string> &events, |
| 6568 | const std::string &lastPrintedEvent) { |
| 6569 | if (events.empty()) { return; } |
| 6570 | |
| 6571 | // index of lastPrintedEvent in events. |
| 6572 | // lastPrintedIdx = events.size() if lastPrintedEvent is not in events |
| 6573 | size_t lastPrintedIdx; |
| 6574 | for (lastPrintedIdx = 0; |
| 6575 | lastPrintedIdx < events.size() && lastPrintedEvent != events[lastPrintedIdx]; |
| 6576 | lastPrintedIdx++); |
| 6577 | |
| 6578 | if (lastPrintedIdx == 0) { return; } // early exit if no new event in `events` |
| 6579 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6580 | // print events in chronological order (latest event last) |
| 6581 | size_t idxToPrint = lastPrintedIdx; |
| 6582 | do { |
| 6583 | idxToPrint--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6584 | dprintf(outFd, "%s:%s %s", cameraId.c_str(), packageName.c_str(), |
| 6585 | events[idxToPrint].c_str()); |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6586 | } while (idxToPrint != 0); |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6587 | } |
| 6588 | |
| 6589 | // Returns true if adb shell cmd watch should be interrupted based on data in inFd. The watch |
| 6590 | // command should be interrupted if the user presses the return key, or if user loses any way to |
| 6591 | // signal interrupt. |
| 6592 | // If timeoutMs == 0, this function will always return false |
| 6593 | bool shouldInterruptWatchCommand(int inFd, int outFd, long timeoutMs) { |
| 6594 | struct timeval startTime; |
| 6595 | int startTimeError = gettimeofday(&startTime, nullptr); |
| 6596 | if (startTimeError) { |
| 6597 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 6598 | return true; |
| 6599 | } |
| 6600 | |
| 6601 | const nfds_t numFds = 1; |
| 6602 | struct pollfd pollFd = { .fd = inFd, .events = POLLIN, .revents = 0 }; |
| 6603 | |
| 6604 | struct timeval currTime; |
| 6605 | char buffer[2]; |
| 6606 | while(true) { |
| 6607 | int currTimeError = gettimeofday(&currTime, nullptr); |
| 6608 | if (currTimeError) { |
| 6609 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 6610 | return true; |
| 6611 | } |
| 6612 | |
| 6613 | long elapsedTimeMs = ((currTime.tv_sec - startTime.tv_sec) * 1000L) |
| 6614 | + ((currTime.tv_usec - startTime.tv_usec) / 1000L); |
| 6615 | int remainingTimeMs = (int) (timeoutMs - elapsedTimeMs); |
| 6616 | |
| 6617 | if (remainingTimeMs <= 0) { |
| 6618 | // No user interrupt within timeoutMs, don't interrupt watch command |
| 6619 | return false; |
| 6620 | } |
| 6621 | |
| 6622 | int numFdsUpdated = poll(&pollFd, numFds, remainingTimeMs); |
| 6623 | if (numFdsUpdated < 0) { |
| 6624 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 6625 | return true; |
| 6626 | } |
| 6627 | |
| 6628 | if (numFdsUpdated == 0) { |
| 6629 | // No user input within timeoutMs, don't interrupt watch command |
| 6630 | return false; |
| 6631 | } |
| 6632 | |
| 6633 | if (!(pollFd.revents & POLLIN)) { |
| 6634 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 6635 | return true; |
| 6636 | } |
| 6637 | |
| 6638 | ssize_t sizeRead = read(inFd, buffer, sizeof(buffer) - 1); |
| 6639 | if (sizeRead < 0) { |
| 6640 | dprintf(outFd, "Error reading user input. Exiting.\n"); |
| 6641 | return true; |
| 6642 | } |
| 6643 | |
| 6644 | if (sizeRead == 0) { |
| 6645 | // Reached end of input fd (can happen if input is piped) |
| 6646 | // User has no way to signal an interrupt, so interrupt here |
| 6647 | return true; |
| 6648 | } |
| 6649 | |
| 6650 | if (buffer[0] == '\n') { |
| 6651 | // User pressed return, interrupt watch command. |
| 6652 | return true; |
| 6653 | } |
| 6654 | } |
| 6655 | } |
| 6656 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6657 | status_t CameraService::printWatchedTagsUntilInterrupt(const Vector<String16> &args, |
| 6658 | int inFd, int outFd) { |
| 6659 | // Figure out refresh interval, if present in args |
| 6660 | long refreshTimeoutMs = 1000L; // refresh every 1s by default |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6661 | if (args.size() > 2) { |
| 6662 | size_t intervalIdx; // index of '-n' |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6663 | for (intervalIdx = 2; intervalIdx < args.size() && toString16("-n") != args[intervalIdx]; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6664 | intervalIdx++); |
| 6665 | |
| 6666 | size_t intervalValIdx = intervalIdx + 1; |
| 6667 | if (intervalValIdx < args.size()) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6668 | refreshTimeoutMs = strtol(toStdString(args[intervalValIdx]).c_str(), nullptr, 10); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6669 | if (errno) { return BAD_VALUE; } |
| 6670 | } |
| 6671 | } |
| 6672 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6673 | // Set min timeout of 10ms. This prevents edge cases in polling when timeout of 0 is passed. |
| 6674 | refreshTimeoutMs = refreshTimeoutMs < 10 ? 10 : refreshTimeoutMs; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6675 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6676 | dprintf(outFd, "Press return to exit...\n\n"); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6677 | std::map<std::string, std::string> packageNameToLastEvent; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6678 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6679 | while (true) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6680 | bool serviceLock = tryLock(mServiceLock); |
| 6681 | auto cameraClients = mActiveClientManager.getAll(); |
| 6682 | if (serviceLock) { mServiceLock.unlock(); } |
| 6683 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6684 | for (const auto& clientDescriptor : cameraClients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6685 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6686 | if (clientDescriptor == nullptr) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6687 | |
| 6688 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6689 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6690 | if (!isClientWatchedLocked(client.get())) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6691 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6692 | const std::string &packageName = client->getPackageName(); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6693 | // This also initializes the map entries with an empty string |
| 6694 | const std::string& lastPrintedEvent = packageNameToLastEvent[packageName]; |
| 6695 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6696 | std::vector<std::string> latestEvents; |
| 6697 | client->dumpWatchedEventsToVector(latestEvents); |
| 6698 | |
| 6699 | if (!latestEvents.empty()) { |
| 6700 | printNewWatchedEvents(outFd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6701 | clientDescriptor->getKey(), |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6702 | packageName, |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6703 | latestEvents, |
| 6704 | lastPrintedEvent); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6705 | packageNameToLastEvent[packageName] = latestEvents[0]; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6706 | } |
| 6707 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6708 | if (shouldInterruptWatchCommand(inFd, outFd, refreshTimeoutMs)) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6709 | break; |
| 6710 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6711 | } |
| 6712 | return OK; |
| 6713 | } |
| 6714 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6715 | void CameraService::parseClientsToWatchLocked(const std::string &clients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6716 | mWatchedClientPackages.clear(); |
| 6717 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6718 | std::istringstream iss(clients); |
| 6719 | std::string nextClient; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6720 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6721 | while (std::getline(iss, nextClient, ',')) { |
| 6722 | if (nextClient == kWatchAllClientsFlag) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6723 | // Don't need to track any other package if 'all' is present |
| 6724 | mWatchedClientPackages.clear(); |
| 6725 | mWatchedClientPackages.emplace(kWatchAllClientsFlag); |
| 6726 | break; |
| 6727 | } |
| 6728 | |
| 6729 | // track package names |
| 6730 | mWatchedClientPackages.emplace(nextClient); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6731 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6732 | } |
| 6733 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6734 | status_t CameraService::printHelp(int out) { |
| 6735 | return dprintf(out, "Camera service commands:\n" |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6736 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 6737 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 6738 | " 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] | 6739 | " set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" |
| 6740 | " Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" |
| 6741 | " 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] | 6742 | " set-autoframing <VALUE> overrides the autoframing value for AUTO\n" |
| 6743 | " Valid values 0=false, 1=true, 2=auto\n" |
| 6744 | " get-autoframing returns the current override autoframing value\n" |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6745 | " set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" |
| 6746 | " Valid values 0=OFF, 1=ON for JPEG\n" |
| 6747 | " 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] | 6748 | " set-camera-mute <0/1> enable or disable camera muting\n" |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6749 | " set-stream-use-case-override <usecase1> <usecase2> ... override stream use cases\n" |
| 6750 | " Use cases applied in descending resolutions. So usecase1 is assigned to the\n" |
| 6751 | " largest resolution, usecase2 is assigned to the 2nd largest resolution, and so\n" |
| 6752 | " on. In case the number of usecases is smaller than the number of streams, the\n" |
| 6753 | " last use case is assigned to all the remaining streams. In case of multiple\n" |
| 6754 | " streams with the same resolution, the tie-breaker is (JPEG, RAW, YUV, and PRIV)\n" |
| 6755 | " Valid values are (case sensitive): DEFAULT, PREVIEW, STILL_CAPTURE, VIDEO_RECORD,\n" |
| 6756 | " PREVIEW_VIDEO_STILL, VIDEO_CALL, CROPPED_RAW\n" |
| 6757 | " clear-stream-use-case-override clear the stream use case override\n" |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6758 | " set-zoom-override <-1/0/1> enable or disable zoom override\n" |
| 6759 | " 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] | 6760 | " set-watchdog <VALUE> enables or disables the camera service watchdog\n" |
| 6761 | " Valid values 0=disable, 1=enable\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6762 | " watch <start|stop|dump|print|clear> manages tag monitoring in connected clients\n" |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 6763 | " 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] | 6764 | " help print this message\n"); |
| 6765 | } |
| 6766 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6767 | bool CameraService::isClientWatched(const BasicClient *client) { |
| 6768 | Mutex::Autolock lock(mLogLock); |
| 6769 | return isClientWatchedLocked(client); |
| 6770 | } |
| 6771 | |
| 6772 | bool CameraService::isClientWatchedLocked(const BasicClient *client) { |
| 6773 | return mWatchedClientPackages.find(kWatchAllClientsFlag) != mWatchedClientPackages.end() || |
| 6774 | mWatchedClientPackages.find(client->getPackageName()) != mWatchedClientPackages.end(); |
| 6775 | } |
| 6776 | |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 6777 | int32_t CameraService::updateAudioRestriction() { |
| 6778 | Mutex::Autolock lock(mServiceLock); |
| 6779 | return updateAudioRestrictionLocked(); |
| 6780 | } |
| 6781 | |
| 6782 | int32_t CameraService::updateAudioRestrictionLocked() { |
| 6783 | int32_t mode = 0; |
| 6784 | // iterate through all active client |
| 6785 | for (const auto& i : mActiveClientManager.getAll()) { |
| 6786 | const auto clientSp = i->getValue(); |
| 6787 | mode |= clientSp->getAudioRestriction(); |
| 6788 | } |
| 6789 | |
| 6790 | bool modeChanged = (mAudioRestriction != mode); |
| 6791 | mAudioRestriction = mode; |
| 6792 | if (modeChanged) { |
| 6793 | mAppOps.setCameraAudioRestriction(mode); |
| 6794 | } |
| 6795 | return mode; |
| 6796 | } |
| 6797 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6798 | status_t CameraService::checkIfInjectionCameraIsPresent(const std::string& externalCamId, |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6799 | sp<BasicClient> clientSp) { |
| 6800 | std::unique_ptr<AutoConditionLock> lock = |
| 6801 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 6802 | status_t res = NO_ERROR; |
| 6803 | if ((res = checkIfDeviceIsUsable(externalCamId)) != NO_ERROR) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6804 | ALOGW("Device %s is not usable!", externalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6805 | mInjectionStatusListener->notifyInjectionError( |
| 6806 | externalCamId, UNKNOWN_TRANSACTION); |
| 6807 | clientSp->notifyError( |
| 6808 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 6809 | CaptureResultExtras()); |
| 6810 | |
| 6811 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 6812 | // other clients from connecting in mServiceLockWrapper if held |
| 6813 | mServiceLock.unlock(); |
| 6814 | |
| 6815 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 6816 | int64_t token = clearCallingIdentity(); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6817 | clientSp->disconnect(); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 6818 | restoreCallingIdentity(token); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6819 | |
| 6820 | // Reacquire mServiceLock |
| 6821 | mServiceLock.lock(); |
| 6822 | } |
| 6823 | |
| 6824 | return res; |
| 6825 | } |
| 6826 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 6827 | void CameraService::clearInjectionParameters() { |
| 6828 | { |
| 6829 | Mutex::Autolock lock(mInjectionParametersLock); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6830 | mInjectionInitPending = false; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 6831 | mInjectionInternalCamId = ""; |
| 6832 | } |
| 6833 | mInjectionExternalCamId = ""; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 6834 | mInjectionStatusListener->removeListener(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 6835 | } |
| 6836 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 6837 | } // namespace android |