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 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 958 | *supported = false; |
| 959 | status_t ret = mCameraProviderManager->isSessionConfigurationSupported(cameraId.c_str(), |
| 960 | sessionConfiguration, /*mOverrideForPerfClass*/false, /*checkSessionParams*/true, |
| 961 | supported); |
| 962 | binder::Status res; |
| 963 | switch (ret) { |
| 964 | case OK: |
| 965 | // Expected, do nothing. |
| 966 | break; |
| 967 | case INVALID_OPERATION: { |
| 968 | std::string msg = fmt::sprintf( |
| 969 | "Camera %s: Session configuration query not supported!", |
| 970 | cameraId.c_str()); |
| 971 | ALOGD("%s: %s", __FUNCTION__, msg.c_str()); |
| 972 | res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
| 973 | } |
| 974 | |
| 975 | break; |
| 976 | default: { |
| 977 | std::string msg = fmt::sprintf( "Camera %s: Error: %s (%d)", cameraId.c_str(), |
| 978 | strerror(-ret), ret); |
| 979 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 980 | res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, |
| 981 | msg.c_str()); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | return res; |
| 986 | } |
| 987 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 988 | Status CameraService::getSessionCharacteristics(const std::string& unresolvedCameraId, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 989 | int targetSdkVersion, bool overrideToPortrait, |
| 990 | const SessionConfiguration& sessionConfiguration, int32_t deviceId, int32_t devicePolicy, |
| 991 | /*out*/ CameraMetadata* outMetadata) { |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 992 | ATRACE_CALL(); |
| 993 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 994 | if (outMetadata == nullptr) { |
| 995 | std::string msg = |
| 996 | fmt::sprintf("Camera %s: Invalid 'outMetadata' input!", unresolvedCameraId.c_str()); |
| 997 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 998 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 999 | } |
| 1000 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame^] | 1001 | if (!mInitialized) { |
| 1002 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 1003 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 1004 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 1005 | } |
| 1006 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1007 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame^] | 1008 | devicePolicy, getCallingUid()); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1009 | if (!cameraIdOptional.has_value()) { |
| 1010 | 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^] | 1011 | unresolvedCameraId.c_str(), deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1012 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1013 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1014 | } |
| 1015 | std::string cameraId = cameraIdOptional.value(); |
| 1016 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame^] | 1017 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1018 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1019 | "Unable to retrieve camera" |
| 1020 | "characteristics for system only device %s: ", |
| 1021 | cameraId.c_str()); |
| 1022 | } |
| 1023 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1024 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
| 1025 | mPerfClassPrimaryCameraIds, cameraId, targetSdkVersion); |
| 1026 | |
| 1027 | status_t ret = mCameraProviderManager->getSessionCharacteristics( |
| 1028 | cameraId, sessionConfiguration, overrideForPerfClass, overrideToPortrait, outMetadata); |
| 1029 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1030 | switch (ret) { |
| 1031 | case OK: |
| 1032 | // Expected, no handling needed. |
| 1033 | break; |
| 1034 | case INVALID_OPERATION: { |
| 1035 | std::string msg = fmt::sprintf( |
| 1036 | "Camera %s: Session characteristics query not supported!", |
| 1037 | cameraId.c_str()); |
| 1038 | ALOGD("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame^] | 1039 | logServiceError(msg, CameraService::ERROR_INVALID_OPERATION); |
| 1040 | outMetadata->clear(); |
| 1041 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
| 1042 | } |
| 1043 | break; |
| 1044 | case NAME_NOT_FOUND: { |
| 1045 | std::string msg = fmt::sprintf( |
| 1046 | "Camera %s: Unknown camera ID.", |
| 1047 | cameraId.c_str()); |
| 1048 | ALOGD("%s: %s", __FUNCTION__, msg.c_str()); |
| 1049 | logServiceError(msg, CameraService::ERROR_ILLEGAL_ARGUMENT); |
| 1050 | outMetadata->clear(); |
| 1051 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1052 | } |
| 1053 | break; |
| 1054 | default: { |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame^] | 1055 | std::string msg = fmt::sprintf( |
| 1056 | "Unable to retrieve session characteristics for camera device %s: " |
| 1057 | "Error: %s (%d)", |
| 1058 | cameraId.c_str(), strerror(-ret), ret); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1059 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame^] | 1060 | logServiceError(msg, CameraService::ERROR_INVALID_OPERATION); |
| 1061 | outMetadata->clear(); |
| 1062 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame^] | 1066 | return filterSensitiveMetadataIfNeeded(cameraId, outMetadata); |
| 1067 | } |
| 1068 | |
| 1069 | Status CameraService::filterSensitiveMetadataIfNeeded( |
| 1070 | const std::string& cameraId, CameraMetadata* metadata) { |
| 1071 | int callingPid = getCallingPid(); |
| 1072 | int callingUid = getCallingUid(); |
| 1073 | |
| 1074 | if (callingPid == getpid()) { |
| 1075 | // Caller is cameraserver; no need to remove keys |
| 1076 | return Status::ok(); |
| 1077 | } |
| 1078 | |
| 1079 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1080 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 1081 | ALOGE("%s: Couldn't get camera kind for camera id %s", __FUNCTION__, cameraId.c_str()); |
| 1082 | metadata->clear(); |
| 1083 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1084 | "Unable to retrieve camera kind for device %s", cameraId.c_str()); |
| 1085 | } |
| 1086 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
| 1087 | // Attempting to query system only camera without system camera permission would have |
| 1088 | // failed the shouldRejectSystemCameraConnection in the caller. So if we get here |
| 1089 | // for a system only camera, then the caller has the required permission. |
| 1090 | // No need to remove keys |
| 1091 | return Status::ok(); |
| 1092 | } |
| 1093 | |
| 1094 | std::vector<int32_t> tagsRemoved; |
| 1095 | bool hasCameraPermission = hasPermissionsForCamera(cameraId, callingPid, callingUid); |
| 1096 | if (hasCameraPermission) { |
| 1097 | // Caller has camera permission; no need to remove keys |
| 1098 | return Status::ok(); |
| 1099 | } |
| 1100 | |
| 1101 | status_t ret = metadata->removePermissionEntries( |
| 1102 | mCameraProviderManager->getProviderTagIdLocked(cameraId), &tagsRemoved); |
| 1103 | if (ret != OK) { |
| 1104 | metadata->clear(); |
| 1105 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1106 | "Failed to remove camera characteristics needing camera permission " |
| 1107 | "for device %s:%s (%d)", |
| 1108 | cameraId.c_str(), strerror(-ret), ret); |
| 1109 | } |
| 1110 | |
| 1111 | if (!tagsRemoved.empty()) { |
| 1112 | ret = metadata->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, |
| 1113 | tagsRemoved.data(), tagsRemoved.size()); |
| 1114 | if (ret != OK) { |
| 1115 | metadata->clear(); |
| 1116 | return STATUS_ERROR_FMT( |
| 1117 | ERROR_INVALID_OPERATION, |
| 1118 | "Failed to insert camera keys needing permission for device %s: %s (%d)", |
| 1119 | cameraId.c_str(), strerror(-ret), ret); |
| 1120 | } |
| 1121 | } |
| 1122 | return Status::ok(); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1125 | Status CameraService::parseCameraIdRemapping( |
| 1126 | const hardware::CameraIdRemapping& cameraIdRemapping, |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1127 | /* out */ TCameraIdRemapping* cameraIdRemappingMap) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1128 | std::string packageName; |
| 1129 | std::string cameraIdToReplace, updatedCameraId; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1130 | for (const auto& packageIdRemapping: cameraIdRemapping.packageIdRemappings) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1131 | packageName = packageIdRemapping.packageName; |
malikakash | 0894f5b | 2023-08-10 22:46:47 +0000 | [diff] [blame] | 1132 | if (packageName.empty()) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1133 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1134 | "CameraIdRemapping: Package name cannot be empty"); |
| 1135 | } |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1136 | if (packageIdRemapping.cameraIdsToReplace.size() |
| 1137 | != packageIdRemapping.updatedCameraIds.size()) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1138 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1139 | "CameraIdRemapping: Mismatch in CameraId Remapping lists sizes for package %s", |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1140 | packageName.c_str()); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1141 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1142 | for (size_t i = 0; i < packageIdRemapping.cameraIdsToReplace.size(); i++) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1143 | cameraIdToReplace = packageIdRemapping.cameraIdsToReplace[i]; |
| 1144 | updatedCameraId = packageIdRemapping.updatedCameraIds[i]; |
malikakash | 0894f5b | 2023-08-10 22:46:47 +0000 | [diff] [blame] | 1145 | if (cameraIdToReplace.empty() || updatedCameraId.empty()) { |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1146 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1147 | "CameraIdRemapping: Camera Id cannot be empty for package %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1148 | packageName.c_str()); |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1149 | } |
| 1150 | if (cameraIdToReplace == updatedCameraId) { |
| 1151 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1152 | "CameraIdRemapping: CameraIdToReplace cannot be the same" |
| 1153 | " as updatedCameraId for %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1154 | packageName.c_str()); |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1155 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1156 | |
| 1157 | // Do not allow any camera remapping that involves a virtual camera. |
| 1158 | auto [deviceIdForCameraToReplace, _] = |
| 1159 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair( |
| 1160 | cameraIdToReplace); |
| 1161 | if (deviceIdForCameraToReplace != kDefaultDeviceId) { |
| 1162 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1163 | "CameraIdRemapping: CameraIdToReplace cannot be a virtual camera"); |
| 1164 | } |
| 1165 | [[maybe_unused]] auto [deviceIdForUpdatedCamera, unusedMappedCameraId] = |
| 1166 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(updatedCameraId); |
| 1167 | if (deviceIdForUpdatedCamera != kDefaultDeviceId) { |
| 1168 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1169 | "CameraIdRemapping: UpdatedCameraId cannot be a virtual camera"); |
| 1170 | } |
| 1171 | |
malikakash | 214f6e6 | 2023-08-10 23:50:56 +0000 | [diff] [blame] | 1172 | (*cameraIdRemappingMap)[packageName][cameraIdToReplace] = updatedCameraId; |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | return Status::ok(); |
| 1176 | } |
| 1177 | |
| 1178 | void CameraService::remapCameraIds(const TCameraIdRemapping& cameraIdRemapping) { |
| 1179 | // Acquire mServiceLock and prevent other clients from connecting |
| 1180 | std::unique_ptr<AutoConditionLock> serviceLockWrapper = |
| 1181 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 1182 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1183 | // Collect all existing clients for camera Ids that are being |
| 1184 | // remapped in the new cameraIdRemapping, but only if they were being used by a |
| 1185 | // targeted packageName. |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1186 | std::vector<sp<BasicClient>> clientsToDisconnect; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1187 | std::vector<std::string> cameraIdsToUpdate; |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1188 | for (const auto& [packageName, injectionMap] : cameraIdRemapping) { |
| 1189 | for (auto& [id0, id1] : injectionMap) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1190 | ALOGI("%s: UPDATE:= %s: %s: %s", __FUNCTION__, packageName.c_str(), |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1191 | id0.c_str(), id1.c_str()); |
| 1192 | auto clientDescriptor = mActiveClientManager.get(id0); |
| 1193 | if (clientDescriptor != nullptr) { |
| 1194 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1195 | if (clientSp->getPackageName() == packageName) { |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1196 | // This camera is being used by a targeted packageName and |
| 1197 | // being remapped to a new camera Id. We should disconnect it. |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1198 | clientsToDisconnect.push_back(clientSp); |
| 1199 | cameraIdsToUpdate.push_back(id0); |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1205 | for (auto& clientSp : clientsToDisconnect) { |
malikakash | f4c80f2 | 2023-09-25 21:50:28 +0000 | [diff] [blame] | 1206 | // Notify the clients about the disconnection. |
| 1207 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1208 | CaptureResultExtras{}); |
| 1209 | } |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1210 | |
| 1211 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 1212 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 1213 | mServiceLock.unlock(); |
| 1214 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1215 | // Clear calling identity for disconnect() PID checks. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1216 | int64_t token = clearCallingIdentity(); |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1217 | |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1218 | // Disconnect clients. |
| 1219 | for (auto& clientSp : clientsToDisconnect) { |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1220 | // This also triggers a call to updateStatus() which also reads mCameraIdRemapping |
| 1221 | // and requires mCameraIdRemappingLock. |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1222 | clientSp->disconnect(); |
| 1223 | } |
| 1224 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1225 | // Invoke destructors (which call disconnect()) now while we don't hold the mServiceLock. |
| 1226 | clientsToDisconnect.clear(); |
| 1227 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1228 | restoreCallingIdentity(token); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1229 | mServiceLock.lock(); |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1230 | |
| 1231 | { |
| 1232 | Mutex::Autolock lock(mCameraIdRemappingLock); |
| 1233 | // Update mCameraIdRemapping. |
| 1234 | mCameraIdRemapping.clear(); |
| 1235 | mCameraIdRemapping.insert(cameraIdRemapping.begin(), cameraIdRemapping.end()); |
| 1236 | } |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1239 | Status CameraService::injectSessionParams( |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1240 | const std::string& cameraId, |
| 1241 | const CameraMetadata& sessionParams) { |
| 1242 | if (!checkCallingPermission(toString16(sCameraInjectExternalCameraPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1243 | const int pid = getCallingPid(); |
| 1244 | const int uid = getCallingUid(); |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1245 | ALOGE("%s: Permission Denial: can't inject session params pid=%d, uid=%d", |
| 1246 | __FUNCTION__, pid, uid); |
| 1247 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 1248 | "Permission Denial: no permission to inject session params"); |
| 1249 | } |
| 1250 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1251 | // Do not allow session params injection for a virtual camera. |
| 1252 | auto [deviceId, _] = mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 1253 | if (deviceId != kDefaultDeviceId) { |
| 1254 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1255 | "Cannot inject session params for a virtual camera"); |
| 1256 | } |
| 1257 | |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1258 | std::unique_ptr<AutoConditionLock> serviceLockWrapper = |
| 1259 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 1260 | |
| 1261 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 1262 | if (clientDescriptor == nullptr) { |
| 1263 | ALOGI("%s: No active client for camera id %s", __FUNCTION__, cameraId.c_str()); |
| 1264 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1265 | "No active client for camera id %s", cameraId.c_str()); |
| 1266 | } |
| 1267 | |
| 1268 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1269 | status_t res = clientSp->injectSessionParams(sessionParams); |
| 1270 | |
| 1271 | if (res != OK) { |
| 1272 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1273 | "Error injecting session params into camera \"%s\": %s (%d)", |
| 1274 | cameraId.c_str(), strerror(-res), res); |
| 1275 | } |
| 1276 | return Status::ok(); |
| 1277 | } |
| 1278 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1279 | std::vector<std::string> CameraService::findOriginalIdsForRemappedCameraId( |
| 1280 | const std::string& inputCameraId, int clientUid) { |
| 1281 | std::string packageName = getPackageNameFromUid(clientUid); |
| 1282 | std::vector<std::string> cameraIds; |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1283 | Mutex::Autolock lock(mCameraIdRemappingLock); |
| 1284 | if (auto packageMapIter = mCameraIdRemapping.find(packageName); |
| 1285 | packageMapIter != mCameraIdRemapping.end()) { |
| 1286 | for (auto& [id0, id1]: packageMapIter->second) { |
| 1287 | if (id1 == inputCameraId) { |
| 1288 | cameraIds.push_back(id0); |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | return cameraIds; |
| 1293 | } |
| 1294 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1295 | std::string CameraService::resolveCameraId( |
| 1296 | const std::string& inputCameraId, |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1297 | int clientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1298 | const std::string& packageName) { |
| 1299 | std::string packageNameVal = packageName; |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1300 | if (packageName.empty()) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1301 | packageNameVal = getPackageNameFromUid(clientUid); |
| 1302 | } |
malikakash | 65d2069 | 2023-09-07 01:06:33 +0000 | [diff] [blame] | 1303 | if (clientUid < AID_APP_START || packageNameVal.empty()) { |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1304 | // We shouldn't remap cameras for processes with system/vendor UIDs. |
| 1305 | return inputCameraId; |
| 1306 | } |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1307 | Mutex::Autolock lock(mCameraIdRemappingLock); |
| 1308 | if (auto packageMapIter = mCameraIdRemapping.find(packageNameVal); |
| 1309 | packageMapIter != mCameraIdRemapping.end()) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1310 | auto packageMap = packageMapIter->second; |
| 1311 | if (auto replacementIdIter = packageMap.find(inputCameraId); |
| 1312 | replacementIdIter != packageMap.end()) { |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1313 | ALOGI("%s: resolveCameraId: remapping cameraId %s for %s to %s", |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1314 | __FUNCTION__, inputCameraId.c_str(), |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1315 | packageNameVal.c_str(), |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1316 | replacementIdIter->second.c_str()); |
| 1317 | return replacementIdIter->second; |
| 1318 | } |
| 1319 | } |
| 1320 | return inputCameraId; |
| 1321 | } |
| 1322 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1323 | std::optional<std::string> CameraService::resolveCameraId( |
| 1324 | const std::string& inputCameraId, |
| 1325 | int32_t deviceId, |
| 1326 | int32_t devicePolicy, |
| 1327 | int clientUid, |
| 1328 | const std::string& packageName) { |
| 1329 | if ((deviceId == kDefaultDeviceId) |
| 1330 | || (devicePolicy == IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT)) { |
| 1331 | auto [storedDeviceId, _] = |
| 1332 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(inputCameraId); |
| 1333 | if (storedDeviceId != kDefaultDeviceId) { |
| 1334 | // Trying to access a virtual camera from default-policy device context, we should fail. |
| 1335 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 1336 | inputCameraId.c_str(), deviceId); |
| 1337 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1338 | return std::nullopt; |
| 1339 | } |
| 1340 | return resolveCameraId(inputCameraId, clientUid, packageName); |
| 1341 | } |
| 1342 | |
| 1343 | return mVirtualDeviceCameraIdMapper.getActualCameraId(deviceId, inputCameraId); |
| 1344 | } |
| 1345 | |
| 1346 | Status CameraService::getCameraInfo(int cameraId, bool overrideToPortrait, int32_t deviceId, |
| 1347 | int32_t devicePolicy, CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1348 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 1349 | Mutex::Autolock l(mServiceLock); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1350 | std::string cameraIdStr = cameraIdIntToStrLocked(cameraId, deviceId, devicePolicy); |
| 1351 | if (cameraIdStr.empty()) { |
| 1352 | std::string msg = fmt::sprintf("Camera %d: Invalid camera id for device id %d", |
| 1353 | cameraId, deviceId); |
| 1354 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1355 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1356 | } |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1357 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1358 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1359 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 1360 | "characteristics for system only device %s: ", cameraIdStr.c_str()); |
| 1361 | } |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 1362 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1363 | if (!mInitialized) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1364 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1365 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1366 | "Camera subsystem is not available"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1367 | } |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1368 | bool hasSystemCameraPermissions = hasPermissionsForSystemCamera(std::to_string(cameraId), |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1369 | getCallingPid(), getCallingUid()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1370 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; |
| 1371 | if (hasSystemCameraPermissions) { |
| 1372 | cameraIdBound = mNumberOfCameras; |
| 1373 | } |
| 1374 | if (cameraId < 0 || cameraId >= cameraIdBound) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1375 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1376 | "CameraId is not valid"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1379 | Status ret = Status::ok(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1380 | int portraitRotation; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1381 | status_t err = mCameraProviderManager->getCameraInfo( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1382 | cameraIdStr, overrideToPortrait, &portraitRotation, cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1383 | if (err != OK) { |
| 1384 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1385 | "Error retrieving camera info from device %d: %s (%d)", cameraId, |
| 1386 | strerror(-err), err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1387 | logServiceError(std::string("Error retrieving camera info from device ") |
| 1388 | + std::to_string(cameraId), ERROR_INVALID_OPERATION); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1389 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1390 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1391 | return ret; |
| 1392 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1393 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1394 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt, |
| 1395 | int32_t deviceId, int32_t devicePolicy) { |
| 1396 | if (vd_flags::camera_device_awareness() && (deviceId != kDefaultDeviceId) |
| 1397 | && (devicePolicy != IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT)) { |
| 1398 | std::optional<std::string> cameraIdOptional = |
| 1399 | mVirtualDeviceCameraIdMapper.getActualCameraId(cameraIdInt, deviceId); |
| 1400 | return cameraIdOptional.has_value() ? cameraIdOptional.value() : std::string{}; |
| 1401 | } |
| 1402 | |
| 1403 | const std::vector<std::string> *cameraIds = &mNormalDeviceIdsWithoutSystemCamera; |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1404 | auto callingPid = getCallingPid(); |
| 1405 | auto callingUid = getCallingUid(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1406 | bool systemCameraPermissions = hasPermissionsForSystemCamera(std::to_string(cameraIdInt), |
| 1407 | callingPid, callingUid, /* checkCameraPermissions= */ false); |
| 1408 | if (systemCameraPermissions || getpid() == callingPid) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1409 | cameraIds = &mNormalDeviceIds; |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1410 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1411 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(cameraIds->size())) { |
| 1412 | ALOGE("%s: input id %d invalid: valid range (0, %zu)", |
| 1413 | __FUNCTION__, cameraIdInt, cameraIds->size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1414 | return std::string{}; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1415 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1416 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1417 | std::string unresolvedCameraId = (*cameraIds)[cameraIdInt]; |
| 1418 | return resolveCameraId(unresolvedCameraId, getCallingUid()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1419 | } |
| 1420 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1421 | std::string CameraService::cameraIdIntToStr(int cameraIdInt, int32_t deviceId, |
| 1422 | int32_t devicePolicy) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1423 | Mutex::Autolock lock(mServiceLock); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1424 | return cameraIdIntToStrLocked(cameraIdInt, deviceId, devicePolicy); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1425 | } |
| 1426 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1427 | Status CameraService::getCameraCharacteristics(const std::string& unresolvedCameraId, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1428 | int targetSdkVersion, bool overrideToPortrait, int32_t deviceId, int32_t devicePolicy, |
| 1429 | CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1430 | ATRACE_CALL(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1431 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1432 | if (!cameraInfo) { |
| 1433 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1434 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1437 | if (!mInitialized) { |
| 1438 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1439 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1440 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1441 | "Camera subsystem is not available");; |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1444 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
| 1445 | devicePolicy, getCallingUid()); |
| 1446 | if (!cameraIdOptional.has_value()) { |
| 1447 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 1448 | unresolvedCameraId.c_str(), deviceId); |
| 1449 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1450 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1451 | } |
| 1452 | std::string cameraId = cameraIdOptional.value(); |
| 1453 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1454 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1455 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1456 | "characteristics for system only device %s: ", cameraId.c_str()); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1459 | bool overrideForPerfClass = |
| 1460 | SessionConfigurationUtils::targetPerfClassPrimaryCamera(mPerfClassPrimaryCameraIds, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1461 | cameraId, targetSdkVersion); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1462 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1463 | cameraId, overrideForPerfClass, cameraInfo, overrideToPortrait); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1464 | if (res != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1465 | if (res == NAME_NOT_FOUND) { |
| 1466 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1467 | "characteristics for unknown device %s: %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1468 | strerror(-res), res); |
| 1469 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1470 | logServiceError(fmt::sprintf("Unable to retrieve camera characteristics for device %s.", |
| 1471 | cameraId.c_str()), ERROR_INVALID_OPERATION); |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1472 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1473 | "characteristics for device %s: %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1474 | strerror(-res), res); |
| 1475 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1476 | } |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 1477 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame^] | 1478 | return filterSensitiveMetadataIfNeeded(cameraId, cameraInfo); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1481 | Status CameraService::getTorchStrengthLevel(const std::string& unresolvedCameraId, int32_t deviceId, |
| 1482 | int32_t devicePolicy, int32_t* torchStrength) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1483 | ATRACE_CALL(); |
| 1484 | Mutex::Autolock l(mServiceLock); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1485 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1486 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
| 1487 | devicePolicy, getCallingUid()); |
| 1488 | if (!cameraIdOptional.has_value()) { |
| 1489 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 1490 | unresolvedCameraId.c_str(), deviceId); |
| 1491 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1492 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1493 | } |
| 1494 | std::string cameraId = cameraIdOptional.value(); |
| 1495 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1496 | if (!mInitialized) { |
| 1497 | ALOGE("%s: Camera HAL couldn't be initialized.", __FUNCTION__); |
| 1498 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera HAL couldn't be initialized."); |
| 1499 | } |
| 1500 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1501 | if (torchStrength == NULL) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1502 | ALOGE("%s: strength level must not be null.", __FUNCTION__); |
| 1503 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Strength level should not be null."); |
| 1504 | } |
| 1505 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1506 | status_t res = mCameraProviderManager->getTorchStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1507 | if (res != OK) { |
| 1508 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve torch " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1509 | "strength level for device %s: %s (%d)", cameraId.c_str(), |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1510 | strerror(-res), res); |
| 1511 | } |
| 1512 | ALOGI("%s: Torch strength level is: %d", __FUNCTION__, *torchStrength); |
| 1513 | return Status::ok(); |
| 1514 | } |
| 1515 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1516 | std::string CameraService::getFormattedCurrentTime() { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1517 | time_t now = time(nullptr); |
| 1518 | char formattedTime[64]; |
| 1519 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1520 | return std::string(formattedTime); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1523 | Status CameraService::getCameraVendorTagDescriptor( |
| 1524 | /*out*/ |
| 1525 | hardware::camera2::params::VendorTagDescriptor* desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1526 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1527 | if (!mInitialized) { |
| 1528 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1529 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 1530 | } |
Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 1531 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 1532 | if (globalDescriptor != nullptr) { |
| 1533 | *desc = *(globalDescriptor.get()); |
| 1534 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1535 | return Status::ok(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 1536 | } |
| 1537 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 1538 | Status CameraService::getCameraVendorTagCache( |
| 1539 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { |
| 1540 | ATRACE_CALL(); |
| 1541 | if (!mInitialized) { |
| 1542 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 1543 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1544 | "Camera subsystem not available"); |
| 1545 | } |
| 1546 | sp<VendorTagDescriptorCache> globalCache = |
| 1547 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 1548 | if (globalCache != nullptr) { |
| 1549 | *cache = *(globalCache.get()); |
| 1550 | } |
| 1551 | return Status::ok(); |
| 1552 | } |
| 1553 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 1554 | void CameraService::clearCachedVariables() { |
| 1555 | BasicClient::BasicClient::sCameraService = nullptr; |
| 1556 | } |
| 1557 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1558 | std::pair<int, IPCTransport> CameraService::getDeviceVersion(const std::string& cameraId, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1559 | bool overrideToPortrait, int* portraitRotation, int* facing, int* orientation) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1560 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1561 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1562 | int deviceVersion = 0; |
| 1563 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1564 | status_t res; |
| 1565 | hardware::hidl_version maxVersion{0,0}; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1566 | IPCTransport transport = IPCTransport::INVALID; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1567 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId, &maxVersion, &transport); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1568 | if (res != OK || transport == IPCTransport::INVALID) { |
| 1569 | 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] | 1570 | cameraId.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1571 | return std::make_pair(-1, IPCTransport::INVALID) ; |
| 1572 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1573 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1574 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1575 | hardware::CameraInfo info; |
| 1576 | if (facing) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1577 | res = mCameraProviderManager->getCameraInfo(cameraId, overrideToPortrait, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1578 | portraitRotation, &info); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1579 | if (res != OK) { |
| 1580 | return std::make_pair(-1, IPCTransport::INVALID); |
| 1581 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1582 | *facing = info.facing; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1583 | if (orientation) { |
| 1584 | *orientation = info.orientation; |
| 1585 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1586 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1587 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1588 | return std::make_pair(deviceVersion, transport); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1589 | } |
| 1590 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1591 | Status CameraService::filterGetInfoErrorCode(status_t err) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1592 | switch(err) { |
| 1593 | case NO_ERROR: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1594 | return Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1595 | case BAD_VALUE: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1596 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1597 | "CameraId is not valid for HAL module"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1598 | case NO_INIT: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1599 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1600 | "Camera device not available"); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1601 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1602 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1603 | "Camera HAL encountered error %d: %s", |
| 1604 | err, strerror(-err)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1605 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1606 | } |
| 1607 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1608 | Status CameraService::makeClient(const sp<CameraService>& cameraService, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1609 | const sp<IInterface>& cameraCb, const std::string& packageName, bool systemNativeClient, |
| 1610 | const std::optional<std::string>& featureId, const std::string& cameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 1611 | int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid, |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1612 | int servicePid, std::pair<int, IPCTransport> deviceVersionAndTransport, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1613 | apiLevel effectiveApiLevel, bool overrideForPerfClass, bool overrideToPortrait, |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1614 | bool forceSlowJpegMode, const std::string& originalCameraId, |
| 1615 | /*out*/sp<BasicClient>* client) { |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1616 | // For HIDL devices |
| 1617 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 1618 | // Create CameraClient based on device version reported by the HAL. |
| 1619 | int deviceVersion = deviceVersionAndTransport.first; |
| 1620 | switch(deviceVersion) { |
| 1621 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 1622 | ALOGE("Camera using old HAL version: %d", deviceVersion); |
| 1623 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, |
| 1624 | "Camera device \"%s\" HAL version %d no longer supported", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1625 | cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1626 | break; |
| 1627 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 1628 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 1629 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 1630 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 1631 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 1632 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 1633 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 1634 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 1635 | break; |
| 1636 | default: |
| 1637 | // Should not be reachable |
| 1638 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 1639 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1640 | "Camera device \"%s\" has unknown HAL version %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1641 | cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 1645 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1646 | *client = new Camera2Client(cameraService, tmp, cameraService->mCameraServiceProxyWrapper, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1647 | cameraService->mAttributionAndPermissionUtils, packageName, featureId, cameraId, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1648 | api1CameraId, facing, sensorOrientation, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1649 | clientPid, clientUid, servicePid, overrideForPerfClass, overrideToPortrait, |
| 1650 | forceSlowJpegMode); |
| 1651 | ALOGI("%s: Camera1 API (legacy), override to portrait %d, forceSlowJpegMode %d", |
| 1652 | __FUNCTION__, overrideToPortrait, forceSlowJpegMode); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1653 | } else { // Camera2 API route |
| 1654 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = |
| 1655 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1656 | *client = new CameraDeviceClient(cameraService, tmp, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1657 | cameraService->mCameraServiceProxyWrapper, |
| 1658 | cameraService->mAttributionAndPermissionUtils, packageName, systemNativeClient, |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1659 | featureId, cameraId, facing, sensorOrientation, clientPid, clientUid, servicePid, |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1660 | overrideForPerfClass, overrideToPortrait, originalCameraId); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1661 | ALOGI("%s: Camera2 API, override to portrait %d", __FUNCTION__, overrideToPortrait); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1662 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1663 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1666 | std::string CameraService::toString(std::set<userid_t> intSet) { |
| 1667 | std::ostringstream s; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1668 | bool first = true; |
| 1669 | for (userid_t i : intSet) { |
| 1670 | if (first) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1671 | s << std::to_string(i); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1672 | first = false; |
| 1673 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1674 | s << ", " << std::to_string(i); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1675 | } |
| 1676 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1677 | return std::move(s.str()); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1680 | int32_t CameraService::mapToInterface(TorchModeStatus status) { |
| 1681 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1682 | switch (status) { |
| 1683 | case TorchModeStatus::NOT_AVAILABLE: |
| 1684 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1685 | break; |
| 1686 | case TorchModeStatus::AVAILABLE_OFF: |
| 1687 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 1688 | break; |
| 1689 | case TorchModeStatus::AVAILABLE_ON: |
| 1690 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
| 1691 | break; |
| 1692 | default: |
| 1693 | ALOGW("Unknown new flash status: %d", status); |
| 1694 | } |
| 1695 | return serviceStatus; |
| 1696 | } |
| 1697 | |
| 1698 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { |
| 1699 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; |
| 1700 | switch (status) { |
| 1701 | case CameraDeviceStatus::NOT_PRESENT: |
| 1702 | serviceStatus = StatusInternal::NOT_PRESENT; |
| 1703 | break; |
| 1704 | case CameraDeviceStatus::PRESENT: |
| 1705 | serviceStatus = StatusInternal::PRESENT; |
| 1706 | break; |
| 1707 | case CameraDeviceStatus::ENUMERATING: |
| 1708 | serviceStatus = StatusInternal::ENUMERATING; |
| 1709 | break; |
| 1710 | default: |
| 1711 | ALOGW("Unknown new HAL device status: %d", status); |
| 1712 | } |
| 1713 | return serviceStatus; |
| 1714 | } |
| 1715 | |
| 1716 | int32_t CameraService::mapToInterface(StatusInternal status) { |
| 1717 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1718 | switch (status) { |
| 1719 | case StatusInternal::NOT_PRESENT: |
| 1720 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1721 | break; |
| 1722 | case StatusInternal::PRESENT: |
| 1723 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; |
| 1724 | break; |
| 1725 | case StatusInternal::ENUMERATING: |
| 1726 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; |
| 1727 | break; |
| 1728 | case StatusInternal::NOT_AVAILABLE: |
| 1729 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; |
| 1730 | break; |
| 1731 | case StatusInternal::UNKNOWN: |
| 1732 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; |
| 1733 | break; |
| 1734 | default: |
| 1735 | ALOGW("Unknown new internal device status: %d", status); |
| 1736 | } |
| 1737 | return serviceStatus; |
| 1738 | } |
| 1739 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1740 | Status CameraService::initializeShimMetadata(int cameraId) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1741 | int uid = getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1742 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1743 | std::string cameraIdStr = std::to_string(cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1744 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1745 | sp<Client> tmp = nullptr; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1746 | if (!(ret = connectHelper<ICameraClient,Client>( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1747 | sp<ICameraClient>{nullptr}, cameraIdStr, cameraId, |
| 1748 | kServiceName, /*systemNativeClient*/ false, {}, uid, USE_CALLING_PID, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1749 | API_1, /*shimUpdateOnly*/ true, /*oomScoreOffset*/ 0, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1750 | /*targetSdkVersion*/ __ANDROID_API_FUTURE__, /*overrideToPortrait*/ true, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1751 | /*forceSlowJpegMode*/false, cameraIdStr, /*out*/ tmp) |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1752 | ).isOk()) { |
Tomasz Wasilczyk | 12b04a5 | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 1753 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().c_str()); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1754 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1755 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1758 | Status CameraService::getLegacyParametersLazy(int cameraId, |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1759 | /*out*/ |
| 1760 | CameraParameters* parameters) { |
| 1761 | |
| 1762 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 1763 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1764 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1765 | |
| 1766 | if (parameters == NULL) { |
| 1767 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1768 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1769 | } |
| 1770 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1771 | std::string unresolvedCameraId = std::to_string(cameraId); |
| 1772 | std::string cameraIdStr = resolveCameraId(unresolvedCameraId, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1773 | getCallingUid()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1774 | |
| 1775 | // Check if we already have parameters |
| 1776 | { |
| 1777 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1778 | Mutex::Autolock lock(mServiceLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1779 | auto cameraState = getCameraState(cameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1780 | if (cameraState == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1781 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, cameraIdStr.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1782 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1783 | "Invalid camera ID: %s", cameraIdStr.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1784 | } |
| 1785 | CameraParameters p = cameraState->getShimParams(); |
| 1786 | if (!p.isEmpty()) { |
| 1787 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1788 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1789 | } |
| 1790 | } |
| 1791 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1792 | int64_t token = clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1793 | ret = initializeShimMetadata(cameraId); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1794 | restoreCallingIdentity(token); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1795 | if (!ret.isOk()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1796 | // Error already logged by callee |
| 1797 | return ret; |
| 1798 | } |
| 1799 | |
| 1800 | // Check for parameters again |
| 1801 | { |
| 1802 | // Scope for service lock |
| 1803 | Mutex::Autolock lock(mServiceLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1804 | auto cameraState = getCameraState(cameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1805 | if (cameraState == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1806 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, cameraIdStr.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1807 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1808 | "Invalid camera ID: %s", cameraIdStr.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1809 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1810 | CameraParameters p = cameraState->getShimParams(); |
| 1811 | if (!p.isEmpty()) { |
| 1812 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1813 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1814 | } |
| 1815 | } |
| 1816 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1817 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 1818 | __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1819 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1822 | Status CameraService::validateConnectLocked(const std::string& cameraId, |
| 1823 | const std::string& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1824 | /*out*/int& originalClientPid) const { |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1825 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1826 | #ifdef __BRILLO__ |
| 1827 | UNUSED(clientName8); |
| 1828 | UNUSED(clientUid); |
| 1829 | UNUSED(clientPid); |
| 1830 | UNUSED(originalClientPid); |
| 1831 | #else |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1832 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, |
| 1833 | originalClientPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1834 | if (!allowed.isOk()) { |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1835 | return allowed; |
| 1836 | } |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1837 | #endif // __BRILLO__ |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1838 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1839 | int callingPid = getCallingPid(); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1840 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1841 | if (!mInitialized) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1842 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 1843 | callingPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1844 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1845 | "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] | 1846 | } |
| 1847 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1848 | if (getCameraState(cameraId) == nullptr) { |
| 1849 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1850 | cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1851 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1852 | "No camera device with ID \"%s\" available", cameraId.c_str()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1855 | status_t err = checkIfDeviceIsUsable(cameraId); |
| 1856 | if (err != NO_ERROR) { |
| 1857 | switch(err) { |
| 1858 | case -ENODEV: |
| 1859 | case -EBUSY: |
| 1860 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1861 | "No camera device with ID \"%s\" currently available", cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1862 | default: |
| 1863 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1864 | "Unknown error connecting to ID \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | return Status::ok(); |
Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1868 | } |
| 1869 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1870 | Status CameraService::validateClientPermissionsLocked(const std::string& cameraId, |
| 1871 | const std::string& clientName, int& clientUid, int& clientPid, |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1872 | /*out*/int& originalClientPid) const { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1873 | int callingPid = getCallingPid(); |
| 1874 | int callingUid = getCallingUid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1875 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1876 | // Check if we can trust clientUid |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1877 | if (clientUid == USE_CALLING_UID) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1878 | clientUid = callingUid; |
| 1879 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1880 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1881 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1882 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1883 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1884 | "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] | 1885 | callingPid, callingUid, cameraId.c_str(), |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1886 | clientName.c_str(), clientPid, clientUid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1889 | // Check if we can trust clientPid |
| 1890 | if (clientPid == USE_CALLING_PID) { |
| 1891 | clientPid = callingPid; |
| 1892 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1893 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1894 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1895 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1896 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1897 | "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] | 1898 | callingPid, callingUid, cameraId.c_str(), |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1899 | clientName.c_str(), clientPid, clientUid); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1900 | } |
| 1901 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1902 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1903 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", |
| 1904 | cameraId.c_str()); |
| 1905 | 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] | 1906 | "available", cameraId.c_str()); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1907 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1908 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1909 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1910 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1911 | 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] | 1912 | "found while trying to query device kind", cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1913 | |
| 1914 | } |
| 1915 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1916 | // If it's not calling from cameraserver, check the permission if the |
| 1917 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for |
| 1918 | // android.permission.SYSTEM_CAMERA for system only camera devices). |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1919 | bool checkPermissionForCamera = |
| 1920 | hasPermissionsForCamera(cameraId, clientPid, clientUid, clientName); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1921 | if (callingPid != getpid() && |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1922 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && !checkPermissionForCamera) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1923 | 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] | 1924 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1925 | "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] | 1926 | clientName.c_str(), clientPid, clientUid, cameraId.c_str()); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1927 | } |
| 1928 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1929 | // 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] | 1930 | if (!mUidPolicy->isUidActive(callingUid, clientName)) { |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1931 | int32_t procState = mUidPolicy->getProcState(callingUid); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1932 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", |
| 1933 | clientPid, clientUid); |
| 1934 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1935 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" |
| 1936 | "calling UID %d proc state %" PRId32 ")", |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1937 | clientName.c_str(), clientPid, clientUid, cameraId.c_str(), |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1938 | callingUid, procState); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1939 | } |
| 1940 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1941 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use cases |
| 1942 | // such as rear view and surround view cannot be disabled and are exempt from sensor privacy |
| 1943 | // policy. In all other cases,if sensor privacy is enabled then prevent access to the camera. |
| 1944 | if ((!isAutomotivePrivilegedClient(callingUid) || |
| 1945 | !isAutomotiveExteriorSystemCamera(cameraId)) && |
| 1946 | mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1947 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); |
| 1948 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1949 | "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] | 1950 | "is enabled", clientName.c_str(), clientPid, clientUid, cameraId.c_str()); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1951 | } |
| 1952 | |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1953 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's |
| 1954 | // connected to camera service directly. |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1955 | originalClientPid = clientPid; |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1956 | clientPid = callingPid; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1957 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1958 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1959 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1960 | // For non-system clients : Only allow clients who are being used by the current foreground |
| 1961 | // device user, unless calling from our own process. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1962 | if (!callerHasSystemUid() && callingPid != getpid() && |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1963 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1964 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 1965 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1966 | toString(mAllowedUsers).c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1967 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1968 | "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] | 1969 | clientUserId, cameraId.c_str()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 1972 | if (flags::camera_hsum_permission()) { |
| 1973 | // If the System User tries to access the camera when the device is running in |
| 1974 | // headless system user mode, ensure that client has the required permission |
| 1975 | // CAMERA_HEADLESS_SYSTEM_USER. |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1976 | if (isHeadlessSystemUserMode() |
| 1977 | && (clientUserId == USER_SYSTEM) |
| 1978 | && !hasPermissionsForCameraHeadlessSystemUser(cameraId, callingPid, callingUid)) { |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 1979 | ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid); |
| 1980 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1981 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" as Headless System \ |
| 1982 | User without camera headless system user permission", |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1983 | clientName.c_str(), clientPid, clientUid, cameraId.c_str()); |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 1984 | } |
Jyoti Bhayana | 5bdb5a6 | 2023-08-24 14:46:08 -0700 | [diff] [blame] | 1985 | } |
| 1986 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1987 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1988 | } |
| 1989 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1990 | status_t CameraService::checkIfDeviceIsUsable(const std::string& cameraId) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1991 | auto cameraState = getCameraState(cameraId); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1992 | int callingPid = getCallingPid(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1993 | if (cameraState == nullptr) { |
| 1994 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1995 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1996 | return -ENODEV; |
| 1997 | } |
| 1998 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1999 | StatusInternal currentStatus = cameraState->getStatus(); |
| 2000 | if (currentStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2001 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2002 | callingPid, cameraId.c_str()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 2003 | return -ENODEV; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2004 | } else if (currentStatus == StatusInternal::ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2005 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2006 | callingPid, cameraId.c_str()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 2007 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2008 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2009 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2010 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2011 | } |
| 2012 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2013 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2014 | const CameraService::DescriptorPtr& desc, int oomScoreOffset, bool systemNativeClient) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2015 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2016 | // Make a descriptor for the incoming client |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2017 | auto clientDescriptor = |
| 2018 | CameraService::CameraClientManager::makeClientDescriptor(client, desc, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2019 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2020 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 2021 | |
| 2022 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2023 | client->getPackageName()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2024 | |
| 2025 | if (evicted.size() > 0) { |
| 2026 | // This should never happen - clients should already have been removed in disconnect |
| 2027 | for (auto& i : evicted) { |
| 2028 | 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] | 2029 | __FUNCTION__, i->getKey().c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 2033 | __FUNCTION__); |
| 2034 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 2035 | |
| 2036 | // And register a death notification for the client callback. Do |
| 2037 | // this last to avoid Binder policy where a nested Binder |
| 2038 | // transaction might be pre-empted to service the client death |
| 2039 | // notification if the client process dies before linkToDeath is |
| 2040 | // invoked. |
| 2041 | sp<IBinder> remoteCallback = client->getRemote(); |
| 2042 | if (remoteCallback != nullptr) { |
| 2043 | remoteCallback->linkToDeath(this); |
| 2044 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2045 | } |
| 2046 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2047 | status_t CameraService::handleEvictionsLocked(const std::string& cameraId, int clientPid, |
| 2048 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, |
| 2049 | const std::string& packageName, int oomScoreOffset, bool systemNativeClient, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2050 | /*out*/ |
| 2051 | sp<BasicClient>* client, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2052 | std::shared_ptr<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2053 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2054 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2055 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2056 | DescriptorPtr clientDescriptor; |
| 2057 | { |
| 2058 | if (effectiveApiLevel == API_1) { |
| 2059 | // If we are using API1, any existing client for this camera ID with the same remote |
| 2060 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 2061 | |
| 2062 | auto current = mActiveClientManager.get(cameraId); |
| 2063 | if (current != nullptr) { |
| 2064 | auto clientSp = current->getValue(); |
| 2065 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2066 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
Shuzhen Wang | b2d43f6 | 2021-08-25 14:01:11 -0700 | [diff] [blame] | 2067 | ALOGW("CameraService connect called with a different" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2068 | " API level, evicting prior client..."); |
| 2069 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2070 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 2071 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2072 | *client = clientSp; |
| 2073 | return NO_ERROR; |
| 2074 | } |
| 2075 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 2076 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 2077 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 2078 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2079 | // Get state for the given cameraId |
| 2080 | auto state = getCameraState(cameraId); |
| 2081 | if (state == nullptr) { |
| 2082 | 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] | 2083 | clientPid, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2084 | // Should never get here because validateConnectLocked should have errored out |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2085 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2086 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2087 | |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 2088 | sp<IServiceManager> sm = defaultServiceManager(); |
| 2089 | sp<IBinder> binder = sm->checkService(String16(kProcessInfoServiceName)); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2090 | if (!binder && isAutomotivePrivilegedClient(getCallingUid())) { |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 2091 | // If processinfo service is not available and the client is automotive privileged |
| 2092 | // client used for safety critical uses cases such as rear-view and surround-view which |
| 2093 | // needs to be available before android boot completes, then use the hardcoded values |
| 2094 | // for the process state and priority score. As this scenario is before android system |
| 2095 | // services are up and client is native client, hence using NATIVE_ADJ as the priority |
| 2096 | // score and state as PROCESS_STATE_BOUND_TOP as such automotive apps need to be |
| 2097 | // visible on the top. |
| 2098 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 2099 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 2100 | state->getConflicting(), resource_policy::NATIVE_ADJ, clientPid, |
| 2101 | ActivityManager::PROCESS_STATE_BOUND_TOP, oomScoreOffset, systemNativeClient); |
| 2102 | } else { |
| 2103 | // Get current active client PIDs |
| 2104 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 2105 | ownerPids.push_back(clientPid); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2106 | |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 2107 | std::vector<int> priorityScores(ownerPids.size()); |
| 2108 | std::vector<int> states(ownerPids.size()); |
| 2109 | |
| 2110 | // Get priority scores of all active PIDs |
| 2111 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids(ownerPids.size(), |
| 2112 | &ownerPids[0], /*out*/&states[0], /*out*/&priorityScores[0]); |
| 2113 | if (err != OK) { |
| 2114 | ALOGE("%s: Priority score query failed: %d", __FUNCTION__, err); |
| 2115 | return err; |
| 2116 | } |
| 2117 | |
| 2118 | // Update all active clients' priorities |
| 2119 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; |
| 2120 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
| 2121 | pidToPriorityMap.emplace(ownerPids[i], |
| 2122 | resource_policy::ClientPriority(priorityScores[i], states[i], |
| 2123 | /* isVendorClient won't get copied over*/ false, |
| 2124 | /* oomScoreOffset won't get copied over*/ 0)); |
| 2125 | } |
| 2126 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
| 2127 | |
| 2128 | int32_t actualScore = priorityScores[priorityScores.size() - 1]; |
| 2129 | int32_t actualState = states[states.size() - 1]; |
| 2130 | |
| 2131 | // Make descriptor for incoming client. We store the oomScoreOffset |
| 2132 | // since we might need it later on new handleEvictionsLocked and |
| 2133 | // ProcessInfoService would not take that into account. |
| 2134 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 2135 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 2136 | state->getConflicting(), actualScore, clientPid, actualState, |
| 2137 | oomScoreOffset, systemNativeClient); |
| 2138 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2139 | |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2140 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); |
| 2141 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2142 | // Find clients that would be evicted |
| 2143 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 2144 | |
| 2145 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 2146 | // background, so we cannot do evictions |
| 2147 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 2148 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 2149 | " priority).", clientPid); |
| 2150 | |
| 2151 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2152 | std::string curTime = getFormattedCurrentTime(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2153 | auto incompatibleClients = |
| 2154 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 2155 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2156 | std::string msg = fmt::sprintf("%s : DENIED connect device %s client for package %s " |
| 2157 | "(PID %d, score %d state %d) due to eviction policy", curTime.c_str(), |
| 2158 | cameraId.c_str(), packageName.c_str(), clientPid, |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2159 | clientPriority.getScore(), clientPriority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2160 | |
| 2161 | for (auto& i : incompatibleClients) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2162 | 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] | 2163 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2164 | i->getKey().c_str(), |
| 2165 | i->getValue()->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2166 | i->getOwnerId(), i->getPriority().getScore(), |
| 2167 | i->getPriority().getState()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 2168 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2169 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().c_str(), |
| 2170 | i->getValue()->getPackageName().c_str(), i->getOwnerId(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2171 | i->getPriority().getScore(), i->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2175 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2176 | mEventLog.add(msg); |
| 2177 | |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 2178 | auto current = mActiveClientManager.get(cameraId); |
| 2179 | if (current != nullptr) { |
| 2180 | return -EBUSY; // CAMERA_IN_USE |
| 2181 | } else { |
| 2182 | return -EUSERS; // MAX_CAMERAS_IN_USE |
| 2183 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | for (auto& i : evicted) { |
| 2187 | sp<BasicClient> clientSp = i->getValue(); |
| 2188 | if (clientSp.get() == nullptr) { |
| 2189 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 2190 | |
| 2191 | // TODO: Remove this |
| 2192 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 2193 | __FUNCTION__); |
| 2194 | mActiveClientManager.remove(i); |
| 2195 | continue; |
| 2196 | } |
| 2197 | |
| 2198 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2199 | i->getKey().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2200 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2201 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2202 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2203 | logEvent(fmt::sprintf("EVICT device %s client held by package %s (PID" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2204 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" |
| 2205 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2206 | i->getKey().c_str(), clientSp->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2207 | i->getOwnerId(), i->getPriority().getScore(), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2208 | i->getPriority().getState(), cameraId.c_str(), |
| 2209 | packageName.c_str(), clientPid, clientPriority.getScore(), |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2210 | clientPriority.getState())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2211 | |
| 2212 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2213 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2214 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2215 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2218 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 2219 | // other clients from connecting in mServiceLockWrapper if held |
| 2220 | mServiceLock.unlock(); |
| 2221 | |
| 2222 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2223 | int64_t token = clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2224 | |
| 2225 | // Destroy evicted clients |
| 2226 | for (auto& i : evictedClients) { |
| 2227 | // 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] | 2228 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2229 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2230 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2231 | restoreCallingIdentity(token); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2232 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2233 | for (const auto& i : evictedClients) { |
| 2234 | 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] | 2235 | __FUNCTION__, i->getKey().c_str(), i->getOwnerId()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2236 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 2237 | if (ret == TIMED_OUT) { |
| 2238 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2239 | "current clients:\n%s", __FUNCTION__, i->getKey().c_str(), |
| 2240 | mActiveClientManager.toString().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2241 | return -EBUSY; |
| 2242 | } |
| 2243 | if (ret != NO_ERROR) { |
| 2244 | 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] | 2245 | "current clients:\n%s", __FUNCTION__, i->getKey().c_str(), strerror(-ret), |
| 2246 | ret, mActiveClientManager.toString().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2247 | return ret; |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | evictedClients.clear(); |
| 2252 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2253 | // Once clients have been disconnected, relock |
| 2254 | mServiceLock.lock(); |
| 2255 | |
| 2256 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 2257 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 2258 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2259 | } |
| 2260 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2261 | *partial = clientDescriptor; |
| 2262 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2265 | Status CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2266 | const sp<ICameraClient>& cameraClient, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2267 | int api1CameraId, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2268 | const std::string& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 2269 | int clientUid, |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 2270 | int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2271 | int targetSdkVersion, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2272 | bool overrideToPortrait, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2273 | bool forceSlowJpegMode, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2274 | int32_t deviceId, |
| 2275 | int32_t devicePolicy, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 2276 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2277 | sp<ICamera>* device) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2278 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2279 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2280 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2281 | std::string cameraIdStr = cameraIdIntToStr(api1CameraId, deviceId, devicePolicy); |
| 2282 | if (cameraIdStr.empty()) { |
| 2283 | std::string msg = fmt::sprintf("Camera %d: Invalid camera id for device id %d", |
| 2284 | api1CameraId, deviceId); |
| 2285 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2286 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 2287 | } |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 2288 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2289 | sp<Client> client = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2290 | ret = connectHelper<ICameraClient,Client>(cameraClient, cameraIdStr, api1CameraId, |
| 2291 | clientPackageName, /*systemNativeClient*/ false, {}, clientUid, clientPid, API_1, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2292 | /*shimUpdateOnly*/ false, /*oomScoreOffset*/ 0, targetSdkVersion, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2293 | overrideToPortrait, forceSlowJpegMode, cameraIdStr, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2294 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2295 | if (!ret.isOk()) { |
| 2296 | logRejected(cameraIdStr, getCallingPid(), clientPackageName, toStdString(ret.toString8())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2297 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2298 | } |
| 2299 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2300 | *device = client; |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2301 | |
| 2302 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 2303 | const auto& mActivityManager = getActivityManager(); |
| 2304 | if (mActivityManager) { |
| 2305 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2306 | getCallingUid(), |
| 2307 | getCallingPid()); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2310 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2311 | } |
| 2312 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2313 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, |
| 2314 | bool isVendorListener, int clientPid, int clientUid) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2315 | // If the client is not a vendor client, don't add listener if |
| 2316 | // a) the camera is a publicly hidden secure camera OR |
| 2317 | // b) the camera is a system only camera and the client doesn't |
| 2318 | // have android.permission.SYSTEM_CAMERA permissions. |
| 2319 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || |
| 2320 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2321 | !hasPermissionsForSystemCamera(std::string(), clientPid, clientUid)))) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2322 | return true; |
| 2323 | } |
| 2324 | return false; |
| 2325 | } |
| 2326 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2327 | bool CameraService::shouldRejectSystemCameraConnection(const std::string& cameraId) const { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2328 | // Rules for rejection: |
| 2329 | // 1) If cameraserver tries to access this camera device, accept the |
| 2330 | // connection. |
| 2331 | // 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] | 2332 | // non system component is trying to access it. |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2333 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY |
| 2334 | // and the serving thread is a non hwbinder thread, the client must have |
| 2335 | // android.permission.SYSTEM_CAMERA permissions to connect. |
| 2336 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2337 | int cPid = getCallingPid(); |
| 2338 | int cUid = getCallingUid(); |
| 2339 | bool systemClient = callerHasSystemUid(); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2340 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 2341 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 2342 | // This isn't a known camera ID, so it's not a system camera |
| 2343 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); |
| 2344 | return false; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2345 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2346 | |
| 2347 | // (1) Cameraserver trying to connect, accept. |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2348 | if (isCallerCameraServerNotDelegating()) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2349 | return false; |
| 2350 | } |
| 2351 | // (2) |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2352 | if (!systemClient && systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2353 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); |
| 2354 | return true; |
| 2355 | } |
| 2356 | // (3) Here we only check for permissions if it is a system only camera device. This is since |
| 2357 | // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some |
| 2358 | // characteristics) even if clients don't have android.permission.CAMERA. We do not want the |
| 2359 | // same behavior for system camera devices. |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2360 | if (!systemClient && systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2361 | !hasPermissionsForSystemCamera(cameraId, cPid, cUid)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2362 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", |
| 2363 | cameraId.c_str()); |
| 2364 | return true; |
| 2365 | } |
| 2366 | |
| 2367 | return false; |
| 2368 | } |
| 2369 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2370 | Status CameraService::connectDevice( |
| 2371 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2372 | const std::string& unresolvedCameraId, |
| 2373 | const std::string& clientPackageName, |
| 2374 | const std::optional<std::string>& clientFeatureId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2375 | int clientUid, int oomScoreOffset, int targetSdkVersion, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2376 | bool overrideToPortrait, int32_t deviceId, int32_t devicePolicy, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2377 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2378 | sp<hardware::camera2::ICameraDeviceUser>* device) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2379 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2380 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2381 | sp<CameraDeviceClient> client = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2382 | std::string clientPackageNameAdj = clientPackageName; |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2383 | int callingPid = getCallingPid(); |
| 2384 | int callingUid = getCallingUid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2385 | bool systemNativeClient = false; |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2386 | if (callerHasSystemUid() && (clientPackageNameAdj.size() == 0)) { |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2387 | std::string systemClient = fmt::sprintf("client.pid<%d>", callingPid); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2388 | clientPackageNameAdj = systemClient; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2389 | systemNativeClient = true; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2390 | } |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2391 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2392 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
| 2393 | devicePolicy, callingUid, clientPackageNameAdj); |
| 2394 | if (!cameraIdOptional.has_value()) { |
| 2395 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 2396 | unresolvedCameraId.c_str(), deviceId); |
| 2397 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2398 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 2399 | } |
| 2400 | std::string cameraId = cameraIdOptional.value(); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2401 | |
| 2402 | if (oomScoreOffset < 0) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2403 | std::string msg = |
| 2404 | fmt::sprintf("Cannot increase the priority of a client %s pid %d for " |
| 2405 | "camera id %s", clientPackageNameAdj.c_str(), callingPid, |
| 2406 | cameraId.c_str()); |
| 2407 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2408 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 2411 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 2412 | if (clientUid == USE_CALLING_UID) { |
| 2413 | clientUserId = multiuser_get_user_id(callingUid); |
| 2414 | } |
| 2415 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2416 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use cases |
| 2417 | // such as rear view and surround view cannot be disabled. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2418 | if ((!isAutomotivePrivilegedClient(callingUid) || !isAutomotiveExteriorSystemCamera(cameraId)) |
| 2419 | && mCameraServiceProxyWrapper->isCameraDisabled(clientUserId)) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2420 | std::string msg = "Camera disabled by device policy"; |
| 2421 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2422 | return STATUS_ERROR(ERROR_DISABLED, msg.c_str()); |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 2423 | } |
| 2424 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2425 | // enforce system camera permissions |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2426 | if (oomScoreOffset > 0 |
| 2427 | && !hasPermissionsForSystemCamera(cameraId, callingPid, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2428 | callingUid) |
| 2429 | && !isTrustedCallingUid(callingUid)) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2430 | 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] | 2431 | "camera id %s without SYSTEM_CAMERA permissions", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2432 | clientPackageNameAdj.c_str(), callingPid, cameraId.c_str()); |
| 2433 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2434 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, msg.c_str()); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2437 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, |
| 2438 | cameraId, /*api1CameraId*/-1, clientPackageNameAdj, systemNativeClient, clientFeatureId, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2439 | clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, oomScoreOffset, |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 2440 | targetSdkVersion, overrideToPortrait, /*forceSlowJpegMode*/false, unresolvedCameraId, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2441 | /*out*/client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2442 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2443 | if (!ret.isOk()) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2444 | logRejected(cameraId, callingPid, clientPackageNameAdj, toStdString(ret.toString8())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2445 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2446 | } |
| 2447 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2448 | *device = client; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 2449 | Mutex::Autolock lock(mServiceLock); |
| 2450 | |
| 2451 | // Clear the previous cached logs and reposition the |
| 2452 | // file offset to beginning of the file to log new data. |
| 2453 | // If either truncate or lseek fails, close the previous file and create a new one. |
| 2454 | if ((ftruncate(mMemFd, 0) == -1) || (lseek(mMemFd, 0, SEEK_SET) == -1)) { |
| 2455 | ALOGE("%s: Error while truncating the file: %s", __FUNCTION__, sFileName); |
| 2456 | // Close the previous memfd. |
| 2457 | close(mMemFd); |
| 2458 | // If failure to wipe the data, then create a new file and |
| 2459 | // assign the new value to mMemFd. |
| 2460 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 2461 | if (mMemFd == -1) { |
| 2462 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 2463 | } |
| 2464 | } |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2465 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 2466 | const auto& mActivityManager = getActivityManager(); |
| 2467 | if (mActivityManager) { |
| 2468 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2469 | callingUid, |
| 2470 | callingPid); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2471 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2472 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2473 | } |
| 2474 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2475 | bool CameraService::isCameraPrivacyEnabled(const String16& packageName, const std::string& cam_id, |
| 2476 | int callingPid, int callingUid) { |
| 2477 | if (!isAutomotiveDevice()) { |
| 2478 | return mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
| 2479 | } |
| 2480 | |
| 2481 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for |
| 2482 | // safety-critical use cases cannot be disabled and are exempt from camera privacy policy. |
| 2483 | if ((isAutomotivePrivilegedClient(callingUid) && isAutomotiveExteriorSystemCamera(cam_id))) { |
| 2484 | ALOGI("Camera privacy cannot be enabled for automotive privileged client %d " |
| 2485 | "using camera %s", callingUid, cam_id.c_str()); |
| 2486 | return false; |
| 2487 | } |
| 2488 | |
| 2489 | if (mSensorPrivacyPolicy->isCameraPrivacyEnabled(packageName)) { |
| 2490 | return true; |
| 2491 | } else if (mSensorPrivacyPolicy->getCameraPrivacyState() == SensorPrivacyManager::DISABLED) { |
| 2492 | return false; |
Jyoti Bhayana | 54a4b00 | 2024-02-27 15:36:09 -0800 | [diff] [blame] | 2493 | } else if (mSensorPrivacyPolicy->getCameraPrivacyState() |
| 2494 | == SensorPrivacyManager::ENABLED_EXCEPT_ALLOWLISTED_APPS) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2495 | if (hasPermissionsForCameraPrivacyAllowlist(callingPid, callingUid)) { |
| 2496 | return false; |
| 2497 | } else { |
| 2498 | return true; |
| 2499 | } |
| 2500 | } |
| 2501 | return false; |
| 2502 | } |
| 2503 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2504 | std::string CameraService::getPackageNameFromUid(int clientUid) { |
| 2505 | std::string packageName(""); |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2506 | |
Avichal Rakesh | 5788fec | 2024-03-15 14:39:20 -0700 | [diff] [blame] | 2507 | sp<IPermissionController> permCtrl; |
| 2508 | if (flags::cache_permission_services()) { |
| 2509 | permCtrl = getPermissionController(); |
| 2510 | } else { |
| 2511 | sp<IServiceManager> sm = defaultServiceManager(); |
| 2512 | #pragma clang diagnostic push |
| 2513 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 2514 | // Using deprecated function to preserve functionality until the |
| 2515 | // cache_permission_services flag is removed. |
| 2516 | sp<IBinder> binder = sm->getService(toString16(kPermissionServiceName)); |
| 2517 | #pragma clang diagnostic pop |
| 2518 | if (binder == 0) { |
| 2519 | ALOGE("Cannot get permission service"); |
| 2520 | permCtrl = nullptr; |
| 2521 | } else { |
| 2522 | permCtrl = interface_cast<IPermissionController>(binder); |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | if (permCtrl == nullptr) { |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2527 | // Return empty package name and the further interaction |
| 2528 | // with camera will likely fail |
| 2529 | return packageName; |
| 2530 | } |
| 2531 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2532 | Vector<String16> packages; |
| 2533 | |
| 2534 | permCtrl->getPackagesForUid(clientUid, packages); |
| 2535 | |
| 2536 | if (packages.isEmpty()) { |
| 2537 | ALOGE("No packages for calling UID %d", clientUid); |
| 2538 | // Return empty package name and the further interaction |
| 2539 | // with camera will likely fail |
| 2540 | return packageName; |
| 2541 | } |
| 2542 | |
| 2543 | // Arbitrarily pick the first name in the list |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2544 | packageName = toStdString(packages[0]); |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2545 | |
| 2546 | return packageName; |
| 2547 | } |
| 2548 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2549 | template<class CALLBACK, class CLIENT> |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2550 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const std::string& cameraId, |
| 2551 | int api1CameraId, const std::string& clientPackageNameMaybe, bool systemNativeClient, |
| 2552 | const std::optional<std::string>& clientFeatureId, int clientUid, int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2553 | apiLevel effectiveApiLevel, bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2554 | bool overrideToPortrait, bool forceSlowJpegMode, const std::string& originalCameraId, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2555 | /*out*/sp<CLIENT>& device) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2556 | binder::Status ret = binder::Status::ok(); |
| 2557 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2558 | bool isNonSystemNdk = false; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2559 | std::string clientPackageName; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2560 | int packageUid = (clientUid == USE_CALLING_UID) ? |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2561 | getCallingUid() : clientUid; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2562 | if (clientPackageNameMaybe.size() <= 0) { |
| 2563 | // NDK calls don't come with package names, but we need one for various cases. |
| 2564 | // Generally, there's a 1:1 mapping between UID and package name, but shared UIDs |
| 2565 | // do exist. For all authentication cases, all packages under the same UID get the |
| 2566 | // same permissions, so picking any associated package name is sufficient. For some |
| 2567 | // other cases, this may give inaccurate names for clients in logs. |
| 2568 | isNonSystemNdk = true; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2569 | clientPackageName = getPackageNameFromUid(packageUid); |
| 2570 | } else { |
| 2571 | clientPackageName = clientPackageNameMaybe; |
| 2572 | } |
| 2573 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2574 | int originalClientPid = 0; |
| 2575 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2576 | int packagePid = (clientPid == USE_CALLING_PID) ? |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2577 | getCallingPid() : clientPid; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 2578 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2579 | "Camera API version %d", packagePid, clientPackageName.c_str(), cameraId.c_str(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2580 | static_cast<int>(effectiveApiLevel)); |
| 2581 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2582 | nsecs_t openTimeNs = systemTime(); |
| 2583 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2584 | sp<CLIENT> client = nullptr; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2585 | int facing = -1; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 2586 | int orientation = 0; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2587 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2588 | { |
| 2589 | // Acquire mServiceLock and prevent other clients from connecting |
| 2590 | std::unique_ptr<AutoConditionLock> lock = |
| 2591 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2592 | |
| 2593 | if (lock == nullptr) { |
| 2594 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." |
| 2595 | , clientPid); |
| 2596 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2597 | "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] | 2598 | cameraId.c_str(), clientPackageName.c_str(), clientPid); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2599 | } |
| 2600 | |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 2601 | // Enforce client permissions and do basic validity checks |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2602 | if (!(ret = validateConnectLocked(cameraId, clientPackageName, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2603 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { |
| 2604 | return ret; |
| 2605 | } |
| 2606 | |
| 2607 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 2608 | // we were doing a shim update, return immediately |
| 2609 | if (shimUpdateOnly) { |
| 2610 | auto cameraState = getCameraState(cameraId); |
| 2611 | if (cameraState != nullptr) { |
| 2612 | if (!cameraState->getShimParams().isEmpty()) return ret; |
| 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | status_t err; |
| 2617 | |
| 2618 | sp<BasicClient> clientTmp = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2619 | std::shared_ptr<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>> partial; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2620 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2621 | IInterface::asBinder(cameraCb), clientPackageName, oomScoreOffset, |
| 2622 | systemNativeClient, /*out*/&clientTmp, /*out*/&partial)) != NO_ERROR) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2623 | switch (err) { |
| 2624 | case -ENODEV: |
| 2625 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2626 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2627 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2628 | case -EBUSY: |
| 2629 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2630 | "Higher-priority client using camera, ID \"%s\" currently unavailable", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2631 | cameraId.c_str()); |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 2632 | case -EUSERS: |
| 2633 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2634 | "Too many cameras already open, cannot open camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2635 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2636 | default: |
| 2637 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2638 | "Unexpected error %s (%d) opening camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2639 | strerror(-err), err, cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | if (clientTmp.get() != nullptr) { |
| 2644 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 2645 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 2646 | return ret; |
| 2647 | } |
| 2648 | |
| 2649 | // give flashlight a chance to close devices if necessary. |
| 2650 | mFlashlight->prepareDeviceOpen(cameraId); |
| 2651 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2652 | int portraitRotation; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2653 | auto deviceVersionAndTransport = |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2654 | getDeviceVersion(cameraId, overrideToPortrait, /*out*/&portraitRotation, |
| 2655 | /*out*/&facing, /*out*/&orientation); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2656 | if (facing == -1) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2657 | 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] | 2658 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2659 | "Unable to get camera device \"%s\" facing", cameraId.c_str()); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2660 | } |
| 2661 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2662 | sp<BasicClient> tmp = nullptr; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2663 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2664 | mPerfClassPrimaryCameraIds, cameraId, targetSdkVersion); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2665 | if(!(ret = makeClient(this, cameraCb, clientPackageName, systemNativeClient, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2666 | clientFeatureId, cameraId, api1CameraId, facing, |
| 2667 | orientation, clientPid, clientUid, getpid(), |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2668 | deviceVersionAndTransport, effectiveApiLevel, overrideForPerfClass, |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 2669 | overrideToPortrait, forceSlowJpegMode, originalCameraId, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2670 | /*out*/&tmp)).isOk()) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2671 | return ret; |
| 2672 | } |
| 2673 | client = static_cast<CLIENT*>(tmp.get()); |
| 2674 | |
| 2675 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 2676 | __FUNCTION__); |
| 2677 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2678 | std::string monitorTags = isClientWatched(client.get()) ? mMonitorTags : std::string(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2679 | err = client->initialize(mCameraProviderManager, monitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2680 | if (err != OK) { |
| 2681 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2682 | // Errors could be from the HAL module open call or from AppOpsManager |
Kwangkyu Park | b1aaf9a | 2023-07-08 00:42:03 +0900 | [diff] [blame] | 2683 | mServiceLock.unlock(); |
| 2684 | client->disconnect(); |
| 2685 | mServiceLock.lock(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2686 | switch(err) { |
| 2687 | case BAD_VALUE: |
| 2688 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2689 | "Illegal argument to HAL module for camera \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2690 | case -EBUSY: |
| 2691 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2692 | "Camera \"%s\" is already open", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2693 | case -EUSERS: |
| 2694 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2695 | "Too many cameras already open, cannot open camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2696 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2697 | case PERMISSION_DENIED: |
| 2698 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2699 | "No permission to open camera \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2700 | case -EACCES: |
| 2701 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2702 | "Camera \"%s\" disabled by policy", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2703 | case -ENODEV: |
| 2704 | default: |
| 2705 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2706 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2707 | strerror(-err), err); |
| 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | // Update shim paremeters for legacy clients |
| 2712 | if (effectiveApiLevel == API_1) { |
| 2713 | // Assume we have always received a Client subclass for API1 |
| 2714 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 2715 | String8 rawParams = shimClient->getParameters(); |
| 2716 | CameraParameters params(rawParams); |
| 2717 | |
| 2718 | auto cameraState = getCameraState(cameraId); |
| 2719 | if (cameraState != nullptr) { |
| 2720 | cameraState->setShimParams(params); |
| 2721 | } else { |
| 2722 | 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] | 2723 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2724 | } |
| 2725 | } |
| 2726 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 2727 | // Enable/disable camera service watchdog |
| 2728 | client->setCameraServiceWatchdog(mCameraServiceWatchdogEnabled); |
| 2729 | |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2730 | CameraMetadata chars; |
| 2731 | bool rotateAndCropSupported = true; |
| 2732 | err = mCameraProviderManager->getCameraCharacteristics(cameraId, overrideForPerfClass, |
| 2733 | &chars, overrideToPortrait); |
| 2734 | if (err == OK) { |
| 2735 | auto availableRotateCropEntry = chars.find( |
| 2736 | ANDROID_SCALER_AVAILABLE_ROTATE_AND_CROP_MODES); |
| 2737 | if (availableRotateCropEntry.count <= 1) { |
| 2738 | rotateAndCropSupported = false; |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2739 | } |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame] | 2740 | } else { |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2741 | ALOGE("%s: Unable to query static metadata for camera %s: %s (%d)", __FUNCTION__, |
| 2742 | cameraId.c_str(), strerror(-err), err); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2745 | if (rotateAndCropSupported) { |
| 2746 | // Set rotate-and-crop override behavior |
| 2747 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { |
| 2748 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); |
| 2749 | } else if (overrideToPortrait && portraitRotation != 0) { |
| 2750 | uint8_t rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_AUTO; |
| 2751 | switch (portraitRotation) { |
| 2752 | case 90: |
| 2753 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_90; |
| 2754 | break; |
| 2755 | case 180: |
| 2756 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_180; |
| 2757 | break; |
| 2758 | case 270: |
| 2759 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_270; |
| 2760 | break; |
| 2761 | default: |
| 2762 | ALOGE("Unexpected portrait rotation: %d", portraitRotation); |
| 2763 | break; |
| 2764 | } |
| 2765 | client->setRotateAndCropOverride(rotateAndCropMode); |
| 2766 | } else { |
| 2767 | client->setRotateAndCropOverride( |
| 2768 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 2769 | clientPackageName, facing, multiuser_get_user_id(clientUid))); |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | bool autoframingSupported = true; |
| 2774 | auto availableAutoframingEntry = chars.find(ANDROID_CONTROL_AUTOFRAMING_AVAILABLE); |
| 2775 | if ((availableAutoframingEntry.count == 1) && (availableAutoframingEntry.data.u8[0] == |
| 2776 | ANDROID_CONTROL_AUTOFRAMING_AVAILABLE_FALSE)) { |
| 2777 | autoframingSupported = false; |
| 2778 | } |
| 2779 | |
| 2780 | if (autoframingSupported) { |
| 2781 | // Set autoframing override behaviour |
| 2782 | if (mOverrideAutoframingMode != ANDROID_CONTROL_AUTOFRAMING_AUTO) { |
| 2783 | client->setAutoframingOverride(mOverrideAutoframingMode); |
| 2784 | } else { |
| 2785 | client->setAutoframingOverride( |
| 2786 | mCameraServiceProxyWrapper->getAutoframingOverride( |
| 2787 | clientPackageName)); |
| 2788 | } |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 2789 | } |
| 2790 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2791 | bool isCameraPrivacyEnabled; |
| 2792 | if (flags::camera_privacy_allowlist()) { |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2793 | // Set camera muting behavior. |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2794 | isCameraPrivacyEnabled = this->isCameraPrivacyEnabled( |
| 2795 | toString16(client->getPackageName()), cameraId, packagePid, packageUid); |
| 2796 | } else { |
| 2797 | isCameraPrivacyEnabled = |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2798 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2799 | } |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 2800 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2801 | if (client->supportsCameraMute()) { |
| 2802 | client->setCameraMute( |
| 2803 | mOverrideCameraMuteMode || isCameraPrivacyEnabled); |
| 2804 | } else if (isCameraPrivacyEnabled) { |
| 2805 | // no camera mute supported, but privacy is on! => disconnect |
| 2806 | ALOGI("Camera mute not supported for package: %s, camera id: %s", |
| 2807 | client->getPackageName().c_str(), cameraId.c_str()); |
| 2808 | // Do not hold mServiceLock while disconnecting clients, but |
| 2809 | // retain the condition blocking other clients from connecting |
| 2810 | // in mServiceLockWrapper if held. |
| 2811 | mServiceLock.unlock(); |
| 2812 | // Clear caller identity temporarily so client disconnect PID |
| 2813 | // checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2814 | int64_t token = clearCallingIdentity(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2815 | // Note AppOp to trigger the "Unblock" dialog |
| 2816 | client->noteAppOp(); |
| 2817 | client->disconnect(); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2818 | restoreCallingIdentity(token); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2819 | // Reacquire mServiceLock |
| 2820 | mServiceLock.lock(); |
| 2821 | |
| 2822 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 2823 | "Camera \"%s\" disabled due to camera mute", cameraId.c_str()); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2824 | } |
| 2825 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2826 | if (shimUpdateOnly) { |
| 2827 | // If only updating legacy shim parameters, immediately disconnect client |
| 2828 | mServiceLock.unlock(); |
| 2829 | client->disconnect(); |
| 2830 | mServiceLock.lock(); |
| 2831 | } else { |
| 2832 | // Otherwise, add client to active clients list |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2833 | finishConnectLocked(client, partial, oomScoreOffset, systemNativeClient); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2834 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 2835 | |
| 2836 | client->setImageDumpMask(mImageDumpMask); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 2837 | client->setStreamUseCaseOverrides(mStreamUseCaseOverrides); |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 2838 | client->setZoomOverride(mZoomOverrideValue); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2839 | } // lock is destroyed, allow further connect calls |
| 2840 | |
| 2841 | // Important: release the mutex here so the client can call back into the service from its |
| 2842 | // destructor (can be at the end of the call) |
| 2843 | device = client; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2844 | |
| 2845 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 2846 | mCameraServiceProxyWrapper->logOpen(cameraId, facing, clientPackageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2847 | effectiveApiLevel, isNonSystemNdk, openLatencyMs); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2848 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2849 | { |
| 2850 | Mutex::Autolock lock(mInjectionParametersLock); |
| 2851 | if (cameraId == mInjectionInternalCamId && mInjectionInitPending) { |
| 2852 | mInjectionInitPending = false; |
| 2853 | status_t res = NO_ERROR; |
| 2854 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 2855 | if (clientDescriptor != nullptr) { |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2856 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 2857 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 2858 | if(res != OK) { |
| 2859 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2860 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2861 | mInjectionExternalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2862 | } |
| 2863 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2864 | if (res != OK) { |
| 2865 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2866 | } |
| 2867 | } else { |
| 2868 | ALOGE("%s: Internal camera ID = %s 's client does not exist!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2869 | __FUNCTION__, mInjectionInternalCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2870 | res = NO_INIT; |
| 2871 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2872 | } |
| 2873 | } |
| 2874 | } |
| 2875 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2876 | return ret; |
| 2877 | } |
| 2878 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2879 | status_t CameraService::addOfflineClient(const std::string &cameraId, |
| 2880 | sp<BasicClient> offlineClient) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2881 | if (offlineClient.get() == nullptr) { |
| 2882 | return BAD_VALUE; |
| 2883 | } |
| 2884 | |
| 2885 | { |
| 2886 | // Acquire mServiceLock and prevent other clients from connecting |
| 2887 | std::unique_ptr<AutoConditionLock> lock = |
| 2888 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2889 | |
| 2890 | if (lock == nullptr) { |
| 2891 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." |
| 2892 | , __FUNCTION__, offlineClient->getClientPid()); |
| 2893 | return TIMED_OUT; |
| 2894 | } |
| 2895 | |
| 2896 | auto onlineClientDesc = mActiveClientManager.get(cameraId); |
| 2897 | if (onlineClientDesc.get() == nullptr) { |
| 2898 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, |
| 2899 | cameraId.c_str()); |
| 2900 | return BAD_VALUE; |
| 2901 | } |
| 2902 | |
| 2903 | // Offline clients do not evict or conflict with other online devices. Resource sharing |
| 2904 | // conflicts are handled by the camera provider which will either succeed or fail before |
| 2905 | // reaching this method. |
| 2906 | const auto& onlinePriority = onlineClientDesc->getPriority(); |
| 2907 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( |
| 2908 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2909 | /*conflictingKeys*/ std::set<std::string>(), onlinePriority.getScore(), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2910 | onlineClientDesc->getOwnerId(), onlinePriority.getState(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2911 | // native clients don't have offline processing support. |
| 2912 | /*ommScoreOffset*/ 0, /*systemNativeClient*/false); |
Guillaume Bailey | 417e43d | 2022-11-02 15:30:24 +0100 | [diff] [blame] | 2913 | if (offlineClientDesc == nullptr) { |
| 2914 | ALOGE("%s: Offline client descriptor was NULL", __FUNCTION__); |
| 2915 | return BAD_VALUE; |
| 2916 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2917 | |
| 2918 | // Allow only one offline device per camera |
| 2919 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); |
| 2920 | if (!incompatibleClients.empty()) { |
| 2921 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); |
| 2922 | return BAD_VALUE; |
| 2923 | } |
| 2924 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2925 | std::string monitorTags = isClientWatched(offlineClient.get()) |
| 2926 | ? mMonitorTags : std::string(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2927 | auto err = offlineClient->initialize(mCameraProviderManager, monitorTags); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2928 | if (err != OK) { |
| 2929 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); |
| 2930 | return err; |
| 2931 | } |
| 2932 | |
| 2933 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); |
| 2934 | if (evicted.size() > 0) { |
| 2935 | for (auto& i : evicted) { |
| 2936 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2937 | __FUNCTION__, i->getKey().c_str()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " |
| 2941 | "properly", __FUNCTION__); |
| 2942 | |
| 2943 | return BAD_VALUE; |
| 2944 | } |
| 2945 | |
| 2946 | logConnectedOffline(offlineClientDesc->getKey(), |
| 2947 | static_cast<int>(offlineClientDesc->getOwnerId()), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2948 | offlineClient->getPackageName()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2949 | |
| 2950 | sp<IBinder> remoteCallback = offlineClient->getRemote(); |
| 2951 | if (remoteCallback != nullptr) { |
| 2952 | remoteCallback->linkToDeath(this); |
| 2953 | } |
| 2954 | } // lock is destroyed, allow further connect calls |
| 2955 | |
| 2956 | return OK; |
| 2957 | } |
| 2958 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2959 | Status CameraService::turnOnTorchWithStrengthLevel(const std::string& unresolvedCameraId, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2960 | int32_t torchStrength, const sp<IBinder>& clientBinder, int32_t deviceId, |
| 2961 | int32_t devicePolicy) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2962 | Mutex::Autolock lock(mServiceLock); |
| 2963 | |
| 2964 | ATRACE_CALL(); |
| 2965 | if (clientBinder == nullptr) { |
| 2966 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
| 2967 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2968 | "Torch client binder in null."); |
| 2969 | } |
| 2970 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2971 | int uid = getCallingUid(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2972 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
| 2973 | devicePolicy, uid); |
| 2974 | if (!cameraIdOptional.has_value()) { |
| 2975 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 2976 | unresolvedCameraId.c_str(), deviceId); |
| 2977 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2978 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 2979 | } |
| 2980 | std::string cameraId = cameraIdOptional.value(); |
| 2981 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2982 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2983 | 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] | 2984 | "for system only device %s: ", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | // verify id is valid |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2988 | auto state = getCameraState(cameraId); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2989 | if (state == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2990 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2991 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2992 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | StatusInternal cameraStatus = state->getStatus(); |
| 2996 | if (cameraStatus != StatusInternal::NOT_AVAILABLE && |
| 2997 | cameraStatus != StatusInternal::PRESENT) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2998 | 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] | 2999 | (int)cameraStatus); |
| 3000 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3001 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | { |
| 3005 | Mutex::Autolock al(mTorchStatusMutex); |
| 3006 | TorchModeStatus status; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3007 | status_t err = getTorchStatusLocked(cameraId, &status); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3008 | if (err != OK) { |
| 3009 | if (err == NAME_NOT_FOUND) { |
| 3010 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3011 | "Camera \"%s\" does not have a flash unit", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3012 | } |
| 3013 | ALOGE("%s: getting current torch status failed for camera %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3014 | __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3015 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 3016 | "Error changing torch strength level for camera \"%s\": %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3017 | cameraId.c_str(), strerror(-err), err); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
| 3021 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
| 3022 | ALOGE("%s: torch mode of camera %s is not available because " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3023 | "camera is in use.", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3024 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 3025 | "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] | 3026 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3027 | } else { |
| 3028 | ALOGE("%s: torch mode of camera %s is not available due to " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3029 | "insufficient resources", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3030 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 3031 | "Torch for camera \"%s\" is not available due to insufficient resources", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3032 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3033 | } |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | { |
| 3038 | Mutex::Autolock al(mTorchUidMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3039 | updateTorchUidMapLocked(cameraId, uid); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3040 | } |
| 3041 | // Check if the current torch strength level is same as the new one. |
| 3042 | bool shouldSkipTorchStrengthUpdates = mCameraProviderManager->shouldSkipTorchStrengthUpdate( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3043 | cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3044 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3045 | status_t err = mFlashlight->turnOnTorchWithStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3046 | |
| 3047 | if (err != OK) { |
| 3048 | int32_t errorCode; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3049 | std::string msg; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3050 | switch (err) { |
| 3051 | case -ENOSYS: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3052 | msg = fmt::sprintf("Camera \"%s\" has no flashlight.", |
| 3053 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3054 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 3055 | break; |
| 3056 | case -EBUSY: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3057 | msg = fmt::sprintf("Camera \"%s\" is in use", |
| 3058 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3059 | errorCode = ERROR_CAMERA_IN_USE; |
| 3060 | break; |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 3061 | case -EINVAL: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3062 | msg = fmt::sprintf("Torch strength level %d is not within the " |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 3063 | "valid range.", torchStrength); |
| 3064 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 3065 | break; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3066 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3067 | msg = "Changing torch strength level failed."; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3068 | errorCode = ERROR_INVALID_OPERATION; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3069 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3070 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3071 | return STATUS_ERROR(errorCode, msg.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | { |
| 3075 | // update the link to client's death |
| 3076 | // Store the last client that turns on each camera's torch mode. |
| 3077 | Mutex::Autolock al(mTorchClientMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3078 | ssize_t index = mTorchClientMap.indexOfKey(cameraId); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3079 | if (index == NAME_NOT_FOUND) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3080 | mTorchClientMap.add(cameraId, clientBinder); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3081 | } else { |
| 3082 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
| 3083 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 3084 | } |
| 3085 | clientBinder->linkToDeath(this); |
| 3086 | } |
| 3087 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3088 | int clientPid = getCallingPid(); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3089 | 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] | 3090 | __FUNCTION__, cameraId.c_str(), torchStrength, clientPid); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3091 | if (!shouldSkipTorchStrengthUpdates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3092 | broadcastTorchStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3093 | } |
| 3094 | return Status::ok(); |
| 3095 | } |
| 3096 | |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 3097 | Status CameraService::setTorchMode(const std::string& unresolvedCameraId, bool enabled, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3098 | const sp<IBinder>& clientBinder, int32_t deviceId, int32_t devicePolicy) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3099 | Mutex::Autolock lock(mServiceLock); |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3100 | |
| 3101 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3102 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3103 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3104 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 3105 | "Torch client Binder is null"); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3106 | } |
| 3107 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3108 | int uid = getCallingUid(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3109 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, deviceId, |
| 3110 | devicePolicy, uid); |
| 3111 | if (!cameraIdOptional.has_value()) { |
| 3112 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 3113 | unresolvedCameraId.c_str(), deviceId); |
| 3114 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3115 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 3116 | } |
| 3117 | std::string cameraId = cameraIdOptional.value(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3118 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3119 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3120 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3121 | " for system only device %s: ", cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3122 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3123 | // verify id is valid. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3124 | auto state = getCameraState(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3125 | if (state == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3126 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3127 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3128 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3129 | } |
| 3130 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3131 | StatusInternal cameraStatus = state->getStatus(); |
| 3132 | if (cameraStatus != StatusInternal::PRESENT && |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 3133 | cameraStatus != StatusInternal::NOT_AVAILABLE) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3134 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, cameraId.c_str(), |
| 3135 | (int)cameraStatus); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3136 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3137 | "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] | 3138 | } |
| 3139 | |
| 3140 | { |
| 3141 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3142 | TorchModeStatus status; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3143 | status_t err = getTorchStatusLocked(cameraId, &status); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3144 | if (err != OK) { |
| 3145 | if (err == NAME_NOT_FOUND) { |
| 3146 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3147 | "Camera \"%s\" does not have a flash unit", cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3148 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3149 | ALOGE("%s: getting current torch status failed for camera %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3150 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3151 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3152 | "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] | 3153 | strerror(-err), err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3154 | } |
| 3155 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3156 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 3157 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3158 | ALOGE("%s: torch mode of camera %s is not available because " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3159 | "camera is in use", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3160 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 3161 | "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] | 3162 | cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3163 | } else { |
| 3164 | ALOGE("%s: torch mode of camera %s is not available due to " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3165 | "insufficient resources", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3166 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 3167 | "Torch for camera \"%s\" is not available due to insufficient resources", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3168 | cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3169 | } |
| 3170 | } |
| 3171 | } |
| 3172 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3173 | { |
| 3174 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 3175 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 3176 | Mutex::Autolock al(mTorchUidMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3177 | updateTorchUidMapLocked(cameraId, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3180 | status_t err = mFlashlight->setTorchMode(cameraId, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3181 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3182 | if (err != OK) { |
| 3183 | int32_t errorCode; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3184 | std::string msg; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3185 | switch (err) { |
| 3186 | case -ENOSYS: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3187 | msg = fmt::sprintf("Camera \"%s\" has no flashlight", |
| 3188 | cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3189 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 3190 | break; |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 3191 | case -EBUSY: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3192 | msg = fmt::sprintf("Camera \"%s\" is in use", |
| 3193 | cameraId.c_str()); |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 3194 | errorCode = ERROR_CAMERA_IN_USE; |
| 3195 | break; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3196 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3197 | msg = fmt::sprintf( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3198 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3199 | cameraId.c_str(), enabled, strerror(-err), err); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3200 | errorCode = ERROR_INVALID_OPERATION; |
| 3201 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3202 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3203 | logServiceError(msg, errorCode); |
| 3204 | return STATUS_ERROR(errorCode, msg.c_str()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3205 | } |
| 3206 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3207 | { |
| 3208 | // update the link to client's death |
| 3209 | Mutex::Autolock al(mTorchClientMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3210 | ssize_t index = mTorchClientMap.indexOfKey(cameraId); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3211 | if (enabled) { |
| 3212 | if (index == NAME_NOT_FOUND) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3213 | mTorchClientMap.add(cameraId, clientBinder); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3214 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3215 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3216 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 3217 | } |
| 3218 | clientBinder->linkToDeath(this); |
| 3219 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3220 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3221 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3222 | } |
| 3223 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3224 | int clientPid = getCallingPid(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3225 | std::string torchState = enabled ? "on" : "off"; |
| 3226 | ALOGI("Torch for camera id %s turned %s for client PID %d", cameraId.c_str(), |
| 3227 | torchState.c_str(), clientPid); |
| 3228 | logTorchEvent(cameraId, torchState, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3229 | return Status::ok(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3230 | } |
| 3231 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3232 | void CameraService::updateTorchUidMapLocked(const std::string& cameraId, int uid) { |
| 3233 | if (mTorchUidMap.find(cameraId) == mTorchUidMap.end()) { |
| 3234 | mTorchUidMap[cameraId].first = uid; |
| 3235 | mTorchUidMap[cameraId].second = uid; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3236 | } else { |
| 3237 | // Set the pending UID |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3238 | mTorchUidMap[cameraId].first = uid; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3239 | } |
| 3240 | } |
| 3241 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3242 | Status CameraService::notifySystemEvent(int32_t eventId, |
| 3243 | const std::vector<int32_t>& args) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3244 | const int pid = getCallingPid(); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3245 | const int selfPid = getpid(); |
| 3246 | |
| 3247 | // Permission checks |
| 3248 | if (pid != selfPid) { |
| 3249 | // Ensure we're being called by system_server, or similar process with |
| 3250 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3251 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3252 | const int uid = getCallingUid(); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3253 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 3254 | " events from pid=%d, uid=%d", pid, uid); |
| 3255 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 3256 | "No permission to send updates to camera service about system events" |
| 3257 | " from pid=%d, uid=%d", pid, uid); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3258 | } |
| 3259 | } |
| 3260 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3261 | ATRACE_CALL(); |
| 3262 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3263 | switch(eventId) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3264 | case ICameraService::EVENT_USER_SWITCHED: { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3265 | // 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] | 3266 | // from a system server crash |
| 3267 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3268 | mSensorPrivacyPolicy->registerSelf(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3269 | doUserSwitch(/*newUserIds*/ args); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3270 | break; |
| 3271 | } |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 3272 | case ICameraService::EVENT_USB_DEVICE_ATTACHED: |
| 3273 | case ICameraService::EVENT_USB_DEVICE_DETACHED: { |
Pawan Wagh | 99f44e2 | 2023-07-05 21:26:43 +0000 | [diff] [blame] | 3274 | if (args.size() != 1) { |
| 3275 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, |
| 3276 | "USB Device Event requires 1 argument"); |
| 3277 | } |
| 3278 | |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 3279 | // Notify CameraProviderManager for lazy HALs |
| 3280 | mCameraProviderManager->notifyUsbDeviceEvent(eventId, |
| 3281 | std::to_string(args[0])); |
| 3282 | break; |
| 3283 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3284 | case ICameraService::EVENT_NONE: |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3285 | default: { |
| 3286 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 3287 | eventId); |
| 3288 | break; |
| 3289 | } |
| 3290 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3291 | return Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3292 | } |
| 3293 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3294 | void CameraService::notifyMonitoredUids() { |
| 3295 | Mutex::Autolock lock(mStatusListenerLock); |
| 3296 | |
| 3297 | for (const auto& it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3298 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 3299 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 3300 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3301 | } |
| 3302 | } |
| 3303 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3304 | void CameraService::notifyMonitoredUids(const std::unordered_set<uid_t> ¬ifyUidSet) { |
| 3305 | Mutex::Autolock lock(mStatusListenerLock); |
| 3306 | |
| 3307 | for (const auto& it : mListenerList) { |
| 3308 | if (notifyUidSet.find(it->getListenerUid()) != notifyUidSet.end()) { |
| 3309 | ALOGV("%s: notifying uid %d", __FUNCTION__, it->getListenerUid()); |
| 3310 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
| 3311 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 3312 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
| 3313 | } |
| 3314 | } |
| 3315 | } |
| 3316 | |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3317 | Status CameraService::notifyDeviceStateChange(int64_t newState) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3318 | const int pid = getCallingPid(); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3319 | const int selfPid = getpid(); |
| 3320 | |
| 3321 | // Permission checks |
| 3322 | if (pid != selfPid) { |
| 3323 | // Ensure we're being called by system_server, or similar process with |
| 3324 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3325 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3326 | const int uid = getCallingUid(); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3327 | ALOGE("Permission Denial: cannot send updates to camera service about device" |
| 3328 | " state changes from pid=%d, uid=%d", pid, uid); |
| 3329 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 3330 | "No permission to send updates to camera service about device state" |
| 3331 | " changes from pid=%d, uid=%d", pid, uid); |
| 3332 | } |
| 3333 | } |
| 3334 | |
| 3335 | ATRACE_CALL(); |
| 3336 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3337 | { |
| 3338 | Mutex::Autolock lock(mServiceLock); |
| 3339 | mDeviceState = newState; |
| 3340 | } |
| 3341 | |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 3342 | mCameraProviderManager->notifyDeviceStateChange(newState); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3343 | |
| 3344 | return Status::ok(); |
| 3345 | } |
| 3346 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3347 | Status CameraService::notifyDisplayConfigurationChange() { |
| 3348 | ATRACE_CALL(); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3349 | const int callingPid = getCallingPid(); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3350 | const int selfPid = getpid(); |
| 3351 | |
| 3352 | // Permission checks |
| 3353 | if (callingPid != selfPid) { |
| 3354 | // Ensure we're being called by system_server, or similar process with |
| 3355 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3356 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3357 | const int uid = getCallingUid(); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3358 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" |
| 3359 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 3360 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 3361 | "No permission to send updates to camera service about orientation" |
| 3362 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 3363 | } |
| 3364 | } |
| 3365 | |
| 3366 | Mutex::Autolock lock(mServiceLock); |
| 3367 | |
| 3368 | // Don't do anything if rotate-and-crop override via cmd is active |
| 3369 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); |
| 3370 | |
| 3371 | const auto clients = mActiveClientManager.getAll(); |
| 3372 | for (auto& current : clients) { |
| 3373 | if (current != nullptr) { |
| 3374 | const auto basicClient = current->getValue(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3375 | if (basicClient.get() != nullptr && !basicClient->getOverrideToPortrait()) { |
| 3376 | basicClient->setRotateAndCropOverride( |
| 3377 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 3378 | basicClient->getPackageName(), |
| 3379 | basicClient->getCameraFacing(), |
| 3380 | multiuser_get_user_id(basicClient->getClientUid()))); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3381 | } |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | return Status::ok(); |
| 3386 | } |
| 3387 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3388 | // TODO(b/291736219): This to be made device-aware. |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3389 | Status CameraService::getConcurrentCameraIds( |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3390 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { |
| 3391 | ATRACE_CALL(); |
| 3392 | if (!concurrentCameraIds) { |
| 3393 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); |
| 3394 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); |
| 3395 | } |
| 3396 | |
| 3397 | if (!mInitialized) { |
| 3398 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3399 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3400 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 3401 | "Camera subsystem is not available"); |
| 3402 | } |
| 3403 | // First call into the provider and get the set of concurrent camera |
| 3404 | // combinations |
| 3405 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = |
Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 3406 | mCameraProviderManager->getConcurrentCameraIds(); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3407 | for (auto &combination : concurrentCameraCombinations) { |
| 3408 | std::vector<std::string> validCombination; |
| 3409 | for (auto &cameraId : combination) { |
| 3410 | // if the camera state is not present, skip |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3411 | auto state = getCameraState(cameraId); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3412 | if (state == nullptr) { |
| 3413 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); |
| 3414 | continue; |
| 3415 | } |
| 3416 | StatusInternal status = state->getStatus(); |
| 3417 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { |
| 3418 | continue; |
| 3419 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3420 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3421 | continue; |
| 3422 | } |
| 3423 | validCombination.push_back(cameraId); |
| 3424 | } |
| 3425 | if (validCombination.size() != 0) { |
| 3426 | concurrentCameraIds->push_back(std::move(validCombination)); |
| 3427 | } |
| 3428 | } |
| 3429 | return Status::ok(); |
| 3430 | } |
| 3431 | |
| 3432 | Status CameraService::isConcurrentSessionConfigurationSupported( |
| 3433 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 3434 | int targetSdkVersion, /*out*/bool* isSupported) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3435 | if (!isSupported) { |
| 3436 | ALOGE("%s: isSupported is NULL", __FUNCTION__); |
| 3437 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); |
| 3438 | } |
| 3439 | |
| 3440 | if (!mInitialized) { |
| 3441 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 3442 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 3443 | "Camera subsystem is not available"); |
| 3444 | } |
| 3445 | |
| 3446 | // Check for camera permissions |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3447 | int callingPid = getCallingPid(); |
| 3448 | int callingUid = getCallingUid(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 3449 | bool hasCameraPermission = ((callingPid == getpid()) || |
| 3450 | hasPermissionsForCamera(callingPid, callingUid)); |
| 3451 | if (!hasCameraPermission) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3452 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 3453 | "android.permission.CAMERA needed to call" |
| 3454 | "isConcurrentSessionConfigurationSupported"); |
| 3455 | } |
| 3456 | |
| 3457 | status_t res = |
| 3458 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 3459 | cameraIdsAndSessionConfigurations, mPerfClassPrimaryCameraIds, |
| 3460 | targetSdkVersion, isSupported); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3461 | if (res != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3462 | logServiceError("Unable to query session configuration support", |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3463 | ERROR_INVALID_OPERATION); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3464 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " |
| 3465 | "support %s (%d)", strerror(-res), res); |
| 3466 | } |
| 3467 | return Status::ok(); |
| 3468 | } |
| 3469 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3470 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, |
| 3471 | /*out*/ |
| 3472 | std::vector<hardware::CameraStatus> *cameraStatuses) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3473 | return addListenerHelper(listener, cameraStatuses); |
| 3474 | } |
| 3475 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3476 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, |
| 3477 | std::vector<hardware::CameraStatus>* cameraStatuses) { |
| 3478 | return addListenerHelper(listener, cameraStatuses, false, true); |
| 3479 | } |
| 3480 | |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3481 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, |
| 3482 | /*out*/ |
| 3483 | std::vector<hardware::CameraStatus> *cameraStatuses, |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3484 | bool isVendorListener, bool isProcessLocalTest) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3485 | ATRACE_CALL(); |
| 3486 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3487 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3488 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 3489 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3490 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3491 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3492 | } |
| 3493 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3494 | auto clientPid = getCallingPid(); |
| 3495 | auto clientUid = getCallingUid(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 3496 | bool openCloseCallbackAllowed = hasPermissionsForOpenCloseListener(clientPid, clientUid); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3497 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3498 | Mutex::Autolock lock(mServiceLock); |
| 3499 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3500 | { |
| 3501 | Mutex::Autolock lock(mStatusListenerLock); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3502 | for (const auto &it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3503 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3504 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 3505 | __FUNCTION__, listener.get()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3506 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3507 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3508 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3509 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3510 | sp<ServiceListener> serviceListener = |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3511 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, |
| 3512 | openCloseCallbackAllowed); |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3513 | auto ret = serviceListener->initialize(isProcessLocalTest); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3514 | if (ret != NO_ERROR) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3515 | std::string msg = fmt::sprintf("Failed to initialize service listener: %s (%d)", |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3516 | strerror(-ret), ret); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3517 | logServiceError(msg, ERROR_ILLEGAL_ARGUMENT); |
| 3518 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3519 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3520 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3521 | // The listener still needs to be added to the list of listeners, regardless of what |
| 3522 | // permissions the listener process has / whether it is a vendor listener. Since it might be |
| 3523 | // eligible to listen to other camera ids. |
| 3524 | mListenerList.emplace_back(serviceListener); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3525 | mUidPolicy->registerMonitorUid(clientUid, /*openCamera*/false); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3526 | } |
| 3527 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3528 | /* Collect current devices and status */ |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3529 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3530 | Mutex::Autolock lock(mCameraStatesLock); |
| 3531 | for (auto& i : mCameraStates) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3532 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 3533 | auto [deviceId, mappedCameraId] = |
| 3534 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(i.first); |
| 3535 | |
| 3536 | cameraStatuses->emplace_back(mappedCameraId, |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 3537 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds(), |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3538 | openCloseCallbackAllowed ? i.second->getClientPackage() : std::string(), |
| 3539 | deviceId); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3540 | } |
| 3541 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3542 | // Remove the camera statuses that should be hidden from the client, we do |
| 3543 | // this after collecting the states in order to avoid holding |
| 3544 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at |
| 3545 | // the same time. |
| 3546 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), |
| 3547 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3548 | std::string cameraId = s.cameraId; |
| 3549 | std::optional<std::string> cameraIdOptional = resolveCameraId(s.cameraId, |
| 3550 | s.deviceId, IVirtualDeviceManagerNative::DEVICE_POLICY_CUSTOM, |
| 3551 | clientUid); |
| 3552 | if (!cameraIdOptional.has_value()) { |
| 3553 | std::string msg = |
| 3554 | fmt::sprintf( |
| 3555 | "Camera %s: Invalid camera id for device id %d", |
| 3556 | s.cameraId.c_str(), s.deviceId); |
| 3557 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3558 | return true; |
| 3559 | } |
| 3560 | cameraId = cameraIdOptional.value(); |
| 3561 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 3562 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 3563 | ALOGE("%s: Invalid camera id %s, skipping status update", |
| 3564 | __FUNCTION__, s.cameraId.c_str()); |
| 3565 | return true; |
| 3566 | } |
| 3567 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, |
| 3568 | clientUid); |
| 3569 | }), cameraStatuses->end()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3570 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3571 | // cameraStatuses will have non-eligible camera ids removed. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3572 | std::set<std::string> idsChosenForCallback; |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3573 | for (const auto &s : *cameraStatuses) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3574 | // Add only default device cameras here, as virtual cameras currently don't support torch |
| 3575 | // anyway. Note that this is a simplification of the implementation here, and we should |
| 3576 | // change this when virtual cameras support torch. |
| 3577 | if (s.deviceId == kDefaultDeviceId) { |
| 3578 | idsChosenForCallback.insert(s.cameraId); |
| 3579 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3580 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3581 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3582 | /* |
| 3583 | * Immediately signal current torch status to this listener only |
| 3584 | * This may be a subset of all the devices, so don't include it in the response directly |
| 3585 | */ |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3586 | { |
| 3587 | Mutex::Autolock al(mTorchStatusMutex); |
| 3588 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3589 | const std::string &id = mTorchStatusMap.keyAt(i); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3590 | // The camera id is visible to the client. Fine to send torch |
| 3591 | // callback. |
| 3592 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3593 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id, |
| 3594 | kDefaultDeviceId); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3595 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3596 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3597 | } |
| 3598 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3599 | return Status::ok(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3600 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3601 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3602 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3603 | ATRACE_CALL(); |
| 3604 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3605 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 3606 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3607 | if (listener == 0) { |
| 3608 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3609 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3610 | } |
| 3611 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3612 | Mutex::Autolock lock(mServiceLock); |
| 3613 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3614 | { |
| 3615 | Mutex::Autolock lock(mStatusListenerLock); |
| 3616 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3617 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3618 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid(), /*closeCamera*/false); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3619 | IInterface::asBinder(listener)->unlinkToDeath(*it); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3620 | mListenerList.erase(it); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3621 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3622 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 3627 | __FUNCTION__, listener.get()); |
| 3628 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3629 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3630 | } |
| 3631 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3632 | Status CameraService::getLegacyParameters(int cameraId, /*out*/std::string* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3633 | |
| 3634 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3635 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 3636 | |
| 3637 | if (parameters == NULL) { |
| 3638 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3639 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3640 | } |
| 3641 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3642 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3643 | |
| 3644 | CameraParameters shimParams; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3645 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3646 | // Error logged by caller |
| 3647 | return ret; |
| 3648 | } |
| 3649 | |
| 3650 | String8 shimParamsString8 = shimParams.flatten(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3651 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3652 | *parameters = toStdString(shimParamsString8); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3653 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3654 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3655 | } |
| 3656 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3657 | Status CameraService::supportsCameraApi(const std::string& unresolvedCameraId, int apiVersion, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3658 | /*out*/ bool *isSupported) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3659 | ATRACE_CALL(); |
| 3660 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 3661 | const std::string cameraId = resolveCameraId( |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3662 | unresolvedCameraId, getCallingUid()); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 3663 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3664 | ALOGV("%s: for camera ID = %s", __FUNCTION__, cameraId.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3665 | |
| 3666 | switch (apiVersion) { |
| 3667 | case API_VERSION_1: |
| 3668 | case API_VERSION_2: |
| 3669 | break; |
| 3670 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3671 | std::string msg = fmt::sprintf("Unknown API version %d", apiVersion); |
| 3672 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3673 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3674 | } |
| 3675 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3676 | int portraitRotation; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3677 | auto deviceVersionAndTransport = getDeviceVersion(cameraId, false, &portraitRotation); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3678 | if (deviceVersionAndTransport.first == -1) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3679 | std::string msg = fmt::sprintf("Unknown camera ID %s", cameraId.c_str()); |
| 3680 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3681 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3682 | } |
| 3683 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 3684 | int deviceVersion = deviceVersionAndTransport.first; |
| 3685 | switch (deviceVersion) { |
| 3686 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 3687 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 3688 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 3689 | if (apiVersion == API_VERSION_2) { |
| 3690 | 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] | 3691 | "shim", __FUNCTION__, cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3692 | *isSupported = false; |
| 3693 | } else { // if (apiVersion == API_VERSION_1) { |
| 3694 | 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] | 3695 | "supported", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3696 | *isSupported = true; |
| 3697 | } |
| 3698 | break; |
| 3699 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 3700 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 3701 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 3702 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 3703 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 3704 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 3705 | 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] | 3706 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3707 | *isSupported = true; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3708 | break; |
| 3709 | default: { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3710 | std::string msg = fmt::sprintf("Unknown device version %x for device %s", |
| 3711 | deviceVersion, cameraId.c_str()); |
| 3712 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3713 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3714 | } |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3715 | } |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3716 | } else { |
| 3717 | *isSupported = true; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3718 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3719 | return Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3720 | } |
| 3721 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3722 | Status CameraService::isHiddenPhysicalCamera(const std::string& unresolvedCameraId, |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3723 | /*out*/ bool *isSupported) { |
| 3724 | ATRACE_CALL(); |
| 3725 | |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 3726 | const std::string cameraId = resolveCameraId(unresolvedCameraId, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3727 | getCallingUid()); |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3728 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3729 | ALOGV("%s: for camera ID = %s", __FUNCTION__, cameraId.c_str()); |
| 3730 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(cameraId); |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3731 | |
| 3732 | return Status::ok(); |
| 3733 | } |
| 3734 | |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3735 | Status CameraService::injectCamera( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3736 | const std::string& packageName, const std::string& internalCamId, |
| 3737 | const std::string& externalCamId, |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3738 | const sp<ICameraInjectionCallback>& callback, |
| 3739 | /*out*/ |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3740 | sp<ICameraInjectionSession>* cameraInjectionSession) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3741 | ATRACE_CALL(); |
| 3742 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3743 | if (!checkCallingPermission(toString16(sCameraInjectExternalCameraPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3744 | const int pid = getCallingPid(); |
| 3745 | const int uid = getCallingUid(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3746 | ALOGE("Permission Denial: can't inject camera pid=%d, uid=%d", pid, uid); |
| 3747 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3748 | "Permission Denial: no permission to inject camera"); |
| 3749 | } |
| 3750 | |
| 3751 | // Do not allow any camera injection that injects or replaces a virtual camera. |
| 3752 | auto [deviceIdForInternalCamera, _] = |
| 3753 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(internalCamId); |
| 3754 | if (deviceIdForInternalCamera != kDefaultDeviceId) { |
| 3755 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED, |
| 3756 | "Cannot replace a virtual camera"); |
| 3757 | } |
| 3758 | [[maybe_unused]] auto [deviceIdForExternalCamera, unusedMappedCameraId] = |
| 3759 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(externalCamId); |
| 3760 | if (deviceIdForExternalCamera != kDefaultDeviceId) { |
| 3761 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED, |
| 3762 | "Cannot inject a virtual camera to replace an internal camera"); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | ALOGV( |
| 3766 | "%s: Package name = %s, Internal camera ID = %s, External camera ID = " |
| 3767 | "%s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3768 | __FUNCTION__, packageName.c_str(), |
| 3769 | internalCamId.c_str(), externalCamId.c_str()); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3770 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3771 | { |
| 3772 | Mutex::Autolock lock(mInjectionParametersLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3773 | mInjectionInternalCamId = internalCamId; |
| 3774 | mInjectionExternalCamId = externalCamId; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3775 | mInjectionStatusListener->addListener(callback); |
| 3776 | *cameraInjectionSession = new CameraInjectionSession(this); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3777 | status_t res = NO_ERROR; |
| 3778 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 3779 | // If the client already exists, we can directly connect to the camera device through the |
| 3780 | // client's injectCamera(), otherwise we need to wait until the client is established |
| 3781 | // (execute connectHelper()) before injecting the camera to the camera device. |
| 3782 | if (clientDescriptor != nullptr) { |
| 3783 | mInjectionInitPending = false; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3784 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 3785 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 3786 | if(res != OK) { |
| 3787 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 3788 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3789 | mInjectionExternalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3790 | } |
| 3791 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3792 | if (res != OK) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3793 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 3794 | } |
| 3795 | } else { |
| 3796 | mInjectionInitPending = true; |
| 3797 | } |
| 3798 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3799 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3800 | return binder::Status::ok(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3801 | } |
| 3802 | |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 3803 | Status CameraService::reportExtensionSessionStats( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3804 | const hardware::CameraExtensionSessionStats& stats, std::string* sessionKey /*out*/) { |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 3805 | ALOGV("%s: reported %s", __FUNCTION__, stats.toString().c_str()); |
| 3806 | *sessionKey = mCameraServiceProxyWrapper->updateExtensionStats(stats); |
| 3807 | return Status::ok(); |
| 3808 | } |
| 3809 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3810 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3811 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3812 | for (auto& i : mActiveClientManager.getAll()) { |
| 3813 | auto clientSp = i->getValue(); |
| 3814 | if (clientSp.get() == client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3815 | cacheClientTagDumpIfNeeded(client->mCameraIdStr, clientSp.get()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3816 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3817 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3818 | } |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3819 | updateAudioRestrictionLocked(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3820 | } |
| 3821 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3822 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3823 | bool ret = false; |
| 3824 | { |
| 3825 | // Acquire mServiceLock and prevent other clients from connecting |
| 3826 | std::unique_ptr<AutoConditionLock> lock = |
| 3827 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3828 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3829 | std::vector<sp<BasicClient>> evicted; |
| 3830 | for (auto& i : mActiveClientManager.getAll()) { |
| 3831 | auto clientSp = i->getValue(); |
| 3832 | if (clientSp.get() == nullptr) { |
| 3833 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3834 | mActiveClientManager.remove(i); |
| 3835 | continue; |
| 3836 | } |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 3837 | if (remote == clientSp->getRemote()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3838 | mActiveClientManager.remove(i); |
| 3839 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3840 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3841 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3842 | clientSp->notifyError( |
| 3843 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3844 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3845 | } |
| 3846 | } |
| 3847 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3848 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 3849 | // other clients from connecting in mServiceLockWrapper if held |
| 3850 | mServiceLock.unlock(); |
| 3851 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3852 | // Do not clear caller identity, remote caller should be client proccess |
| 3853 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3854 | for (auto& i : evicted) { |
| 3855 | if (i.get() != nullptr) { |
| 3856 | i->disconnect(); |
| 3857 | ret = true; |
| 3858 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3859 | } |
| 3860 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3861 | // Reacquire mServiceLock |
| 3862 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3863 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3864 | } // lock is destroyed, allow further connect calls |
| 3865 | |
| 3866 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3867 | } |
| 3868 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3869 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3870 | const std::string& cameraId) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3871 | std::shared_ptr<CameraState> state; |
| 3872 | { |
| 3873 | Mutex::Autolock lock(mCameraStatesLock); |
| 3874 | auto iter = mCameraStates.find(cameraId); |
| 3875 | if (iter != mCameraStates.end()) { |
| 3876 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3877 | } |
| 3878 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3879 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3880 | } |
| 3881 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3882 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const std::string& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3883 | // Remove from active clients list |
| 3884 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 3885 | if (clientDescriptorPtr == nullptr) { |
| 3886 | 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] | 3887 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3888 | return sp<BasicClient>{nullptr}; |
| 3889 | } |
| 3890 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3891 | sp<BasicClient> client = clientDescriptorPtr->getValue(); |
| 3892 | if (client.get() != nullptr) { |
| 3893 | cacheClientTagDumpIfNeeded(clientDescriptorPtr->getKey(), client.get()); |
| 3894 | } |
| 3895 | return client; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 3896 | } |
| 3897 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3898 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3899 | // Acquire mServiceLock and prevent other clients from connecting |
| 3900 | std::unique_ptr<AutoConditionLock> lock = |
| 3901 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 3902 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3903 | std::set<userid_t> newAllowedUsers; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3904 | for (size_t i = 0; i < newUserIds.size(); i++) { |
| 3905 | if (newUserIds[i] < 0) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3906 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3907 | __FUNCTION__, newUserIds[i]); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3908 | return; |
| 3909 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3910 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3911 | } |
| 3912 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3913 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3914 | if (newAllowedUsers == mAllowedUsers) { |
| 3915 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 3916 | return; |
| 3917 | } |
| 3918 | |
| 3919 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 3920 | |
| 3921 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3922 | |
| 3923 | // Current user has switched, evict all current clients. |
| 3924 | std::vector<sp<BasicClient>> evicted; |
| 3925 | for (auto& i : mActiveClientManager.getAll()) { |
| 3926 | auto clientSp = i->getValue(); |
| 3927 | |
| 3928 | if (clientSp.get() == nullptr) { |
| 3929 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3930 | continue; |
| 3931 | } |
| 3932 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3933 | // Don't evict clients that are still allowed. |
| 3934 | uid_t clientUid = clientSp->getClientUid(); |
| 3935 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 3936 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 3937 | continue; |
| 3938 | } |
| 3939 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3940 | evicted.push_back(clientSp); |
| 3941 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3942 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3943 | i->getKey().c_str()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3944 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3945 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3946 | logEvent(fmt::sprintf("EVICT device %s client held by package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3947 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3948 | " to user switch.", i->getKey().c_str(), |
| 3949 | clientSp->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3950 | i->getOwnerId(), i->getPriority().getScore(), |
| 3951 | i->getPriority().getState())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3952 | |
| 3953 | } |
| 3954 | |
| 3955 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 3956 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 3957 | mServiceLock.unlock(); |
| 3958 | |
| 3959 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3960 | int64_t token = clearCallingIdentity(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3961 | |
| 3962 | for (auto& i : evicted) { |
| 3963 | i->disconnect(); |
| 3964 | } |
| 3965 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3966 | restoreCallingIdentity(token); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3967 | |
| 3968 | // Reacquire mServiceLock |
| 3969 | mServiceLock.lock(); |
| 3970 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3971 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3972 | void CameraService::logEvent(const std::string &event) { |
| 3973 | std::string curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3974 | Mutex::Autolock l(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3975 | std::string msg = curTime + " : " + event; |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3976 | // For service error events, print the msg only once. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3977 | if (msg.find("SERVICE ERROR") != std::string::npos) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3978 | mEventLog.add(msg); |
| 3979 | } else if(sServiceErrorEventSet.find(msg) == sServiceErrorEventSet.end()) { |
| 3980 | // Error event not added to the dumpsys log before |
| 3981 | mEventLog.add(msg); |
| 3982 | sServiceErrorEventSet.insert(msg); |
| 3983 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3984 | } |
| 3985 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3986 | void CameraService::logDisconnected(const std::string &cameraId, int clientPid, |
| 3987 | const std::string &clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3988 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3989 | logEvent(fmt::sprintf("DISCONNECT device %s client for package %s (PID %d)", cameraId.c_str(), |
| 3990 | clientPackage.c_str(), clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3991 | } |
| 3992 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3993 | void CameraService::logDisconnectedOffline(const std::string &cameraId, int clientPid, |
| 3994 | const std::string &clientPackage) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3995 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3996 | logEvent(fmt::sprintf("DISCONNECT offline device %s client for package %s (PID %d)", |
| 3997 | cameraId.c_str(), clientPackage.c_str(), clientPid)); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3998 | } |
| 3999 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4000 | void CameraService::logConnected(const std::string &cameraId, int clientPid, |
| 4001 | const std::string &clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4002 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4003 | logEvent(fmt::sprintf("CONNECT device %s client for package %s (PID %d)", cameraId.c_str(), |
| 4004 | clientPackage.c_str(), clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4005 | } |
| 4006 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4007 | void CameraService::logConnectedOffline(const std::string &cameraId, int clientPid, |
| 4008 | const std::string &clientPackage) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 4009 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4010 | logEvent(fmt::sprintf("CONNECT offline device %s client for package %s (PID %d)", |
| 4011 | cameraId.c_str(), clientPackage.c_str(), clientPid)); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 4012 | } |
| 4013 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4014 | void CameraService::logRejected(const std::string &cameraId, int clientPid, |
| 4015 | const std::string &clientPackage, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4016 | // Log the client rejected |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4017 | logEvent(fmt::sprintf("REJECT device %s client for package %s (PID %d), reason: (%s)", |
| 4018 | cameraId.c_str(), clientPackage.c_str(), clientPid, reason.c_str())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4019 | } |
| 4020 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4021 | void CameraService::logTorchEvent(const std::string &cameraId, const std::string &torchState, |
| 4022 | int clientPid) { |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 4023 | // Log torch event |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4024 | logEvent(fmt::sprintf("Torch for camera id %s turned %s for client PID %d", cameraId.c_str(), |
| 4025 | torchState.c_str(), clientPid)); |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 4026 | } |
| 4027 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4028 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 4029 | const std::set<userid_t>& newUserIds) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4030 | std::string newUsers = toString(newUserIds); |
| 4031 | std::string oldUsers = toString(oldUserIds); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4032 | if (oldUsers.size() == 0) { |
| 4033 | oldUsers = "<None>"; |
| 4034 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4035 | // Log the new and old users |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4036 | logEvent(fmt::sprintf("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s", |
| 4037 | oldUsers.c_str(), newUsers.c_str())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4038 | } |
| 4039 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4040 | void CameraService::logDeviceRemoved(const std::string &cameraId, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4041 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4042 | 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] | 4043 | } |
| 4044 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4045 | void CameraService::logDeviceAdded(const std::string &cameraId, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4046 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4047 | 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] | 4048 | } |
| 4049 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4050 | void CameraService::logClientDied(int clientPid, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4051 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4052 | 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] | 4053 | } |
| 4054 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4055 | void CameraService::logServiceError(const std::string &msg, int errorCode) { |
| 4056 | logEvent(fmt::sprintf("SERVICE ERROR: %s : %d (%s)", msg.c_str(), errorCode, |
| 4057 | strerror(-errorCode))); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4058 | } |
| 4059 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4060 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 4061 | uint32_t flags) { |
| 4062 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4063 | // Permission checks |
| 4064 | switch (code) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4065 | case SHELL_COMMAND_TRANSACTION: { |
| 4066 | int in = data.readFileDescriptor(); |
| 4067 | int out = data.readFileDescriptor(); |
| 4068 | int err = data.readFileDescriptor(); |
| 4069 | int argc = data.readInt32(); |
| 4070 | Vector<String16> args; |
| 4071 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 4072 | args.add(data.readString16()); |
| 4073 | } |
| 4074 | sp<IBinder> unusedCallback; |
| 4075 | sp<IResultReceiver> resultReceiver; |
| 4076 | status_t status; |
| 4077 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 4078 | return status; |
| 4079 | } |
| 4080 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 4081 | return status; |
| 4082 | } |
| 4083 | status = shellCommand(in, out, err, args); |
| 4084 | if (resultReceiver != nullptr) { |
| 4085 | resultReceiver->send(status); |
| 4086 | } |
| 4087 | return NO_ERROR; |
| 4088 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | return BnCameraService::onTransact(code, data, reply, flags); |
| 4092 | } |
| 4093 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4094 | // We share the media players for shutter and recording sound for all clients. |
| 4095 | // A reference count is kept to determine when we will actually release the |
| 4096 | // media players. |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 4097 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { |
| 4098 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 4099 | status_t error; |
| 4100 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 4101 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 4102 | error = mp->prepare(); |
| 4103 | } |
| 4104 | if (error != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4105 | ALOGE("Failed to load CameraService sounds: %s", file); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 4106 | mp->disconnect(); |
| 4107 | mp.clear(); |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4108 | return nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4109 | } |
| 4110 | return mp; |
| 4111 | } |
| 4112 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4113 | void CameraService::increaseSoundRef() { |
| 4114 | Mutex::Autolock lock(mSoundLock); |
| 4115 | mSoundRef++; |
| 4116 | } |
| 4117 | |
| 4118 | void CameraService::loadSoundLocked(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4119 | ATRACE_CALL(); |
| 4120 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4121 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); |
| 4122 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { |
| 4123 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); |
| 4124 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { |
| 4125 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 4126 | } |
| 4127 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) { |
| 4128 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); |
| 4129 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { |
| 4130 | mSoundPlayer[SOUND_RECORDING_START] = |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4131 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4132 | } |
| 4133 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { |
| 4134 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); |
| 4135 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { |
| 4136 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
| 4137 | } |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4138 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4139 | } |
| 4140 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4141 | void CameraService::decreaseSoundRef() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4142 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4143 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4144 | if (--mSoundRef) return; |
| 4145 | |
| 4146 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 4147 | if (mSoundPlayer[i] != 0) { |
| 4148 | mSoundPlayer[i]->disconnect(); |
| 4149 | mSoundPlayer[i].clear(); |
| 4150 | } |
| 4151 | } |
| 4152 | } |
| 4153 | |
| 4154 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4155 | ATRACE_CALL(); |
| 4156 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4157 | LOG1("playSound(%d)", kind); |
Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame] | 4158 | if (kind < 0 || kind >= NUM_SOUNDS) { |
| 4159 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); |
| 4160 | return; |
| 4161 | } |
| 4162 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4163 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4164 | loadSoundLocked(kind); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4165 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 4166 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 4167 | player->seekTo(0); |
| 4168 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4169 | } |
| 4170 | } |
| 4171 | |
| 4172 | // ---------------------------------------------------------------------------- |
| 4173 | |
| 4174 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 4175 | const sp<ICameraClient>& cameraClient, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4176 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4177 | const std::string& clientPackageName, bool systemNativeClient, |
| 4178 | const std::optional<std::string>& clientFeatureId, |
| 4179 | const std::string& cameraIdStr, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4180 | int api1CameraId, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4181 | int clientPid, uid_t clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 4182 | int servicePid, bool overrideToPortrait) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 4183 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 4184 | IInterface::asBinder(cameraClient), |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4185 | attributionAndPermissionUtils, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4186 | clientPackageName, systemNativeClient, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4187 | cameraIdStr, cameraFacing, sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4188 | clientPid, clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 4189 | servicePid, overrideToPortrait), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4190 | mCameraId(api1CameraId) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4191 | { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4192 | int callingPid = getCallingPid(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4193 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4194 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 4195 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4196 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4197 | cameraService->increaseSoundRef(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4198 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4199 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4200 | } |
| 4201 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4202 | // tear down the client |
| 4203 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4204 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4205 | mDestructionStarted = true; |
| 4206 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4207 | sCameraService->decreaseSoundRef(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 4208 | // unconditionally disconnect. function is idempotent |
| 4209 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4210 | } |
| 4211 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4212 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; |
| 4213 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4214 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4215 | const sp<IBinder>& remoteCallback, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4216 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4217 | const std::string& clientPackageName, bool nativeClient, |
| 4218 | const std::optional<std::string>& clientFeatureId, const std::string& cameraIdStr, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4219 | int cameraFacing, int sensorOrientation, int clientPid, uid_t clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 4220 | int servicePid, bool overrideToPortrait): |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4221 | AttributionAndPermissionUtilsEncapsulator(attributionAndPermissionUtils), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4222 | mDestructionStarted(false), |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4223 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), mOrientation(sensorOrientation), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4224 | mClientPackageName(clientPackageName), mSystemNativeClient(nativeClient), |
| 4225 | mClientFeatureId(clientFeatureId), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 4226 | mClientPid(clientPid), mClientUid(clientUid), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4227 | mServicePid(servicePid), |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 4228 | mDisconnected(false), mUidIsTrusted(false), |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 4229 | mOverrideToPortrait(overrideToPortrait), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4230 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4231 | mRemoteBinder(remoteCallback), |
| 4232 | mOpsActive(false), |
| 4233 | mOpsStreaming(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4234 | { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4235 | if (sCameraService == nullptr) { |
| 4236 | sCameraService = cameraService; |
| 4237 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 4238 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4239 | // There are 2 scenarios in which a client won't have AppOps operations |
| 4240 | // (both scenarios : native clients) |
| 4241 | // 1) It's an system native client*, the package name will be empty |
| 4242 | // and it will return from this function in the previous if condition |
| 4243 | // (This is the same as the previously existing behavior). |
| 4244 | // 2) It is a system native client, but its package name has been |
| 4245 | // modified for debugging, however it still must not use AppOps since |
| 4246 | // the package name is not a real one. |
| 4247 | // |
| 4248 | // * system native client - native client with UID < AID_APP_START. It |
| 4249 | // doesn't exclude clients not on the system partition. |
| 4250 | if (!mSystemNativeClient) { |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4251 | mAppOpsManager = std::make_unique<AppOpsManager>(); |
| 4252 | } |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 4253 | |
Charles Chen | f075f08 | 2024-03-04 23:32:55 +0000 | [diff] [blame] | 4254 | mUidIsTrusted = isTrustedCallingUid(mClientUid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4258 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4259 | mDestructionStarted = true; |
| 4260 | } |
| 4261 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4262 | binder::Status CameraService::BasicClient::disconnect() { |
| 4263 | binder::Status res = Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4264 | if (mDisconnected) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4265 | return res; |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4266 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 4267 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4268 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4269 | sCameraService->removeByClient(this); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4270 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, mClientPackageName); |
Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 4271 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4272 | mCameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4273 | |
| 4274 | sp<IBinder> remote = getRemote(); |
| 4275 | if (remote != nullptr) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4276 | remote->unlinkToDeath(sCameraService); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4277 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4278 | |
| 4279 | finishCameraOps(); |
Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 4280 | // Notify flashlight that a camera device is closed. |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4281 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4282 | 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] | 4283 | mClientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4284 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4285 | // client shouldn't be able to call into us anymore |
| 4286 | mClientPid = 0; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4287 | |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 4288 | const auto& mActivityManager = getActivityManager(); |
| 4289 | if (mActivityManager) { |
| 4290 | mActivityManager->logFgsApiEnd(LOG_FGS_CAMERA_API, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4291 | getCallingUid(), |
| 4292 | getCallingPid()); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4295 | return res; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4296 | } |
| 4297 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 4298 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 4299 | // No dumping of clients directly over Binder, |
| 4300 | // must go through CameraService::dump |
| 4301 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4302 | getCallingUid(), NULL, 0); |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 4303 | return OK; |
| 4304 | } |
| 4305 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4306 | status_t CameraService::BasicClient::startWatchingTags(const std::string&, int) { |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4307 | // Can't watch tags directly, must go through CameraService::startWatchingTags |
| 4308 | return OK; |
| 4309 | } |
| 4310 | |
| 4311 | status_t CameraService::BasicClient::stopWatchingTags(int) { |
| 4312 | // Can't watch tags directly, must go through CameraService::stopWatchingTags |
| 4313 | return OK; |
| 4314 | } |
| 4315 | |
| 4316 | status_t CameraService::BasicClient::dumpWatchedEventsToVector(std::vector<std::string> &) { |
| 4317 | // Can't watch tags directly, must go through CameraService::dumpWatchedEventsToVector |
| 4318 | return OK; |
| 4319 | } |
| 4320 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4321 | std::string CameraService::BasicClient::getPackageName() const { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 4322 | return mClientPackageName; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4323 | } |
| 4324 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4325 | int CameraService::BasicClient::getCameraFacing() const { |
| 4326 | return mCameraFacing; |
| 4327 | } |
| 4328 | |
| 4329 | int CameraService::BasicClient::getCameraOrientation() const { |
| 4330 | return mOrientation; |
| 4331 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4332 | |
| 4333 | int CameraService::BasicClient::getClientPid() const { |
| 4334 | return mClientPid; |
| 4335 | } |
| 4336 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4337 | uid_t CameraService::BasicClient::getClientUid() const { |
| 4338 | return mClientUid; |
| 4339 | } |
| 4340 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 4341 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 4342 | // Defaults to API2. |
| 4343 | return level == API_2; |
| 4344 | } |
| 4345 | |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 4346 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4347 | { |
| 4348 | Mutex::Autolock l(mAudioRestrictionLock); |
| 4349 | mAudioRestriction = mode; |
| 4350 | } |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 4351 | sCameraService->updateAudioRestriction(); |
| 4352 | return OK; |
| 4353 | } |
| 4354 | |
| 4355 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4356 | return sCameraService->updateAudioRestriction(); |
| 4357 | } |
| 4358 | |
| 4359 | int32_t CameraService::BasicClient::getAudioRestriction() const { |
| 4360 | Mutex::Autolock l(mAudioRestrictionLock); |
| 4361 | return mAudioRestriction; |
| 4362 | } |
| 4363 | |
| 4364 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { |
| 4365 | switch (mode) { |
| 4366 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: |
| 4367 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: |
| 4368 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: |
| 4369 | return true; |
| 4370 | default: |
| 4371 | return false; |
| 4372 | } |
| 4373 | } |
| 4374 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4375 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { |
| 4376 | if (mode == AppOpsManager::MODE_ERRORED) { |
| 4377 | ALOGI("Camera %s: Access for \"%s\" has been revoked", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4378 | mCameraIdStr.c_str(), mClientPackageName.c_str()); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4379 | return PERMISSION_DENIED; |
| 4380 | } else if (!mUidIsTrusted && mode == AppOpsManager::MODE_IGNORED) { |
| 4381 | // If the calling Uid is trusted (a native service), the AppOpsManager could |
| 4382 | // return MODE_IGNORED. Do not treat such case as error. |
| 4383 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, |
| 4384 | mClientPackageName); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4385 | |
| 4386 | bool isCameraPrivacyEnabled; |
| 4387 | if (flags::camera_privacy_allowlist()) { |
| 4388 | isCameraPrivacyEnabled = sCameraService->isCameraPrivacyEnabled( |
| 4389 | toString16(mClientPackageName), std::string(), mClientPid, mClientUid); |
| 4390 | } else { |
| 4391 | isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4392 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4393 | } |
Jyoti Bhayana | 8143e57 | 2023-01-09 08:46:49 -0800 | [diff] [blame] | 4394 | // We don't want to return EACCESS if the CameraPrivacy is enabled. |
| 4395 | // We prefer to successfully open the camera and perform camera muting |
| 4396 | // or blocking in connectHelper as handleAppOpMode can be called before the |
| 4397 | // connection has been fully established and at that time camera muting |
| 4398 | // capabilities are unknown. |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4399 | if (!isUidActive || !isCameraPrivacyEnabled) { |
| 4400 | ALOGI("Camera %s: Access for \"%s\" has been restricted", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4401 | mCameraIdStr.c_str(), mClientPackageName.c_str()); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4402 | // Return the same error as for device policy manager rejection |
| 4403 | return -EACCES; |
| 4404 | } |
| 4405 | } |
| 4406 | return OK; |
| 4407 | } |
| 4408 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4409 | status_t CameraService::BasicClient::startCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4410 | ATRACE_CALL(); |
| 4411 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4412 | { |
| 4413 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4414 | __FUNCTION__, mClientPackageName.c_str(), mClientUid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4415 | } |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4416 | if (mAppOpsManager != nullptr) { |
| 4417 | // Notify app ops that the camera is not available |
| 4418 | mOpsCallback = new OpsCallback(this); |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4419 | |
| 4420 | if (flags::watch_foreground_changes()) { |
| 4421 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 4422 | toString16(mClientPackageName), |
| 4423 | AppOpsManager::WATCH_FOREGROUND_CHANGES, mOpsCallback); |
| 4424 | } else { |
| 4425 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4426 | toString16(mClientPackageName), mOpsCallback); |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4427 | } |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4428 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4429 | // Just check for camera acccess here on open - delay startOp until |
| 4430 | // camera frames start streaming in startCameraStreamingOps |
| 4431 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4432 | toString16(mClientPackageName)); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4433 | status_t res = handleAppOpMode(mode); |
| 4434 | if (res != OK) { |
| 4435 | return res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4436 | } |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 4437 | } |
| 4438 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4439 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4440 | |
| 4441 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4442 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4443 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4444 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid, /*openCamera*/true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4445 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4446 | // Notify listeners of camera open/close status |
| 4447 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); |
| 4448 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4449 | return OK; |
| 4450 | } |
| 4451 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4452 | status_t CameraService::BasicClient::startCameraStreamingOps() { |
| 4453 | ATRACE_CALL(); |
| 4454 | |
| 4455 | if (!mOpsActive) { |
| 4456 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 4457 | return INVALID_OPERATION; |
| 4458 | } |
| 4459 | if (mOpsStreaming) { |
| 4460 | ALOGV("%s: Streaming already active!", __FUNCTION__); |
| 4461 | return OK; |
| 4462 | } |
| 4463 | |
| 4464 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4465 | __FUNCTION__, mClientPackageName.c_str(), mClientUid); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4466 | |
| 4467 | if (mAppOpsManager != nullptr) { |
| 4468 | int32_t mode = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4469 | toString16(mClientPackageName), /*startIfModeDefault*/ false, |
| 4470 | toString16(mClientFeatureId), |
| 4471 | toString16("start camera ") + toString16(mCameraIdStr)); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4472 | status_t res = handleAppOpMode(mode); |
| 4473 | if (res != OK) { |
| 4474 | return res; |
| 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | mOpsStreaming = true; |
| 4479 | |
| 4480 | return OK; |
| 4481 | } |
| 4482 | |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4483 | status_t CameraService::BasicClient::noteAppOp() { |
| 4484 | ATRACE_CALL(); |
| 4485 | |
| 4486 | ALOGV("%s: Start camera noteAppOp, package name = %s, client UID = %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4487 | __FUNCTION__, mClientPackageName.c_str(), mClientUid); |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4488 | |
| 4489 | // noteAppOp is only used for when camera mute is not supported, in order |
| 4490 | // to trigger the sensor privacy "Unblock" dialog |
| 4491 | if (mAppOpsManager != nullptr) { |
| 4492 | int32_t mode = mAppOpsManager->noteOp(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4493 | toString16(mClientPackageName), toString16(mClientFeatureId), |
| 4494 | toString16("start camera ") + toString16(mCameraIdStr)); |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4495 | status_t res = handleAppOpMode(mode); |
| 4496 | if (res != OK) { |
| 4497 | return res; |
| 4498 | } |
| 4499 | } |
| 4500 | |
| 4501 | return OK; |
| 4502 | } |
| 4503 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4504 | status_t CameraService::BasicClient::finishCameraStreamingOps() { |
| 4505 | ATRACE_CALL(); |
| 4506 | |
| 4507 | if (!mOpsActive) { |
| 4508 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 4509 | return INVALID_OPERATION; |
| 4510 | } |
| 4511 | if (!mOpsStreaming) { |
| 4512 | ALOGV("%s: Streaming not active!", __FUNCTION__); |
| 4513 | return OK; |
| 4514 | } |
| 4515 | |
| 4516 | if (mAppOpsManager != nullptr) { |
| 4517 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4518 | toString16(mClientPackageName), toString16(mClientFeatureId)); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4519 | mOpsStreaming = false; |
| 4520 | } |
| 4521 | |
| 4522 | return OK; |
| 4523 | } |
| 4524 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4525 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4526 | ATRACE_CALL(); |
| 4527 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4528 | if (mOpsStreaming) { |
| 4529 | // Make sure we've notified everyone about camera stopping |
| 4530 | finishCameraStreamingOps(); |
| 4531 | } |
| 4532 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4533 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4534 | if (mOpsActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4535 | mOpsActive = false; |
| 4536 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 4537 | // This function is called when a client disconnects. This should |
| 4538 | // release the camera, but actually only if it was in a proper |
| 4539 | // functional state, i.e. with status NOT_AVAILABLE |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4540 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 4541 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4542 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4543 | // 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] | 4544 | sCameraService->updateStatus(StatusInternal::PRESENT, |
| 4545 | mCameraIdStr, rejected); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4546 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4547 | // Always stop watching, even if no camera op is active |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4548 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 4549 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 4550 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4551 | mOpsCallback.clear(); |
| 4552 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4553 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid, /*closeCamera*/true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4554 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4555 | // Notify listeners of camera open/close status |
| 4556 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); |
| 4557 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4558 | return OK; |
| 4559 | } |
| 4560 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4561 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4562 | ATRACE_CALL(); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4563 | if (mAppOpsManager == nullptr) { |
| 4564 | return; |
| 4565 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 4566 | // TODO : add offline camera session case |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4567 | if (op != AppOpsManager::OP_CAMERA) { |
| 4568 | ALOGW("Unexpected app ops notification received: %d", op); |
| 4569 | return; |
| 4570 | } |
| 4571 | |
| 4572 | int32_t res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4573 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4574 | mClientUid, toString16(mClientPackageName)); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4575 | ALOGV("checkOp returns: %d, %s ", res, |
| 4576 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 4577 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 4578 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 4579 | "UNKNOWN"); |
| 4580 | |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4581 | if (res == AppOpsManager::MODE_ERRORED) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4582 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.c_str(), |
| 4583 | mClientPackageName.c_str()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4584 | block(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4585 | } else if (res == AppOpsManager::MODE_IGNORED) { |
| 4586 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4587 | |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4588 | // Uid may be active, but not visible to the user (e.g. PROCESS_STATE_FOREGROUND_SERVICE). |
| 4589 | // If not visible, but still active, then we want to block instead of muting the camera. |
| 4590 | int32_t procState = sCameraService->mUidPolicy->getProcState(mClientUid); |
| 4591 | bool isUidVisible = (procState <= ActivityManager::PROCESS_STATE_BOUND_TOP); |
| 4592 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4593 | bool isCameraPrivacyEnabled; |
| 4594 | if (flags::camera_privacy_allowlist()) { |
| 4595 | isCameraPrivacyEnabled = sCameraService->isCameraPrivacyEnabled( |
| 4596 | toString16(mClientPackageName),std::string(),mClientPid,mClientUid); |
| 4597 | } else { |
| 4598 | isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4599 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
| 4602 | 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] | 4603 | " isUidVisible %d, isCameraPrivacyEnabled %d", mCameraIdStr.c_str(), |
| 4604 | mClientPackageName.c_str(), mUidIsTrusted, isUidActive, isUidVisible, |
| 4605 | isCameraPrivacyEnabled); |
| 4606 | // If the calling Uid is trusted (a native service), or the client Uid is active / visible |
| 4607 | // (WAR for b/175320666)the AppOpsManager could return MODE_IGNORED. Do not treat such |
| 4608 | // cases as error. |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4609 | if (!mUidIsTrusted) { |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4610 | if (flags::watch_foreground_changes()) { |
| 4611 | if (isUidVisible && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 4612 | setCameraMute(true); |
| 4613 | } else { |
| 4614 | block(); |
| 4615 | } |
| 4616 | } else { |
| 4617 | if (isUidActive && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 4618 | setCameraMute(true); |
| 4619 | } else if (!isUidActive |
| 4620 | || (isCameraPrivacyEnabled && !supportsCameraMute())) { |
| 4621 | block(); |
| 4622 | } |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4623 | } |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4624 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4625 | } else if (res == AppOpsManager::MODE_ALLOWED) { |
| 4626 | setCameraMute(sCameraService->mOverrideCameraMuteMode); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4627 | } |
| 4628 | } |
| 4629 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4630 | void CameraService::BasicClient::block() { |
| 4631 | ATRACE_CALL(); |
| 4632 | |
| 4633 | // Reset the client PID to allow server-initiated disconnect, |
| 4634 | // and to prevent further calls by client. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4635 | mClientPid = getCallingPid(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4636 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 4637 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); |
| 4638 | disconnect(); |
| 4639 | } |
| 4640 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4641 | // ---------------------------------------------------------------------------- |
| 4642 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4643 | void CameraService::Client::notifyError(int32_t errorCode, |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 4644 | [[maybe_unused]] const CaptureResultExtras& resultExtras) { |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 4645 | if (mRemoteCallback != NULL) { |
Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 4646 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; |
| 4647 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { |
| 4648 | api1ErrorCode = CAMERA_ERROR_DISABLED; |
| 4649 | } |
| 4650 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 4651 | } else { |
| 4652 | ALOGE("mRemoteCallback is NULL!!"); |
| 4653 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4654 | } |
| 4655 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 4656 | // NOTE: function is idempotent |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4657 | binder::Status CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4658 | ALOGV("Client::disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4659 | return BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 4660 | } |
| 4661 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 4662 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 4663 | return level == API_1; |
| 4664 | } |
| 4665 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4666 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 4667 | mClient(client) { |
| 4668 | } |
| 4669 | |
| 4670 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 4671 | const String16& packageName) { |
| 4672 | sp<BasicClient> client = mClient.promote(); |
| 4673 | if (client != NULL) { |
| 4674 | client->opChanged(op, packageName); |
| 4675 | } |
| 4676 | } |
| 4677 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4678 | // ---------------------------------------------------------------------------- |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4679 | // UidPolicy |
| 4680 | // ---------------------------------------------------------------------------- |
| 4681 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4682 | void CameraService::UidPolicy::registerWithActivityManager() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4683 | Mutex::Autolock _l(mUidLock); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4684 | int32_t emptyUidArray[] = { }; |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4685 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4686 | if (mRegistered) return; |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 4687 | status_t res = mAm.linkToDeath(this); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4688 | mAm.registerUidObserverForUids(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4689 | | ActivityManager::UID_OBSERVER_IDLE |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4690 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE |
| 4691 | | ActivityManager::UID_OBSERVER_PROC_OOM_ADJ, |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4692 | ActivityManager::PROCESS_STATE_UNKNOWN, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4693 | toString16(kServiceName), emptyUidArray, 0, mObserverToken); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4694 | if (res == OK) { |
| 4695 | mRegistered = true; |
| 4696 | ALOGV("UidPolicy: Registered with ActivityManager"); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4697 | } else { |
| 4698 | ALOGE("UidPolicy: Failed to register with ActivityManager: 0x%08x", res); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4699 | } |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4700 | } |
| 4701 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4702 | void CameraService::UidPolicy::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4703 | if (name != toString16(kActivityServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4704 | return; |
| 4705 | } |
| 4706 | |
| 4707 | registerWithActivityManager(); |
| 4708 | } |
| 4709 | |
| 4710 | void CameraService::UidPolicy::registerSelf() { |
| 4711 | // Use check service to see if the activity service is available |
| 4712 | // If not available then register for notifications, instead of blocking |
| 4713 | // till the service is ready |
| 4714 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4715 | sp<IBinder> binder = sm->checkService(toString16(kActivityServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4716 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4717 | sm->registerForNotifications(toString16(kActivityServiceName), this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4718 | } else { |
| 4719 | registerWithActivityManager(); |
| 4720 | } |
| 4721 | } |
| 4722 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4723 | void CameraService::UidPolicy::unregisterSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4724 | Mutex::Autolock _l(mUidLock); |
| 4725 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 4726 | mAm.unregisterUidObserver(this); |
| 4727 | mAm.unlinkToDeath(this); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4728 | mRegistered = false; |
| 4729 | mActiveUids.clear(); |
| 4730 | ALOGV("UidPolicy: Unregistered with ActivityManager"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4731 | } |
| 4732 | |
| 4733 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { |
| 4734 | onUidIdle(uid, disabled); |
| 4735 | } |
| 4736 | |
| 4737 | void CameraService::UidPolicy::onUidActive(uid_t uid) { |
| 4738 | Mutex::Autolock _l(mUidLock); |
| 4739 | mActiveUids.insert(uid); |
| 4740 | } |
| 4741 | |
| 4742 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { |
| 4743 | bool deleted = false; |
| 4744 | { |
| 4745 | Mutex::Autolock _l(mUidLock); |
| 4746 | if (mActiveUids.erase(uid) > 0) { |
| 4747 | deleted = true; |
| 4748 | } |
| 4749 | } |
| 4750 | if (deleted) { |
| 4751 | sp<CameraService> service = mService.promote(); |
| 4752 | if (service != nullptr) { |
| 4753 | service->blockClientsForUid(uid); |
| 4754 | } |
| 4755 | } |
| 4756 | } |
| 4757 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4758 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 4759 | int64_t procStateSeq __unused, int32_t capability __unused) { |
Austin Borger | c5585dc | 2022-05-12 00:48:17 +0000 | [diff] [blame] | 4760 | bool procStateChange = false; |
| 4761 | { |
| 4762 | Mutex::Autolock _l(mUidLock); |
| 4763 | if (mMonitoredUids.find(uid) != mMonitoredUids.end() && |
| 4764 | mMonitoredUids[uid].procState != procState) { |
| 4765 | mMonitoredUids[uid].procState = procState; |
| 4766 | procStateChange = true; |
| 4767 | } |
| 4768 | } |
| 4769 | |
| 4770 | if (procStateChange) { |
| 4771 | sp<CameraService> service = mService.promote(); |
| 4772 | if (service != nullptr) { |
| 4773 | service->notifyMonitoredUids(); |
| 4774 | } |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4775 | } |
| 4776 | } |
| 4777 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4778 | /** |
| 4779 | * When the OOM adj of the uid owning the camera changes, a different uid waiting on camera |
| 4780 | * privileges may take precedence if the owner's new OOM adj is greater than the waiting package. |
| 4781 | * Here, we track which monitoredUid has the camera, and track its adj relative to other |
| 4782 | * monitoredUids. If it is revised above some other monitoredUid, signal |
| 4783 | * onCameraAccessPrioritiesChanged. This only needs to capture the case where there are two |
| 4784 | * foreground apps in split screen - state changes will capture all other cases. |
| 4785 | */ |
| 4786 | void CameraService::UidPolicy::onUidProcAdjChanged(uid_t uid, int32_t adj) { |
| 4787 | std::unordered_set<uid_t> notifyUidSet; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4788 | { |
| 4789 | Mutex::Autolock _l(mUidLock); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4790 | auto it = mMonitoredUids.find(uid); |
| 4791 | |
| 4792 | if (it != mMonitoredUids.end()) { |
| 4793 | if (it->second.hasCamera) { |
| 4794 | for (auto &monitoredUid : mMonitoredUids) { |
| 4795 | if (monitoredUid.first != uid && adj > monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4796 | ALOGV("%s: notify uid %d", __FUNCTION__, monitoredUid.first); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4797 | notifyUidSet.emplace(monitoredUid.first); |
| 4798 | } |
| 4799 | } |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4800 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4801 | notifyUidSet.emplace(uid); |
| 4802 | } else { |
| 4803 | for (auto &monitoredUid : mMonitoredUids) { |
| 4804 | if (monitoredUid.second.hasCamera && adj < monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4805 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4806 | notifyUidSet.emplace(uid); |
| 4807 | } |
| 4808 | } |
| 4809 | } |
| 4810 | it->second.procAdj = adj; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4811 | } |
| 4812 | } |
| 4813 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4814 | if (notifyUidSet.size() > 0) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4815 | sp<CameraService> service = mService.promote(); |
| 4816 | if (service != nullptr) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4817 | service->notifyMonitoredUids(notifyUidSet); |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4818 | } |
| 4819 | } |
| 4820 | } |
| 4821 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4822 | /** |
| 4823 | * Register a uid for monitoring, and note whether it owns a camera. |
| 4824 | */ |
| 4825 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid, bool openCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4826 | Mutex::Autolock _l(mUidLock); |
| 4827 | auto it = mMonitoredUids.find(uid); |
| 4828 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4829 | it->second.refCount++; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4830 | } else { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4831 | MonitoredUid monitoredUid; |
| 4832 | monitoredUid.procState = ActivityManager::PROCESS_STATE_NONEXISTENT; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4833 | monitoredUid.procAdj = resource_policy::UNKNOWN_ADJ; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4834 | monitoredUid.refCount = 1; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4835 | it = mMonitoredUids.emplace(std::pair<uid_t, MonitoredUid>(uid, monitoredUid)).first; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4836 | status_t res = mAm.addUidToObserver(mObserverToken, toString16(kServiceName), uid); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4837 | if (res != OK) { |
| 4838 | ALOGE("UidPolicy: Failed to add uid to observer: 0x%08x", res); |
| 4839 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4840 | } |
| 4841 | |
| 4842 | if (openCamera) { |
| 4843 | it->second.hasCamera = true; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4844 | } |
| 4845 | } |
| 4846 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4847 | /** |
| 4848 | * Unregister a uid for monitoring, and note whether it lost ownership of a camera. |
| 4849 | */ |
| 4850 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid, bool closeCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4851 | Mutex::Autolock _l(mUidLock); |
| 4852 | auto it = mMonitoredUids.find(uid); |
| 4853 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4854 | it->second.refCount--; |
| 4855 | if (it->second.refCount == 0) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4856 | mMonitoredUids.erase(it); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4857 | status_t res = mAm.removeUidFromObserver(mObserverToken, toString16(kServiceName), uid); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4858 | if (res != OK) { |
| 4859 | ALOGE("UidPolicy: Failed to remove uid from observer: 0x%08x", res); |
| 4860 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4861 | } else if (closeCamera) { |
| 4862 | it->second.hasCamera = false; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4863 | } |
| 4864 | } else { |
| 4865 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 4866 | } |
| 4867 | } |
| 4868 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4869 | bool CameraService::UidPolicy::isUidActive(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4870 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4871 | return isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4872 | } |
| 4873 | |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4874 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; |
| 4875 | static const int64_t kPollUidActiveTimeoutMillis = 50; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4876 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4877 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4878 | // Non-app UIDs are considered always active |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4879 | // If activity manager is unreachable, assume everything is active |
| 4880 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4881 | return true; |
| 4882 | } |
| 4883 | auto it = mOverrideUids.find(uid); |
| 4884 | if (it != mOverrideUids.end()) { |
| 4885 | return it->second; |
| 4886 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4887 | bool active = mActiveUids.find(uid) != mActiveUids.end(); |
| 4888 | if (!active) { |
| 4889 | // We want active UIDs to always access camera with their first attempt since |
| 4890 | // there is no guarantee the app is robustly written and would retry getting |
| 4891 | // the camera on failure. The inverse case is not a problem as we would take |
| 4892 | // camera away soon once we get the callback that the uid is no longer active. |
| 4893 | ActivityManager am; |
| 4894 | // Okay to access with a lock held as UID changes are dispatched without |
| 4895 | // a lock and we are a higher level component. |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4896 | int64_t startTimeMillis = 0; |
| 4897 | do { |
| 4898 | // TODO: Fix this b/109950150! |
| 4899 | // Okay this is a hack. There is a race between the UID turning active and |
| 4900 | // activity being resumed. The proper fix is very risky, so we temporary add |
| 4901 | // some polling which should happen pretty rarely anyway as the race is hard |
| 4902 | // to hit. |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4903 | active = mActiveUids.find(uid) != mActiveUids.end(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4904 | if (!active) active = am.isUidActive(uid, toString16(callingPackage)); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4905 | if (active) { |
| 4906 | break; |
| 4907 | } |
| 4908 | if (startTimeMillis <= 0) { |
| 4909 | startTimeMillis = uptimeMillis(); |
| 4910 | } |
| 4911 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4912 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4913 | if (remainingTimeMillis <= 0) { |
| 4914 | break; |
| 4915 | } |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4916 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); |
| 4917 | |
| 4918 | mUidLock.unlock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4919 | usleep(remainingTimeMillis * 1000); |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4920 | mUidLock.lock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4921 | } while (true); |
| 4922 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4923 | if (active) { |
| 4924 | // Now that we found out the UID is actually active, cache that |
| 4925 | mActiveUids.insert(uid); |
| 4926 | } |
| 4927 | } |
| 4928 | return active; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4929 | } |
| 4930 | |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4931 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { |
| 4932 | Mutex::Autolock _l(mUidLock); |
| 4933 | return getProcStateLocked(uid); |
| 4934 | } |
| 4935 | |
| 4936 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { |
| 4937 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 4938 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4939 | procState = mMonitoredUids[uid].procState; |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4940 | } |
| 4941 | return procState; |
| 4942 | } |
| 4943 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4944 | void CameraService::UidPolicy::addOverrideUid(uid_t uid, |
| 4945 | const std::string &callingPackage, bool active) { |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4946 | updateOverrideUid(uid, callingPackage, active, true); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4947 | } |
| 4948 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4949 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4950 | updateOverrideUid(uid, callingPackage, false, false); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4951 | } |
| 4952 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4953 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 4954 | Mutex::Autolock _l(mUidLock); |
| 4955 | ALOGV("UidPolicy: ActivityManager has died"); |
| 4956 | mRegistered = false; |
| 4957 | mActiveUids.clear(); |
| 4958 | } |
| 4959 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4960 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, const std::string &callingPackage, |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4961 | bool active, bool insert) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4962 | bool wasActive = false; |
| 4963 | bool isActive = false; |
| 4964 | { |
| 4965 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4966 | wasActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4967 | mOverrideUids.erase(uid); |
| 4968 | if (insert) { |
| 4969 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 4970 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4971 | isActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4972 | } |
| 4973 | if (wasActive != isActive && !isActive) { |
| 4974 | sp<CameraService> service = mService.promote(); |
| 4975 | if (service != nullptr) { |
| 4976 | service->blockClientsForUid(uid); |
| 4977 | } |
| 4978 | } |
| 4979 | } |
| 4980 | |
| 4981 | // ---------------------------------------------------------------------------- |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4982 | // SensorPrivacyPolicy |
| 4983 | // ---------------------------------------------------------------------------- |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4984 | |
| 4985 | void CameraService::SensorPrivacyPolicy::registerWithSensorPrivacyManager() |
| 4986 | { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4987 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4988 | if (mRegistered) { |
| 4989 | return; |
| 4990 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4991 | hasCameraPrivacyFeature(); // Called so the result is cached |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4992 | mSpm.addSensorPrivacyListener(this); |
Charles Chen | f075f08 | 2024-03-04 23:32:55 +0000 | [diff] [blame] | 4993 | if (isAutomotiveDevice()) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4994 | mSpm.addToggleSensorPrivacyListener(this); |
| 4995 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4996 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4997 | if (flags::camera_privacy_allowlist()) { |
| 4998 | mCameraPrivacyState = mSpm.getToggleSensorPrivacyState( |
| 4999 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, |
| 5000 | SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 5001 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5002 | status_t res = mSpm.linkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5003 | if (res == OK) { |
| 5004 | mRegistered = true; |
| 5005 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); |
| 5006 | } |
| 5007 | } |
| 5008 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5009 | void CameraService::SensorPrivacyPolicy::onServiceRegistration(const String16& name, |
| 5010 | const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5011 | if (name != toString16(kSensorPrivacyServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5012 | return; |
| 5013 | } |
| 5014 | |
| 5015 | registerWithSensorPrivacyManager(); |
| 5016 | } |
| 5017 | |
| 5018 | void CameraService::SensorPrivacyPolicy::registerSelf() { |
| 5019 | // Use checkservice to see if the sensor_privacy service is available |
| 5020 | // If service is not available then register for notification |
| 5021 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5022 | sp<IBinder> binder = sm->checkService(toString16(kSensorPrivacyServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5023 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5024 | sm->registerForNotifications(toString16(kSensorPrivacyServiceName),this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5025 | } else { |
| 5026 | registerWithSensorPrivacyManager(); |
| 5027 | } |
| 5028 | } |
| 5029 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5030 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { |
| 5031 | Mutex::Autolock _l(mSensorPrivacyLock); |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5032 | mSpm.removeSensorPrivacyListener(this); |
Charles Chen | f075f08 | 2024-03-04 23:32:55 +0000 | [diff] [blame] | 5033 | if (isAutomotiveDevice()) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5034 | mSpm.removeToggleSensorPrivacyListener(this); |
| 5035 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5036 | mSpm.unlinkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5037 | mRegistered = false; |
| 5038 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); |
| 5039 | } |
| 5040 | |
| 5041 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5042 | if (!mRegistered) { |
| 5043 | registerWithSensorPrivacyManager(); |
| 5044 | } |
| 5045 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5046 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5047 | return mSensorPrivacyEnabled; |
| 5048 | } |
| 5049 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5050 | int CameraService::SensorPrivacyPolicy::getCameraPrivacyState() { |
| 5051 | if (!mRegistered) { |
| 5052 | registerWithSensorPrivacyManager(); |
| 5053 | } |
| 5054 | |
| 5055 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5056 | return mCameraPrivacyState; |
| 5057 | } |
| 5058 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5059 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled() { |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5060 | if (!hasCameraPrivacyFeature()) { |
| 5061 | return false; |
| 5062 | } |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5063 | return mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5064 | } |
| 5065 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5066 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled(const String16& packageName) { |
| 5067 | if (!hasCameraPrivacyFeature()) { |
| 5068 | return SensorPrivacyManager::DISABLED; |
| 5069 | } |
| 5070 | return mSpm.isCameraPrivacyEnabled(packageName); |
| 5071 | } |
| 5072 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5073 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged( |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5074 | int toggleType, int sensor, bool enabled) { |
| 5075 | if ((toggleType == SensorPrivacyManager::TOGGLE_TYPE_UNKNOWN) |
| 5076 | && (sensor == SensorPrivacyManager::TOGGLE_SENSOR_UNKNOWN)) { |
| 5077 | { |
| 5078 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5079 | mSensorPrivacyEnabled = enabled; |
| 5080 | } |
| 5081 | // if sensor privacy is enabled then block all clients from accessing the camera |
| 5082 | if (enabled) { |
| 5083 | sp<CameraService> service = mService.promote(); |
| 5084 | if (service != nullptr) { |
| 5085 | service->blockAllClients(); |
| 5086 | } |
| 5087 | } |
| 5088 | } |
| 5089 | return binder::Status::ok(); |
| 5090 | } |
| 5091 | |
| 5092 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyStateChanged( |
| 5093 | int, int sensor, int state) { |
| 5094 | if (!flags::camera_privacy_allowlist() |
| 5095 | || (sensor != SensorPrivacyManager::TOGGLE_SENSOR_CAMERA)) { |
| 5096 | return binder::Status::ok(); |
| 5097 | } |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5098 | { |
| 5099 | Mutex::Autolock _l(mSensorPrivacyLock); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5100 | mCameraPrivacyState = state; |
| 5101 | } |
| 5102 | sp<CameraService> service = mService.promote(); |
| 5103 | if (!service) { |
| 5104 | return binder::Status::ok(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5105 | } |
| 5106 | // 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] | 5107 | if (state == SensorPrivacyManager::ENABLED) { |
| 5108 | service->blockAllClients(); |
Jyoti Bhayana | 54a4b00 | 2024-02-27 15:36:09 -0800 | [diff] [blame] | 5109 | } else if (state == SensorPrivacyManager::ENABLED_EXCEPT_ALLOWLISTED_APPS) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5110 | service->blockPrivacyEnabledClients(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5111 | } |
| 5112 | return binder::Status::ok(); |
| 5113 | } |
| 5114 | |
| 5115 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 5116 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5117 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); |
| 5118 | mRegistered = false; |
| 5119 | } |
| 5120 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5121 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5122 | bool supportsSoftwareToggle = mSpm.supportsSensorToggle( |
| 5123 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 5124 | bool supportsHardwareToggle = mSpm.supportsSensorToggle( |
| 5125 | SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 5126 | return supportsSoftwareToggle || supportsHardwareToggle; |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5127 | } |
| 5128 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5129 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5130 | // CameraState |
| 5131 | // ---------------------------------------------------------------------------- |
| 5132 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5133 | CameraService::CameraState::CameraState(const std::string& id, int cost, |
| 5134 | const std::set<std::string>& conflicting, SystemCameraKind systemCameraKind, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5135 | const std::vector<std::string>& physicalCameras) : mId(id), |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5136 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5137 | mSystemCameraKind(systemCameraKind), mPhysicalCameras(physicalCameras) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5138 | |
| 5139 | CameraService::CameraState::~CameraState() {} |
| 5140 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5141 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5142 | Mutex::Autolock lock(mStatusLock); |
| 5143 | return mStatus; |
| 5144 | } |
| 5145 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5146 | std::vector<std::string> CameraService::CameraState::getUnavailablePhysicalIds() const { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5147 | Mutex::Autolock lock(mStatusLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5148 | std::vector<std::string> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5149 | return res; |
| 5150 | } |
| 5151 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5152 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 5153 | return mShimParams; |
| 5154 | } |
| 5155 | |
| 5156 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 5157 | mShimParams = params; |
| 5158 | } |
| 5159 | |
| 5160 | int CameraService::CameraState::getCost() const { |
| 5161 | return mCost; |
| 5162 | } |
| 5163 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5164 | std::set<std::string> CameraService::CameraState::getConflicting() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5165 | return mConflicting; |
| 5166 | } |
| 5167 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5168 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { |
| 5169 | return mSystemCameraKind; |
| 5170 | } |
| 5171 | |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5172 | bool CameraService::CameraState::containsPhysicalCamera(const std::string& physicalCameraId) const { |
| 5173 | return std::find(mPhysicalCameras.begin(), mPhysicalCameras.end(), physicalCameraId) |
| 5174 | != mPhysicalCameras.end(); |
| 5175 | } |
| 5176 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5177 | bool CameraService::CameraState::addUnavailablePhysicalId(const std::string& physicalId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5178 | Mutex::Autolock lock(mStatusLock); |
| 5179 | auto result = mUnavailablePhysicalIds.insert(physicalId); |
| 5180 | return result.second; |
| 5181 | } |
| 5182 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5183 | bool CameraService::CameraState::removeUnavailablePhysicalId(const std::string& physicalId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5184 | Mutex::Autolock lock(mStatusLock); |
| 5185 | auto count = mUnavailablePhysicalIds.erase(physicalId); |
| 5186 | return count > 0; |
| 5187 | } |
| 5188 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5189 | void CameraService::CameraState::setClientPackage(const std::string& clientPackage) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5190 | Mutex::Autolock lock(mStatusLock); |
| 5191 | mClientPackage = clientPackage; |
| 5192 | } |
| 5193 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5194 | std::string CameraService::CameraState::getClientPackage() const { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5195 | Mutex::Autolock lock(mStatusLock); |
| 5196 | return mClientPackage; |
| 5197 | } |
| 5198 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5199 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5200 | // ClientEventListener |
| 5201 | // ---------------------------------------------------------------------------- |
| 5202 | |
| 5203 | void CameraService::ClientEventListener::onClientAdded( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5204 | const resource_policy::ClientDescriptor<std::string, |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5205 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 5206 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5207 | if (basicClient.get() != nullptr) { |
| 5208 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5209 | notifier.noteStartCamera(toString8(descriptor.getKey()), |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5210 | static_cast<int>(basicClient->getClientUid())); |
| 5211 | } |
| 5212 | } |
| 5213 | |
| 5214 | void CameraService::ClientEventListener::onClientRemoved( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5215 | const resource_policy::ClientDescriptor<std::string, |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5216 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 5217 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5218 | if (basicClient.get() != nullptr) { |
| 5219 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5220 | notifier.noteStopCamera(toString8(descriptor.getKey()), |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5221 | static_cast<int>(basicClient->getClientUid())); |
| 5222 | } |
| 5223 | } |
| 5224 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5225 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5226 | // CameraClientManager |
| 5227 | // ---------------------------------------------------------------------------- |
| 5228 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5229 | CameraService::CameraClientManager::CameraClientManager() { |
| 5230 | setListener(std::make_shared<ClientEventListener>()); |
| 5231 | } |
| 5232 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5233 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 5234 | |
| 5235 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5236 | const std::string& id) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5237 | auto descriptor = get(id); |
| 5238 | if (descriptor == nullptr) { |
| 5239 | return sp<BasicClient>{nullptr}; |
| 5240 | } |
| 5241 | return descriptor->getValue(); |
| 5242 | } |
| 5243 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5244 | std::string CameraService::CameraClientManager::toString() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5245 | auto all = getAll(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5246 | std::ostringstream ret; |
| 5247 | ret << "["; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5248 | bool hasAny = false; |
| 5249 | for (auto& i : all) { |
| 5250 | hasAny = true; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5251 | std::string key = i->getKey(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5252 | int32_t cost = i->getCost(); |
| 5253 | int32_t pid = i->getOwnerId(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 5254 | int32_t score = i->getPriority().getScore(); |
| 5255 | int32_t state = i->getPriority().getState(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5256 | auto conflicting = i->getConflicting(); |
| 5257 | auto clientSp = i->getValue(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5258 | std::string packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 5259 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5260 | if (clientSp.get() != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5261 | packageName = clientSp->getPackageName(); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5262 | uid_t clientUid = clientSp->getClientUid(); |
| 5263 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5264 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5265 | ret << fmt::sprintf("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" |
| 5266 | PRId32 ", State: %" PRId32, key.c_str(), cost, pid, score, state); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5267 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5268 | if (clientSp.get() != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5269 | ret << fmt::sprintf("User Id: %d, ", clientUserId); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5270 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5271 | if (packageName.size() != 0) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5272 | ret << fmt::sprintf("Client Package Name: %s", packageName.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5273 | } |
| 5274 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5275 | ret << ", Conflicting Client Devices: {"; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5276 | for (auto& j : conflicting) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5277 | ret << fmt::sprintf("%s, ", j.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5278 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5279 | ret << "})"; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5280 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5281 | if (hasAny) ret << "\n"; |
| 5282 | ret << "]\n"; |
| 5283 | return std::move(ret.str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5287 | const std::string& key, const sp<BasicClient>& value, int32_t cost, |
| 5288 | const std::set<std::string>& conflictingKeys, int32_t score, int32_t ownerId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5289 | int32_t state, int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5290 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5291 | int32_t score_adj = systemNativeClient ? kSystemNativeClientScore : score; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5292 | int32_t state_adj = systemNativeClient ? kSystemNativeClientState : state; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 5293 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5294 | return std::make_shared<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>>( |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5295 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, |
| 5296 | systemNativeClient, oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5297 | } |
| 5298 | |
| 5299 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 5300 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5301 | int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5302 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 5303 | partial->getConflicting(), partial->getPriority().getScore(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5304 | partial->getOwnerId(), partial->getPriority().getState(), oomScoreOffset, |
| 5305 | systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | // ---------------------------------------------------------------------------- |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5309 | // InjectionStatusListener |
| 5310 | // ---------------------------------------------------------------------------- |
| 5311 | |
| 5312 | void CameraService::InjectionStatusListener::addListener( |
| 5313 | const sp<ICameraInjectionCallback>& callback) { |
| 5314 | Mutex::Autolock lock(mListenerLock); |
| 5315 | if (mCameraInjectionCallback) return; |
| 5316 | status_t res = IInterface::asBinder(callback)->linkToDeath(this); |
| 5317 | if (res == OK) { |
| 5318 | mCameraInjectionCallback = callback; |
| 5319 | } |
| 5320 | } |
| 5321 | |
| 5322 | void CameraService::InjectionStatusListener::removeListener() { |
| 5323 | Mutex::Autolock lock(mListenerLock); |
| 5324 | if (mCameraInjectionCallback == nullptr) { |
| 5325 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 5326 | return; |
| 5327 | } |
| 5328 | IInterface::asBinder(mCameraInjectionCallback)->unlinkToDeath(this); |
| 5329 | mCameraInjectionCallback = nullptr; |
| 5330 | } |
| 5331 | |
| 5332 | void CameraService::InjectionStatusListener::notifyInjectionError( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5333 | const std::string &injectedCamId, status_t err) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5334 | if (mCameraInjectionCallback == nullptr) { |
| 5335 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 5336 | return; |
| 5337 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5338 | |
| 5339 | switch (err) { |
| 5340 | case -ENODEV: |
| 5341 | mCameraInjectionCallback->onInjectionError( |
| 5342 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5343 | ALOGE("No camera device with ID \"%s\" currently available!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5344 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5345 | break; |
| 5346 | case -EBUSY: |
| 5347 | mCameraInjectionCallback->onInjectionError( |
| 5348 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5349 | ALOGE("Higher-priority client using camera, ID \"%s\" currently unavailable!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5350 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5351 | break; |
| 5352 | case DEAD_OBJECT: |
| 5353 | mCameraInjectionCallback->onInjectionError( |
| 5354 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5355 | ALOGE("Camera ID \"%s\" object is dead!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5356 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5357 | break; |
| 5358 | case INVALID_OPERATION: |
| 5359 | mCameraInjectionCallback->onInjectionError( |
| 5360 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5361 | ALOGE("Camera ID \"%s\" encountered an operating or internal error!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5362 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5363 | break; |
| 5364 | case UNKNOWN_TRANSACTION: |
| 5365 | mCameraInjectionCallback->onInjectionError( |
| 5366 | ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED); |
| 5367 | ALOGE("Camera ID \"%s\" method doesn't support!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5368 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5369 | break; |
| 5370 | default: |
| 5371 | mCameraInjectionCallback->onInjectionError( |
| 5372 | ICameraInjectionCallback::ERROR_INJECTION_INVALID_ERROR); |
| 5373 | ALOGE("Unexpected error %s (%d) opening camera \"%s\"!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5374 | strerror(-err), err, injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5375 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5376 | } |
| 5377 | |
| 5378 | void CameraService::InjectionStatusListener::binderDied( |
| 5379 | const wp<IBinder>& /*who*/) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5380 | ALOGV("InjectionStatusListener: ICameraInjectionCallback has died"); |
| 5381 | auto parent = mParent.promote(); |
| 5382 | if (parent != nullptr) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5383 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 5384 | if (clientDescriptor != nullptr) { |
| 5385 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 5386 | baseClientPtr->stopInjection(); |
| 5387 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 5388 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5389 | } |
| 5390 | } |
| 5391 | |
| 5392 | // ---------------------------------------------------------------------------- |
| 5393 | // CameraInjectionSession |
| 5394 | // ---------------------------------------------------------------------------- |
| 5395 | |
| 5396 | binder::Status CameraService::CameraInjectionSession::stopInjection() { |
| 5397 | Mutex::Autolock lock(mInjectionSessionLock); |
| 5398 | auto parent = mParent.promote(); |
| 5399 | if (parent == nullptr) { |
| 5400 | ALOGE("CameraInjectionSession: Parent is gone"); |
| 5401 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SERVICE, |
| 5402 | "Camera service encountered error"); |
| 5403 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5404 | |
| 5405 | status_t res = NO_ERROR; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5406 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 5407 | if (clientDescriptor != nullptr) { |
| 5408 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 5409 | res = baseClientPtr->stopInjection(); |
| 5410 | if (res != OK) { |
| 5411 | ALOGE("CameraInjectionSession: Failed to stop the injection camera!" |
| 5412 | " ret != NO_ERROR: %d", res); |
| 5413 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SESSION, |
| 5414 | "Camera session encountered error"); |
| 5415 | } |
| 5416 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 5417 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5418 | return binder::Status::ok(); |
| 5419 | } |
| 5420 | |
| 5421 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5422 | |
| 5423 | static const int kDumpLockRetries = 50; |
| 5424 | static const int kDumpLockSleep = 60000; |
| 5425 | |
| 5426 | static bool tryLock(Mutex& mutex) |
| 5427 | { |
| 5428 | bool locked = false; |
| 5429 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 5430 | if (mutex.tryLock() == NO_ERROR) { |
| 5431 | locked = true; |
| 5432 | break; |
| 5433 | } |
| 5434 | usleep(kDumpLockSleep); |
| 5435 | } |
| 5436 | return locked; |
| 5437 | } |
| 5438 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5439 | void CameraService::cacheDump() { |
| 5440 | if (mMemFd != -1) { |
| 5441 | const Vector<String16> args; |
| 5442 | ATRACE_CALL(); |
| 5443 | // Acquiring service lock here will avoid the deadlock since |
| 5444 | // cacheDump will not be called during the second disconnect. |
| 5445 | Mutex::Autolock lock(mServiceLock); |
| 5446 | |
| 5447 | Mutex::Autolock l(mCameraStatesLock); |
| 5448 | // Start collecting the info for open sessions and store it in temp file. |
| 5449 | for (const auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5450 | std::string cameraId = state.first; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5451 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 5452 | if (clientDescriptor != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5453 | dprintf(mMemFd, "== Camera device %s dynamic info: ==\n", cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5454 | // Log the current open session info before device is disconnected. |
| 5455 | dumpOpenSessionClientLogs(mMemFd, args, cameraId); |
| 5456 | } |
| 5457 | } |
| 5458 | } |
| 5459 | } |
| 5460 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5461 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 5462 | ATRACE_CALL(); |
| 5463 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5464 | if (checkCallingPermission(toString16(sDumpPermission)) == false) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5465 | 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] | 5466 | getCallingPid(), |
| 5467 | getCallingUid()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5468 | return NO_ERROR; |
| 5469 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5470 | bool locked = tryLock(mServiceLock); |
| 5471 | // failed to lock - CameraService is probably deadlocked |
| 5472 | if (!locked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5473 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5474 | } |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5475 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5476 | if (!mInitialized) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5477 | dprintf(fd, "!! No camera HAL available !!\n"); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 5478 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5479 | // Dump event log for error information |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5480 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5481 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5482 | if (locked) mServiceLock.unlock(); |
| 5483 | return NO_ERROR; |
| 5484 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5485 | dprintf(fd, "\n== Service global info: ==\n\n"); |
| 5486 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 5487 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 5488 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", |
| 5489 | mNormalDeviceIdsWithoutSystemCamera.size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 5490 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { |
| 5491 | dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); |
| 5492 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5493 | std::string activeClientString = mActiveClientManager.toString(); |
| 5494 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.c_str()); |
| 5495 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).c_str()); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5496 | if (mStreamUseCaseOverrides.size() > 0) { |
| 5497 | dprintf(fd, "Active stream use case overrides:"); |
| 5498 | for (int64_t useCaseOverride : mStreamUseCaseOverrides) { |
| 5499 | dprintf(fd, " %" PRId64, useCaseOverride); |
| 5500 | } |
| 5501 | dprintf(fd, "\n"); |
| 5502 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5503 | |
| 5504 | dumpEventLog(fd); |
| 5505 | |
| 5506 | bool stateLocked = tryLock(mCameraStatesLock); |
| 5507 | if (!stateLocked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5508 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5509 | } |
| 5510 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5511 | int argSize = args.size(); |
| 5512 | for (int i = 0; i < argSize; i++) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5513 | if (args[i] == toString16(TagMonitor::kMonitorOption)) { |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5514 | if (i + 1 < argSize) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5515 | mMonitorTags = toStdString(args[i + 1]); |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5516 | } |
| 5517 | break; |
| 5518 | } |
| 5519 | } |
| 5520 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5521 | for (auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5522 | const std::string &cameraId = state.first; |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5523 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5524 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.c_str()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5525 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5526 | CameraParameters p = state.second->getShimParams(); |
| 5527 | if (!p.isEmpty()) { |
| 5528 | dprintf(fd, " Camera1 API shim is using parameters:\n "); |
| 5529 | p.dump(fd, args); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5530 | } |
| 5531 | |
| 5532 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 5533 | if (clientDescriptor != nullptr) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5534 | // log the current open session info |
| 5535 | dumpOpenSessionClientLogs(fd, args, cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 5536 | } else { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5537 | dumpClosedSessionClientLogs(fd, cameraId); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5538 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5539 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | if (stateLocked) mCameraStatesLock.unlock(); |
| 5543 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5544 | if (locked) mServiceLock.unlock(); |
| 5545 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 5546 | mCameraProviderManager->dump(fd, args); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5547 | |
| 5548 | dprintf(fd, "\n== Vendor tags: ==\n\n"); |
| 5549 | |
| 5550 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 5551 | if (desc == NULL) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 5552 | sp<VendorTagDescriptorCache> cache = |
| 5553 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 5554 | if (cache == NULL) { |
| 5555 | dprintf(fd, "No vendor tags.\n"); |
| 5556 | } else { |
| 5557 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 5558 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5559 | } else { |
| 5560 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 5561 | } |
| 5562 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5563 | // Dump camera traces if there were any |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5564 | dprintf(fd, "\n"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5565 | camera3::CameraTraces::dump(fd); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5566 | |
| 5567 | // Process dump arguments, if any |
| 5568 | int n = args.size(); |
| 5569 | String16 verboseOption("-v"); |
| 5570 | String16 unreachableOption("--unreachable"); |
| 5571 | for (int i = 0; i < n; i++) { |
| 5572 | if (args[i] == verboseOption) { |
| 5573 | // change logging level |
| 5574 | if (i + 1 >= n) continue; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5575 | std::string levelStr = toStdString(args[i+1]); |
| 5576 | int level = atoi(levelStr.c_str()); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5577 | dprintf(fd, "\nSetting log level to %d.\n", level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5578 | setLogLevel(level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5579 | } else if (args[i] == unreachableOption) { |
| 5580 | // Dump memory analysis |
| 5581 | // TODO - should limit be an argument parameter? |
| 5582 | UnreachableMemoryInfo info; |
| 5583 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); |
| 5584 | if (!success) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5585 | dprintf(fd, "\n== Unable to dump unreachable memory. " |
| 5586 | "Try disabling SELinux enforcement. ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5587 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5588 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5589 | std::string s = info.ToString(/*log_contents*/ true); |
| 5590 | write(fd, s.c_str(), s.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5591 | } |
| 5592 | } |
| 5593 | } |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5594 | |
| 5595 | bool serviceLocked = tryLock(mServiceLock); |
| 5596 | |
| 5597 | // Dump info from previous open sessions. |
| 5598 | // Reposition the offset to beginning of the file before reading |
| 5599 | |
| 5600 | if ((mMemFd >= 0) && (lseek(mMemFd, 0, SEEK_SET) != -1)) { |
| 5601 | dprintf(fd, "\n**********Dumpsys from previous open session**********\n"); |
| 5602 | ssize_t size_read; |
| 5603 | char buf[4096]; |
| 5604 | while ((size_read = read(mMemFd, buf, (sizeof(buf) - 1))) > 0) { |
| 5605 | // Read data from file to a small buffer and write it to fd. |
| 5606 | write(fd, buf, size_read); |
| 5607 | if (size_read == -1) { |
| 5608 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 5609 | break; |
| 5610 | } |
| 5611 | } |
| 5612 | dprintf(fd, "\n**********End of Dumpsys from previous open session**********\n"); |
| 5613 | } else { |
| 5614 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 5615 | } |
| 5616 | |
| 5617 | if (serviceLocked) mServiceLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5618 | return NO_ERROR; |
| 5619 | } |
| 5620 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5621 | void CameraService::dumpOpenSessionClientLogs(int fd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5622 | const Vector<String16>& args, const std::string& cameraId) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5623 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Rucha Katakwar | 71a0e05 | 2022-04-07 15:44:18 -0700 | [diff] [blame] | 5624 | dprintf(fd, " %s : Device %s is open. Client instance dump:\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5625 | getFormattedCurrentTime().c_str(), |
| 5626 | cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5627 | dprintf(fd, " Client priority score: %d state: %d\n", |
| 5628 | clientDescriptor->getPriority().getScore(), |
| 5629 | clientDescriptor->getPriority().getState()); |
| 5630 | dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 5631 | |
| 5632 | auto client = clientDescriptor->getValue(); |
| 5633 | dprintf(fd, " Client package: %s\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5634 | client->getPackageName().c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5635 | |
| 5636 | client->dumpClient(fd, args); |
| 5637 | } |
| 5638 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5639 | void CameraService::dumpClosedSessionClientLogs(int fd, const std::string& cameraId) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5640 | dprintf(fd, " Device %s is closed, no client instance\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5641 | cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5642 | } |
| 5643 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5644 | void CameraService::dumpEventLog(int fd) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5645 | 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] | 5646 | |
| 5647 | Mutex::Autolock l(mLogLock); |
| 5648 | for (const auto& msg : mEventLog) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5649 | dprintf(fd, " %s\n", msg.c_str()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5653 | dprintf(fd, " ...\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5654 | } else if (mEventLog.size() == 0) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5655 | dprintf(fd, " [no events yet]\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5656 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5657 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5658 | } |
| 5659 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5660 | void CameraService::cacheClientTagDumpIfNeeded(const std::string &cameraId, BasicClient* client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5661 | Mutex::Autolock lock(mLogLock); |
| 5662 | if (!isClientWatchedLocked(client)) { return; } |
| 5663 | |
| 5664 | std::vector<std::string> dumpVector; |
| 5665 | client->dumpWatchedEventsToVector(dumpVector); |
| 5666 | |
| 5667 | if (dumpVector.empty()) { return; } |
| 5668 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5669 | std::ostringstream dumpString; |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 5670 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5671 | std::string currentTime = getFormattedCurrentTime(); |
| 5672 | dumpString << "Cached @ "; |
| 5673 | dumpString << currentTime; |
| 5674 | dumpString << "\n"; // First line is the timestamp of when client is cached. |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 5675 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5676 | size_t i = dumpVector.size(); |
| 5677 | |
| 5678 | // Store the string in reverse order (latest last) |
| 5679 | while (i > 0) { |
| 5680 | i--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5681 | dumpString << cameraId; |
| 5682 | dumpString << ":"; |
| 5683 | dumpString << client->getPackageName(); |
| 5684 | dumpString << " "; |
| 5685 | dumpString << dumpVector[i]; // implicitly ends with '\n' |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5686 | } |
| 5687 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5688 | mWatchedClientsDumpCache[client->getPackageName()] = dumpString.str(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5689 | } |
| 5690 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5691 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5692 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5693 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 5694 | if (mTorchClientMap[i] == who) { |
| 5695 | // turn off the torch mode that was turned on by dead client |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5696 | std::string cameraId = mTorchClientMap.keyAt(i); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5697 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 5698 | if (res) { |
| 5699 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 5700 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 5701 | return; |
| 5702 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5703 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5704 | break; |
| 5705 | } |
| 5706 | } |
| 5707 | } |
| 5708 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5709 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 5710 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 5711 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 5712 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 5713 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 5714 | */ |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 5715 | // PID here is approximate and can be wrong. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 5716 | logClientDied(getCallingPid(), "Binder died unexpectedly"); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 5717 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5718 | // check torch client |
| 5719 | handleTorchClientBinderDied(who); |
| 5720 | |
| 5721 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5722 | if(!evictClientIdByRemote(who)) { |
| 5723 | 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] | 5724 | return; |
| 5725 | } |
| 5726 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5727 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 5728 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 5729 | } |
| 5730 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5731 | void CameraService::updateStatus(StatusInternal status, const std::string& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5732 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 5733 | } |
| 5734 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5735 | void CameraService::updateStatus(StatusInternal status, const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5736 | std::initializer_list<StatusInternal> rejectSourceStates) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5737 | // Do not lock mServiceLock here or can get into a deadlock from |
| 5738 | // connect() -> disconnect -> updateStatus |
| 5739 | |
| 5740 | auto state = getCameraState(cameraId); |
| 5741 | |
| 5742 | if (state == nullptr) { |
| 5743 | 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] | 5744 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5745 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 5746 | } |
| 5747 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5748 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 5749 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 5750 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5751 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5752 | return; |
| 5753 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5754 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5755 | if (vd_flags::camera_device_awareness() && status == StatusInternal::PRESENT) { |
| 5756 | CameraMetadata cameraInfo; |
| 5757 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
| 5758 | cameraId, false, &cameraInfo, false); |
| 5759 | if (res != OK) { |
| 5760 | ALOGW("%s: Not able to get camera characteristics for camera id %s", |
| 5761 | __FUNCTION__, cameraId.c_str()); |
| 5762 | } else { |
| 5763 | int32_t deviceId = getDeviceId(cameraInfo); |
| 5764 | if (deviceId != kDefaultDeviceId) { |
| 5765 | const auto &lensFacingEntry = cameraInfo.find(ANDROID_LENS_FACING); |
| 5766 | camera_metadata_enum_android_lens_facing_t androidLensFacing = |
| 5767 | static_cast<camera_metadata_enum_android_lens_facing_t>( |
| 5768 | lensFacingEntry.data.u8[0]); |
| 5769 | std::string mappedCameraId; |
| 5770 | if (androidLensFacing == ANDROID_LENS_FACING_BACK) { |
| 5771 | mappedCameraId = kVirtualDeviceBackCameraId; |
| 5772 | } else if (androidLensFacing == ANDROID_LENS_FACING_FRONT) { |
| 5773 | mappedCameraId = kVirtualDeviceFrontCameraId; |
| 5774 | } else { |
| 5775 | ALOGD("%s: Not adding entry for an external camera of a virtual device", |
| 5776 | __func__); |
| 5777 | } |
| 5778 | if (!mappedCameraId.empty()) { |
| 5779 | mVirtualDeviceCameraIdMapper.addCamera(cameraId, deviceId, mappedCameraId); |
| 5780 | } |
| 5781 | } |
| 5782 | } |
| 5783 | } |
| 5784 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5785 | // Collect the logical cameras without holding mStatusLock in updateStatus |
| 5786 | // as that can lead to a deadlock(b/162192331). |
| 5787 | auto logicalCameraIds = getLogicalCameras(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5788 | // 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] | 5789 | // of the listeners with both the mStatusLock and mStatusListenerLock held |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 5790 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5791 | &logicalCameraIds] |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5792 | (const std::string& cameraId, StatusInternal status) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5793 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 5794 | auto [deviceId, mappedCameraId] = |
| 5795 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5796 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5797 | if (status != StatusInternal::ENUMERATING) { |
| 5798 | // Update torch status if it has a flash unit. |
| 5799 | Mutex::Autolock al(mTorchStatusMutex); |
| 5800 | TorchModeStatus torchStatus; |
| 5801 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 5802 | NAME_NOT_FOUND) { |
| 5803 | TorchModeStatus newTorchStatus = |
| 5804 | status == StatusInternal::PRESENT ? |
| 5805 | TorchModeStatus::AVAILABLE_OFF : |
| 5806 | TorchModeStatus::NOT_AVAILABLE; |
| 5807 | if (torchStatus != newTorchStatus) { |
| 5808 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); |
| 5809 | } |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 5810 | } |
| 5811 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5812 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5813 | Mutex::Autolock lock(mStatusListenerLock); |
| 5814 | notifyPhysicalCameraStatusLocked(mapToInterface(status), mappedCameraId, |
| 5815 | logicalCameraIds, deviceKind, deviceId); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5816 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5817 | for (auto& listener : mListenerList) { |
| 5818 | bool isVendorListener = listener->isVendorListener(); |
| 5819 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, |
| 5820 | listener->getListenerPid(), listener->getListenerUid())) { |
| 5821 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 5822 | cameraId.c_str()); |
| 5823 | continue; |
| 5824 | } |
| 5825 | |
| 5826 | auto ret = listener->getListener()->onStatusChanged(mapToInterface(status), |
| 5827 | mappedCameraId, deviceId); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5828 | listener->handleBinderStatus(ret, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5829 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5830 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 5831 | ret.exceptionCode()); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5832 | |
| 5833 | // Only cameras of the default device can be remapped to a different camera |
| 5834 | // (using remapCameraIds method), so do the following only if the camera is |
| 5835 | // associated with the default device. |
| 5836 | if (deviceId == kDefaultDeviceId) { |
| 5837 | // For the default device, also trigger the callbacks for cameras that were |
| 5838 | // remapped to the current cameraId for the specific package that this |
| 5839 | // listener belongs to. |
| 5840 | std::vector<std::string> remappedCameraIds = |
| 5841 | findOriginalIdsForRemappedCameraId(cameraId, |
| 5842 | listener->getListenerUid()); |
| 5843 | for (auto &remappedCameraId: remappedCameraIds) { |
| 5844 | ret = listener->getListener()->onStatusChanged( |
| 5845 | mapToInterface(status), remappedCameraId, kDefaultDeviceId); |
| 5846 | listener->handleBinderStatus(ret, |
| 5847 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", |
| 5848 | __FUNCTION__, listener->getListenerUid(), |
| 5849 | listener->getListenerPid(), ret.exceptionCode()); |
| 5850 | } |
| 5851 | } |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5852 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5853 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 5854 | } |
| 5855 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5856 | void CameraService::updateOpenCloseStatus(const std::string& cameraId, bool open, |
| 5857 | const std::string& clientPackageName) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5858 | auto state = getCameraState(cameraId); |
| 5859 | if (state == nullptr) { |
| 5860 | 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] | 5861 | cameraId.c_str()); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5862 | return; |
| 5863 | } |
| 5864 | if (open) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5865 | state->setClientPackage(clientPackageName); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5866 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5867 | state->setClientPackage(std::string()); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5868 | } |
| 5869 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5870 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 5871 | auto [deviceId, mappedCameraId] = |
| 5872 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 5873 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5874 | Mutex::Autolock lock(mStatusListenerLock); |
| 5875 | |
| 5876 | for (const auto& it : mListenerList) { |
| 5877 | if (!it->isOpenCloseCallbackAllowed()) { |
| 5878 | continue; |
| 5879 | } |
| 5880 | |
| 5881 | binder::Status ret; |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5882 | if (open) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5883 | ret = it->getListener()->onCameraOpened(mappedCameraId, clientPackageName, |
| 5884 | deviceId); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5885 | } else { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5886 | ret = it->getListener()->onCameraClosed(mappedCameraId, deviceId); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5887 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5888 | |
| 5889 | it->handleBinderStatus(ret, |
| 5890 | "%s: Failed to trigger onCameraOpened/onCameraClosed callback for %d:%d: %d", |
| 5891 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5892 | } |
| 5893 | } |
| 5894 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5895 | template<class Func> |
| 5896 | void CameraService::CameraState::updateStatus(StatusInternal status, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5897 | const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5898 | std::initializer_list<StatusInternal> rejectSourceStates, |
| 5899 | Func onStatusUpdatedLocked) { |
| 5900 | Mutex::Autolock lock(mStatusLock); |
| 5901 | StatusInternal oldStatus = mStatus; |
| 5902 | mStatus = status; |
| 5903 | |
| 5904 | if (oldStatus == status) { |
| 5905 | return; |
| 5906 | } |
| 5907 | |
| 5908 | 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] | 5909 | cameraId.c_str(), oldStatus, status); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5910 | |
| 5911 | if (oldStatus == StatusInternal::NOT_PRESENT && |
| 5912 | (status != StatusInternal::PRESENT && |
| 5913 | status != StatusInternal::ENUMERATING)) { |
| 5914 | |
| 5915 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 5916 | __FUNCTION__); |
| 5917 | mStatus = oldStatus; |
| 5918 | return; |
| 5919 | } |
| 5920 | |
| 5921 | /** |
| 5922 | * Sometimes we want to conditionally do a transition. |
| 5923 | * For example if a client disconnects, we want to go to PRESENT |
| 5924 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 5925 | */ |
| 5926 | for (auto& rejectStatus : rejectSourceStates) { |
| 5927 | if (oldStatus == rejectStatus) { |
| 5928 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5929 | "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] | 5930 | mStatus = oldStatus; |
| 5931 | return; |
| 5932 | } |
| 5933 | } |
| 5934 | |
| 5935 | onStatusUpdatedLocked(cameraId, status); |
| 5936 | } |
| 5937 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5938 | status_t CameraService::getTorchStatusLocked( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5939 | const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5940 | TorchModeStatus *status) const { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5941 | if (!status) { |
| 5942 | return BAD_VALUE; |
| 5943 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5944 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 5945 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5946 | // invalid camera ID or the camera doesn't have a flash unit |
| 5947 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5948 | } |
| 5949 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5950 | *status = mTorchStatusMap.valueAt(index); |
| 5951 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5952 | } |
| 5953 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5954 | status_t CameraService::setTorchStatusLocked(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5955 | TorchModeStatus status) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5956 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 5957 | if (index == NAME_NOT_FOUND) { |
| 5958 | return BAD_VALUE; |
| 5959 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5960 | mTorchStatusMap.editValueAt(index) = status; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5961 | |
| 5962 | return OK; |
| 5963 | } |
| 5964 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5965 | std::list<std::string> CameraService::getLogicalCameras( |
| 5966 | const std::string& physicalCameraId) { |
| 5967 | std::list<std::string> retList; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5968 | Mutex::Autolock lock(mCameraStatesLock); |
| 5969 | for (const auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5970 | if (state.second->containsPhysicalCamera(physicalCameraId)) { |
| 5971 | retList.emplace_back(state.first); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5972 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5973 | } |
| 5974 | return retList; |
| 5975 | } |
| 5976 | |
| 5977 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5978 | const std::string& physicalCameraId, const std::list<std::string>& logicalCameraIds, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5979 | SystemCameraKind deviceKind, int32_t deviceId) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5980 | // mStatusListenerLock is expected to be locked |
| 5981 | for (const auto& logicalCameraId : logicalCameraIds) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5982 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5983 | // Note: we check only the deviceKind of the physical camera id |
| 5984 | // since, logical camera ids and their physical camera ids are |
| 5985 | // guaranteed to have the same system camera kind. |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 5986 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 5987 | listener->getListenerPid(), listener->getListenerUid())) { |
| 5988 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5989 | physicalCameraId.c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 5990 | continue; |
| 5991 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5992 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged(status, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5993 | logicalCameraId, physicalCameraId, deviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5994 | listener->handleBinderStatus(ret, |
| 5995 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 5996 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 5997 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5998 | } |
| 5999 | } |
| 6000 | } |
| 6001 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6002 | void CameraService::blockClientsForUid(uid_t uid) { |
| 6003 | const auto clients = mActiveClientManager.getAll(); |
| 6004 | for (auto& current : clients) { |
| 6005 | if (current != nullptr) { |
| 6006 | const auto basicClient = current->getValue(); |
| 6007 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { |
| 6008 | basicClient->block(); |
| 6009 | } |
| 6010 | } |
| 6011 | } |
| 6012 | } |
| 6013 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 6014 | void CameraService::blockAllClients() { |
| 6015 | const auto clients = mActiveClientManager.getAll(); |
| 6016 | for (auto& current : clients) { |
| 6017 | if (current != nullptr) { |
| 6018 | const auto basicClient = current->getValue(); |
| 6019 | if (basicClient.get() != nullptr) { |
| 6020 | basicClient->block(); |
| 6021 | } |
| 6022 | } |
| 6023 | } |
| 6024 | } |
| 6025 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 6026 | void CameraService::blockPrivacyEnabledClients() { |
| 6027 | const auto clients = mActiveClientManager.getAll(); |
| 6028 | for (auto& current : clients) { |
| 6029 | if (current != nullptr) { |
| 6030 | const auto basicClient = current->getValue(); |
| 6031 | if (basicClient.get() != nullptr) { |
| 6032 | std::string pkgName = basicClient->getPackageName(); |
| 6033 | bool cameraPrivacyEnabled = |
| 6034 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(toString16(pkgName)); |
| 6035 | if (cameraPrivacyEnabled) { |
| 6036 | basicClient->block(); |
| 6037 | } |
| 6038 | } |
| 6039 | } |
| 6040 | } |
| 6041 | } |
| 6042 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6043 | // NOTE: This is a remote API - make sure all args are validated |
| 6044 | 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] | 6045 | if (!checkCallingPermission(toString16(sManageCameraPermission), nullptr, nullptr)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6046 | return PERMISSION_DENIED; |
| 6047 | } |
| 6048 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 6049 | return BAD_VALUE; |
| 6050 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6051 | if (args.size() >= 3 && args[0] == toString16("set-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6052 | return handleSetUidState(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6053 | } else if (args.size() >= 2 && args[0] == toString16("reset-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6054 | return handleResetUidState(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6055 | } else if (args.size() >= 2 && args[0] == toString16("get-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6056 | return handleGetUidState(args, out, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6057 | } 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] | 6058 | return handleSetRotateAndCrop(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6059 | } 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] | 6060 | return handleGetRotateAndCrop(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6061 | } else if (args.size() >= 2 && args[0] == toString16("set-autoframing")) { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6062 | return handleSetAutoframing(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6063 | } else if (args.size() >= 1 && args[0] == toString16("get-autoframing")) { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6064 | return handleGetAutoframing(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6065 | } else if (args.size() >= 2 && args[0] == toString16("set-image-dump-mask")) { |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6066 | return handleSetImageDumpMask(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6067 | } else if (args.size() >= 1 && args[0] == toString16("get-image-dump-mask")) { |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6068 | return handleGetImageDumpMask(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6069 | } else if (args.size() >= 2 && args[0] == toString16("set-camera-mute")) { |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6070 | return handleSetCameraMute(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6071 | } 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] | 6072 | return handleSetStreamUseCaseOverrides(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6073 | } 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] | 6074 | handleClearStreamUseCaseOverrides(); |
| 6075 | return OK; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6076 | } else if (args.size() >= 1 && args[0] == toString16("set-zoom-override")) { |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6077 | return handleSetZoomOverride(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6078 | } else if (args.size() >= 2 && args[0] == toString16("watch")) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6079 | return handleWatchCommand(args, in, out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6080 | } else if (args.size() >= 2 && args[0] == toString16("set-watchdog")) { |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6081 | return handleSetCameraServiceWatchdog(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6082 | } else if (args.size() >= 4 && args[0] == toString16("remap-camera-id")) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 6083 | return handleCameraIdRemapping(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6084 | } else if (args.size() == 1 && args[0] == toString16("help")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6085 | printHelp(out); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6086 | return OK; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6087 | } |
| 6088 | printHelp(err); |
| 6089 | return BAD_VALUE; |
| 6090 | } |
| 6091 | |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 6092 | status_t CameraService::handleCameraIdRemapping(const Vector<String16>& args, int err) { |
| 6093 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 6094 | if (uid != AID_ROOT) { |
| 6095 | dprintf(err, "Must be adb root\n"); |
| 6096 | return PERMISSION_DENIED; |
| 6097 | } |
| 6098 | if (args.size() != 4) { |
| 6099 | dprintf(err, "Expected format: remap-camera-id <PACKAGE> <Id0> <Id1>\n"); |
| 6100 | return BAD_VALUE; |
| 6101 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6102 | std::string packageName = toStdString(args[1]); |
| 6103 | std::string cameraIdToReplace = toStdString(args[2]); |
| 6104 | std::string cameraIdNew = toStdString(args[3]); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 6105 | remapCameraIds({{packageName, {{cameraIdToReplace, cameraIdNew}}}}); |
| 6106 | return OK; |
| 6107 | } |
| 6108 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6109 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6110 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6111 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6112 | bool active = false; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6113 | if (args[2] == toString16("active")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6114 | active = true; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6115 | } else if ((args[2] != toString16("idle"))) { |
| 6116 | 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] | 6117 | return BAD_VALUE; |
| 6118 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6119 | |
| 6120 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6121 | if (args.size() >= 5 && args[3] == toString16("--user")) { |
| 6122 | userId = atoi(toStdString(args[4]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6123 | } |
| 6124 | |
| 6125 | uid_t uid; |
| 6126 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
| 6127 | return BAD_VALUE; |
| 6128 | } |
| 6129 | |
| 6130 | mUidPolicy->addOverrideUid(uid, packageName, active); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6131 | return NO_ERROR; |
| 6132 | } |
| 6133 | |
| 6134 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6135 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6136 | |
| 6137 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6138 | if (args.size() >= 4 && args[2] == toString16("--user")) { |
| 6139 | userId = atoi(toStdString(args[3]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6140 | } |
| 6141 | |
| 6142 | uid_t uid; |
| 6143 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6144 | return BAD_VALUE; |
| 6145 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6146 | |
| 6147 | mUidPolicy->removeOverrideUid(uid, packageName); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6148 | return NO_ERROR; |
| 6149 | } |
| 6150 | |
| 6151 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6152 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6153 | |
| 6154 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6155 | if (args.size() >= 4 && args[2] == toString16("--user")) { |
| 6156 | userId = atoi(toStdString(args[3]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6157 | } |
| 6158 | |
| 6159 | uid_t uid; |
| 6160 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6161 | return BAD_VALUE; |
| 6162 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6163 | |
| 6164 | if (mUidPolicy->isUidActive(uid, packageName)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6165 | return dprintf(out, "active\n"); |
| 6166 | } else { |
| 6167 | return dprintf(out, "idle\n"); |
| 6168 | } |
| 6169 | } |
| 6170 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6171 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6172 | int rotateValue = atoi(toStdString(args[1]).c_str()); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6173 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || |
| 6174 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; |
| 6175 | Mutex::Autolock lock(mServiceLock); |
| 6176 | |
| 6177 | mOverrideRotateAndCropMode = rotateValue; |
| 6178 | |
| 6179 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; |
| 6180 | |
| 6181 | const auto clients = mActiveClientManager.getAll(); |
| 6182 | for (auto& current : clients) { |
| 6183 | if (current != nullptr) { |
| 6184 | const auto basicClient = current->getValue(); |
| 6185 | if (basicClient.get() != nullptr) { |
| 6186 | basicClient->setRotateAndCropOverride(rotateValue); |
| 6187 | } |
| 6188 | } |
| 6189 | } |
| 6190 | |
| 6191 | return OK; |
| 6192 | } |
| 6193 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6194 | status_t CameraService::handleSetAutoframing(const Vector<String16>& args) { |
| 6195 | char* end; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6196 | int autoframingValue = (int) strtol(toStdString(args[1]).c_str(), &end, /*base=*/10); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6197 | if ((*end != '\0') || |
| 6198 | (autoframingValue != ANDROID_CONTROL_AUTOFRAMING_OFF && |
| 6199 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_ON && |
| 6200 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_AUTO)) { |
| 6201 | return BAD_VALUE; |
| 6202 | } |
| 6203 | |
| 6204 | Mutex::Autolock lock(mServiceLock); |
| 6205 | mOverrideAutoframingMode = autoframingValue; |
| 6206 | |
| 6207 | if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) return OK; |
| 6208 | |
| 6209 | const auto clients = mActiveClientManager.getAll(); |
| 6210 | for (auto& current : clients) { |
| 6211 | if (current != nullptr) { |
| 6212 | const auto basicClient = current->getValue(); |
| 6213 | if (basicClient.get() != nullptr) { |
| 6214 | basicClient->setAutoframingOverride(autoframingValue); |
| 6215 | } |
| 6216 | } |
| 6217 | } |
| 6218 | |
| 6219 | return OK; |
| 6220 | } |
| 6221 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6222 | status_t CameraService::handleSetCameraServiceWatchdog(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6223 | int enableWatchdog = atoi(toStdString(args[1]).c_str()); |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6224 | |
| 6225 | if (enableWatchdog < 0 || enableWatchdog > 1) return BAD_VALUE; |
| 6226 | |
| 6227 | Mutex::Autolock lock(mServiceLock); |
| 6228 | |
| 6229 | mCameraServiceWatchdogEnabled = enableWatchdog; |
| 6230 | |
| 6231 | const auto clients = mActiveClientManager.getAll(); |
| 6232 | for (auto& current : clients) { |
| 6233 | if (current != nullptr) { |
| 6234 | const auto basicClient = current->getValue(); |
| 6235 | if (basicClient.get() != nullptr) { |
| 6236 | basicClient->setCameraServiceWatchdog(enableWatchdog); |
| 6237 | } |
| 6238 | } |
| 6239 | } |
| 6240 | |
| 6241 | return OK; |
| 6242 | } |
| 6243 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6244 | status_t CameraService::handleGetRotateAndCrop(int out) { |
| 6245 | Mutex::Autolock lock(mServiceLock); |
| 6246 | |
| 6247 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); |
| 6248 | } |
| 6249 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6250 | status_t CameraService::handleGetAutoframing(int out) { |
| 6251 | Mutex::Autolock lock(mServiceLock); |
| 6252 | |
| 6253 | return dprintf(out, "autoframing override: %d\n", mOverrideAutoframingMode); |
| 6254 | } |
| 6255 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6256 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { |
| 6257 | char *endPtr; |
| 6258 | errno = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6259 | std::string maskString = toStdString(args[1]); |
| 6260 | long maskValue = strtol(maskString.c_str(), &endPtr, 10); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6261 | |
| 6262 | if (errno != 0) return BAD_VALUE; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6263 | if (endPtr != maskString.c_str() + maskString.size()) return BAD_VALUE; |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6264 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; |
| 6265 | |
| 6266 | Mutex::Autolock lock(mServiceLock); |
| 6267 | |
| 6268 | mImageDumpMask = maskValue; |
| 6269 | |
| 6270 | return OK; |
| 6271 | } |
| 6272 | |
| 6273 | status_t CameraService::handleGetImageDumpMask(int out) { |
| 6274 | Mutex::Autolock lock(mServiceLock); |
| 6275 | |
| 6276 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); |
| 6277 | } |
| 6278 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6279 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6280 | int muteValue = strtol(toStdString(args[1]).c_str(), nullptr, 10); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6281 | if (errno != 0) return BAD_VALUE; |
| 6282 | |
| 6283 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; |
| 6284 | Mutex::Autolock lock(mServiceLock); |
| 6285 | |
| 6286 | mOverrideCameraMuteMode = (muteValue == 1); |
| 6287 | |
| 6288 | const auto clients = mActiveClientManager.getAll(); |
| 6289 | for (auto& current : clients) { |
| 6290 | if (current != nullptr) { |
| 6291 | const auto basicClient = current->getValue(); |
| 6292 | if (basicClient.get() != nullptr) { |
| 6293 | if (basicClient->supportsCameraMute()) { |
| 6294 | basicClient->setCameraMute(mOverrideCameraMuteMode); |
| 6295 | } |
| 6296 | } |
| 6297 | } |
| 6298 | } |
| 6299 | |
| 6300 | return OK; |
| 6301 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6302 | |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6303 | status_t CameraService::handleSetStreamUseCaseOverrides(const Vector<String16>& args) { |
| 6304 | std::vector<int64_t> useCasesOverride; |
| 6305 | for (size_t i = 1; i < args.size(); i++) { |
| 6306 | int64_t useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6307 | std::string arg = toStdString(args[i]); |
| 6308 | if (arg == "DEFAULT") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6309 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6310 | } else if (arg == "PREVIEW") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6311 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6312 | } else if (arg == "STILL_CAPTURE") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6313 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_STILL_CAPTURE; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6314 | } else if (arg == "VIDEO_RECORD") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6315 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_RECORD; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6316 | } else if (arg == "PREVIEW_VIDEO_STILL") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6317 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW_VIDEO_STILL; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6318 | } else if (arg == "VIDEO_CALL") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6319 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6320 | } else if (arg == "CROPPED_RAW") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6321 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_CROPPED_RAW; |
| 6322 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6323 | ALOGE("%s: Invalid stream use case %s", __FUNCTION__, arg.c_str()); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6324 | return BAD_VALUE; |
| 6325 | } |
| 6326 | useCasesOverride.push_back(useCase); |
| 6327 | } |
| 6328 | |
| 6329 | Mutex::Autolock lock(mServiceLock); |
| 6330 | mStreamUseCaseOverrides = std::move(useCasesOverride); |
| 6331 | |
| 6332 | return OK; |
| 6333 | } |
| 6334 | |
| 6335 | void CameraService::handleClearStreamUseCaseOverrides() { |
| 6336 | Mutex::Autolock lock(mServiceLock); |
| 6337 | mStreamUseCaseOverrides.clear(); |
| 6338 | } |
| 6339 | |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6340 | status_t CameraService::handleSetZoomOverride(const Vector<String16>& args) { |
| 6341 | char* end; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6342 | int zoomOverrideValue = strtol(toStdString(args[1]).c_str(), &end, /*base=*/10); |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6343 | if ((*end != '\0') || |
| 6344 | (zoomOverrideValue != -1 && |
| 6345 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF && |
| 6346 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM)) { |
| 6347 | return BAD_VALUE; |
| 6348 | } |
| 6349 | |
| 6350 | Mutex::Autolock lock(mServiceLock); |
| 6351 | mZoomOverrideValue = zoomOverrideValue; |
| 6352 | |
| 6353 | const auto clients = mActiveClientManager.getAll(); |
| 6354 | for (auto& current : clients) { |
| 6355 | if (current != nullptr) { |
| 6356 | const auto basicClient = current->getValue(); |
| 6357 | if (basicClient.get() != nullptr) { |
| 6358 | if (basicClient->supportsZoomOverride()) { |
| 6359 | basicClient->setZoomOverride(mZoomOverrideValue); |
| 6360 | } |
| 6361 | } |
| 6362 | } |
| 6363 | } |
| 6364 | |
| 6365 | return OK; |
| 6366 | } |
| 6367 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6368 | status_t CameraService::handleWatchCommand(const Vector<String16>& args, int inFd, int outFd) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6369 | if (args.size() >= 3 && args[1] == toString16("start")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6370 | return startWatchingTags(args, outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6371 | } else if (args.size() == 2 && args[1] == toString16("stop")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6372 | return stopWatchingTags(outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6373 | } else if (args.size() == 2 && args[1] == toString16("dump")) { |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6374 | return printWatchedTags(outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6375 | } else if (args.size() >= 2 && args[1] == toString16("live")) { |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6376 | return printWatchedTagsUntilInterrupt(args, inFd, outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6377 | } else if (args.size() == 2 && args[1] == toString16("clear")) { |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6378 | return clearCachedMonitoredTagDumps(outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6379 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6380 | dprintf(outFd, "Camera service watch commands:\n" |
| 6381 | " start -m <comma_separated_tag_list> [-c <comma_separated_client_list>]\n" |
| 6382 | " starts watching the provided tags for clients with provided package\n" |
| 6383 | " recognizes tag shorthands like '3a'\n" |
| 6384 | " 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] | 6385 | " dump dumps the monitoring information and exits\n" |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6386 | " stop stops watching all tags\n" |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6387 | " live [-n <refresh_interval_ms>]\n" |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6388 | " prints the monitored information in real time\n" |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6389 | " Hit return to exit\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6390 | " clear clears all buffers storing information for watch command"); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 6391 | return BAD_VALUE; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6392 | } |
| 6393 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6394 | status_t CameraService::startWatchingTags(const Vector<String16> &args, int outFd) { |
| 6395 | Mutex::Autolock lock(mLogLock); |
| 6396 | size_t tagsIdx; // index of '-m' |
| 6397 | String16 tags(""); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6398 | for (tagsIdx = 2; tagsIdx < args.size() && args[tagsIdx] != toString16("-m"); tagsIdx++); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6399 | if (tagsIdx < args.size() - 1) { |
| 6400 | tags = args[tagsIdx + 1]; |
| 6401 | } else { |
| 6402 | dprintf(outFd, "No tags provided.\n"); |
| 6403 | return BAD_VALUE; |
| 6404 | } |
| 6405 | |
| 6406 | size_t clientsIdx; // index of '-c' |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6407 | // watch all clients if no clients are provided |
| 6408 | String16 clients = toString16(kWatchAllClientsFlag); |
| 6409 | for (clientsIdx = 2; clientsIdx < args.size() && args[clientsIdx] != toString16("-c"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6410 | clientsIdx++); |
| 6411 | if (clientsIdx < args.size() - 1) { |
| 6412 | clients = args[clientsIdx + 1]; |
| 6413 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6414 | parseClientsToWatchLocked(toStdString(clients)); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6415 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6416 | // track tags to initialize future clients with the monitoring information |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6417 | mMonitorTags = toStdString(tags); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6418 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6419 | bool serviceLock = tryLock(mServiceLock); |
| 6420 | int numWatchedClients = 0; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6421 | auto cameraClients = mActiveClientManager.getAll(); |
| 6422 | for (const auto &clientDescriptor: cameraClients) { |
| 6423 | if (clientDescriptor == nullptr) { continue; } |
| 6424 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6425 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6426 | |
| 6427 | if (isClientWatchedLocked(client.get())) { |
| 6428 | client->startWatchingTags(mMonitorTags, outFd); |
| 6429 | numWatchedClients++; |
| 6430 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6431 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6432 | dprintf(outFd, "Started watching %d active clients\n", numWatchedClients); |
| 6433 | |
| 6434 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6435 | return OK; |
| 6436 | } |
| 6437 | |
| 6438 | status_t CameraService::stopWatchingTags(int outFd) { |
| 6439 | // clear mMonitorTags to prevent new clients from monitoring tags at initialization |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6440 | Mutex::Autolock lock(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6441 | mMonitorTags = ""; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6442 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6443 | mWatchedClientPackages.clear(); |
| 6444 | mWatchedClientsDumpCache.clear(); |
| 6445 | |
| 6446 | bool serviceLock = tryLock(mServiceLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6447 | auto cameraClients = mActiveClientManager.getAll(); |
| 6448 | for (const auto &clientDescriptor : cameraClients) { |
| 6449 | if (clientDescriptor == nullptr) { continue; } |
| 6450 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6451 | if (client.get() == nullptr) { continue; } |
| 6452 | client->stopWatchingTags(outFd); |
| 6453 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6454 | dprintf(outFd, "Stopped watching all clients.\n"); |
| 6455 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6456 | return OK; |
| 6457 | } |
| 6458 | |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6459 | status_t CameraService::clearCachedMonitoredTagDumps(int outFd) { |
| 6460 | Mutex::Autolock lock(mLogLock); |
| 6461 | size_t clearedSize = mWatchedClientsDumpCache.size(); |
| 6462 | mWatchedClientsDumpCache.clear(); |
| 6463 | dprintf(outFd, "Cleared tag information of %zu cached clients.\n", clearedSize); |
| 6464 | return OK; |
| 6465 | } |
| 6466 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6467 | status_t CameraService::printWatchedTags(int outFd) { |
| 6468 | Mutex::Autolock logLock(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6469 | std::set<std::string> connectedMonitoredClients; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6470 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6471 | bool printedSomething = false; // tracks if any monitoring information was printed |
| 6472 | // (from either cached or active clients) |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6473 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6474 | bool serviceLock = tryLock(mServiceLock); |
| 6475 | // get all watched clients that are currently connected |
| 6476 | for (const auto &clientDescriptor: mActiveClientManager.getAll()) { |
| 6477 | if (clientDescriptor == nullptr) { continue; } |
| 6478 | |
| 6479 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6480 | if (client.get() == nullptr) { continue; } |
| 6481 | if (!isClientWatchedLocked(client.get())) { continue; } |
| 6482 | |
| 6483 | std::vector<std::string> dumpVector; |
| 6484 | client->dumpWatchedEventsToVector(dumpVector); |
| 6485 | |
| 6486 | size_t printIdx = dumpVector.size(); |
| 6487 | if (printIdx == 0) { |
| 6488 | continue; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6489 | } |
| 6490 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6491 | // Print tag dumps for active client |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6492 | const std::string &cameraId = clientDescriptor->getKey(); |
| 6493 | dprintf(outFd, "Client: %s (active)\n", client->getPackageName().c_str()); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6494 | while(printIdx > 0) { |
| 6495 | printIdx--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6496 | dprintf(outFd, "%s:%s %s", cameraId.c_str(), client->getPackageName().c_str(), |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6497 | dumpVector[printIdx].c_str()); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6498 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6499 | dprintf(outFd, "\n"); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6500 | printedSomething = true; |
| 6501 | |
| 6502 | connectedMonitoredClients.emplace(client->getPackageName()); |
| 6503 | } |
| 6504 | if (serviceLock) { mServiceLock.unlock(); } |
| 6505 | |
| 6506 | // Print entries in mWatchedClientsDumpCache for clients that are not connected |
| 6507 | for (const auto &kv: mWatchedClientsDumpCache) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6508 | const std::string &package = kv.first; |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6509 | if (connectedMonitoredClients.find(package) != connectedMonitoredClients.end()) { |
| 6510 | continue; |
| 6511 | } |
| 6512 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6513 | dprintf(outFd, "Client: %s (cached)\n", package.c_str()); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6514 | dprintf(outFd, "%s\n", kv.second.c_str()); |
| 6515 | printedSomething = true; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6516 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6517 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6518 | if (!printedSomething) { |
| 6519 | dprintf(outFd, "No monitoring information to print.\n"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6520 | } |
| 6521 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6522 | return OK; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6523 | } |
| 6524 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6525 | // Print all events in vector `events' that came after lastPrintedEvent |
| 6526 | void printNewWatchedEvents(int outFd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6527 | const std::string &cameraId, |
| 6528 | const std::string &packageName, |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6529 | const std::vector<std::string> &events, |
| 6530 | const std::string &lastPrintedEvent) { |
| 6531 | if (events.empty()) { return; } |
| 6532 | |
| 6533 | // index of lastPrintedEvent in events. |
| 6534 | // lastPrintedIdx = events.size() if lastPrintedEvent is not in events |
| 6535 | size_t lastPrintedIdx; |
| 6536 | for (lastPrintedIdx = 0; |
| 6537 | lastPrintedIdx < events.size() && lastPrintedEvent != events[lastPrintedIdx]; |
| 6538 | lastPrintedIdx++); |
| 6539 | |
| 6540 | if (lastPrintedIdx == 0) { return; } // early exit if no new event in `events` |
| 6541 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6542 | // print events in chronological order (latest event last) |
| 6543 | size_t idxToPrint = lastPrintedIdx; |
| 6544 | do { |
| 6545 | idxToPrint--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6546 | dprintf(outFd, "%s:%s %s", cameraId.c_str(), packageName.c_str(), |
| 6547 | events[idxToPrint].c_str()); |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6548 | } while (idxToPrint != 0); |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6549 | } |
| 6550 | |
| 6551 | // Returns true if adb shell cmd watch should be interrupted based on data in inFd. The watch |
| 6552 | // command should be interrupted if the user presses the return key, or if user loses any way to |
| 6553 | // signal interrupt. |
| 6554 | // If timeoutMs == 0, this function will always return false |
| 6555 | bool shouldInterruptWatchCommand(int inFd, int outFd, long timeoutMs) { |
| 6556 | struct timeval startTime; |
| 6557 | int startTimeError = gettimeofday(&startTime, nullptr); |
| 6558 | if (startTimeError) { |
| 6559 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 6560 | return true; |
| 6561 | } |
| 6562 | |
| 6563 | const nfds_t numFds = 1; |
| 6564 | struct pollfd pollFd = { .fd = inFd, .events = POLLIN, .revents = 0 }; |
| 6565 | |
| 6566 | struct timeval currTime; |
| 6567 | char buffer[2]; |
| 6568 | while(true) { |
| 6569 | int currTimeError = gettimeofday(&currTime, nullptr); |
| 6570 | if (currTimeError) { |
| 6571 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 6572 | return true; |
| 6573 | } |
| 6574 | |
| 6575 | long elapsedTimeMs = ((currTime.tv_sec - startTime.tv_sec) * 1000L) |
| 6576 | + ((currTime.tv_usec - startTime.tv_usec) / 1000L); |
| 6577 | int remainingTimeMs = (int) (timeoutMs - elapsedTimeMs); |
| 6578 | |
| 6579 | if (remainingTimeMs <= 0) { |
| 6580 | // No user interrupt within timeoutMs, don't interrupt watch command |
| 6581 | return false; |
| 6582 | } |
| 6583 | |
| 6584 | int numFdsUpdated = poll(&pollFd, numFds, remainingTimeMs); |
| 6585 | if (numFdsUpdated < 0) { |
| 6586 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 6587 | return true; |
| 6588 | } |
| 6589 | |
| 6590 | if (numFdsUpdated == 0) { |
| 6591 | // No user input within timeoutMs, don't interrupt watch command |
| 6592 | return false; |
| 6593 | } |
| 6594 | |
| 6595 | if (!(pollFd.revents & POLLIN)) { |
| 6596 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 6597 | return true; |
| 6598 | } |
| 6599 | |
| 6600 | ssize_t sizeRead = read(inFd, buffer, sizeof(buffer) - 1); |
| 6601 | if (sizeRead < 0) { |
| 6602 | dprintf(outFd, "Error reading user input. Exiting.\n"); |
| 6603 | return true; |
| 6604 | } |
| 6605 | |
| 6606 | if (sizeRead == 0) { |
| 6607 | // Reached end of input fd (can happen if input is piped) |
| 6608 | // User has no way to signal an interrupt, so interrupt here |
| 6609 | return true; |
| 6610 | } |
| 6611 | |
| 6612 | if (buffer[0] == '\n') { |
| 6613 | // User pressed return, interrupt watch command. |
| 6614 | return true; |
| 6615 | } |
| 6616 | } |
| 6617 | } |
| 6618 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6619 | status_t CameraService::printWatchedTagsUntilInterrupt(const Vector<String16> &args, |
| 6620 | int inFd, int outFd) { |
| 6621 | // Figure out refresh interval, if present in args |
| 6622 | long refreshTimeoutMs = 1000L; // refresh every 1s by default |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6623 | if (args.size() > 2) { |
| 6624 | size_t intervalIdx; // index of '-n' |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6625 | for (intervalIdx = 2; intervalIdx < args.size() && toString16("-n") != args[intervalIdx]; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6626 | intervalIdx++); |
| 6627 | |
| 6628 | size_t intervalValIdx = intervalIdx + 1; |
| 6629 | if (intervalValIdx < args.size()) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6630 | refreshTimeoutMs = strtol(toStdString(args[intervalValIdx]).c_str(), nullptr, 10); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6631 | if (errno) { return BAD_VALUE; } |
| 6632 | } |
| 6633 | } |
| 6634 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6635 | // Set min timeout of 10ms. This prevents edge cases in polling when timeout of 0 is passed. |
| 6636 | refreshTimeoutMs = refreshTimeoutMs < 10 ? 10 : refreshTimeoutMs; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6637 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6638 | dprintf(outFd, "Press return to exit...\n\n"); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6639 | std::map<std::string, std::string> packageNameToLastEvent; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6640 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6641 | while (true) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6642 | bool serviceLock = tryLock(mServiceLock); |
| 6643 | auto cameraClients = mActiveClientManager.getAll(); |
| 6644 | if (serviceLock) { mServiceLock.unlock(); } |
| 6645 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6646 | for (const auto& clientDescriptor : cameraClients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6647 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6648 | if (clientDescriptor == nullptr) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6649 | |
| 6650 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6651 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6652 | if (!isClientWatchedLocked(client.get())) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6653 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6654 | const std::string &packageName = client->getPackageName(); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6655 | // This also initializes the map entries with an empty string |
| 6656 | const std::string& lastPrintedEvent = packageNameToLastEvent[packageName]; |
| 6657 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6658 | std::vector<std::string> latestEvents; |
| 6659 | client->dumpWatchedEventsToVector(latestEvents); |
| 6660 | |
| 6661 | if (!latestEvents.empty()) { |
| 6662 | printNewWatchedEvents(outFd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6663 | clientDescriptor->getKey(), |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6664 | packageName, |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6665 | latestEvents, |
| 6666 | lastPrintedEvent); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6667 | packageNameToLastEvent[packageName] = latestEvents[0]; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6668 | } |
| 6669 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6670 | if (shouldInterruptWatchCommand(inFd, outFd, refreshTimeoutMs)) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6671 | break; |
| 6672 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6673 | } |
| 6674 | return OK; |
| 6675 | } |
| 6676 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6677 | void CameraService::parseClientsToWatchLocked(const std::string &clients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6678 | mWatchedClientPackages.clear(); |
| 6679 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6680 | std::istringstream iss(clients); |
| 6681 | std::string nextClient; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6682 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6683 | while (std::getline(iss, nextClient, ',')) { |
| 6684 | if (nextClient == kWatchAllClientsFlag) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6685 | // Don't need to track any other package if 'all' is present |
| 6686 | mWatchedClientPackages.clear(); |
| 6687 | mWatchedClientPackages.emplace(kWatchAllClientsFlag); |
| 6688 | break; |
| 6689 | } |
| 6690 | |
| 6691 | // track package names |
| 6692 | mWatchedClientPackages.emplace(nextClient); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6693 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6694 | } |
| 6695 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6696 | status_t CameraService::printHelp(int out) { |
| 6697 | return dprintf(out, "Camera service commands:\n" |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6698 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 6699 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 6700 | " 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] | 6701 | " set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" |
| 6702 | " Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" |
| 6703 | " 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] | 6704 | " set-autoframing <VALUE> overrides the autoframing value for AUTO\n" |
| 6705 | " Valid values 0=false, 1=true, 2=auto\n" |
| 6706 | " get-autoframing returns the current override autoframing value\n" |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6707 | " set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" |
| 6708 | " Valid values 0=OFF, 1=ON for JPEG\n" |
| 6709 | " 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] | 6710 | " set-camera-mute <0/1> enable or disable camera muting\n" |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6711 | " set-stream-use-case-override <usecase1> <usecase2> ... override stream use cases\n" |
| 6712 | " Use cases applied in descending resolutions. So usecase1 is assigned to the\n" |
| 6713 | " largest resolution, usecase2 is assigned to the 2nd largest resolution, and so\n" |
| 6714 | " on. In case the number of usecases is smaller than the number of streams, the\n" |
| 6715 | " last use case is assigned to all the remaining streams. In case of multiple\n" |
| 6716 | " streams with the same resolution, the tie-breaker is (JPEG, RAW, YUV, and PRIV)\n" |
| 6717 | " Valid values are (case sensitive): DEFAULT, PREVIEW, STILL_CAPTURE, VIDEO_RECORD,\n" |
| 6718 | " PREVIEW_VIDEO_STILL, VIDEO_CALL, CROPPED_RAW\n" |
| 6719 | " clear-stream-use-case-override clear the stream use case override\n" |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6720 | " set-zoom-override <-1/0/1> enable or disable zoom override\n" |
| 6721 | " 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] | 6722 | " set-watchdog <VALUE> enables or disables the camera service watchdog\n" |
| 6723 | " Valid values 0=disable, 1=enable\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6724 | " watch <start|stop|dump|print|clear> manages tag monitoring in connected clients\n" |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 6725 | " 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] | 6726 | " help print this message\n"); |
| 6727 | } |
| 6728 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6729 | bool CameraService::isClientWatched(const BasicClient *client) { |
| 6730 | Mutex::Autolock lock(mLogLock); |
| 6731 | return isClientWatchedLocked(client); |
| 6732 | } |
| 6733 | |
| 6734 | bool CameraService::isClientWatchedLocked(const BasicClient *client) { |
| 6735 | return mWatchedClientPackages.find(kWatchAllClientsFlag) != mWatchedClientPackages.end() || |
| 6736 | mWatchedClientPackages.find(client->getPackageName()) != mWatchedClientPackages.end(); |
| 6737 | } |
| 6738 | |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 6739 | int32_t CameraService::updateAudioRestriction() { |
| 6740 | Mutex::Autolock lock(mServiceLock); |
| 6741 | return updateAudioRestrictionLocked(); |
| 6742 | } |
| 6743 | |
| 6744 | int32_t CameraService::updateAudioRestrictionLocked() { |
| 6745 | int32_t mode = 0; |
| 6746 | // iterate through all active client |
| 6747 | for (const auto& i : mActiveClientManager.getAll()) { |
| 6748 | const auto clientSp = i->getValue(); |
| 6749 | mode |= clientSp->getAudioRestriction(); |
| 6750 | } |
| 6751 | |
| 6752 | bool modeChanged = (mAudioRestriction != mode); |
| 6753 | mAudioRestriction = mode; |
| 6754 | if (modeChanged) { |
| 6755 | mAppOps.setCameraAudioRestriction(mode); |
| 6756 | } |
| 6757 | return mode; |
| 6758 | } |
| 6759 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6760 | status_t CameraService::checkIfInjectionCameraIsPresent(const std::string& externalCamId, |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6761 | sp<BasicClient> clientSp) { |
| 6762 | std::unique_ptr<AutoConditionLock> lock = |
| 6763 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 6764 | status_t res = NO_ERROR; |
| 6765 | if ((res = checkIfDeviceIsUsable(externalCamId)) != NO_ERROR) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6766 | ALOGW("Device %s is not usable!", externalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6767 | mInjectionStatusListener->notifyInjectionError( |
| 6768 | externalCamId, UNKNOWN_TRANSACTION); |
| 6769 | clientSp->notifyError( |
| 6770 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 6771 | CaptureResultExtras()); |
| 6772 | |
| 6773 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 6774 | // other clients from connecting in mServiceLockWrapper if held |
| 6775 | mServiceLock.unlock(); |
| 6776 | |
| 6777 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 6778 | int64_t token = clearCallingIdentity(); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6779 | clientSp->disconnect(); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 6780 | restoreCallingIdentity(token); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6781 | |
| 6782 | // Reacquire mServiceLock |
| 6783 | mServiceLock.lock(); |
| 6784 | } |
| 6785 | |
| 6786 | return res; |
| 6787 | } |
| 6788 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 6789 | void CameraService::clearInjectionParameters() { |
| 6790 | { |
| 6791 | Mutex::Autolock lock(mInjectionParametersLock); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6792 | mInjectionInitPending = false; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 6793 | mInjectionInternalCamId = ""; |
| 6794 | } |
| 6795 | mInjectionExternalCamId = ""; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 6796 | mInjectionStatusListener->removeListener(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 6797 | } |
| 6798 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 6799 | } // namespace android |