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" |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 85 | #include "utils/CameraServiceProxyWrapper.h" |
Emilian Peev | 31bd242 | 2024-04-23 22:24:09 +0000 | [diff] [blame] | 86 | #include "utils/CameraTraces.h" |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 87 | #include "utils/SessionConfigurationUtils.h" |
Emilian Peev | 31bd242 | 2024-04-23 22:24:09 +0000 | [diff] [blame] | 88 | #include "utils/TagMonitor.h" |
| 89 | #include "utils/Utils.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 90 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 91 | namespace { |
| 92 | const char* kPermissionServiceName = "permission"; |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 93 | const char* kActivityServiceName = "activity"; |
| 94 | const char* kSensorPrivacyServiceName = "sensor_privacy"; |
| 95 | const char* kAppopsServiceName = "appops"; |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 96 | const char* kProcessInfoServiceName = "processinfo"; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 97 | const char* kVirtualDeviceBackCameraId = "0"; |
| 98 | const char* kVirtualDeviceFrontCameraId = "1"; |
| 99 | |
| 100 | int32_t getDeviceId(const android::CameraMetadata& cameraInfo) { |
| 101 | if (!cameraInfo.exists(ANDROID_INFO_DEVICE_ID)) { |
| 102 | return android::kDefaultDeviceId; |
| 103 | } |
| 104 | |
| 105 | const auto &deviceIdEntry = cameraInfo.find(ANDROID_INFO_DEVICE_ID); |
| 106 | return deviceIdEntry.data.i32[0]; |
| 107 | } |
| 108 | } // namespace anonymous |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 109 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 110 | namespace android { |
| 111 | |
Austin Borger | ea93124 | 2021-12-13 23:10:41 +0000 | [diff] [blame] | 112 | using namespace camera3; |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 113 | using namespace camera3::SessionConfigurationUtils; |
| 114 | |
| 115 | using binder::Status; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 116 | using companion::virtualnative::IVirtualDeviceManagerNative; |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 117 | using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService; |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 118 | using frameworks::cameraservice::service::implementation::AidlCameraService; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 119 | using hardware::ICamera; |
| 120 | using hardware::ICameraClient; |
| 121 | using hardware::ICameraServiceListener; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 122 | using hardware::camera2::ICameraInjectionCallback; |
| 123 | using hardware::camera2::ICameraInjectionSession; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 124 | using hardware::camera2::utils::CameraIdAndSessionConfiguration; |
| 125 | using hardware::camera2::utils::ConcurrentCameraIdCombination; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 126 | |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 127 | namespace flags = com::android::internal::camera::flags; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 128 | namespace vd_flags = android::companion::virtualdevice::flags; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 129 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 130 | // ---------------------------------------------------------------------------- |
| 131 | // Logging support -- this is for debugging only |
| 132 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 133 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 134 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 135 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 136 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 137 | |
| 138 | static void setLogLevel(int level) { |
| 139 | android_atomic_write(level, &gLogLevel); |
| 140 | } |
| 141 | |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 142 | int32_t format_as(CameraService::StatusInternal s) { |
| 143 | return fmt::underlying(s); |
| 144 | } |
| 145 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 146 | // ---------------------------------------------------------------------------- |
| 147 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 148 | // Permission strings (references to AttributionAndPermissionUtils for brevity) |
| 149 | static const std::string &sDumpPermission = |
| 150 | AttributionAndPermissionUtils::sDumpPermission; |
| 151 | static const std::string &sManageCameraPermission = |
| 152 | AttributionAndPermissionUtils::sManageCameraPermission; |
| 153 | static const std::string &sCameraSendSystemEventsPermission = |
| 154 | AttributionAndPermissionUtils::sCameraSendSystemEventsPermission; |
| 155 | static const std::string &sCameraInjectExternalCameraPermission = |
| 156 | AttributionAndPermissionUtils::sCameraInjectExternalCameraPermission; |
| 157 | |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 158 | // Constant integer for FGS Logging, used to denote the API type for logger |
| 159 | static const int LOG_FGS_CAMERA_API = 1; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 160 | const char *sFileName = "lastOpenSessionDumpFile"; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 161 | static constexpr int32_t kSystemNativeClientScore = resource_policy::PERCEPTIBLE_APP_ADJ; |
| 162 | static constexpr int32_t kSystemNativeClientState = |
| 163 | ActivityManager::PROCESS_STATE_PERSISTENT_UI; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 164 | static const std::string kServiceName("cameraserver"); |
Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame] | 165 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 166 | const std::string CameraService::kOfflineDevice("offline-"); |
| 167 | const std::string CameraService::kWatchAllClientsFlag("all"); |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 168 | |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 169 | constexpr int32_t kInvalidDeviceId = -1; |
| 170 | |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 171 | // Set to keep track of logged service error events. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 172 | static std::set<std::string> sServiceErrorEventSet; |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 173 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 174 | CameraService::CameraService( |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 175 | std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper, |
| 176 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils) : |
| 177 | AttributionAndPermissionUtilsEncapsulator(attributionAndPermissionUtils == nullptr ? |
| 178 | std::make_shared<AttributionAndPermissionUtils>()\ |
| 179 | : attributionAndPermissionUtils), |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 180 | mCameraServiceProxyWrapper(cameraServiceProxyWrapper == nullptr ? |
| 181 | std::make_shared<CameraServiceProxyWrapper>() : cameraServiceProxyWrapper), |
Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 182 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 183 | mNumberOfCameras(0), |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 184 | mNumberOfCamerasWithoutSystemCamera(0), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 185 | mSoundRef(0), mInitialized(false), |
| 186 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 187 | ALOGI("CameraService started (pid=%d)", getpid()); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 188 | mAttributionAndPermissionUtils->setCameraService(this); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 189 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 190 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 191 | if (mMemFd == -1) { |
| 192 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 193 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Charles Chen | a7b613c | 2023-01-24 21:57:33 +0000 | [diff] [blame] | 196 | // Enable processes with isolated AID to request the binder |
| 197 | void CameraService::instantiate() { |
| 198 | CameraService::publish(true); |
| 199 | } |
| 200 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 201 | void CameraService::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 202 | if (name != toString16(kAppopsServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
| 206 | ALOGV("appops service registered. setting camera audio restriction"); |
| 207 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
| 208 | } |
| 209 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 210 | void CameraService::onFirstRef() |
| 211 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 212 | ALOGI("CameraService process starting"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 213 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 214 | BnCameraService::onFirstRef(); |
| 215 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 216 | // Update battery life tracking if service is restarting |
| 217 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 218 | notifier.noteResetCamera(); |
| 219 | notifier.noteResetFlashlight(); |
| 220 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 221 | status_t res = INVALID_OPERATION; |
Eino-Ville Talvala | 9cbbc83 | 2017-01-23 15:39:53 -0800 | [diff] [blame] | 222 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 223 | res = enumerateProviders(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 224 | if (res == OK) { |
| 225 | mInitialized = true; |
| 226 | } |
| 227 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 228 | mUidPolicy = new UidPolicy(this); |
| 229 | mUidPolicy->registerSelf(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 230 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this, mAttributionAndPermissionUtils); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 231 | mSensorPrivacyPolicy->registerSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 232 | mInjectionStatusListener = new InjectionStatusListener(this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 233 | |
| 234 | // appops function setCamerAudioRestriction uses getService which |
| 235 | // is blocking till the appops service is ready. To enable early |
| 236 | // boot availability for cameraservice, use checkService which is |
| 237 | // non blocking and register for notifications |
| 238 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 239 | sp<IBinder> binder = sm->checkService(toString16(kAppopsServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 240 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 241 | sm->registerForNotifications(toString16(kAppopsServiceName), this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 242 | } else { |
| 243 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
| 244 | } |
| 245 | |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 246 | sp<HidlCameraService> hcs = HidlCameraService::getInstance(this); |
| 247 | if (hcs->registerAsService() != android::OK) { |
Devin Moore | a1350e7 | 2023-01-11 23:40:42 +0000 | [diff] [blame] | 248 | // Deprecated, so it will fail to register on newer devices |
| 249 | ALOGW("%s: Did not register default android.frameworks.cameraservice.service@2.2", |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 250 | __FUNCTION__); |
| 251 | } |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 252 | |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 253 | if (!AidlCameraService::registerService(this)) { |
| 254 | ALOGE("%s: Failed to register default AIDL VNDK CameraService", __FUNCTION__); |
| 255 | } |
| 256 | |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 257 | // This needs to be last call in this function, so that it's as close to |
| 258 | // ServiceManager::addService() as possible. |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 259 | mCameraServiceProxyWrapper->pingCameraServiceProxy(); |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 260 | ALOGI("CameraService pinged cameraservice proxy"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 263 | status_t CameraService::enumerateProviders() { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 264 | status_t res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 265 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 266 | std::vector<std::string> deviceIds; |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 267 | std::unordered_map<std::string, std::set<std::string>> unavailPhysicalIds; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 268 | { |
| 269 | Mutex::Autolock l(mServiceLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 270 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 271 | if (nullptr == mCameraProviderManager.get()) { |
| 272 | mCameraProviderManager = new CameraProviderManager(); |
| 273 | res = mCameraProviderManager->initialize(this); |
| 274 | if (res != OK) { |
| 275 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", |
| 276 | __FUNCTION__, strerror(-res), res); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 277 | logServiceError("Unable to initialize camera provider manager", |
| 278 | ERROR_DISCONNECTED); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 279 | return res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 283 | // Setup vendor tags before we call get_camera_info the first time |
| 284 | // because HAL might need to setup static vendor keys in get_camera_info |
| 285 | // TODO: maybe put this into CameraProviderManager::initialize()? |
| 286 | mCameraProviderManager->setUpVendorTags(); |
| 287 | |
| 288 | if (nullptr == mFlashlight.get()) { |
| 289 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 292 | res = mFlashlight->findFlashUnits(); |
| 293 | if (res != OK) { |
| 294 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); |
| 295 | } |
| 296 | |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 297 | deviceIds = mCameraProviderManager->getCameraDeviceIds(&unavailPhysicalIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 300 | for (auto& cameraId : deviceIds) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 301 | if (getCameraState(cameraId) == nullptr) { |
| 302 | onDeviceStatusChanged(cameraId, CameraDeviceStatus::PRESENT); |
Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 303 | } |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 304 | if (unavailPhysicalIds.count(cameraId) > 0) { |
| 305 | for (const auto& physicalId : unavailPhysicalIds[cameraId]) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 306 | onDeviceStatusChanged(cameraId, physicalId, CameraDeviceStatus::NOT_PRESENT); |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 311 | // Derive primary rear/front cameras, and filter their charactierstics. |
| 312 | // 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] | 313 | if (SessionConfigurationUtils::IS_PERF_CLASS) { |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 314 | // Assume internal cameras are advertised from the same |
| 315 | // provider. If multiple providers are registered at different time, |
| 316 | // and each provider contains multiple internal color cameras, the current |
| 317 | // logic may filter the characteristics of more than one front/rear color |
| 318 | // cameras. |
| 319 | Mutex::Autolock l(mServiceLock); |
| 320 | filterSPerfClassCharacteristicsLocked(); |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 321 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 322 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 323 | return OK; |
| 324 | } |
| 325 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 326 | void CameraService::broadcastTorchModeStatus(const std::string& cameraId, TorchModeStatus status, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 327 | SystemCameraKind systemCameraKind) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 328 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 329 | auto [deviceId, mappedCameraId] = |
| 330 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 331 | |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 332 | Mutex::Autolock lock(mStatusListenerLock); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 333 | for (auto& i : mListenerList) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 334 | if (shouldSkipStatusUpdates(systemCameraKind, i->isVendorListener(), i->getListenerPid(), |
| 335 | i->getListenerUid())) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 336 | ALOGV("%s: Skipping torch callback for system-only camera device %s", |
| 337 | __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 338 | continue; |
| 339 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 340 | |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 341 | auto ret = i->getListener()->onTorchStatusChanged(mapToInterface(status), |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 342 | mappedCameraId, deviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 343 | i->handleBinderStatus(ret, "%s: Failed to trigger onTorchStatusChanged for %d:%d: %d", |
| 344 | __FUNCTION__, i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 348 | CameraService::~CameraService() { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 349 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 350 | mUidPolicy->unregisterSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 351 | mSensorPrivacyPolicy->unregisterSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 352 | mInjectionStatusListener->removeListener(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 355 | void CameraService::onNewProviderRegistered() { |
| 356 | enumerateProviders(); |
| 357 | } |
| 358 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 359 | void CameraService::filterAPI1SystemCameraLocked( |
| 360 | const std::vector<std::string> &normalDeviceIds) { |
| 361 | mNormalDeviceIdsWithoutSystemCamera.clear(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 362 | for (auto &cameraId : normalDeviceIds) { |
| 363 | if (vd_flags::camera_device_awareness()) { |
| 364 | CameraMetadata cameraInfo; |
| 365 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 366 | cameraId, false, &cameraInfo, |
| 367 | hardware::ICameraService::ROTATION_OVERRIDE_NONE); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 368 | int32_t deviceId = kDefaultDeviceId; |
| 369 | if (res != OK) { |
| 370 | ALOGW("%s: Not able to get camera characteristics for camera id %s", |
| 371 | __FUNCTION__, cameraId.c_str()); |
| 372 | } else { |
| 373 | deviceId = getDeviceId(cameraInfo); |
| 374 | } |
| 375 | // Cameras associated with non-default device id's (i.e., virtual cameras) can never be |
| 376 | // system cameras, so skip for non-default device id's. |
| 377 | if (deviceId != kDefaultDeviceId) { |
| 378 | continue; |
| 379 | } |
| 380 | } |
| 381 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 382 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 383 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 384 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 385 | continue; |
| 386 | } |
| 387 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 388 | // All system camera ids will necessarily come after public camera |
| 389 | // device ids as per the HAL interface contract. |
| 390 | break; |
| 391 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 392 | mNormalDeviceIdsWithoutSystemCamera.push_back(cameraId); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 393 | } |
| 394 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, |
| 395 | mNormalDeviceIdsWithoutSystemCamera.size()); |
| 396 | } |
| 397 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 398 | status_t CameraService::getSystemCameraKind(const std::string& cameraId, |
| 399 | SystemCameraKind *kind) const { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 400 | auto state = getCameraState(cameraId); |
| 401 | if (state != nullptr) { |
| 402 | *kind = state->getSystemCameraKind(); |
| 403 | return OK; |
| 404 | } |
| 405 | // Hidden physical camera ids won't have CameraState |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 406 | return mCameraProviderManager->getSystemCameraKind(cameraId, kind); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 409 | void CameraService::updateCameraNumAndIds() { |
| 410 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 411 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); |
| 412 | // Excludes hidden secure cameras |
| 413 | mNumberOfCameras = |
| 414 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; |
| 415 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 416 | mNormalDeviceIds = |
| 417 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 418 | filterAPI1SystemCameraLocked(mNormalDeviceIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 421 | void CameraService::filterSPerfClassCharacteristicsLocked() { |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 422 | // To claim to be S Performance primary cameras, the cameras must be |
| 423 | // backward compatible. So performance class primary camera Ids must be API1 |
| 424 | // compatible. |
| 425 | bool firstRearCameraSeen = false, firstFrontCameraSeen = false; |
| 426 | for (const auto& cameraId : mNormalDeviceIdsWithoutSystemCamera) { |
| 427 | int facing = -1; |
| 428 | int orientation = 0; |
Shuzhen Wang | f221e8d | 2022-12-15 13:26:29 -0800 | [diff] [blame] | 429 | int portraitRotation; |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 430 | getDeviceVersion(cameraId, |
| 431 | /*rotationOverride*/hardware::ICameraService::ROTATION_OVERRIDE_NONE, |
| 432 | /*out*/&portraitRotation, /*out*/&facing, /*out*/&orientation); |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 433 | if (facing == -1) { |
| 434 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.c_str()); |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | if ((facing == hardware::CAMERA_FACING_BACK && !firstRearCameraSeen) || |
| 439 | (facing == hardware::CAMERA_FACING_FRONT && !firstFrontCameraSeen)) { |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 440 | status_t res = mCameraProviderManager->filterSmallJpegSizes(cameraId); |
| 441 | if (res == OK) { |
| 442 | mPerfClassPrimaryCameraIds.insert(cameraId); |
| 443 | } else { |
| 444 | ALOGE("%s: Failed to filter small JPEG sizes for performance class primary " |
| 445 | "camera %s: %s(%d)", __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
| 446 | break; |
| 447 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 448 | |
| 449 | if (facing == hardware::CAMERA_FACING_BACK) { |
| 450 | firstRearCameraSeen = true; |
| 451 | } |
| 452 | if (facing == hardware::CAMERA_FACING_FRONT) { |
| 453 | firstFrontCameraSeen = true; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (firstRearCameraSeen && firstFrontCameraSeen) { |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 463 | void CameraService::addStates(const std::string& cameraId) { |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 464 | CameraResourceCost cost; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 465 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); |
| 466 | if (res != OK) { |
| 467 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); |
| 468 | return; |
| 469 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 470 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 471 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); |
| 472 | if (res != OK) { |
| 473 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); |
| 474 | return; |
| 475 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 476 | std::vector<std::string> physicalCameraIds; |
| 477 | mCameraProviderManager->isLogicalCamera(cameraId, &physicalCameraIds); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 478 | std::set<std::string> conflicting; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 479 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 480 | conflicting.emplace(cost.conflictingDevices[i]); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | { |
| 484 | Mutex::Autolock lock(mCameraStatesLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 485 | mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost.resourceCost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 486 | conflicting, deviceKind, physicalCameraIds)); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 489 | if (mFlashlight->hasFlashUnit(cameraId)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 490 | Mutex::Autolock al(mTorchStatusMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 491 | mTorchStatusMap.add(cameraId, TorchModeStatus::AVAILABLE_OFF); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 492 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 493 | broadcastTorchModeStatus(cameraId, TorchModeStatus::AVAILABLE_OFF, deviceKind); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 494 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 495 | |
| 496 | updateCameraNumAndIds(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 497 | logDeviceAdded(cameraId, "Device added"); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 498 | } |
| 499 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 500 | void CameraService::removeStates(const std::string& cameraId) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 501 | updateCameraNumAndIds(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 502 | if (mFlashlight->hasFlashUnit(cameraId)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 503 | Mutex::Autolock al(mTorchStatusMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 504 | mTorchStatusMap.removeItem(cameraId); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | { |
| 508 | Mutex::Autolock lock(mCameraStatesLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 509 | mCameraStates.erase(cameraId); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 513 | void CameraService::onDeviceStatusChanged(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 514 | CameraDeviceStatus newHalStatus) { |
| 515 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 516 | cameraId.c_str(), eToI(newHalStatus)); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 517 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 518 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 519 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 520 | std::shared_ptr<CameraState> state = getCameraState(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 521 | |
| 522 | if (state == nullptr) { |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 523 | if (newStatus == StatusInternal::PRESENT) { |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 524 | ALOGI("%s: Unknown camera ID %s, a new camera is added", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 525 | __FUNCTION__, cameraId.c_str()); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 526 | |
| 527 | // First add as absent to make sure clients are notified below |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 528 | addStates(cameraId); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 529 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 530 | updateStatus(newStatus, cameraId); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 531 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 532 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, cameraId.c_str()); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 533 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 534 | return; |
| 535 | } |
| 536 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 537 | StatusInternal oldStatus = state->getStatus(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 538 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 539 | if (oldStatus == newStatus) { |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 540 | ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, |
| 541 | eToI(newStatus)); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 542 | return; |
| 543 | } |
| 544 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 545 | if (newStatus == StatusInternal::NOT_PRESENT) { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 546 | logDeviceRemoved(cameraId, fmt::format("Device status changed from {} to {}", |
| 547 | oldStatus, newStatus)); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 548 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 549 | // to this device until the status changes |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 550 | updateStatus(StatusInternal::NOT_PRESENT, cameraId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 551 | mVirtualDeviceCameraIdMapper.removeCamera(cameraId); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 552 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 553 | sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 554 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 555 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 556 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 557 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 558 | // Remove cached shim parameters |
| 559 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 560 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 561 | // Remove online as well as offline client from the list of active clients, |
| 562 | // if they are present |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 563 | clientToDisconnectOnline = removeClientLocked(cameraId); |
| 564 | clientToDisconnectOffline = removeClientLocked(kOfflineDevice + cameraId); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 565 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 566 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 567 | disconnectClient(cameraId, clientToDisconnectOnline); |
| 568 | disconnectClient(kOfflineDevice + cameraId, clientToDisconnectOffline); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 569 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 570 | removeStates(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 571 | } else { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 572 | if (oldStatus == StatusInternal::NOT_PRESENT) { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 573 | logDeviceAdded(cameraId, fmt::format("Device status changed from {} to {}", |
| 574 | oldStatus, newStatus)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 575 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 576 | updateStatus(newStatus, cameraId); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 577 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 578 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 579 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 580 | void CameraService::onDeviceStatusChanged(const std::string& id, |
| 581 | const std::string& physicalId, |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 582 | CameraDeviceStatus newHalStatus) { |
| 583 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 584 | __FUNCTION__, id.c_str(), physicalId.c_str(), eToI(newHalStatus)); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 585 | |
| 586 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 587 | |
| 588 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 589 | |
| 590 | if (state == nullptr) { |
| 591 | 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] | 592 | __FUNCTION__, physicalId.c_str(), id.c_str()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 593 | return; |
| 594 | } |
| 595 | |
| 596 | StatusInternal logicalCameraStatus = state->getStatus(); |
| 597 | if (logicalCameraStatus != StatusInternal::PRESENT && |
| 598 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 599 | ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d", |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 600 | __FUNCTION__, physicalId.c_str(), eToI(newHalStatus), eToI(logicalCameraStatus)); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 601 | return; |
| 602 | } |
| 603 | |
| 604 | bool updated = false; |
| 605 | if (newStatus == StatusInternal::PRESENT) { |
| 606 | updated = state->removeUnavailablePhysicalId(physicalId); |
| 607 | } else { |
| 608 | updated = state->addUnavailablePhysicalId(physicalId); |
| 609 | } |
| 610 | |
| 611 | if (updated) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 612 | std::string idCombo = id + " : " + physicalId; |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 613 | if (newStatus == StatusInternal::PRESENT) { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 614 | logDeviceAdded(idCombo, fmt::format("Device status changed to {}", newStatus)); |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 615 | } else { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 616 | logDeviceRemoved(idCombo, fmt::format("Device status changed to {}", newStatus)); |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 617 | } |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 618 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 619 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 620 | if (getSystemCameraKind(id, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 621 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 622 | return; |
| 623 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 624 | Mutex::Autolock lock(mStatusListenerLock); |
| 625 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 626 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 627 | listener->getListenerPid(), listener->getListenerUid())) { |
| 628 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 629 | id.c_str()); |
| 630 | continue; |
| 631 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 632 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged( |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 633 | mapToInterface(newStatus), id, physicalId, kDefaultDeviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 634 | listener->handleBinderStatus(ret, |
| 635 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 636 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 637 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 638 | } |
| 639 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 642 | void CameraService::disconnectClient(const std::string& id, sp<BasicClient> clientToDisconnect) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 643 | if (clientToDisconnect.get() != nullptr) { |
| 644 | 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] | 645 | __FUNCTION__, id.c_str()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 646 | // Notify the client of disconnection |
| 647 | clientToDisconnect->notifyError( |
| 648 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 649 | CaptureResultExtras{}); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 650 | clientToDisconnect->disconnect(); |
| 651 | } |
| 652 | } |
| 653 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 654 | void CameraService::onTorchStatusChanged(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 655 | TorchModeStatus newStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 656 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 657 | status_t res = getSystemCameraKind(cameraId, &systemCameraKind); |
| 658 | if (res != OK) { |
| 659 | 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] | 660 | cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 661 | return; |
| 662 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 663 | Mutex::Autolock al(mTorchStatusMutex); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 664 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 665 | } |
| 666 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 667 | void CameraService::onTorchStatusChanged(const std::string& cameraId, |
Jayant Chowdhary | 46ef0f5 | 2021-10-05 14:36:13 -0700 | [diff] [blame] | 668 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
| 669 | Mutex::Autolock al(mTorchStatusMutex); |
| 670 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
| 671 | } |
| 672 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 673 | void CameraService::broadcastTorchStrengthLevel(const std::string& cameraId, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 674 | int32_t newStrengthLevel) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 675 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 676 | auto [deviceId, mappedCameraId] = |
| 677 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 678 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 679 | Mutex::Autolock lock(mStatusListenerLock); |
| 680 | for (auto& i : mListenerList) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 681 | auto ret = i->getListener()->onTorchStrengthLevelChanged(mappedCameraId, |
| 682 | newStrengthLevel, deviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 683 | i->handleBinderStatus(ret, |
| 684 | "%s: Failed to trigger onTorchStrengthLevelChanged for %d:%d: %d", __FUNCTION__, |
| 685 | i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 689 | void CameraService::onTorchStatusChangedLocked(const std::string& cameraId, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 690 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 691 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 692 | __FUNCTION__, cameraId.c_str(), eToI(newStatus)); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 693 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 694 | TorchModeStatus status; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 695 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 696 | if (res) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 697 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 698 | __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 699 | return; |
| 700 | } |
| 701 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 702 | return; |
| 703 | } |
| 704 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 705 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 706 | if (res) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 707 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, |
| 708 | (uint32_t)newStatus, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 709 | return; |
| 710 | } |
| 711 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 712 | { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 713 | // Update battery life logging for flashlight |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 714 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 715 | auto iter = mTorchUidMap.find(cameraId); |
| 716 | if (iter != mTorchUidMap.end()) { |
| 717 | int oldUid = iter->second.second; |
| 718 | int newUid = iter->second.first; |
| 719 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 720 | if (oldUid != newUid) { |
| 721 | // 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] | 722 | if (status == TorchModeStatus::AVAILABLE_ON) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 723 | notifier.noteFlashlightOff(toString8(cameraId), oldUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 724 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 725 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 726 | notifier.noteFlashlightOn(toString8(cameraId), newUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 727 | } |
| 728 | iter->second.second = newUid; |
| 729 | } else { |
| 730 | // If the UID has not changed, log the status |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 731 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 732 | notifier.noteFlashlightOn(toString8(cameraId), oldUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 733 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 734 | notifier.noteFlashlightOff(toString8(cameraId), oldUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | } |
| 738 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 739 | broadcastTorchModeStatus(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 740 | } |
| 741 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 742 | bool CameraService::isAutomotiveExteriorSystemCamera(const std::string& cam_id) const { |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 743 | // Returns false if this is not an automotive device type. |
| 744 | if (!isAutomotiveDevice()) |
| 745 | return false; |
| 746 | |
| 747 | // Returns false if no camera id is provided. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 748 | if (cam_id.empty()) |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 749 | return false; |
| 750 | |
| 751 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 752 | if (getSystemCameraKind(cam_id, &systemCameraKind) != OK) { |
| 753 | // This isn't a known camera ID, so it's not a system camera. |
| 754 | ALOGE("%s: Unknown camera id %s, ", __FUNCTION__, cam_id.c_str()); |
| 755 | return false; |
| 756 | } |
| 757 | |
| 758 | if (systemCameraKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
| 759 | ALOGE("%s: camera id %s is not a system camera", __FUNCTION__, cam_id.c_str()); |
| 760 | return false; |
| 761 | } |
| 762 | |
| 763 | CameraMetadata cameraInfo; |
| 764 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 765 | cam_id, false, &cameraInfo, hardware::ICameraService::ROTATION_OVERRIDE_NONE); |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 766 | if (res != OK){ |
| 767 | ALOGE("%s: Not able to get camera characteristics for camera id %s",__FUNCTION__, |
| 768 | cam_id.c_str()); |
| 769 | return false; |
| 770 | } |
| 771 | |
| 772 | camera_metadata_entry auto_location = cameraInfo.find(ANDROID_AUTOMOTIVE_LOCATION); |
| 773 | if (auto_location.count != 1) |
| 774 | return false; |
| 775 | |
| 776 | uint8_t location = auto_location.data.u8[0]; |
| 777 | if ((location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_FRONT) && |
| 778 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_REAR) && |
| 779 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_LEFT) && |
| 780 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_RIGHT)) { |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | return true; |
| 785 | } |
| 786 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 787 | Status CameraService::getNumberOfCameras(int32_t type, |
| 788 | const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 789 | int32_t* numCameras) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 790 | ATRACE_CALL(); |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 791 | if (vd_flags::camera_device_awareness() && (clientAttribution.deviceId != kDefaultDeviceId) |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 792 | && (devicePolicy != IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT)) { |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 793 | *numCameras = mVirtualDeviceCameraIdMapper.getNumberOfCameras(clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 794 | return Status::ok(); |
| 795 | } |
| 796 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 797 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 798 | bool hasSystemCameraPermissions = |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 799 | hasPermissionsForSystemCamera(std::string(), getCallingPid(), |
| 800 | getCallingUid()); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 801 | switch (type) { |
| 802 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 803 | if (hasSystemCameraPermissions) { |
| 804 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); |
| 805 | } else { |
| 806 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); |
| 807 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 808 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 809 | case CAMERA_TYPE_ALL: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 810 | if (hasSystemCameraPermissions) { |
| 811 | *numCameras = mNumberOfCameras; |
| 812 | } else { |
| 813 | *numCameras = mNumberOfCamerasWithoutSystemCamera; |
| 814 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 815 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 816 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 817 | ALOGW("%s: Unknown camera type %d", |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 818 | __FUNCTION__, type); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 819 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 820 | "Unknown camera type %d", type); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 821 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 822 | return Status::ok(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 825 | Status CameraService::createDefaultRequest(const std::string& unresolvedCameraId, int templateId, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 826 | const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 827 | /* out */ |
| 828 | hardware::camera2::impl::CameraMetadataNative* request) { |
| 829 | ATRACE_CALL(); |
| 830 | |
| 831 | if (!flags::feature_combination_query()) { |
| 832 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, |
| 833 | "Camera subsystem doesn't support this method!"); |
| 834 | } |
| 835 | if (!mInitialized) { |
| 836 | ALOGE("%s: Camera subsystem is not available", __FUNCTION__); |
| 837 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 838 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 839 | } |
| 840 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 841 | std::optional<std::string> cameraIdOptional = |
| 842 | resolveCameraId(unresolvedCameraId, clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 843 | if (!cameraIdOptional.has_value()) { |
| 844 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 845 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 846 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 847 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 848 | } |
| 849 | std::string cameraId = cameraIdOptional.value(); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 850 | |
| 851 | binder::Status res; |
| 852 | if (request == nullptr) { |
| 853 | res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, |
| 854 | "Camera %s: Error creating default request", cameraId.c_str()); |
| 855 | return res; |
| 856 | } |
| 857 | camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT; |
| 858 | res = SessionConfigurationUtils::mapRequestTemplateFromClient( |
| 859 | cameraId, templateId, &tempId); |
| 860 | if (!res.isOk()) { |
| 861 | ALOGE("%s: Camera %s: failed to map request Template %d", |
| 862 | __FUNCTION__, cameraId.c_str(), templateId); |
| 863 | return res; |
| 864 | } |
| 865 | |
| 866 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 867 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to create default" |
| 868 | "request for system only device %s: ", cameraId.c_str()); |
| 869 | } |
| 870 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 871 | CameraMetadata metadata; |
| 872 | status_t err = mCameraProviderManager->createDefaultRequest(cameraId, tempId, &metadata); |
| 873 | if (err == OK) { |
| 874 | request->swap(metadata); |
| 875 | } else if (err == BAD_VALUE) { |
| 876 | res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT, |
| 877 | "Camera %s: Template ID %d is invalid or not supported: %s (%d)", |
| 878 | cameraId.c_str(), templateId, strerror(-err), err); |
| 879 | } else { |
| 880 | res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, |
| 881 | "Camera %s: Error creating default request for template %d: %s (%d)", |
| 882 | cameraId.c_str(), templateId, strerror(-err), err); |
| 883 | } |
| 884 | return res; |
| 885 | } |
| 886 | |
| 887 | Status CameraService::isSessionConfigurationWithParametersSupported( |
Avichal Rakesh | caf179b | 2024-04-04 18:42:46 -0700 | [diff] [blame] | 888 | const std::string& unresolvedCameraId, int targetSdkVersion, |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 889 | const SessionConfiguration& sessionConfiguration, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 890 | const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
Avichal Rakesh | caf179b | 2024-04-04 18:42:46 -0700 | [diff] [blame] | 891 | /*out*/ bool* supported) { |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 892 | ATRACE_CALL(); |
| 893 | |
| 894 | if (!flags::feature_combination_query()) { |
| 895 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, |
| 896 | "Camera subsystem doesn't support this method!"); |
| 897 | } |
| 898 | if (!mInitialized) { |
| 899 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 900 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 901 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 902 | } |
| 903 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 904 | std::optional<std::string> cameraIdOptional = |
| 905 | resolveCameraId(unresolvedCameraId, clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 906 | if (!cameraIdOptional.has_value()) { |
| 907 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 908 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 909 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 910 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 911 | } |
| 912 | std::string cameraId = cameraIdOptional.value(); |
| 913 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 914 | if (supported == nullptr) { |
| 915 | std::string msg = fmt::sprintf("Camera %s: Invalid 'support' input!", |
| 916 | unresolvedCameraId.c_str()); |
| 917 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 918 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 919 | } |
| 920 | |
| 921 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 922 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query " |
| 923 | "session configuration with parameters support for system only device %s: ", |
| 924 | cameraId.c_str()); |
| 925 | } |
| 926 | |
Avichal Rakesh | caf179b | 2024-04-04 18:42:46 -0700 | [diff] [blame] | 927 | bool overrideForPerfClass = flags::calculate_perf_override_during_session_support() && |
| 928 | SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
| 929 | mPerfClassPrimaryCameraIds, cameraId, targetSdkVersion); |
| 930 | |
Shuzhen Wang | e3e8e73 | 2024-05-22 17:48:01 -0700 | [diff] [blame] | 931 | auto ret = isSessionConfigurationWithParametersSupportedUnsafe(cameraId, |
| 932 | sessionConfiguration, overrideForPerfClass, supported); |
| 933 | if (flags::analytics_24q3()) { |
| 934 | mCameraServiceProxyWrapper->logFeatureCombinationQuery(cameraId, |
| 935 | getCallingUid(), sessionConfiguration, ret); |
| 936 | } |
| 937 | return ret; |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | Status CameraService::isSessionConfigurationWithParametersSupportedUnsafe( |
| 941 | const std::string& cameraId, const SessionConfiguration& sessionConfiguration, |
| 942 | bool overrideForPerfClass, /*out*/ bool* supported) { |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 943 | *supported = false; |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 944 | status_t ret = mCameraProviderManager->isSessionConfigurationSupported( |
| 945 | cameraId, sessionConfiguration, overrideForPerfClass, |
| 946 | /*checkSessionParams=*/true, supported); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 947 | binder::Status res; |
| 948 | switch (ret) { |
| 949 | case OK: |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 950 | // Expected. Do Nothing. |
| 951 | return Status::ok(); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 952 | case INVALID_OPERATION: { |
| 953 | std::string msg = fmt::sprintf( |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 954 | "Camera %s: Session configuration with parameters supported query not " |
| 955 | "supported!", |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 956 | cameraId.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 957 | ALOGW("%s: %s", __FUNCTION__, msg.c_str()); |
| 958 | logServiceError(msg, CameraService::ERROR_INVALID_OPERATION); |
| 959 | *supported = false; |
| 960 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 961 | } |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 962 | break; |
| 963 | case NAME_NOT_FOUND: { |
| 964 | std::string msg = fmt::sprintf("Camera %s: Unknown camera ID.", cameraId.c_str()); |
| 965 | ALOGW("%s: %s", __FUNCTION__, msg.c_str()); |
| 966 | logServiceError(msg, CameraService::ERROR_ILLEGAL_ARGUMENT); |
| 967 | *supported = false; |
| 968 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 969 | } |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 970 | break; |
| 971 | default: { |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 972 | std::string msg = fmt::sprintf( |
| 973 | "Unable to retrieve session configuration support for camera " |
| 974 | "device %s: Error: %s (%d)", |
| 975 | cameraId.c_str(), strerror(-ret), ret); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 976 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 977 | logServiceError(msg, CameraService::ERROR_ILLEGAL_ARGUMENT); |
| 978 | *supported = false; |
| 979 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 980 | } |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 981 | break; |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 982 | } |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 985 | Status CameraService::getSessionCharacteristics(const std::string& unresolvedCameraId, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 986 | int targetSdkVersion, int rotationOverride, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 987 | const SessionConfiguration& sessionConfiguration, |
| 988 | const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 989 | /*out*/ CameraMetadata* outMetadata) { |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 990 | ATRACE_CALL(); |
| 991 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 992 | if (outMetadata == nullptr) { |
| 993 | std::string msg = |
| 994 | fmt::sprintf("Camera %s: Invalid 'outMetadata' input!", unresolvedCameraId.c_str()); |
| 995 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 996 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 997 | } |
| 998 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 999 | if (!mInitialized) { |
| 1000 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 1001 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 1002 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 1003 | } |
| 1004 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1005 | std::optional<std::string> cameraIdOptional = |
| 1006 | resolveCameraId(unresolvedCameraId, clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1007 | if (!cameraIdOptional.has_value()) { |
| 1008 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1009 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1010 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1011 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1012 | } |
| 1013 | std::string cameraId = cameraIdOptional.value(); |
| 1014 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1015 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1016 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1017 | "Unable to retrieve camera" |
| 1018 | "characteristics for system only device %s: ", |
| 1019 | cameraId.c_str()); |
| 1020 | } |
| 1021 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1022 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
| 1023 | mPerfClassPrimaryCameraIds, cameraId, targetSdkVersion); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 1024 | if (flags::check_session_support_before_session_char()) { |
| 1025 | bool sessionConfigSupported; |
| 1026 | Status res = isSessionConfigurationWithParametersSupportedUnsafe( |
| 1027 | cameraId, sessionConfiguration, overrideForPerfClass, &sessionConfigSupported); |
| 1028 | if (!res.isOk()) { |
| 1029 | // isSessionConfigurationWithParametersSupportedUnsafe should log what went wrong and |
| 1030 | // report the correct Status to send to the client. Simply forward the error to |
| 1031 | // the client. |
| 1032 | outMetadata->clear(); |
| 1033 | return res; |
| 1034 | } |
| 1035 | if (!sessionConfigSupported) { |
| 1036 | std::string msg = fmt::sprintf( |
| 1037 | "Session configuration not supported for camera device %s.", cameraId.c_str()); |
| 1038 | outMetadata->clear(); |
| 1039 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1040 | } |
| 1041 | } |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1042 | |
| 1043 | status_t ret = mCameraProviderManager->getSessionCharacteristics( |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1044 | cameraId, sessionConfiguration, overrideForPerfClass, rotationOverride, outMetadata); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1045 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1046 | switch (ret) { |
| 1047 | case OK: |
| 1048 | // Expected, no handling needed. |
| 1049 | break; |
| 1050 | case INVALID_OPERATION: { |
| 1051 | std::string msg = fmt::sprintf( |
| 1052 | "Camera %s: Session characteristics query not supported!", |
| 1053 | cameraId.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 1054 | ALOGW("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1055 | logServiceError(msg, CameraService::ERROR_INVALID_OPERATION); |
| 1056 | outMetadata->clear(); |
| 1057 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
| 1058 | } |
| 1059 | break; |
| 1060 | case NAME_NOT_FOUND: { |
| 1061 | std::string msg = fmt::sprintf( |
| 1062 | "Camera %s: Unknown camera ID.", |
| 1063 | cameraId.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 1064 | ALOGW("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1065 | logServiceError(msg, CameraService::ERROR_ILLEGAL_ARGUMENT); |
| 1066 | outMetadata->clear(); |
| 1067 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1068 | } |
| 1069 | break; |
| 1070 | default: { |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1071 | std::string msg = fmt::sprintf( |
| 1072 | "Unable to retrieve session characteristics for camera device %s: " |
| 1073 | "Error: %s (%d)", |
| 1074 | cameraId.c_str(), strerror(-ret), ret); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1075 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1076 | logServiceError(msg, CameraService::ERROR_INVALID_OPERATION); |
| 1077 | outMetadata->clear(); |
| 1078 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
Shuzhen Wang | e3e8e73 | 2024-05-22 17:48:01 -0700 | [diff] [blame] | 1082 | Status res = filterSensitiveMetadataIfNeeded(cameraId, outMetadata); |
| 1083 | if (flags::analytics_24q3()) { |
| 1084 | mCameraServiceProxyWrapper->logSessionCharacteristicsQuery(cameraId, |
| 1085 | getCallingUid(), sessionConfiguration, res); |
| 1086 | } |
| 1087 | return res; |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | Status CameraService::filterSensitiveMetadataIfNeeded( |
| 1091 | const std::string& cameraId, CameraMetadata* metadata) { |
| 1092 | int callingPid = getCallingPid(); |
| 1093 | int callingUid = getCallingUid(); |
| 1094 | |
| 1095 | if (callingPid == getpid()) { |
| 1096 | // Caller is cameraserver; no need to remove keys |
| 1097 | return Status::ok(); |
| 1098 | } |
| 1099 | |
| 1100 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1101 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 1102 | ALOGE("%s: Couldn't get camera kind for camera id %s", __FUNCTION__, cameraId.c_str()); |
| 1103 | metadata->clear(); |
| 1104 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1105 | "Unable to retrieve camera kind for device %s", cameraId.c_str()); |
| 1106 | } |
| 1107 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
| 1108 | // Attempting to query system only camera without system camera permission would have |
| 1109 | // failed the shouldRejectSystemCameraConnection in the caller. So if we get here |
| 1110 | // for a system only camera, then the caller has the required permission. |
| 1111 | // No need to remove keys |
| 1112 | return Status::ok(); |
| 1113 | } |
| 1114 | |
| 1115 | std::vector<int32_t> tagsRemoved; |
Biswarup Pal | e506e73 | 2024-03-27 13:46:20 +0000 | [diff] [blame] | 1116 | // Get the device id that owns this camera. |
| 1117 | auto [cameraOwnerDeviceId, _] = mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair( |
| 1118 | cameraId); |
| 1119 | bool hasCameraPermission = hasPermissionsForCamera(cameraId, callingPid, callingUid, |
| 1120 | cameraOwnerDeviceId); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1121 | if (hasCameraPermission) { |
| 1122 | // Caller has camera permission; no need to remove keys |
| 1123 | return Status::ok(); |
| 1124 | } |
| 1125 | |
| 1126 | status_t ret = metadata->removePermissionEntries( |
| 1127 | mCameraProviderManager->getProviderTagIdLocked(cameraId), &tagsRemoved); |
| 1128 | if (ret != OK) { |
| 1129 | metadata->clear(); |
| 1130 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1131 | "Failed to remove camera characteristics needing camera permission " |
| 1132 | "for device %s:%s (%d)", |
| 1133 | cameraId.c_str(), strerror(-ret), ret); |
| 1134 | } |
| 1135 | |
| 1136 | if (!tagsRemoved.empty()) { |
| 1137 | ret = metadata->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, |
| 1138 | tagsRemoved.data(), tagsRemoved.size()); |
| 1139 | if (ret != OK) { |
| 1140 | metadata->clear(); |
| 1141 | return STATUS_ERROR_FMT( |
| 1142 | ERROR_INVALID_OPERATION, |
| 1143 | "Failed to insert camera keys needing permission for device %s: %s (%d)", |
| 1144 | cameraId.c_str(), strerror(-ret), ret); |
| 1145 | } |
| 1146 | } |
| 1147 | return Status::ok(); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1150 | Status CameraService::injectSessionParams( |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1151 | const std::string& cameraId, |
| 1152 | const CameraMetadata& sessionParams) { |
| 1153 | if (!checkCallingPermission(toString16(sCameraInjectExternalCameraPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1154 | const int pid = getCallingPid(); |
| 1155 | const int uid = getCallingUid(); |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1156 | ALOGE("%s: Permission Denial: can't inject session params pid=%d, uid=%d", |
| 1157 | __FUNCTION__, pid, uid); |
| 1158 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 1159 | "Permission Denial: no permission to inject session params"); |
| 1160 | } |
| 1161 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1162 | // Do not allow session params injection for a virtual camera. |
| 1163 | auto [deviceId, _] = mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 1164 | if (deviceId != kDefaultDeviceId) { |
| 1165 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1166 | "Cannot inject session params for a virtual camera"); |
| 1167 | } |
| 1168 | |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1169 | std::unique_ptr<AutoConditionLock> serviceLockWrapper = |
| 1170 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 1171 | |
| 1172 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 1173 | if (clientDescriptor == nullptr) { |
| 1174 | ALOGI("%s: No active client for camera id %s", __FUNCTION__, cameraId.c_str()); |
| 1175 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1176 | "No active client for camera id %s", cameraId.c_str()); |
| 1177 | } |
| 1178 | |
| 1179 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1180 | status_t res = clientSp->injectSessionParams(sessionParams); |
| 1181 | |
| 1182 | if (res != OK) { |
| 1183 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1184 | "Error injecting session params into camera \"%s\": %s (%d)", |
| 1185 | cameraId.c_str(), strerror(-res), res); |
| 1186 | } |
| 1187 | return Status::ok(); |
| 1188 | } |
| 1189 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1190 | std::optional<std::string> CameraService::resolveCameraId( |
| 1191 | const std::string& inputCameraId, |
| 1192 | int32_t deviceId, |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 1193 | int32_t devicePolicy) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1194 | if ((deviceId == kDefaultDeviceId) |
| 1195 | || (devicePolicy == IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT)) { |
| 1196 | auto [storedDeviceId, _] = |
| 1197 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(inputCameraId); |
| 1198 | if (storedDeviceId != kDefaultDeviceId) { |
| 1199 | // Trying to access a virtual camera from default-policy device context, we should fail. |
| 1200 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 1201 | inputCameraId.c_str(), deviceId); |
| 1202 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1203 | return std::nullopt; |
| 1204 | } |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 1205 | return inputCameraId; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | return mVirtualDeviceCameraIdMapper.getActualCameraId(deviceId, inputCameraId); |
| 1209 | } |
| 1210 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1211 | Status CameraService::getCameraInfo(int cameraId, int rotationOverride, |
| 1212 | const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
| 1213 | CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1214 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 1215 | Mutex::Autolock l(mServiceLock); |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1216 | std::string cameraIdStr = |
| 1217 | cameraIdIntToStrLocked(cameraId, clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1218 | if (cameraIdStr.empty()) { |
| 1219 | std::string msg = fmt::sprintf("Camera %d: Invalid camera id for device id %d", |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1220 | cameraId, clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1221 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1222 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1223 | } |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1224 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1225 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1226 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 1227 | "characteristics for system only device %s: ", cameraIdStr.c_str()); |
| 1228 | } |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 1229 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1230 | if (!mInitialized) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1231 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1232 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1233 | "Camera subsystem is not available"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1234 | } |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1235 | bool hasSystemCameraPermissions = hasPermissionsForSystemCamera(std::to_string(cameraId), |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1236 | getCallingPid(), getCallingUid()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1237 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; |
| 1238 | if (hasSystemCameraPermissions) { |
| 1239 | cameraIdBound = mNumberOfCameras; |
| 1240 | } |
| 1241 | if (cameraId < 0 || cameraId >= cameraIdBound) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1242 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1243 | "CameraId is not valid"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1246 | Status ret = Status::ok(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1247 | int portraitRotation; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1248 | status_t err = mCameraProviderManager->getCameraInfo( |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1249 | cameraIdStr, rotationOverride, &portraitRotation, cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1250 | if (err != OK) { |
| 1251 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1252 | "Error retrieving camera info from device %d: %s (%d)", cameraId, |
| 1253 | strerror(-err), err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1254 | logServiceError(std::string("Error retrieving camera info from device ") |
| 1255 | + std::to_string(cameraId), ERROR_INVALID_OPERATION); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1256 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1257 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1258 | return ret; |
| 1259 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1260 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1261 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt, |
| 1262 | int32_t deviceId, int32_t devicePolicy) { |
| 1263 | if (vd_flags::camera_device_awareness() && (deviceId != kDefaultDeviceId) |
| 1264 | && (devicePolicy != IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT)) { |
| 1265 | std::optional<std::string> cameraIdOptional = |
| 1266 | mVirtualDeviceCameraIdMapper.getActualCameraId(cameraIdInt, deviceId); |
| 1267 | return cameraIdOptional.has_value() ? cameraIdOptional.value() : std::string{}; |
| 1268 | } |
| 1269 | |
| 1270 | const std::vector<std::string> *cameraIds = &mNormalDeviceIdsWithoutSystemCamera; |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1271 | auto callingPid = getCallingPid(); |
| 1272 | auto callingUid = getCallingUid(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1273 | bool systemCameraPermissions = hasPermissionsForSystemCamera(std::to_string(cameraIdInt), |
| 1274 | callingPid, callingUid, /* checkCameraPermissions= */ false); |
| 1275 | if (systemCameraPermissions || getpid() == callingPid) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1276 | cameraIds = &mNormalDeviceIds; |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1277 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1278 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(cameraIds->size())) { |
| 1279 | ALOGE("%s: input id %d invalid: valid range (0, %zu)", |
| 1280 | __FUNCTION__, cameraIdInt, cameraIds->size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1281 | return std::string{}; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1282 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1283 | |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 1284 | return (*cameraIds)[cameraIdInt]; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1287 | std::string CameraService::cameraIdIntToStr(int cameraIdInt, int32_t deviceId, |
| 1288 | int32_t devicePolicy) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1289 | Mutex::Autolock lock(mServiceLock); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1290 | return cameraIdIntToStrLocked(cameraIdInt, deviceId, devicePolicy); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1293 | Status CameraService::getCameraCharacteristics(const std::string& unresolvedCameraId, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1294 | int targetSdkVersion, int rotationOverride, const AttributionSourceState& clientAttribution, |
| 1295 | int32_t devicePolicy, CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1296 | ATRACE_CALL(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1297 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1298 | if (!cameraInfo) { |
| 1299 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1300 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1303 | if (!mInitialized) { |
| 1304 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1305 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1306 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1307 | "Camera subsystem is not available");; |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1310 | std::optional<std::string> cameraIdOptional = |
| 1311 | resolveCameraId(unresolvedCameraId, clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1312 | if (!cameraIdOptional.has_value()) { |
| 1313 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1314 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1315 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1316 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1317 | } |
| 1318 | std::string cameraId = cameraIdOptional.value(); |
| 1319 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1320 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1321 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1322 | "characteristics for system only device %s: ", cameraId.c_str()); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1325 | bool overrideForPerfClass = |
| 1326 | SessionConfigurationUtils::targetPerfClassPrimaryCamera(mPerfClassPrimaryCameraIds, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1327 | cameraId, targetSdkVersion); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1328 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1329 | cameraId, overrideForPerfClass, cameraInfo, rotationOverride); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1330 | if (res != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1331 | if (res == NAME_NOT_FOUND) { |
| 1332 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1333 | "characteristics for unknown device %s: %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1334 | strerror(-res), res); |
| 1335 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1336 | logServiceError(fmt::sprintf("Unable to retrieve camera characteristics for device %s.", |
| 1337 | cameraId.c_str()), ERROR_INVALID_OPERATION); |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1338 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1339 | "characteristics for device %s: %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1340 | strerror(-res), res); |
| 1341 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1342 | } |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 1343 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1344 | return filterSensitiveMetadataIfNeeded(cameraId, cameraInfo); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1347 | Status CameraService::getTorchStrengthLevel(const std::string& unresolvedCameraId, |
| 1348 | const AttributionSourceState& clientAttribution, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1349 | int32_t devicePolicy, int32_t* torchStrength) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1350 | ATRACE_CALL(); |
| 1351 | Mutex::Autolock l(mServiceLock); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1352 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1353 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, |
| 1354 | clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1355 | if (!cameraIdOptional.has_value()) { |
| 1356 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1357 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1358 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1359 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1360 | } |
| 1361 | std::string cameraId = cameraIdOptional.value(); |
| 1362 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1363 | if (!mInitialized) { |
| 1364 | ALOGE("%s: Camera HAL couldn't be initialized.", __FUNCTION__); |
| 1365 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera HAL couldn't be initialized."); |
| 1366 | } |
| 1367 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1368 | if (torchStrength == NULL) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1369 | ALOGE("%s: strength level must not be null.", __FUNCTION__); |
| 1370 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Strength level should not be null."); |
| 1371 | } |
| 1372 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1373 | status_t res = mCameraProviderManager->getTorchStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1374 | if (res != OK) { |
| 1375 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve torch " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1376 | "strength level for device %s: %s (%d)", cameraId.c_str(), |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1377 | strerror(-res), res); |
| 1378 | } |
| 1379 | ALOGI("%s: Torch strength level is: %d", __FUNCTION__, *torchStrength); |
| 1380 | return Status::ok(); |
| 1381 | } |
| 1382 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1383 | std::string CameraService::getFormattedCurrentTime() { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1384 | time_t now = time(nullptr); |
| 1385 | char formattedTime[64]; |
| 1386 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1387 | return std::string(formattedTime); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1388 | } |
| 1389 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1390 | Status CameraService::getCameraVendorTagDescriptor( |
| 1391 | /*out*/ |
| 1392 | hardware::camera2::params::VendorTagDescriptor* desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1393 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1394 | if (!mInitialized) { |
| 1395 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1396 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 1397 | } |
Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 1398 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 1399 | if (globalDescriptor != nullptr) { |
| 1400 | *desc = *(globalDescriptor.get()); |
| 1401 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1402 | return Status::ok(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 1403 | } |
| 1404 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 1405 | Status CameraService::getCameraVendorTagCache( |
| 1406 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { |
| 1407 | ATRACE_CALL(); |
| 1408 | if (!mInitialized) { |
| 1409 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 1410 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1411 | "Camera subsystem not available"); |
| 1412 | } |
| 1413 | sp<VendorTagDescriptorCache> globalCache = |
| 1414 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 1415 | if (globalCache != nullptr) { |
| 1416 | *cache = *(globalCache.get()); |
| 1417 | } |
| 1418 | return Status::ok(); |
| 1419 | } |
| 1420 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 1421 | void CameraService::clearCachedVariables() { |
| 1422 | BasicClient::BasicClient::sCameraService = nullptr; |
| 1423 | } |
| 1424 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1425 | std::pair<int, IPCTransport> CameraService::getDeviceVersion(const std::string& cameraId, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1426 | int rotationOverride, int* portraitRotation, int* facing, |
| 1427 | int* orientation) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1428 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1429 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1430 | int deviceVersion = 0; |
| 1431 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1432 | status_t res; |
| 1433 | hardware::hidl_version maxVersion{0,0}; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1434 | IPCTransport transport = IPCTransport::INVALID; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1435 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId, &maxVersion, &transport); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1436 | if (res != OK || transport == IPCTransport::INVALID) { |
| 1437 | 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] | 1438 | cameraId.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1439 | return std::make_pair(-1, IPCTransport::INVALID) ; |
| 1440 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1441 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1442 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1443 | hardware::CameraInfo info; |
| 1444 | if (facing) { |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1445 | res = mCameraProviderManager->getCameraInfo(cameraId, rotationOverride, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1446 | portraitRotation, &info); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1447 | if (res != OK) { |
| 1448 | return std::make_pair(-1, IPCTransport::INVALID); |
| 1449 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1450 | *facing = info.facing; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1451 | if (orientation) { |
| 1452 | *orientation = info.orientation; |
| 1453 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1454 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1455 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1456 | return std::make_pair(deviceVersion, transport); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1457 | } |
| 1458 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1459 | Status CameraService::filterGetInfoErrorCode(status_t err) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1460 | switch(err) { |
| 1461 | case NO_ERROR: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1462 | return Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1463 | case BAD_VALUE: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1464 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1465 | "CameraId is not valid for HAL module"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1466 | case NO_INIT: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1467 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1468 | "Camera device not available"); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1469 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1470 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1471 | "Camera HAL encountered error %d: %s", |
| 1472 | err, strerror(-err)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1473 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1474 | } |
| 1475 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1476 | Status CameraService::makeClient(const sp<CameraService>& cameraService, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1477 | const sp<IInterface>& cameraCb, const std::string& packageName, bool systemNativeClient, |
| 1478 | const std::optional<std::string>& featureId, const std::string& cameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 1479 | int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid, |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1480 | int servicePid, std::pair<int, IPCTransport> deviceVersionAndTransport, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1481 | apiLevel effectiveApiLevel, bool overrideForPerfClass, int rotationOverride, |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 1482 | bool forceSlowJpegMode, const std::string& originalCameraId, |
| 1483 | /*out*/sp<BasicClient>* client) { |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1484 | // For HIDL devices |
| 1485 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 1486 | // Create CameraClient based on device version reported by the HAL. |
| 1487 | int deviceVersion = deviceVersionAndTransport.first; |
| 1488 | switch(deviceVersion) { |
| 1489 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 1490 | ALOGE("Camera using old HAL version: %d", deviceVersion); |
| 1491 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, |
| 1492 | "Camera device \"%s\" HAL version %d no longer supported", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1493 | cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1494 | break; |
| 1495 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 1496 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 1497 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 1498 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 1499 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 1500 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 1501 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 1502 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 1503 | break; |
| 1504 | default: |
| 1505 | // Should not be reachable |
| 1506 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 1507 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1508 | "Camera device \"%s\" has unknown HAL version %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1509 | cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 1513 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1514 | *client = new Camera2Client(cameraService, tmp, cameraService->mCameraServiceProxyWrapper, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1515 | cameraService->mAttributionAndPermissionUtils, packageName, featureId, cameraId, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1516 | api1CameraId, facing, sensorOrientation, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1517 | clientPid, clientUid, servicePid, overrideForPerfClass, rotationOverride, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1518 | forceSlowJpegMode); |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1519 | ALOGI("%s: Camera1 API (legacy), rotationOverride %d, forceSlowJpegMode %d", |
| 1520 | __FUNCTION__, rotationOverride, forceSlowJpegMode); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1521 | } else { // Camera2 API route |
| 1522 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = |
| 1523 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1524 | *client = new CameraDeviceClient(cameraService, tmp, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1525 | cameraService->mCameraServiceProxyWrapper, |
| 1526 | cameraService->mAttributionAndPermissionUtils, packageName, systemNativeClient, |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1527 | featureId, cameraId, facing, sensorOrientation, clientPid, clientUid, servicePid, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1528 | overrideForPerfClass, rotationOverride, originalCameraId); |
| 1529 | ALOGI("%s: Camera2 API, rotationOverride %d", __FUNCTION__, rotationOverride); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1530 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1531 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1532 | } |
| 1533 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1534 | std::string CameraService::toString(std::set<userid_t> intSet) { |
| 1535 | std::ostringstream s; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1536 | bool first = true; |
| 1537 | for (userid_t i : intSet) { |
| 1538 | if (first) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1539 | s << std::to_string(i); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1540 | first = false; |
| 1541 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1542 | s << ", " << std::to_string(i); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1543 | } |
| 1544 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1545 | return std::move(s.str()); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1548 | int32_t CameraService::mapToInterface(TorchModeStatus status) { |
| 1549 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1550 | switch (status) { |
| 1551 | case TorchModeStatus::NOT_AVAILABLE: |
| 1552 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1553 | break; |
| 1554 | case TorchModeStatus::AVAILABLE_OFF: |
| 1555 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 1556 | break; |
| 1557 | case TorchModeStatus::AVAILABLE_ON: |
| 1558 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
| 1559 | break; |
| 1560 | default: |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 1561 | ALOGW("Unknown new flash status: %d", eToI(status)); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1562 | } |
| 1563 | return serviceStatus; |
| 1564 | } |
| 1565 | |
| 1566 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { |
| 1567 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; |
| 1568 | switch (status) { |
| 1569 | case CameraDeviceStatus::NOT_PRESENT: |
| 1570 | serviceStatus = StatusInternal::NOT_PRESENT; |
| 1571 | break; |
| 1572 | case CameraDeviceStatus::PRESENT: |
| 1573 | serviceStatus = StatusInternal::PRESENT; |
| 1574 | break; |
| 1575 | case CameraDeviceStatus::ENUMERATING: |
| 1576 | serviceStatus = StatusInternal::ENUMERATING; |
| 1577 | break; |
| 1578 | default: |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 1579 | ALOGW("Unknown new HAL device status: %d", eToI(status)); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1580 | } |
| 1581 | return serviceStatus; |
| 1582 | } |
| 1583 | |
| 1584 | int32_t CameraService::mapToInterface(StatusInternal status) { |
| 1585 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1586 | switch (status) { |
| 1587 | case StatusInternal::NOT_PRESENT: |
| 1588 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1589 | break; |
| 1590 | case StatusInternal::PRESENT: |
| 1591 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; |
| 1592 | break; |
| 1593 | case StatusInternal::ENUMERATING: |
| 1594 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; |
| 1595 | break; |
| 1596 | case StatusInternal::NOT_AVAILABLE: |
| 1597 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; |
| 1598 | break; |
| 1599 | case StatusInternal::UNKNOWN: |
| 1600 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; |
| 1601 | break; |
| 1602 | default: |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 1603 | ALOGW("Unknown new internal device status: %d", eToI(status)); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1604 | } |
| 1605 | return serviceStatus; |
| 1606 | } |
| 1607 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1608 | Status CameraService::initializeShimMetadata(int cameraId) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1609 | int uid = getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1610 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1611 | std::string cameraIdStr = std::to_string(cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1612 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1613 | sp<Client> tmp = nullptr; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1614 | if (!(ret = connectHelper<ICameraClient,Client>( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1615 | sp<ICameraClient>{nullptr}, cameraIdStr, cameraId, |
| 1616 | kServiceName, /*systemNativeClient*/ false, {}, uid, USE_CALLING_PID, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1617 | API_1, /*shimUpdateOnly*/ true, /*oomScoreOffset*/ 0, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1618 | /*targetSdkVersion*/ __ANDROID_API_FUTURE__, |
| 1619 | /*rotationOverride*/hardware::ICameraService::ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1620 | /*forceSlowJpegMode*/false, cameraIdStr, /*out*/ tmp) |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1621 | ).isOk()) { |
Tomasz Wasilczyk | 12b04a5 | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 1622 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().c_str()); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1623 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1624 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1625 | } |
| 1626 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1627 | Status CameraService::getLegacyParametersLazy(int cameraId, |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1628 | /*out*/ |
| 1629 | CameraParameters* parameters) { |
| 1630 | |
| 1631 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 1632 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1633 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1634 | |
| 1635 | if (parameters == NULL) { |
| 1636 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1637 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 1640 | std::string cameraIdStr = std::to_string(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1641 | |
| 1642 | // Check if we already have parameters |
| 1643 | { |
| 1644 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1645 | Mutex::Autolock lock(mServiceLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1646 | auto cameraState = getCameraState(cameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1647 | if (cameraState == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1648 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, cameraIdStr.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1649 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1650 | "Invalid camera ID: %s", cameraIdStr.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1651 | } |
| 1652 | CameraParameters p = cameraState->getShimParams(); |
| 1653 | if (!p.isEmpty()) { |
| 1654 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1655 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1656 | } |
| 1657 | } |
| 1658 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1659 | int64_t token = clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1660 | ret = initializeShimMetadata(cameraId); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1661 | restoreCallingIdentity(token); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1662 | if (!ret.isOk()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1663 | // Error already logged by callee |
| 1664 | return ret; |
| 1665 | } |
| 1666 | |
| 1667 | // Check for parameters again |
| 1668 | { |
| 1669 | // Scope for service lock |
| 1670 | Mutex::Autolock lock(mServiceLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1671 | auto cameraState = getCameraState(cameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1672 | if (cameraState == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1673 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, cameraIdStr.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1674 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1675 | "Invalid camera ID: %s", cameraIdStr.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1676 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1677 | CameraParameters p = cameraState->getShimParams(); |
| 1678 | if (!p.isEmpty()) { |
| 1679 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1680 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1684 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 1685 | __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1686 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1689 | Status CameraService::validateConnectLocked(const std::string& cameraId, |
| 1690 | const std::string& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1691 | /*out*/int& originalClientPid) const { |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1692 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1693 | #ifdef __BRILLO__ |
| 1694 | UNUSED(clientName8); |
| 1695 | UNUSED(clientUid); |
| 1696 | UNUSED(clientPid); |
| 1697 | UNUSED(originalClientPid); |
| 1698 | #else |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1699 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, |
| 1700 | originalClientPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1701 | if (!allowed.isOk()) { |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1702 | return allowed; |
| 1703 | } |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1704 | #endif // __BRILLO__ |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1705 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1706 | int callingPid = getCallingPid(); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1707 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1708 | if (!mInitialized) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1709 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 1710 | callingPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1711 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1712 | "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] | 1713 | } |
| 1714 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1715 | if (getCameraState(cameraId) == nullptr) { |
| 1716 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1717 | cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1718 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1719 | "No camera device with ID \"%s\" available", cameraId.c_str()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1722 | status_t err = checkIfDeviceIsUsable(cameraId); |
| 1723 | if (err != NO_ERROR) { |
| 1724 | switch(err) { |
| 1725 | case -ENODEV: |
| 1726 | case -EBUSY: |
| 1727 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1728 | "No camera device with ID \"%s\" currently available", cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1729 | default: |
| 1730 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1731 | "Unknown error connecting to ID \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | return Status::ok(); |
Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1735 | } |
| 1736 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1737 | Status CameraService::validateClientPermissionsLocked(const std::string& cameraId, |
| 1738 | const std::string& clientName, int& clientUid, int& clientPid, |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1739 | /*out*/int& originalClientPid) const { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1740 | int callingPid = getCallingPid(); |
| 1741 | int callingUid = getCallingUid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1742 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1743 | // Check if we can trust clientUid |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1744 | if (clientUid == USE_CALLING_UID) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1745 | clientUid = callingUid; |
| 1746 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1747 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1748 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1749 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1750 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1751 | "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] | 1752 | callingPid, callingUid, cameraId.c_str(), |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1753 | clientName.c_str(), clientPid, clientUid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1756 | // Check if we can trust clientPid |
| 1757 | if (clientPid == USE_CALLING_PID) { |
| 1758 | clientPid = callingPid; |
| 1759 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1760 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1761 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1762 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1763 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1764 | "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] | 1765 | callingPid, callingUid, cameraId.c_str(), |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1766 | clientName.c_str(), clientPid, clientUid); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1767 | } |
| 1768 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1769 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1770 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", |
| 1771 | cameraId.c_str()); |
| 1772 | 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] | 1773 | "available", cameraId.c_str()); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1774 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1775 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1776 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1777 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1778 | 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] | 1779 | "found while trying to query device kind", cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
Biswarup Pal | e506e73 | 2024-03-27 13:46:20 +0000 | [diff] [blame] | 1782 | // Get the device id that owns this camera. |
| 1783 | auto [deviceId, _] = mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 1784 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1785 | // If it's not calling from cameraserver, check the permission if the |
| 1786 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for |
| 1787 | // android.permission.SYSTEM_CAMERA for system only camera devices). |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1788 | bool checkPermissionForCamera = |
Biswarup Pal | e506e73 | 2024-03-27 13:46:20 +0000 | [diff] [blame] | 1789 | hasPermissionsForCamera(cameraId, clientPid, clientUid, clientName, deviceId); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1790 | if (callingPid != getpid() && |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1791 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && !checkPermissionForCamera) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1792 | 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] | 1793 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1794 | "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] | 1795 | clientName.c_str(), clientPid, clientUid, cameraId.c_str()); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1796 | } |
| 1797 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1798 | // 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] | 1799 | if (!mUidPolicy->isUidActive(callingUid, clientName)) { |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1800 | int32_t procState = mUidPolicy->getProcState(callingUid); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1801 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", |
| 1802 | clientPid, clientUid); |
| 1803 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1804 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" |
| 1805 | "calling UID %d proc state %" PRId32 ")", |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1806 | clientName.c_str(), clientPid, clientUid, cameraId.c_str(), |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1807 | callingUid, procState); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1808 | } |
| 1809 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1810 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use cases |
| 1811 | // such as rear view and surround view cannot be disabled and are exempt from sensor privacy |
| 1812 | // policy. In all other cases,if sensor privacy is enabled then prevent access to the camera. |
| 1813 | if ((!isAutomotivePrivilegedClient(callingUid) || |
| 1814 | !isAutomotiveExteriorSystemCamera(cameraId)) && |
| 1815 | mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1816 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); |
| 1817 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1818 | "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] | 1819 | "is enabled", clientName.c_str(), clientPid, clientUid, cameraId.c_str()); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1820 | } |
| 1821 | |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1822 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's |
| 1823 | // connected to camera service directly. |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1824 | originalClientPid = clientPid; |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1825 | clientPid = callingPid; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1826 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1827 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1828 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1829 | // For non-system clients : Only allow clients who are being used by the current foreground |
| 1830 | // device user, unless calling from our own process. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1831 | if (!callerHasSystemUid() && callingPid != getpid() && |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1832 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1833 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 1834 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1835 | toString(mAllowedUsers).c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1836 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1837 | "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] | 1838 | clientUserId, cameraId.c_str()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 1841 | if (flags::camera_hsum_permission()) { |
| 1842 | // If the System User tries to access the camera when the device is running in |
| 1843 | // headless system user mode, ensure that client has the required permission |
| 1844 | // CAMERA_HEADLESS_SYSTEM_USER. |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1845 | if (isHeadlessSystemUserMode() |
| 1846 | && (clientUserId == USER_SYSTEM) |
| 1847 | && !hasPermissionsForCameraHeadlessSystemUser(cameraId, callingPid, callingUid)) { |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 1848 | ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid); |
| 1849 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1850 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" as Headless System \ |
| 1851 | User without camera headless system user permission", |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1852 | clientName.c_str(), clientPid, clientUid, cameraId.c_str()); |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 1853 | } |
Jyoti Bhayana | 5bdb5a6 | 2023-08-24 14:46:08 -0700 | [diff] [blame] | 1854 | } |
| 1855 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1856 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1857 | } |
| 1858 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1859 | status_t CameraService::checkIfDeviceIsUsable(const std::string& cameraId) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1860 | auto cameraState = getCameraState(cameraId); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1861 | int callingPid = getCallingPid(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1862 | if (cameraState == nullptr) { |
| 1863 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1864 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1865 | return -ENODEV; |
| 1866 | } |
| 1867 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1868 | StatusInternal currentStatus = cameraState->getStatus(); |
| 1869 | if (currentStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1870 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1871 | callingPid, cameraId.c_str()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1872 | return -ENODEV; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1873 | } else if (currentStatus == StatusInternal::ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1874 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1875 | callingPid, cameraId.c_str()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1876 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1877 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1878 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1879 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1880 | } |
| 1881 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1882 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1883 | const CameraService::DescriptorPtr& desc, int oomScoreOffset, bool systemNativeClient) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1884 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1885 | // Make a descriptor for the incoming client |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1886 | auto clientDescriptor = |
| 1887 | CameraService::CameraClientManager::makeClientDescriptor(client, desc, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1888 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1889 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 1890 | |
| 1891 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1892 | client->getPackageName()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1893 | |
| 1894 | if (evicted.size() > 0) { |
| 1895 | // This should never happen - clients should already have been removed in disconnect |
| 1896 | for (auto& i : evicted) { |
| 1897 | 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] | 1898 | __FUNCTION__, i->getKey().c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 1902 | __FUNCTION__); |
| 1903 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1904 | |
| 1905 | // And register a death notification for the client callback. Do |
| 1906 | // this last to avoid Binder policy where a nested Binder |
| 1907 | // transaction might be pre-empted to service the client death |
| 1908 | // notification if the client process dies before linkToDeath is |
| 1909 | // invoked. |
| 1910 | sp<IBinder> remoteCallback = client->getRemote(); |
| 1911 | if (remoteCallback != nullptr) { |
| 1912 | remoteCallback->linkToDeath(this); |
| 1913 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1914 | } |
| 1915 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1916 | status_t CameraService::handleEvictionsLocked(const std::string& cameraId, int clientPid, |
| 1917 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, |
| 1918 | const std::string& packageName, int oomScoreOffset, bool systemNativeClient, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1919 | /*out*/ |
| 1920 | sp<BasicClient>* client, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1921 | std::shared_ptr<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1922 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1923 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1924 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1925 | DescriptorPtr clientDescriptor; |
| 1926 | { |
| 1927 | if (effectiveApiLevel == API_1) { |
| 1928 | // If we are using API1, any existing client for this camera ID with the same remote |
| 1929 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 1930 | |
| 1931 | auto current = mActiveClientManager.get(cameraId); |
| 1932 | if (current != nullptr) { |
| 1933 | auto clientSp = current->getValue(); |
| 1934 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1935 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
Shuzhen Wang | b2d43f6 | 2021-08-25 14:01:11 -0700 | [diff] [blame] | 1936 | ALOGW("CameraService connect called with a different" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1937 | " API level, evicting prior client..."); |
| 1938 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1939 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1940 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1941 | *client = clientSp; |
| 1942 | return NO_ERROR; |
| 1943 | } |
| 1944 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1945 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1946 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 1947 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1948 | // Get state for the given cameraId |
| 1949 | auto state = getCameraState(cameraId); |
| 1950 | if (state == nullptr) { |
| 1951 | 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] | 1952 | clientPid, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1953 | // Should never get here because validateConnectLocked should have errored out |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1954 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1955 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1956 | |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 1957 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1958 | sp<IBinder> binder = sm->checkService(String16(kProcessInfoServiceName)); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1959 | if (!binder && isAutomotivePrivilegedClient(getCallingUid())) { |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 1960 | // If processinfo service is not available and the client is automotive privileged |
| 1961 | // client used for safety critical uses cases such as rear-view and surround-view which |
| 1962 | // needs to be available before android boot completes, then use the hardcoded values |
| 1963 | // for the process state and priority score. As this scenario is before android system |
| 1964 | // services are up and client is native client, hence using NATIVE_ADJ as the priority |
| 1965 | // score and state as PROCESS_STATE_BOUND_TOP as such automotive apps need to be |
| 1966 | // visible on the top. |
| 1967 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 1968 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 1969 | state->getConflicting(), resource_policy::NATIVE_ADJ, clientPid, |
| 1970 | ActivityManager::PROCESS_STATE_BOUND_TOP, oomScoreOffset, systemNativeClient); |
| 1971 | } else { |
| 1972 | // Get current active client PIDs |
| 1973 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 1974 | ownerPids.push_back(clientPid); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1975 | |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 1976 | std::vector<int> priorityScores(ownerPids.size()); |
| 1977 | std::vector<int> states(ownerPids.size()); |
| 1978 | |
| 1979 | // Get priority scores of all active PIDs |
| 1980 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids(ownerPids.size(), |
| 1981 | &ownerPids[0], /*out*/&states[0], /*out*/&priorityScores[0]); |
| 1982 | if (err != OK) { |
| 1983 | ALOGE("%s: Priority score query failed: %d", __FUNCTION__, err); |
| 1984 | return err; |
| 1985 | } |
| 1986 | |
| 1987 | // Update all active clients' priorities |
| 1988 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; |
| 1989 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
| 1990 | pidToPriorityMap.emplace(ownerPids[i], |
| 1991 | resource_policy::ClientPriority(priorityScores[i], states[i], |
| 1992 | /* isVendorClient won't get copied over*/ false, |
| 1993 | /* oomScoreOffset won't get copied over*/ 0)); |
| 1994 | } |
| 1995 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
| 1996 | |
| 1997 | int32_t actualScore = priorityScores[priorityScores.size() - 1]; |
| 1998 | int32_t actualState = states[states.size() - 1]; |
| 1999 | |
| 2000 | // Make descriptor for incoming client. We store the oomScoreOffset |
| 2001 | // since we might need it later on new handleEvictionsLocked and |
| 2002 | // ProcessInfoService would not take that into account. |
| 2003 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 2004 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 2005 | state->getConflicting(), actualScore, clientPid, actualState, |
| 2006 | oomScoreOffset, systemNativeClient); |
| 2007 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2008 | |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2009 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); |
| 2010 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2011 | // Find clients that would be evicted |
| 2012 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 2013 | |
| 2014 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 2015 | // background, so we cannot do evictions |
| 2016 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 2017 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 2018 | " priority).", clientPid); |
| 2019 | |
| 2020 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2021 | std::string curTime = getFormattedCurrentTime(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2022 | auto incompatibleClients = |
| 2023 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 2024 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2025 | std::string msg = fmt::sprintf("%s : DENIED connect device %s client for package %s " |
| 2026 | "(PID %d, score %d state %d) due to eviction policy", curTime.c_str(), |
| 2027 | cameraId.c_str(), packageName.c_str(), clientPid, |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2028 | clientPriority.getScore(), clientPriority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2029 | |
| 2030 | for (auto& i : incompatibleClients) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2031 | 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] | 2032 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2033 | i->getKey().c_str(), |
| 2034 | i->getValue()->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2035 | i->getOwnerId(), i->getPriority().getScore(), |
| 2036 | i->getPriority().getState()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 2037 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2038 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().c_str(), |
| 2039 | i->getValue()->getPackageName().c_str(), i->getOwnerId(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2040 | i->getPriority().getScore(), i->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2044 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2045 | mEventLog.add(msg); |
| 2046 | |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 2047 | auto current = mActiveClientManager.get(cameraId); |
| 2048 | if (current != nullptr) { |
| 2049 | return -EBUSY; // CAMERA_IN_USE |
| 2050 | } else { |
| 2051 | return -EUSERS; // MAX_CAMERAS_IN_USE |
| 2052 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | for (auto& i : evicted) { |
| 2056 | sp<BasicClient> clientSp = i->getValue(); |
| 2057 | if (clientSp.get() == nullptr) { |
| 2058 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 2059 | |
| 2060 | // TODO: Remove this |
| 2061 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 2062 | __FUNCTION__); |
| 2063 | mActiveClientManager.remove(i); |
| 2064 | continue; |
| 2065 | } |
| 2066 | |
| 2067 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2068 | i->getKey().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2069 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2070 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2071 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2072 | logEvent(fmt::sprintf("EVICT device %s client held by package %s (PID" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2073 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" |
| 2074 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2075 | i->getKey().c_str(), clientSp->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2076 | i->getOwnerId(), i->getPriority().getScore(), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2077 | i->getPriority().getState(), cameraId.c_str(), |
| 2078 | packageName.c_str(), clientPid, clientPriority.getScore(), |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2079 | clientPriority.getState())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2080 | |
| 2081 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2082 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2083 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2084 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2085 | } |
| 2086 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2087 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 2088 | // other clients from connecting in mServiceLockWrapper if held |
| 2089 | mServiceLock.unlock(); |
| 2090 | |
| 2091 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2092 | int64_t token = clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2093 | |
| 2094 | // Destroy evicted clients |
| 2095 | for (auto& i : evictedClients) { |
| 2096 | // 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] | 2097 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2098 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2099 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2100 | restoreCallingIdentity(token); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2101 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2102 | for (const auto& i : evictedClients) { |
| 2103 | 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] | 2104 | __FUNCTION__, i->getKey().c_str(), i->getOwnerId()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2105 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 2106 | if (ret == TIMED_OUT) { |
| 2107 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2108 | "current clients:\n%s", __FUNCTION__, i->getKey().c_str(), |
| 2109 | mActiveClientManager.toString().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2110 | return -EBUSY; |
| 2111 | } |
| 2112 | if (ret != NO_ERROR) { |
| 2113 | 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] | 2114 | "current clients:\n%s", __FUNCTION__, i->getKey().c_str(), strerror(-ret), |
| 2115 | ret, mActiveClientManager.toString().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2116 | return ret; |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | evictedClients.clear(); |
| 2121 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2122 | // Once clients have been disconnected, relock |
| 2123 | mServiceLock.lock(); |
| 2124 | |
| 2125 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 2126 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 2127 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2128 | } |
| 2129 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2130 | *partial = clientDescriptor; |
| 2131 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2132 | } |
| 2133 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2134 | Status CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2135 | const sp<ICameraClient>& cameraClient, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2136 | int api1CameraId, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2137 | const std::string& clientPackageName, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2138 | int targetSdkVersion, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2139 | int rotationOverride, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2140 | bool forceSlowJpegMode, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2141 | const AttributionSourceState& clientAttribution, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2142 | int32_t devicePolicy, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 2143 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2144 | sp<ICamera>* device) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2145 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2146 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2147 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2148 | std::string cameraIdStr = |
| 2149 | cameraIdIntToStr(api1CameraId, clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2150 | if (cameraIdStr.empty()) { |
| 2151 | std::string msg = fmt::sprintf("Camera %d: Invalid camera id for device id %d", |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2152 | api1CameraId, clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2153 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2154 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 2155 | } |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 2156 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2157 | sp<Client> client = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2158 | ret = connectHelper<ICameraClient,Client>(cameraClient, cameraIdStr, api1CameraId, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2159 | clientPackageName, /*systemNativeClient*/ false, {}, clientAttribution.uid, |
| 2160 | clientAttribution.pid, API_1, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2161 | /*shimUpdateOnly*/ false, /*oomScoreOffset*/ 0, targetSdkVersion, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2162 | rotationOverride, forceSlowJpegMode, cameraIdStr, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2163 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2164 | if (!ret.isOk()) { |
| 2165 | logRejected(cameraIdStr, getCallingPid(), clientPackageName, toStdString(ret.toString8())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2166 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2167 | } |
| 2168 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2169 | *device = client; |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2170 | |
| 2171 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 2172 | const auto& mActivityManager = getActivityManager(); |
| 2173 | if (mActivityManager) { |
| 2174 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2175 | getCallingUid(), |
| 2176 | getCallingPid()); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2179 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2182 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, |
| 2183 | bool isVendorListener, int clientPid, int clientUid) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2184 | // If the client is not a vendor client, don't add listener if |
| 2185 | // a) the camera is a publicly hidden secure camera OR |
| 2186 | // b) the camera is a system only camera and the client doesn't |
| 2187 | // have android.permission.SYSTEM_CAMERA permissions. |
| 2188 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || |
| 2189 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2190 | !hasPermissionsForSystemCamera(std::string(), clientPid, clientUid)))) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2191 | return true; |
| 2192 | } |
| 2193 | return false; |
| 2194 | } |
| 2195 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2196 | bool CameraService::shouldRejectSystemCameraConnection(const std::string& cameraId) const { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2197 | // Rules for rejection: |
| 2198 | // 1) If cameraserver tries to access this camera device, accept the |
| 2199 | // connection. |
| 2200 | // 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] | 2201 | // non system component is trying to access it. |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2202 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY |
| 2203 | // and the serving thread is a non hwbinder thread, the client must have |
| 2204 | // android.permission.SYSTEM_CAMERA permissions to connect. |
| 2205 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2206 | int cPid = getCallingPid(); |
| 2207 | int cUid = getCallingUid(); |
| 2208 | bool systemClient = callerHasSystemUid(); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2209 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 2210 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 2211 | // This isn't a known camera ID, so it's not a system camera |
| 2212 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); |
| 2213 | return false; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2214 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2215 | |
| 2216 | // (1) Cameraserver trying to connect, accept. |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2217 | if (isCallerCameraServerNotDelegating()) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2218 | return false; |
| 2219 | } |
| 2220 | // (2) |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2221 | if (!systemClient && systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2222 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); |
| 2223 | return true; |
| 2224 | } |
| 2225 | // (3) Here we only check for permissions if it is a system only camera device. This is since |
| 2226 | // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some |
| 2227 | // characteristics) even if clients don't have android.permission.CAMERA. We do not want the |
| 2228 | // same behavior for system camera devices. |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2229 | if (!systemClient && systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2230 | !hasPermissionsForSystemCamera(cameraId, cPid, cUid)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2231 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", |
| 2232 | cameraId.c_str()); |
| 2233 | return true; |
| 2234 | } |
| 2235 | |
| 2236 | return false; |
| 2237 | } |
| 2238 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2239 | Status CameraService::connectDevice( |
| 2240 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2241 | const std::string& unresolvedCameraId, |
| 2242 | const std::string& clientPackageName, |
| 2243 | const std::optional<std::string>& clientFeatureId, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2244 | int oomScoreOffset, int targetSdkVersion, |
| 2245 | int rotationOverride, const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2246 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2247 | sp<hardware::camera2::ICameraDeviceUser>* device) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2248 | ATRACE_CALL(); |
Emilian Peev | 31bd242 | 2024-04-23 22:24:09 +0000 | [diff] [blame] | 2249 | RunThreadWithRealtimePriority priorityBump; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2250 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2251 | sp<CameraDeviceClient> client = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2252 | std::string clientPackageNameAdj = clientPackageName; |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2253 | int callingPid = getCallingPid(); |
| 2254 | int callingUid = getCallingUid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2255 | bool systemNativeClient = false; |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2256 | if (callerHasSystemUid() && (clientPackageNameAdj.size() == 0)) { |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2257 | std::string systemClient = fmt::sprintf("client.pid<%d>", callingPid); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2258 | clientPackageNameAdj = systemClient; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2259 | systemNativeClient = true; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2260 | } |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2261 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2262 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, |
| 2263 | clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2264 | if (!cameraIdOptional.has_value()) { |
| 2265 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2266 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2267 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2268 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 2269 | } |
| 2270 | std::string cameraId = cameraIdOptional.value(); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2271 | |
| 2272 | if (oomScoreOffset < 0) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2273 | std::string msg = |
| 2274 | fmt::sprintf("Cannot increase the priority of a client %s pid %d for " |
| 2275 | "camera id %s", clientPackageNameAdj.c_str(), callingPid, |
| 2276 | cameraId.c_str()); |
| 2277 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2278 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2281 | userid_t clientUserId = multiuser_get_user_id(clientAttribution.uid); |
| 2282 | if (clientAttribution.uid == USE_CALLING_UID) { |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 2283 | clientUserId = multiuser_get_user_id(callingUid); |
| 2284 | } |
| 2285 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2286 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use cases |
| 2287 | // such as rear view and surround view cannot be disabled. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2288 | if ((!isAutomotivePrivilegedClient(callingUid) || !isAutomotiveExteriorSystemCamera(cameraId)) |
| 2289 | && mCameraServiceProxyWrapper->isCameraDisabled(clientUserId)) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2290 | std::string msg = "Camera disabled by device policy"; |
| 2291 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2292 | return STATUS_ERROR(ERROR_DISABLED, msg.c_str()); |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 2293 | } |
| 2294 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2295 | // enforce system camera permissions |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2296 | if (oomScoreOffset > 0 |
| 2297 | && !hasPermissionsForSystemCamera(cameraId, callingPid, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2298 | callingUid) |
| 2299 | && !isTrustedCallingUid(callingUid)) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2300 | 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] | 2301 | "camera id %s without SYSTEM_CAMERA permissions", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2302 | clientPackageNameAdj.c_str(), callingPid, cameraId.c_str()); |
| 2303 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2304 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, msg.c_str()); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2307 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, |
| 2308 | cameraId, /*api1CameraId*/-1, clientPackageNameAdj, systemNativeClient, clientFeatureId, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2309 | clientAttribution.uid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, |
| 2310 | oomScoreOffset, targetSdkVersion, rotationOverride, /*forceSlowJpegMode*/false, |
| 2311 | unresolvedCameraId, /*out*/client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2312 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2313 | if (!ret.isOk()) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2314 | logRejected(cameraId, callingPid, clientPackageNameAdj, toStdString(ret.toString8())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2315 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2316 | } |
| 2317 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2318 | *device = client; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 2319 | Mutex::Autolock lock(mServiceLock); |
| 2320 | |
| 2321 | // Clear the previous cached logs and reposition the |
| 2322 | // file offset to beginning of the file to log new data. |
| 2323 | // If either truncate or lseek fails, close the previous file and create a new one. |
| 2324 | if ((ftruncate(mMemFd, 0) == -1) || (lseek(mMemFd, 0, SEEK_SET) == -1)) { |
| 2325 | ALOGE("%s: Error while truncating the file: %s", __FUNCTION__, sFileName); |
| 2326 | // Close the previous memfd. |
| 2327 | close(mMemFd); |
| 2328 | // If failure to wipe the data, then create a new file and |
| 2329 | // assign the new value to mMemFd. |
| 2330 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 2331 | if (mMemFd == -1) { |
| 2332 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 2333 | } |
| 2334 | } |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2335 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 2336 | const auto& mActivityManager = getActivityManager(); |
| 2337 | if (mActivityManager) { |
| 2338 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2339 | callingUid, |
| 2340 | callingPid); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2341 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2342 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2343 | } |
| 2344 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2345 | bool CameraService::isCameraPrivacyEnabled(const String16& packageName, const std::string& cam_id, |
| 2346 | int callingPid, int callingUid) { |
| 2347 | if (!isAutomotiveDevice()) { |
| 2348 | return mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
| 2349 | } |
| 2350 | |
| 2351 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for |
| 2352 | // safety-critical use cases cannot be disabled and are exempt from camera privacy policy. |
| 2353 | if ((isAutomotivePrivilegedClient(callingUid) && isAutomotiveExteriorSystemCamera(cam_id))) { |
| 2354 | ALOGI("Camera privacy cannot be enabled for automotive privileged client %d " |
| 2355 | "using camera %s", callingUid, cam_id.c_str()); |
| 2356 | return false; |
| 2357 | } |
| 2358 | |
| 2359 | if (mSensorPrivacyPolicy->isCameraPrivacyEnabled(packageName)) { |
| 2360 | return true; |
| 2361 | } else if (mSensorPrivacyPolicy->getCameraPrivacyState() == SensorPrivacyManager::DISABLED) { |
| 2362 | return false; |
Jyoti Bhayana | 54a4b00 | 2024-02-27 15:36:09 -0800 | [diff] [blame] | 2363 | } else if (mSensorPrivacyPolicy->getCameraPrivacyState() |
| 2364 | == SensorPrivacyManager::ENABLED_EXCEPT_ALLOWLISTED_APPS) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2365 | if (hasPermissionsForCameraPrivacyAllowlist(callingPid, callingUid)) { |
| 2366 | return false; |
| 2367 | } else { |
| 2368 | return true; |
| 2369 | } |
| 2370 | } |
| 2371 | return false; |
| 2372 | } |
| 2373 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2374 | std::string CameraService::getPackageNameFromUid(int clientUid) { |
| 2375 | std::string packageName(""); |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2376 | |
Avichal Rakesh | 5788fec | 2024-03-15 14:39:20 -0700 | [diff] [blame] | 2377 | sp<IPermissionController> permCtrl; |
| 2378 | if (flags::cache_permission_services()) { |
| 2379 | permCtrl = getPermissionController(); |
| 2380 | } else { |
| 2381 | sp<IServiceManager> sm = defaultServiceManager(); |
| 2382 | #pragma clang diagnostic push |
| 2383 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 2384 | // Using deprecated function to preserve functionality until the |
| 2385 | // cache_permission_services flag is removed. |
| 2386 | sp<IBinder> binder = sm->getService(toString16(kPermissionServiceName)); |
| 2387 | #pragma clang diagnostic pop |
| 2388 | if (binder == 0) { |
| 2389 | ALOGE("Cannot get permission service"); |
| 2390 | permCtrl = nullptr; |
| 2391 | } else { |
| 2392 | permCtrl = interface_cast<IPermissionController>(binder); |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | if (permCtrl == nullptr) { |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2397 | // Return empty package name and the further interaction |
| 2398 | // with camera will likely fail |
| 2399 | return packageName; |
| 2400 | } |
| 2401 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2402 | Vector<String16> packages; |
| 2403 | |
| 2404 | permCtrl->getPackagesForUid(clientUid, packages); |
| 2405 | |
| 2406 | if (packages.isEmpty()) { |
| 2407 | ALOGE("No packages for calling UID %d", clientUid); |
| 2408 | // Return empty package name and the further interaction |
| 2409 | // with camera will likely fail |
| 2410 | return packageName; |
| 2411 | } |
| 2412 | |
| 2413 | // Arbitrarily pick the first name in the list |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2414 | packageName = toStdString(packages[0]); |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2415 | |
| 2416 | return packageName; |
| 2417 | } |
| 2418 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2419 | template<class CALLBACK, class CLIENT> |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2420 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const std::string& cameraId, |
| 2421 | int api1CameraId, const std::string& clientPackageNameMaybe, bool systemNativeClient, |
| 2422 | const std::optional<std::string>& clientFeatureId, int clientUid, int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2423 | apiLevel effectiveApiLevel, bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2424 | int rotationOverride, bool forceSlowJpegMode, |
| 2425 | const std::string& originalCameraId, /*out*/sp<CLIENT>& device) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2426 | binder::Status ret = binder::Status::ok(); |
| 2427 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2428 | bool isNonSystemNdk = false; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2429 | std::string clientPackageName; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2430 | int packageUid = (clientUid == USE_CALLING_UID) ? |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2431 | getCallingUid() : clientUid; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2432 | if (clientPackageNameMaybe.size() <= 0) { |
| 2433 | // NDK calls don't come with package names, but we need one for various cases. |
| 2434 | // Generally, there's a 1:1 mapping between UID and package name, but shared UIDs |
| 2435 | // do exist. For all authentication cases, all packages under the same UID get the |
| 2436 | // same permissions, so picking any associated package name is sufficient. For some |
| 2437 | // other cases, this may give inaccurate names for clients in logs. |
| 2438 | isNonSystemNdk = true; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2439 | clientPackageName = getPackageNameFromUid(packageUid); |
| 2440 | } else { |
| 2441 | clientPackageName = clientPackageNameMaybe; |
| 2442 | } |
| 2443 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2444 | int originalClientPid = 0; |
| 2445 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2446 | int packagePid = (clientPid == USE_CALLING_PID) ? |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2447 | getCallingPid() : clientPid; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 2448 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2449 | "Camera API version %d", packagePid, clientPackageName.c_str(), cameraId.c_str(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2450 | static_cast<int>(effectiveApiLevel)); |
| 2451 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2452 | nsecs_t openTimeNs = systemTime(); |
| 2453 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2454 | sp<CLIENT> client = nullptr; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2455 | int facing = -1; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 2456 | int orientation = 0; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2457 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2458 | { |
| 2459 | // Acquire mServiceLock and prevent other clients from connecting |
| 2460 | std::unique_ptr<AutoConditionLock> lock = |
| 2461 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2462 | |
| 2463 | if (lock == nullptr) { |
| 2464 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." |
| 2465 | , clientPid); |
| 2466 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2467 | "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] | 2468 | cameraId.c_str(), clientPackageName.c_str(), clientPid); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2469 | } |
| 2470 | |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 2471 | // Enforce client permissions and do basic validity checks |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2472 | if (!(ret = validateConnectLocked(cameraId, clientPackageName, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2473 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { |
| 2474 | return ret; |
| 2475 | } |
| 2476 | |
| 2477 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 2478 | // we were doing a shim update, return immediately |
| 2479 | if (shimUpdateOnly) { |
| 2480 | auto cameraState = getCameraState(cameraId); |
| 2481 | if (cameraState != nullptr) { |
| 2482 | if (!cameraState->getShimParams().isEmpty()) return ret; |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | status_t err; |
| 2487 | |
| 2488 | sp<BasicClient> clientTmp = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2489 | std::shared_ptr<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>> partial; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2490 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2491 | IInterface::asBinder(cameraCb), clientPackageName, oomScoreOffset, |
| 2492 | systemNativeClient, /*out*/&clientTmp, /*out*/&partial)) != NO_ERROR) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2493 | switch (err) { |
| 2494 | case -ENODEV: |
| 2495 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2496 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2497 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2498 | case -EBUSY: |
| 2499 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2500 | "Higher-priority client using camera, ID \"%s\" currently unavailable", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2501 | cameraId.c_str()); |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 2502 | case -EUSERS: |
| 2503 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2504 | "Too many cameras already open, cannot open camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2505 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2506 | default: |
| 2507 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2508 | "Unexpected error %s (%d) opening camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2509 | strerror(-err), err, cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | if (clientTmp.get() != nullptr) { |
| 2514 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 2515 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 2516 | return ret; |
| 2517 | } |
| 2518 | |
| 2519 | // give flashlight a chance to close devices if necessary. |
| 2520 | mFlashlight->prepareDeviceOpen(cameraId); |
| 2521 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2522 | int portraitRotation; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2523 | auto deviceVersionAndTransport = |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2524 | getDeviceVersion(cameraId, rotationOverride, /*out*/&portraitRotation, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2525 | /*out*/&facing, /*out*/&orientation); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2526 | if (facing == -1) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2527 | 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] | 2528 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2529 | "Unable to get camera device \"%s\" facing", cameraId.c_str()); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2530 | } |
| 2531 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2532 | sp<BasicClient> tmp = nullptr; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2533 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2534 | mPerfClassPrimaryCameraIds, cameraId, targetSdkVersion); |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2535 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2536 | if(!(ret = makeClient(this, cameraCb, clientPackageName, systemNativeClient, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2537 | clientFeatureId, cameraId, api1CameraId, facing, |
| 2538 | orientation, clientPid, clientUid, getpid(), |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2539 | deviceVersionAndTransport, effectiveApiLevel, overrideForPerfClass, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2540 | rotationOverride, forceSlowJpegMode, originalCameraId, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2541 | /*out*/&tmp)).isOk()) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2542 | return ret; |
| 2543 | } |
| 2544 | client = static_cast<CLIENT*>(tmp.get()); |
| 2545 | |
| 2546 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 2547 | __FUNCTION__); |
| 2548 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2549 | std::string monitorTags = isClientWatched(client.get()) ? mMonitorTags : std::string(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2550 | err = client->initialize(mCameraProviderManager, monitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2551 | if (err != OK) { |
| 2552 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2553 | // Errors could be from the HAL module open call or from AppOpsManager |
Kwangkyu Park | b1aaf9a | 2023-07-08 00:42:03 +0900 | [diff] [blame] | 2554 | mServiceLock.unlock(); |
| 2555 | client->disconnect(); |
| 2556 | mServiceLock.lock(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2557 | switch(err) { |
| 2558 | case BAD_VALUE: |
| 2559 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2560 | "Illegal argument to HAL module for camera \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2561 | case -EBUSY: |
| 2562 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2563 | "Camera \"%s\" is already open", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2564 | case -EUSERS: |
| 2565 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2566 | "Too many cameras already open, cannot open camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2567 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2568 | case PERMISSION_DENIED: |
| 2569 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2570 | "No permission to open camera \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2571 | case -EACCES: |
| 2572 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2573 | "Camera \"%s\" disabled by policy", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2574 | case -ENODEV: |
| 2575 | default: |
| 2576 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2577 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2578 | strerror(-err), err); |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | // Update shim paremeters for legacy clients |
| 2583 | if (effectiveApiLevel == API_1) { |
| 2584 | // Assume we have always received a Client subclass for API1 |
| 2585 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 2586 | String8 rawParams = shimClient->getParameters(); |
| 2587 | CameraParameters params(rawParams); |
| 2588 | |
| 2589 | auto cameraState = getCameraState(cameraId); |
| 2590 | if (cameraState != nullptr) { |
| 2591 | cameraState->setShimParams(params); |
| 2592 | } else { |
| 2593 | 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] | 2594 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2595 | } |
| 2596 | } |
| 2597 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 2598 | // Enable/disable camera service watchdog |
| 2599 | client->setCameraServiceWatchdog(mCameraServiceWatchdogEnabled); |
| 2600 | |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2601 | CameraMetadata chars; |
| 2602 | bool rotateAndCropSupported = true; |
| 2603 | err = mCameraProviderManager->getCameraCharacteristics(cameraId, overrideForPerfClass, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2604 | &chars, rotationOverride); |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2605 | if (err == OK) { |
| 2606 | auto availableRotateCropEntry = chars.find( |
| 2607 | ANDROID_SCALER_AVAILABLE_ROTATE_AND_CROP_MODES); |
| 2608 | if (availableRotateCropEntry.count <= 1) { |
| 2609 | rotateAndCropSupported = false; |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2610 | } |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame] | 2611 | } else { |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2612 | ALOGE("%s: Unable to query static metadata for camera %s: %s (%d)", __FUNCTION__, |
| 2613 | cameraId.c_str(), strerror(-err), err); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2616 | if (rotateAndCropSupported) { |
| 2617 | // Set rotate-and-crop override behavior |
| 2618 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { |
| 2619 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2620 | } else if (rotationOverride != hardware::ICameraService::ROTATION_OVERRIDE_NONE && |
| 2621 | portraitRotation != 0) { |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2622 | uint8_t rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_AUTO; |
| 2623 | switch (portraitRotation) { |
| 2624 | case 90: |
| 2625 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_90; |
| 2626 | break; |
| 2627 | case 180: |
| 2628 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_180; |
| 2629 | break; |
| 2630 | case 270: |
| 2631 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_270; |
| 2632 | break; |
| 2633 | default: |
| 2634 | ALOGE("Unexpected portrait rotation: %d", portraitRotation); |
| 2635 | break; |
| 2636 | } |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2637 | // Here we're communicating to the client the chosen rotate |
| 2638 | // and crop mode to send to the HAL |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2639 | client->setRotateAndCropOverride(rotateAndCropMode); |
| 2640 | } else { |
| 2641 | client->setRotateAndCropOverride( |
| 2642 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 2643 | clientPackageName, facing, multiuser_get_user_id(clientUid))); |
| 2644 | } |
| 2645 | } |
| 2646 | |
| 2647 | bool autoframingSupported = true; |
| 2648 | auto availableAutoframingEntry = chars.find(ANDROID_CONTROL_AUTOFRAMING_AVAILABLE); |
| 2649 | if ((availableAutoframingEntry.count == 1) && (availableAutoframingEntry.data.u8[0] == |
| 2650 | ANDROID_CONTROL_AUTOFRAMING_AVAILABLE_FALSE)) { |
| 2651 | autoframingSupported = false; |
| 2652 | } |
| 2653 | |
| 2654 | if (autoframingSupported) { |
| 2655 | // Set autoframing override behaviour |
| 2656 | if (mOverrideAutoframingMode != ANDROID_CONTROL_AUTOFRAMING_AUTO) { |
| 2657 | client->setAutoframingOverride(mOverrideAutoframingMode); |
| 2658 | } else { |
| 2659 | client->setAutoframingOverride( |
| 2660 | mCameraServiceProxyWrapper->getAutoframingOverride( |
| 2661 | clientPackageName)); |
| 2662 | } |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 2663 | } |
| 2664 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2665 | bool isCameraPrivacyEnabled; |
| 2666 | if (flags::camera_privacy_allowlist()) { |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2667 | // Set camera muting behavior. |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2668 | isCameraPrivacyEnabled = this->isCameraPrivacyEnabled( |
| 2669 | toString16(client->getPackageName()), cameraId, packagePid, packageUid); |
| 2670 | } else { |
| 2671 | isCameraPrivacyEnabled = |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2672 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2673 | } |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 2674 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2675 | if (client->supportsCameraMute()) { |
| 2676 | client->setCameraMute( |
| 2677 | mOverrideCameraMuteMode || isCameraPrivacyEnabled); |
| 2678 | } else if (isCameraPrivacyEnabled) { |
| 2679 | // no camera mute supported, but privacy is on! => disconnect |
| 2680 | ALOGI("Camera mute not supported for package: %s, camera id: %s", |
| 2681 | client->getPackageName().c_str(), cameraId.c_str()); |
| 2682 | // Do not hold mServiceLock while disconnecting clients, but |
| 2683 | // retain the condition blocking other clients from connecting |
| 2684 | // in mServiceLockWrapper if held. |
| 2685 | mServiceLock.unlock(); |
| 2686 | // Clear caller identity temporarily so client disconnect PID |
| 2687 | // checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2688 | int64_t token = clearCallingIdentity(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2689 | // Note AppOp to trigger the "Unblock" dialog |
| 2690 | client->noteAppOp(); |
| 2691 | client->disconnect(); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2692 | restoreCallingIdentity(token); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2693 | // Reacquire mServiceLock |
| 2694 | mServiceLock.lock(); |
| 2695 | |
| 2696 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 2697 | "Camera \"%s\" disabled due to camera mute", cameraId.c_str()); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2698 | } |
| 2699 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2700 | if (shimUpdateOnly) { |
| 2701 | // If only updating legacy shim parameters, immediately disconnect client |
| 2702 | mServiceLock.unlock(); |
| 2703 | client->disconnect(); |
| 2704 | mServiceLock.lock(); |
| 2705 | } else { |
| 2706 | // Otherwise, add client to active clients list |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2707 | finishConnectLocked(client, partial, oomScoreOffset, systemNativeClient); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2708 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 2709 | |
| 2710 | client->setImageDumpMask(mImageDumpMask); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 2711 | client->setStreamUseCaseOverrides(mStreamUseCaseOverrides); |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 2712 | client->setZoomOverride(mZoomOverrideValue); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2713 | } // lock is destroyed, allow further connect calls |
| 2714 | |
| 2715 | // Important: release the mutex here so the client can call back into the service from its |
| 2716 | // destructor (can be at the end of the call) |
| 2717 | device = client; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2718 | |
| 2719 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 2720 | mCameraServiceProxyWrapper->logOpen(cameraId, facing, clientPackageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2721 | effectiveApiLevel, isNonSystemNdk, openLatencyMs); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2722 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2723 | { |
| 2724 | Mutex::Autolock lock(mInjectionParametersLock); |
| 2725 | if (cameraId == mInjectionInternalCamId && mInjectionInitPending) { |
| 2726 | mInjectionInitPending = false; |
| 2727 | status_t res = NO_ERROR; |
| 2728 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 2729 | if (clientDescriptor != nullptr) { |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2730 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 2731 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 2732 | if(res != OK) { |
| 2733 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2734 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2735 | mInjectionExternalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2736 | } |
| 2737 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2738 | if (res != OK) { |
| 2739 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2740 | } |
| 2741 | } else { |
| 2742 | ALOGE("%s: Internal camera ID = %s 's client does not exist!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2743 | __FUNCTION__, mInjectionInternalCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2744 | res = NO_INIT; |
| 2745 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2746 | } |
| 2747 | } |
| 2748 | } |
| 2749 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2750 | return ret; |
| 2751 | } |
| 2752 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2753 | status_t CameraService::addOfflineClient(const std::string &cameraId, |
| 2754 | sp<BasicClient> offlineClient) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2755 | if (offlineClient.get() == nullptr) { |
| 2756 | return BAD_VALUE; |
| 2757 | } |
| 2758 | |
| 2759 | { |
| 2760 | // Acquire mServiceLock and prevent other clients from connecting |
| 2761 | std::unique_ptr<AutoConditionLock> lock = |
| 2762 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2763 | |
| 2764 | if (lock == nullptr) { |
| 2765 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." |
| 2766 | , __FUNCTION__, offlineClient->getClientPid()); |
| 2767 | return TIMED_OUT; |
| 2768 | } |
| 2769 | |
| 2770 | auto onlineClientDesc = mActiveClientManager.get(cameraId); |
| 2771 | if (onlineClientDesc.get() == nullptr) { |
| 2772 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, |
| 2773 | cameraId.c_str()); |
| 2774 | return BAD_VALUE; |
| 2775 | } |
| 2776 | |
| 2777 | // Offline clients do not evict or conflict with other online devices. Resource sharing |
| 2778 | // conflicts are handled by the camera provider which will either succeed or fail before |
| 2779 | // reaching this method. |
| 2780 | const auto& onlinePriority = onlineClientDesc->getPriority(); |
| 2781 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( |
| 2782 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2783 | /*conflictingKeys*/ std::set<std::string>(), onlinePriority.getScore(), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2784 | onlineClientDesc->getOwnerId(), onlinePriority.getState(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2785 | // native clients don't have offline processing support. |
| 2786 | /*ommScoreOffset*/ 0, /*systemNativeClient*/false); |
Guillaume Bailey | 417e43d | 2022-11-02 15:30:24 +0100 | [diff] [blame] | 2787 | if (offlineClientDesc == nullptr) { |
| 2788 | ALOGE("%s: Offline client descriptor was NULL", __FUNCTION__); |
| 2789 | return BAD_VALUE; |
| 2790 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2791 | |
| 2792 | // Allow only one offline device per camera |
| 2793 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); |
| 2794 | if (!incompatibleClients.empty()) { |
| 2795 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); |
| 2796 | return BAD_VALUE; |
| 2797 | } |
| 2798 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2799 | std::string monitorTags = isClientWatched(offlineClient.get()) |
| 2800 | ? mMonitorTags : std::string(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2801 | auto err = offlineClient->initialize(mCameraProviderManager, monitorTags); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2802 | if (err != OK) { |
| 2803 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); |
| 2804 | return err; |
| 2805 | } |
| 2806 | |
| 2807 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); |
| 2808 | if (evicted.size() > 0) { |
| 2809 | for (auto& i : evicted) { |
| 2810 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2811 | __FUNCTION__, i->getKey().c_str()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " |
| 2815 | "properly", __FUNCTION__); |
| 2816 | |
| 2817 | return BAD_VALUE; |
| 2818 | } |
| 2819 | |
| 2820 | logConnectedOffline(offlineClientDesc->getKey(), |
| 2821 | static_cast<int>(offlineClientDesc->getOwnerId()), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2822 | offlineClient->getPackageName()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2823 | |
| 2824 | sp<IBinder> remoteCallback = offlineClient->getRemote(); |
| 2825 | if (remoteCallback != nullptr) { |
| 2826 | remoteCallback->linkToDeath(this); |
| 2827 | } |
| 2828 | } // lock is destroyed, allow further connect calls |
| 2829 | |
| 2830 | return OK; |
| 2831 | } |
| 2832 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2833 | Status CameraService::turnOnTorchWithStrengthLevel(const std::string& unresolvedCameraId, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2834 | int32_t torchStrength, const sp<IBinder>& clientBinder, |
| 2835 | const AttributionSourceState& clientAttribution, int32_t devicePolicy) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2836 | Mutex::Autolock lock(mServiceLock); |
| 2837 | |
| 2838 | ATRACE_CALL(); |
| 2839 | if (clientBinder == nullptr) { |
| 2840 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
| 2841 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2842 | "Torch client binder in null."); |
| 2843 | } |
| 2844 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2845 | int uid = getCallingUid(); |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2846 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, |
| 2847 | clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2848 | if (!cameraIdOptional.has_value()) { |
| 2849 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2850 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2851 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2852 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 2853 | } |
| 2854 | std::string cameraId = cameraIdOptional.value(); |
| 2855 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2856 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2857 | 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] | 2858 | "for system only device %s: ", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | // verify id is valid |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2862 | auto state = getCameraState(cameraId); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2863 | if (state == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2864 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2865 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2866 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | StatusInternal cameraStatus = state->getStatus(); |
| 2870 | if (cameraStatus != StatusInternal::NOT_AVAILABLE && |
| 2871 | cameraStatus != StatusInternal::PRESENT) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2872 | 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] | 2873 | (int)cameraStatus); |
| 2874 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2875 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | { |
| 2879 | Mutex::Autolock al(mTorchStatusMutex); |
| 2880 | TorchModeStatus status; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2881 | status_t err = getTorchStatusLocked(cameraId, &status); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2882 | if (err != OK) { |
| 2883 | if (err == NAME_NOT_FOUND) { |
| 2884 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2885 | "Camera \"%s\" does not have a flash unit", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2886 | } |
| 2887 | ALOGE("%s: getting current torch status failed for camera %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2888 | __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2889 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2890 | "Error changing torch strength level for camera \"%s\": %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2891 | cameraId.c_str(), strerror(-err), err); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
| 2895 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
| 2896 | ALOGE("%s: torch mode of camera %s is not available because " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2897 | "camera is in use.", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2898 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2899 | "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] | 2900 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2901 | } else { |
| 2902 | ALOGE("%s: torch mode of camera %s is not available due to " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2903 | "insufficient resources", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2904 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2905 | "Torch for camera \"%s\" is not available due to insufficient resources", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2906 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2907 | } |
| 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | { |
| 2912 | Mutex::Autolock al(mTorchUidMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2913 | updateTorchUidMapLocked(cameraId, uid); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2914 | } |
| 2915 | // Check if the current torch strength level is same as the new one. |
| 2916 | bool shouldSkipTorchStrengthUpdates = mCameraProviderManager->shouldSkipTorchStrengthUpdate( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2917 | cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2918 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2919 | status_t err = mFlashlight->turnOnTorchWithStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2920 | |
| 2921 | if (err != OK) { |
| 2922 | int32_t errorCode; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2923 | std::string msg; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2924 | switch (err) { |
| 2925 | case -ENOSYS: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2926 | msg = fmt::sprintf("Camera \"%s\" has no flashlight.", |
| 2927 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2928 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2929 | break; |
| 2930 | case -EBUSY: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2931 | msg = fmt::sprintf("Camera \"%s\" is in use", |
| 2932 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2933 | errorCode = ERROR_CAMERA_IN_USE; |
| 2934 | break; |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 2935 | case -EINVAL: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2936 | msg = fmt::sprintf("Torch strength level %d is not within the " |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 2937 | "valid range.", torchStrength); |
| 2938 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2939 | break; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2940 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2941 | msg = "Changing torch strength level failed."; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2942 | errorCode = ERROR_INVALID_OPERATION; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2943 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2944 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2945 | return STATUS_ERROR(errorCode, msg.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | { |
| 2949 | // update the link to client's death |
| 2950 | // Store the last client that turns on each camera's torch mode. |
| 2951 | Mutex::Autolock al(mTorchClientMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2952 | ssize_t index = mTorchClientMap.indexOfKey(cameraId); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2953 | if (index == NAME_NOT_FOUND) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2954 | mTorchClientMap.add(cameraId, clientBinder); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2955 | } else { |
| 2956 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
| 2957 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2958 | } |
| 2959 | clientBinder->linkToDeath(this); |
| 2960 | } |
| 2961 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2962 | int clientPid = getCallingPid(); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2963 | 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] | 2964 | __FUNCTION__, cameraId.c_str(), torchStrength, clientPid); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2965 | if (!shouldSkipTorchStrengthUpdates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2966 | broadcastTorchStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2967 | } |
| 2968 | return Status::ok(); |
| 2969 | } |
| 2970 | |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 2971 | Status CameraService::setTorchMode(const std::string& unresolvedCameraId, bool enabled, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2972 | const sp<IBinder>& clientBinder, const AttributionSourceState& clientAttribution, |
| 2973 | int32_t devicePolicy) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2974 | Mutex::Autolock lock(mServiceLock); |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2975 | |
| 2976 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2977 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2978 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2979 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2980 | "Torch client Binder is null"); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2981 | } |
| 2982 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2983 | int uid = getCallingUid(); |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2984 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, |
| 2985 | clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2986 | if (!cameraIdOptional.has_value()) { |
| 2987 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2988 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2989 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2990 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 2991 | } |
| 2992 | std::string cameraId = cameraIdOptional.value(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2993 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2994 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2995 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2996 | " for system only device %s: ", cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2997 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2998 | // verify id is valid. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2999 | auto state = getCameraState(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3000 | if (state == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3001 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3002 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3003 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3004 | } |
| 3005 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3006 | StatusInternal cameraStatus = state->getStatus(); |
| 3007 | if (cameraStatus != StatusInternal::PRESENT && |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 3008 | cameraStatus != StatusInternal::NOT_AVAILABLE) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3009 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, cameraId.c_str(), |
| 3010 | (int)cameraStatus); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3011 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3012 | "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] | 3013 | } |
| 3014 | |
| 3015 | { |
| 3016 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3017 | TorchModeStatus status; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3018 | status_t err = getTorchStatusLocked(cameraId, &status); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3019 | if (err != OK) { |
| 3020 | if (err == NAME_NOT_FOUND) { |
| 3021 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3022 | "Camera \"%s\" does not have a flash unit", cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3023 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3024 | ALOGE("%s: getting current torch status failed for camera %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3025 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3026 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3027 | "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] | 3028 | strerror(-err), err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3029 | } |
| 3030 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3031 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 3032 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3033 | ALOGE("%s: torch mode of camera %s is not available because " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3034 | "camera is in use", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3035 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 3036 | "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] | 3037 | cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3038 | } else { |
| 3039 | ALOGE("%s: torch mode of camera %s is not available due to " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3040 | "insufficient resources", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3041 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 3042 | "Torch for camera \"%s\" is not available due to insufficient resources", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3043 | cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3044 | } |
| 3045 | } |
| 3046 | } |
| 3047 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3048 | { |
| 3049 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 3050 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 3051 | Mutex::Autolock al(mTorchUidMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3052 | updateTorchUidMapLocked(cameraId, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3053 | } |
| 3054 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3055 | status_t err = mFlashlight->setTorchMode(cameraId, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3056 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3057 | if (err != OK) { |
| 3058 | int32_t errorCode; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3059 | std::string msg; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3060 | switch (err) { |
| 3061 | case -ENOSYS: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3062 | msg = fmt::sprintf("Camera \"%s\" has no flashlight", |
| 3063 | cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3064 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 3065 | break; |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 3066 | case -EBUSY: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3067 | msg = fmt::sprintf("Camera \"%s\" is in use", |
| 3068 | cameraId.c_str()); |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 3069 | errorCode = ERROR_CAMERA_IN_USE; |
| 3070 | break; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3071 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3072 | msg = fmt::sprintf( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3073 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3074 | cameraId.c_str(), enabled, strerror(-err), err); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3075 | errorCode = ERROR_INVALID_OPERATION; |
| 3076 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3077 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3078 | logServiceError(msg, errorCode); |
| 3079 | return STATUS_ERROR(errorCode, msg.c_str()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3080 | } |
| 3081 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3082 | { |
| 3083 | // update the link to client's death |
| 3084 | Mutex::Autolock al(mTorchClientMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3085 | ssize_t index = mTorchClientMap.indexOfKey(cameraId); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3086 | if (enabled) { |
| 3087 | if (index == NAME_NOT_FOUND) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3088 | mTorchClientMap.add(cameraId, clientBinder); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3089 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3090 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3091 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 3092 | } |
| 3093 | clientBinder->linkToDeath(this); |
| 3094 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3095 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3096 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3097 | } |
| 3098 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3099 | int clientPid = getCallingPid(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3100 | std::string torchState = enabled ? "on" : "off"; |
| 3101 | ALOGI("Torch for camera id %s turned %s for client PID %d", cameraId.c_str(), |
| 3102 | torchState.c_str(), clientPid); |
| 3103 | logTorchEvent(cameraId, torchState, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3104 | return Status::ok(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3105 | } |
| 3106 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3107 | void CameraService::updateTorchUidMapLocked(const std::string& cameraId, int uid) { |
| 3108 | if (mTorchUidMap.find(cameraId) == mTorchUidMap.end()) { |
| 3109 | mTorchUidMap[cameraId].first = uid; |
| 3110 | mTorchUidMap[cameraId].second = uid; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3111 | } else { |
| 3112 | // Set the pending UID |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3113 | mTorchUidMap[cameraId].first = uid; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3114 | } |
| 3115 | } |
| 3116 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3117 | Status CameraService::notifySystemEvent(int32_t eventId, |
| 3118 | const std::vector<int32_t>& args) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3119 | const int pid = getCallingPid(); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3120 | const int selfPid = getpid(); |
| 3121 | |
| 3122 | // Permission checks |
| 3123 | if (pid != selfPid) { |
| 3124 | // Ensure we're being called by system_server, or similar process with |
| 3125 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3126 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3127 | const int uid = getCallingUid(); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3128 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 3129 | " events from pid=%d, uid=%d", pid, uid); |
| 3130 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 3131 | "No permission to send updates to camera service about system events" |
| 3132 | " from pid=%d, uid=%d", pid, uid); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3133 | } |
| 3134 | } |
| 3135 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3136 | ATRACE_CALL(); |
| 3137 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3138 | switch(eventId) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3139 | case ICameraService::EVENT_USER_SWITCHED: { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3140 | // 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] | 3141 | // from a system server crash |
| 3142 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3143 | mSensorPrivacyPolicy->registerSelf(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3144 | doUserSwitch(/*newUserIds*/ args); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3145 | break; |
| 3146 | } |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 3147 | case ICameraService::EVENT_USB_DEVICE_ATTACHED: |
| 3148 | case ICameraService::EVENT_USB_DEVICE_DETACHED: { |
Pawan Wagh | 99f44e2 | 2023-07-05 21:26:43 +0000 | [diff] [blame] | 3149 | if (args.size() != 1) { |
| 3150 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, |
| 3151 | "USB Device Event requires 1 argument"); |
| 3152 | } |
| 3153 | |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 3154 | // Notify CameraProviderManager for lazy HALs |
| 3155 | mCameraProviderManager->notifyUsbDeviceEvent(eventId, |
| 3156 | std::to_string(args[0])); |
| 3157 | break; |
| 3158 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3159 | case ICameraService::EVENT_NONE: |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3160 | default: { |
| 3161 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 3162 | eventId); |
| 3163 | break; |
| 3164 | } |
| 3165 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3166 | return Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3169 | void CameraService::notifyMonitoredUids() { |
| 3170 | Mutex::Autolock lock(mStatusListenerLock); |
| 3171 | |
| 3172 | for (const auto& it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3173 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 3174 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 3175 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3176 | } |
| 3177 | } |
| 3178 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3179 | void CameraService::notifyMonitoredUids(const std::unordered_set<uid_t> ¬ifyUidSet) { |
| 3180 | Mutex::Autolock lock(mStatusListenerLock); |
| 3181 | |
| 3182 | for (const auto& it : mListenerList) { |
| 3183 | if (notifyUidSet.find(it->getListenerUid()) != notifyUidSet.end()) { |
| 3184 | ALOGV("%s: notifying uid %d", __FUNCTION__, it->getListenerUid()); |
| 3185 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
| 3186 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 3187 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
| 3188 | } |
| 3189 | } |
| 3190 | } |
| 3191 | |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3192 | Status CameraService::notifyDeviceStateChange(int64_t newState) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3193 | const int pid = getCallingPid(); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3194 | const int selfPid = getpid(); |
| 3195 | |
| 3196 | // Permission checks |
| 3197 | if (pid != selfPid) { |
| 3198 | // Ensure we're being called by system_server, or similar process with |
| 3199 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3200 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3201 | const int uid = getCallingUid(); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3202 | ALOGE("Permission Denial: cannot send updates to camera service about device" |
| 3203 | " state changes from pid=%d, uid=%d", pid, uid); |
| 3204 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 3205 | "No permission to send updates to camera service about device state" |
| 3206 | " changes from pid=%d, uid=%d", pid, uid); |
| 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | ATRACE_CALL(); |
| 3211 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3212 | { |
| 3213 | Mutex::Autolock lock(mServiceLock); |
| 3214 | mDeviceState = newState; |
| 3215 | } |
| 3216 | |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 3217 | mCameraProviderManager->notifyDeviceStateChange(newState); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3218 | |
| 3219 | return Status::ok(); |
| 3220 | } |
| 3221 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3222 | Status CameraService::notifyDisplayConfigurationChange() { |
| 3223 | ATRACE_CALL(); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3224 | const int callingPid = getCallingPid(); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3225 | const int selfPid = getpid(); |
| 3226 | |
| 3227 | // Permission checks |
| 3228 | if (callingPid != selfPid) { |
| 3229 | // Ensure we're being called by system_server, or similar process with |
| 3230 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3231 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3232 | const int uid = getCallingUid(); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3233 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" |
| 3234 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 3235 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 3236 | "No permission to send updates to camera service about orientation" |
| 3237 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 3238 | } |
| 3239 | } |
| 3240 | |
| 3241 | Mutex::Autolock lock(mServiceLock); |
| 3242 | |
| 3243 | // Don't do anything if rotate-and-crop override via cmd is active |
| 3244 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); |
| 3245 | |
| 3246 | const auto clients = mActiveClientManager.getAll(); |
| 3247 | for (auto& current : clients) { |
| 3248 | if (current != nullptr) { |
| 3249 | const auto basicClient = current->getValue(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3250 | if (basicClient.get() != nullptr && !basicClient->getOverrideToPortrait()) { |
| 3251 | basicClient->setRotateAndCropOverride( |
| 3252 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 3253 | basicClient->getPackageName(), |
| 3254 | basicClient->getCameraFacing(), |
| 3255 | multiuser_get_user_id(basicClient->getClientUid()))); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3256 | } |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | return Status::ok(); |
| 3261 | } |
| 3262 | |
| 3263 | Status CameraService::getConcurrentCameraIds( |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3264 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { |
| 3265 | ATRACE_CALL(); |
| 3266 | if (!concurrentCameraIds) { |
| 3267 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); |
| 3268 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); |
| 3269 | } |
| 3270 | |
| 3271 | if (!mInitialized) { |
| 3272 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3273 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3274 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 3275 | "Camera subsystem is not available"); |
| 3276 | } |
| 3277 | // First call into the provider and get the set of concurrent camera |
| 3278 | // combinations |
| 3279 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = |
Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 3280 | mCameraProviderManager->getConcurrentCameraIds(); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3281 | for (auto &combination : concurrentCameraCombinations) { |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3282 | std::vector<std::pair<std::string, int32_t>> validCombination; |
| 3283 | int32_t firstDeviceId = kInvalidDeviceId; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3284 | for (auto &cameraId : combination) { |
| 3285 | // if the camera state is not present, skip |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3286 | auto state = getCameraState(cameraId); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3287 | if (state == nullptr) { |
| 3288 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); |
| 3289 | continue; |
| 3290 | } |
| 3291 | StatusInternal status = state->getStatus(); |
| 3292 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { |
| 3293 | continue; |
| 3294 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3295 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3296 | continue; |
| 3297 | } |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3298 | auto [cameraOwnerDeviceId, mappedCameraId] = |
| 3299 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 3300 | if (firstDeviceId == kInvalidDeviceId) { |
| 3301 | firstDeviceId = cameraOwnerDeviceId; |
| 3302 | } else if (firstDeviceId != cameraOwnerDeviceId) { |
| 3303 | // Found an invalid combination which contains cameras with different device id's, |
| 3304 | // hence discard it. |
| 3305 | validCombination.clear(); |
| 3306 | break; |
| 3307 | } |
| 3308 | validCombination.push_back({mappedCameraId, cameraOwnerDeviceId}); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3309 | } |
| 3310 | if (validCombination.size() != 0) { |
| 3311 | concurrentCameraIds->push_back(std::move(validCombination)); |
| 3312 | } |
| 3313 | } |
| 3314 | return Status::ok(); |
| 3315 | } |
| 3316 | |
| 3317 | Status CameraService::isConcurrentSessionConfigurationSupported( |
| 3318 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 3319 | int targetSdkVersion, const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3320 | /*out*/bool* isSupported) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3321 | if (!isSupported) { |
| 3322 | ALOGE("%s: isSupported is NULL", __FUNCTION__); |
| 3323 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); |
| 3324 | } |
| 3325 | |
| 3326 | if (!mInitialized) { |
| 3327 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 3328 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 3329 | "Camera subsystem is not available"); |
| 3330 | } |
| 3331 | |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3332 | for (auto cameraIdAndSessionConfiguration : cameraIdsAndSessionConfigurations) { |
| 3333 | std::optional<std::string> cameraIdOptional = |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 3334 | resolveCameraId(cameraIdAndSessionConfiguration.mCameraId, |
| 3335 | clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3336 | if (!cameraIdOptional.has_value()) { |
| 3337 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 3338 | cameraIdAndSessionConfiguration.mCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3339 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3340 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 3341 | } |
| 3342 | cameraIdAndSessionConfiguration.mCameraId = cameraIdOptional.value(); |
| 3343 | } |
| 3344 | |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3345 | // Check for camera permissions |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3346 | int callingPid = getCallingPid(); |
| 3347 | int callingUid = getCallingUid(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 3348 | bool hasCameraPermission = ((callingPid == getpid()) || |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3349 | hasPermissionsForCamera(callingPid, callingUid, |
| 3350 | devicePolicy == IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 3351 | ? kDefaultDeviceId : clientAttribution.deviceId)); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 3352 | if (!hasCameraPermission) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3353 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 3354 | "android.permission.CAMERA needed to call" |
| 3355 | "isConcurrentSessionConfigurationSupported"); |
| 3356 | } |
| 3357 | |
| 3358 | status_t res = |
| 3359 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 3360 | cameraIdsAndSessionConfigurations, mPerfClassPrimaryCameraIds, |
| 3361 | targetSdkVersion, isSupported); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3362 | if (res != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3363 | logServiceError("Unable to query session configuration support", |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3364 | ERROR_INVALID_OPERATION); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3365 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " |
| 3366 | "support %s (%d)", strerror(-res), res); |
| 3367 | } |
| 3368 | return Status::ok(); |
| 3369 | } |
| 3370 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3371 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, |
| 3372 | /*out*/ |
| 3373 | std::vector<hardware::CameraStatus> *cameraStatuses) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3374 | return addListenerHelper(listener, cameraStatuses); |
| 3375 | } |
| 3376 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3377 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, |
| 3378 | std::vector<hardware::CameraStatus>* cameraStatuses) { |
| 3379 | return addListenerHelper(listener, cameraStatuses, false, true); |
| 3380 | } |
| 3381 | |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3382 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, |
| 3383 | /*out*/ |
| 3384 | std::vector<hardware::CameraStatus> *cameraStatuses, |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3385 | bool isVendorListener, bool isProcessLocalTest) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3386 | ATRACE_CALL(); |
| 3387 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3388 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3389 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 3390 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3391 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3392 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3393 | } |
| 3394 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3395 | auto clientPid = getCallingPid(); |
| 3396 | auto clientUid = getCallingUid(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 3397 | bool openCloseCallbackAllowed = hasPermissionsForOpenCloseListener(clientPid, clientUid); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3398 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3399 | Mutex::Autolock lock(mServiceLock); |
| 3400 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3401 | { |
| 3402 | Mutex::Autolock lock(mStatusListenerLock); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3403 | for (const auto &it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3404 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3405 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 3406 | __FUNCTION__, listener.get()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3407 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3408 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3409 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3410 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3411 | sp<ServiceListener> serviceListener = |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3412 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, |
| 3413 | openCloseCallbackAllowed); |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3414 | auto ret = serviceListener->initialize(isProcessLocalTest); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3415 | if (ret != NO_ERROR) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3416 | std::string msg = fmt::sprintf("Failed to initialize service listener: %s (%d)", |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3417 | strerror(-ret), ret); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3418 | logServiceError(msg, ERROR_ILLEGAL_ARGUMENT); |
| 3419 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3420 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3421 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3422 | // The listener still needs to be added to the list of listeners, regardless of what |
| 3423 | // permissions the listener process has / whether it is a vendor listener. Since it might be |
| 3424 | // eligible to listen to other camera ids. |
| 3425 | mListenerList.emplace_back(serviceListener); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3426 | mUidPolicy->registerMonitorUid(clientUid, /*openCamera*/false); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3427 | } |
| 3428 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3429 | /* Collect current devices and status */ |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3430 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3431 | Mutex::Autolock lock(mCameraStatesLock); |
| 3432 | for (auto& i : mCameraStates) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3433 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 3434 | auto [deviceId, mappedCameraId] = |
| 3435 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(i.first); |
| 3436 | |
| 3437 | cameraStatuses->emplace_back(mappedCameraId, |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 3438 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds(), |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3439 | openCloseCallbackAllowed ? i.second->getClientPackage() : std::string(), |
| 3440 | deviceId); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3441 | } |
| 3442 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3443 | // Remove the camera statuses that should be hidden from the client, we do |
| 3444 | // this after collecting the states in order to avoid holding |
| 3445 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at |
| 3446 | // the same time. |
| 3447 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), |
| 3448 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3449 | std::string cameraId = s.cameraId; |
| 3450 | std::optional<std::string> cameraIdOptional = resolveCameraId(s.cameraId, |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 3451 | s.deviceId, IVirtualDeviceManagerNative::DEVICE_POLICY_CUSTOM); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3452 | if (!cameraIdOptional.has_value()) { |
| 3453 | std::string msg = |
| 3454 | fmt::sprintf( |
| 3455 | "Camera %s: Invalid camera id for device id %d", |
| 3456 | s.cameraId.c_str(), s.deviceId); |
| 3457 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3458 | return true; |
| 3459 | } |
| 3460 | cameraId = cameraIdOptional.value(); |
| 3461 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 3462 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 3463 | ALOGE("%s: Invalid camera id %s, skipping status update", |
| 3464 | __FUNCTION__, s.cameraId.c_str()); |
| 3465 | return true; |
| 3466 | } |
| 3467 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, |
| 3468 | clientUid); |
| 3469 | }), cameraStatuses->end()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3470 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3471 | // cameraStatuses will have non-eligible camera ids removed. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3472 | std::set<std::string> idsChosenForCallback; |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3473 | for (const auto &s : *cameraStatuses) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3474 | // Add only default device cameras here, as virtual cameras currently don't support torch |
| 3475 | // anyway. Note that this is a simplification of the implementation here, and we should |
| 3476 | // change this when virtual cameras support torch. |
| 3477 | if (s.deviceId == kDefaultDeviceId) { |
| 3478 | idsChosenForCallback.insert(s.cameraId); |
| 3479 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3480 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3481 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3482 | /* |
| 3483 | * Immediately signal current torch status to this listener only |
| 3484 | * This may be a subset of all the devices, so don't include it in the response directly |
| 3485 | */ |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3486 | { |
| 3487 | Mutex::Autolock al(mTorchStatusMutex); |
| 3488 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3489 | const std::string &id = mTorchStatusMap.keyAt(i); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3490 | // The camera id is visible to the client. Fine to send torch |
| 3491 | // callback. |
| 3492 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3493 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id, |
| 3494 | kDefaultDeviceId); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3495 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3496 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3497 | } |
| 3498 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3499 | return Status::ok(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3500 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3501 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3502 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3503 | ATRACE_CALL(); |
| 3504 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3505 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 3506 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3507 | if (listener == 0) { |
| 3508 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3509 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3510 | } |
| 3511 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3512 | Mutex::Autolock lock(mServiceLock); |
| 3513 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3514 | { |
| 3515 | Mutex::Autolock lock(mStatusListenerLock); |
| 3516 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3517 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3518 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid(), /*closeCamera*/false); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3519 | IInterface::asBinder(listener)->unlinkToDeath(*it); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3520 | mListenerList.erase(it); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3521 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3522 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3523 | } |
| 3524 | } |
| 3525 | |
| 3526 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 3527 | __FUNCTION__, listener.get()); |
| 3528 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3529 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3530 | } |
| 3531 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3532 | Status CameraService::getLegacyParameters(int cameraId, /*out*/std::string* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3533 | |
| 3534 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3535 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 3536 | |
| 3537 | if (parameters == NULL) { |
| 3538 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3539 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3540 | } |
| 3541 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3542 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3543 | |
| 3544 | CameraParameters shimParams; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3545 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3546 | // Error logged by caller |
| 3547 | return ret; |
| 3548 | } |
| 3549 | |
| 3550 | String8 shimParamsString8 = shimParams.flatten(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3551 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3552 | *parameters = toStdString(shimParamsString8); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3553 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3554 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3555 | } |
| 3556 | |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 3557 | Status CameraService::supportsCameraApi(const std::string& cameraId, int apiVersion, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3558 | /*out*/ bool *isSupported) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3559 | ATRACE_CALL(); |
| 3560 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3561 | ALOGV("%s: for camera ID = %s", __FUNCTION__, cameraId.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3562 | |
| 3563 | switch (apiVersion) { |
| 3564 | case API_VERSION_1: |
| 3565 | case API_VERSION_2: |
| 3566 | break; |
| 3567 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3568 | std::string msg = fmt::sprintf("Unknown API version %d", apiVersion); |
| 3569 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3570 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3571 | } |
| 3572 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3573 | int portraitRotation; |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 3574 | auto deviceVersionAndTransport = |
| 3575 | getDeviceVersion(cameraId, |
| 3576 | /*rotationOverride*/hardware::ICameraService::ROTATION_OVERRIDE_NONE, |
| 3577 | &portraitRotation); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3578 | if (deviceVersionAndTransport.first == -1) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3579 | std::string msg = fmt::sprintf("Unknown camera ID %s", cameraId.c_str()); |
| 3580 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3581 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3582 | } |
| 3583 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 3584 | int deviceVersion = deviceVersionAndTransport.first; |
| 3585 | switch (deviceVersion) { |
| 3586 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 3587 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 3588 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 3589 | if (apiVersion == API_VERSION_2) { |
| 3590 | 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] | 3591 | "shim", __FUNCTION__, cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3592 | *isSupported = false; |
| 3593 | } else { // if (apiVersion == API_VERSION_1) { |
| 3594 | 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] | 3595 | "supported", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3596 | *isSupported = true; |
| 3597 | } |
| 3598 | break; |
| 3599 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 3600 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 3601 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 3602 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 3603 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 3604 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 3605 | 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] | 3606 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3607 | *isSupported = true; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3608 | break; |
| 3609 | default: { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3610 | std::string msg = fmt::sprintf("Unknown device version %x for device %s", |
| 3611 | deviceVersion, cameraId.c_str()); |
| 3612 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3613 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3614 | } |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3615 | } |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3616 | } else { |
| 3617 | *isSupported = true; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3618 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3619 | return Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3620 | } |
| 3621 | |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 3622 | Status CameraService::isHiddenPhysicalCamera(const std::string& cameraId, |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3623 | /*out*/ bool *isSupported) { |
| 3624 | ATRACE_CALL(); |
| 3625 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3626 | ALOGV("%s: for camera ID = %s", __FUNCTION__, cameraId.c_str()); |
| 3627 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(cameraId); |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3628 | |
| 3629 | return Status::ok(); |
| 3630 | } |
| 3631 | |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3632 | Status CameraService::injectCamera( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3633 | const std::string& packageName, const std::string& internalCamId, |
| 3634 | const std::string& externalCamId, |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3635 | const sp<ICameraInjectionCallback>& callback, |
| 3636 | /*out*/ |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3637 | sp<ICameraInjectionSession>* cameraInjectionSession) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3638 | ATRACE_CALL(); |
| 3639 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3640 | if (!checkCallingPermission(toString16(sCameraInjectExternalCameraPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3641 | const int pid = getCallingPid(); |
| 3642 | const int uid = getCallingUid(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3643 | ALOGE("Permission Denial: can't inject camera pid=%d, uid=%d", pid, uid); |
| 3644 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3645 | "Permission Denial: no permission to inject camera"); |
| 3646 | } |
| 3647 | |
| 3648 | // Do not allow any camera injection that injects or replaces a virtual camera. |
| 3649 | auto [deviceIdForInternalCamera, _] = |
| 3650 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(internalCamId); |
| 3651 | if (deviceIdForInternalCamera != kDefaultDeviceId) { |
| 3652 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED, |
| 3653 | "Cannot replace a virtual camera"); |
| 3654 | } |
| 3655 | [[maybe_unused]] auto [deviceIdForExternalCamera, unusedMappedCameraId] = |
| 3656 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(externalCamId); |
| 3657 | if (deviceIdForExternalCamera != kDefaultDeviceId) { |
| 3658 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED, |
| 3659 | "Cannot inject a virtual camera to replace an internal camera"); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3660 | } |
| 3661 | |
| 3662 | ALOGV( |
| 3663 | "%s: Package name = %s, Internal camera ID = %s, External camera ID = " |
| 3664 | "%s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3665 | __FUNCTION__, packageName.c_str(), |
| 3666 | internalCamId.c_str(), externalCamId.c_str()); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3667 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3668 | { |
| 3669 | Mutex::Autolock lock(mInjectionParametersLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3670 | mInjectionInternalCamId = internalCamId; |
| 3671 | mInjectionExternalCamId = externalCamId; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3672 | mInjectionStatusListener->addListener(callback); |
| 3673 | *cameraInjectionSession = new CameraInjectionSession(this); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3674 | status_t res = NO_ERROR; |
| 3675 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 3676 | // If the client already exists, we can directly connect to the camera device through the |
| 3677 | // client's injectCamera(), otherwise we need to wait until the client is established |
| 3678 | // (execute connectHelper()) before injecting the camera to the camera device. |
| 3679 | if (clientDescriptor != nullptr) { |
| 3680 | mInjectionInitPending = false; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3681 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 3682 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 3683 | if(res != OK) { |
| 3684 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 3685 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3686 | mInjectionExternalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3687 | } |
| 3688 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3689 | if (res != OK) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3690 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 3691 | } |
| 3692 | } else { |
| 3693 | mInjectionInitPending = true; |
| 3694 | } |
| 3695 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3696 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3697 | return binder::Status::ok(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3698 | } |
| 3699 | |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 3700 | Status CameraService::reportExtensionSessionStats( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3701 | const hardware::CameraExtensionSessionStats& stats, std::string* sessionKey /*out*/) { |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 3702 | ALOGV("%s: reported %s", __FUNCTION__, stats.toString().c_str()); |
| 3703 | *sessionKey = mCameraServiceProxyWrapper->updateExtensionStats(stats); |
| 3704 | return Status::ok(); |
| 3705 | } |
| 3706 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3707 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3708 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3709 | for (auto& i : mActiveClientManager.getAll()) { |
| 3710 | auto clientSp = i->getValue(); |
| 3711 | if (clientSp.get() == client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3712 | cacheClientTagDumpIfNeeded(client->mCameraIdStr, clientSp.get()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3713 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3714 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3715 | } |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3716 | updateAudioRestrictionLocked(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3717 | } |
| 3718 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3719 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3720 | bool ret = false; |
| 3721 | { |
| 3722 | // Acquire mServiceLock and prevent other clients from connecting |
| 3723 | std::unique_ptr<AutoConditionLock> lock = |
| 3724 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3725 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3726 | std::vector<sp<BasicClient>> evicted; |
| 3727 | for (auto& i : mActiveClientManager.getAll()) { |
| 3728 | auto clientSp = i->getValue(); |
| 3729 | if (clientSp.get() == nullptr) { |
| 3730 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3731 | mActiveClientManager.remove(i); |
| 3732 | continue; |
| 3733 | } |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 3734 | if (remote == clientSp->getRemote()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3735 | mActiveClientManager.remove(i); |
| 3736 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3737 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3738 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3739 | clientSp->notifyError( |
| 3740 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3741 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3742 | } |
| 3743 | } |
| 3744 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3745 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 3746 | // other clients from connecting in mServiceLockWrapper if held |
| 3747 | mServiceLock.unlock(); |
| 3748 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3749 | // Do not clear caller identity, remote caller should be client proccess |
| 3750 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3751 | for (auto& i : evicted) { |
| 3752 | if (i.get() != nullptr) { |
| 3753 | i->disconnect(); |
| 3754 | ret = true; |
| 3755 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3756 | } |
| 3757 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3758 | // Reacquire mServiceLock |
| 3759 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3760 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3761 | } // lock is destroyed, allow further connect calls |
| 3762 | |
| 3763 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3764 | } |
| 3765 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3766 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3767 | const std::string& cameraId) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3768 | std::shared_ptr<CameraState> state; |
| 3769 | { |
| 3770 | Mutex::Autolock lock(mCameraStatesLock); |
| 3771 | auto iter = mCameraStates.find(cameraId); |
| 3772 | if (iter != mCameraStates.end()) { |
| 3773 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3774 | } |
| 3775 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3776 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3777 | } |
| 3778 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3779 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const std::string& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3780 | // Remove from active clients list |
| 3781 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 3782 | if (clientDescriptorPtr == nullptr) { |
| 3783 | 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] | 3784 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3785 | return sp<BasicClient>{nullptr}; |
| 3786 | } |
| 3787 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3788 | sp<BasicClient> client = clientDescriptorPtr->getValue(); |
| 3789 | if (client.get() != nullptr) { |
| 3790 | cacheClientTagDumpIfNeeded(clientDescriptorPtr->getKey(), client.get()); |
| 3791 | } |
| 3792 | return client; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 3793 | } |
| 3794 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3795 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3796 | // Acquire mServiceLock and prevent other clients from connecting |
| 3797 | std::unique_ptr<AutoConditionLock> lock = |
| 3798 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 3799 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3800 | std::set<userid_t> newAllowedUsers; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3801 | for (size_t i = 0; i < newUserIds.size(); i++) { |
| 3802 | if (newUserIds[i] < 0) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3803 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3804 | __FUNCTION__, newUserIds[i]); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3805 | return; |
| 3806 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3807 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3808 | } |
| 3809 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3810 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3811 | if (newAllowedUsers == mAllowedUsers) { |
| 3812 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 3813 | return; |
| 3814 | } |
| 3815 | |
| 3816 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 3817 | |
| 3818 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3819 | |
| 3820 | // Current user has switched, evict all current clients. |
| 3821 | std::vector<sp<BasicClient>> evicted; |
| 3822 | for (auto& i : mActiveClientManager.getAll()) { |
| 3823 | auto clientSp = i->getValue(); |
| 3824 | |
| 3825 | if (clientSp.get() == nullptr) { |
| 3826 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3827 | continue; |
| 3828 | } |
| 3829 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3830 | // Don't evict clients that are still allowed. |
| 3831 | uid_t clientUid = clientSp->getClientUid(); |
| 3832 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 3833 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 3834 | continue; |
| 3835 | } |
| 3836 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3837 | evicted.push_back(clientSp); |
| 3838 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3839 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3840 | i->getKey().c_str()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3841 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3842 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3843 | logEvent(fmt::sprintf("EVICT device %s client held by package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3844 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3845 | " to user switch.", i->getKey().c_str(), |
| 3846 | clientSp->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3847 | i->getOwnerId(), i->getPriority().getScore(), |
| 3848 | i->getPriority().getState())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3849 | |
| 3850 | } |
| 3851 | |
| 3852 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 3853 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 3854 | mServiceLock.unlock(); |
| 3855 | |
| 3856 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3857 | int64_t token = clearCallingIdentity(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3858 | |
| 3859 | for (auto& i : evicted) { |
| 3860 | i->disconnect(); |
| 3861 | } |
| 3862 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3863 | restoreCallingIdentity(token); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3864 | |
| 3865 | // Reacquire mServiceLock |
| 3866 | mServiceLock.lock(); |
| 3867 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3868 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3869 | void CameraService::logEvent(const std::string &event) { |
| 3870 | std::string curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3871 | Mutex::Autolock l(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3872 | std::string msg = curTime + " : " + event; |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3873 | // For service error events, print the msg only once. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3874 | if (msg.find("SERVICE ERROR") != std::string::npos) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3875 | mEventLog.add(msg); |
| 3876 | } else if(sServiceErrorEventSet.find(msg) == sServiceErrorEventSet.end()) { |
| 3877 | // Error event not added to the dumpsys log before |
| 3878 | mEventLog.add(msg); |
| 3879 | sServiceErrorEventSet.insert(msg); |
| 3880 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3881 | } |
| 3882 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3883 | void CameraService::logDisconnected(const std::string &cameraId, int clientPid, |
| 3884 | const std::string &clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3885 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3886 | logEvent(fmt::sprintf("DISCONNECT device %s client for package %s (PID %d)", cameraId.c_str(), |
| 3887 | clientPackage.c_str(), clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3888 | } |
| 3889 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3890 | void CameraService::logDisconnectedOffline(const std::string &cameraId, int clientPid, |
| 3891 | const std::string &clientPackage) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3892 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3893 | logEvent(fmt::sprintf("DISCONNECT offline device %s client for package %s (PID %d)", |
| 3894 | cameraId.c_str(), clientPackage.c_str(), clientPid)); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3895 | } |
| 3896 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3897 | void CameraService::logConnected(const std::string &cameraId, int clientPid, |
| 3898 | const std::string &clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3899 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3900 | logEvent(fmt::sprintf("CONNECT device %s client for package %s (PID %d)", cameraId.c_str(), |
| 3901 | clientPackage.c_str(), clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3902 | } |
| 3903 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3904 | void CameraService::logConnectedOffline(const std::string &cameraId, int clientPid, |
| 3905 | const std::string &clientPackage) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3906 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3907 | logEvent(fmt::sprintf("CONNECT offline device %s client for package %s (PID %d)", |
| 3908 | cameraId.c_str(), clientPackage.c_str(), clientPid)); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3909 | } |
| 3910 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3911 | void CameraService::logRejected(const std::string &cameraId, int clientPid, |
| 3912 | const std::string &clientPackage, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3913 | // Log the client rejected |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3914 | logEvent(fmt::sprintf("REJECT device %s client for package %s (PID %d), reason: (%s)", |
| 3915 | cameraId.c_str(), clientPackage.c_str(), clientPid, reason.c_str())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3918 | void CameraService::logTorchEvent(const std::string &cameraId, const std::string &torchState, |
| 3919 | int clientPid) { |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3920 | // Log torch event |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3921 | logEvent(fmt::sprintf("Torch for camera id %s turned %s for client PID %d", cameraId.c_str(), |
| 3922 | torchState.c_str(), clientPid)); |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3923 | } |
| 3924 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3925 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 3926 | const std::set<userid_t>& newUserIds) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3927 | std::string newUsers = toString(newUserIds); |
| 3928 | std::string oldUsers = toString(oldUserIds); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3929 | if (oldUsers.size() == 0) { |
| 3930 | oldUsers = "<None>"; |
| 3931 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3932 | // Log the new and old users |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3933 | logEvent(fmt::sprintf("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s", |
| 3934 | oldUsers.c_str(), newUsers.c_str())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3935 | } |
| 3936 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3937 | void CameraService::logDeviceRemoved(const std::string &cameraId, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3938 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3939 | 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] | 3940 | } |
| 3941 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3942 | void CameraService::logDeviceAdded(const std::string &cameraId, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3943 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3944 | 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] | 3945 | } |
| 3946 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3947 | void CameraService::logClientDied(int clientPid, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3948 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3949 | 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] | 3950 | } |
| 3951 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3952 | void CameraService::logServiceError(const std::string &msg, int errorCode) { |
| 3953 | logEvent(fmt::sprintf("SERVICE ERROR: %s : %d (%s)", msg.c_str(), errorCode, |
| 3954 | strerror(-errorCode))); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3955 | } |
| 3956 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3957 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 3958 | uint32_t flags) { |
| 3959 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3960 | // Permission checks |
| 3961 | switch (code) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3962 | case SHELL_COMMAND_TRANSACTION: { |
| 3963 | int in = data.readFileDescriptor(); |
| 3964 | int out = data.readFileDescriptor(); |
| 3965 | int err = data.readFileDescriptor(); |
| 3966 | int argc = data.readInt32(); |
| 3967 | Vector<String16> args; |
| 3968 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 3969 | args.add(data.readString16()); |
| 3970 | } |
| 3971 | sp<IBinder> unusedCallback; |
| 3972 | sp<IResultReceiver> resultReceiver; |
| 3973 | status_t status; |
| 3974 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 3975 | return status; |
| 3976 | } |
| 3977 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 3978 | return status; |
| 3979 | } |
| 3980 | status = shellCommand(in, out, err, args); |
| 3981 | if (resultReceiver != nullptr) { |
| 3982 | resultReceiver->send(status); |
| 3983 | } |
| 3984 | return NO_ERROR; |
| 3985 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3986 | } |
| 3987 | |
| 3988 | return BnCameraService::onTransact(code, data, reply, flags); |
| 3989 | } |
| 3990 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3991 | // We share the media players for shutter and recording sound for all clients. |
| 3992 | // A reference count is kept to determine when we will actually release the |
| 3993 | // media players. |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3994 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { |
| 3995 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 3996 | status_t error; |
| 3997 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 3998 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3999 | error = mp->prepare(); |
| 4000 | } |
| 4001 | if (error != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4002 | ALOGE("Failed to load CameraService sounds: %s", file); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 4003 | mp->disconnect(); |
| 4004 | mp.clear(); |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4005 | return nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4006 | } |
| 4007 | return mp; |
| 4008 | } |
| 4009 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4010 | void CameraService::increaseSoundRef() { |
| 4011 | Mutex::Autolock lock(mSoundLock); |
| 4012 | mSoundRef++; |
| 4013 | } |
| 4014 | |
| 4015 | void CameraService::loadSoundLocked(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4016 | ATRACE_CALL(); |
| 4017 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4018 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); |
| 4019 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { |
| 4020 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); |
| 4021 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { |
| 4022 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 4023 | } |
| 4024 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) { |
| 4025 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); |
| 4026 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { |
| 4027 | mSoundPlayer[SOUND_RECORDING_START] = |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4028 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4029 | } |
| 4030 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { |
| 4031 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); |
| 4032 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { |
| 4033 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
| 4034 | } |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4035 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4036 | } |
| 4037 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4038 | void CameraService::decreaseSoundRef() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4039 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4040 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4041 | if (--mSoundRef) return; |
| 4042 | |
| 4043 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 4044 | if (mSoundPlayer[i] != 0) { |
| 4045 | mSoundPlayer[i]->disconnect(); |
| 4046 | mSoundPlayer[i].clear(); |
| 4047 | } |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4052 | ATRACE_CALL(); |
| 4053 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4054 | LOG1("playSound(%d)", kind); |
Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame] | 4055 | if (kind < 0 || kind >= NUM_SOUNDS) { |
| 4056 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); |
| 4057 | return; |
| 4058 | } |
| 4059 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4060 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4061 | loadSoundLocked(kind); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4062 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 4063 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 4064 | player->seekTo(0); |
| 4065 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4066 | } |
| 4067 | } |
| 4068 | |
| 4069 | // ---------------------------------------------------------------------------- |
| 4070 | |
| 4071 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 4072 | const sp<ICameraClient>& cameraClient, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4073 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4074 | const std::string& clientPackageName, bool systemNativeClient, |
| 4075 | const std::optional<std::string>& clientFeatureId, |
| 4076 | const std::string& cameraIdStr, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4077 | int api1CameraId, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4078 | int clientPid, uid_t clientUid, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 4079 | int servicePid, int rotationOverride) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 4080 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 4081 | IInterface::asBinder(cameraClient), |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4082 | attributionAndPermissionUtils, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4083 | clientPackageName, systemNativeClient, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4084 | cameraIdStr, cameraFacing, sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4085 | clientPid, clientUid, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 4086 | servicePid, rotationOverride), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4087 | mCameraId(api1CameraId) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4088 | { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4089 | int callingPid = getCallingPid(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4090 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4091 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 4092 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4093 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4094 | cameraService->increaseSoundRef(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4095 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4096 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4097 | } |
| 4098 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4099 | // tear down the client |
| 4100 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4101 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4102 | mDestructionStarted = true; |
| 4103 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4104 | sCameraService->decreaseSoundRef(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 4105 | // unconditionally disconnect. function is idempotent |
| 4106 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4107 | } |
| 4108 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4109 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; |
| 4110 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4111 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4112 | const sp<IBinder>& remoteCallback, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4113 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4114 | const std::string& clientPackageName, bool nativeClient, |
| 4115 | const std::optional<std::string>& clientFeatureId, const std::string& cameraIdStr, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4116 | int cameraFacing, int sensorOrientation, int clientPid, uid_t clientUid, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 4117 | int servicePid, int rotationOverride): |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4118 | AttributionAndPermissionUtilsEncapsulator(attributionAndPermissionUtils), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4119 | mDestructionStarted(false), |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4120 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), mOrientation(sensorOrientation), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4121 | mClientPackageName(clientPackageName), mSystemNativeClient(nativeClient), |
| 4122 | mClientFeatureId(clientFeatureId), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 4123 | mClientPid(clientPid), mClientUid(clientUid), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4124 | mServicePid(servicePid), |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 4125 | mDisconnected(false), mUidIsTrusted(false), |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 4126 | mRotationOverride(rotationOverride), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4127 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4128 | mRemoteBinder(remoteCallback), |
| 4129 | mOpsActive(false), |
| 4130 | mOpsStreaming(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4131 | { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4132 | if (sCameraService == nullptr) { |
| 4133 | sCameraService = cameraService; |
| 4134 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 4135 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4136 | // There are 2 scenarios in which a client won't have AppOps operations |
| 4137 | // (both scenarios : native clients) |
| 4138 | // 1) It's an system native client*, the package name will be empty |
| 4139 | // and it will return from this function in the previous if condition |
| 4140 | // (This is the same as the previously existing behavior). |
| 4141 | // 2) It is a system native client, but its package name has been |
| 4142 | // modified for debugging, however it still must not use AppOps since |
| 4143 | // the package name is not a real one. |
| 4144 | // |
| 4145 | // * system native client - native client with UID < AID_APP_START. It |
| 4146 | // doesn't exclude clients not on the system partition. |
| 4147 | if (!mSystemNativeClient) { |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4148 | mAppOpsManager = std::make_unique<AppOpsManager>(); |
| 4149 | } |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 4150 | |
Charles Chen | f075f08 | 2024-03-04 23:32:55 +0000 | [diff] [blame] | 4151 | mUidIsTrusted = isTrustedCallingUid(mClientUid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4155 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4156 | mDestructionStarted = true; |
| 4157 | } |
| 4158 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4159 | binder::Status CameraService::BasicClient::disconnect() { |
| 4160 | binder::Status res = Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4161 | if (mDisconnected) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4162 | return res; |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4163 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 4164 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4165 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4166 | sCameraService->removeByClient(this); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4167 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, mClientPackageName); |
Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 4168 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4169 | mCameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4170 | |
| 4171 | sp<IBinder> remote = getRemote(); |
| 4172 | if (remote != nullptr) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4173 | remote->unlinkToDeath(sCameraService); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4174 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4175 | |
| 4176 | finishCameraOps(); |
Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 4177 | // Notify flashlight that a camera device is closed. |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4178 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4179 | 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] | 4180 | mClientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4181 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4182 | // client shouldn't be able to call into us anymore |
| 4183 | mClientPid = 0; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4184 | |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 4185 | const auto& mActivityManager = getActivityManager(); |
| 4186 | if (mActivityManager) { |
| 4187 | mActivityManager->logFgsApiEnd(LOG_FGS_CAMERA_API, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4188 | getCallingUid(), |
| 4189 | getCallingPid()); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4192 | return res; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4193 | } |
| 4194 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 4195 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 4196 | // No dumping of clients directly over Binder, |
| 4197 | // must go through CameraService::dump |
| 4198 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4199 | getCallingUid(), NULL, 0); |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 4200 | return OK; |
| 4201 | } |
| 4202 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4203 | status_t CameraService::BasicClient::startWatchingTags(const std::string&, int) { |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4204 | // Can't watch tags directly, must go through CameraService::startWatchingTags |
| 4205 | return OK; |
| 4206 | } |
| 4207 | |
| 4208 | status_t CameraService::BasicClient::stopWatchingTags(int) { |
| 4209 | // Can't watch tags directly, must go through CameraService::stopWatchingTags |
| 4210 | return OK; |
| 4211 | } |
| 4212 | |
| 4213 | status_t CameraService::BasicClient::dumpWatchedEventsToVector(std::vector<std::string> &) { |
| 4214 | // Can't watch tags directly, must go through CameraService::dumpWatchedEventsToVector |
| 4215 | return OK; |
| 4216 | } |
| 4217 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4218 | std::string CameraService::BasicClient::getPackageName() const { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 4219 | return mClientPackageName; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4220 | } |
| 4221 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4222 | int CameraService::BasicClient::getCameraFacing() const { |
| 4223 | return mCameraFacing; |
| 4224 | } |
| 4225 | |
| 4226 | int CameraService::BasicClient::getCameraOrientation() const { |
| 4227 | return mOrientation; |
| 4228 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4229 | |
| 4230 | int CameraService::BasicClient::getClientPid() const { |
| 4231 | return mClientPid; |
| 4232 | } |
| 4233 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4234 | uid_t CameraService::BasicClient::getClientUid() const { |
| 4235 | return mClientUid; |
| 4236 | } |
| 4237 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 4238 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 4239 | // Defaults to API2. |
| 4240 | return level == API_2; |
| 4241 | } |
| 4242 | |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 4243 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4244 | { |
| 4245 | Mutex::Autolock l(mAudioRestrictionLock); |
| 4246 | mAudioRestriction = mode; |
| 4247 | } |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 4248 | sCameraService->updateAudioRestriction(); |
| 4249 | return OK; |
| 4250 | } |
| 4251 | |
| 4252 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4253 | return sCameraService->updateAudioRestriction(); |
| 4254 | } |
| 4255 | |
| 4256 | int32_t CameraService::BasicClient::getAudioRestriction() const { |
| 4257 | Mutex::Autolock l(mAudioRestrictionLock); |
| 4258 | return mAudioRestriction; |
| 4259 | } |
| 4260 | |
| 4261 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { |
| 4262 | switch (mode) { |
| 4263 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: |
| 4264 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: |
| 4265 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: |
| 4266 | return true; |
| 4267 | default: |
| 4268 | return false; |
| 4269 | } |
| 4270 | } |
| 4271 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4272 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { |
| 4273 | if (mode == AppOpsManager::MODE_ERRORED) { |
| 4274 | ALOGI("Camera %s: Access for \"%s\" has been revoked", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4275 | mCameraIdStr.c_str(), mClientPackageName.c_str()); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4276 | return PERMISSION_DENIED; |
| 4277 | } else if (!mUidIsTrusted && mode == AppOpsManager::MODE_IGNORED) { |
| 4278 | // If the calling Uid is trusted (a native service), the AppOpsManager could |
| 4279 | // return MODE_IGNORED. Do not treat such case as error. |
| 4280 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, |
| 4281 | mClientPackageName); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4282 | |
| 4283 | bool isCameraPrivacyEnabled; |
| 4284 | if (flags::camera_privacy_allowlist()) { |
| 4285 | isCameraPrivacyEnabled = sCameraService->isCameraPrivacyEnabled( |
| 4286 | toString16(mClientPackageName), std::string(), mClientPid, mClientUid); |
| 4287 | } else { |
| 4288 | isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4289 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4290 | } |
Jyoti Bhayana | 8143e57 | 2023-01-09 08:46:49 -0800 | [diff] [blame] | 4291 | // We don't want to return EACCESS if the CameraPrivacy is enabled. |
| 4292 | // We prefer to successfully open the camera and perform camera muting |
| 4293 | // or blocking in connectHelper as handleAppOpMode can be called before the |
| 4294 | // connection has been fully established and at that time camera muting |
| 4295 | // capabilities are unknown. |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4296 | if (!isUidActive || !isCameraPrivacyEnabled) { |
Austin Borger | fd05d98 | 2024-04-22 15:54:50 -0700 | [diff] [blame] | 4297 | ALOGI("Camera %s: Access for \"%s\" has been restricted." |
| 4298 | "uid active: %s, privacy enabled: %s", mCameraIdStr.c_str(), |
| 4299 | mClientPackageName.c_str(), isUidActive ? "true" : "false", |
| 4300 | isCameraPrivacyEnabled ? "true" : "false"); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4301 | // Return the same error as for device policy manager rejection |
| 4302 | return -EACCES; |
| 4303 | } |
| 4304 | } |
| 4305 | return OK; |
| 4306 | } |
| 4307 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4308 | status_t CameraService::BasicClient::startCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4309 | ATRACE_CALL(); |
| 4310 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4311 | { |
| 4312 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4313 | __FUNCTION__, mClientPackageName.c_str(), mClientUid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4314 | } |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4315 | if (mAppOpsManager != nullptr) { |
| 4316 | // Notify app ops that the camera is not available |
| 4317 | mOpsCallback = new OpsCallback(this); |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4318 | |
| 4319 | if (flags::watch_foreground_changes()) { |
| 4320 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 4321 | toString16(mClientPackageName), |
| 4322 | AppOpsManager::WATCH_FOREGROUND_CHANGES, mOpsCallback); |
| 4323 | } else { |
| 4324 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4325 | toString16(mClientPackageName), mOpsCallback); |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4326 | } |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4327 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4328 | // Just check for camera acccess here on open - delay startOp until |
| 4329 | // camera frames start streaming in startCameraStreamingOps |
| 4330 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4331 | toString16(mClientPackageName)); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4332 | status_t res = handleAppOpMode(mode); |
| 4333 | if (res != OK) { |
| 4334 | return res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4335 | } |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 4336 | } |
| 4337 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4338 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4339 | |
| 4340 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4341 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4342 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4343 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid, /*openCamera*/true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4344 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4345 | // Notify listeners of camera open/close status |
| 4346 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); |
| 4347 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4348 | return OK; |
| 4349 | } |
| 4350 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4351 | status_t CameraService::BasicClient::startCameraStreamingOps() { |
| 4352 | ATRACE_CALL(); |
| 4353 | |
| 4354 | if (!mOpsActive) { |
| 4355 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 4356 | return INVALID_OPERATION; |
| 4357 | } |
| 4358 | if (mOpsStreaming) { |
| 4359 | ALOGV("%s: Streaming already active!", __FUNCTION__); |
| 4360 | return OK; |
| 4361 | } |
| 4362 | |
| 4363 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4364 | __FUNCTION__, mClientPackageName.c_str(), mClientUid); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4365 | |
| 4366 | if (mAppOpsManager != nullptr) { |
| 4367 | int32_t mode = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4368 | toString16(mClientPackageName), /*startIfModeDefault*/ false, |
| 4369 | toString16(mClientFeatureId), |
| 4370 | toString16("start camera ") + toString16(mCameraIdStr)); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4371 | status_t res = handleAppOpMode(mode); |
| 4372 | if (res != OK) { |
| 4373 | return res; |
| 4374 | } |
| 4375 | } |
| 4376 | |
| 4377 | mOpsStreaming = true; |
| 4378 | |
| 4379 | return OK; |
| 4380 | } |
| 4381 | |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4382 | status_t CameraService::BasicClient::noteAppOp() { |
| 4383 | ATRACE_CALL(); |
| 4384 | |
| 4385 | ALOGV("%s: Start camera noteAppOp, package name = %s, client UID = %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4386 | __FUNCTION__, mClientPackageName.c_str(), mClientUid); |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4387 | |
| 4388 | // noteAppOp is only used for when camera mute is not supported, in order |
| 4389 | // to trigger the sensor privacy "Unblock" dialog |
| 4390 | if (mAppOpsManager != nullptr) { |
| 4391 | int32_t mode = mAppOpsManager->noteOp(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4392 | toString16(mClientPackageName), toString16(mClientFeatureId), |
| 4393 | toString16("start camera ") + toString16(mCameraIdStr)); |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4394 | status_t res = handleAppOpMode(mode); |
| 4395 | if (res != OK) { |
| 4396 | return res; |
| 4397 | } |
| 4398 | } |
| 4399 | |
| 4400 | return OK; |
| 4401 | } |
| 4402 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4403 | status_t CameraService::BasicClient::finishCameraStreamingOps() { |
| 4404 | ATRACE_CALL(); |
| 4405 | |
| 4406 | if (!mOpsActive) { |
| 4407 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 4408 | return INVALID_OPERATION; |
| 4409 | } |
| 4410 | if (!mOpsStreaming) { |
| 4411 | ALOGV("%s: Streaming not active!", __FUNCTION__); |
| 4412 | return OK; |
| 4413 | } |
| 4414 | |
| 4415 | if (mAppOpsManager != nullptr) { |
| 4416 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4417 | toString16(mClientPackageName), toString16(mClientFeatureId)); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4418 | mOpsStreaming = false; |
| 4419 | } |
| 4420 | |
| 4421 | return OK; |
| 4422 | } |
| 4423 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4424 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4425 | ATRACE_CALL(); |
| 4426 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4427 | if (mOpsStreaming) { |
| 4428 | // Make sure we've notified everyone about camera stopping |
| 4429 | finishCameraStreamingOps(); |
| 4430 | } |
| 4431 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4432 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4433 | if (mOpsActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4434 | mOpsActive = false; |
| 4435 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 4436 | // This function is called when a client disconnects. This should |
| 4437 | // release the camera, but actually only if it was in a proper |
| 4438 | // functional state, i.e. with status NOT_AVAILABLE |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4439 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 4440 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4441 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4442 | // 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] | 4443 | sCameraService->updateStatus(StatusInternal::PRESENT, |
| 4444 | mCameraIdStr, rejected); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4445 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4446 | // Always stop watching, even if no camera op is active |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4447 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 4448 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 4449 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4450 | mOpsCallback.clear(); |
| 4451 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4452 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid, /*closeCamera*/true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4453 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4454 | // Notify listeners of camera open/close status |
| 4455 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); |
| 4456 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4457 | return OK; |
| 4458 | } |
| 4459 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4460 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4461 | ATRACE_CALL(); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4462 | if (mAppOpsManager == nullptr) { |
| 4463 | return; |
| 4464 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 4465 | // TODO : add offline camera session case |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4466 | if (op != AppOpsManager::OP_CAMERA) { |
| 4467 | ALOGW("Unexpected app ops notification received: %d", op); |
| 4468 | return; |
| 4469 | } |
| 4470 | |
| 4471 | int32_t res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4472 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4473 | mClientUid, toString16(mClientPackageName)); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4474 | ALOGV("checkOp returns: %d, %s ", res, |
| 4475 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 4476 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 4477 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 4478 | "UNKNOWN"); |
| 4479 | |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4480 | if (res == AppOpsManager::MODE_ERRORED) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4481 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.c_str(), |
| 4482 | mClientPackageName.c_str()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4483 | block(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4484 | } else if (res == AppOpsManager::MODE_IGNORED) { |
| 4485 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4486 | |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4487 | // Uid may be active, but not visible to the user (e.g. PROCESS_STATE_FOREGROUND_SERVICE). |
| 4488 | // If not visible, but still active, then we want to block instead of muting the camera. |
| 4489 | int32_t procState = sCameraService->mUidPolicy->getProcState(mClientUid); |
| 4490 | bool isUidVisible = (procState <= ActivityManager::PROCESS_STATE_BOUND_TOP); |
| 4491 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4492 | bool isCameraPrivacyEnabled; |
| 4493 | if (flags::camera_privacy_allowlist()) { |
| 4494 | isCameraPrivacyEnabled = sCameraService->isCameraPrivacyEnabled( |
| 4495 | toString16(mClientPackageName),std::string(),mClientPid,mClientUid); |
| 4496 | } else { |
| 4497 | isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4498 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | 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] | 4502 | " isUidVisible %d, isCameraPrivacyEnabled %d", mCameraIdStr.c_str(), |
| 4503 | mClientPackageName.c_str(), mUidIsTrusted, isUidActive, isUidVisible, |
| 4504 | isCameraPrivacyEnabled); |
| 4505 | // If the calling Uid is trusted (a native service), or the client Uid is active / visible |
| 4506 | // (WAR for b/175320666)the AppOpsManager could return MODE_IGNORED. Do not treat such |
| 4507 | // cases as error. |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4508 | if (!mUidIsTrusted) { |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4509 | if (flags::watch_foreground_changes()) { |
| 4510 | if (isUidVisible && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 4511 | setCameraMute(true); |
| 4512 | } else { |
| 4513 | block(); |
| 4514 | } |
| 4515 | } else { |
| 4516 | if (isUidActive && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 4517 | setCameraMute(true); |
| 4518 | } else if (!isUidActive |
| 4519 | || (isCameraPrivacyEnabled && !supportsCameraMute())) { |
| 4520 | block(); |
| 4521 | } |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4522 | } |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4523 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4524 | } else if (res == AppOpsManager::MODE_ALLOWED) { |
| 4525 | setCameraMute(sCameraService->mOverrideCameraMuteMode); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4526 | } |
| 4527 | } |
| 4528 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4529 | void CameraService::BasicClient::block() { |
| 4530 | ATRACE_CALL(); |
| 4531 | |
| 4532 | // Reset the client PID to allow server-initiated disconnect, |
| 4533 | // and to prevent further calls by client. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4534 | mClientPid = getCallingPid(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4535 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 4536 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); |
| 4537 | disconnect(); |
| 4538 | } |
| 4539 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4540 | // ---------------------------------------------------------------------------- |
| 4541 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4542 | void CameraService::Client::notifyError(int32_t errorCode, |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 4543 | [[maybe_unused]] const CaptureResultExtras& resultExtras) { |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 4544 | if (mRemoteCallback != NULL) { |
Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 4545 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; |
| 4546 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { |
| 4547 | api1ErrorCode = CAMERA_ERROR_DISABLED; |
| 4548 | } |
| 4549 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 4550 | } else { |
| 4551 | ALOGE("mRemoteCallback is NULL!!"); |
| 4552 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4553 | } |
| 4554 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 4555 | // NOTE: function is idempotent |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4556 | binder::Status CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4557 | ALOGV("Client::disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4558 | return BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 4559 | } |
| 4560 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 4561 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 4562 | return level == API_1; |
| 4563 | } |
| 4564 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4565 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 4566 | mClient(client) { |
| 4567 | } |
| 4568 | |
| 4569 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 4570 | const String16& packageName) { |
| 4571 | sp<BasicClient> client = mClient.promote(); |
| 4572 | if (client != NULL) { |
| 4573 | client->opChanged(op, packageName); |
| 4574 | } |
| 4575 | } |
| 4576 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4577 | // ---------------------------------------------------------------------------- |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4578 | // UidPolicy |
| 4579 | // ---------------------------------------------------------------------------- |
| 4580 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4581 | void CameraService::UidPolicy::registerWithActivityManager() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4582 | Mutex::Autolock _l(mUidLock); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4583 | int32_t emptyUidArray[] = { }; |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4584 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4585 | if (mRegistered) return; |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 4586 | status_t res = mAm.linkToDeath(this); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4587 | mAm.registerUidObserverForUids(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4588 | | ActivityManager::UID_OBSERVER_IDLE |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4589 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE |
| 4590 | | ActivityManager::UID_OBSERVER_PROC_OOM_ADJ, |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4591 | ActivityManager::PROCESS_STATE_UNKNOWN, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4592 | toString16(kServiceName), emptyUidArray, 0, mObserverToken); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4593 | if (res == OK) { |
| 4594 | mRegistered = true; |
| 4595 | ALOGV("UidPolicy: Registered with ActivityManager"); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4596 | } else { |
| 4597 | ALOGE("UidPolicy: Failed to register with ActivityManager: 0x%08x", res); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4598 | } |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4599 | } |
| 4600 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4601 | void CameraService::UidPolicy::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4602 | if (name != toString16(kActivityServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4603 | return; |
| 4604 | } |
| 4605 | |
| 4606 | registerWithActivityManager(); |
| 4607 | } |
| 4608 | |
| 4609 | void CameraService::UidPolicy::registerSelf() { |
| 4610 | // Use check service to see if the activity service is available |
| 4611 | // If not available then register for notifications, instead of blocking |
| 4612 | // till the service is ready |
| 4613 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4614 | sp<IBinder> binder = sm->checkService(toString16(kActivityServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4615 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4616 | sm->registerForNotifications(toString16(kActivityServiceName), this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4617 | } else { |
| 4618 | registerWithActivityManager(); |
| 4619 | } |
| 4620 | } |
| 4621 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4622 | void CameraService::UidPolicy::unregisterSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4623 | Mutex::Autolock _l(mUidLock); |
| 4624 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 4625 | mAm.unregisterUidObserver(this); |
| 4626 | mAm.unlinkToDeath(this); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4627 | mRegistered = false; |
| 4628 | mActiveUids.clear(); |
| 4629 | ALOGV("UidPolicy: Unregistered with ActivityManager"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { |
| 4633 | onUidIdle(uid, disabled); |
| 4634 | } |
| 4635 | |
| 4636 | void CameraService::UidPolicy::onUidActive(uid_t uid) { |
| 4637 | Mutex::Autolock _l(mUidLock); |
| 4638 | mActiveUids.insert(uid); |
| 4639 | } |
| 4640 | |
| 4641 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { |
| 4642 | bool deleted = false; |
| 4643 | { |
| 4644 | Mutex::Autolock _l(mUidLock); |
| 4645 | if (mActiveUids.erase(uid) > 0) { |
| 4646 | deleted = true; |
| 4647 | } |
| 4648 | } |
| 4649 | if (deleted) { |
| 4650 | sp<CameraService> service = mService.promote(); |
| 4651 | if (service != nullptr) { |
| 4652 | service->blockClientsForUid(uid); |
| 4653 | } |
| 4654 | } |
| 4655 | } |
| 4656 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4657 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 4658 | int64_t procStateSeq __unused, int32_t capability __unused) { |
Austin Borger | c5585dc | 2022-05-12 00:48:17 +0000 | [diff] [blame] | 4659 | bool procStateChange = false; |
| 4660 | { |
| 4661 | Mutex::Autolock _l(mUidLock); |
| 4662 | if (mMonitoredUids.find(uid) != mMonitoredUids.end() && |
| 4663 | mMonitoredUids[uid].procState != procState) { |
| 4664 | mMonitoredUids[uid].procState = procState; |
| 4665 | procStateChange = true; |
| 4666 | } |
| 4667 | } |
| 4668 | |
| 4669 | if (procStateChange) { |
| 4670 | sp<CameraService> service = mService.promote(); |
| 4671 | if (service != nullptr) { |
| 4672 | service->notifyMonitoredUids(); |
| 4673 | } |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4674 | } |
| 4675 | } |
| 4676 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4677 | /** |
| 4678 | * When the OOM adj of the uid owning the camera changes, a different uid waiting on camera |
| 4679 | * privileges may take precedence if the owner's new OOM adj is greater than the waiting package. |
| 4680 | * Here, we track which monitoredUid has the camera, and track its adj relative to other |
| 4681 | * monitoredUids. If it is revised above some other monitoredUid, signal |
| 4682 | * onCameraAccessPrioritiesChanged. This only needs to capture the case where there are two |
| 4683 | * foreground apps in split screen - state changes will capture all other cases. |
| 4684 | */ |
| 4685 | void CameraService::UidPolicy::onUidProcAdjChanged(uid_t uid, int32_t adj) { |
| 4686 | std::unordered_set<uid_t> notifyUidSet; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4687 | { |
| 4688 | Mutex::Autolock _l(mUidLock); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4689 | auto it = mMonitoredUids.find(uid); |
| 4690 | |
| 4691 | if (it != mMonitoredUids.end()) { |
| 4692 | if (it->second.hasCamera) { |
| 4693 | for (auto &monitoredUid : mMonitoredUids) { |
| 4694 | if (monitoredUid.first != uid && adj > monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4695 | ALOGV("%s: notify uid %d", __FUNCTION__, monitoredUid.first); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4696 | notifyUidSet.emplace(monitoredUid.first); |
| 4697 | } |
| 4698 | } |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4699 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4700 | notifyUidSet.emplace(uid); |
| 4701 | } else { |
| 4702 | for (auto &monitoredUid : mMonitoredUids) { |
| 4703 | if (monitoredUid.second.hasCamera && adj < monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4704 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4705 | notifyUidSet.emplace(uid); |
| 4706 | } |
| 4707 | } |
| 4708 | } |
| 4709 | it->second.procAdj = adj; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4710 | } |
| 4711 | } |
| 4712 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4713 | if (notifyUidSet.size() > 0) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4714 | sp<CameraService> service = mService.promote(); |
| 4715 | if (service != nullptr) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4716 | service->notifyMonitoredUids(notifyUidSet); |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4717 | } |
| 4718 | } |
| 4719 | } |
| 4720 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4721 | /** |
| 4722 | * Register a uid for monitoring, and note whether it owns a camera. |
| 4723 | */ |
| 4724 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid, bool openCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4725 | Mutex::Autolock _l(mUidLock); |
| 4726 | auto it = mMonitoredUids.find(uid); |
| 4727 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4728 | it->second.refCount++; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4729 | } else { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4730 | MonitoredUid monitoredUid; |
| 4731 | monitoredUid.procState = ActivityManager::PROCESS_STATE_NONEXISTENT; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4732 | monitoredUid.procAdj = resource_policy::UNKNOWN_ADJ; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4733 | monitoredUid.refCount = 1; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4734 | it = mMonitoredUids.emplace(std::pair<uid_t, MonitoredUid>(uid, monitoredUid)).first; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4735 | status_t res = mAm.addUidToObserver(mObserverToken, toString16(kServiceName), uid); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4736 | if (res != OK) { |
| 4737 | ALOGE("UidPolicy: Failed to add uid to observer: 0x%08x", res); |
| 4738 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | if (openCamera) { |
| 4742 | it->second.hasCamera = true; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4743 | } |
| 4744 | } |
| 4745 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4746 | /** |
| 4747 | * Unregister a uid for monitoring, and note whether it lost ownership of a camera. |
| 4748 | */ |
| 4749 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid, bool closeCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4750 | Mutex::Autolock _l(mUidLock); |
| 4751 | auto it = mMonitoredUids.find(uid); |
| 4752 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4753 | it->second.refCount--; |
| 4754 | if (it->second.refCount == 0) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4755 | mMonitoredUids.erase(it); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4756 | status_t res = mAm.removeUidFromObserver(mObserverToken, toString16(kServiceName), uid); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4757 | if (res != OK) { |
| 4758 | ALOGE("UidPolicy: Failed to remove uid from observer: 0x%08x", res); |
| 4759 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4760 | } else if (closeCamera) { |
| 4761 | it->second.hasCamera = false; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4762 | } |
| 4763 | } else { |
| 4764 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 4765 | } |
| 4766 | } |
| 4767 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4768 | bool CameraService::UidPolicy::isUidActive(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4769 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4770 | return isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4771 | } |
| 4772 | |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4773 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; |
| 4774 | static const int64_t kPollUidActiveTimeoutMillis = 50; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4775 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4776 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4777 | // Non-app UIDs are considered always active |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4778 | // If activity manager is unreachable, assume everything is active |
| 4779 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4780 | return true; |
| 4781 | } |
| 4782 | auto it = mOverrideUids.find(uid); |
| 4783 | if (it != mOverrideUids.end()) { |
| 4784 | return it->second; |
| 4785 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4786 | bool active = mActiveUids.find(uid) != mActiveUids.end(); |
| 4787 | if (!active) { |
| 4788 | // We want active UIDs to always access camera with their first attempt since |
| 4789 | // there is no guarantee the app is robustly written and would retry getting |
| 4790 | // the camera on failure. The inverse case is not a problem as we would take |
| 4791 | // camera away soon once we get the callback that the uid is no longer active. |
| 4792 | ActivityManager am; |
| 4793 | // Okay to access with a lock held as UID changes are dispatched without |
| 4794 | // a lock and we are a higher level component. |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4795 | int64_t startTimeMillis = 0; |
| 4796 | do { |
| 4797 | // TODO: Fix this b/109950150! |
| 4798 | // Okay this is a hack. There is a race between the UID turning active and |
| 4799 | // activity being resumed. The proper fix is very risky, so we temporary add |
| 4800 | // some polling which should happen pretty rarely anyway as the race is hard |
| 4801 | // to hit. |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4802 | active = mActiveUids.find(uid) != mActiveUids.end(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4803 | if (!active) active = am.isUidActive(uid, toString16(callingPackage)); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4804 | if (active) { |
| 4805 | break; |
| 4806 | } |
| 4807 | if (startTimeMillis <= 0) { |
| 4808 | startTimeMillis = uptimeMillis(); |
| 4809 | } |
| 4810 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4811 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4812 | if (remainingTimeMillis <= 0) { |
| 4813 | break; |
| 4814 | } |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4815 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); |
| 4816 | |
| 4817 | mUidLock.unlock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4818 | usleep(remainingTimeMillis * 1000); |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4819 | mUidLock.lock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4820 | } while (true); |
| 4821 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4822 | if (active) { |
| 4823 | // Now that we found out the UID is actually active, cache that |
| 4824 | mActiveUids.insert(uid); |
| 4825 | } |
| 4826 | } |
| 4827 | return active; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4828 | } |
| 4829 | |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4830 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { |
| 4831 | Mutex::Autolock _l(mUidLock); |
| 4832 | return getProcStateLocked(uid); |
| 4833 | } |
| 4834 | |
| 4835 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { |
| 4836 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 4837 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4838 | procState = mMonitoredUids[uid].procState; |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4839 | } |
| 4840 | return procState; |
| 4841 | } |
| 4842 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4843 | void CameraService::UidPolicy::addOverrideUid(uid_t uid, |
| 4844 | const std::string &callingPackage, bool active) { |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4845 | updateOverrideUid(uid, callingPackage, active, true); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4846 | } |
| 4847 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4848 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4849 | updateOverrideUid(uid, callingPackage, false, false); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4850 | } |
| 4851 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4852 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 4853 | Mutex::Autolock _l(mUidLock); |
| 4854 | ALOGV("UidPolicy: ActivityManager has died"); |
| 4855 | mRegistered = false; |
| 4856 | mActiveUids.clear(); |
| 4857 | } |
| 4858 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4859 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, const std::string &callingPackage, |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4860 | bool active, bool insert) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4861 | bool wasActive = false; |
| 4862 | bool isActive = false; |
| 4863 | { |
| 4864 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4865 | wasActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4866 | mOverrideUids.erase(uid); |
| 4867 | if (insert) { |
| 4868 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 4869 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4870 | isActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4871 | } |
| 4872 | if (wasActive != isActive && !isActive) { |
| 4873 | sp<CameraService> service = mService.promote(); |
| 4874 | if (service != nullptr) { |
| 4875 | service->blockClientsForUid(uid); |
| 4876 | } |
| 4877 | } |
| 4878 | } |
| 4879 | |
| 4880 | // ---------------------------------------------------------------------------- |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4881 | // SensorPrivacyPolicy |
| 4882 | // ---------------------------------------------------------------------------- |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4883 | |
| 4884 | void CameraService::SensorPrivacyPolicy::registerWithSensorPrivacyManager() |
| 4885 | { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4886 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4887 | if (mRegistered) { |
| 4888 | return; |
| 4889 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4890 | hasCameraPrivacyFeature(); // Called so the result is cached |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4891 | mSpm.addSensorPrivacyListener(this); |
Charles Chen | f075f08 | 2024-03-04 23:32:55 +0000 | [diff] [blame] | 4892 | if (isAutomotiveDevice()) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4893 | mSpm.addToggleSensorPrivacyListener(this); |
| 4894 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4895 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4896 | if (flags::camera_privacy_allowlist()) { |
| 4897 | mCameraPrivacyState = mSpm.getToggleSensorPrivacyState( |
| 4898 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, |
| 4899 | SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 4900 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4901 | status_t res = mSpm.linkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4902 | if (res == OK) { |
| 4903 | mRegistered = true; |
| 4904 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); |
| 4905 | } |
| 4906 | } |
| 4907 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4908 | void CameraService::SensorPrivacyPolicy::onServiceRegistration(const String16& name, |
| 4909 | const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4910 | if (name != toString16(kSensorPrivacyServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4911 | return; |
| 4912 | } |
| 4913 | |
| 4914 | registerWithSensorPrivacyManager(); |
| 4915 | } |
| 4916 | |
| 4917 | void CameraService::SensorPrivacyPolicy::registerSelf() { |
| 4918 | // Use checkservice to see if the sensor_privacy service is available |
| 4919 | // If service is not available then register for notification |
| 4920 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4921 | sp<IBinder> binder = sm->checkService(toString16(kSensorPrivacyServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4922 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4923 | sm->registerForNotifications(toString16(kSensorPrivacyServiceName),this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4924 | } else { |
| 4925 | registerWithSensorPrivacyManager(); |
| 4926 | } |
| 4927 | } |
| 4928 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4929 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { |
| 4930 | Mutex::Autolock _l(mSensorPrivacyLock); |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4931 | mSpm.removeSensorPrivacyListener(this); |
Charles Chen | f075f08 | 2024-03-04 23:32:55 +0000 | [diff] [blame] | 4932 | if (isAutomotiveDevice()) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4933 | mSpm.removeToggleSensorPrivacyListener(this); |
| 4934 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4935 | mSpm.unlinkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4936 | mRegistered = false; |
| 4937 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); |
| 4938 | } |
| 4939 | |
| 4940 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4941 | if (!mRegistered) { |
| 4942 | registerWithSensorPrivacyManager(); |
| 4943 | } |
| 4944 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4945 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4946 | return mSensorPrivacyEnabled; |
| 4947 | } |
| 4948 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4949 | int CameraService::SensorPrivacyPolicy::getCameraPrivacyState() { |
| 4950 | if (!mRegistered) { |
| 4951 | registerWithSensorPrivacyManager(); |
| 4952 | } |
| 4953 | |
| 4954 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4955 | return mCameraPrivacyState; |
| 4956 | } |
| 4957 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4958 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled() { |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4959 | if (!hasCameraPrivacyFeature()) { |
| 4960 | return false; |
| 4961 | } |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4962 | return mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4963 | } |
| 4964 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4965 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled(const String16& packageName) { |
| 4966 | if (!hasCameraPrivacyFeature()) { |
Jyoti Bhayana | 5882b03 | 2024-04-26 11:18:58 -0700 | [diff] [blame] | 4967 | return false; |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4968 | } |
| 4969 | return mSpm.isCameraPrivacyEnabled(packageName); |
| 4970 | } |
| 4971 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4972 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged( |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4973 | int toggleType, int sensor, bool enabled) { |
| 4974 | if ((toggleType == SensorPrivacyManager::TOGGLE_TYPE_UNKNOWN) |
| 4975 | && (sensor == SensorPrivacyManager::TOGGLE_SENSOR_UNKNOWN)) { |
| 4976 | { |
| 4977 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4978 | mSensorPrivacyEnabled = enabled; |
| 4979 | } |
| 4980 | // if sensor privacy is enabled then block all clients from accessing the camera |
| 4981 | if (enabled) { |
| 4982 | sp<CameraService> service = mService.promote(); |
| 4983 | if (service != nullptr) { |
| 4984 | service->blockAllClients(); |
| 4985 | } |
| 4986 | } |
| 4987 | } |
| 4988 | return binder::Status::ok(); |
| 4989 | } |
| 4990 | |
| 4991 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyStateChanged( |
| 4992 | int, int sensor, int state) { |
| 4993 | if (!flags::camera_privacy_allowlist() |
| 4994 | || (sensor != SensorPrivacyManager::TOGGLE_SENSOR_CAMERA)) { |
| 4995 | return binder::Status::ok(); |
| 4996 | } |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4997 | { |
| 4998 | Mutex::Autolock _l(mSensorPrivacyLock); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4999 | mCameraPrivacyState = state; |
| 5000 | } |
| 5001 | sp<CameraService> service = mService.promote(); |
| 5002 | if (!service) { |
| 5003 | return binder::Status::ok(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5004 | } |
| 5005 | // 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] | 5006 | if (state == SensorPrivacyManager::ENABLED) { |
| 5007 | service->blockAllClients(); |
Jyoti Bhayana | 54a4b00 | 2024-02-27 15:36:09 -0800 | [diff] [blame] | 5008 | } else if (state == SensorPrivacyManager::ENABLED_EXCEPT_ALLOWLISTED_APPS) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5009 | service->blockPrivacyEnabledClients(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5010 | } |
| 5011 | return binder::Status::ok(); |
| 5012 | } |
| 5013 | |
| 5014 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 5015 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5016 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); |
| 5017 | mRegistered = false; |
| 5018 | } |
| 5019 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5020 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5021 | bool supportsSoftwareToggle = mSpm.supportsSensorToggle( |
| 5022 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 5023 | bool supportsHardwareToggle = mSpm.supportsSensorToggle( |
| 5024 | SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 5025 | return supportsSoftwareToggle || supportsHardwareToggle; |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5026 | } |
| 5027 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5028 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5029 | // CameraState |
| 5030 | // ---------------------------------------------------------------------------- |
| 5031 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5032 | CameraService::CameraState::CameraState(const std::string& id, int cost, |
| 5033 | const std::set<std::string>& conflicting, SystemCameraKind systemCameraKind, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5034 | const std::vector<std::string>& physicalCameras) : mId(id), |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5035 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5036 | mSystemCameraKind(systemCameraKind), mPhysicalCameras(physicalCameras) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5037 | |
| 5038 | CameraService::CameraState::~CameraState() {} |
| 5039 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5040 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5041 | Mutex::Autolock lock(mStatusLock); |
| 5042 | return mStatus; |
| 5043 | } |
| 5044 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5045 | std::vector<std::string> CameraService::CameraState::getUnavailablePhysicalIds() const { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5046 | Mutex::Autolock lock(mStatusLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5047 | std::vector<std::string> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5048 | return res; |
| 5049 | } |
| 5050 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5051 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 5052 | return mShimParams; |
| 5053 | } |
| 5054 | |
| 5055 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 5056 | mShimParams = params; |
| 5057 | } |
| 5058 | |
| 5059 | int CameraService::CameraState::getCost() const { |
| 5060 | return mCost; |
| 5061 | } |
| 5062 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5063 | std::set<std::string> CameraService::CameraState::getConflicting() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5064 | return mConflicting; |
| 5065 | } |
| 5066 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5067 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { |
| 5068 | return mSystemCameraKind; |
| 5069 | } |
| 5070 | |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5071 | bool CameraService::CameraState::containsPhysicalCamera(const std::string& physicalCameraId) const { |
| 5072 | return std::find(mPhysicalCameras.begin(), mPhysicalCameras.end(), physicalCameraId) |
| 5073 | != mPhysicalCameras.end(); |
| 5074 | } |
| 5075 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5076 | bool CameraService::CameraState::addUnavailablePhysicalId(const std::string& physicalId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5077 | Mutex::Autolock lock(mStatusLock); |
| 5078 | auto result = mUnavailablePhysicalIds.insert(physicalId); |
| 5079 | return result.second; |
| 5080 | } |
| 5081 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5082 | bool CameraService::CameraState::removeUnavailablePhysicalId(const std::string& physicalId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5083 | Mutex::Autolock lock(mStatusLock); |
| 5084 | auto count = mUnavailablePhysicalIds.erase(physicalId); |
| 5085 | return count > 0; |
| 5086 | } |
| 5087 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5088 | void CameraService::CameraState::setClientPackage(const std::string& clientPackage) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5089 | Mutex::Autolock lock(mStatusLock); |
| 5090 | mClientPackage = clientPackage; |
| 5091 | } |
| 5092 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5093 | std::string CameraService::CameraState::getClientPackage() const { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5094 | Mutex::Autolock lock(mStatusLock); |
| 5095 | return mClientPackage; |
| 5096 | } |
| 5097 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5098 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5099 | // ClientEventListener |
| 5100 | // ---------------------------------------------------------------------------- |
| 5101 | |
| 5102 | void CameraService::ClientEventListener::onClientAdded( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5103 | const resource_policy::ClientDescriptor<std::string, |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5104 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 5105 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5106 | if (basicClient.get() != nullptr) { |
| 5107 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5108 | notifier.noteStartCamera(toString8(descriptor.getKey()), |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5109 | static_cast<int>(basicClient->getClientUid())); |
| 5110 | } |
| 5111 | } |
| 5112 | |
| 5113 | void CameraService::ClientEventListener::onClientRemoved( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5114 | const resource_policy::ClientDescriptor<std::string, |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5115 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 5116 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5117 | if (basicClient.get() != nullptr) { |
| 5118 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5119 | notifier.noteStopCamera(toString8(descriptor.getKey()), |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5120 | static_cast<int>(basicClient->getClientUid())); |
| 5121 | } |
| 5122 | } |
| 5123 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5124 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5125 | // CameraClientManager |
| 5126 | // ---------------------------------------------------------------------------- |
| 5127 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5128 | CameraService::CameraClientManager::CameraClientManager() { |
| 5129 | setListener(std::make_shared<ClientEventListener>()); |
| 5130 | } |
| 5131 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5132 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 5133 | |
| 5134 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5135 | const std::string& id) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5136 | auto descriptor = get(id); |
| 5137 | if (descriptor == nullptr) { |
| 5138 | return sp<BasicClient>{nullptr}; |
| 5139 | } |
| 5140 | return descriptor->getValue(); |
| 5141 | } |
| 5142 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5143 | std::string CameraService::CameraClientManager::toString() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5144 | auto all = getAll(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5145 | std::ostringstream ret; |
| 5146 | ret << "["; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5147 | bool hasAny = false; |
| 5148 | for (auto& i : all) { |
| 5149 | hasAny = true; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5150 | std::string key = i->getKey(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5151 | int32_t cost = i->getCost(); |
| 5152 | int32_t pid = i->getOwnerId(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 5153 | int32_t score = i->getPriority().getScore(); |
| 5154 | int32_t state = i->getPriority().getState(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5155 | auto conflicting = i->getConflicting(); |
| 5156 | auto clientSp = i->getValue(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5157 | std::string packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 5158 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5159 | if (clientSp.get() != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5160 | packageName = clientSp->getPackageName(); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5161 | uid_t clientUid = clientSp->getClientUid(); |
| 5162 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5163 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5164 | ret << fmt::sprintf("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" |
| 5165 | PRId32 ", State: %" PRId32, key.c_str(), cost, pid, score, state); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5166 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5167 | if (clientSp.get() != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5168 | ret << fmt::sprintf("User Id: %d, ", clientUserId); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5169 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5170 | if (packageName.size() != 0) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5171 | ret << fmt::sprintf("Client Package Name: %s", packageName.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5172 | } |
| 5173 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5174 | ret << ", Conflicting Client Devices: {"; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5175 | for (auto& j : conflicting) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5176 | ret << fmt::sprintf("%s, ", j.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5177 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5178 | ret << "})"; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5179 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5180 | if (hasAny) ret << "\n"; |
| 5181 | ret << "]\n"; |
| 5182 | return std::move(ret.str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5186 | const std::string& key, const sp<BasicClient>& value, int32_t cost, |
| 5187 | const std::set<std::string>& conflictingKeys, int32_t score, int32_t ownerId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5188 | int32_t state, int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5189 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5190 | int32_t score_adj = systemNativeClient ? kSystemNativeClientScore : score; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5191 | int32_t state_adj = systemNativeClient ? kSystemNativeClientState : state; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 5192 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5193 | return std::make_shared<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>>( |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5194 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, |
| 5195 | systemNativeClient, oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5196 | } |
| 5197 | |
| 5198 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 5199 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5200 | int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5201 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 5202 | partial->getConflicting(), partial->getPriority().getScore(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5203 | partial->getOwnerId(), partial->getPriority().getState(), oomScoreOffset, |
| 5204 | systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5205 | } |
| 5206 | |
| 5207 | // ---------------------------------------------------------------------------- |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5208 | // InjectionStatusListener |
| 5209 | // ---------------------------------------------------------------------------- |
| 5210 | |
| 5211 | void CameraService::InjectionStatusListener::addListener( |
| 5212 | const sp<ICameraInjectionCallback>& callback) { |
| 5213 | Mutex::Autolock lock(mListenerLock); |
| 5214 | if (mCameraInjectionCallback) return; |
| 5215 | status_t res = IInterface::asBinder(callback)->linkToDeath(this); |
| 5216 | if (res == OK) { |
| 5217 | mCameraInjectionCallback = callback; |
| 5218 | } |
| 5219 | } |
| 5220 | |
| 5221 | void CameraService::InjectionStatusListener::removeListener() { |
| 5222 | Mutex::Autolock lock(mListenerLock); |
| 5223 | if (mCameraInjectionCallback == nullptr) { |
| 5224 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 5225 | return; |
| 5226 | } |
| 5227 | IInterface::asBinder(mCameraInjectionCallback)->unlinkToDeath(this); |
| 5228 | mCameraInjectionCallback = nullptr; |
| 5229 | } |
| 5230 | |
| 5231 | void CameraService::InjectionStatusListener::notifyInjectionError( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5232 | const std::string &injectedCamId, status_t err) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5233 | if (mCameraInjectionCallback == nullptr) { |
| 5234 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 5235 | return; |
| 5236 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5237 | |
| 5238 | switch (err) { |
| 5239 | case -ENODEV: |
| 5240 | mCameraInjectionCallback->onInjectionError( |
| 5241 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5242 | ALOGE("No camera device with ID \"%s\" currently available!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5243 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5244 | break; |
| 5245 | case -EBUSY: |
| 5246 | mCameraInjectionCallback->onInjectionError( |
| 5247 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5248 | ALOGE("Higher-priority client using camera, ID \"%s\" currently unavailable!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5249 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5250 | break; |
| 5251 | case DEAD_OBJECT: |
| 5252 | mCameraInjectionCallback->onInjectionError( |
| 5253 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5254 | ALOGE("Camera ID \"%s\" object is dead!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5255 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5256 | break; |
| 5257 | case INVALID_OPERATION: |
| 5258 | mCameraInjectionCallback->onInjectionError( |
| 5259 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5260 | ALOGE("Camera ID \"%s\" encountered an operating or internal error!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5261 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5262 | break; |
| 5263 | case UNKNOWN_TRANSACTION: |
| 5264 | mCameraInjectionCallback->onInjectionError( |
| 5265 | ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED); |
| 5266 | ALOGE("Camera ID \"%s\" method doesn't support!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5267 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5268 | break; |
| 5269 | default: |
| 5270 | mCameraInjectionCallback->onInjectionError( |
| 5271 | ICameraInjectionCallback::ERROR_INJECTION_INVALID_ERROR); |
| 5272 | ALOGE("Unexpected error %s (%d) opening camera \"%s\"!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5273 | strerror(-err), err, injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5274 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5275 | } |
| 5276 | |
| 5277 | void CameraService::InjectionStatusListener::binderDied( |
| 5278 | const wp<IBinder>& /*who*/) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5279 | ALOGV("InjectionStatusListener: ICameraInjectionCallback has died"); |
| 5280 | auto parent = mParent.promote(); |
| 5281 | if (parent != nullptr) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5282 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 5283 | if (clientDescriptor != nullptr) { |
| 5284 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 5285 | baseClientPtr->stopInjection(); |
| 5286 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 5287 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5288 | } |
| 5289 | } |
| 5290 | |
| 5291 | // ---------------------------------------------------------------------------- |
| 5292 | // CameraInjectionSession |
| 5293 | // ---------------------------------------------------------------------------- |
| 5294 | |
| 5295 | binder::Status CameraService::CameraInjectionSession::stopInjection() { |
| 5296 | Mutex::Autolock lock(mInjectionSessionLock); |
| 5297 | auto parent = mParent.promote(); |
| 5298 | if (parent == nullptr) { |
| 5299 | ALOGE("CameraInjectionSession: Parent is gone"); |
| 5300 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SERVICE, |
| 5301 | "Camera service encountered error"); |
| 5302 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5303 | |
| 5304 | status_t res = NO_ERROR; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5305 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 5306 | if (clientDescriptor != nullptr) { |
| 5307 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 5308 | res = baseClientPtr->stopInjection(); |
| 5309 | if (res != OK) { |
| 5310 | ALOGE("CameraInjectionSession: Failed to stop the injection camera!" |
| 5311 | " ret != NO_ERROR: %d", res); |
| 5312 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SESSION, |
| 5313 | "Camera session encountered error"); |
| 5314 | } |
| 5315 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 5316 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5317 | return binder::Status::ok(); |
| 5318 | } |
| 5319 | |
| 5320 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5321 | |
| 5322 | static const int kDumpLockRetries = 50; |
| 5323 | static const int kDumpLockSleep = 60000; |
| 5324 | |
| 5325 | static bool tryLock(Mutex& mutex) |
| 5326 | { |
| 5327 | bool locked = false; |
| 5328 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 5329 | if (mutex.tryLock() == NO_ERROR) { |
| 5330 | locked = true; |
| 5331 | break; |
| 5332 | } |
| 5333 | usleep(kDumpLockSleep); |
| 5334 | } |
| 5335 | return locked; |
| 5336 | } |
| 5337 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5338 | void CameraService::cacheDump() { |
| 5339 | if (mMemFd != -1) { |
| 5340 | const Vector<String16> args; |
| 5341 | ATRACE_CALL(); |
| 5342 | // Acquiring service lock here will avoid the deadlock since |
| 5343 | // cacheDump will not be called during the second disconnect. |
| 5344 | Mutex::Autolock lock(mServiceLock); |
| 5345 | |
| 5346 | Mutex::Autolock l(mCameraStatesLock); |
| 5347 | // Start collecting the info for open sessions and store it in temp file. |
| 5348 | for (const auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5349 | std::string cameraId = state.first; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5350 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 5351 | if (clientDescriptor != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5352 | dprintf(mMemFd, "== Camera device %s dynamic info: ==\n", cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5353 | // Log the current open session info before device is disconnected. |
| 5354 | dumpOpenSessionClientLogs(mMemFd, args, cameraId); |
| 5355 | } |
| 5356 | } |
| 5357 | } |
| 5358 | } |
| 5359 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5360 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 5361 | ATRACE_CALL(); |
| 5362 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5363 | if (checkCallingPermission(toString16(sDumpPermission)) == false) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5364 | 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] | 5365 | getCallingPid(), |
| 5366 | getCallingUid()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5367 | return NO_ERROR; |
| 5368 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5369 | bool locked = tryLock(mServiceLock); |
| 5370 | // failed to lock - CameraService is probably deadlocked |
| 5371 | if (!locked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5372 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5373 | } |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5374 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5375 | if (!mInitialized) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5376 | dprintf(fd, "!! No camera HAL available !!\n"); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 5377 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5378 | // Dump event log for error information |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5379 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5380 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5381 | if (locked) mServiceLock.unlock(); |
| 5382 | return NO_ERROR; |
| 5383 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5384 | dprintf(fd, "\n== Service global info: ==\n\n"); |
| 5385 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 5386 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 5387 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", |
| 5388 | mNormalDeviceIdsWithoutSystemCamera.size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 5389 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { |
| 5390 | dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); |
| 5391 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5392 | std::string activeClientString = mActiveClientManager.toString(); |
| 5393 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.c_str()); |
| 5394 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).c_str()); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5395 | if (mStreamUseCaseOverrides.size() > 0) { |
| 5396 | dprintf(fd, "Active stream use case overrides:"); |
| 5397 | for (int64_t useCaseOverride : mStreamUseCaseOverrides) { |
| 5398 | dprintf(fd, " %" PRId64, useCaseOverride); |
| 5399 | } |
| 5400 | dprintf(fd, "\n"); |
| 5401 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5402 | |
| 5403 | dumpEventLog(fd); |
| 5404 | |
| 5405 | bool stateLocked = tryLock(mCameraStatesLock); |
| 5406 | if (!stateLocked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5407 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5408 | } |
| 5409 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5410 | int argSize = args.size(); |
| 5411 | for (int i = 0; i < argSize; i++) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5412 | if (args[i] == toString16(TagMonitor::kMonitorOption)) { |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5413 | if (i + 1 < argSize) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5414 | mMonitorTags = toStdString(args[i + 1]); |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5415 | } |
| 5416 | break; |
| 5417 | } |
| 5418 | } |
| 5419 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5420 | for (auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5421 | const std::string &cameraId = state.first; |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5422 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5423 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.c_str()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5424 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5425 | CameraParameters p = state.second->getShimParams(); |
| 5426 | if (!p.isEmpty()) { |
| 5427 | dprintf(fd, " Camera1 API shim is using parameters:\n "); |
| 5428 | p.dump(fd, args); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5429 | } |
| 5430 | |
| 5431 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 5432 | if (clientDescriptor != nullptr) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5433 | // log the current open session info |
| 5434 | dumpOpenSessionClientLogs(fd, args, cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 5435 | } else { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5436 | dumpClosedSessionClientLogs(fd, cameraId); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5437 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5438 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5439 | } |
| 5440 | |
| 5441 | if (stateLocked) mCameraStatesLock.unlock(); |
| 5442 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5443 | if (locked) mServiceLock.unlock(); |
| 5444 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 5445 | mCameraProviderManager->dump(fd, args); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5446 | |
| 5447 | dprintf(fd, "\n== Vendor tags: ==\n\n"); |
| 5448 | |
| 5449 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 5450 | if (desc == NULL) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 5451 | sp<VendorTagDescriptorCache> cache = |
| 5452 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 5453 | if (cache == NULL) { |
| 5454 | dprintf(fd, "No vendor tags.\n"); |
| 5455 | } else { |
| 5456 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 5457 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5458 | } else { |
| 5459 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 5460 | } |
| 5461 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5462 | // Dump camera traces if there were any |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5463 | dprintf(fd, "\n"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5464 | camera3::CameraTraces::dump(fd); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5465 | |
| 5466 | // Process dump arguments, if any |
| 5467 | int n = args.size(); |
| 5468 | String16 verboseOption("-v"); |
| 5469 | String16 unreachableOption("--unreachable"); |
| 5470 | for (int i = 0; i < n; i++) { |
| 5471 | if (args[i] == verboseOption) { |
| 5472 | // change logging level |
| 5473 | if (i + 1 >= n) continue; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5474 | std::string levelStr = toStdString(args[i+1]); |
| 5475 | int level = atoi(levelStr.c_str()); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5476 | dprintf(fd, "\nSetting log level to %d.\n", level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5477 | setLogLevel(level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5478 | } else if (args[i] == unreachableOption) { |
| 5479 | // Dump memory analysis |
| 5480 | // TODO - should limit be an argument parameter? |
| 5481 | UnreachableMemoryInfo info; |
| 5482 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); |
| 5483 | if (!success) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5484 | dprintf(fd, "\n== Unable to dump unreachable memory. " |
| 5485 | "Try disabling SELinux enforcement. ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5486 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5487 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5488 | std::string s = info.ToString(/*log_contents*/ true); |
| 5489 | write(fd, s.c_str(), s.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5490 | } |
| 5491 | } |
| 5492 | } |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5493 | |
| 5494 | bool serviceLocked = tryLock(mServiceLock); |
| 5495 | |
| 5496 | // Dump info from previous open sessions. |
| 5497 | // Reposition the offset to beginning of the file before reading |
| 5498 | |
| 5499 | if ((mMemFd >= 0) && (lseek(mMemFd, 0, SEEK_SET) != -1)) { |
| 5500 | dprintf(fd, "\n**********Dumpsys from previous open session**********\n"); |
| 5501 | ssize_t size_read; |
| 5502 | char buf[4096]; |
| 5503 | while ((size_read = read(mMemFd, buf, (sizeof(buf) - 1))) > 0) { |
| 5504 | // Read data from file to a small buffer and write it to fd. |
| 5505 | write(fd, buf, size_read); |
| 5506 | if (size_read == -1) { |
| 5507 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 5508 | break; |
| 5509 | } |
| 5510 | } |
| 5511 | dprintf(fd, "\n**********End of Dumpsys from previous open session**********\n"); |
| 5512 | } else { |
| 5513 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 5514 | } |
| 5515 | |
| 5516 | if (serviceLocked) mServiceLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5517 | return NO_ERROR; |
| 5518 | } |
| 5519 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5520 | void CameraService::dumpOpenSessionClientLogs(int fd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5521 | const Vector<String16>& args, const std::string& cameraId) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5522 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Rucha Katakwar | 71a0e05 | 2022-04-07 15:44:18 -0700 | [diff] [blame] | 5523 | dprintf(fd, " %s : Device %s is open. Client instance dump:\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5524 | getFormattedCurrentTime().c_str(), |
| 5525 | cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5526 | dprintf(fd, " Client priority score: %d state: %d\n", |
| 5527 | clientDescriptor->getPriority().getScore(), |
| 5528 | clientDescriptor->getPriority().getState()); |
| 5529 | dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 5530 | |
| 5531 | auto client = clientDescriptor->getValue(); |
| 5532 | dprintf(fd, " Client package: %s\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5533 | client->getPackageName().c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5534 | |
| 5535 | client->dumpClient(fd, args); |
| 5536 | } |
| 5537 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5538 | void CameraService::dumpClosedSessionClientLogs(int fd, const std::string& cameraId) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5539 | dprintf(fd, " Device %s is closed, no client instance\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5540 | cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5541 | } |
| 5542 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5543 | void CameraService::dumpEventLog(int fd) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5544 | 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] | 5545 | |
| 5546 | Mutex::Autolock l(mLogLock); |
| 5547 | for (const auto& msg : mEventLog) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5548 | dprintf(fd, " %s\n", msg.c_str()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5552 | dprintf(fd, " ...\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5553 | } else if (mEventLog.size() == 0) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5554 | dprintf(fd, " [no events yet]\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5555 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5556 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5557 | } |
| 5558 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5559 | void CameraService::cacheClientTagDumpIfNeeded(const std::string &cameraId, BasicClient* client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5560 | Mutex::Autolock lock(mLogLock); |
| 5561 | if (!isClientWatchedLocked(client)) { return; } |
| 5562 | |
| 5563 | std::vector<std::string> dumpVector; |
| 5564 | client->dumpWatchedEventsToVector(dumpVector); |
| 5565 | |
| 5566 | if (dumpVector.empty()) { return; } |
| 5567 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5568 | std::ostringstream dumpString; |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 5569 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5570 | std::string currentTime = getFormattedCurrentTime(); |
| 5571 | dumpString << "Cached @ "; |
| 5572 | dumpString << currentTime; |
| 5573 | dumpString << "\n"; // First line is the timestamp of when client is cached. |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 5574 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5575 | size_t i = dumpVector.size(); |
| 5576 | |
| 5577 | // Store the string in reverse order (latest last) |
| 5578 | while (i > 0) { |
| 5579 | i--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5580 | dumpString << cameraId; |
| 5581 | dumpString << ":"; |
| 5582 | dumpString << client->getPackageName(); |
| 5583 | dumpString << " "; |
| 5584 | dumpString << dumpVector[i]; // implicitly ends with '\n' |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5585 | } |
| 5586 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5587 | mWatchedClientsDumpCache[client->getPackageName()] = dumpString.str(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5588 | } |
| 5589 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5590 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5591 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5592 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 5593 | if (mTorchClientMap[i] == who) { |
| 5594 | // turn off the torch mode that was turned on by dead client |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5595 | std::string cameraId = mTorchClientMap.keyAt(i); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5596 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 5597 | if (res) { |
| 5598 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 5599 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 5600 | return; |
| 5601 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5602 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5603 | break; |
| 5604 | } |
| 5605 | } |
| 5606 | } |
| 5607 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5608 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 5609 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 5610 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 5611 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 5612 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 5613 | */ |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 5614 | // PID here is approximate and can be wrong. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 5615 | logClientDied(getCallingPid(), "Binder died unexpectedly"); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 5616 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5617 | // check torch client |
| 5618 | handleTorchClientBinderDied(who); |
| 5619 | |
| 5620 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5621 | if(!evictClientIdByRemote(who)) { |
| 5622 | 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] | 5623 | return; |
| 5624 | } |
| 5625 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5626 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 5627 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 5628 | } |
| 5629 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5630 | void CameraService::updateStatus(StatusInternal status, const std::string& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5631 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 5632 | } |
| 5633 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5634 | void CameraService::updateStatus(StatusInternal status, const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5635 | std::initializer_list<StatusInternal> rejectSourceStates) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5636 | // Do not lock mServiceLock here or can get into a deadlock from |
| 5637 | // connect() -> disconnect -> updateStatus |
| 5638 | |
| 5639 | auto state = getCameraState(cameraId); |
| 5640 | |
| 5641 | if (state == nullptr) { |
| 5642 | 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] | 5643 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5644 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 5645 | } |
| 5646 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5647 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 5648 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 5649 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5650 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5651 | return; |
| 5652 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5653 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5654 | if (vd_flags::camera_device_awareness() && status == StatusInternal::PRESENT) { |
| 5655 | CameraMetadata cameraInfo; |
| 5656 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 5657 | cameraId, false, &cameraInfo, hardware::ICameraService::ROTATION_OVERRIDE_NONE); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5658 | if (res != OK) { |
| 5659 | ALOGW("%s: Not able to get camera characteristics for camera id %s", |
| 5660 | __FUNCTION__, cameraId.c_str()); |
| 5661 | } else { |
| 5662 | int32_t deviceId = getDeviceId(cameraInfo); |
| 5663 | if (deviceId != kDefaultDeviceId) { |
| 5664 | const auto &lensFacingEntry = cameraInfo.find(ANDROID_LENS_FACING); |
| 5665 | camera_metadata_enum_android_lens_facing_t androidLensFacing = |
| 5666 | static_cast<camera_metadata_enum_android_lens_facing_t>( |
| 5667 | lensFacingEntry.data.u8[0]); |
| 5668 | std::string mappedCameraId; |
| 5669 | if (androidLensFacing == ANDROID_LENS_FACING_BACK) { |
| 5670 | mappedCameraId = kVirtualDeviceBackCameraId; |
| 5671 | } else if (androidLensFacing == ANDROID_LENS_FACING_FRONT) { |
| 5672 | mappedCameraId = kVirtualDeviceFrontCameraId; |
| 5673 | } else { |
| 5674 | ALOGD("%s: Not adding entry for an external camera of a virtual device", |
| 5675 | __func__); |
| 5676 | } |
| 5677 | if (!mappedCameraId.empty()) { |
| 5678 | mVirtualDeviceCameraIdMapper.addCamera(cameraId, deviceId, mappedCameraId); |
| 5679 | } |
| 5680 | } |
| 5681 | } |
| 5682 | } |
| 5683 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5684 | // Collect the logical cameras without holding mStatusLock in updateStatus |
| 5685 | // as that can lead to a deadlock(b/162192331). |
| 5686 | auto logicalCameraIds = getLogicalCameras(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5687 | // 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] | 5688 | // of the listeners with both the mStatusLock and mStatusListenerLock held |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 5689 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5690 | &logicalCameraIds] |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5691 | (const std::string& cameraId, StatusInternal status) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5692 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 5693 | auto [deviceId, mappedCameraId] = |
| 5694 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5695 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5696 | if (status != StatusInternal::ENUMERATING) { |
| 5697 | // Update torch status if it has a flash unit. |
| 5698 | Mutex::Autolock al(mTorchStatusMutex); |
| 5699 | TorchModeStatus torchStatus; |
| 5700 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 5701 | NAME_NOT_FOUND) { |
| 5702 | TorchModeStatus newTorchStatus = |
| 5703 | status == StatusInternal::PRESENT ? |
| 5704 | TorchModeStatus::AVAILABLE_OFF : |
| 5705 | TorchModeStatus::NOT_AVAILABLE; |
| 5706 | if (torchStatus != newTorchStatus) { |
| 5707 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); |
| 5708 | } |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 5709 | } |
| 5710 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5711 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5712 | Mutex::Autolock lock(mStatusListenerLock); |
| 5713 | notifyPhysicalCameraStatusLocked(mapToInterface(status), mappedCameraId, |
| 5714 | logicalCameraIds, deviceKind, deviceId); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5715 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5716 | for (auto& listener : mListenerList) { |
| 5717 | bool isVendorListener = listener->isVendorListener(); |
| 5718 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, |
| 5719 | listener->getListenerPid(), listener->getListenerUid())) { |
| 5720 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 5721 | cameraId.c_str()); |
| 5722 | continue; |
| 5723 | } |
| 5724 | |
| 5725 | auto ret = listener->getListener()->onStatusChanged(mapToInterface(status), |
| 5726 | mappedCameraId, deviceId); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5727 | listener->handleBinderStatus(ret, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5728 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5729 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 5730 | ret.exceptionCode()); |
| 5731 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5732 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 5733 | } |
| 5734 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5735 | void CameraService::updateOpenCloseStatus(const std::string& cameraId, bool open, |
| 5736 | const std::string& clientPackageName) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5737 | auto state = getCameraState(cameraId); |
| 5738 | if (state == nullptr) { |
| 5739 | 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] | 5740 | cameraId.c_str()); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5741 | return; |
| 5742 | } |
| 5743 | if (open) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5744 | state->setClientPackage(clientPackageName); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5745 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5746 | state->setClientPackage(std::string()); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5747 | } |
| 5748 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5749 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 5750 | auto [deviceId, mappedCameraId] = |
| 5751 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 5752 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5753 | Mutex::Autolock lock(mStatusListenerLock); |
| 5754 | |
| 5755 | for (const auto& it : mListenerList) { |
| 5756 | if (!it->isOpenCloseCallbackAllowed()) { |
| 5757 | continue; |
| 5758 | } |
| 5759 | |
| 5760 | binder::Status ret; |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5761 | if (open) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5762 | ret = it->getListener()->onCameraOpened(mappedCameraId, clientPackageName, |
| 5763 | deviceId); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5764 | } else { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5765 | ret = it->getListener()->onCameraClosed(mappedCameraId, deviceId); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5766 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5767 | |
| 5768 | it->handleBinderStatus(ret, |
| 5769 | "%s: Failed to trigger onCameraOpened/onCameraClosed callback for %d:%d: %d", |
| 5770 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5771 | } |
| 5772 | } |
| 5773 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5774 | template<class Func> |
| 5775 | void CameraService::CameraState::updateStatus(StatusInternal status, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5776 | const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5777 | std::initializer_list<StatusInternal> rejectSourceStates, |
| 5778 | Func onStatusUpdatedLocked) { |
| 5779 | Mutex::Autolock lock(mStatusLock); |
| 5780 | StatusInternal oldStatus = mStatus; |
| 5781 | mStatus = status; |
| 5782 | |
| 5783 | if (oldStatus == status) { |
| 5784 | return; |
| 5785 | } |
| 5786 | |
| 5787 | ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__, |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 5788 | cameraId.c_str(), eToI(oldStatus), eToI(status)); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5789 | |
| 5790 | if (oldStatus == StatusInternal::NOT_PRESENT && |
| 5791 | (status != StatusInternal::PRESENT && |
| 5792 | status != StatusInternal::ENUMERATING)) { |
| 5793 | |
| 5794 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 5795 | __FUNCTION__); |
| 5796 | mStatus = oldStatus; |
| 5797 | return; |
| 5798 | } |
| 5799 | |
| 5800 | /** |
| 5801 | * Sometimes we want to conditionally do a transition. |
| 5802 | * For example if a client disconnects, we want to go to PRESENT |
| 5803 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 5804 | */ |
| 5805 | for (auto& rejectStatus : rejectSourceStates) { |
| 5806 | if (oldStatus == rejectStatus) { |
| 5807 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5808 | "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] | 5809 | mStatus = oldStatus; |
| 5810 | return; |
| 5811 | } |
| 5812 | } |
| 5813 | |
| 5814 | onStatusUpdatedLocked(cameraId, status); |
| 5815 | } |
| 5816 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5817 | status_t CameraService::getTorchStatusLocked( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5818 | const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5819 | TorchModeStatus *status) const { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5820 | if (!status) { |
| 5821 | return BAD_VALUE; |
| 5822 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5823 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 5824 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5825 | // invalid camera ID or the camera doesn't have a flash unit |
| 5826 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5827 | } |
| 5828 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5829 | *status = mTorchStatusMap.valueAt(index); |
| 5830 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5831 | } |
| 5832 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5833 | status_t CameraService::setTorchStatusLocked(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5834 | TorchModeStatus status) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5835 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 5836 | if (index == NAME_NOT_FOUND) { |
| 5837 | return BAD_VALUE; |
| 5838 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5839 | mTorchStatusMap.editValueAt(index) = status; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5840 | |
| 5841 | return OK; |
| 5842 | } |
| 5843 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5844 | std::list<std::string> CameraService::getLogicalCameras( |
| 5845 | const std::string& physicalCameraId) { |
| 5846 | std::list<std::string> retList; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5847 | Mutex::Autolock lock(mCameraStatesLock); |
| 5848 | for (const auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5849 | if (state.second->containsPhysicalCamera(physicalCameraId)) { |
| 5850 | retList.emplace_back(state.first); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5851 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5852 | } |
| 5853 | return retList; |
| 5854 | } |
| 5855 | |
| 5856 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5857 | const std::string& physicalCameraId, const std::list<std::string>& logicalCameraIds, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5858 | SystemCameraKind deviceKind, int32_t deviceId) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5859 | // mStatusListenerLock is expected to be locked |
| 5860 | for (const auto& logicalCameraId : logicalCameraIds) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5861 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5862 | // Note: we check only the deviceKind of the physical camera id |
| 5863 | // since, logical camera ids and their physical camera ids are |
| 5864 | // guaranteed to have the same system camera kind. |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 5865 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 5866 | listener->getListenerPid(), listener->getListenerUid())) { |
| 5867 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5868 | physicalCameraId.c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 5869 | continue; |
| 5870 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5871 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged(status, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5872 | logicalCameraId, physicalCameraId, deviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5873 | listener->handleBinderStatus(ret, |
| 5874 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 5875 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 5876 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5881 | void CameraService::blockClientsForUid(uid_t uid) { |
| 5882 | const auto clients = mActiveClientManager.getAll(); |
| 5883 | for (auto& current : clients) { |
| 5884 | if (current != nullptr) { |
| 5885 | const auto basicClient = current->getValue(); |
| 5886 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { |
| 5887 | basicClient->block(); |
| 5888 | } |
| 5889 | } |
| 5890 | } |
| 5891 | } |
| 5892 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5893 | void CameraService::blockAllClients() { |
| 5894 | const auto clients = mActiveClientManager.getAll(); |
| 5895 | for (auto& current : clients) { |
| 5896 | if (current != nullptr) { |
| 5897 | const auto basicClient = current->getValue(); |
| 5898 | if (basicClient.get() != nullptr) { |
| 5899 | basicClient->block(); |
| 5900 | } |
| 5901 | } |
| 5902 | } |
| 5903 | } |
| 5904 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5905 | void CameraService::blockPrivacyEnabledClients() { |
| 5906 | const auto clients = mActiveClientManager.getAll(); |
| 5907 | for (auto& current : clients) { |
| 5908 | if (current != nullptr) { |
| 5909 | const auto basicClient = current->getValue(); |
| 5910 | if (basicClient.get() != nullptr) { |
| 5911 | std::string pkgName = basicClient->getPackageName(); |
| 5912 | bool cameraPrivacyEnabled = |
| 5913 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(toString16(pkgName)); |
| 5914 | if (cameraPrivacyEnabled) { |
| 5915 | basicClient->block(); |
| 5916 | } |
| 5917 | } |
| 5918 | } |
| 5919 | } |
| 5920 | } |
| 5921 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5922 | // NOTE: This is a remote API - make sure all args are validated |
| 5923 | 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] | 5924 | if (!checkCallingPermission(toString16(sManageCameraPermission), nullptr, nullptr)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5925 | return PERMISSION_DENIED; |
| 5926 | } |
| 5927 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 5928 | return BAD_VALUE; |
| 5929 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5930 | if (args.size() >= 3 && args[0] == toString16("set-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5931 | return handleSetUidState(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5932 | } else if (args.size() >= 2 && args[0] == toString16("reset-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5933 | return handleResetUidState(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5934 | } else if (args.size() >= 2 && args[0] == toString16("get-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5935 | return handleGetUidState(args, out, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5936 | } 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] | 5937 | return handleSetRotateAndCrop(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5938 | } 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] | 5939 | return handleGetRotateAndCrop(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5940 | } else if (args.size() >= 2 && args[0] == toString16("set-autoframing")) { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5941 | return handleSetAutoframing(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5942 | } else if (args.size() >= 1 && args[0] == toString16("get-autoframing")) { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5943 | return handleGetAutoframing(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5944 | } else if (args.size() >= 2 && args[0] == toString16("set-image-dump-mask")) { |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5945 | return handleSetImageDumpMask(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5946 | } else if (args.size() >= 1 && args[0] == toString16("get-image-dump-mask")) { |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5947 | return handleGetImageDumpMask(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5948 | } else if (args.size() >= 2 && args[0] == toString16("set-camera-mute")) { |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 5949 | return handleSetCameraMute(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5950 | } 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] | 5951 | return handleSetStreamUseCaseOverrides(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5952 | } 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] | 5953 | handleClearStreamUseCaseOverrides(); |
| 5954 | return OK; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5955 | } else if (args.size() >= 1 && args[0] == toString16("set-zoom-override")) { |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 5956 | return handleSetZoomOverride(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5957 | } else if (args.size() >= 2 && args[0] == toString16("watch")) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5958 | return handleWatchCommand(args, in, out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5959 | } else if (args.size() >= 2 && args[0] == toString16("set-watchdog")) { |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 5960 | return handleSetCameraServiceWatchdog(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5961 | } else if (args.size() == 1 && args[0] == toString16("help")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5962 | printHelp(out); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5963 | return OK; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5964 | } |
| 5965 | printHelp(err); |
| 5966 | return BAD_VALUE; |
| 5967 | } |
| 5968 | |
| 5969 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5970 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5971 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5972 | bool active = false; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5973 | if (args[2] == toString16("active")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5974 | active = true; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5975 | } else if ((args[2] != toString16("idle"))) { |
| 5976 | 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] | 5977 | return BAD_VALUE; |
| 5978 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5979 | |
| 5980 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5981 | if (args.size() >= 5 && args[3] == toString16("--user")) { |
| 5982 | userId = atoi(toStdString(args[4]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5983 | } |
| 5984 | |
| 5985 | uid_t uid; |
| 5986 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
| 5987 | return BAD_VALUE; |
| 5988 | } |
| 5989 | |
| 5990 | mUidPolicy->addOverrideUid(uid, packageName, active); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5991 | return NO_ERROR; |
| 5992 | } |
| 5993 | |
| 5994 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5995 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5996 | |
| 5997 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5998 | if (args.size() >= 4 && args[2] == toString16("--user")) { |
| 5999 | userId = atoi(toStdString(args[3]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6000 | } |
| 6001 | |
| 6002 | uid_t uid; |
| 6003 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6004 | return BAD_VALUE; |
| 6005 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6006 | |
| 6007 | mUidPolicy->removeOverrideUid(uid, packageName); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6008 | return NO_ERROR; |
| 6009 | } |
| 6010 | |
| 6011 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6012 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6013 | |
| 6014 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6015 | if (args.size() >= 4 && args[2] == toString16("--user")) { |
| 6016 | userId = atoi(toStdString(args[3]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6017 | } |
| 6018 | |
| 6019 | uid_t uid; |
| 6020 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6021 | return BAD_VALUE; |
| 6022 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6023 | |
| 6024 | if (mUidPolicy->isUidActive(uid, packageName)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6025 | return dprintf(out, "active\n"); |
| 6026 | } else { |
| 6027 | return dprintf(out, "idle\n"); |
| 6028 | } |
| 6029 | } |
| 6030 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6031 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6032 | int rotateValue = atoi(toStdString(args[1]).c_str()); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6033 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || |
| 6034 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; |
| 6035 | Mutex::Autolock lock(mServiceLock); |
| 6036 | |
| 6037 | mOverrideRotateAndCropMode = rotateValue; |
| 6038 | |
| 6039 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; |
| 6040 | |
| 6041 | const auto clients = mActiveClientManager.getAll(); |
| 6042 | for (auto& current : clients) { |
| 6043 | if (current != nullptr) { |
| 6044 | const auto basicClient = current->getValue(); |
| 6045 | if (basicClient.get() != nullptr) { |
| 6046 | basicClient->setRotateAndCropOverride(rotateValue); |
| 6047 | } |
| 6048 | } |
| 6049 | } |
| 6050 | |
| 6051 | return OK; |
| 6052 | } |
| 6053 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6054 | status_t CameraService::handleSetAutoframing(const Vector<String16>& args) { |
| 6055 | char* end; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6056 | int autoframingValue = (int) strtol(toStdString(args[1]).c_str(), &end, /*base=*/10); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6057 | if ((*end != '\0') || |
| 6058 | (autoframingValue != ANDROID_CONTROL_AUTOFRAMING_OFF && |
| 6059 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_ON && |
| 6060 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_AUTO)) { |
| 6061 | return BAD_VALUE; |
| 6062 | } |
| 6063 | |
| 6064 | Mutex::Autolock lock(mServiceLock); |
| 6065 | mOverrideAutoframingMode = autoframingValue; |
| 6066 | |
| 6067 | if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) return OK; |
| 6068 | |
| 6069 | const auto clients = mActiveClientManager.getAll(); |
| 6070 | for (auto& current : clients) { |
| 6071 | if (current != nullptr) { |
| 6072 | const auto basicClient = current->getValue(); |
| 6073 | if (basicClient.get() != nullptr) { |
| 6074 | basicClient->setAutoframingOverride(autoframingValue); |
| 6075 | } |
| 6076 | } |
| 6077 | } |
| 6078 | |
| 6079 | return OK; |
| 6080 | } |
| 6081 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6082 | status_t CameraService::handleSetCameraServiceWatchdog(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6083 | int enableWatchdog = atoi(toStdString(args[1]).c_str()); |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6084 | |
| 6085 | if (enableWatchdog < 0 || enableWatchdog > 1) return BAD_VALUE; |
| 6086 | |
| 6087 | Mutex::Autolock lock(mServiceLock); |
| 6088 | |
| 6089 | mCameraServiceWatchdogEnabled = enableWatchdog; |
| 6090 | |
| 6091 | const auto clients = mActiveClientManager.getAll(); |
| 6092 | for (auto& current : clients) { |
| 6093 | if (current != nullptr) { |
| 6094 | const auto basicClient = current->getValue(); |
| 6095 | if (basicClient.get() != nullptr) { |
| 6096 | basicClient->setCameraServiceWatchdog(enableWatchdog); |
| 6097 | } |
| 6098 | } |
| 6099 | } |
| 6100 | |
| 6101 | return OK; |
| 6102 | } |
| 6103 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6104 | status_t CameraService::handleGetRotateAndCrop(int out) { |
| 6105 | Mutex::Autolock lock(mServiceLock); |
| 6106 | |
| 6107 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); |
| 6108 | } |
| 6109 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6110 | status_t CameraService::handleGetAutoframing(int out) { |
| 6111 | Mutex::Autolock lock(mServiceLock); |
| 6112 | |
| 6113 | return dprintf(out, "autoframing override: %d\n", mOverrideAutoframingMode); |
| 6114 | } |
| 6115 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6116 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { |
| 6117 | char *endPtr; |
| 6118 | errno = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6119 | std::string maskString = toStdString(args[1]); |
| 6120 | long maskValue = strtol(maskString.c_str(), &endPtr, 10); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6121 | |
| 6122 | if (errno != 0) return BAD_VALUE; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6123 | if (endPtr != maskString.c_str() + maskString.size()) return BAD_VALUE; |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6124 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; |
| 6125 | |
| 6126 | Mutex::Autolock lock(mServiceLock); |
| 6127 | |
| 6128 | mImageDumpMask = maskValue; |
| 6129 | |
| 6130 | return OK; |
| 6131 | } |
| 6132 | |
| 6133 | status_t CameraService::handleGetImageDumpMask(int out) { |
| 6134 | Mutex::Autolock lock(mServiceLock); |
| 6135 | |
| 6136 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); |
| 6137 | } |
| 6138 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6139 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6140 | int muteValue = strtol(toStdString(args[1]).c_str(), nullptr, 10); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6141 | if (errno != 0) return BAD_VALUE; |
| 6142 | |
| 6143 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; |
| 6144 | Mutex::Autolock lock(mServiceLock); |
| 6145 | |
| 6146 | mOverrideCameraMuteMode = (muteValue == 1); |
| 6147 | |
| 6148 | const auto clients = mActiveClientManager.getAll(); |
| 6149 | for (auto& current : clients) { |
| 6150 | if (current != nullptr) { |
| 6151 | const auto basicClient = current->getValue(); |
| 6152 | if (basicClient.get() != nullptr) { |
| 6153 | if (basicClient->supportsCameraMute()) { |
| 6154 | basicClient->setCameraMute(mOverrideCameraMuteMode); |
| 6155 | } |
| 6156 | } |
| 6157 | } |
| 6158 | } |
| 6159 | |
| 6160 | return OK; |
| 6161 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6162 | |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6163 | status_t CameraService::handleSetStreamUseCaseOverrides(const Vector<String16>& args) { |
| 6164 | std::vector<int64_t> useCasesOverride; |
| 6165 | for (size_t i = 1; i < args.size(); i++) { |
| 6166 | int64_t useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6167 | std::string arg = toStdString(args[i]); |
| 6168 | if (arg == "DEFAULT") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6169 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6170 | } else if (arg == "PREVIEW") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6171 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6172 | } else if (arg == "STILL_CAPTURE") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6173 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_STILL_CAPTURE; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6174 | } else if (arg == "VIDEO_RECORD") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6175 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_RECORD; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6176 | } else if (arg == "PREVIEW_VIDEO_STILL") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6177 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW_VIDEO_STILL; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6178 | } else if (arg == "VIDEO_CALL") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6179 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6180 | } else if (arg == "CROPPED_RAW") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6181 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_CROPPED_RAW; |
| 6182 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6183 | ALOGE("%s: Invalid stream use case %s", __FUNCTION__, arg.c_str()); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6184 | return BAD_VALUE; |
| 6185 | } |
| 6186 | useCasesOverride.push_back(useCase); |
| 6187 | } |
| 6188 | |
| 6189 | Mutex::Autolock lock(mServiceLock); |
| 6190 | mStreamUseCaseOverrides = std::move(useCasesOverride); |
| 6191 | |
| 6192 | return OK; |
| 6193 | } |
| 6194 | |
| 6195 | void CameraService::handleClearStreamUseCaseOverrides() { |
| 6196 | Mutex::Autolock lock(mServiceLock); |
| 6197 | mStreamUseCaseOverrides.clear(); |
| 6198 | } |
| 6199 | |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6200 | status_t CameraService::handleSetZoomOverride(const Vector<String16>& args) { |
| 6201 | char* end; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6202 | int zoomOverrideValue = strtol(toStdString(args[1]).c_str(), &end, /*base=*/10); |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6203 | if ((*end != '\0') || |
| 6204 | (zoomOverrideValue != -1 && |
| 6205 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF && |
| 6206 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM)) { |
| 6207 | return BAD_VALUE; |
| 6208 | } |
| 6209 | |
| 6210 | Mutex::Autolock lock(mServiceLock); |
| 6211 | mZoomOverrideValue = zoomOverrideValue; |
| 6212 | |
| 6213 | const auto clients = mActiveClientManager.getAll(); |
| 6214 | for (auto& current : clients) { |
| 6215 | if (current != nullptr) { |
| 6216 | const auto basicClient = current->getValue(); |
| 6217 | if (basicClient.get() != nullptr) { |
| 6218 | if (basicClient->supportsZoomOverride()) { |
| 6219 | basicClient->setZoomOverride(mZoomOverrideValue); |
| 6220 | } |
| 6221 | } |
| 6222 | } |
| 6223 | } |
| 6224 | |
| 6225 | return OK; |
| 6226 | } |
| 6227 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6228 | status_t CameraService::handleWatchCommand(const Vector<String16>& args, int inFd, int outFd) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6229 | if (args.size() >= 3 && args[1] == toString16("start")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6230 | return startWatchingTags(args, outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6231 | } else if (args.size() == 2 && args[1] == toString16("stop")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6232 | return stopWatchingTags(outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6233 | } else if (args.size() == 2 && args[1] == toString16("dump")) { |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6234 | return printWatchedTags(outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6235 | } else if (args.size() >= 2 && args[1] == toString16("live")) { |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6236 | return printWatchedTagsUntilInterrupt(args, inFd, outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6237 | } else if (args.size() == 2 && args[1] == toString16("clear")) { |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6238 | return clearCachedMonitoredTagDumps(outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6239 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6240 | dprintf(outFd, "Camera service watch commands:\n" |
| 6241 | " start -m <comma_separated_tag_list> [-c <comma_separated_client_list>]\n" |
| 6242 | " starts watching the provided tags for clients with provided package\n" |
| 6243 | " recognizes tag shorthands like '3a'\n" |
| 6244 | " 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] | 6245 | " dump dumps the monitoring information and exits\n" |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6246 | " stop stops watching all tags\n" |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6247 | " live [-n <refresh_interval_ms>]\n" |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6248 | " prints the monitored information in real time\n" |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6249 | " Hit return to exit\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6250 | " clear clears all buffers storing information for watch command"); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 6251 | return BAD_VALUE; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6252 | } |
| 6253 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6254 | status_t CameraService::startWatchingTags(const Vector<String16> &args, int outFd) { |
| 6255 | Mutex::Autolock lock(mLogLock); |
| 6256 | size_t tagsIdx; // index of '-m' |
| 6257 | String16 tags(""); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6258 | for (tagsIdx = 2; tagsIdx < args.size() && args[tagsIdx] != toString16("-m"); tagsIdx++); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6259 | if (tagsIdx < args.size() - 1) { |
| 6260 | tags = args[tagsIdx + 1]; |
| 6261 | } else { |
| 6262 | dprintf(outFd, "No tags provided.\n"); |
| 6263 | return BAD_VALUE; |
| 6264 | } |
| 6265 | |
| 6266 | size_t clientsIdx; // index of '-c' |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6267 | // watch all clients if no clients are provided |
| 6268 | String16 clients = toString16(kWatchAllClientsFlag); |
| 6269 | for (clientsIdx = 2; clientsIdx < args.size() && args[clientsIdx] != toString16("-c"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6270 | clientsIdx++); |
| 6271 | if (clientsIdx < args.size() - 1) { |
| 6272 | clients = args[clientsIdx + 1]; |
| 6273 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6274 | parseClientsToWatchLocked(toStdString(clients)); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6275 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6276 | // track tags to initialize future clients with the monitoring information |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6277 | mMonitorTags = toStdString(tags); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6278 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6279 | bool serviceLock = tryLock(mServiceLock); |
| 6280 | int numWatchedClients = 0; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6281 | auto cameraClients = mActiveClientManager.getAll(); |
| 6282 | for (const auto &clientDescriptor: cameraClients) { |
| 6283 | if (clientDescriptor == nullptr) { continue; } |
| 6284 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6285 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6286 | |
| 6287 | if (isClientWatchedLocked(client.get())) { |
| 6288 | client->startWatchingTags(mMonitorTags, outFd); |
| 6289 | numWatchedClients++; |
| 6290 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6291 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6292 | dprintf(outFd, "Started watching %d active clients\n", numWatchedClients); |
| 6293 | |
| 6294 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6295 | return OK; |
| 6296 | } |
| 6297 | |
| 6298 | status_t CameraService::stopWatchingTags(int outFd) { |
| 6299 | // clear mMonitorTags to prevent new clients from monitoring tags at initialization |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6300 | Mutex::Autolock lock(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6301 | mMonitorTags = ""; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6302 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6303 | mWatchedClientPackages.clear(); |
| 6304 | mWatchedClientsDumpCache.clear(); |
| 6305 | |
| 6306 | bool serviceLock = tryLock(mServiceLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6307 | auto cameraClients = mActiveClientManager.getAll(); |
| 6308 | for (const auto &clientDescriptor : cameraClients) { |
| 6309 | if (clientDescriptor == nullptr) { continue; } |
| 6310 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6311 | if (client.get() == nullptr) { continue; } |
| 6312 | client->stopWatchingTags(outFd); |
| 6313 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6314 | dprintf(outFd, "Stopped watching all clients.\n"); |
| 6315 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6316 | return OK; |
| 6317 | } |
| 6318 | |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6319 | status_t CameraService::clearCachedMonitoredTagDumps(int outFd) { |
| 6320 | Mutex::Autolock lock(mLogLock); |
| 6321 | size_t clearedSize = mWatchedClientsDumpCache.size(); |
| 6322 | mWatchedClientsDumpCache.clear(); |
| 6323 | dprintf(outFd, "Cleared tag information of %zu cached clients.\n", clearedSize); |
| 6324 | return OK; |
| 6325 | } |
| 6326 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6327 | status_t CameraService::printWatchedTags(int outFd) { |
| 6328 | Mutex::Autolock logLock(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6329 | std::set<std::string> connectedMonitoredClients; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6330 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6331 | bool printedSomething = false; // tracks if any monitoring information was printed |
| 6332 | // (from either cached or active clients) |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6333 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6334 | bool serviceLock = tryLock(mServiceLock); |
| 6335 | // get all watched clients that are currently connected |
| 6336 | for (const auto &clientDescriptor: mActiveClientManager.getAll()) { |
| 6337 | if (clientDescriptor == nullptr) { continue; } |
| 6338 | |
| 6339 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6340 | if (client.get() == nullptr) { continue; } |
| 6341 | if (!isClientWatchedLocked(client.get())) { continue; } |
| 6342 | |
| 6343 | std::vector<std::string> dumpVector; |
| 6344 | client->dumpWatchedEventsToVector(dumpVector); |
| 6345 | |
| 6346 | size_t printIdx = dumpVector.size(); |
| 6347 | if (printIdx == 0) { |
| 6348 | continue; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6349 | } |
| 6350 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6351 | // Print tag dumps for active client |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6352 | const std::string &cameraId = clientDescriptor->getKey(); |
| 6353 | dprintf(outFd, "Client: %s (active)\n", client->getPackageName().c_str()); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6354 | while(printIdx > 0) { |
| 6355 | printIdx--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6356 | dprintf(outFd, "%s:%s %s", cameraId.c_str(), client->getPackageName().c_str(), |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6357 | dumpVector[printIdx].c_str()); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6358 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6359 | dprintf(outFd, "\n"); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6360 | printedSomething = true; |
| 6361 | |
| 6362 | connectedMonitoredClients.emplace(client->getPackageName()); |
| 6363 | } |
| 6364 | if (serviceLock) { mServiceLock.unlock(); } |
| 6365 | |
| 6366 | // Print entries in mWatchedClientsDumpCache for clients that are not connected |
| 6367 | for (const auto &kv: mWatchedClientsDumpCache) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6368 | const std::string &package = kv.first; |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6369 | if (connectedMonitoredClients.find(package) != connectedMonitoredClients.end()) { |
| 6370 | continue; |
| 6371 | } |
| 6372 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6373 | dprintf(outFd, "Client: %s (cached)\n", package.c_str()); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6374 | dprintf(outFd, "%s\n", kv.second.c_str()); |
| 6375 | printedSomething = true; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6376 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6377 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6378 | if (!printedSomething) { |
| 6379 | dprintf(outFd, "No monitoring information to print.\n"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6380 | } |
| 6381 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6382 | return OK; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6383 | } |
| 6384 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6385 | // Print all events in vector `events' that came after lastPrintedEvent |
| 6386 | void printNewWatchedEvents(int outFd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6387 | const std::string &cameraId, |
| 6388 | const std::string &packageName, |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6389 | const std::vector<std::string> &events, |
| 6390 | const std::string &lastPrintedEvent) { |
| 6391 | if (events.empty()) { return; } |
| 6392 | |
| 6393 | // index of lastPrintedEvent in events. |
| 6394 | // lastPrintedIdx = events.size() if lastPrintedEvent is not in events |
| 6395 | size_t lastPrintedIdx; |
| 6396 | for (lastPrintedIdx = 0; |
| 6397 | lastPrintedIdx < events.size() && lastPrintedEvent != events[lastPrintedIdx]; |
| 6398 | lastPrintedIdx++); |
| 6399 | |
| 6400 | if (lastPrintedIdx == 0) { return; } // early exit if no new event in `events` |
| 6401 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6402 | // print events in chronological order (latest event last) |
| 6403 | size_t idxToPrint = lastPrintedIdx; |
| 6404 | do { |
| 6405 | idxToPrint--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6406 | dprintf(outFd, "%s:%s %s", cameraId.c_str(), packageName.c_str(), |
| 6407 | events[idxToPrint].c_str()); |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6408 | } while (idxToPrint != 0); |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6409 | } |
| 6410 | |
| 6411 | // Returns true if adb shell cmd watch should be interrupted based on data in inFd. The watch |
| 6412 | // command should be interrupted if the user presses the return key, or if user loses any way to |
| 6413 | // signal interrupt. |
| 6414 | // If timeoutMs == 0, this function will always return false |
| 6415 | bool shouldInterruptWatchCommand(int inFd, int outFd, long timeoutMs) { |
| 6416 | struct timeval startTime; |
| 6417 | int startTimeError = gettimeofday(&startTime, nullptr); |
| 6418 | if (startTimeError) { |
| 6419 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 6420 | return true; |
| 6421 | } |
| 6422 | |
| 6423 | const nfds_t numFds = 1; |
| 6424 | struct pollfd pollFd = { .fd = inFd, .events = POLLIN, .revents = 0 }; |
| 6425 | |
| 6426 | struct timeval currTime; |
| 6427 | char buffer[2]; |
| 6428 | while(true) { |
| 6429 | int currTimeError = gettimeofday(&currTime, nullptr); |
| 6430 | if (currTimeError) { |
| 6431 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 6432 | return true; |
| 6433 | } |
| 6434 | |
| 6435 | long elapsedTimeMs = ((currTime.tv_sec - startTime.tv_sec) * 1000L) |
| 6436 | + ((currTime.tv_usec - startTime.tv_usec) / 1000L); |
| 6437 | int remainingTimeMs = (int) (timeoutMs - elapsedTimeMs); |
| 6438 | |
| 6439 | if (remainingTimeMs <= 0) { |
| 6440 | // No user interrupt within timeoutMs, don't interrupt watch command |
| 6441 | return false; |
| 6442 | } |
| 6443 | |
| 6444 | int numFdsUpdated = poll(&pollFd, numFds, remainingTimeMs); |
| 6445 | if (numFdsUpdated < 0) { |
| 6446 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 6447 | return true; |
| 6448 | } |
| 6449 | |
| 6450 | if (numFdsUpdated == 0) { |
| 6451 | // No user input within timeoutMs, don't interrupt watch command |
| 6452 | return false; |
| 6453 | } |
| 6454 | |
| 6455 | if (!(pollFd.revents & POLLIN)) { |
| 6456 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 6457 | return true; |
| 6458 | } |
| 6459 | |
| 6460 | ssize_t sizeRead = read(inFd, buffer, sizeof(buffer) - 1); |
| 6461 | if (sizeRead < 0) { |
| 6462 | dprintf(outFd, "Error reading user input. Exiting.\n"); |
| 6463 | return true; |
| 6464 | } |
| 6465 | |
| 6466 | if (sizeRead == 0) { |
| 6467 | // Reached end of input fd (can happen if input is piped) |
| 6468 | // User has no way to signal an interrupt, so interrupt here |
| 6469 | return true; |
| 6470 | } |
| 6471 | |
| 6472 | if (buffer[0] == '\n') { |
| 6473 | // User pressed return, interrupt watch command. |
| 6474 | return true; |
| 6475 | } |
| 6476 | } |
| 6477 | } |
| 6478 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6479 | status_t CameraService::printWatchedTagsUntilInterrupt(const Vector<String16> &args, |
| 6480 | int inFd, int outFd) { |
| 6481 | // Figure out refresh interval, if present in args |
| 6482 | long refreshTimeoutMs = 1000L; // refresh every 1s by default |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6483 | if (args.size() > 2) { |
| 6484 | size_t intervalIdx; // index of '-n' |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6485 | for (intervalIdx = 2; intervalIdx < args.size() && toString16("-n") != args[intervalIdx]; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6486 | intervalIdx++); |
| 6487 | |
| 6488 | size_t intervalValIdx = intervalIdx + 1; |
| 6489 | if (intervalValIdx < args.size()) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6490 | refreshTimeoutMs = strtol(toStdString(args[intervalValIdx]).c_str(), nullptr, 10); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6491 | if (errno) { return BAD_VALUE; } |
| 6492 | } |
| 6493 | } |
| 6494 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6495 | // Set min timeout of 10ms. This prevents edge cases in polling when timeout of 0 is passed. |
| 6496 | refreshTimeoutMs = refreshTimeoutMs < 10 ? 10 : refreshTimeoutMs; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6497 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6498 | dprintf(outFd, "Press return to exit...\n\n"); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6499 | std::map<std::string, std::string> packageNameToLastEvent; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6500 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6501 | while (true) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6502 | bool serviceLock = tryLock(mServiceLock); |
| 6503 | auto cameraClients = mActiveClientManager.getAll(); |
| 6504 | if (serviceLock) { mServiceLock.unlock(); } |
| 6505 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6506 | for (const auto& clientDescriptor : cameraClients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6507 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6508 | if (clientDescriptor == nullptr) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6509 | |
| 6510 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6511 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6512 | if (!isClientWatchedLocked(client.get())) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6513 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6514 | const std::string &packageName = client->getPackageName(); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6515 | // This also initializes the map entries with an empty string |
| 6516 | const std::string& lastPrintedEvent = packageNameToLastEvent[packageName]; |
| 6517 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6518 | std::vector<std::string> latestEvents; |
| 6519 | client->dumpWatchedEventsToVector(latestEvents); |
| 6520 | |
| 6521 | if (!latestEvents.empty()) { |
| 6522 | printNewWatchedEvents(outFd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6523 | clientDescriptor->getKey(), |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6524 | packageName, |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6525 | latestEvents, |
| 6526 | lastPrintedEvent); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6527 | packageNameToLastEvent[packageName] = latestEvents[0]; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6528 | } |
| 6529 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6530 | if (shouldInterruptWatchCommand(inFd, outFd, refreshTimeoutMs)) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6531 | break; |
| 6532 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6533 | } |
| 6534 | return OK; |
| 6535 | } |
| 6536 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6537 | void CameraService::parseClientsToWatchLocked(const std::string &clients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6538 | mWatchedClientPackages.clear(); |
| 6539 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6540 | std::istringstream iss(clients); |
| 6541 | std::string nextClient; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6542 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6543 | while (std::getline(iss, nextClient, ',')) { |
| 6544 | if (nextClient == kWatchAllClientsFlag) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6545 | // Don't need to track any other package if 'all' is present |
| 6546 | mWatchedClientPackages.clear(); |
| 6547 | mWatchedClientPackages.emplace(kWatchAllClientsFlag); |
| 6548 | break; |
| 6549 | } |
| 6550 | |
| 6551 | // track package names |
| 6552 | mWatchedClientPackages.emplace(nextClient); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6553 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6554 | } |
| 6555 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6556 | status_t CameraService::printHelp(int out) { |
| 6557 | return dprintf(out, "Camera service commands:\n" |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6558 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 6559 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 6560 | " 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] | 6561 | " set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" |
| 6562 | " Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" |
| 6563 | " 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] | 6564 | " set-autoframing <VALUE> overrides the autoframing value for AUTO\n" |
| 6565 | " Valid values 0=false, 1=true, 2=auto\n" |
| 6566 | " get-autoframing returns the current override autoframing value\n" |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6567 | " set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" |
| 6568 | " Valid values 0=OFF, 1=ON for JPEG\n" |
| 6569 | " 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] | 6570 | " set-camera-mute <0/1> enable or disable camera muting\n" |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6571 | " set-stream-use-case-override <usecase1> <usecase2> ... override stream use cases\n" |
| 6572 | " Use cases applied in descending resolutions. So usecase1 is assigned to the\n" |
| 6573 | " largest resolution, usecase2 is assigned to the 2nd largest resolution, and so\n" |
| 6574 | " on. In case the number of usecases is smaller than the number of streams, the\n" |
| 6575 | " last use case is assigned to all the remaining streams. In case of multiple\n" |
| 6576 | " streams with the same resolution, the tie-breaker is (JPEG, RAW, YUV, and PRIV)\n" |
| 6577 | " Valid values are (case sensitive): DEFAULT, PREVIEW, STILL_CAPTURE, VIDEO_RECORD,\n" |
| 6578 | " PREVIEW_VIDEO_STILL, VIDEO_CALL, CROPPED_RAW\n" |
| 6579 | " clear-stream-use-case-override clear the stream use case override\n" |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6580 | " set-zoom-override <-1/0/1> enable or disable zoom override\n" |
| 6581 | " 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] | 6582 | " set-watchdog <VALUE> enables or disables the camera service watchdog\n" |
| 6583 | " Valid values 0=disable, 1=enable\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6584 | " watch <start|stop|dump|print|clear> manages tag monitoring in connected clients\n" |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6585 | " help print this message\n"); |
| 6586 | } |
| 6587 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6588 | bool CameraService::isClientWatched(const BasicClient *client) { |
| 6589 | Mutex::Autolock lock(mLogLock); |
| 6590 | return isClientWatchedLocked(client); |
| 6591 | } |
| 6592 | |
| 6593 | bool CameraService::isClientWatchedLocked(const BasicClient *client) { |
| 6594 | return mWatchedClientPackages.find(kWatchAllClientsFlag) != mWatchedClientPackages.end() || |
| 6595 | mWatchedClientPackages.find(client->getPackageName()) != mWatchedClientPackages.end(); |
| 6596 | } |
| 6597 | |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 6598 | int32_t CameraService::updateAudioRestriction() { |
| 6599 | Mutex::Autolock lock(mServiceLock); |
| 6600 | return updateAudioRestrictionLocked(); |
| 6601 | } |
| 6602 | |
| 6603 | int32_t CameraService::updateAudioRestrictionLocked() { |
| 6604 | int32_t mode = 0; |
| 6605 | // iterate through all active client |
| 6606 | for (const auto& i : mActiveClientManager.getAll()) { |
| 6607 | const auto clientSp = i->getValue(); |
| 6608 | mode |= clientSp->getAudioRestriction(); |
| 6609 | } |
| 6610 | |
| 6611 | bool modeChanged = (mAudioRestriction != mode); |
| 6612 | mAudioRestriction = mode; |
| 6613 | if (modeChanged) { |
| 6614 | mAppOps.setCameraAudioRestriction(mode); |
| 6615 | } |
| 6616 | return mode; |
| 6617 | } |
| 6618 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6619 | status_t CameraService::checkIfInjectionCameraIsPresent(const std::string& externalCamId, |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6620 | sp<BasicClient> clientSp) { |
| 6621 | std::unique_ptr<AutoConditionLock> lock = |
| 6622 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 6623 | status_t res = NO_ERROR; |
| 6624 | if ((res = checkIfDeviceIsUsable(externalCamId)) != NO_ERROR) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6625 | ALOGW("Device %s is not usable!", externalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6626 | mInjectionStatusListener->notifyInjectionError( |
| 6627 | externalCamId, UNKNOWN_TRANSACTION); |
| 6628 | clientSp->notifyError( |
| 6629 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 6630 | CaptureResultExtras()); |
| 6631 | |
| 6632 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 6633 | // other clients from connecting in mServiceLockWrapper if held |
| 6634 | mServiceLock.unlock(); |
| 6635 | |
| 6636 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 6637 | int64_t token = clearCallingIdentity(); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6638 | clientSp->disconnect(); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 6639 | restoreCallingIdentity(token); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6640 | |
| 6641 | // Reacquire mServiceLock |
| 6642 | mServiceLock.lock(); |
| 6643 | } |
| 6644 | |
| 6645 | return res; |
| 6646 | } |
| 6647 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 6648 | void CameraService::clearInjectionParameters() { |
| 6649 | { |
| 6650 | Mutex::Autolock lock(mInjectionParametersLock); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6651 | mInjectionInitPending = false; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 6652 | mInjectionInternalCamId = ""; |
| 6653 | } |
| 6654 | mInjectionExternalCamId = ""; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 6655 | mInjectionStatusListener->removeListener(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 6656 | } |
| 6657 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 6658 | } // namespace android |