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 { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 92 | const char* kActivityServiceName = "activity"; |
| 93 | const char* kSensorPrivacyServiceName = "sensor_privacy"; |
| 94 | const char* kAppopsServiceName = "appops"; |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 95 | const char* kProcessInfoServiceName = "processinfo"; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 96 | const char* kVirtualDeviceBackCameraId = "0"; |
| 97 | const char* kVirtualDeviceFrontCameraId = "1"; |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 98 | const char* kUnknownPackageName = "<unknown>"; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 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 | } |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 108 | |
| 109 | static android::PermissionChecker::PermissionResult appOpModeToPermissionResult(int32_t res) { |
| 110 | switch (res) { |
| 111 | case android::AppOpsManager::MODE_ERRORED: |
| 112 | return android::PermissionChecker::PERMISSION_HARD_DENIED; |
| 113 | case android::AppOpsManager::MODE_IGNORED: |
| 114 | return android::PermissionChecker::PERMISSION_SOFT_DENIED; |
| 115 | case android::AppOpsManager::MODE_ALLOWED: |
| 116 | return android::PermissionChecker::PERMISSION_GRANTED; |
| 117 | } |
| 118 | |
| 119 | ALOGE("%s: Unexpected appOpMode %d", __FUNCTION__, res); |
| 120 | return android::PermissionChecker::PERMISSION_HARD_DENIED; |
| 121 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 122 | } // namespace anonymous |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 123 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 124 | namespace android { |
| 125 | |
Austin Borger | ea93124 | 2021-12-13 23:10:41 +0000 | [diff] [blame] | 126 | using namespace camera3; |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 127 | using namespace camera3::SessionConfigurationUtils; |
| 128 | |
| 129 | using binder::Status; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 130 | using companion::virtualnative::IVirtualDeviceManagerNative; |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 131 | using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService; |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 132 | using frameworks::cameraservice::service::implementation::AidlCameraService; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 133 | using hardware::ICamera; |
| 134 | using hardware::ICameraClient; |
| 135 | using hardware::ICameraServiceListener; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 136 | using hardware::camera2::ICameraInjectionCallback; |
| 137 | using hardware::camera2::ICameraInjectionSession; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 138 | using hardware::camera2::utils::CameraIdAndSessionConfiguration; |
| 139 | using hardware::camera2::utils::ConcurrentCameraIdCombination; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 140 | |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 141 | namespace flags = com::android::internal::camera::flags; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 142 | namespace vd_flags = android::companion::virtualdevice::flags; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 143 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 144 | // ---------------------------------------------------------------------------- |
| 145 | // Logging support -- this is for debugging only |
| 146 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 147 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 148 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 149 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 150 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 151 | |
| 152 | static void setLogLevel(int level) { |
| 153 | android_atomic_write(level, &gLogLevel); |
| 154 | } |
| 155 | |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 156 | int32_t format_as(CameraService::StatusInternal s) { |
| 157 | return fmt::underlying(s); |
| 158 | } |
| 159 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 160 | // ---------------------------------------------------------------------------- |
| 161 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 162 | // Permission strings (references to AttributionAndPermissionUtils for brevity) |
| 163 | static const std::string &sDumpPermission = |
| 164 | AttributionAndPermissionUtils::sDumpPermission; |
| 165 | static const std::string &sManageCameraPermission = |
| 166 | AttributionAndPermissionUtils::sManageCameraPermission; |
| 167 | static const std::string &sCameraSendSystemEventsPermission = |
| 168 | AttributionAndPermissionUtils::sCameraSendSystemEventsPermission; |
| 169 | static const std::string &sCameraInjectExternalCameraPermission = |
| 170 | AttributionAndPermissionUtils::sCameraInjectExternalCameraPermission; |
| 171 | |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 172 | // Constant integer for FGS Logging, used to denote the API type for logger |
| 173 | static const int LOG_FGS_CAMERA_API = 1; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 174 | const char *sFileName = "lastOpenSessionDumpFile"; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 175 | static constexpr int32_t kSystemNativeClientScore = resource_policy::PERCEPTIBLE_APP_ADJ; |
| 176 | static constexpr int32_t kSystemNativeClientState = |
| 177 | ActivityManager::PROCESS_STATE_PERSISTENT_UI; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 178 | static const std::string kServiceName("cameraserver"); |
Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame] | 179 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 180 | const std::string CameraService::kOfflineDevice("offline-"); |
| 181 | const std::string CameraService::kWatchAllClientsFlag("all"); |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 182 | |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 183 | constexpr int32_t kInvalidDeviceId = -1; |
| 184 | |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 185 | // Set to keep track of logged service error events. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 186 | static std::set<std::string> sServiceErrorEventSet; |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 187 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 188 | CameraService::CameraService( |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 189 | std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper, |
| 190 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils) : |
| 191 | AttributionAndPermissionUtilsEncapsulator(attributionAndPermissionUtils == nullptr ? |
| 192 | std::make_shared<AttributionAndPermissionUtils>()\ |
| 193 | : attributionAndPermissionUtils), |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 194 | mCameraServiceProxyWrapper(cameraServiceProxyWrapper == nullptr ? |
| 195 | std::make_shared<CameraServiceProxyWrapper>() : cameraServiceProxyWrapper), |
Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 196 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 197 | mNumberOfCameras(0), |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 198 | mNumberOfCamerasWithoutSystemCamera(0), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 199 | mSoundRef(0), mInitialized(false), |
| 200 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 201 | ALOGI("CameraService started (pid=%d)", getpid()); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 202 | mAttributionAndPermissionUtils->setCameraService(this); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 203 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 204 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 205 | if (mMemFd == -1) { |
| 206 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 207 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Charles Chen | a7b613c | 2023-01-24 21:57:33 +0000 | [diff] [blame] | 210 | // Enable processes with isolated AID to request the binder |
| 211 | void CameraService::instantiate() { |
| 212 | CameraService::publish(true); |
| 213 | } |
| 214 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 215 | void CameraService::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 216 | if (name != toString16(kAppopsServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | |
| 220 | ALOGV("appops service registered. setting camera audio restriction"); |
| 221 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
| 222 | } |
| 223 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 224 | void CameraService::onFirstRef() |
| 225 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 226 | ALOGI("CameraService process starting"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 227 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 228 | BnCameraService::onFirstRef(); |
| 229 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 230 | // Update battery life tracking if service is restarting |
| 231 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 232 | notifier.noteResetCamera(); |
| 233 | notifier.noteResetFlashlight(); |
| 234 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 235 | status_t res = INVALID_OPERATION; |
Eino-Ville Talvala | 9cbbc83 | 2017-01-23 15:39:53 -0800 | [diff] [blame] | 236 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 237 | res = enumerateProviders(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 238 | if (res == OK) { |
| 239 | mInitialized = true; |
| 240 | } |
| 241 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 242 | mUidPolicy = new UidPolicy(this); |
| 243 | mUidPolicy->registerSelf(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 244 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this, mAttributionAndPermissionUtils); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 245 | mSensorPrivacyPolicy->registerSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 246 | mInjectionStatusListener = new InjectionStatusListener(this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 247 | |
| 248 | // appops function setCamerAudioRestriction uses getService which |
| 249 | // is blocking till the appops service is ready. To enable early |
| 250 | // boot availability for cameraservice, use checkService which is |
| 251 | // non blocking and register for notifications |
| 252 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 253 | sp<IBinder> binder = sm->checkService(toString16(kAppopsServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 254 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 255 | sm->registerForNotifications(toString16(kAppopsServiceName), this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 256 | } else { |
| 257 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
| 258 | } |
| 259 | |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 260 | sp<HidlCameraService> hcs = HidlCameraService::getInstance(this); |
| 261 | if (hcs->registerAsService() != android::OK) { |
Devin Moore | a1350e7 | 2023-01-11 23:40:42 +0000 | [diff] [blame] | 262 | // Deprecated, so it will fail to register on newer devices |
| 263 | ALOGW("%s: Did not register default android.frameworks.cameraservice.service@2.2", |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 264 | __FUNCTION__); |
| 265 | } |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 266 | |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 267 | if (!AidlCameraService::registerService(this)) { |
| 268 | ALOGE("%s: Failed to register default AIDL VNDK CameraService", __FUNCTION__); |
| 269 | } |
| 270 | |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 271 | // This needs to be last call in this function, so that it's as close to |
| 272 | // ServiceManager::addService() as possible. |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 273 | mCameraServiceProxyWrapper->pingCameraServiceProxy(); |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 274 | ALOGI("CameraService pinged cameraservice proxy"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 277 | status_t CameraService::enumerateProviders() { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 278 | status_t res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 279 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 280 | std::vector<std::string> deviceIds; |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 281 | std::unordered_map<std::string, std::set<std::string>> unavailPhysicalIds; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 282 | { |
| 283 | Mutex::Autolock l(mServiceLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 284 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 285 | if (nullptr == mCameraProviderManager.get()) { |
| 286 | mCameraProviderManager = new CameraProviderManager(); |
| 287 | res = mCameraProviderManager->initialize(this); |
| 288 | if (res != OK) { |
| 289 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", |
| 290 | __FUNCTION__, strerror(-res), res); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 291 | logServiceError("Unable to initialize camera provider manager", |
| 292 | ERROR_DISCONNECTED); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 293 | return res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 297 | // Setup vendor tags before we call get_camera_info the first time |
| 298 | // because HAL might need to setup static vendor keys in get_camera_info |
| 299 | // TODO: maybe put this into CameraProviderManager::initialize()? |
| 300 | mCameraProviderManager->setUpVendorTags(); |
| 301 | |
| 302 | if (nullptr == mFlashlight.get()) { |
| 303 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 306 | res = mFlashlight->findFlashUnits(); |
| 307 | if (res != OK) { |
| 308 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); |
| 309 | } |
| 310 | |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 311 | deviceIds = mCameraProviderManager->getCameraDeviceIds(&unavailPhysicalIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 314 | for (auto& cameraId : deviceIds) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 315 | if (getCameraState(cameraId) == nullptr) { |
| 316 | onDeviceStatusChanged(cameraId, CameraDeviceStatus::PRESENT); |
Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 317 | } |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 318 | if (unavailPhysicalIds.count(cameraId) > 0) { |
| 319 | for (const auto& physicalId : unavailPhysicalIds[cameraId]) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 320 | onDeviceStatusChanged(cameraId, physicalId, CameraDeviceStatus::NOT_PRESENT); |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 325 | // Derive primary rear/front cameras, and filter their charactierstics. |
| 326 | // 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] | 327 | if (SessionConfigurationUtils::IS_PERF_CLASS) { |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 328 | // Assume internal cameras are advertised from the same |
| 329 | // provider. If multiple providers are registered at different time, |
| 330 | // and each provider contains multiple internal color cameras, the current |
| 331 | // logic may filter the characteristics of more than one front/rear color |
| 332 | // cameras. |
| 333 | Mutex::Autolock l(mServiceLock); |
| 334 | filterSPerfClassCharacteristicsLocked(); |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 335 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 336 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 337 | return OK; |
| 338 | } |
| 339 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 340 | void CameraService::broadcastTorchModeStatus(const std::string& cameraId, TorchModeStatus status, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 341 | SystemCameraKind systemCameraKind) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 342 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 343 | auto [deviceId, mappedCameraId] = |
| 344 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 345 | |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 346 | Mutex::Autolock lock(mStatusListenerLock); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 347 | for (auto& i : mListenerList) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 348 | if (shouldSkipStatusUpdates(systemCameraKind, i->isVendorListener(), i->getListenerPid(), |
| 349 | i->getListenerUid())) { |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 350 | ALOGV("%s: Skipping torch callback for system-only camera device %s", |
| 351 | __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 352 | continue; |
| 353 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 354 | |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 355 | auto ret = i->getListener()->onTorchStatusChanged(mapToInterface(status), |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 356 | mappedCameraId, deviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 357 | i->handleBinderStatus(ret, "%s: Failed to trigger onTorchStatusChanged for %d:%d: %d", |
| 358 | __FUNCTION__, i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 362 | CameraService::~CameraService() { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 363 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 364 | mUidPolicy->unregisterSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 365 | mSensorPrivacyPolicy->unregisterSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 366 | mInjectionStatusListener->removeListener(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 369 | void CameraService::onNewProviderRegistered() { |
| 370 | enumerateProviders(); |
| 371 | } |
| 372 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 373 | void CameraService::filterAPI1SystemCameraLocked( |
| 374 | const std::vector<std::string> &normalDeviceIds) { |
| 375 | mNormalDeviceIdsWithoutSystemCamera.clear(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 376 | for (auto &cameraId : normalDeviceIds) { |
| 377 | if (vd_flags::camera_device_awareness()) { |
| 378 | CameraMetadata cameraInfo; |
| 379 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 380 | cameraId, false, &cameraInfo, |
| 381 | hardware::ICameraService::ROTATION_OVERRIDE_NONE); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 382 | int32_t deviceId = kDefaultDeviceId; |
| 383 | if (res != OK) { |
| 384 | ALOGW("%s: Not able to get camera characteristics for camera id %s", |
| 385 | __FUNCTION__, cameraId.c_str()); |
| 386 | } else { |
| 387 | deviceId = getDeviceId(cameraInfo); |
| 388 | } |
| 389 | // Cameras associated with non-default device id's (i.e., virtual cameras) can never be |
| 390 | // system cameras, so skip for non-default device id's. |
| 391 | if (deviceId != kDefaultDeviceId) { |
| 392 | continue; |
| 393 | } |
| 394 | } |
| 395 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 396 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 397 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 398 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 399 | continue; |
| 400 | } |
| 401 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 402 | // All system camera ids will necessarily come after public camera |
| 403 | // device ids as per the HAL interface contract. |
| 404 | break; |
| 405 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 406 | mNormalDeviceIdsWithoutSystemCamera.push_back(cameraId); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 407 | } |
| 408 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, |
| 409 | mNormalDeviceIdsWithoutSystemCamera.size()); |
| 410 | } |
| 411 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 412 | status_t CameraService::getSystemCameraKind(const std::string& cameraId, |
| 413 | SystemCameraKind *kind) const { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 414 | auto state = getCameraState(cameraId); |
| 415 | if (state != nullptr) { |
| 416 | *kind = state->getSystemCameraKind(); |
| 417 | return OK; |
| 418 | } |
| 419 | // Hidden physical camera ids won't have CameraState |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 420 | return mCameraProviderManager->getSystemCameraKind(cameraId, kind); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 423 | void CameraService::updateCameraNumAndIds() { |
| 424 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 425 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); |
| 426 | // Excludes hidden secure cameras |
| 427 | mNumberOfCameras = |
| 428 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; |
| 429 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 430 | mNormalDeviceIds = |
| 431 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 432 | filterAPI1SystemCameraLocked(mNormalDeviceIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 435 | void CameraService::filterSPerfClassCharacteristicsLocked() { |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 436 | // To claim to be S Performance primary cameras, the cameras must be |
| 437 | // backward compatible. So performance class primary camera Ids must be API1 |
| 438 | // compatible. |
| 439 | bool firstRearCameraSeen = false, firstFrontCameraSeen = false; |
| 440 | for (const auto& cameraId : mNormalDeviceIdsWithoutSystemCamera) { |
| 441 | int facing = -1; |
| 442 | int orientation = 0; |
Shuzhen Wang | f221e8d | 2022-12-15 13:26:29 -0800 | [diff] [blame] | 443 | int portraitRotation; |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 444 | getDeviceVersion(cameraId, |
| 445 | /*rotationOverride*/hardware::ICameraService::ROTATION_OVERRIDE_NONE, |
| 446 | /*out*/&portraitRotation, /*out*/&facing, /*out*/&orientation); |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 447 | if (facing == -1) { |
| 448 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.c_str()); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | if ((facing == hardware::CAMERA_FACING_BACK && !firstRearCameraSeen) || |
| 453 | (facing == hardware::CAMERA_FACING_FRONT && !firstFrontCameraSeen)) { |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 454 | status_t res = mCameraProviderManager->filterSmallJpegSizes(cameraId); |
| 455 | if (res == OK) { |
| 456 | mPerfClassPrimaryCameraIds.insert(cameraId); |
| 457 | } else { |
| 458 | ALOGE("%s: Failed to filter small JPEG sizes for performance class primary " |
| 459 | "camera %s: %s(%d)", __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
| 460 | break; |
| 461 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 462 | |
| 463 | if (facing == hardware::CAMERA_FACING_BACK) { |
| 464 | firstRearCameraSeen = true; |
| 465 | } |
| 466 | if (facing == hardware::CAMERA_FACING_FRONT) { |
| 467 | firstFrontCameraSeen = true; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | if (firstRearCameraSeen && firstFrontCameraSeen) { |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 477 | void CameraService::addStates(const std::string& cameraId) { |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 478 | CameraResourceCost cost; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 479 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); |
| 480 | if (res != OK) { |
| 481 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); |
| 482 | return; |
| 483 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 484 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 485 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); |
| 486 | if (res != OK) { |
| 487 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); |
| 488 | return; |
| 489 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 490 | std::vector<std::string> physicalCameraIds; |
| 491 | mCameraProviderManager->isLogicalCamera(cameraId, &physicalCameraIds); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 492 | std::set<std::string> conflicting; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 493 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 494 | conflicting.emplace(cost.conflictingDevices[i]); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | { |
| 498 | Mutex::Autolock lock(mCameraStatesLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 499 | mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost.resourceCost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 500 | conflicting, deviceKind, physicalCameraIds)); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 503 | if (mFlashlight->hasFlashUnit(cameraId)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 504 | Mutex::Autolock al(mTorchStatusMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 505 | mTorchStatusMap.add(cameraId, TorchModeStatus::AVAILABLE_OFF); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 506 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 507 | broadcastTorchModeStatus(cameraId, TorchModeStatus::AVAILABLE_OFF, deviceKind); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 508 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 509 | |
| 510 | updateCameraNumAndIds(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 511 | logDeviceAdded(cameraId, "Device added"); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 512 | } |
| 513 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 514 | void CameraService::removeStates(const std::string& cameraId) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 515 | updateCameraNumAndIds(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 516 | if (mFlashlight->hasFlashUnit(cameraId)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 517 | Mutex::Autolock al(mTorchStatusMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 518 | mTorchStatusMap.removeItem(cameraId); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | { |
| 522 | Mutex::Autolock lock(mCameraStatesLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 523 | mCameraStates.erase(cameraId); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 527 | void CameraService::onDeviceStatusChanged(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 528 | CameraDeviceStatus newHalStatus) { |
| 529 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 530 | cameraId.c_str(), eToI(newHalStatus)); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 531 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 532 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 533 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 534 | std::shared_ptr<CameraState> state = getCameraState(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 535 | |
| 536 | if (state == nullptr) { |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 537 | if (newStatus == StatusInternal::PRESENT) { |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 538 | ALOGI("%s: Unknown camera ID %s, a new camera is added", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 539 | __FUNCTION__, cameraId.c_str()); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 540 | |
| 541 | // First add as absent to make sure clients are notified below |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 542 | addStates(cameraId); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 543 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 544 | updateStatus(newStatus, cameraId); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 545 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 546 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, cameraId.c_str()); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 547 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 548 | return; |
| 549 | } |
| 550 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 551 | StatusInternal oldStatus = state->getStatus(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 552 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 553 | if (oldStatus == newStatus) { |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 554 | ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, |
| 555 | eToI(newStatus)); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 556 | return; |
| 557 | } |
| 558 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 559 | if (newStatus == StatusInternal::NOT_PRESENT) { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 560 | logDeviceRemoved(cameraId, fmt::format("Device status changed from {} to {}", |
| 561 | oldStatus, newStatus)); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 562 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 563 | // to this device until the status changes |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 564 | updateStatus(StatusInternal::NOT_PRESENT, cameraId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 565 | mVirtualDeviceCameraIdMapper.removeCamera(cameraId); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 566 | |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 567 | std::vector<sp<BasicClient>> clientsToDisconnectOnline, clientsToDisconnectOffline; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 568 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 569 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 570 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 571 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 572 | // Remove cached shim parameters |
| 573 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 574 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 575 | // Remove online as well as offline client from the list of active clients, |
| 576 | // if they are present |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 577 | clientsToDisconnectOnline = removeClientsLocked(cameraId); |
| 578 | clientsToDisconnectOffline = removeClientsLocked(kOfflineDevice + cameraId); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 579 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 580 | |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 581 | disconnectClients(cameraId, clientsToDisconnectOnline); |
| 582 | disconnectClients(kOfflineDevice + cameraId, clientsToDisconnectOffline); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 583 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 584 | removeStates(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 585 | } else { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 586 | if (oldStatus == StatusInternal::NOT_PRESENT) { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 587 | logDeviceAdded(cameraId, fmt::format("Device status changed from {} to {}", |
| 588 | oldStatus, newStatus)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 589 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 590 | updateStatus(newStatus, cameraId); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 591 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 592 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 593 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 594 | void CameraService::onDeviceStatusChanged(const std::string& id, |
| 595 | const std::string& physicalId, |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 596 | CameraDeviceStatus newHalStatus) { |
| 597 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 598 | __FUNCTION__, id.c_str(), physicalId.c_str(), eToI(newHalStatus)); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 599 | |
| 600 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 601 | |
| 602 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 603 | |
| 604 | if (state == nullptr) { |
| 605 | 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] | 606 | __FUNCTION__, physicalId.c_str(), id.c_str()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 607 | return; |
| 608 | } |
| 609 | |
| 610 | StatusInternal logicalCameraStatus = state->getStatus(); |
| 611 | if (logicalCameraStatus != StatusInternal::PRESENT && |
| 612 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 613 | 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] | 614 | __FUNCTION__, physicalId.c_str(), eToI(newHalStatus), eToI(logicalCameraStatus)); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 615 | return; |
| 616 | } |
| 617 | |
| 618 | bool updated = false; |
| 619 | if (newStatus == StatusInternal::PRESENT) { |
| 620 | updated = state->removeUnavailablePhysicalId(physicalId); |
| 621 | } else { |
| 622 | updated = state->addUnavailablePhysicalId(physicalId); |
| 623 | } |
| 624 | |
| 625 | if (updated) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 626 | std::string idCombo = id + " : " + physicalId; |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 627 | if (newStatus == StatusInternal::PRESENT) { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 628 | logDeviceAdded(idCombo, fmt::format("Device status changed to {}", newStatus)); |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 629 | } else { |
Henri Chataing | bcb9945 | 2023-11-01 17:40:30 +0000 | [diff] [blame] | 630 | logDeviceRemoved(idCombo, fmt::format("Device status changed to {}", newStatus)); |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 631 | } |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 632 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 633 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 634 | if (getSystemCameraKind(id, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 635 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 636 | return; |
| 637 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 638 | Mutex::Autolock lock(mStatusListenerLock); |
| 639 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 640 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 641 | listener->getListenerPid(), listener->getListenerUid())) { |
| 642 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 643 | id.c_str()); |
| 644 | continue; |
| 645 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 646 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged( |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 647 | mapToInterface(newStatus), id, physicalId, kDefaultDeviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 648 | listener->handleBinderStatus(ret, |
| 649 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 650 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 651 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 652 | } |
| 653 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 656 | void CameraService::disconnectClients(const std::string& id, |
| 657 | std::vector<sp<BasicClient>> clientsToDisconnect) { |
| 658 | for (auto& client : clientsToDisconnect) { |
| 659 | disconnectClient(id, client); |
| 660 | } |
| 661 | } |
| 662 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 663 | void CameraService::disconnectClient(const std::string& id, sp<BasicClient> clientToDisconnect) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 664 | if (clientToDisconnect.get() != nullptr) { |
| 665 | 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] | 666 | __FUNCTION__, id.c_str()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 667 | // Notify the client of disconnection |
| 668 | clientToDisconnect->notifyError( |
| 669 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 670 | CaptureResultExtras{}); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 671 | clientToDisconnect->disconnect(); |
| 672 | } |
| 673 | } |
| 674 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 675 | void CameraService::onTorchStatusChanged(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 676 | TorchModeStatus newStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 677 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 678 | status_t res = getSystemCameraKind(cameraId, &systemCameraKind); |
| 679 | if (res != OK) { |
| 680 | 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] | 681 | cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 682 | return; |
| 683 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 684 | Mutex::Autolock al(mTorchStatusMutex); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 685 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 686 | } |
| 687 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 688 | void CameraService::onTorchStatusChanged(const std::string& cameraId, |
Jayant Chowdhary | 46ef0f5 | 2021-10-05 14:36:13 -0700 | [diff] [blame] | 689 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
| 690 | Mutex::Autolock al(mTorchStatusMutex); |
| 691 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
| 692 | } |
| 693 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 694 | void CameraService::broadcastTorchStrengthLevel(const std::string& cameraId, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 695 | int32_t newStrengthLevel) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 696 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 697 | auto [deviceId, mappedCameraId] = |
| 698 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 699 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 700 | Mutex::Autolock lock(mStatusListenerLock); |
| 701 | for (auto& i : mListenerList) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 702 | auto ret = i->getListener()->onTorchStrengthLevelChanged(mappedCameraId, |
| 703 | newStrengthLevel, deviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 704 | i->handleBinderStatus(ret, |
| 705 | "%s: Failed to trigger onTorchStrengthLevelChanged for %d:%d: %d", __FUNCTION__, |
| 706 | i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 710 | void CameraService::onTorchStatusChangedLocked(const std::string& cameraId, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 711 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 712 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 713 | __FUNCTION__, cameraId.c_str(), eToI(newStatus)); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 714 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 715 | TorchModeStatus status; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 716 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 717 | if (res) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 718 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 719 | __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 720 | return; |
| 721 | } |
| 722 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 723 | return; |
| 724 | } |
| 725 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 726 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 727 | if (res) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 728 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, |
| 729 | (uint32_t)newStatus, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 730 | return; |
| 731 | } |
| 732 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 733 | { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 734 | // Update battery life logging for flashlight |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 735 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 736 | auto iter = mTorchUidMap.find(cameraId); |
| 737 | if (iter != mTorchUidMap.end()) { |
| 738 | int oldUid = iter->second.second; |
| 739 | int newUid = iter->second.first; |
| 740 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 741 | if (oldUid != newUid) { |
| 742 | // 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] | 743 | if (status == TorchModeStatus::AVAILABLE_ON) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 744 | notifier.noteFlashlightOff(toString8(cameraId), oldUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 745 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 746 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 747 | notifier.noteFlashlightOn(toString8(cameraId), newUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 748 | } |
| 749 | iter->second.second = newUid; |
| 750 | } else { |
| 751 | // If the UID has not changed, log the status |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 752 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 753 | notifier.noteFlashlightOn(toString8(cameraId), oldUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 754 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 755 | notifier.noteFlashlightOff(toString8(cameraId), oldUid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 760 | broadcastTorchModeStatus(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 763 | bool CameraService::isAutomotiveExteriorSystemCamera(const std::string& cam_id) const { |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 764 | // Returns false if this is not an automotive device type. |
| 765 | if (!isAutomotiveDevice()) |
| 766 | return false; |
| 767 | |
| 768 | // Returns false if no camera id is provided. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 769 | if (cam_id.empty()) |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 770 | return false; |
| 771 | |
| 772 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 773 | if (getSystemCameraKind(cam_id, &systemCameraKind) != OK) { |
| 774 | // This isn't a known camera ID, so it's not a system camera. |
| 775 | ALOGE("%s: Unknown camera id %s, ", __FUNCTION__, cam_id.c_str()); |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | if (systemCameraKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
| 780 | ALOGE("%s: camera id %s is not a system camera", __FUNCTION__, cam_id.c_str()); |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | CameraMetadata cameraInfo; |
| 785 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 786 | cam_id, false, &cameraInfo, hardware::ICameraService::ROTATION_OVERRIDE_NONE); |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 787 | if (res != OK){ |
| 788 | ALOGE("%s: Not able to get camera characteristics for camera id %s",__FUNCTION__, |
| 789 | cam_id.c_str()); |
| 790 | return false; |
| 791 | } |
| 792 | |
| 793 | camera_metadata_entry auto_location = cameraInfo.find(ANDROID_AUTOMOTIVE_LOCATION); |
| 794 | if (auto_location.count != 1) |
| 795 | return false; |
| 796 | |
| 797 | uint8_t location = auto_location.data.u8[0]; |
| 798 | if ((location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_FRONT) && |
| 799 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_REAR) && |
| 800 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_LEFT) && |
| 801 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_RIGHT)) { |
| 802 | return false; |
| 803 | } |
| 804 | |
| 805 | return true; |
| 806 | } |
| 807 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 808 | Status CameraService::getNumberOfCameras(int32_t type, |
| 809 | const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 810 | int32_t* numCameras) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 811 | ATRACE_CALL(); |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 812 | if (vd_flags::camera_device_awareness() && (clientAttribution.deviceId != kDefaultDeviceId) |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 813 | && (devicePolicy != IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT)) { |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 814 | *numCameras = mVirtualDeviceCameraIdMapper.getNumberOfCameras(clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 815 | return Status::ok(); |
| 816 | } |
| 817 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 818 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 819 | bool hasSystemCameraPermissions = |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 820 | hasPermissionsForSystemCamera(std::string(), getCallingPid(), |
| 821 | getCallingUid()); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 822 | switch (type) { |
| 823 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 824 | if (hasSystemCameraPermissions) { |
| 825 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); |
| 826 | } else { |
| 827 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); |
| 828 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 829 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 830 | case CAMERA_TYPE_ALL: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 831 | if (hasSystemCameraPermissions) { |
| 832 | *numCameras = mNumberOfCameras; |
| 833 | } else { |
| 834 | *numCameras = mNumberOfCamerasWithoutSystemCamera; |
| 835 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 836 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 837 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 838 | ALOGW("%s: Unknown camera type %d", |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 839 | __FUNCTION__, type); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 840 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 841 | "Unknown camera type %d", type); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 842 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 843 | return Status::ok(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 846 | Status CameraService::createDefaultRequest(const std::string& unresolvedCameraId, int templateId, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 847 | const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 848 | /* out */ |
| 849 | hardware::camera2::impl::CameraMetadataNative* request) { |
| 850 | ATRACE_CALL(); |
| 851 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 852 | if (!mInitialized) { |
| 853 | ALOGE("%s: Camera subsystem is not available", __FUNCTION__); |
| 854 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 855 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 856 | } |
| 857 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 858 | std::optional<std::string> cameraIdOptional = |
| 859 | resolveCameraId(unresolvedCameraId, clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 860 | if (!cameraIdOptional.has_value()) { |
| 861 | 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] | 862 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 863 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 864 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 865 | } |
| 866 | std::string cameraId = cameraIdOptional.value(); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 867 | |
| 868 | binder::Status res; |
| 869 | if (request == nullptr) { |
| 870 | res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, |
| 871 | "Camera %s: Error creating default request", cameraId.c_str()); |
| 872 | return res; |
| 873 | } |
| 874 | camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT; |
| 875 | res = SessionConfigurationUtils::mapRequestTemplateFromClient( |
| 876 | cameraId, templateId, &tempId); |
| 877 | if (!res.isOk()) { |
| 878 | ALOGE("%s: Camera %s: failed to map request Template %d", |
| 879 | __FUNCTION__, cameraId.c_str(), templateId); |
| 880 | return res; |
| 881 | } |
| 882 | |
| 883 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 884 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to create default" |
| 885 | "request for system only device %s: ", cameraId.c_str()); |
| 886 | } |
| 887 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 888 | CameraMetadata metadata; |
| 889 | status_t err = mCameraProviderManager->createDefaultRequest(cameraId, tempId, &metadata); |
| 890 | if (err == OK) { |
| 891 | request->swap(metadata); |
| 892 | } else if (err == BAD_VALUE) { |
| 893 | res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT, |
| 894 | "Camera %s: Template ID %d is invalid or not supported: %s (%d)", |
| 895 | cameraId.c_str(), templateId, strerror(-err), err); |
| 896 | } else { |
| 897 | res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, |
| 898 | "Camera %s: Error creating default request for template %d: %s (%d)", |
| 899 | cameraId.c_str(), templateId, strerror(-err), err); |
| 900 | } |
| 901 | return res; |
| 902 | } |
| 903 | |
| 904 | Status CameraService::isSessionConfigurationWithParametersSupported( |
Avichal Rakesh | caf179b | 2024-04-04 18:42:46 -0700 | [diff] [blame] | 905 | const std::string& unresolvedCameraId, int targetSdkVersion, |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 906 | const SessionConfiguration& sessionConfiguration, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 907 | const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
Avichal Rakesh | caf179b | 2024-04-04 18:42:46 -0700 | [diff] [blame] | 908 | /*out*/ bool* supported) { |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 909 | ATRACE_CALL(); |
| 910 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 911 | if (!mInitialized) { |
| 912 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 913 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 914 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 915 | } |
| 916 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 917 | std::optional<std::string> cameraIdOptional = |
| 918 | resolveCameraId(unresolvedCameraId, clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 919 | if (!cameraIdOptional.has_value()) { |
| 920 | 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] | 921 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 922 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 923 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 924 | } |
| 925 | std::string cameraId = cameraIdOptional.value(); |
| 926 | |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 927 | if (supported == nullptr) { |
| 928 | std::string msg = fmt::sprintf("Camera %s: Invalid 'support' input!", |
| 929 | unresolvedCameraId.c_str()); |
| 930 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 931 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 932 | } |
| 933 | |
| 934 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 935 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query " |
| 936 | "session configuration with parameters support for system only device %s: ", |
| 937 | cameraId.c_str()); |
| 938 | } |
| 939 | |
Avichal Rakesh | e005df5 | 2024-09-25 17:13:27 -0700 | [diff] [blame] | 940 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
Avichal Rakesh | caf179b | 2024-04-04 18:42:46 -0700 | [diff] [blame] | 941 | mPerfClassPrimaryCameraIds, cameraId, targetSdkVersion); |
| 942 | |
Shuzhen Wang | e3e8e73 | 2024-05-22 17:48:01 -0700 | [diff] [blame] | 943 | auto ret = isSessionConfigurationWithParametersSupportedUnsafe(cameraId, |
| 944 | sessionConfiguration, overrideForPerfClass, supported); |
| 945 | if (flags::analytics_24q3()) { |
| 946 | mCameraServiceProxyWrapper->logFeatureCombinationQuery(cameraId, |
| 947 | getCallingUid(), sessionConfiguration, ret); |
| 948 | } |
| 949 | return ret; |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | Status CameraService::isSessionConfigurationWithParametersSupportedUnsafe( |
| 953 | const std::string& cameraId, const SessionConfiguration& sessionConfiguration, |
| 954 | bool overrideForPerfClass, /*out*/ bool* supported) { |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 955 | *supported = false; |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 956 | status_t ret = mCameraProviderManager->isSessionConfigurationSupported( |
| 957 | cameraId, sessionConfiguration, overrideForPerfClass, |
| 958 | /*checkSessionParams=*/true, supported); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 959 | binder::Status res; |
| 960 | switch (ret) { |
| 961 | case OK: |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 962 | // Expected. Do Nothing. |
| 963 | return Status::ok(); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 964 | case INVALID_OPERATION: { |
| 965 | std::string msg = fmt::sprintf( |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 966 | "Camera %s: Session configuration with parameters supported query not " |
| 967 | "supported!", |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 968 | cameraId.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 969 | ALOGW("%s: %s", __FUNCTION__, msg.c_str()); |
| 970 | logServiceError(msg, CameraService::ERROR_INVALID_OPERATION); |
| 971 | *supported = false; |
| 972 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 973 | } |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 974 | break; |
| 975 | case NAME_NOT_FOUND: { |
| 976 | std::string msg = fmt::sprintf("Camera %s: Unknown camera ID.", cameraId.c_str()); |
| 977 | ALOGW("%s: %s", __FUNCTION__, msg.c_str()); |
| 978 | logServiceError(msg, CameraService::ERROR_ILLEGAL_ARGUMENT); |
| 979 | *supported = false; |
| 980 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 981 | } |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 982 | break; |
| 983 | default: { |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 984 | std::string msg = fmt::sprintf( |
| 985 | "Unable to retrieve session configuration support for camera " |
| 986 | "device %s: Error: %s (%d)", |
| 987 | cameraId.c_str(), strerror(-ret), ret); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 988 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 989 | logServiceError(msg, CameraService::ERROR_ILLEGAL_ARGUMENT); |
| 990 | *supported = false; |
| 991 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 992 | } |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 993 | break; |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 994 | } |
Shuzhen Wang | 045be6c | 2023-10-12 10:01:10 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 997 | Status CameraService::getSessionCharacteristics(const std::string& unresolvedCameraId, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 998 | int targetSdkVersion, int rotationOverride, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 999 | const SessionConfiguration& sessionConfiguration, |
| 1000 | const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1001 | /*out*/ CameraMetadata* outMetadata) { |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1002 | ATRACE_CALL(); |
| 1003 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1004 | if (outMetadata == nullptr) { |
| 1005 | std::string msg = |
| 1006 | fmt::sprintf("Camera %s: Invalid 'outMetadata' input!", unresolvedCameraId.c_str()); |
| 1007 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1008 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1009 | } |
| 1010 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1011 | if (!mInitialized) { |
| 1012 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 1013 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
| 1014 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem is not available"); |
| 1015 | } |
| 1016 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1017 | std::optional<std::string> cameraIdOptional = |
| 1018 | resolveCameraId(unresolvedCameraId, clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1019 | if (!cameraIdOptional.has_value()) { |
| 1020 | 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] | 1021 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1022 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1023 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1024 | } |
| 1025 | std::string cameraId = cameraIdOptional.value(); |
| 1026 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1027 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1028 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1029 | "Unable to retrieve camera" |
| 1030 | "characteristics for system only device %s: ", |
| 1031 | cameraId.c_str()); |
| 1032 | } |
| 1033 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1034 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
| 1035 | mPerfClassPrimaryCameraIds, cameraId, targetSdkVersion); |
Avichal Rakesh | eb961c7 | 2024-09-25 17:26:26 -0700 | [diff] [blame] | 1036 | |
| 1037 | bool sessionConfigSupported; |
| 1038 | Status res = isSessionConfigurationWithParametersSupportedUnsafe( |
| 1039 | cameraId, sessionConfiguration, overrideForPerfClass, &sessionConfigSupported); |
| 1040 | if (!res.isOk()) { |
| 1041 | // isSessionConfigurationWithParametersSupportedUnsafe should log what went wrong and |
| 1042 | // report the correct Status to send to the client. Simply forward the error to |
| 1043 | // the client. |
| 1044 | outMetadata->clear(); |
| 1045 | return res; |
| 1046 | } |
| 1047 | |
| 1048 | if (!sessionConfigSupported) { |
| 1049 | std::string msg = fmt::sprintf("Session configuration not supported for camera device %s.", |
| 1050 | cameraId.c_str()); |
| 1051 | outMetadata->clear(); |
| 1052 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 1053 | } |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1054 | |
| 1055 | status_t ret = mCameraProviderManager->getSessionCharacteristics( |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1056 | cameraId, sessionConfiguration, overrideForPerfClass, rotationOverride, outMetadata); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1057 | |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1058 | switch (ret) { |
| 1059 | case OK: |
| 1060 | // Expected, no handling needed. |
| 1061 | break; |
| 1062 | case INVALID_OPERATION: { |
| 1063 | std::string msg = fmt::sprintf( |
| 1064 | "Camera %s: Session characteristics query not supported!", |
| 1065 | cameraId.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 1066 | ALOGW("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1067 | logServiceError(msg, CameraService::ERROR_INVALID_OPERATION); |
| 1068 | outMetadata->clear(); |
| 1069 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
| 1070 | } |
| 1071 | break; |
| 1072 | case NAME_NOT_FOUND: { |
| 1073 | std::string msg = fmt::sprintf( |
| 1074 | "Camera %s: Unknown camera ID.", |
| 1075 | cameraId.c_str()); |
Avichal Rakesh | 8fbda41 | 2024-04-04 17:16:33 -0700 | [diff] [blame] | 1076 | ALOGW("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1077 | logServiceError(msg, CameraService::ERROR_ILLEGAL_ARGUMENT); |
| 1078 | outMetadata->clear(); |
| 1079 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1080 | } |
| 1081 | break; |
| 1082 | default: { |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1083 | std::string msg = fmt::sprintf( |
| 1084 | "Unable to retrieve session characteristics for camera device %s: " |
| 1085 | "Error: %s (%d)", |
| 1086 | cameraId.c_str(), strerror(-ret), ret); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1087 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1088 | logServiceError(msg, CameraService::ERROR_INVALID_OPERATION); |
| 1089 | outMetadata->clear(); |
| 1090 | return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str()); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
Avichal Rakesh | eb961c7 | 2024-09-25 17:26:26 -0700 | [diff] [blame] | 1094 | res = filterSensitiveMetadataIfNeeded(cameraId, outMetadata); |
Shuzhen Wang | e3e8e73 | 2024-05-22 17:48:01 -0700 | [diff] [blame] | 1095 | if (flags::analytics_24q3()) { |
| 1096 | mCameraServiceProxyWrapper->logSessionCharacteristicsQuery(cameraId, |
| 1097 | getCallingUid(), sessionConfiguration, res); |
| 1098 | } |
| 1099 | return res; |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | Status CameraService::filterSensitiveMetadataIfNeeded( |
| 1103 | const std::string& cameraId, CameraMetadata* metadata) { |
| 1104 | int callingPid = getCallingPid(); |
| 1105 | int callingUid = getCallingUid(); |
| 1106 | |
| 1107 | if (callingPid == getpid()) { |
| 1108 | // Caller is cameraserver; no need to remove keys |
| 1109 | return Status::ok(); |
| 1110 | } |
| 1111 | |
| 1112 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1113 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 1114 | ALOGE("%s: Couldn't get camera kind for camera id %s", __FUNCTION__, cameraId.c_str()); |
| 1115 | metadata->clear(); |
| 1116 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1117 | "Unable to retrieve camera kind for device %s", cameraId.c_str()); |
| 1118 | } |
| 1119 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
| 1120 | // Attempting to query system only camera without system camera permission would have |
| 1121 | // failed the shouldRejectSystemCameraConnection in the caller. So if we get here |
| 1122 | // for a system only camera, then the caller has the required permission. |
| 1123 | // No need to remove keys |
| 1124 | return Status::ok(); |
| 1125 | } |
| 1126 | |
| 1127 | std::vector<int32_t> tagsRemoved; |
Biswarup Pal | e506e73 | 2024-03-27 13:46:20 +0000 | [diff] [blame] | 1128 | // Get the device id that owns this camera. |
| 1129 | auto [cameraOwnerDeviceId, _] = mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair( |
| 1130 | cameraId); |
| 1131 | bool hasCameraPermission = hasPermissionsForCamera(cameraId, callingPid, callingUid, |
| 1132 | cameraOwnerDeviceId); |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1133 | if (hasCameraPermission) { |
| 1134 | // Caller has camera permission; no need to remove keys |
| 1135 | return Status::ok(); |
| 1136 | } |
| 1137 | |
| 1138 | status_t ret = metadata->removePermissionEntries( |
| 1139 | mCameraProviderManager->getProviderTagIdLocked(cameraId), &tagsRemoved); |
| 1140 | if (ret != OK) { |
| 1141 | metadata->clear(); |
| 1142 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1143 | "Failed to remove camera characteristics needing camera permission " |
| 1144 | "for device %s:%s (%d)", |
| 1145 | cameraId.c_str(), strerror(-ret), ret); |
| 1146 | } |
| 1147 | |
| 1148 | if (!tagsRemoved.empty()) { |
| 1149 | ret = metadata->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, |
| 1150 | tagsRemoved.data(), tagsRemoved.size()); |
| 1151 | if (ret != OK) { |
| 1152 | metadata->clear(); |
| 1153 | return STATUS_ERROR_FMT( |
| 1154 | ERROR_INVALID_OPERATION, |
| 1155 | "Failed to insert camera keys needing permission for device %s: %s (%d)", |
| 1156 | cameraId.c_str(), strerror(-ret), ret); |
| 1157 | } |
| 1158 | } |
| 1159 | return Status::ok(); |
Avichal Rakesh | 3c522e2 | 2024-02-07 16:40:46 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1162 | Status CameraService::injectSessionParams( |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1163 | const std::string& cameraId, |
| 1164 | const CameraMetadata& sessionParams) { |
| 1165 | if (!checkCallingPermission(toString16(sCameraInjectExternalCameraPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1166 | const int pid = getCallingPid(); |
| 1167 | const int uid = getCallingUid(); |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1168 | ALOGE("%s: Permission Denial: can't inject session params pid=%d, uid=%d", |
| 1169 | __FUNCTION__, pid, uid); |
| 1170 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 1171 | "Permission Denial: no permission to inject session params"); |
| 1172 | } |
| 1173 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1174 | // Do not allow session params injection for a virtual camera. |
| 1175 | auto [deviceId, _] = mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 1176 | if (deviceId != kDefaultDeviceId) { |
| 1177 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1178 | "Cannot inject session params for a virtual camera"); |
| 1179 | } |
| 1180 | |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 1181 | std::unique_ptr<AutoConditionLock> serviceLockWrapper = |
| 1182 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 1183 | |
| 1184 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 1185 | if (clientDescriptor == nullptr) { |
| 1186 | ALOGI("%s: No active client for camera id %s", __FUNCTION__, cameraId.c_str()); |
| 1187 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1188 | "No active client for camera id %s", cameraId.c_str()); |
| 1189 | } |
| 1190 | |
| 1191 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1192 | status_t res = clientSp->injectSessionParams(sessionParams); |
| 1193 | |
| 1194 | if (res != OK) { |
| 1195 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1196 | "Error injecting session params into camera \"%s\": %s (%d)", |
| 1197 | cameraId.c_str(), strerror(-res), res); |
| 1198 | } |
| 1199 | return Status::ok(); |
| 1200 | } |
| 1201 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1202 | std::optional<std::string> CameraService::resolveCameraId( |
| 1203 | const std::string& inputCameraId, |
| 1204 | int32_t deviceId, |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 1205 | int32_t devicePolicy) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1206 | if ((deviceId == kDefaultDeviceId) |
| 1207 | || (devicePolicy == IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT)) { |
| 1208 | auto [storedDeviceId, _] = |
| 1209 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(inputCameraId); |
| 1210 | if (storedDeviceId != kDefaultDeviceId) { |
| 1211 | // Trying to access a virtual camera from default-policy device context, we should fail. |
| 1212 | std::string msg = fmt::sprintf("Camera %s: Invalid camera id for device id %d", |
| 1213 | inputCameraId.c_str(), deviceId); |
| 1214 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1215 | return std::nullopt; |
| 1216 | } |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 1217 | return inputCameraId; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | return mVirtualDeviceCameraIdMapper.getActualCameraId(deviceId, inputCameraId); |
| 1221 | } |
| 1222 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1223 | Status CameraService::getCameraInfo(int cameraId, int rotationOverride, |
| 1224 | const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
| 1225 | CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1226 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 1227 | Mutex::Autolock l(mServiceLock); |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1228 | std::string cameraIdStr = |
| 1229 | cameraIdIntToStrLocked(cameraId, clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1230 | if (cameraIdStr.empty()) { |
| 1231 | 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] | 1232 | cameraId, clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1233 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1234 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1235 | } |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 1236 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1237 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1238 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 1239 | "characteristics for system only device %s: ", cameraIdStr.c_str()); |
| 1240 | } |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 1241 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1242 | if (!mInitialized) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1243 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1244 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1245 | "Camera subsystem is not available"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1246 | } |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1247 | bool hasSystemCameraPermissions = hasPermissionsForSystemCamera(std::to_string(cameraId), |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1248 | getCallingPid(), getCallingUid()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1249 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; |
| 1250 | if (hasSystemCameraPermissions) { |
| 1251 | cameraIdBound = mNumberOfCameras; |
| 1252 | } |
| 1253 | if (cameraId < 0 || cameraId >= cameraIdBound) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1254 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1255 | "CameraId is not valid"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1258 | Status ret = Status::ok(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1259 | int portraitRotation; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1260 | status_t err = mCameraProviderManager->getCameraInfo( |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1261 | cameraIdStr, rotationOverride, &portraitRotation, cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1262 | if (err != OK) { |
| 1263 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1264 | "Error retrieving camera info from device %d: %s (%d)", cameraId, |
| 1265 | strerror(-err), err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1266 | logServiceError(std::string("Error retrieving camera info from device ") |
| 1267 | + std::to_string(cameraId), ERROR_INVALID_OPERATION); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1268 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1269 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1270 | return ret; |
| 1271 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1272 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1273 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt, |
| 1274 | int32_t deviceId, int32_t devicePolicy) { |
| 1275 | if (vd_flags::camera_device_awareness() && (deviceId != kDefaultDeviceId) |
| 1276 | && (devicePolicy != IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT)) { |
| 1277 | std::optional<std::string> cameraIdOptional = |
| 1278 | mVirtualDeviceCameraIdMapper.getActualCameraId(cameraIdInt, deviceId); |
| 1279 | return cameraIdOptional.has_value() ? cameraIdOptional.value() : std::string{}; |
| 1280 | } |
| 1281 | |
| 1282 | const std::vector<std::string> *cameraIds = &mNormalDeviceIdsWithoutSystemCamera; |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1283 | auto callingPid = getCallingPid(); |
| 1284 | auto callingUid = getCallingUid(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1285 | bool systemCameraPermissions = hasPermissionsForSystemCamera(std::to_string(cameraIdInt), |
| 1286 | callingPid, callingUid, /* checkCameraPermissions= */ false); |
| 1287 | if (systemCameraPermissions || getpid() == callingPid) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1288 | cameraIds = &mNormalDeviceIds; |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 1289 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1290 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(cameraIds->size())) { |
| 1291 | ALOGE("%s: input id %d invalid: valid range (0, %zu)", |
| 1292 | __FUNCTION__, cameraIdInt, cameraIds->size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1293 | return std::string{}; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1294 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1295 | |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 1296 | return (*cameraIds)[cameraIdInt]; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1299 | std::string CameraService::cameraIdIntToStr(int cameraIdInt, int32_t deviceId, |
| 1300 | int32_t devicePolicy) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1301 | Mutex::Autolock lock(mServiceLock); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1302 | return cameraIdIntToStrLocked(cameraIdInt, deviceId, devicePolicy); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1305 | Status CameraService::getCameraCharacteristics(const std::string& unresolvedCameraId, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1306 | int targetSdkVersion, int rotationOverride, const AttributionSourceState& clientAttribution, |
| 1307 | int32_t devicePolicy, CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1308 | ATRACE_CALL(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1309 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1310 | if (!cameraInfo) { |
| 1311 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1312 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1315 | if (!mInitialized) { |
| 1316 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1317 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1318 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1319 | "Camera subsystem is not available");; |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1322 | std::optional<std::string> cameraIdOptional = |
| 1323 | resolveCameraId(unresolvedCameraId, clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1324 | if (!cameraIdOptional.has_value()) { |
| 1325 | 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] | 1326 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1327 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1328 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1329 | } |
| 1330 | std::string cameraId = cameraIdOptional.value(); |
| 1331 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1332 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1333 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1334 | "characteristics for system only device %s: ", cameraId.c_str()); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1337 | bool overrideForPerfClass = |
| 1338 | SessionConfigurationUtils::targetPerfClassPrimaryCamera(mPerfClassPrimaryCameraIds, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1339 | cameraId, targetSdkVersion); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1340 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1341 | cameraId, overrideForPerfClass, cameraInfo, rotationOverride); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1342 | if (res != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1343 | if (res == NAME_NOT_FOUND) { |
| 1344 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1345 | "characteristics for unknown device %s: %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1346 | strerror(-res), res); |
| 1347 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1348 | logServiceError(fmt::sprintf("Unable to retrieve camera characteristics for device %s.", |
| 1349 | cameraId.c_str()), ERROR_INVALID_OPERATION); |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1350 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1351 | "characteristics for device %s: %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1352 | strerror(-res), res); |
| 1353 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1354 | } |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 1355 | |
Avichal Rakesh | 4baf726 | 2024-03-20 19:16:04 -0700 | [diff] [blame] | 1356 | return filterSensitiveMetadataIfNeeded(cameraId, cameraInfo); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1359 | Status CameraService::getTorchStrengthLevel(const std::string& unresolvedCameraId, |
| 1360 | const AttributionSourceState& clientAttribution, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1361 | int32_t devicePolicy, int32_t* torchStrength) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1362 | ATRACE_CALL(); |
| 1363 | Mutex::Autolock l(mServiceLock); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1364 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 1365 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, |
| 1366 | clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1367 | if (!cameraIdOptional.has_value()) { |
| 1368 | 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] | 1369 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1370 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 1371 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 1372 | } |
| 1373 | std::string cameraId = cameraIdOptional.value(); |
| 1374 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1375 | if (!mInitialized) { |
| 1376 | ALOGE("%s: Camera HAL couldn't be initialized.", __FUNCTION__); |
| 1377 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera HAL couldn't be initialized."); |
| 1378 | } |
| 1379 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 1380 | if (torchStrength == NULL) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1381 | ALOGE("%s: strength level must not be null.", __FUNCTION__); |
| 1382 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Strength level should not be null."); |
| 1383 | } |
| 1384 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1385 | status_t res = mCameraProviderManager->getTorchStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1386 | if (res != OK) { |
| 1387 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve torch " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1388 | "strength level for device %s: %s (%d)", cameraId.c_str(), |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 1389 | strerror(-res), res); |
| 1390 | } |
| 1391 | ALOGI("%s: Torch strength level is: %d", __FUNCTION__, *torchStrength); |
| 1392 | return Status::ok(); |
| 1393 | } |
| 1394 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1395 | std::string CameraService::getFormattedCurrentTime() { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1396 | time_t now = time(nullptr); |
| 1397 | char formattedTime[64]; |
| 1398 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1399 | return std::string(formattedTime); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1402 | Status CameraService::getCameraVendorTagDescriptor( |
| 1403 | /*out*/ |
| 1404 | hardware::camera2::params::VendorTagDescriptor* desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1405 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1406 | if (!mInitialized) { |
| 1407 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1408 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 1409 | } |
Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 1410 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 1411 | if (globalDescriptor != nullptr) { |
| 1412 | *desc = *(globalDescriptor.get()); |
| 1413 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1414 | return Status::ok(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 1415 | } |
| 1416 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 1417 | Status CameraService::getCameraVendorTagCache( |
| 1418 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { |
| 1419 | ATRACE_CALL(); |
| 1420 | if (!mInitialized) { |
| 1421 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 1422 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1423 | "Camera subsystem not available"); |
| 1424 | } |
| 1425 | sp<VendorTagDescriptorCache> globalCache = |
| 1426 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 1427 | if (globalCache != nullptr) { |
| 1428 | *cache = *(globalCache.get()); |
| 1429 | } |
| 1430 | return Status::ok(); |
| 1431 | } |
| 1432 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 1433 | void CameraService::clearCachedVariables() { |
| 1434 | BasicClient::BasicClient::sCameraService = nullptr; |
| 1435 | } |
| 1436 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1437 | std::pair<int, IPCTransport> CameraService::getDeviceVersion(const std::string& cameraId, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1438 | int rotationOverride, int* portraitRotation, int* facing, |
| 1439 | int* orientation) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1440 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1441 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1442 | int deviceVersion = 0; |
| 1443 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1444 | status_t res; |
| 1445 | hardware::hidl_version maxVersion{0,0}; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1446 | IPCTransport transport = IPCTransport::INVALID; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1447 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId, &maxVersion, &transport); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1448 | if (res != OK || transport == IPCTransport::INVALID) { |
| 1449 | 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] | 1450 | cameraId.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1451 | return std::make_pair(-1, IPCTransport::INVALID) ; |
| 1452 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1453 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1454 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1455 | hardware::CameraInfo info; |
| 1456 | if (facing) { |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1457 | res = mCameraProviderManager->getCameraInfo(cameraId, rotationOverride, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1458 | portraitRotation, &info); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1459 | if (res != OK) { |
| 1460 | return std::make_pair(-1, IPCTransport::INVALID); |
| 1461 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1462 | *facing = info.facing; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1463 | if (orientation) { |
| 1464 | *orientation = info.orientation; |
| 1465 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1466 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1467 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1468 | return std::make_pair(deviceVersion, transport); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1469 | } |
| 1470 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1471 | Status CameraService::filterGetInfoErrorCode(status_t err) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1472 | switch(err) { |
| 1473 | case NO_ERROR: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1474 | return Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1475 | case BAD_VALUE: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1476 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1477 | "CameraId is not valid for HAL module"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1478 | case NO_INIT: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1479 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1480 | "Camera device not available"); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1481 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1482 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1483 | "Camera HAL encountered error %d: %s", |
| 1484 | err, strerror(-err)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1485 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 1488 | Status CameraService::makeClient( |
| 1489 | const sp<CameraService>& cameraService, const sp<IInterface>& cameraCb, |
| 1490 | const AttributionSourceState& clientAttribution, int callingPid, bool systemNativeClient, |
| 1491 | const std::string& cameraId, int api1CameraId, int facing, int sensorOrientation, |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1492 | int servicePid, std::pair<int, IPCTransport> deviceVersionAndTransport, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1493 | apiLevel effectiveApiLevel, bool overrideForPerfClass, int rotationOverride, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 1494 | bool forceSlowJpegMode, const std::string& originalCameraId, bool sharedMode, |
| 1495 | /*out*/sp<BasicClient>* client) { |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1496 | // For HIDL devices |
| 1497 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 1498 | // Create CameraClient based on device version reported by the HAL. |
| 1499 | int deviceVersion = deviceVersionAndTransport.first; |
| 1500 | switch(deviceVersion) { |
| 1501 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 1502 | ALOGE("Camera using old HAL version: %d", deviceVersion); |
| 1503 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, |
| 1504 | "Camera device \"%s\" HAL version %d no longer supported", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1505 | cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1506 | break; |
| 1507 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 1508 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 1509 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 1510 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 1511 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 1512 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 1513 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 1514 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 1515 | break; |
| 1516 | default: |
| 1517 | // Should not be reachable |
| 1518 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 1519 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1520 | "Camera device \"%s\" has unknown HAL version %d", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1521 | cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1522 | } |
| 1523 | } |
| 1524 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 1525 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1526 | *client = new Camera2Client(cameraService, tmp, cameraService->mCameraServiceProxyWrapper, |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 1527 | cameraService->mAttributionAndPermissionUtils, |
| 1528 | clientAttribution, callingPid, cameraId, api1CameraId, facing, |
| 1529 | sensorOrientation, servicePid, overrideForPerfClass, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 1530 | rotationOverride, forceSlowJpegMode, /*sharedMode*/false); |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1531 | ALOGI("%s: Camera1 API (legacy), rotationOverride %d, forceSlowJpegMode %d", |
| 1532 | __FUNCTION__, rotationOverride, forceSlowJpegMode); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1533 | } else { // Camera2 API route |
| 1534 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = |
| 1535 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 1536 | *client = new CameraDeviceClient( |
| 1537 | cameraService, tmp, cameraService->mCameraServiceProxyWrapper, |
| 1538 | cameraService->mAttributionAndPermissionUtils, clientAttribution, callingPid, |
| 1539 | systemNativeClient, cameraId, facing, sensorOrientation, servicePid, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 1540 | overrideForPerfClass, rotationOverride, originalCameraId, sharedMode); |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 1541 | ALOGI("%s: Camera2 API, rotationOverride %d", __FUNCTION__, rotationOverride); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1542 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1543 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1546 | std::string CameraService::toString(std::set<userid_t> intSet) { |
| 1547 | std::ostringstream s; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1548 | bool first = true; |
| 1549 | for (userid_t i : intSet) { |
| 1550 | if (first) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1551 | s << std::to_string(i); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1552 | first = false; |
| 1553 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1554 | s << ", " << std::to_string(i); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1555 | } |
| 1556 | } |
Yi Kong | 3ac211f | 2024-08-12 07:31:44 +0800 | [diff] [blame] | 1557 | return s.str(); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1560 | int32_t CameraService::mapToInterface(TorchModeStatus status) { |
| 1561 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1562 | switch (status) { |
| 1563 | case TorchModeStatus::NOT_AVAILABLE: |
| 1564 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1565 | break; |
| 1566 | case TorchModeStatus::AVAILABLE_OFF: |
| 1567 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 1568 | break; |
| 1569 | case TorchModeStatus::AVAILABLE_ON: |
| 1570 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
| 1571 | break; |
| 1572 | default: |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 1573 | ALOGW("Unknown new flash status: %d", eToI(status)); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1574 | } |
| 1575 | return serviceStatus; |
| 1576 | } |
| 1577 | |
| 1578 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { |
| 1579 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; |
| 1580 | switch (status) { |
| 1581 | case CameraDeviceStatus::NOT_PRESENT: |
| 1582 | serviceStatus = StatusInternal::NOT_PRESENT; |
| 1583 | break; |
| 1584 | case CameraDeviceStatus::PRESENT: |
| 1585 | serviceStatus = StatusInternal::PRESENT; |
| 1586 | break; |
| 1587 | case CameraDeviceStatus::ENUMERATING: |
| 1588 | serviceStatus = StatusInternal::ENUMERATING; |
| 1589 | break; |
| 1590 | default: |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 1591 | ALOGW("Unknown new HAL device status: %d", eToI(status)); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1592 | } |
| 1593 | return serviceStatus; |
| 1594 | } |
| 1595 | |
| 1596 | int32_t CameraService::mapToInterface(StatusInternal status) { |
| 1597 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1598 | switch (status) { |
| 1599 | case StatusInternal::NOT_PRESENT: |
| 1600 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1601 | break; |
| 1602 | case StatusInternal::PRESENT: |
| 1603 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; |
| 1604 | break; |
| 1605 | case StatusInternal::ENUMERATING: |
| 1606 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; |
| 1607 | break; |
| 1608 | case StatusInternal::NOT_AVAILABLE: |
| 1609 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; |
| 1610 | break; |
| 1611 | case StatusInternal::UNKNOWN: |
| 1612 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; |
| 1613 | break; |
| 1614 | default: |
Eino-Ville Talvala | b772320 | 2024-06-24 17:45:51 -0700 | [diff] [blame] | 1615 | ALOGW("Unknown new internal device status: %d", eToI(status)); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1616 | } |
| 1617 | return serviceStatus; |
| 1618 | } |
| 1619 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1620 | Status CameraService::initializeShimMetadata(int cameraId) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1621 | int uid = getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1622 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1623 | std::string cameraIdStr = std::to_string(cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1624 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1625 | sp<Client> tmp = nullptr; |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 1626 | |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 1627 | int callingPid = getCallingPid(); |
| 1628 | logConnectionAttempt(callingPid, kServiceName, cameraIdStr, API_1); |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 1629 | |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 1630 | AttributionSourceState clientAttribution = |
| 1631 | buildAttributionSource(callingPid, uid, kServiceName, kDefaultDeviceId); |
| 1632 | |
| 1633 | if (!(ret = connectHelper<ICameraClient, Client>( |
| 1634 | sp<ICameraClient>{nullptr}, cameraIdStr, cameraId, clientAttribution, |
| 1635 | /*systemNativeClient*/ false, API_1, /*shimUpdateOnly*/ true, |
| 1636 | /*oomScoreOffset*/ 0, |
| 1637 | /*targetSdkVersion*/ __ANDROID_API_FUTURE__, |
| 1638 | /*rotationOverride*/ |
| 1639 | hardware::ICameraService::ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 1640 | /*forceSlowJpegMode*/ false, cameraIdStr, /*isNonSystemNdk*/ false, |
| 1641 | /*sharedMode*/false, /*out*/ tmp)) |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 1642 | .isOk()) { |
Tomasz Wasilczyk | 12b04a5 | 2023-08-11 15:52:22 +0000 | [diff] [blame] | 1643 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().c_str()); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1644 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1645 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1646 | } |
| 1647 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1648 | Status CameraService::getLegacyParametersLazy(int cameraId, |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1649 | /*out*/ |
| 1650 | CameraParameters* parameters) { |
| 1651 | |
| 1652 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 1653 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1654 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1655 | |
| 1656 | if (parameters == NULL) { |
| 1657 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1658 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 1661 | std::string cameraIdStr = std::to_string(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1662 | |
| 1663 | // Check if we already have parameters |
| 1664 | { |
| 1665 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1666 | Mutex::Autolock lock(mServiceLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1667 | auto cameraState = getCameraState(cameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1668 | if (cameraState == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1669 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, cameraIdStr.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1670 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1671 | "Invalid camera ID: %s", cameraIdStr.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1672 | } |
| 1673 | CameraParameters p = cameraState->getShimParams(); |
| 1674 | if (!p.isEmpty()) { |
| 1675 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1676 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1677 | } |
| 1678 | } |
| 1679 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1680 | int64_t token = clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1681 | ret = initializeShimMetadata(cameraId); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1682 | restoreCallingIdentity(token); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1683 | if (!ret.isOk()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1684 | // Error already logged by callee |
| 1685 | return ret; |
| 1686 | } |
| 1687 | |
| 1688 | // Check for parameters again |
| 1689 | { |
| 1690 | // Scope for service lock |
| 1691 | Mutex::Autolock lock(mServiceLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1692 | auto cameraState = getCameraState(cameraIdStr); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1693 | if (cameraState == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1694 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, cameraIdStr.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1695 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1696 | "Invalid camera ID: %s", cameraIdStr.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1697 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1698 | CameraParameters p = cameraState->getShimParams(); |
| 1699 | if (!p.isEmpty()) { |
| 1700 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1701 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1702 | } |
| 1703 | } |
| 1704 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1705 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 1706 | __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1707 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1708 | } |
| 1709 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1710 | Status CameraService::validateConnectLocked(const std::string& cameraId, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 1711 | const AttributionSourceState& clientAttribution, |
| 1712 | bool sharedMode) const { |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1713 | #ifdef __BRILLO__ |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 1714 | UNUSED(clientAttribution); |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1715 | #else |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 1716 | Status allowed = validateClientPermissionsLocked(cameraId, clientAttribution, sharedMode); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1717 | if (!allowed.isOk()) { |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1718 | return allowed; |
| 1719 | } |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1720 | #endif // __BRILLO__ |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1721 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1722 | int callingPid = getCallingPid(); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1723 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1724 | if (!mInitialized) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1725 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 1726 | callingPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1727 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1728 | "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] | 1729 | } |
| 1730 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1731 | if (getCameraState(cameraId) == nullptr) { |
| 1732 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1733 | cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1734 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1735 | "No camera device with ID \"%s\" available", cameraId.c_str()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1738 | status_t err = checkIfDeviceIsUsable(cameraId); |
| 1739 | if (err != NO_ERROR) { |
| 1740 | switch(err) { |
| 1741 | case -ENODEV: |
| 1742 | case -EBUSY: |
| 1743 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1744 | "No camera device with ID \"%s\" currently available", cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1745 | default: |
| 1746 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1747 | "Unknown error connecting to ID \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1748 | } |
| 1749 | } |
| 1750 | return Status::ok(); |
Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1751 | } |
| 1752 | |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 1753 | Status CameraService::validateClientPermissionsLocked( |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 1754 | const std::string& cameraId, const AttributionSourceState& clientAttribution, |
| 1755 | bool sharedMode) const { |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 1756 | int callingPid = getCallingPid(); |
| 1757 | int callingUid = getCallingUid(); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1758 | |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 1759 | int clientPid = clientAttribution.pid; |
| 1760 | int clientUid = clientAttribution.uid; |
| 1761 | const std::string clientName = clientAttribution.packageName.value_or(kUnknownPackageName); |
| 1762 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1763 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1764 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", |
| 1765 | cameraId.c_str()); |
| 1766 | 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] | 1767 | "available", cameraId.c_str()); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1768 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1769 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1770 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1771 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1772 | 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] | 1773 | "found while trying to query device kind", cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 1776 | if (flags::camera_multi_client() && sharedMode |
| 1777 | && (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA)) { |
| 1778 | ALOGE("%s: camera id %s is not system camera. Device sharing only supported for" |
| 1779 | " system cameras.", __FUNCTION__, cameraId.c_str()); |
| 1780 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "camera device sharing not supported for " |
| 1781 | "camera ID \"%s\"", cameraId.c_str()); |
| 1782 | } |
| 1783 | |
Biswarup Pal | e506e73 | 2024-03-27 13:46:20 +0000 | [diff] [blame] | 1784 | // Get the device id that owns this camera. |
| 1785 | auto [deviceId, _] = mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 1786 | AttributionSourceState clientAttributionWithDeviceId = clientAttribution; |
| 1787 | clientAttributionWithDeviceId.deviceId = deviceId; |
Biswarup Pal | e506e73 | 2024-03-27 13:46:20 +0000 | [diff] [blame] | 1788 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1789 | // If it's not calling from cameraserver, check the permission if the |
| 1790 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for |
| 1791 | // android.permission.SYSTEM_CAMERA for system only camera devices). |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1792 | bool checkPermissionForCamera = |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 1793 | hasPermissionsForCamera(cameraId, clientAttributionWithDeviceId); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1794 | if (callingPid != getpid() && |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1795 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && !checkPermissionForCamera) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1796 | 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] | 1797 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1798 | "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] | 1799 | clientName.c_str(), clientPid, clientUid, cameraId.c_str()); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1800 | } |
| 1801 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1802 | // 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] | 1803 | if (!mUidPolicy->isUidActive(callingUid, clientName)) { |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1804 | int32_t procState = mUidPolicy->getProcState(callingUid); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1805 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", |
| 1806 | clientPid, clientUid); |
| 1807 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1808 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" |
| 1809 | "calling UID %d proc state %" PRId32 ")", |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1810 | clientName.c_str(), clientPid, clientUid, cameraId.c_str(), |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1811 | callingUid, procState); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1812 | } |
| 1813 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1814 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use cases |
| 1815 | // such as rear view and surround view cannot be disabled and are exempt from sensor privacy |
| 1816 | // policy. In all other cases,if sensor privacy is enabled then prevent access to the camera. |
| 1817 | if ((!isAutomotivePrivilegedClient(callingUid) || |
| 1818 | !isAutomotiveExteriorSystemCamera(cameraId)) && |
| 1819 | mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1820 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); |
| 1821 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1822 | "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] | 1823 | "is enabled", clientName.c_str(), clientPid, clientUid, cameraId.c_str()); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1824 | } |
| 1825 | |
Austin Borger | 6e831c4 | 2024-07-22 13:07:56 -0700 | [diff] [blame] | 1826 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 1827 | |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1828 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's |
| 1829 | // connected to camera service directly. |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1830 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1831 | // For non-system clients : Only allow clients who are being used by the current foreground |
| 1832 | // device user, unless calling from our own process. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1833 | if (!callerHasSystemUid() && callingPid != getpid() && |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1834 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1835 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 1836 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1837 | toString(mAllowedUsers).c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1838 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1839 | "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] | 1840 | clientUserId, cameraId.c_str()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 1843 | if (flags::camera_hsum_permission()) { |
| 1844 | // If the System User tries to access the camera when the device is running in |
| 1845 | // headless system user mode, ensure that client has the required permission |
| 1846 | // CAMERA_HEADLESS_SYSTEM_USER. |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 1847 | if (isHeadlessSystemUserMode() |
| 1848 | && (clientUserId == USER_SYSTEM) |
| 1849 | && !hasPermissionsForCameraHeadlessSystemUser(cameraId, callingPid, callingUid)) { |
Austin Borger | 6e831c4 | 2024-07-22 13:07:56 -0700 | [diff] [blame] | 1850 | ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", callingPid, clientUid); |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 1851 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1852 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" as Headless System \ |
| 1853 | User without camera headless system user permission", |
Austin Borger | 6e831c4 | 2024-07-22 13:07:56 -0700 | [diff] [blame] | 1854 | clientName.c_str(), callingPid, clientUid, cameraId.c_str()); |
Jyoti Bhayana | a16cc4c | 2023-09-26 15:37:19 -0700 | [diff] [blame] | 1855 | } |
Jyoti Bhayana | 5bdb5a6 | 2023-08-24 14:46:08 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1858 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1859 | } |
| 1860 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1861 | status_t CameraService::checkIfDeviceIsUsable(const std::string& cameraId) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1862 | auto cameraState = getCameraState(cameraId); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1863 | int callingPid = getCallingPid(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1864 | if (cameraState == nullptr) { |
| 1865 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1866 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1867 | return -ENODEV; |
| 1868 | } |
| 1869 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1870 | StatusInternal currentStatus = cameraState->getStatus(); |
| 1871 | if (currentStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1872 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1873 | callingPid, cameraId.c_str()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1874 | return -ENODEV; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1875 | } else if (currentStatus == StatusInternal::ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1876 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1877 | callingPid, cameraId.c_str()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1878 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1879 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1880 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1881 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1882 | } |
| 1883 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1884 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1885 | const CameraService::DescriptorPtr& desc, int oomScoreOffset, bool systemNativeClient) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1886 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1887 | // Make a descriptor for the incoming client |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1888 | auto clientDescriptor = |
| 1889 | CameraService::CameraClientManager::makeClientDescriptor(client, desc, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1890 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1891 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 1892 | |
| 1893 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1894 | client->getPackageName()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1895 | |
| 1896 | if (evicted.size() > 0) { |
| 1897 | // This should never happen - clients should already have been removed in disconnect |
| 1898 | for (auto& i : evicted) { |
| 1899 | 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] | 1900 | __FUNCTION__, i->getKey().c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 1904 | __FUNCTION__); |
| 1905 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1906 | |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 1907 | if (flags::camera_multi_client()) { |
| 1908 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1909 | auto primaryClient = mActiveClientManager.getPrimaryClient(desc->getKey()); |
| 1910 | if (primaryClient == nullptr) { |
| 1911 | // There is no primary client yet. Assign this first client as |
| 1912 | // primary |
| 1913 | clientSp->setPrimaryClient(true); |
| 1914 | } else { |
| 1915 | // There is already primary client. If the incoming client has a |
| 1916 | // higher priority than the existing primary, then assign incoming |
| 1917 | // client as primary and change the existing client to secondary. |
| 1918 | // Otherwise incoming client is secondary client. |
| 1919 | if (clientDescriptor->getPriority() <= primaryClient->getPriority()) { |
| 1920 | clientSp->setPrimaryClient(true); |
| 1921 | primaryClient->getValue()->setPrimaryClient(false); |
| 1922 | primaryClient->getValue()->notifyClientSharedAccessPriorityChanged(false); |
| 1923 | } else { |
| 1924 | clientSp->setPrimaryClient(false); |
| 1925 | } |
| 1926 | } |
| 1927 | } |
| 1928 | |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1929 | // And register a death notification for the client callback. Do |
| 1930 | // this last to avoid Binder policy where a nested Binder |
| 1931 | // transaction might be pre-empted to service the client death |
| 1932 | // notification if the client process dies before linkToDeath is |
| 1933 | // invoked. |
| 1934 | sp<IBinder> remoteCallback = client->getRemote(); |
| 1935 | if (remoteCallback != nullptr) { |
| 1936 | remoteCallback->linkToDeath(this); |
| 1937 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1938 | } |
| 1939 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1940 | status_t CameraService::handleEvictionsLocked(const std::string& cameraId, int clientPid, |
| 1941 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, |
| 1942 | const std::string& packageName, int oomScoreOffset, bool systemNativeClient, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 1943 | bool sharedMode, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1944 | /*out*/ |
| 1945 | sp<BasicClient>* client, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1946 | std::shared_ptr<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1947 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1948 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1949 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1950 | DescriptorPtr clientDescriptor; |
| 1951 | { |
| 1952 | if (effectiveApiLevel == API_1) { |
| 1953 | // If we are using API1, any existing client for this camera ID with the same remote |
| 1954 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 1955 | |
| 1956 | auto current = mActiveClientManager.get(cameraId); |
| 1957 | if (current != nullptr) { |
| 1958 | auto clientSp = current->getValue(); |
| 1959 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1960 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
Shuzhen Wang | b2d43f6 | 2021-08-25 14:01:11 -0700 | [diff] [blame] | 1961 | ALOGW("CameraService connect called with a different" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1962 | " API level, evicting prior client..."); |
| 1963 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1964 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1965 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1966 | *client = clientSp; |
| 1967 | return NO_ERROR; |
| 1968 | } |
| 1969 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1970 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1971 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 1972 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1973 | // Get state for the given cameraId |
| 1974 | auto state = getCameraState(cameraId); |
| 1975 | if (state == nullptr) { |
| 1976 | 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] | 1977 | clientPid, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1978 | // Should never get here because validateConnectLocked should have errored out |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1979 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1980 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1981 | |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 1982 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1983 | sp<IBinder> binder = sm->checkService(String16(kProcessInfoServiceName)); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 1984 | if (!binder && isAutomotivePrivilegedClient(getCallingUid())) { |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 1985 | // If processinfo service is not available and the client is automotive privileged |
| 1986 | // client used for safety critical uses cases such as rear-view and surround-view which |
| 1987 | // needs to be available before android boot completes, then use the hardcoded values |
| 1988 | // for the process state and priority score. As this scenario is before android system |
| 1989 | // services are up and client is native client, hence using NATIVE_ADJ as the priority |
| 1990 | // score and state as PROCESS_STATE_BOUND_TOP as such automotive apps need to be |
| 1991 | // visible on the top. |
| 1992 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 1993 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 1994 | state->getConflicting(), resource_policy::NATIVE_ADJ, clientPid, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 1995 | ActivityManager::PROCESS_STATE_BOUND_TOP, oomScoreOffset, systemNativeClient, |
| 1996 | sharedMode); |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 1997 | } else { |
| 1998 | // Get current active client PIDs |
| 1999 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 2000 | ownerPids.push_back(clientPid); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2001 | |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 2002 | std::vector<int> priorityScores(ownerPids.size()); |
| 2003 | std::vector<int> states(ownerPids.size()); |
| 2004 | |
| 2005 | // Get priority scores of all active PIDs |
| 2006 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids(ownerPids.size(), |
| 2007 | &ownerPids[0], /*out*/&states[0], /*out*/&priorityScores[0]); |
| 2008 | if (err != OK) { |
| 2009 | ALOGE("%s: Priority score query failed: %d", __FUNCTION__, err); |
| 2010 | return err; |
| 2011 | } |
| 2012 | |
| 2013 | // Update all active clients' priorities |
| 2014 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; |
| 2015 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
| 2016 | pidToPriorityMap.emplace(ownerPids[i], |
| 2017 | resource_policy::ClientPriority(priorityScores[i], states[i], |
| 2018 | /* isVendorClient won't get copied over*/ false, |
| 2019 | /* oomScoreOffset won't get copied over*/ 0)); |
| 2020 | } |
| 2021 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
| 2022 | |
| 2023 | int32_t actualScore = priorityScores[priorityScores.size() - 1]; |
| 2024 | int32_t actualState = states[states.size() - 1]; |
| 2025 | |
| 2026 | // Make descriptor for incoming client. We store the oomScoreOffset |
| 2027 | // since we might need it later on new handleEvictionsLocked and |
| 2028 | // ProcessInfoService would not take that into account. |
| 2029 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 2030 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 2031 | state->getConflicting(), actualScore, clientPid, actualState, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 2032 | oomScoreOffset, systemNativeClient, sharedMode); |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame] | 2033 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2034 | |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2035 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); |
| 2036 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2037 | // Find clients that would be evicted |
| 2038 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 2039 | |
| 2040 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 2041 | // background, so we cannot do evictions |
| 2042 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 2043 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 2044 | " priority).", clientPid); |
| 2045 | |
| 2046 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2047 | std::string curTime = getFormattedCurrentTime(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2048 | auto incompatibleClients = |
| 2049 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 2050 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2051 | std::string msg = fmt::sprintf("%s : DENIED connect device %s client for package %s " |
| 2052 | "(PID %d, score %d state %d) due to eviction policy", curTime.c_str(), |
| 2053 | cameraId.c_str(), packageName.c_str(), clientPid, |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2054 | clientPriority.getScore(), clientPriority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2055 | |
| 2056 | for (auto& i : incompatibleClients) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2057 | 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] | 2058 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2059 | i->getKey().c_str(), |
| 2060 | i->getValue()->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2061 | i->getOwnerId(), i->getPriority().getScore(), |
| 2062 | i->getPriority().getState()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 2063 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2064 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().c_str(), |
| 2065 | i->getValue()->getPackageName().c_str(), i->getOwnerId(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2066 | i->getPriority().getScore(), i->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2070 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2071 | mEventLog.add(msg); |
| 2072 | |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 2073 | auto current = mActiveClientManager.get(cameraId); |
| 2074 | if (current != nullptr) { |
| 2075 | return -EBUSY; // CAMERA_IN_USE |
| 2076 | } else { |
| 2077 | return -EUSERS; // MAX_CAMERAS_IN_USE |
| 2078 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | for (auto& i : evicted) { |
| 2082 | sp<BasicClient> clientSp = i->getValue(); |
| 2083 | if (clientSp.get() == nullptr) { |
| 2084 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 2085 | |
| 2086 | // TODO: Remove this |
| 2087 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 2088 | __FUNCTION__); |
| 2089 | mActiveClientManager.remove(i); |
| 2090 | continue; |
| 2091 | } |
| 2092 | |
| 2093 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2094 | i->getKey().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2095 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2096 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2097 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2098 | logEvent(fmt::sprintf("EVICT device %s client held by package %s (PID" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2099 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" |
| 2100 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2101 | i->getKey().c_str(), clientSp->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2102 | i->getOwnerId(), i->getPriority().getScore(), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2103 | i->getPriority().getState(), cameraId.c_str(), |
| 2104 | packageName.c_str(), clientPid, clientPriority.getScore(), |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2105 | clientPriority.getState())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2106 | |
| 2107 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2108 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2109 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2110 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2111 | } |
| 2112 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2113 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 2114 | // other clients from connecting in mServiceLockWrapper if held |
| 2115 | mServiceLock.unlock(); |
| 2116 | |
| 2117 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2118 | int64_t token = clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2119 | |
| 2120 | // Destroy evicted clients |
| 2121 | for (auto& i : evictedClients) { |
| 2122 | // 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] | 2123 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2124 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2125 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2126 | restoreCallingIdentity(token); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2127 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2128 | for (const auto& i : evictedClients) { |
| 2129 | 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] | 2130 | __FUNCTION__, i->getKey().c_str(), i->getOwnerId()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2131 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 2132 | if (ret == TIMED_OUT) { |
| 2133 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2134 | "current clients:\n%s", __FUNCTION__, i->getKey().c_str(), |
| 2135 | mActiveClientManager.toString().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2136 | return -EBUSY; |
| 2137 | } |
| 2138 | if (ret != NO_ERROR) { |
| 2139 | 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] | 2140 | "current clients:\n%s", __FUNCTION__, i->getKey().c_str(), strerror(-ret), |
| 2141 | ret, mActiveClientManager.toString().c_str()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 2142 | return ret; |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | evictedClients.clear(); |
| 2147 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2148 | // Once clients have been disconnected, relock |
| 2149 | mServiceLock.lock(); |
| 2150 | |
| 2151 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 2152 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 2153 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2154 | } |
| 2155 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2156 | *partial = clientDescriptor; |
| 2157 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2160 | Status CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 2161 | const sp<ICameraClient>& cameraClient, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2162 | int api1CameraId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2163 | int targetSdkVersion, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2164 | int rotationOverride, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2165 | bool forceSlowJpegMode, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2166 | const AttributionSourceState& clientAttribution, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2167 | int32_t devicePolicy, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 2168 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2169 | sp<ICamera>* device) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2170 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2171 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2172 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2173 | std::string cameraIdStr = |
| 2174 | cameraIdIntToStr(api1CameraId, clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2175 | if (cameraIdStr.empty()) { |
| 2176 | 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] | 2177 | api1CameraId, clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2178 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2179 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 2180 | } |
malikakash | edb3896 | 2023-09-06 00:03:35 +0000 | [diff] [blame] | 2181 | |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 2182 | std::string clientPackageNameMaybe = clientAttribution.packageName.value_or(""); |
| 2183 | bool isNonSystemNdk = clientPackageNameMaybe.size() == 0; |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 2184 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2185 | AttributionSourceState resolvedClientAttribution(clientAttribution); |
| 2186 | ret = resolveAttributionSource(resolvedClientAttribution, __FUNCTION__, cameraIdStr); |
| 2187 | if (!ret.isOk()) { |
| 2188 | logRejected(cameraIdStr, getCallingPid(), |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2189 | clientAttribution.packageName.value_or(kUnknownPackageName), |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2190 | toStdString(ret.toString8())); |
| 2191 | return ret; |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 2192 | } |
| 2193 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2194 | const int clientPid = resolvedClientAttribution.pid; |
| 2195 | const int clientUid = resolvedClientAttribution.uid; |
| 2196 | const std::string& clientPackageName = *resolvedClientAttribution.packageName; |
| 2197 | |
| 2198 | logConnectionAttempt(clientPid, clientPackageName, cameraIdStr, API_1); |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 2199 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2200 | sp<Client> client = nullptr; |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2201 | ret = connectHelper<ICameraClient, Client>( |
| 2202 | cameraClient, cameraIdStr, api1CameraId, resolvedClientAttribution, |
| 2203 | /*systemNativeClient*/ false, API_1, |
| 2204 | /*shimUpdateOnly*/ false, /*oomScoreOffset*/ 0, targetSdkVersion, rotationOverride, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 2205 | forceSlowJpegMode, cameraIdStr, isNonSystemNdk, /*sharedMode*/false, /*out*/ client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2206 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2207 | if (!ret.isOk()) { |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2208 | logRejected(cameraIdStr, getCallingPid(), |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2209 | clientAttribution.packageName.value_or(kUnknownPackageName), |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2210 | toStdString(ret.toString8())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2211 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2214 | *device = client; |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2215 | |
| 2216 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 2217 | const auto& mActivityManager = getActivityManager(); |
| 2218 | if (mActivityManager) { |
| 2219 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2220 | getCallingUid(), |
| 2221 | getCallingPid()); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2224 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 2225 | } |
| 2226 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2227 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, |
| 2228 | bool isVendorListener, int clientPid, int clientUid) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2229 | // If the client is not a vendor client, don't add listener if |
| 2230 | // a) the camera is a publicly hidden secure camera OR |
| 2231 | // b) the camera is a system only camera and the client doesn't |
| 2232 | // have android.permission.SYSTEM_CAMERA permissions. |
| 2233 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || |
| 2234 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2235 | !hasPermissionsForSystemCamera(std::string(), clientPid, clientUid)))) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2236 | return true; |
| 2237 | } |
| 2238 | return false; |
| 2239 | } |
| 2240 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2241 | bool CameraService::shouldRejectSystemCameraConnection(const std::string& cameraId) const { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2242 | // Rules for rejection: |
| 2243 | // 1) If cameraserver tries to access this camera device, accept the |
| 2244 | // connection. |
| 2245 | // 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] | 2246 | // non system component is trying to access it. |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2247 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY |
| 2248 | // and the serving thread is a non hwbinder thread, the client must have |
| 2249 | // android.permission.SYSTEM_CAMERA permissions to connect. |
| 2250 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2251 | int cPid = getCallingPid(); |
| 2252 | int cUid = getCallingUid(); |
| 2253 | bool systemClient = callerHasSystemUid(); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2254 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 2255 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 2256 | // This isn't a known camera ID, so it's not a system camera |
| 2257 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); |
| 2258 | return false; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2259 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2260 | |
| 2261 | // (1) Cameraserver trying to connect, accept. |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2262 | if (isCallerCameraServerNotDelegating()) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2263 | return false; |
| 2264 | } |
| 2265 | // (2) |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2266 | if (!systemClient && systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2267 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); |
| 2268 | return true; |
| 2269 | } |
| 2270 | // (3) Here we only check for permissions if it is a system only camera device. This is since |
| 2271 | // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some |
| 2272 | // characteristics) even if clients don't have android.permission.CAMERA. We do not want the |
| 2273 | // same behavior for system camera devices. |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2274 | if (!systemClient && systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2275 | !hasPermissionsForSystemCamera(cameraId, cPid, cUid)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2276 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", |
| 2277 | cameraId.c_str()); |
| 2278 | return true; |
| 2279 | } |
| 2280 | |
| 2281 | return false; |
| 2282 | } |
| 2283 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2284 | Status CameraService::connectDevice( |
| 2285 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2286 | const std::string& unresolvedCameraId, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2287 | int oomScoreOffset, int targetSdkVersion, |
| 2288 | int rotationOverride, const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 2289 | bool sharedMode, /*out*/sp<hardware::camera2::ICameraDeviceUser>* device) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2290 | ATRACE_CALL(); |
Emilian Peev | 31bd242 | 2024-04-23 22:24:09 +0000 | [diff] [blame] | 2291 | RunThreadWithRealtimePriority priorityBump; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2292 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2293 | sp<CameraDeviceClient> client = nullptr; |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 2294 | std::string clientPackageNameMaybe = clientAttribution.packageName.value_or(""); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2295 | int callingPid = getCallingPid(); |
| 2296 | int callingUid = getCallingUid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2297 | bool systemNativeClient = false; |
Jayant Chowdhary | 0267d47 | 2024-11-01 20:42:15 +0000 | [diff] [blame] | 2298 | AttributionSourceState resolvedClientAttribution(clientAttribution); |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 2299 | if (callerHasSystemUid() && (clientPackageNameMaybe.size() == 0)) { |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2300 | std::string systemClient = fmt::sprintf("client.pid<%d>", callingPid); |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 2301 | clientPackageNameMaybe = systemClient; |
Jayant Chowdhary | 0267d47 | 2024-11-01 20:42:15 +0000 | [diff] [blame] | 2302 | // Pass in packageName since AttributionAndPermissionUtils can't resolve vndk clients. |
| 2303 | resolvedClientAttribution.packageName = clientPackageNameMaybe; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2304 | systemNativeClient = true; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 2305 | } |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2306 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2307 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, |
| 2308 | clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2309 | if (!cameraIdOptional.has_value()) { |
| 2310 | 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] | 2311 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2312 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2313 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 2314 | } |
| 2315 | std::string cameraId = cameraIdOptional.value(); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2316 | |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 2317 | bool isNonSystemNdk = clientPackageNameMaybe.size() == 0; |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 2318 | |
Austin Borger | 2a3f9a7 | 2024-11-07 12:24:49 -0800 | [diff] [blame] | 2319 | if (!flags::data_delivery_permission_checks()) { |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2320 | resolvedClientAttribution.pid = USE_CALLING_PID; |
| 2321 | } |
Jayant Chowdhary | 0267d47 | 2024-11-01 20:42:15 +0000 | [diff] [blame] | 2322 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2323 | ret = resolveAttributionSource(resolvedClientAttribution, __FUNCTION__, cameraId); |
| 2324 | if (!ret.isOk()) { |
| 2325 | logRejected(cameraId, getCallingPid(), clientAttribution.packageName.value_or(""), |
| 2326 | toStdString(ret.toString8())); |
| 2327 | return ret; |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 2328 | } |
| 2329 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2330 | const int clientPid = resolvedClientAttribution.pid; |
| 2331 | const int clientUid = resolvedClientAttribution.uid; |
| 2332 | const std::string& clientPackageName = *resolvedClientAttribution.packageName; |
| 2333 | userid_t clientUserId = multiuser_get_user_id(resolvedClientAttribution.uid); |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 2334 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2335 | logConnectionAttempt(clientPid, clientPackageName, cameraId, API_2); |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 2336 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2337 | if (oomScoreOffset < 0) { |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2338 | std::string msg = fmt::sprintf( |
| 2339 | "Cannot increase the priority of a client %s pid %d for " |
| 2340 | "camera id %s", |
| 2341 | clientPackageName.c_str(), clientPid, cameraId.c_str()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2342 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2343 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2346 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use cases |
| 2347 | // such as rear view and surround view cannot be disabled. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2348 | if ((!isAutomotivePrivilegedClient(callingUid) || !isAutomotiveExteriorSystemCamera(cameraId)) |
| 2349 | && mCameraServiceProxyWrapper->isCameraDisabled(clientUserId)) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2350 | std::string msg = "Camera disabled by device policy"; |
| 2351 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2352 | return STATUS_ERROR(ERROR_DISABLED, msg.c_str()); |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 2353 | } |
| 2354 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2355 | // enforce system camera permissions |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2356 | if (oomScoreOffset > 0 && !hasPermissionsForSystemCamera(cameraId, clientPid, callingUid) && |
| 2357 | !isTrustedCallingUid(callingUid)) { |
| 2358 | std::string msg = fmt::sprintf( |
| 2359 | "Cannot change the priority of a client %s pid %d for " |
| 2360 | "camera id %s without SYSTEM_CAMERA permissions", |
| 2361 | clientPackageName.c_str(), clientPid, cameraId.c_str()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2362 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2363 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, msg.c_str()); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2366 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks, CameraDeviceClient>( |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2367 | cameraCb, cameraId, /*api1CameraId*/ -1, resolvedClientAttribution, systemNativeClient, |
| 2368 | API_2, /*shimUpdateOnly*/ false, oomScoreOffset, targetSdkVersion, rotationOverride, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 2369 | /*forceSlowJpegMode*/ false, unresolvedCameraId, isNonSystemNdk, sharedMode, |
| 2370 | /*out*/ client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2371 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2372 | if (!ret.isOk()) { |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2373 | logRejected(cameraId, clientPid, clientPackageName, toStdString(ret.toString8())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2374 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2375 | } |
| 2376 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2377 | *device = client; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 2378 | Mutex::Autolock lock(mServiceLock); |
| 2379 | |
| 2380 | // Clear the previous cached logs and reposition the |
| 2381 | // file offset to beginning of the file to log new data. |
| 2382 | // If either truncate or lseek fails, close the previous file and create a new one. |
| 2383 | if ((ftruncate(mMemFd, 0) == -1) || (lseek(mMemFd, 0, SEEK_SET) == -1)) { |
| 2384 | ALOGE("%s: Error while truncating the file: %s", __FUNCTION__, sFileName); |
| 2385 | // Close the previous memfd. |
| 2386 | close(mMemFd); |
| 2387 | // If failure to wipe the data, then create a new file and |
| 2388 | // assign the new value to mMemFd. |
| 2389 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 2390 | if (mMemFd == -1) { |
| 2391 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 2392 | } |
| 2393 | } |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2394 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 2395 | const auto& mActivityManager = getActivityManager(); |
| 2396 | if (mActivityManager) { |
| 2397 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 2398 | callingUid, |
| 2399 | callingPid); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 2400 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2401 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2402 | } |
| 2403 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2404 | bool CameraService::isCameraPrivacyEnabled(const String16& packageName, const std::string& cam_id, |
| 2405 | int callingPid, int callingUid) { |
| 2406 | if (!isAutomotiveDevice()) { |
| 2407 | return mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
| 2408 | } |
| 2409 | |
| 2410 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for |
| 2411 | // safety-critical use cases cannot be disabled and are exempt from camera privacy policy. |
| 2412 | if ((isAutomotivePrivilegedClient(callingUid) && isAutomotiveExteriorSystemCamera(cam_id))) { |
| 2413 | ALOGI("Camera privacy cannot be enabled for automotive privileged client %d " |
| 2414 | "using camera %s", callingUid, cam_id.c_str()); |
| 2415 | return false; |
| 2416 | } |
| 2417 | |
| 2418 | if (mSensorPrivacyPolicy->isCameraPrivacyEnabled(packageName)) { |
| 2419 | return true; |
| 2420 | } else if (mSensorPrivacyPolicy->getCameraPrivacyState() == SensorPrivacyManager::DISABLED) { |
| 2421 | return false; |
Jyoti Bhayana | 54a4b00 | 2024-02-27 15:36:09 -0800 | [diff] [blame] | 2422 | } else if (mSensorPrivacyPolicy->getCameraPrivacyState() |
| 2423 | == SensorPrivacyManager::ENABLED_EXCEPT_ALLOWLISTED_APPS) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2424 | if (hasPermissionsForCameraPrivacyAllowlist(callingPid, callingUid)) { |
| 2425 | return false; |
| 2426 | } else { |
| 2427 | return true; |
| 2428 | } |
| 2429 | } |
| 2430 | return false; |
| 2431 | } |
| 2432 | |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 2433 | void CameraService::logConnectionAttempt(int clientPid, const std::string& clientPackageName, |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2434 | const std::string& cameraId, |
| 2435 | apiLevel effectiveApiLevel) const { |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 2436 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 2437 | "Camera API version %d", |
| 2438 | clientPid, clientPackageName.c_str(), cameraId.c_str(), |
| 2439 | static_cast<int>(effectiveApiLevel)); |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 2440 | } |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2441 | |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2442 | template <class CALLBACK, class CLIENT> |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 2443 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const std::string& cameraId, |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2444 | int api1CameraId, |
| 2445 | const AttributionSourceState& clientAttribution, |
| 2446 | bool systemNativeClient, apiLevel effectiveApiLevel, |
| 2447 | bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion, |
| 2448 | int rotationOverride, bool forceSlowJpegMode, |
| 2449 | const std::string& originalCameraId, bool isNonSystemNdk, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 2450 | bool sharedMode, /*out*/ sp<CLIENT>& device) { |
Austin Borger | 7d2b923 | 2024-07-22 14:17:14 -0700 | [diff] [blame] | 2451 | binder::Status ret = binder::Status::ok(); |
| 2452 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2453 | nsecs_t openTimeNs = systemTime(); |
| 2454 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2455 | sp<CLIENT> client = nullptr; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2456 | int facing = -1; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 2457 | int orientation = 0; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2458 | |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2459 | const std::string clientPackageName = |
| 2460 | clientAttribution.packageName.value_or(kUnknownPackageName); |
| 2461 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2462 | { |
| 2463 | // Acquire mServiceLock and prevent other clients from connecting |
| 2464 | std::unique_ptr<AutoConditionLock> lock = |
| 2465 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2466 | |
| 2467 | if (lock == nullptr) { |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2468 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting).", |
| 2469 | clientAttribution.pid); |
| 2470 | return STATUS_ERROR_FMT( |
| 2471 | ERROR_MAX_CAMERAS_IN_USE, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2472 | "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting", |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2473 | cameraId.c_str(), clientPackageName.c_str(), clientAttribution.pid); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2474 | } |
| 2475 | |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 2476 | // Enforce client permissions and do basic validity checks |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 2477 | if (!(ret = validateConnectLocked(cameraId, clientAttribution, sharedMode)).isOk()) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2478 | return ret; |
| 2479 | } |
| 2480 | |
| 2481 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 2482 | // we were doing a shim update, return immediately |
| 2483 | if (shimUpdateOnly) { |
| 2484 | auto cameraState = getCameraState(cameraId); |
| 2485 | if (cameraState != nullptr) { |
| 2486 | if (!cameraState->getShimParams().isEmpty()) return ret; |
| 2487 | } |
| 2488 | } |
| 2489 | |
| 2490 | status_t err; |
| 2491 | |
| 2492 | sp<BasicClient> clientTmp = nullptr; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2493 | std::shared_ptr<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>> partial; |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2494 | if ((err = handleEvictionsLocked( |
| 2495 | cameraId, clientAttribution.pid, effectiveApiLevel, |
| 2496 | IInterface::asBinder(cameraCb), |
| 2497 | clientAttribution.packageName.value_or(kUnknownPackageName), oomScoreOffset, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 2498 | systemNativeClient, sharedMode, /*out*/ &clientTmp, |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2499 | /*out*/ &partial)) != NO_ERROR) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2500 | switch (err) { |
| 2501 | case -ENODEV: |
| 2502 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2503 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2504 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2505 | case -EBUSY: |
| 2506 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2507 | "Higher-priority client using camera, ID \"%s\" currently unavailable", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2508 | cameraId.c_str()); |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 2509 | case -EUSERS: |
| 2510 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2511 | "Too many cameras already open, cannot open camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2512 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2513 | default: |
| 2514 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2515 | "Unexpected error %s (%d) opening camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2516 | strerror(-err), err, cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | if (clientTmp.get() != nullptr) { |
| 2521 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 2522 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 2523 | return ret; |
| 2524 | } |
| 2525 | |
| 2526 | // give flashlight a chance to close devices if necessary. |
| 2527 | mFlashlight->prepareDeviceOpen(cameraId); |
| 2528 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2529 | int portraitRotation; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2530 | auto deviceVersionAndTransport = |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2531 | getDeviceVersion(cameraId, rotationOverride, /*out*/&portraitRotation, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2532 | /*out*/&facing, /*out*/&orientation); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2533 | if (facing == -1) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2534 | 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] | 2535 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2536 | "Unable to get camera device \"%s\" facing", cameraId.c_str()); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2537 | } |
| 2538 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2539 | sp<BasicClient> tmp = nullptr; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2540 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2541 | mPerfClassPrimaryCameraIds, cameraId, targetSdkVersion); |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2542 | |
Austin Borger | 6e831c4 | 2024-07-22 13:07:56 -0700 | [diff] [blame] | 2543 | // Only use passed in clientPid to check permission. Use calling PID as the client PID |
| 2544 | // that's connected to camera service directly. |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 2545 | if (!(ret = makeClient(this, cameraCb, clientAttribution, getCallingPid(), |
| 2546 | systemNativeClient, cameraId, api1CameraId, facing, orientation, |
| 2547 | getpid(), deviceVersionAndTransport, effectiveApiLevel, |
| 2548 | overrideForPerfClass, rotationOverride, forceSlowJpegMode, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 2549 | originalCameraId, sharedMode, |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2550 | /*out*/ &tmp)) |
| 2551 | .isOk()) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2552 | return ret; |
| 2553 | } |
| 2554 | client = static_cast<CLIENT*>(tmp.get()); |
| 2555 | |
| 2556 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 2557 | __FUNCTION__); |
| 2558 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2559 | std::string monitorTags = isClientWatched(client.get()) ? mMonitorTags : std::string(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2560 | err = client->initialize(mCameraProviderManager, monitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2561 | if (err != OK) { |
| 2562 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2563 | // Errors could be from the HAL module open call or from AppOpsManager |
Kwangkyu Park | b1aaf9a | 2023-07-08 00:42:03 +0900 | [diff] [blame] | 2564 | mServiceLock.unlock(); |
| 2565 | client->disconnect(); |
| 2566 | mServiceLock.lock(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2567 | switch(err) { |
| 2568 | case BAD_VALUE: |
| 2569 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2570 | "Illegal argument to HAL module for camera \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2571 | case -EBUSY: |
| 2572 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2573 | "Camera \"%s\" is already open", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2574 | case -EUSERS: |
| 2575 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2576 | "Too many cameras already open, cannot open camera \"%s\"", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2577 | cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2578 | case PERMISSION_DENIED: |
| 2579 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2580 | "No permission to open camera \"%s\"", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2581 | case -EACCES: |
| 2582 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2583 | "Camera \"%s\" disabled by policy", cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2584 | case -ENODEV: |
| 2585 | default: |
| 2586 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2587 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.c_str(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2588 | strerror(-err), err); |
| 2589 | } |
| 2590 | } |
| 2591 | |
| 2592 | // Update shim paremeters for legacy clients |
| 2593 | if (effectiveApiLevel == API_1) { |
| 2594 | // Assume we have always received a Client subclass for API1 |
| 2595 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 2596 | String8 rawParams = shimClient->getParameters(); |
| 2597 | CameraParameters params(rawParams); |
| 2598 | |
| 2599 | auto cameraState = getCameraState(cameraId); |
| 2600 | if (cameraState != nullptr) { |
| 2601 | cameraState->setShimParams(params); |
| 2602 | } else { |
| 2603 | 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] | 2604 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2605 | } |
| 2606 | } |
| 2607 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 2608 | // Enable/disable camera service watchdog |
| 2609 | client->setCameraServiceWatchdog(mCameraServiceWatchdogEnabled); |
| 2610 | |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2611 | CameraMetadata chars; |
| 2612 | bool rotateAndCropSupported = true; |
| 2613 | err = mCameraProviderManager->getCameraCharacteristics(cameraId, overrideForPerfClass, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2614 | &chars, rotationOverride); |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2615 | if (err == OK) { |
| 2616 | auto availableRotateCropEntry = chars.find( |
| 2617 | ANDROID_SCALER_AVAILABLE_ROTATE_AND_CROP_MODES); |
| 2618 | if (availableRotateCropEntry.count <= 1) { |
| 2619 | rotateAndCropSupported = false; |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2620 | } |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame] | 2621 | } else { |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2622 | ALOGE("%s: Unable to query static metadata for camera %s: %s (%d)", __FUNCTION__, |
| 2623 | cameraId.c_str(), strerror(-err), err); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2626 | if (rotateAndCropSupported) { |
| 2627 | // Set rotate-and-crop override behavior |
| 2628 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { |
| 2629 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2630 | } else if (rotationOverride != hardware::ICameraService::ROTATION_OVERRIDE_NONE && |
| 2631 | portraitRotation != 0) { |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2632 | uint8_t rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_AUTO; |
| 2633 | switch (portraitRotation) { |
| 2634 | case 90: |
| 2635 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_90; |
| 2636 | break; |
| 2637 | case 180: |
| 2638 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_180; |
| 2639 | break; |
| 2640 | case 270: |
| 2641 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_270; |
| 2642 | break; |
| 2643 | default: |
| 2644 | ALOGE("Unexpected portrait rotation: %d", portraitRotation); |
| 2645 | break; |
| 2646 | } |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 2647 | // Here we're communicating to the client the chosen rotate |
| 2648 | // and crop mode to send to the HAL |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2649 | client->setRotateAndCropOverride(rotateAndCropMode); |
| 2650 | } else { |
| 2651 | client->setRotateAndCropOverride( |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2652 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 2653 | clientPackageName, facing, |
| 2654 | multiuser_get_user_id(clientAttribution.uid))); |
Emilian Peev | 6d45db8 | 2024-01-18 20:11:54 +0000 | [diff] [blame] | 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | bool autoframingSupported = true; |
| 2659 | auto availableAutoframingEntry = chars.find(ANDROID_CONTROL_AUTOFRAMING_AVAILABLE); |
| 2660 | if ((availableAutoframingEntry.count == 1) && (availableAutoframingEntry.data.u8[0] == |
| 2661 | ANDROID_CONTROL_AUTOFRAMING_AVAILABLE_FALSE)) { |
| 2662 | autoframingSupported = false; |
| 2663 | } |
| 2664 | |
| 2665 | if (autoframingSupported) { |
| 2666 | // Set autoframing override behaviour |
| 2667 | if (mOverrideAutoframingMode != ANDROID_CONTROL_AUTOFRAMING_AUTO) { |
| 2668 | client->setAutoframingOverride(mOverrideAutoframingMode); |
| 2669 | } else { |
| 2670 | client->setAutoframingOverride( |
| 2671 | mCameraServiceProxyWrapper->getAutoframingOverride( |
| 2672 | clientPackageName)); |
| 2673 | } |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 2674 | } |
| 2675 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2676 | bool isCameraPrivacyEnabled; |
| 2677 | if (flags::camera_privacy_allowlist()) { |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2678 | // Set camera muting behavior. |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 2679 | isCameraPrivacyEnabled = |
| 2680 | this->isCameraPrivacyEnabled(toString16(client->getPackageName()), cameraId, |
| 2681 | clientAttribution.pid, clientAttribution.uid); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2682 | } else { |
| 2683 | isCameraPrivacyEnabled = |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2684 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2685 | } |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 2686 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2687 | if (client->supportsCameraMute()) { |
| 2688 | client->setCameraMute( |
| 2689 | mOverrideCameraMuteMode || isCameraPrivacyEnabled); |
| 2690 | } else if (isCameraPrivacyEnabled) { |
| 2691 | // no camera mute supported, but privacy is on! => disconnect |
| 2692 | ALOGI("Camera mute not supported for package: %s, camera id: %s", |
| 2693 | client->getPackageName().c_str(), cameraId.c_str()); |
| 2694 | // Do not hold mServiceLock while disconnecting clients, but |
| 2695 | // retain the condition blocking other clients from connecting |
| 2696 | // in mServiceLockWrapper if held. |
| 2697 | mServiceLock.unlock(); |
| 2698 | // Clear caller identity temporarily so client disconnect PID |
| 2699 | // checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2700 | int64_t token = clearCallingIdentity(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2701 | // Note AppOp to trigger the "Unblock" dialog |
| 2702 | client->noteAppOp(); |
| 2703 | client->disconnect(); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2704 | restoreCallingIdentity(token); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 2705 | // Reacquire mServiceLock |
| 2706 | mServiceLock.lock(); |
| 2707 | |
| 2708 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 2709 | "Camera \"%s\" disabled due to camera mute", cameraId.c_str()); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2710 | } |
| 2711 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2712 | if (shimUpdateOnly) { |
| 2713 | // If only updating legacy shim parameters, immediately disconnect client |
| 2714 | mServiceLock.unlock(); |
| 2715 | client->disconnect(); |
| 2716 | mServiceLock.lock(); |
| 2717 | } else { |
| 2718 | // Otherwise, add client to active clients list |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2719 | finishConnectLocked(client, partial, oomScoreOffset, systemNativeClient); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2720 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 2721 | |
| 2722 | client->setImageDumpMask(mImageDumpMask); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 2723 | client->setStreamUseCaseOverrides(mStreamUseCaseOverrides); |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 2724 | client->setZoomOverride(mZoomOverrideValue); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2725 | } // lock is destroyed, allow further connect calls |
| 2726 | |
| 2727 | // Important: release the mutex here so the client can call back into the service from its |
| 2728 | // destructor (can be at the end of the call) |
| 2729 | device = client; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2730 | |
| 2731 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 2732 | mCameraServiceProxyWrapper->logOpen(cameraId, facing, clientPackageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2733 | effectiveApiLevel, isNonSystemNdk, openLatencyMs); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2734 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2735 | { |
| 2736 | Mutex::Autolock lock(mInjectionParametersLock); |
| 2737 | if (cameraId == mInjectionInternalCamId && mInjectionInitPending) { |
| 2738 | mInjectionInitPending = false; |
| 2739 | status_t res = NO_ERROR; |
| 2740 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 2741 | if (clientDescriptor != nullptr) { |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2742 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 2743 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 2744 | if(res != OK) { |
| 2745 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2746 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2747 | mInjectionExternalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2748 | } |
| 2749 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2750 | if (res != OK) { |
| 2751 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2752 | } |
| 2753 | } else { |
| 2754 | ALOGE("%s: Internal camera ID = %s 's client does not exist!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2755 | __FUNCTION__, mInjectionInternalCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2756 | res = NO_INIT; |
| 2757 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2758 | } |
| 2759 | } |
| 2760 | } |
| 2761 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2762 | return ret; |
| 2763 | } |
| 2764 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2765 | status_t CameraService::addOfflineClient(const std::string &cameraId, |
| 2766 | sp<BasicClient> offlineClient) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2767 | if (offlineClient.get() == nullptr) { |
| 2768 | return BAD_VALUE; |
| 2769 | } |
| 2770 | |
| 2771 | { |
| 2772 | // Acquire mServiceLock and prevent other clients from connecting |
| 2773 | std::unique_ptr<AutoConditionLock> lock = |
| 2774 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2775 | |
| 2776 | if (lock == nullptr) { |
| 2777 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 2778 | , __FUNCTION__, offlineClient->getClientCallingPid()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2779 | return TIMED_OUT; |
| 2780 | } |
| 2781 | |
| 2782 | auto onlineClientDesc = mActiveClientManager.get(cameraId); |
| 2783 | if (onlineClientDesc.get() == nullptr) { |
| 2784 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, |
| 2785 | cameraId.c_str()); |
| 2786 | return BAD_VALUE; |
| 2787 | } |
| 2788 | |
| 2789 | // Offline clients do not evict or conflict with other online devices. Resource sharing |
| 2790 | // conflicts are handled by the camera provider which will either succeed or fail before |
| 2791 | // reaching this method. |
| 2792 | const auto& onlinePriority = onlineClientDesc->getPriority(); |
| 2793 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( |
| 2794 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2795 | /*conflictingKeys*/ std::set<std::string>(), onlinePriority.getScore(), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2796 | onlineClientDesc->getOwnerId(), onlinePriority.getState(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2797 | // native clients don't have offline processing support. |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 2798 | /*ommScoreOffset*/ 0, /*systemNativeClient*/false, /*sharedMode*/false); |
Guillaume Bailey | 417e43d | 2022-11-02 15:30:24 +0100 | [diff] [blame] | 2799 | if (offlineClientDesc == nullptr) { |
| 2800 | ALOGE("%s: Offline client descriptor was NULL", __FUNCTION__); |
| 2801 | return BAD_VALUE; |
| 2802 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2803 | |
| 2804 | // Allow only one offline device per camera |
| 2805 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); |
| 2806 | if (!incompatibleClients.empty()) { |
| 2807 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); |
| 2808 | return BAD_VALUE; |
| 2809 | } |
| 2810 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2811 | std::string monitorTags = isClientWatched(offlineClient.get()) |
| 2812 | ? mMonitorTags : std::string(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2813 | auto err = offlineClient->initialize(mCameraProviderManager, monitorTags); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2814 | if (err != OK) { |
| 2815 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); |
| 2816 | return err; |
| 2817 | } |
| 2818 | |
| 2819 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); |
| 2820 | if (evicted.size() > 0) { |
| 2821 | for (auto& i : evicted) { |
| 2822 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2823 | __FUNCTION__, i->getKey().c_str()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " |
| 2827 | "properly", __FUNCTION__); |
| 2828 | |
| 2829 | return BAD_VALUE; |
| 2830 | } |
| 2831 | |
| 2832 | logConnectedOffline(offlineClientDesc->getKey(), |
| 2833 | static_cast<int>(offlineClientDesc->getOwnerId()), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2834 | offlineClient->getPackageName()); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2835 | |
| 2836 | sp<IBinder> remoteCallback = offlineClient->getRemote(); |
| 2837 | if (remoteCallback != nullptr) { |
| 2838 | remoteCallback->linkToDeath(this); |
| 2839 | } |
| 2840 | } // lock is destroyed, allow further connect calls |
| 2841 | |
| 2842 | return OK; |
| 2843 | } |
| 2844 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2845 | Status CameraService::turnOnTorchWithStrengthLevel(const std::string& unresolvedCameraId, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2846 | int32_t torchStrength, const sp<IBinder>& clientBinder, |
| 2847 | const AttributionSourceState& clientAttribution, int32_t devicePolicy) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2848 | Mutex::Autolock lock(mServiceLock); |
| 2849 | |
| 2850 | ATRACE_CALL(); |
| 2851 | if (clientBinder == nullptr) { |
| 2852 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
| 2853 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2854 | "Torch client binder in null."); |
| 2855 | } |
| 2856 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2857 | int uid = getCallingUid(); |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2858 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, |
| 2859 | clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2860 | if (!cameraIdOptional.has_value()) { |
| 2861 | 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] | 2862 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2863 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2864 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 2865 | } |
| 2866 | std::string cameraId = cameraIdOptional.value(); |
| 2867 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2868 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2869 | 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] | 2870 | "for system only device %s: ", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | // verify id is valid |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2874 | auto state = getCameraState(cameraId); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2875 | if (state == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2876 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2877 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2878 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | StatusInternal cameraStatus = state->getStatus(); |
| 2882 | if (cameraStatus != StatusInternal::NOT_AVAILABLE && |
| 2883 | cameraStatus != StatusInternal::PRESENT) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2884 | 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] | 2885 | (int)cameraStatus); |
| 2886 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2887 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2888 | } |
| 2889 | |
| 2890 | { |
| 2891 | Mutex::Autolock al(mTorchStatusMutex); |
| 2892 | TorchModeStatus status; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2893 | status_t err = getTorchStatusLocked(cameraId, &status); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2894 | if (err != OK) { |
| 2895 | if (err == NAME_NOT_FOUND) { |
| 2896 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2897 | "Camera \"%s\" does not have a flash unit", cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2898 | } |
| 2899 | ALOGE("%s: getting current torch status failed for camera %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2900 | __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2901 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2902 | "Error changing torch strength level for camera \"%s\": %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2903 | cameraId.c_str(), strerror(-err), err); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
| 2907 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
| 2908 | ALOGE("%s: torch mode of camera %s is not available because " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2909 | "camera is in use.", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2910 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2911 | "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] | 2912 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2913 | } else { |
| 2914 | ALOGE("%s: torch mode of camera %s is not available due to " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2915 | "insufficient resources", __FUNCTION__, cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2916 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2917 | "Torch for camera \"%s\" is not available due to insufficient resources", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2918 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2919 | } |
| 2920 | } |
| 2921 | } |
| 2922 | |
| 2923 | { |
| 2924 | Mutex::Autolock al(mTorchUidMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2925 | updateTorchUidMapLocked(cameraId, uid); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2926 | } |
| 2927 | // Check if the current torch strength level is same as the new one. |
| 2928 | bool shouldSkipTorchStrengthUpdates = mCameraProviderManager->shouldSkipTorchStrengthUpdate( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2929 | cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2930 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2931 | status_t err = mFlashlight->turnOnTorchWithStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2932 | |
| 2933 | if (err != OK) { |
| 2934 | int32_t errorCode; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2935 | std::string msg; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2936 | switch (err) { |
| 2937 | case -ENOSYS: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2938 | msg = fmt::sprintf("Camera \"%s\" has no flashlight.", |
| 2939 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2940 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2941 | break; |
| 2942 | case -EBUSY: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2943 | msg = fmt::sprintf("Camera \"%s\" is in use", |
| 2944 | cameraId.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2945 | errorCode = ERROR_CAMERA_IN_USE; |
| 2946 | break; |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 2947 | case -EINVAL: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2948 | msg = fmt::sprintf("Torch strength level %d is not within the " |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 2949 | "valid range.", torchStrength); |
| 2950 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2951 | break; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2952 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2953 | msg = "Changing torch strength level failed."; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2954 | errorCode = ERROR_INVALID_OPERATION; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2955 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2956 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 2957 | return STATUS_ERROR(errorCode, msg.c_str()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | { |
| 2961 | // update the link to client's death |
| 2962 | // Store the last client that turns on each camera's torch mode. |
| 2963 | Mutex::Autolock al(mTorchClientMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2964 | ssize_t index = mTorchClientMap.indexOfKey(cameraId); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2965 | if (index == NAME_NOT_FOUND) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2966 | mTorchClientMap.add(cameraId, clientBinder); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2967 | } else { |
| 2968 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
| 2969 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2970 | } |
| 2971 | clientBinder->linkToDeath(this); |
| 2972 | } |
| 2973 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2974 | int clientPid = getCallingPid(); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2975 | 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] | 2976 | __FUNCTION__, cameraId.c_str(), torchStrength, clientPid); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2977 | if (!shouldSkipTorchStrengthUpdates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 2978 | broadcastTorchStrengthLevel(cameraId, torchStrength); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2979 | } |
| 2980 | return Status::ok(); |
| 2981 | } |
| 2982 | |
malikakash | 73125c6 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 2983 | Status CameraService::setTorchMode(const std::string& unresolvedCameraId, bool enabled, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2984 | const sp<IBinder>& clientBinder, const AttributionSourceState& clientAttribution, |
| 2985 | int32_t devicePolicy) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2986 | Mutex::Autolock lock(mServiceLock); |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2987 | |
| 2988 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2989 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2990 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2991 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2992 | "Torch client Binder is null"); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2993 | } |
| 2994 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 2995 | int uid = getCallingUid(); |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 2996 | std::optional<std::string> cameraIdOptional = resolveCameraId(unresolvedCameraId, |
| 2997 | clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 2998 | if (!cameraIdOptional.has_value()) { |
| 2999 | 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] | 3000 | unresolvedCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3001 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3002 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 3003 | } |
| 3004 | std::string cameraId = cameraIdOptional.value(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3005 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3006 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3007 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3008 | " for system only device %s: ", cameraId.c_str()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3009 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3010 | // verify id is valid. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3011 | auto state = getCameraState(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3012 | if (state == nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3013 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3014 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3015 | "Camera ID \"%s\" is a not valid camera ID", cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3016 | } |
| 3017 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3018 | StatusInternal cameraStatus = state->getStatus(); |
| 3019 | if (cameraStatus != StatusInternal::PRESENT && |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 3020 | cameraStatus != StatusInternal::NOT_AVAILABLE) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3021 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, cameraId.c_str(), |
| 3022 | (int)cameraStatus); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3023 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3024 | "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] | 3025 | } |
| 3026 | |
| 3027 | { |
| 3028 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3029 | TorchModeStatus status; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3030 | status_t err = getTorchStatusLocked(cameraId, &status); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3031 | if (err != OK) { |
| 3032 | if (err == NAME_NOT_FOUND) { |
| 3033 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3034 | "Camera \"%s\" does not have a flash unit", cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3035 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3036 | ALOGE("%s: getting current torch status failed for camera %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3037 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3038 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3039 | "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] | 3040 | strerror(-err), err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3041 | } |
| 3042 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3043 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 3044 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3045 | ALOGE("%s: torch mode of camera %s is not available because " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3046 | "camera is in use", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3047 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 3048 | "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] | 3049 | cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3050 | } else { |
| 3051 | ALOGE("%s: torch mode of camera %s is not available due to " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3052 | "insufficient resources", __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3053 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 3054 | "Torch for camera \"%s\" is not available due to insufficient resources", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3055 | cameraId.c_str()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3056 | } |
| 3057 | } |
| 3058 | } |
| 3059 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3060 | { |
| 3061 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 3062 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 3063 | Mutex::Autolock al(mTorchUidMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3064 | updateTorchUidMapLocked(cameraId, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3065 | } |
| 3066 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3067 | status_t err = mFlashlight->setTorchMode(cameraId, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3068 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3069 | if (err != OK) { |
| 3070 | int32_t errorCode; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3071 | std::string msg; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3072 | switch (err) { |
| 3073 | case -ENOSYS: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3074 | msg = fmt::sprintf("Camera \"%s\" has no flashlight", |
| 3075 | cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3076 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 3077 | break; |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 3078 | case -EBUSY: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3079 | msg = fmt::sprintf("Camera \"%s\" is in use", |
| 3080 | cameraId.c_str()); |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 3081 | errorCode = ERROR_CAMERA_IN_USE; |
| 3082 | break; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3083 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3084 | msg = fmt::sprintf( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3085 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3086 | cameraId.c_str(), enabled, strerror(-err), err); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3087 | errorCode = ERROR_INVALID_OPERATION; |
| 3088 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3089 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3090 | logServiceError(msg, errorCode); |
| 3091 | return STATUS_ERROR(errorCode, msg.c_str()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3092 | } |
| 3093 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3094 | { |
| 3095 | // update the link to client's death |
| 3096 | Mutex::Autolock al(mTorchClientMapMutex); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3097 | ssize_t index = mTorchClientMap.indexOfKey(cameraId); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3098 | if (enabled) { |
| 3099 | if (index == NAME_NOT_FOUND) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3100 | mTorchClientMap.add(cameraId, clientBinder); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3101 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3102 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 3103 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 3104 | } |
| 3105 | clientBinder->linkToDeath(this); |
| 3106 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3107 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3108 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3109 | } |
| 3110 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3111 | int clientPid = getCallingPid(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3112 | std::string torchState = enabled ? "on" : "off"; |
| 3113 | ALOGI("Torch for camera id %s turned %s for client PID %d", cameraId.c_str(), |
| 3114 | torchState.c_str(), clientPid); |
| 3115 | logTorchEvent(cameraId, torchState, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3116 | return Status::ok(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3117 | } |
| 3118 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3119 | void CameraService::updateTorchUidMapLocked(const std::string& cameraId, int uid) { |
| 3120 | if (mTorchUidMap.find(cameraId) == mTorchUidMap.end()) { |
| 3121 | mTorchUidMap[cameraId].first = uid; |
| 3122 | mTorchUidMap[cameraId].second = uid; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3123 | } else { |
| 3124 | // Set the pending UID |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3125 | mTorchUidMap[cameraId].first = uid; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 3126 | } |
| 3127 | } |
| 3128 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3129 | Status CameraService::notifySystemEvent(int32_t eventId, |
| 3130 | const std::vector<int32_t>& args) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3131 | const int pid = getCallingPid(); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3132 | const int selfPid = getpid(); |
| 3133 | |
| 3134 | // Permission checks |
| 3135 | if (pid != selfPid) { |
| 3136 | // Ensure we're being called by system_server, or similar process with |
| 3137 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3138 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3139 | const int uid = getCallingUid(); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3140 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 3141 | " events from pid=%d, uid=%d", pid, uid); |
| 3142 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 3143 | "No permission to send updates to camera service about system events" |
| 3144 | " from pid=%d, uid=%d", pid, uid); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 3145 | } |
| 3146 | } |
| 3147 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3148 | ATRACE_CALL(); |
| 3149 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3150 | switch(eventId) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3151 | case ICameraService::EVENT_USER_SWITCHED: { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3152 | // 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] | 3153 | // from a system server crash |
| 3154 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3155 | mSensorPrivacyPolicy->registerSelf(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3156 | doUserSwitch(/*newUserIds*/ args); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3157 | break; |
| 3158 | } |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 3159 | case ICameraService::EVENT_USB_DEVICE_ATTACHED: |
| 3160 | case ICameraService::EVENT_USB_DEVICE_DETACHED: { |
Pawan Wagh | 99f44e2 | 2023-07-05 21:26:43 +0000 | [diff] [blame] | 3161 | if (args.size() != 1) { |
| 3162 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, |
| 3163 | "USB Device Event requires 1 argument"); |
| 3164 | } |
| 3165 | |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 3166 | // Notify CameraProviderManager for lazy HALs |
| 3167 | mCameraProviderManager->notifyUsbDeviceEvent(eventId, |
| 3168 | std::to_string(args[0])); |
| 3169 | break; |
| 3170 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3171 | case ICameraService::EVENT_NONE: |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3172 | default: { |
| 3173 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 3174 | eventId); |
| 3175 | break; |
| 3176 | } |
| 3177 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3178 | return Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3179 | } |
| 3180 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3181 | void CameraService::notifyMonitoredUids() { |
| 3182 | Mutex::Autolock lock(mStatusListenerLock); |
| 3183 | |
| 3184 | for (const auto& it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3185 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 3186 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 3187 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3188 | } |
| 3189 | } |
| 3190 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3191 | void CameraService::notifyMonitoredUids(const std::unordered_set<uid_t> ¬ifyUidSet) { |
| 3192 | Mutex::Autolock lock(mStatusListenerLock); |
| 3193 | |
| 3194 | for (const auto& it : mListenerList) { |
| 3195 | if (notifyUidSet.find(it->getListenerUid()) != notifyUidSet.end()) { |
| 3196 | ALOGV("%s: notifying uid %d", __FUNCTION__, it->getListenerUid()); |
| 3197 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
| 3198 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 3199 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
| 3200 | } |
| 3201 | } |
| 3202 | } |
| 3203 | |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3204 | Status CameraService::notifyDeviceStateChange(int64_t newState) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3205 | const int pid = getCallingPid(); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3206 | const int selfPid = getpid(); |
| 3207 | |
| 3208 | // Permission checks |
| 3209 | if (pid != selfPid) { |
| 3210 | // Ensure we're being called by system_server, or similar process with |
| 3211 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3212 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3213 | const int uid = getCallingUid(); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3214 | ALOGE("Permission Denial: cannot send updates to camera service about device" |
| 3215 | " state changes from pid=%d, uid=%d", pid, uid); |
| 3216 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 3217 | "No permission to send updates to camera service about device state" |
| 3218 | " changes from pid=%d, uid=%d", pid, uid); |
| 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | ATRACE_CALL(); |
| 3223 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3224 | { |
| 3225 | Mutex::Autolock lock(mServiceLock); |
| 3226 | mDeviceState = newState; |
| 3227 | } |
| 3228 | |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 3229 | mCameraProviderManager->notifyDeviceStateChange(newState); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 3230 | |
| 3231 | return Status::ok(); |
| 3232 | } |
| 3233 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3234 | Status CameraService::notifyDisplayConfigurationChange() { |
| 3235 | ATRACE_CALL(); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3236 | const int callingPid = getCallingPid(); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3237 | const int selfPid = getpid(); |
| 3238 | |
| 3239 | // Permission checks |
| 3240 | if (callingPid != selfPid) { |
| 3241 | // Ensure we're being called by system_server, or similar process with |
| 3242 | // permissions to notify the camera service about system events |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3243 | if (!checkCallingPermission(toString16(sCameraSendSystemEventsPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3244 | const int uid = getCallingUid(); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3245 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" |
| 3246 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 3247 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 3248 | "No permission to send updates to camera service about orientation" |
| 3249 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 3250 | } |
| 3251 | } |
| 3252 | |
| 3253 | Mutex::Autolock lock(mServiceLock); |
| 3254 | |
| 3255 | // Don't do anything if rotate-and-crop override via cmd is active |
| 3256 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); |
| 3257 | |
| 3258 | const auto clients = mActiveClientManager.getAll(); |
| 3259 | for (auto& current : clients) { |
| 3260 | if (current != nullptr) { |
| 3261 | const auto basicClient = current->getValue(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3262 | if (basicClient.get() != nullptr && !basicClient->getOverrideToPortrait()) { |
| 3263 | basicClient->setRotateAndCropOverride( |
| 3264 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 3265 | basicClient->getPackageName(), |
| 3266 | basicClient->getCameraFacing(), |
| 3267 | multiuser_get_user_id(basicClient->getClientUid()))); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3268 | } |
| 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | return Status::ok(); |
| 3273 | } |
| 3274 | |
| 3275 | Status CameraService::getConcurrentCameraIds( |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3276 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { |
| 3277 | ATRACE_CALL(); |
| 3278 | if (!concurrentCameraIds) { |
| 3279 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); |
| 3280 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); |
| 3281 | } |
| 3282 | |
| 3283 | if (!mInitialized) { |
| 3284 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3285 | logServiceError("Camera subsystem is not available", ERROR_DISCONNECTED); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3286 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 3287 | "Camera subsystem is not available"); |
| 3288 | } |
| 3289 | // First call into the provider and get the set of concurrent camera |
| 3290 | // combinations |
| 3291 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = |
Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 3292 | mCameraProviderManager->getConcurrentCameraIds(); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3293 | for (auto &combination : concurrentCameraCombinations) { |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3294 | std::vector<std::pair<std::string, int32_t>> validCombination; |
| 3295 | int32_t firstDeviceId = kInvalidDeviceId; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3296 | for (auto &cameraId : combination) { |
| 3297 | // if the camera state is not present, skip |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3298 | auto state = getCameraState(cameraId); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3299 | if (state == nullptr) { |
| 3300 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); |
| 3301 | continue; |
| 3302 | } |
| 3303 | StatusInternal status = state->getStatus(); |
| 3304 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { |
| 3305 | continue; |
| 3306 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3307 | if (shouldRejectSystemCameraConnection(cameraId)) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3308 | continue; |
| 3309 | } |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3310 | auto [cameraOwnerDeviceId, mappedCameraId] = |
| 3311 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 3312 | if (firstDeviceId == kInvalidDeviceId) { |
| 3313 | firstDeviceId = cameraOwnerDeviceId; |
| 3314 | } else if (firstDeviceId != cameraOwnerDeviceId) { |
| 3315 | // Found an invalid combination which contains cameras with different device id's, |
| 3316 | // hence discard it. |
| 3317 | validCombination.clear(); |
| 3318 | break; |
| 3319 | } |
| 3320 | validCombination.push_back({mappedCameraId, cameraOwnerDeviceId}); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3321 | } |
| 3322 | if (validCombination.size() != 0) { |
| 3323 | concurrentCameraIds->push_back(std::move(validCombination)); |
| 3324 | } |
| 3325 | } |
| 3326 | return Status::ok(); |
| 3327 | } |
| 3328 | |
| 3329 | Status CameraService::isConcurrentSessionConfigurationSupported( |
| 3330 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 3331 | int targetSdkVersion, const AttributionSourceState& clientAttribution, int32_t devicePolicy, |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3332 | /*out*/bool* isSupported) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3333 | if (!isSupported) { |
| 3334 | ALOGE("%s: isSupported is NULL", __FUNCTION__); |
| 3335 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); |
| 3336 | } |
| 3337 | |
| 3338 | if (!mInitialized) { |
| 3339 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 3340 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 3341 | "Camera subsystem is not available"); |
| 3342 | } |
| 3343 | |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3344 | for (auto cameraIdAndSessionConfiguration : cameraIdsAndSessionConfigurations) { |
| 3345 | std::optional<std::string> cameraIdOptional = |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 3346 | resolveCameraId(cameraIdAndSessionConfiguration.mCameraId, |
| 3347 | clientAttribution.deviceId, devicePolicy); |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3348 | if (!cameraIdOptional.has_value()) { |
| 3349 | 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] | 3350 | cameraIdAndSessionConfiguration.mCameraId.c_str(), clientAttribution.deviceId); |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3351 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3352 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
| 3353 | } |
| 3354 | cameraIdAndSessionConfiguration.mCameraId = cameraIdOptional.value(); |
| 3355 | } |
| 3356 | |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3357 | // Check for camera permissions |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3358 | int callingPid = getCallingPid(); |
| 3359 | int callingUid = getCallingUid(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 3360 | bool hasCameraPermission = ((callingPid == getpid()) || |
Biswarup Pal | 7d07286 | 2024-04-17 15:24:47 +0000 | [diff] [blame] | 3361 | hasPermissionsForCamera(callingPid, callingUid, |
| 3362 | devicePolicy == IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 3363 | ? kDefaultDeviceId : clientAttribution.deviceId)); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 3364 | if (!hasCameraPermission) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3365 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 3366 | "android.permission.CAMERA needed to call" |
| 3367 | "isConcurrentSessionConfigurationSupported"); |
| 3368 | } |
| 3369 | |
| 3370 | status_t res = |
| 3371 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 3372 | cameraIdsAndSessionConfigurations, mPerfClassPrimaryCameraIds, |
| 3373 | targetSdkVersion, isSupported); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3374 | if (res != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3375 | logServiceError("Unable to query session configuration support", |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3376 | ERROR_INVALID_OPERATION); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 3377 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " |
| 3378 | "support %s (%d)", strerror(-res), res); |
| 3379 | } |
| 3380 | return Status::ok(); |
| 3381 | } |
| 3382 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3383 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, |
| 3384 | /*out*/ |
| 3385 | std::vector<hardware::CameraStatus> *cameraStatuses) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3386 | return addListenerHelper(listener, cameraStatuses); |
| 3387 | } |
| 3388 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3389 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, |
| 3390 | std::vector<hardware::CameraStatus>* cameraStatuses) { |
| 3391 | return addListenerHelper(listener, cameraStatuses, false, true); |
| 3392 | } |
| 3393 | |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 3394 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, |
| 3395 | /*out*/ |
| 3396 | std::vector<hardware::CameraStatus> *cameraStatuses, |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3397 | bool isVendorListener, bool isProcessLocalTest) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3398 | ATRACE_CALL(); |
| 3399 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3400 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3401 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 3402 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3403 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3404 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3405 | } |
| 3406 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3407 | auto clientPid = getCallingPid(); |
| 3408 | auto clientUid = getCallingUid(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 3409 | bool openCloseCallbackAllowed = hasPermissionsForOpenCloseListener(clientPid, clientUid); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3410 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3411 | Mutex::Autolock lock(mServiceLock); |
| 3412 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3413 | { |
| 3414 | Mutex::Autolock lock(mStatusListenerLock); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3415 | for (const auto &it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3416 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3417 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 3418 | __FUNCTION__, listener.get()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3419 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3420 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3421 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3422 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3423 | sp<ServiceListener> serviceListener = |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3424 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, |
| 3425 | openCloseCallbackAllowed); |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 3426 | auto ret = serviceListener->initialize(isProcessLocalTest); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3427 | if (ret != NO_ERROR) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3428 | std::string msg = fmt::sprintf("Failed to initialize service listener: %s (%d)", |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3429 | strerror(-ret), ret); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3430 | logServiceError(msg, ERROR_ILLEGAL_ARGUMENT); |
| 3431 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3432 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3433 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3434 | // The listener still needs to be added to the list of listeners, regardless of what |
| 3435 | // permissions the listener process has / whether it is a vendor listener. Since it might be |
| 3436 | // eligible to listen to other camera ids. |
| 3437 | mListenerList.emplace_back(serviceListener); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3438 | mUidPolicy->registerMonitorUid(clientUid, /*openCamera*/false); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3439 | } |
| 3440 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3441 | /* Collect current devices and status */ |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3442 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3443 | Mutex::Autolock lock(mCameraStatesLock); |
| 3444 | for (auto& i : mCameraStates) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3445 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 3446 | auto [deviceId, mappedCameraId] = |
| 3447 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(i.first); |
| 3448 | |
| 3449 | cameraStatuses->emplace_back(mappedCameraId, |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 3450 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds(), |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3451 | openCloseCallbackAllowed ? i.second->getClientPackage() : std::string(), |
| 3452 | deviceId); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3453 | } |
| 3454 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3455 | // Remove the camera statuses that should be hidden from the client, we do |
| 3456 | // this after collecting the states in order to avoid holding |
| 3457 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at |
| 3458 | // the same time. |
| 3459 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), |
| 3460 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3461 | std::string cameraId = s.cameraId; |
| 3462 | std::optional<std::string> cameraIdOptional = resolveCameraId(s.cameraId, |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 3463 | s.deviceId, IVirtualDeviceManagerNative::DEVICE_POLICY_CUSTOM); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3464 | if (!cameraIdOptional.has_value()) { |
| 3465 | std::string msg = |
| 3466 | fmt::sprintf( |
| 3467 | "Camera %s: Invalid camera id for device id %d", |
| 3468 | s.cameraId.c_str(), s.deviceId); |
| 3469 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3470 | return true; |
| 3471 | } |
| 3472 | cameraId = cameraIdOptional.value(); |
| 3473 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 3474 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 3475 | ALOGE("%s: Invalid camera id %s, skipping status update", |
| 3476 | __FUNCTION__, s.cameraId.c_str()); |
| 3477 | return true; |
| 3478 | } |
| 3479 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, |
| 3480 | clientUid); |
| 3481 | }), cameraStatuses->end()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3482 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3483 | // cameraStatuses will have non-eligible camera ids removed. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3484 | std::set<std::string> idsChosenForCallback; |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3485 | for (const auto &s : *cameraStatuses) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3486 | // Add only default device cameras here, as virtual cameras currently don't support torch |
| 3487 | // anyway. Note that this is a simplification of the implementation here, and we should |
| 3488 | // change this when virtual cameras support torch. |
| 3489 | if (s.deviceId == kDefaultDeviceId) { |
| 3490 | idsChosenForCallback.insert(s.cameraId); |
| 3491 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3492 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3493 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3494 | /* |
| 3495 | * Immediately signal current torch status to this listener only |
| 3496 | * This may be a subset of all the devices, so don't include it in the response directly |
| 3497 | */ |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3498 | { |
| 3499 | Mutex::Autolock al(mTorchStatusMutex); |
| 3500 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3501 | const std::string &id = mTorchStatusMap.keyAt(i); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3502 | // The camera id is visible to the client. Fine to send torch |
| 3503 | // callback. |
| 3504 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3505 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id, |
| 3506 | kDefaultDeviceId); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 3507 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3508 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 3509 | } |
| 3510 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3511 | return Status::ok(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3512 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3513 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3514 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3515 | ATRACE_CALL(); |
| 3516 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3517 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 3518 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3519 | if (listener == 0) { |
| 3520 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3521 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3522 | } |
| 3523 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3524 | Mutex::Autolock lock(mServiceLock); |
| 3525 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3526 | { |
| 3527 | Mutex::Autolock lock(mStatusListenerLock); |
| 3528 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3529 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3530 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid(), /*closeCamera*/false); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3531 | IInterface::asBinder(listener)->unlinkToDeath(*it); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3532 | mListenerList.erase(it); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3533 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3534 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3535 | } |
| 3536 | } |
| 3537 | |
| 3538 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 3539 | __FUNCTION__, listener.get()); |
| 3540 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3541 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3542 | } |
| 3543 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3544 | Status CameraService::getLegacyParameters(int cameraId, /*out*/std::string* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3545 | |
| 3546 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3547 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 3548 | |
| 3549 | if (parameters == NULL) { |
| 3550 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3551 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3552 | } |
| 3553 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3554 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3555 | |
| 3556 | CameraParameters shimParams; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3557 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3558 | // Error logged by caller |
| 3559 | return ret; |
| 3560 | } |
| 3561 | |
| 3562 | String8 shimParamsString8 = shimParams.flatten(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3563 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3564 | *parameters = toStdString(shimParamsString8); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3565 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3566 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3567 | } |
| 3568 | |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 3569 | Status CameraService::supportsCameraApi(const std::string& cameraId, int apiVersion, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3570 | /*out*/ bool *isSupported) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3571 | ATRACE_CALL(); |
| 3572 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3573 | ALOGV("%s: for camera ID = %s", __FUNCTION__, cameraId.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3574 | |
| 3575 | switch (apiVersion) { |
| 3576 | case API_VERSION_1: |
| 3577 | case API_VERSION_2: |
| 3578 | break; |
| 3579 | default: |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3580 | std::string msg = fmt::sprintf("Unknown API version %d", apiVersion); |
| 3581 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3582 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3583 | } |
| 3584 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3585 | int portraitRotation; |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 3586 | auto deviceVersionAndTransport = |
| 3587 | getDeviceVersion(cameraId, |
| 3588 | /*rotationOverride*/hardware::ICameraService::ROTATION_OVERRIDE_NONE, |
| 3589 | &portraitRotation); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3590 | if (deviceVersionAndTransport.first == -1) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3591 | std::string msg = fmt::sprintf("Unknown camera ID %s", cameraId.c_str()); |
| 3592 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3593 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3594 | } |
| 3595 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 3596 | int deviceVersion = deviceVersionAndTransport.first; |
| 3597 | switch (deviceVersion) { |
| 3598 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 3599 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 3600 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 3601 | if (apiVersion == API_VERSION_2) { |
| 3602 | 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] | 3603 | "shim", __FUNCTION__, cameraId.c_str(), deviceVersion); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3604 | *isSupported = false; |
| 3605 | } else { // if (apiVersion == API_VERSION_1) { |
| 3606 | 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] | 3607 | "supported", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3608 | *isSupported = true; |
| 3609 | } |
| 3610 | break; |
| 3611 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 3612 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 3613 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 3614 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 3615 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 3616 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 3617 | 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] | 3618 | __FUNCTION__, cameraId.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3619 | *isSupported = true; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3620 | break; |
| 3621 | default: { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3622 | std::string msg = fmt::sprintf("Unknown device version %x for device %s", |
| 3623 | deviceVersion, cameraId.c_str()); |
| 3624 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
| 3625 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3626 | } |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3627 | } |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3628 | } else { |
| 3629 | *isSupported = true; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3630 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3631 | return Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3632 | } |
| 3633 | |
malikakash | 98260df | 2024-05-10 23:33:57 +0000 | [diff] [blame] | 3634 | Status CameraService::isHiddenPhysicalCamera(const std::string& cameraId, |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3635 | /*out*/ bool *isSupported) { |
| 3636 | ATRACE_CALL(); |
| 3637 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3638 | ALOGV("%s: for camera ID = %s", __FUNCTION__, cameraId.c_str()); |
| 3639 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(cameraId); |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3640 | |
| 3641 | return Status::ok(); |
| 3642 | } |
| 3643 | |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3644 | Status CameraService::injectCamera( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3645 | const std::string& packageName, const std::string& internalCamId, |
| 3646 | const std::string& externalCamId, |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3647 | const sp<ICameraInjectionCallback>& callback, |
| 3648 | /*out*/ |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3649 | sp<ICameraInjectionSession>* cameraInjectionSession) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3650 | ATRACE_CALL(); |
| 3651 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3652 | if (!checkCallingPermission(toString16(sCameraInjectExternalCameraPermission))) { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3653 | const int pid = getCallingPid(); |
| 3654 | const int uid = getCallingUid(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3655 | ALOGE("Permission Denial: can't inject camera pid=%d, uid=%d", pid, uid); |
| 3656 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3657 | "Permission Denial: no permission to inject camera"); |
| 3658 | } |
| 3659 | |
| 3660 | // Do not allow any camera injection that injects or replaces a virtual camera. |
| 3661 | auto [deviceIdForInternalCamera, _] = |
| 3662 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(internalCamId); |
| 3663 | if (deviceIdForInternalCamera != kDefaultDeviceId) { |
| 3664 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED, |
| 3665 | "Cannot replace a virtual camera"); |
| 3666 | } |
| 3667 | [[maybe_unused]] auto [deviceIdForExternalCamera, unusedMappedCameraId] = |
| 3668 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(externalCamId); |
| 3669 | if (deviceIdForExternalCamera != kDefaultDeviceId) { |
| 3670 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED, |
| 3671 | "Cannot inject a virtual camera to replace an internal camera"); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | ALOGV( |
| 3675 | "%s: Package name = %s, Internal camera ID = %s, External camera ID = " |
| 3676 | "%s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3677 | __FUNCTION__, packageName.c_str(), |
| 3678 | internalCamId.c_str(), externalCamId.c_str()); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3679 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3680 | { |
| 3681 | Mutex::Autolock lock(mInjectionParametersLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3682 | mInjectionInternalCamId = internalCamId; |
| 3683 | mInjectionExternalCamId = externalCamId; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3684 | mInjectionStatusListener->addListener(callback); |
| 3685 | *cameraInjectionSession = new CameraInjectionSession(this); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3686 | status_t res = NO_ERROR; |
| 3687 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 3688 | // If the client already exists, we can directly connect to the camera device through the |
| 3689 | // client's injectCamera(), otherwise we need to wait until the client is established |
| 3690 | // (execute connectHelper()) before injecting the camera to the camera device. |
| 3691 | if (clientDescriptor != nullptr) { |
| 3692 | mInjectionInitPending = false; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3693 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 3694 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 3695 | if(res != OK) { |
| 3696 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 3697 | "No camera device with ID \"%s\" currently available", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3698 | mInjectionExternalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3699 | } |
| 3700 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 3701 | if (res != OK) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3702 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 3703 | } |
| 3704 | } else { |
| 3705 | mInjectionInitPending = true; |
| 3706 | } |
| 3707 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3708 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3709 | return binder::Status::ok(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3710 | } |
| 3711 | |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 3712 | Status CameraService::reportExtensionSessionStats( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3713 | const hardware::CameraExtensionSessionStats& stats, std::string* sessionKey /*out*/) { |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 3714 | ALOGV("%s: reported %s", __FUNCTION__, stats.toString().c_str()); |
| 3715 | *sessionKey = mCameraServiceProxyWrapper->updateExtensionStats(stats); |
| 3716 | return Status::ok(); |
| 3717 | } |
| 3718 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3719 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3720 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3721 | for (auto& i : mActiveClientManager.getAll()) { |
| 3722 | auto clientSp = i->getValue(); |
| 3723 | if (clientSp.get() == client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3724 | cacheClientTagDumpIfNeeded(client->mCameraIdStr, clientSp.get()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3725 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3726 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3727 | } |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3728 | updateAudioRestrictionLocked(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3729 | } |
| 3730 | |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 3731 | bool CameraService::isOnlyClient(const BasicClient* client) { |
| 3732 | Mutex::Autolock lock(mServiceLock); |
| 3733 | bool ret = true; |
| 3734 | if (!flags::camera_multi_client()) { |
| 3735 | return ret; |
| 3736 | } |
| 3737 | if (client != nullptr) { |
| 3738 | std::string camId = client->mCameraIdStr; |
| 3739 | for (const auto& i : mActiveClientManager.getAll()) { |
| 3740 | auto clientSp = i->getValue(); |
| 3741 | auto curCamId = i->getKey(); |
| 3742 | if (!curCamId.compare(camId) && clientSp.get() != client) { |
| 3743 | return false; |
| 3744 | } |
| 3745 | } |
| 3746 | } |
| 3747 | return ret; |
| 3748 | } |
| 3749 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3750 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3751 | bool ret = false; |
| 3752 | { |
| 3753 | // Acquire mServiceLock and prevent other clients from connecting |
| 3754 | std::unique_ptr<AutoConditionLock> lock = |
| 3755 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3756 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3757 | std::vector<sp<BasicClient>> evicted; |
| 3758 | for (auto& i : mActiveClientManager.getAll()) { |
| 3759 | auto clientSp = i->getValue(); |
| 3760 | if (clientSp.get() == nullptr) { |
| 3761 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3762 | mActiveClientManager.remove(i); |
| 3763 | continue; |
| 3764 | } |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 3765 | if (remote == clientSp->getRemote()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3766 | mActiveClientManager.remove(i); |
| 3767 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3768 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3769 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3770 | clientSp->notifyError( |
| 3771 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3772 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3773 | } |
| 3774 | } |
| 3775 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3776 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 3777 | // other clients from connecting in mServiceLockWrapper if held |
| 3778 | mServiceLock.unlock(); |
| 3779 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3780 | // Do not clear caller identity, remote caller should be client proccess |
| 3781 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3782 | for (auto& i : evicted) { |
| 3783 | if (i.get() != nullptr) { |
| 3784 | i->disconnect(); |
| 3785 | ret = true; |
| 3786 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3787 | } |
| 3788 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3789 | // Reacquire mServiceLock |
| 3790 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3791 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3792 | } // lock is destroyed, allow further connect calls |
| 3793 | |
| 3794 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3795 | } |
| 3796 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3797 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3798 | const std::string& cameraId) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3799 | std::shared_ptr<CameraState> state; |
| 3800 | { |
| 3801 | Mutex::Autolock lock(mCameraStatesLock); |
| 3802 | auto iter = mCameraStates.find(cameraId); |
| 3803 | if (iter != mCameraStates.end()) { |
| 3804 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3805 | } |
| 3806 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3807 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3808 | } |
| 3809 | |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 3810 | std::vector<sp<CameraService::BasicClient>> CameraService::removeClientsLocked( |
| 3811 | const std::string& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3812 | // Remove from active clients list |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 3813 | std::vector<sp<CameraService::BasicClient>> clients; |
| 3814 | std::vector<CameraService::DescriptorPtr> clientDescriptors; |
| 3815 | clientDescriptors = mActiveClientManager.removeAll(cameraId); |
| 3816 | for (const auto& clientDescriptorPtr : clientDescriptors) { |
| 3817 | sp<BasicClient> client = clientDescriptorPtr->getValue(); |
| 3818 | if (client.get() != nullptr) { |
| 3819 | cacheClientTagDumpIfNeeded(clientDescriptorPtr->getKey(), client.get()); |
| 3820 | } |
| 3821 | clients.push_back(client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3822 | } |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 3823 | return clients; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 3824 | } |
| 3825 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3826 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3827 | // Acquire mServiceLock and prevent other clients from connecting |
| 3828 | std::unique_ptr<AutoConditionLock> lock = |
| 3829 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 3830 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3831 | std::set<userid_t> newAllowedUsers; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3832 | for (size_t i = 0; i < newUserIds.size(); i++) { |
| 3833 | if (newUserIds[i] < 0) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3834 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3835 | __FUNCTION__, newUserIds[i]); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3836 | return; |
| 3837 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3838 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3839 | } |
| 3840 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3841 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3842 | if (newAllowedUsers == mAllowedUsers) { |
| 3843 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 3844 | return; |
| 3845 | } |
| 3846 | |
| 3847 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 3848 | |
| 3849 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3850 | |
| 3851 | // Current user has switched, evict all current clients. |
| 3852 | std::vector<sp<BasicClient>> evicted; |
| 3853 | for (auto& i : mActiveClientManager.getAll()) { |
| 3854 | auto clientSp = i->getValue(); |
| 3855 | |
| 3856 | if (clientSp.get() == nullptr) { |
| 3857 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3858 | continue; |
| 3859 | } |
| 3860 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3861 | // Don't evict clients that are still allowed. |
| 3862 | uid_t clientUid = clientSp->getClientUid(); |
| 3863 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 3864 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 3865 | continue; |
| 3866 | } |
| 3867 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3868 | evicted.push_back(clientSp); |
| 3869 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3870 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3871 | i->getKey().c_str()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3872 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3873 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3874 | logEvent(fmt::sprintf("EVICT device %s client held by package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3875 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due" |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3876 | " to user switch.", i->getKey().c_str(), |
| 3877 | clientSp->getPackageName().c_str(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3878 | i->getOwnerId(), i->getPriority().getScore(), |
| 3879 | i->getPriority().getState())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3880 | |
| 3881 | } |
| 3882 | |
| 3883 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 3884 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 3885 | mServiceLock.unlock(); |
| 3886 | |
| 3887 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3888 | int64_t token = clearCallingIdentity(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3889 | |
| 3890 | for (auto& i : evicted) { |
| 3891 | i->disconnect(); |
| 3892 | } |
| 3893 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 3894 | restoreCallingIdentity(token); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3895 | |
| 3896 | // Reacquire mServiceLock |
| 3897 | mServiceLock.lock(); |
| 3898 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3899 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3900 | void CameraService::logEvent(const std::string &event) { |
| 3901 | std::string curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3902 | Mutex::Autolock l(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3903 | std::string msg = curTime + " : " + event; |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3904 | // For service error events, print the msg only once. |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3905 | if (msg.find("SERVICE ERROR") != std::string::npos) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3906 | mEventLog.add(msg); |
| 3907 | } else if(sServiceErrorEventSet.find(msg) == sServiceErrorEventSet.end()) { |
| 3908 | // Error event not added to the dumpsys log before |
| 3909 | mEventLog.add(msg); |
| 3910 | sServiceErrorEventSet.insert(msg); |
| 3911 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3912 | } |
| 3913 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3914 | void CameraService::logDisconnected(const std::string &cameraId, int clientPid, |
| 3915 | const std::string &clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3916 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3917 | logEvent(fmt::sprintf("DISCONNECT device %s client for package %s (PID %d)", cameraId.c_str(), |
| 3918 | clientPackage.c_str(), clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3919 | } |
| 3920 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3921 | void CameraService::logDisconnectedOffline(const std::string &cameraId, int clientPid, |
| 3922 | const std::string &clientPackage) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3923 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3924 | logEvent(fmt::sprintf("DISCONNECT offline device %s client for package %s (PID %d)", |
| 3925 | cameraId.c_str(), clientPackage.c_str(), clientPid)); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3926 | } |
| 3927 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3928 | void CameraService::logConnected(const std::string &cameraId, int clientPid, |
| 3929 | const std::string &clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3930 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3931 | logEvent(fmt::sprintf("CONNECT device %s client for package %s (PID %d)", cameraId.c_str(), |
| 3932 | clientPackage.c_str(), clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3933 | } |
| 3934 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3935 | void CameraService::logConnectedOffline(const std::string &cameraId, int clientPid, |
| 3936 | const std::string &clientPackage) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3937 | // Log the clients evicted |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3938 | logEvent(fmt::sprintf("CONNECT offline device %s client for package %s (PID %d)", |
| 3939 | cameraId.c_str(), clientPackage.c_str(), clientPid)); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3940 | } |
| 3941 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3942 | void CameraService::logRejected(const std::string &cameraId, int clientPid, |
| 3943 | const std::string &clientPackage, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3944 | // Log the client rejected |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3945 | logEvent(fmt::sprintf("REJECT device %s client for package %s (PID %d), reason: (%s)", |
| 3946 | cameraId.c_str(), clientPackage.c_str(), clientPid, reason.c_str())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3947 | } |
| 3948 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3949 | void CameraService::logTorchEvent(const std::string &cameraId, const std::string &torchState, |
| 3950 | int clientPid) { |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3951 | // Log torch event |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3952 | logEvent(fmt::sprintf("Torch for camera id %s turned %s for client PID %d", cameraId.c_str(), |
| 3953 | torchState.c_str(), clientPid)); |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3954 | } |
| 3955 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3956 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 3957 | const std::set<userid_t>& newUserIds) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3958 | std::string newUsers = toString(newUserIds); |
| 3959 | std::string oldUsers = toString(oldUserIds); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3960 | if (oldUsers.size() == 0) { |
| 3961 | oldUsers = "<None>"; |
| 3962 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3963 | // Log the new and old users |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3964 | logEvent(fmt::sprintf("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s", |
| 3965 | oldUsers.c_str(), newUsers.c_str())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3966 | } |
| 3967 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3968 | void CameraService::logDeviceRemoved(const std::string &cameraId, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3969 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3970 | 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] | 3971 | } |
| 3972 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3973 | void CameraService::logDeviceAdded(const std::string &cameraId, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3974 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3975 | 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] | 3976 | } |
| 3977 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3978 | void CameraService::logClientDied(int clientPid, const std::string &reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3979 | // Log the device removal |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3980 | 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] | 3981 | } |
| 3982 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 3983 | void CameraService::logServiceError(const std::string &msg, int errorCode) { |
| 3984 | logEvent(fmt::sprintf("SERVICE ERROR: %s : %d (%s)", msg.c_str(), errorCode, |
| 3985 | strerror(-errorCode))); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3986 | } |
| 3987 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3988 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 3989 | uint32_t flags) { |
| 3990 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3991 | // Permission checks |
| 3992 | switch (code) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3993 | case SHELL_COMMAND_TRANSACTION: { |
| 3994 | int in = data.readFileDescriptor(); |
| 3995 | int out = data.readFileDescriptor(); |
| 3996 | int err = data.readFileDescriptor(); |
| 3997 | int argc = data.readInt32(); |
| 3998 | Vector<String16> args; |
| 3999 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 4000 | args.add(data.readString16()); |
| 4001 | } |
| 4002 | sp<IBinder> unusedCallback; |
| 4003 | sp<IResultReceiver> resultReceiver; |
| 4004 | status_t status; |
| 4005 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 4006 | return status; |
| 4007 | } |
| 4008 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 4009 | return status; |
| 4010 | } |
| 4011 | status = shellCommand(in, out, err, args); |
| 4012 | if (resultReceiver != nullptr) { |
| 4013 | resultReceiver->send(status); |
| 4014 | } |
| 4015 | return NO_ERROR; |
| 4016 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4017 | } |
| 4018 | |
| 4019 | return BnCameraService::onTransact(code, data, reply, flags); |
| 4020 | } |
| 4021 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4022 | // We share the media players for shutter and recording sound for all clients. |
| 4023 | // A reference count is kept to determine when we will actually release the |
| 4024 | // media players. |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 4025 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { |
| 4026 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 4027 | status_t error; |
| 4028 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 4029 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 4030 | error = mp->prepare(); |
| 4031 | } |
| 4032 | if (error != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4033 | ALOGE("Failed to load CameraService sounds: %s", file); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 4034 | mp->disconnect(); |
| 4035 | mp.clear(); |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4036 | return nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4037 | } |
| 4038 | return mp; |
| 4039 | } |
| 4040 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4041 | void CameraService::increaseSoundRef() { |
| 4042 | Mutex::Autolock lock(mSoundLock); |
| 4043 | mSoundRef++; |
| 4044 | } |
| 4045 | |
| 4046 | void CameraService::loadSoundLocked(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4047 | ATRACE_CALL(); |
| 4048 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4049 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); |
| 4050 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { |
| 4051 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); |
| 4052 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { |
| 4053 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 4054 | } |
| 4055 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) { |
| 4056 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); |
| 4057 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { |
| 4058 | mSoundPlayer[SOUND_RECORDING_START] = |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4059 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4060 | } |
| 4061 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { |
| 4062 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); |
| 4063 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { |
| 4064 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
| 4065 | } |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 4066 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4067 | } |
| 4068 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4069 | void CameraService::decreaseSoundRef() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4070 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4071 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4072 | if (--mSoundRef) return; |
| 4073 | |
| 4074 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 4075 | if (mSoundPlayer[i] != 0) { |
| 4076 | mSoundPlayer[i]->disconnect(); |
| 4077 | mSoundPlayer[i].clear(); |
| 4078 | } |
| 4079 | } |
| 4080 | } |
| 4081 | |
| 4082 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4083 | ATRACE_CALL(); |
| 4084 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4085 | LOG1("playSound(%d)", kind); |
Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame] | 4086 | if (kind < 0 || kind >= NUM_SOUNDS) { |
| 4087 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); |
| 4088 | return; |
| 4089 | } |
| 4090 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4091 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4092 | loadSoundLocked(kind); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4093 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 4094 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 4095 | player->seekTo(0); |
| 4096 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4097 | } |
| 4098 | } |
| 4099 | |
| 4100 | // ---------------------------------------------------------------------------- |
| 4101 | |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4102 | CameraService::Client::Client( |
| 4103 | const sp<CameraService>& cameraService, const sp<ICameraClient>& cameraClient, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4104 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4105 | const AttributionSourceState& clientAttribution, int callingPid, bool systemNativeClient, |
| 4106 | const std::string& cameraIdStr, int api1CameraId, int cameraFacing, int sensorOrientation, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 4107 | int servicePid, int rotationOverride, bool sharedMode) |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4108 | : CameraService::BasicClient(cameraService, IInterface::asBinder(cameraClient), |
| 4109 | attributionAndPermissionUtils, clientAttribution, callingPid, |
| 4110 | systemNativeClient, cameraIdStr, cameraFacing, sensorOrientation, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 4111 | servicePid, rotationOverride, sharedMode), |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4112 | mCameraId(api1CameraId) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4113 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4114 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 4115 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4116 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4117 | cameraService->increaseSoundRef(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4118 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4119 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4120 | } |
| 4121 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4122 | // tear down the client |
| 4123 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4124 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4125 | mDestructionStarted = true; |
| 4126 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 4127 | sCameraService->decreaseSoundRef(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 4128 | // unconditionally disconnect. function is idempotent |
| 4129 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4130 | } |
| 4131 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4132 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; |
| 4133 | |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4134 | CameraService::BasicClient::BasicClient( |
| 4135 | const sp<CameraService>& cameraService, const sp<IBinder>& remoteCallback, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 4136 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4137 | const AttributionSourceState& clientAttribution, int callingPid, bool nativeClient, |
| 4138 | const std::string& cameraIdStr, int cameraFacing, int sensorOrientation, int servicePid, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 4139 | int rotationOverride, bool sharedMode) |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4140 | : AttributionAndPermissionUtilsEncapsulator(attributionAndPermissionUtils), |
| 4141 | mDestructionStarted(false), |
| 4142 | mCameraIdStr(cameraIdStr), |
| 4143 | mCameraFacing(cameraFacing), |
| 4144 | mOrientation(sensorOrientation), |
| 4145 | mClientAttribution(clientAttribution), |
| 4146 | mCallingPid(callingPid), |
| 4147 | mSystemNativeClient(nativeClient), |
| 4148 | mServicePid(servicePid), |
| 4149 | mDisconnected(false), |
| 4150 | mUidIsTrusted(false), |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 4151 | mRotationOverride(rotationOverride), mSharedMode(sharedMode), |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4152 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), |
| 4153 | mRemoteBinder(remoteCallback), |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4154 | mCameraOpen(false), |
| 4155 | mCameraStreaming(false) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4156 | if (sCameraService == nullptr) { |
| 4157 | sCameraService = cameraService; |
| 4158 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 4159 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4160 | // There are 2 scenarios in which a client won't have AppOps operations |
| 4161 | // (both scenarios : native clients) |
| 4162 | // 1) It's an system native client*, the package name will be empty |
| 4163 | // and it will return from this function in the previous if condition |
| 4164 | // (This is the same as the previously existing behavior). |
| 4165 | // 2) It is a system native client, but its package name has been |
| 4166 | // modified for debugging, however it still must not use AppOps since |
| 4167 | // the package name is not a real one. |
| 4168 | // |
| 4169 | // * system native client - native client with UID < AID_APP_START. It |
| 4170 | // doesn't exclude clients not on the system partition. |
| 4171 | if (!mSystemNativeClient) { |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4172 | mAppOpsManager = std::make_unique<AppOpsManager>(); |
| 4173 | } |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 4174 | |
Austin Borger | 3216393 | 2024-10-25 13:56:52 -0700 | [diff] [blame] | 4175 | mUidIsTrusted = isTrustedCallingUid(mClientAttribution.uid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4179 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4180 | mDestructionStarted = true; |
| 4181 | } |
| 4182 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4183 | binder::Status CameraService::BasicClient::disconnect() { |
| 4184 | binder::Status res = Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4185 | if (mDisconnected) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4186 | return res; |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 4187 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 4188 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4189 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4190 | sCameraService->removeByClient(this); |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4191 | sCameraService->logDisconnected(mCameraIdStr, mCallingPid, getPackageName()); |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 4192 | if (!flags::camera_multi_client() || !mSharedMode || (mSharedMode && |
| 4193 | sCameraService->isOnlyClient(this))) { |
| 4194 | // Remove the HAL reference for the camera in either of the following scenarios : |
| 4195 | // 1) Camera was opened in non-shared mode. |
| 4196 | // 2) Camera was opened in shared mode and this is the last client using |
| 4197 | // the camera which is being disconnected |
| 4198 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, |
| 4199 | mCameraIdStr); |
| 4200 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4201 | |
| 4202 | sp<IBinder> remote = getRemote(); |
| 4203 | if (remote != nullptr) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4204 | remote->unlinkToDeath(sCameraService); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4205 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4206 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4207 | notifyCameraClosing(); |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 4208 | if (!flags::camera_multi_client() || !mSharedMode || (mSharedMode && |
| 4209 | sCameraService->isOnlyClient(this))) { |
| 4210 | // Notify flashlight that a camera device is closed. |
| 4211 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); |
| 4212 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4213 | ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.c_str(), |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4214 | mCallingPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4215 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4216 | // client shouldn't be able to call into us anymore |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4217 | mCallingPid = 0; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4218 | |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 4219 | const auto& mActivityManager = getActivityManager(); |
| 4220 | if (mActivityManager) { |
| 4221 | mActivityManager->logFgsApiEnd(LOG_FGS_CAMERA_API, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4222 | getCallingUid(), |
| 4223 | getCallingPid()); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4226 | return res; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 4227 | } |
| 4228 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 4229 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 4230 | // No dumping of clients directly over Binder, |
| 4231 | // must go through CameraService::dump |
| 4232 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 4233 | getCallingUid(), NULL, 0); |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 4234 | return OK; |
| 4235 | } |
| 4236 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4237 | status_t CameraService::BasicClient::startWatchingTags(const std::string&, int) { |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4238 | // Can't watch tags directly, must go through CameraService::startWatchingTags |
| 4239 | return OK; |
| 4240 | } |
| 4241 | |
| 4242 | status_t CameraService::BasicClient::stopWatchingTags(int) { |
| 4243 | // Can't watch tags directly, must go through CameraService::stopWatchingTags |
| 4244 | return OK; |
| 4245 | } |
| 4246 | |
| 4247 | status_t CameraService::BasicClient::dumpWatchedEventsToVector(std::vector<std::string> &) { |
| 4248 | // Can't watch tags directly, must go through CameraService::dumpWatchedEventsToVector |
| 4249 | return OK; |
| 4250 | } |
| 4251 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4252 | std::string CameraService::BasicClient::getPackageName() const { |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4253 | return mClientAttribution.packageName.value_or(kUnknownPackageName); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4254 | } |
| 4255 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 4256 | int CameraService::BasicClient::getCameraFacing() const { |
| 4257 | return mCameraFacing; |
| 4258 | } |
| 4259 | |
| 4260 | int CameraService::BasicClient::getCameraOrientation() const { |
| 4261 | return mOrientation; |
| 4262 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4263 | |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4264 | int CameraService::BasicClient::getClientCallingPid() const { |
| 4265 | return mCallingPid; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4266 | } |
| 4267 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4268 | uid_t CameraService::BasicClient::getClientUid() const { |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4269 | return mClientAttribution.uid; |
| 4270 | } |
| 4271 | |
| 4272 | const std::optional<std::string>& CameraService::BasicClient::getClientAttributionTag() const { |
| 4273 | return mClientAttribution.attributionTag; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4274 | } |
| 4275 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 4276 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 4277 | // Defaults to API2. |
| 4278 | return level == API_2; |
| 4279 | } |
| 4280 | |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 4281 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4282 | { |
| 4283 | Mutex::Autolock l(mAudioRestrictionLock); |
| 4284 | mAudioRestriction = mode; |
| 4285 | } |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 4286 | sCameraService->updateAudioRestriction(); |
| 4287 | return OK; |
| 4288 | } |
| 4289 | |
| 4290 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4291 | return sCameraService->updateAudioRestriction(); |
| 4292 | } |
| 4293 | |
| 4294 | int32_t CameraService::BasicClient::getAudioRestriction() const { |
| 4295 | Mutex::Autolock l(mAudioRestrictionLock); |
| 4296 | return mAudioRestriction; |
| 4297 | } |
| 4298 | |
| 4299 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { |
| 4300 | switch (mode) { |
| 4301 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: |
| 4302 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: |
| 4303 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: |
| 4304 | return true; |
| 4305 | default: |
| 4306 | return false; |
| 4307 | } |
| 4308 | } |
| 4309 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4310 | status_t CameraService::BasicClient::handlePermissionResult( |
| 4311 | PermissionChecker::PermissionResult result) { |
| 4312 | if (result == PermissionChecker::PERMISSION_HARD_DENIED) { |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4313 | ALOGI("Camera %s: Access for \"%s\" has been revoked", mCameraIdStr.c_str(), |
| 4314 | getPackageName().c_str()); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4315 | return PERMISSION_DENIED; |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4316 | } else if (!mUidIsTrusted && result == PermissionChecker::PERMISSION_SOFT_DENIED) { |
| 4317 | // If the calling Uid is trusted (a native service), the AppOpsManager/PermissionChecker |
| 4318 | // could return MODE_IGNORED/PERMISSION_SOFT_DENIED. Do not treat such case as error. |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4319 | bool isUidActive = |
| 4320 | sCameraService->mUidPolicy->isUidActive(getClientUid(), getPackageName()); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4321 | |
| 4322 | bool isCameraPrivacyEnabled; |
| 4323 | if (flags::camera_privacy_allowlist()) { |
| 4324 | isCameraPrivacyEnabled = sCameraService->isCameraPrivacyEnabled( |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4325 | toString16(getPackageName()), std::string(), mCallingPid, getClientUid()); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4326 | } else { |
| 4327 | isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4328 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4329 | } |
Jyoti Bhayana | 8143e57 | 2023-01-09 08:46:49 -0800 | [diff] [blame] | 4330 | // We don't want to return EACCESS if the CameraPrivacy is enabled. |
| 4331 | // We prefer to successfully open the camera and perform camera muting |
| 4332 | // or blocking in connectHelper as handleAppOpMode can be called before the |
| 4333 | // connection has been fully established and at that time camera muting |
| 4334 | // capabilities are unknown. |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4335 | if (!isUidActive || !isCameraPrivacyEnabled) { |
Austin Borger | fd05d98 | 2024-04-22 15:54:50 -0700 | [diff] [blame] | 4336 | ALOGI("Camera %s: Access for \"%s\" has been restricted." |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4337 | "uid active: %s, privacy enabled: %s", |
| 4338 | mCameraIdStr.c_str(), getPackageName().c_str(), isUidActive ? "true" : "false", |
| 4339 | isCameraPrivacyEnabled ? "true" : "false"); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4340 | // Return the same error as for device policy manager rejection |
| 4341 | return -EACCES; |
| 4342 | } |
| 4343 | } |
| 4344 | return OK; |
| 4345 | } |
| 4346 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4347 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { |
| 4348 | return handlePermissionResult(appOpModeToPermissionResult(mode)); |
| 4349 | } |
| 4350 | |
| 4351 | status_t CameraService::BasicClient::notifyCameraOpening() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4352 | ATRACE_CALL(); |
| 4353 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4354 | // Don't start watching until we're streaming when using permissionChecker for data delivery |
Austin Borger | 2a3f9a7 | 2024-11-07 12:24:49 -0800 | [diff] [blame] | 4355 | if (!flags::data_delivery_permission_checks()) { |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4356 | ALOGD("%s: Start camera ops, package name = %s, client UID = %d", __FUNCTION__, |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4357 | getPackageName().c_str(), getClientUid()); |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4358 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4359 | if (mAppOpsManager != nullptr) { |
| 4360 | // Notify app ops that the camera is not available |
| 4361 | mOpsCallback = new OpsCallback(this); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 4362 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4363 | mAppOpsManager->startWatchingMode( |
| 4364 | AppOpsManager::OP_CAMERA, toString16(getPackageName()), |
| 4365 | AppOpsManager::WATCH_FOREGROUND_CHANGES, mOpsCallback); |
| 4366 | |
| 4367 | // Just check for camera access here on open - delay startOp until |
| 4368 | // camera frames start streaming in startCameraStreamingOps |
| 4369 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, getClientUid(), |
| 4370 | toString16(getPackageName())); |
| 4371 | status_t res = handleAppOpMode(mode); |
| 4372 | if (res != OK) { |
| 4373 | return res; |
| 4374 | } |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4375 | } |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4376 | } else { |
Austin Borger | 2a3f9a7 | 2024-11-07 12:24:49 -0800 | [diff] [blame] | 4377 | // TODO: Remove when removing the data_delivery_permission_checks flag |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4378 | ALOGD("%s: Bypassing checkOp for uid %d", __FUNCTION__, getClientUid()); |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 4379 | } |
| 4380 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4381 | mCameraOpen = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4382 | |
| 4383 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 4384 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4385 | |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4386 | sCameraService->mUidPolicy->registerMonitorUid(getClientUid(), /*openCamera*/ true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4387 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4388 | // Notify listeners of camera open/close status |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 4389 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true /*open*/, getPackageName(), |
| 4390 | mSharedMode); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4391 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4392 | return OK; |
| 4393 | } |
| 4394 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4395 | status_t CameraService::BasicClient::startCameraStreamingOps() { |
| 4396 | ATRACE_CALL(); |
| 4397 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4398 | if (!mCameraOpen) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4399 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 4400 | return INVALID_OPERATION; |
| 4401 | } |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4402 | |
| 4403 | if (mCameraStreaming) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4404 | ALOGV("%s: Streaming already active!", __FUNCTION__); |
| 4405 | return OK; |
| 4406 | } |
| 4407 | |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4408 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", __FUNCTION__, |
| 4409 | getPackageName().c_str(), getClientUid()); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4410 | |
| 4411 | if (mAppOpsManager != nullptr) { |
Austin Borger | 2a3f9a7 | 2024-11-07 12:24:49 -0800 | [diff] [blame] | 4412 | if (flags::data_delivery_permission_checks()) { |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4413 | ALOGD("%s: Start data delivery for uid %d", __FUNCTION__, getClientUid()); |
| 4414 | |
| 4415 | const PermissionChecker::PermissionResult result = |
| 4416 | checkPermissionsForCameraForStartDataDelivery(mCameraIdStr, mClientAttribution); |
| 4417 | status_t res = handlePermissionResult(result); |
| 4418 | if (res != OK) { |
| 4419 | return res; |
| 4420 | } |
| 4421 | |
| 4422 | mOpsCallback = new OpsCallback(this); |
| 4423 | std::for_each(AttrSourceItr{mClientAttribution}, AttrSourceItr::end(), |
| 4424 | [&](const auto& attr) { |
| 4425 | mAppOpsManager->startWatchingMode( |
| 4426 | AppOpsManager::OP_CAMERA, |
| 4427 | toString16(attr.packageName.value_or("")), |
| 4428 | AppOpsManager::WATCH_FOREGROUND_CHANGES, mOpsCallback); |
| 4429 | }); |
| 4430 | } else { |
| 4431 | ALOGD("%s: startOp for uid %d", __FUNCTION__, getClientUid()); |
| 4432 | int32_t mode = mAppOpsManager->startOpNoThrow( |
| 4433 | AppOpsManager::OP_CAMERA, getClientUid(), toString16(getPackageName()), |
| 4434 | /*startIfModeDefault*/ false, toString16(getClientAttributionTag()), |
| 4435 | toString16("start camera ") + toString16(mCameraIdStr)); |
| 4436 | status_t res = handleAppOpMode(mode); |
| 4437 | if (res != OK) { |
| 4438 | return res; |
| 4439 | } |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4440 | } |
| 4441 | } |
| 4442 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4443 | mCameraStreaming = true; |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4444 | |
| 4445 | return OK; |
| 4446 | } |
| 4447 | |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4448 | status_t CameraService::BasicClient::noteAppOp() { |
| 4449 | ATRACE_CALL(); |
| 4450 | |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4451 | ALOGV("%s: Start camera noteAppOp, package name = %s, client UID = %d", __FUNCTION__, |
| 4452 | getPackageName().c_str(), getClientUid()); |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4453 | |
| 4454 | // noteAppOp is only used for when camera mute is not supported, in order |
| 4455 | // to trigger the sensor privacy "Unblock" dialog |
Austin Borger | 2a3f9a7 | 2024-11-07 12:24:49 -0800 | [diff] [blame] | 4456 | if (flags::data_delivery_permission_checks()) { |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4457 | // Ignore the result, since we're only triggering the dialog |
| 4458 | ALOGD("%s: Check data delivery permissions for uid %d", __FUNCTION__, getClientUid()); |
| 4459 | hasPermissionsForCameraForDataDelivery(std::string(), mClientAttribution); |
| 4460 | } else if (mAppOpsManager != nullptr) { |
| 4461 | ALOGD("%s: noteOp for uid %d", __FUNCTION__, getClientUid()); |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4462 | int32_t mode = mAppOpsManager->noteOp( |
| 4463 | AppOpsManager::OP_CAMERA, getClientUid(), toString16(getPackageName()), |
| 4464 | toString16(getClientAttributionTag()), |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4465 | toString16("start camera ") + toString16(mCameraIdStr)); |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4466 | status_t res = handleAppOpMode(mode); |
| 4467 | if (res != OK) { |
| 4468 | return res; |
| 4469 | } |
| 4470 | } |
| 4471 | |
| 4472 | return OK; |
| 4473 | } |
| 4474 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4475 | status_t CameraService::BasicClient::finishCameraStreamingOps() { |
| 4476 | ATRACE_CALL(); |
| 4477 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4478 | if (!mCameraOpen) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4479 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 4480 | return INVALID_OPERATION; |
| 4481 | } |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4482 | if (!mCameraStreaming) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4483 | ALOGV("%s: Streaming not active!", __FUNCTION__); |
| 4484 | return OK; |
| 4485 | } |
| 4486 | |
| 4487 | if (mAppOpsManager != nullptr) { |
Austin Borger | 2a3f9a7 | 2024-11-07 12:24:49 -0800 | [diff] [blame] | 4488 | if (flags::data_delivery_permission_checks()) { |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4489 | ALOGD("%s: finishDataDelivery for uid %d", __FUNCTION__, getClientUid()); |
| 4490 | finishDataDelivery(mClientAttribution); |
| 4491 | |
| 4492 | // Stop watching app op changes after stop streaming |
| 4493 | if (mOpsCallback != nullptr) { |
| 4494 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
| 4495 | mOpsCallback.clear(); |
| 4496 | } |
| 4497 | } else { |
| 4498 | ALOGD("%s: finishOp for uid %d", __FUNCTION__, getClientUid()); |
| 4499 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, getClientUid(), |
| 4500 | toString16(getPackageName()), |
| 4501 | toString16(getClientAttributionTag())); |
| 4502 | } |
| 4503 | mCameraStreaming = false; |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4504 | } |
| 4505 | |
| 4506 | return OK; |
| 4507 | } |
| 4508 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4509 | status_t CameraService::BasicClient::notifyCameraClosing() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4510 | ATRACE_CALL(); |
| 4511 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4512 | if (mCameraStreaming) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4513 | // Make sure we've notified everyone about camera stopping |
| 4514 | finishCameraStreamingOps(); |
| 4515 | } |
| 4516 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4517 | // Check if notifyCameraOpening succeeded, and if so, finish the camera op if necessary |
| 4518 | if (mCameraOpen) { |
| 4519 | mCameraOpen = false; |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 4520 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 4521 | // This function is called when a client disconnects. This should |
| 4522 | // release the camera, but actually only if it was in a proper |
| 4523 | // functional state, i.e. with status NOT_AVAILABLE |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4524 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 4525 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 4526 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4527 | // Transition to PRESENT if the camera is not in either of the rejected states |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 4528 | if (!flags::camera_multi_client() || !mSharedMode || (mSharedMode |
| 4529 | && sCameraService->isOnlyClient(this))) { |
| 4530 | sCameraService->updateStatus(StatusInternal::PRESENT, |
| 4531 | mCameraIdStr, rejected); |
| 4532 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4533 | } |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4534 | |
| 4535 | // When using the data delivery permission checks, the open state does not involve AppOps |
Austin Borger | 2a3f9a7 | 2024-11-07 12:24:49 -0800 | [diff] [blame] | 4536 | if (!flags::data_delivery_permission_checks()) { |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4537 | // Always stop watching, even if no camera op is active |
| 4538 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 4539 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
| 4540 | } |
| 4541 | mOpsCallback.clear(); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 4542 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4543 | |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4544 | sCameraService->mUidPolicy->unregisterMonitorUid(getClientUid(), /*closeCamera*/ true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4545 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4546 | // Notify listeners of camera open/close status |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 4547 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false /*open*/, getPackageName(), |
| 4548 | mSharedMode); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4549 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4550 | return OK; |
| 4551 | } |
| 4552 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4553 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4554 | ATRACE_CALL(); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 4555 | if (mAppOpsManager == nullptr) { |
| 4556 | return; |
| 4557 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 4558 | // TODO : add offline camera session case |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4559 | if (op != AppOpsManager::OP_CAMERA) { |
| 4560 | ALOGW("Unexpected app ops notification received: %d", op); |
| 4561 | return; |
| 4562 | } |
| 4563 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4564 | PermissionChecker::PermissionResult res; |
Austin Borger | 2a3f9a7 | 2024-11-07 12:24:49 -0800 | [diff] [blame] | 4565 | if (flags::data_delivery_permission_checks()) { |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4566 | int32_t appOpMode = AppOpsManager::MODE_ALLOWED; |
| 4567 | std::for_each(AttrSourceItr{mClientAttribution}, AttrSourceItr::end(), |
| 4568 | [&](const auto& attr) { |
| 4569 | appOpMode = std::max(appOpMode, mAppOpsManager->checkOp( |
| 4570 | AppOpsManager::OP_CAMERA, attr.uid, |
| 4571 | toString16(attr.packageName.value_or("")))); |
| 4572 | }); |
| 4573 | ALOGV("checkOp returns: %d, %s ", res, |
| 4574 | appOpMode == AppOpsManager::MODE_ALLOWED ? "ALLOWED" |
| 4575 | : appOpMode == AppOpsManager::MODE_IGNORED ? "IGNORED" |
| 4576 | : appOpMode == AppOpsManager::MODE_ERRORED ? "ERRORED" |
| 4577 | : "UNKNOWN"); |
| 4578 | res = appOpModeToPermissionResult(appOpMode); |
| 4579 | } else { |
| 4580 | int32_t appOpMode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, getClientUid(), |
| 4581 | toString16(getPackageName())); |
| 4582 | ALOGV("checkOp returns: %d, %s ", res, |
| 4583 | appOpMode == AppOpsManager::MODE_ALLOWED ? "ALLOWED" |
| 4584 | : appOpMode == AppOpsManager::MODE_IGNORED ? "IGNORED" |
| 4585 | : appOpMode == AppOpsManager::MODE_ERRORED ? "ERRORED" |
| 4586 | : "UNKNOWN"); |
| 4587 | res = appOpModeToPermissionResult(appOpMode); |
| 4588 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4589 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4590 | if (res == PermissionChecker::PERMISSION_HARD_DENIED) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4591 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.c_str(), |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4592 | getPackageName().c_str()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4593 | block(); |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4594 | } else if (res == PermissionChecker::PERMISSION_SOFT_DENIED) { |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4595 | bool isUidActive = |
| 4596 | sCameraService->mUidPolicy->isUidActive(getClientUid(), getPackageName()); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4597 | |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4598 | // Uid may be active, but not visible to the user (e.g. PROCESS_STATE_FOREGROUND_SERVICE). |
| 4599 | // If not visible, but still active, then we want to block instead of muting the camera. |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4600 | int32_t procState = ActivityManager::PROCESS_STATE_NONEXISTENT; |
Austin Borger | 2a3f9a7 | 2024-11-07 12:24:49 -0800 | [diff] [blame] | 4601 | if (flags::data_delivery_permission_checks()) { |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4602 | // Use the proc state of the last uid in the chain (ultimately receiving the data) |
| 4603 | // when determining whether to mute or block |
| 4604 | int32_t uid = -1; |
| 4605 | std::for_each(AttrSourceItr{mClientAttribution}, AttrSourceItr::end(), |
| 4606 | [&](const auto& attr) { |
| 4607 | uid = static_cast<uid_t>(attr.uid); |
| 4608 | }); |
| 4609 | const auto& activityManager = getActivityManager(); |
| 4610 | if (activityManager != nullptr) { |
| 4611 | procState = activityManager->getUidProcessState(uid, toString16(kServiceName)); |
| 4612 | } else { |
| 4613 | ALOGD("%s: getActivityManager returned nullptr.", __FUNCTION__); |
| 4614 | } |
| 4615 | } else { |
| 4616 | procState = sCameraService->mUidPolicy->getProcState(getClientUid()); |
| 4617 | } |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4618 | bool isUidVisible = (procState <= ActivityManager::PROCESS_STATE_BOUND_TOP); |
| 4619 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4620 | bool isCameraPrivacyEnabled; |
| 4621 | if (flags::camera_privacy_allowlist()) { |
| 4622 | isCameraPrivacyEnabled = sCameraService->isCameraPrivacyEnabled( |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4623 | toString16(getPackageName()), std::string(), mCallingPid, getClientUid()); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4624 | } else { |
| 4625 | isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4626 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | ALOGI("Camera %s: Access for \"%s\" has been restricted, isUidTrusted %d, isUidActive %d" |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4630 | " isUidVisible %d, isCameraPrivacyEnabled %d procState %d", |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4631 | mCameraIdStr.c_str(), getPackageName().c_str(), mUidIsTrusted, isUidActive, |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4632 | isUidVisible, isCameraPrivacyEnabled, procState); |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4633 | // If the calling Uid is trusted (a native service), or the client Uid is active / visible |
| 4634 | // (WAR for b/175320666)the AppOpsManager could return MODE_IGNORED. Do not treat such |
| 4635 | // cases as error. |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4636 | if (!mUidIsTrusted) { |
Austin Borger | 5755d9b | 2024-09-09 11:15:44 -0700 | [diff] [blame] | 4637 | if (isUidVisible && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 4638 | setCameraMute(true); |
Austin Borger | ca1e006 | 2023-06-28 11:32:55 -0700 | [diff] [blame] | 4639 | } else { |
Austin Borger | 5755d9b | 2024-09-09 11:15:44 -0700 | [diff] [blame] | 4640 | block(); |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 4641 | } |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 4642 | } |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 4643 | } else if (res == PermissionChecker::PERMISSION_GRANTED) { |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4644 | setCameraMute(sCameraService->mOverrideCameraMuteMode); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4645 | } |
| 4646 | } |
| 4647 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4648 | void CameraService::BasicClient::block() { |
| 4649 | ATRACE_CALL(); |
| 4650 | |
| 4651 | // Reset the client PID to allow server-initiated disconnect, |
| 4652 | // and to prevent further calls by client. |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 4653 | mCallingPid = getCallingPid(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4654 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 4655 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); |
| 4656 | disconnect(); |
| 4657 | } |
| 4658 | |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 4659 | status_t CameraService::BasicClient::isPrimaryClient(bool* isPrimary) { |
| 4660 | ATRACE_CALL(); |
| 4661 | if (!flags::camera_multi_client()) { |
| 4662 | return INVALID_OPERATION; |
| 4663 | } |
| 4664 | |
| 4665 | if (!mSharedMode) { |
| 4666 | ALOGW("%s: Invalid operation when camera is not opened in shared mode", __FUNCTION__); |
| 4667 | return INVALID_OPERATION; |
| 4668 | } |
| 4669 | *isPrimary = mIsPrimaryClient; |
| 4670 | return OK; |
| 4671 | } |
| 4672 | |
| 4673 | status_t CameraService::BasicClient::setPrimaryClient(bool isPrimary) { |
| 4674 | ATRACE_CALL(); |
| 4675 | |
| 4676 | if (!flags::camera_multi_client()) { |
| 4677 | return INVALID_OPERATION; |
| 4678 | } |
| 4679 | |
| 4680 | if (!mSharedMode) { |
| 4681 | ALOGW("%s:Invalid operation when camera is not opened in shared mode", __FUNCTION__); |
| 4682 | return INVALID_OPERATION; |
| 4683 | } |
| 4684 | mIsPrimaryClient = isPrimary; |
| 4685 | return OK; |
| 4686 | } |
| 4687 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4688 | // ---------------------------------------------------------------------------- |
| 4689 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4690 | void CameraService::Client::notifyError(int32_t errorCode, |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 4691 | [[maybe_unused]] const CaptureResultExtras& resultExtras) { |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 4692 | if (mRemoteCallback != NULL) { |
Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 4693 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; |
| 4694 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { |
| 4695 | api1ErrorCode = CAMERA_ERROR_DISABLED; |
| 4696 | } |
| 4697 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 4698 | } else { |
| 4699 | ALOGE("mRemoteCallback is NULL!!"); |
| 4700 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4701 | } |
| 4702 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 4703 | // NOTE: function is idempotent |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4704 | binder::Status CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 4705 | ALOGV("Client::disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4706 | return BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 4707 | } |
| 4708 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 4709 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 4710 | return level == API_1; |
| 4711 | } |
| 4712 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4713 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 4714 | mClient(client) { |
| 4715 | } |
| 4716 | |
| 4717 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 4718 | const String16& packageName) { |
| 4719 | sp<BasicClient> client = mClient.promote(); |
| 4720 | if (client != NULL) { |
| 4721 | client->opChanged(op, packageName); |
| 4722 | } |
| 4723 | } |
| 4724 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4725 | // ---------------------------------------------------------------------------- |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4726 | // UidPolicy |
| 4727 | // ---------------------------------------------------------------------------- |
| 4728 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4729 | void CameraService::UidPolicy::registerWithActivityManager() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4730 | Mutex::Autolock _l(mUidLock); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4731 | int32_t emptyUidArray[] = { }; |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4732 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4733 | if (mRegistered) return; |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 4734 | status_t res = mAm.linkToDeath(this); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4735 | mAm.registerUidObserverForUids(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4736 | | ActivityManager::UID_OBSERVER_IDLE |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4737 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE |
| 4738 | | ActivityManager::UID_OBSERVER_PROC_OOM_ADJ, |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4739 | ActivityManager::PROCESS_STATE_UNKNOWN, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4740 | toString16(kServiceName), emptyUidArray, 0, mObserverToken); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4741 | if (res == OK) { |
| 4742 | mRegistered = true; |
| 4743 | ALOGV("UidPolicy: Registered with ActivityManager"); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4744 | } else { |
| 4745 | ALOGE("UidPolicy: Failed to register with ActivityManager: 0x%08x", res); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4746 | } |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4747 | } |
| 4748 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4749 | void CameraService::UidPolicy::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4750 | if (name != toString16(kActivityServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4751 | return; |
| 4752 | } |
| 4753 | |
| 4754 | registerWithActivityManager(); |
| 4755 | } |
| 4756 | |
| 4757 | void CameraService::UidPolicy::registerSelf() { |
| 4758 | // Use check service to see if the activity service is available |
| 4759 | // If not available then register for notifications, instead of blocking |
| 4760 | // till the service is ready |
| 4761 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4762 | sp<IBinder> binder = sm->checkService(toString16(kActivityServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4763 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4764 | sm->registerForNotifications(toString16(kActivityServiceName), this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4765 | } else { |
| 4766 | registerWithActivityManager(); |
| 4767 | } |
| 4768 | } |
| 4769 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4770 | void CameraService::UidPolicy::unregisterSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4771 | Mutex::Autolock _l(mUidLock); |
| 4772 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 4773 | mAm.unregisterUidObserver(this); |
| 4774 | mAm.unlinkToDeath(this); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4775 | mRegistered = false; |
| 4776 | mActiveUids.clear(); |
| 4777 | ALOGV("UidPolicy: Unregistered with ActivityManager"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { |
| 4781 | onUidIdle(uid, disabled); |
| 4782 | } |
| 4783 | |
| 4784 | void CameraService::UidPolicy::onUidActive(uid_t uid) { |
| 4785 | Mutex::Autolock _l(mUidLock); |
| 4786 | mActiveUids.insert(uid); |
| 4787 | } |
| 4788 | |
| 4789 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { |
| 4790 | bool deleted = false; |
| 4791 | { |
| 4792 | Mutex::Autolock _l(mUidLock); |
| 4793 | if (mActiveUids.erase(uid) > 0) { |
| 4794 | deleted = true; |
| 4795 | } |
| 4796 | } |
| 4797 | if (deleted) { |
| 4798 | sp<CameraService> service = mService.promote(); |
| 4799 | if (service != nullptr) { |
| 4800 | service->blockClientsForUid(uid); |
| 4801 | } |
| 4802 | } |
| 4803 | } |
| 4804 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4805 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 4806 | int64_t procStateSeq __unused, int32_t capability __unused) { |
Austin Borger | c5585dc | 2022-05-12 00:48:17 +0000 | [diff] [blame] | 4807 | bool procStateChange = false; |
| 4808 | { |
| 4809 | Mutex::Autolock _l(mUidLock); |
| 4810 | if (mMonitoredUids.find(uid) != mMonitoredUids.end() && |
| 4811 | mMonitoredUids[uid].procState != procState) { |
| 4812 | mMonitoredUids[uid].procState = procState; |
| 4813 | procStateChange = true; |
| 4814 | } |
| 4815 | } |
| 4816 | |
| 4817 | if (procStateChange) { |
| 4818 | sp<CameraService> service = mService.promote(); |
| 4819 | if (service != nullptr) { |
| 4820 | service->notifyMonitoredUids(); |
| 4821 | } |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4822 | } |
| 4823 | } |
| 4824 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4825 | /** |
| 4826 | * When the OOM adj of the uid owning the camera changes, a different uid waiting on camera |
| 4827 | * privileges may take precedence if the owner's new OOM adj is greater than the waiting package. |
| 4828 | * Here, we track which monitoredUid has the camera, and track its adj relative to other |
| 4829 | * monitoredUids. If it is revised above some other monitoredUid, signal |
| 4830 | * onCameraAccessPrioritiesChanged. This only needs to capture the case where there are two |
| 4831 | * foreground apps in split screen - state changes will capture all other cases. |
| 4832 | */ |
| 4833 | void CameraService::UidPolicy::onUidProcAdjChanged(uid_t uid, int32_t adj) { |
| 4834 | std::unordered_set<uid_t> notifyUidSet; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4835 | { |
| 4836 | Mutex::Autolock _l(mUidLock); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4837 | auto it = mMonitoredUids.find(uid); |
| 4838 | |
| 4839 | if (it != mMonitoredUids.end()) { |
| 4840 | if (it->second.hasCamera) { |
| 4841 | for (auto &monitoredUid : mMonitoredUids) { |
| 4842 | if (monitoredUid.first != uid && adj > monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4843 | ALOGV("%s: notify uid %d", __FUNCTION__, monitoredUid.first); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4844 | notifyUidSet.emplace(monitoredUid.first); |
| 4845 | } |
| 4846 | } |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4847 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4848 | notifyUidSet.emplace(uid); |
| 4849 | } else { |
| 4850 | for (auto &monitoredUid : mMonitoredUids) { |
| 4851 | if (monitoredUid.second.hasCamera && adj < monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4852 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4853 | notifyUidSet.emplace(uid); |
| 4854 | } |
| 4855 | } |
| 4856 | } |
| 4857 | it->second.procAdj = adj; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4858 | } |
| 4859 | } |
| 4860 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4861 | if (notifyUidSet.size() > 0) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4862 | sp<CameraService> service = mService.promote(); |
| 4863 | if (service != nullptr) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4864 | service->notifyMonitoredUids(notifyUidSet); |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4865 | } |
| 4866 | } |
| 4867 | } |
| 4868 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4869 | /** |
| 4870 | * Register a uid for monitoring, and note whether it owns a camera. |
| 4871 | */ |
| 4872 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid, bool openCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4873 | Mutex::Autolock _l(mUidLock); |
| 4874 | auto it = mMonitoredUids.find(uid); |
| 4875 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4876 | it->second.refCount++; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4877 | } else { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4878 | MonitoredUid monitoredUid; |
| 4879 | monitoredUid.procState = ActivityManager::PROCESS_STATE_NONEXISTENT; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4880 | monitoredUid.procAdj = resource_policy::UNKNOWN_ADJ; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4881 | monitoredUid.refCount = 1; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4882 | it = mMonitoredUids.emplace(std::pair<uid_t, MonitoredUid>(uid, monitoredUid)).first; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4883 | status_t res = mAm.addUidToObserver(mObserverToken, toString16(kServiceName), uid); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4884 | if (res != OK) { |
| 4885 | ALOGE("UidPolicy: Failed to add uid to observer: 0x%08x", res); |
| 4886 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | if (openCamera) { |
| 4890 | it->second.hasCamera = true; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4891 | } |
| 4892 | } |
| 4893 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4894 | /** |
| 4895 | * Unregister a uid for monitoring, and note whether it lost ownership of a camera. |
| 4896 | */ |
| 4897 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid, bool closeCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4898 | Mutex::Autolock _l(mUidLock); |
| 4899 | auto it = mMonitoredUids.find(uid); |
| 4900 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4901 | it->second.refCount--; |
| 4902 | if (it->second.refCount == 0) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4903 | mMonitoredUids.erase(it); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4904 | status_t res = mAm.removeUidFromObserver(mObserverToken, toString16(kServiceName), uid); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4905 | if (res != OK) { |
| 4906 | ALOGE("UidPolicy: Failed to remove uid from observer: 0x%08x", res); |
| 4907 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4908 | } else if (closeCamera) { |
| 4909 | it->second.hasCamera = false; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4910 | } |
| 4911 | } else { |
| 4912 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 4913 | } |
| 4914 | } |
| 4915 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4916 | bool CameraService::UidPolicy::isUidActive(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4917 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4918 | return isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4919 | } |
| 4920 | |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4921 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; |
| 4922 | static const int64_t kPollUidActiveTimeoutMillis = 50; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4923 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4924 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4925 | // Non-app UIDs are considered always active |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4926 | // If activity manager is unreachable, assume everything is active |
| 4927 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4928 | return true; |
| 4929 | } |
| 4930 | auto it = mOverrideUids.find(uid); |
| 4931 | if (it != mOverrideUids.end()) { |
| 4932 | return it->second; |
| 4933 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4934 | bool active = mActiveUids.find(uid) != mActiveUids.end(); |
| 4935 | if (!active) { |
| 4936 | // We want active UIDs to always access camera with their first attempt since |
| 4937 | // there is no guarantee the app is robustly written and would retry getting |
| 4938 | // the camera on failure. The inverse case is not a problem as we would take |
| 4939 | // camera away soon once we get the callback that the uid is no longer active. |
| 4940 | ActivityManager am; |
| 4941 | // Okay to access with a lock held as UID changes are dispatched without |
| 4942 | // a lock and we are a higher level component. |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4943 | int64_t startTimeMillis = 0; |
| 4944 | do { |
| 4945 | // TODO: Fix this b/109950150! |
| 4946 | // Okay this is a hack. There is a race between the UID turning active and |
| 4947 | // activity being resumed. The proper fix is very risky, so we temporary add |
| 4948 | // some polling which should happen pretty rarely anyway as the race is hard |
| 4949 | // to hit. |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4950 | active = mActiveUids.find(uid) != mActiveUids.end(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4951 | if (!active) active = am.isUidActive(uid, toString16(callingPackage)); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4952 | if (active) { |
| 4953 | break; |
| 4954 | } |
| 4955 | if (startTimeMillis <= 0) { |
| 4956 | startTimeMillis = uptimeMillis(); |
| 4957 | } |
| 4958 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4959 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4960 | if (remainingTimeMillis <= 0) { |
| 4961 | break; |
| 4962 | } |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4963 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); |
| 4964 | |
| 4965 | mUidLock.unlock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4966 | usleep(remainingTimeMillis * 1000); |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4967 | mUidLock.lock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4968 | } while (true); |
| 4969 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4970 | if (active) { |
| 4971 | // Now that we found out the UID is actually active, cache that |
| 4972 | mActiveUids.insert(uid); |
| 4973 | } |
| 4974 | } |
| 4975 | return active; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4976 | } |
| 4977 | |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4978 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { |
| 4979 | Mutex::Autolock _l(mUidLock); |
| 4980 | return getProcStateLocked(uid); |
| 4981 | } |
| 4982 | |
| 4983 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { |
| 4984 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 4985 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4986 | procState = mMonitoredUids[uid].procState; |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4987 | } |
| 4988 | return procState; |
| 4989 | } |
| 4990 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4991 | void CameraService::UidPolicy::addOverrideUid(uid_t uid, |
| 4992 | const std::string &callingPackage, bool active) { |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4993 | updateOverrideUid(uid, callingPackage, active, true); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4994 | } |
| 4995 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 4996 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, const std::string &callingPackage) { |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4997 | updateOverrideUid(uid, callingPackage, false, false); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4998 | } |
| 4999 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 5000 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 5001 | Mutex::Autolock _l(mUidLock); |
| 5002 | ALOGV("UidPolicy: ActivityManager has died"); |
| 5003 | mRegistered = false; |
| 5004 | mActiveUids.clear(); |
| 5005 | } |
| 5006 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5007 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, const std::string &callingPackage, |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 5008 | bool active, bool insert) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5009 | bool wasActive = false; |
| 5010 | bool isActive = false; |
| 5011 | { |
| 5012 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 5013 | wasActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5014 | mOverrideUids.erase(uid); |
| 5015 | if (insert) { |
| 5016 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 5017 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 5018 | isActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5019 | } |
| 5020 | if (wasActive != isActive && !isActive) { |
| 5021 | sp<CameraService> service = mService.promote(); |
| 5022 | if (service != nullptr) { |
| 5023 | service->blockClientsForUid(uid); |
| 5024 | } |
| 5025 | } |
| 5026 | } |
| 5027 | |
| 5028 | // ---------------------------------------------------------------------------- |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5029 | // SensorPrivacyPolicy |
| 5030 | // ---------------------------------------------------------------------------- |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5031 | |
| 5032 | void CameraService::SensorPrivacyPolicy::registerWithSensorPrivacyManager() |
| 5033 | { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5034 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5035 | if (mRegistered) { |
| 5036 | return; |
| 5037 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5038 | hasCameraPrivacyFeature(); // Called so the result is cached |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5039 | mSpm.addSensorPrivacyListener(this); |
Charles Chen | f075f08 | 2024-03-04 23:32:55 +0000 | [diff] [blame] | 5040 | if (isAutomotiveDevice()) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5041 | mSpm.addToggleSensorPrivacyListener(this); |
| 5042 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5043 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5044 | if (flags::camera_privacy_allowlist()) { |
| 5045 | mCameraPrivacyState = mSpm.getToggleSensorPrivacyState( |
| 5046 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, |
| 5047 | SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 5048 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5049 | status_t res = mSpm.linkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5050 | if (res == OK) { |
| 5051 | mRegistered = true; |
| 5052 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); |
| 5053 | } |
| 5054 | } |
| 5055 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5056 | void CameraService::SensorPrivacyPolicy::onServiceRegistration(const String16& name, |
| 5057 | const sp<IBinder>&) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5058 | if (name != toString16(kSensorPrivacyServiceName)) { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5059 | return; |
| 5060 | } |
| 5061 | |
| 5062 | registerWithSensorPrivacyManager(); |
| 5063 | } |
| 5064 | |
| 5065 | void CameraService::SensorPrivacyPolicy::registerSelf() { |
| 5066 | // Use checkservice to see if the sensor_privacy service is available |
| 5067 | // If service is not available then register for notification |
| 5068 | sp<IServiceManager> sm = defaultServiceManager(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5069 | sp<IBinder> binder = sm->checkService(toString16(kSensorPrivacyServiceName)); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5070 | if (!binder) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5071 | sm->registerForNotifications(toString16(kSensorPrivacyServiceName),this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5072 | } else { |
| 5073 | registerWithSensorPrivacyManager(); |
| 5074 | } |
| 5075 | } |
| 5076 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5077 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { |
| 5078 | Mutex::Autolock _l(mSensorPrivacyLock); |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5079 | mSpm.removeSensorPrivacyListener(this); |
Charles Chen | f075f08 | 2024-03-04 23:32:55 +0000 | [diff] [blame] | 5080 | if (isAutomotiveDevice()) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5081 | mSpm.removeToggleSensorPrivacyListener(this); |
| 5082 | } |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 5083 | mSpm.unlinkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5084 | mRegistered = false; |
| 5085 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); |
| 5086 | } |
| 5087 | |
| 5088 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 5089 | if (!mRegistered) { |
| 5090 | registerWithSensorPrivacyManager(); |
| 5091 | } |
| 5092 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5093 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5094 | return mSensorPrivacyEnabled; |
| 5095 | } |
| 5096 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5097 | int CameraService::SensorPrivacyPolicy::getCameraPrivacyState() { |
| 5098 | if (!mRegistered) { |
| 5099 | registerWithSensorPrivacyManager(); |
| 5100 | } |
| 5101 | |
| 5102 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5103 | return mCameraPrivacyState; |
| 5104 | } |
| 5105 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5106 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled() { |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5107 | if (!hasCameraPrivacyFeature()) { |
| 5108 | return false; |
| 5109 | } |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5110 | return mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5111 | } |
| 5112 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5113 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled(const String16& packageName) { |
| 5114 | if (!hasCameraPrivacyFeature()) { |
Jyoti Bhayana | 5882b03 | 2024-04-26 11:18:58 -0700 | [diff] [blame] | 5115 | return false; |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5116 | } |
| 5117 | return mSpm.isCameraPrivacyEnabled(packageName); |
| 5118 | } |
| 5119 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5120 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged( |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5121 | int toggleType, int sensor, bool enabled) { |
| 5122 | if ((toggleType == SensorPrivacyManager::TOGGLE_TYPE_UNKNOWN) |
| 5123 | && (sensor == SensorPrivacyManager::TOGGLE_SENSOR_UNKNOWN)) { |
| 5124 | { |
| 5125 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5126 | mSensorPrivacyEnabled = enabled; |
| 5127 | } |
| 5128 | // if sensor privacy is enabled then block all clients from accessing the camera |
| 5129 | if (enabled) { |
| 5130 | sp<CameraService> service = mService.promote(); |
| 5131 | if (service != nullptr) { |
| 5132 | service->blockAllClients(); |
| 5133 | } |
| 5134 | } |
| 5135 | } |
| 5136 | return binder::Status::ok(); |
| 5137 | } |
| 5138 | |
| 5139 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyStateChanged( |
| 5140 | int, int sensor, int state) { |
| 5141 | if (!flags::camera_privacy_allowlist() |
| 5142 | || (sensor != SensorPrivacyManager::TOGGLE_SENSOR_CAMERA)) { |
| 5143 | return binder::Status::ok(); |
| 5144 | } |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5145 | { |
| 5146 | Mutex::Autolock _l(mSensorPrivacyLock); |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5147 | mCameraPrivacyState = state; |
| 5148 | } |
| 5149 | sp<CameraService> service = mService.promote(); |
| 5150 | if (!service) { |
| 5151 | return binder::Status::ok(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5152 | } |
| 5153 | // 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] | 5154 | if (state == SensorPrivacyManager::ENABLED) { |
| 5155 | service->blockAllClients(); |
Jyoti Bhayana | 54a4b00 | 2024-02-27 15:36:09 -0800 | [diff] [blame] | 5156 | } else if (state == SensorPrivacyManager::ENABLED_EXCEPT_ALLOWLISTED_APPS) { |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 5157 | service->blockPrivacyEnabledClients(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5158 | } |
| 5159 | return binder::Status::ok(); |
| 5160 | } |
| 5161 | |
| 5162 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 5163 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 5164 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); |
| 5165 | mRegistered = false; |
| 5166 | } |
| 5167 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5168 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 5169 | bool supportsSoftwareToggle = mSpm.supportsSensorToggle( |
| 5170 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 5171 | bool supportsHardwareToggle = mSpm.supportsSensorToggle( |
| 5172 | SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 5173 | return supportsSoftwareToggle || supportsHardwareToggle; |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 5174 | } |
| 5175 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5176 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5177 | // CameraState |
| 5178 | // ---------------------------------------------------------------------------- |
| 5179 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5180 | CameraService::CameraState::CameraState(const std::string& id, int cost, |
| 5181 | const std::set<std::string>& conflicting, SystemCameraKind systemCameraKind, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5182 | const std::vector<std::string>& physicalCameras) : mId(id), |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5183 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5184 | mSystemCameraKind(systemCameraKind), mPhysicalCameras(physicalCameras) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5185 | |
| 5186 | CameraService::CameraState::~CameraState() {} |
| 5187 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5188 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5189 | Mutex::Autolock lock(mStatusLock); |
| 5190 | return mStatus; |
| 5191 | } |
| 5192 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5193 | std::vector<std::string> CameraService::CameraState::getUnavailablePhysicalIds() const { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5194 | Mutex::Autolock lock(mStatusLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5195 | std::vector<std::string> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5196 | return res; |
| 5197 | } |
| 5198 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5199 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 5200 | return mShimParams; |
| 5201 | } |
| 5202 | |
| 5203 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 5204 | mShimParams = params; |
| 5205 | } |
| 5206 | |
| 5207 | int CameraService::CameraState::getCost() const { |
| 5208 | return mCost; |
| 5209 | } |
| 5210 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5211 | std::set<std::string> CameraService::CameraState::getConflicting() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5212 | return mConflicting; |
| 5213 | } |
| 5214 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5215 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { |
| 5216 | return mSystemCameraKind; |
| 5217 | } |
| 5218 | |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5219 | bool CameraService::CameraState::containsPhysicalCamera(const std::string& physicalCameraId) const { |
| 5220 | return std::find(mPhysicalCameras.begin(), mPhysicalCameras.end(), physicalCameraId) |
| 5221 | != mPhysicalCameras.end(); |
| 5222 | } |
| 5223 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5224 | bool CameraService::CameraState::addUnavailablePhysicalId(const std::string& physicalId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5225 | Mutex::Autolock lock(mStatusLock); |
| 5226 | auto result = mUnavailablePhysicalIds.insert(physicalId); |
| 5227 | return result.second; |
| 5228 | } |
| 5229 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5230 | bool CameraService::CameraState::removeUnavailablePhysicalId(const std::string& physicalId) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5231 | Mutex::Autolock lock(mStatusLock); |
| 5232 | auto count = mUnavailablePhysicalIds.erase(physicalId); |
| 5233 | return count > 0; |
| 5234 | } |
| 5235 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5236 | void CameraService::CameraState::setClientPackage(const std::string& clientPackage) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5237 | Mutex::Autolock lock(mStatusLock); |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 5238 | mClientPackages.clear(); |
| 5239 | mClientPackages.insert(clientPackage); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5240 | } |
| 5241 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5242 | std::string CameraService::CameraState::getClientPackage() const { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5243 | Mutex::Autolock lock(mStatusLock); |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 5244 | if (!mClientPackages.empty()) { |
| 5245 | std::set<std::string>::iterator it = mClientPackages.begin(); |
| 5246 | return *it; |
| 5247 | } |
| 5248 | return std::string(); |
| 5249 | } |
| 5250 | |
| 5251 | void CameraService::CameraState::addClientPackage(const std::string& clientPackage) { |
| 5252 | Mutex::Autolock lock(mStatusLock); |
| 5253 | mClientPackages.insert(clientPackage); |
| 5254 | } |
| 5255 | |
| 5256 | void CameraService::CameraState::removeClientPackage(const std::string& clientPackage) { |
| 5257 | Mutex::Autolock lock(mStatusLock); |
| 5258 | mClientPackages.erase(clientPackage); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5259 | } |
| 5260 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5261 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5262 | // ClientEventListener |
| 5263 | // ---------------------------------------------------------------------------- |
| 5264 | |
| 5265 | void CameraService::ClientEventListener::onClientAdded( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5266 | const resource_policy::ClientDescriptor<std::string, |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5267 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 5268 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5269 | if (basicClient.get() != nullptr) { |
| 5270 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5271 | notifier.noteStartCamera(toString8(descriptor.getKey()), |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5272 | static_cast<int>(basicClient->getClientUid())); |
| 5273 | } |
| 5274 | } |
| 5275 | |
| 5276 | void CameraService::ClientEventListener::onClientRemoved( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5277 | const resource_policy::ClientDescriptor<std::string, |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5278 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 5279 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5280 | if (basicClient.get() != nullptr) { |
| 5281 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5282 | notifier.noteStopCamera(toString8(descriptor.getKey()), |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5283 | static_cast<int>(basicClient->getClientUid())); |
| 5284 | } |
| 5285 | } |
| 5286 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5287 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5288 | // CameraClientManager |
| 5289 | // ---------------------------------------------------------------------------- |
| 5290 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 5291 | CameraService::CameraClientManager::CameraClientManager() { |
| 5292 | setListener(std::make_shared<ClientEventListener>()); |
| 5293 | } |
| 5294 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5295 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 5296 | |
| 5297 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5298 | const std::string& id) const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5299 | auto descriptor = get(id); |
| 5300 | if (descriptor == nullptr) { |
| 5301 | return sp<BasicClient>{nullptr}; |
| 5302 | } |
| 5303 | return descriptor->getValue(); |
| 5304 | } |
| 5305 | |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 5306 | void CameraService::CameraClientManager::remove(const CameraService::DescriptorPtr& value) { |
| 5307 | ClientManager::remove(value); |
| 5308 | if (!flags::camera_multi_client()) { |
| 5309 | return; |
| 5310 | } |
| 5311 | auto clientToRemove = value->getValue(); |
| 5312 | if ((clientToRemove.get() != nullptr) && clientToRemove->mSharedMode) { |
| 5313 | bool primaryClient = false; |
| 5314 | status_t ret = clientToRemove->isPrimaryClient(&primaryClient); |
| 5315 | if ((ret == OK) && primaryClient) { |
| 5316 | // Primary client is being removed. Find the next higher priority |
| 5317 | // client to become primary client. |
| 5318 | auto clientDescriptor = get(value->getKey()); |
| 5319 | if (clientDescriptor == nullptr) { |
| 5320 | ALOGV("CameraService::CameraClientManager::no other clients are using same camera"); |
| 5321 | return; |
| 5322 | } |
| 5323 | resource_policy::ClientPriority highestPriority = clientDescriptor->getPriority(); |
| 5324 | sp<BasicClient> highestPriorityClient = clientDescriptor->getValue(); |
| 5325 | if (highestPriorityClient.get() != nullptr) { |
| 5326 | for (auto& i : getAll()) { |
| 5327 | if ((i->getKey() == value->getKey()) && (i->getPriority() < highestPriority)) { |
| 5328 | highestPriority = i->getPriority(); |
| 5329 | highestPriorityClient = i->getValue(); |
| 5330 | } |
| 5331 | } |
| 5332 | highestPriorityClient->setPrimaryClient(true); |
| 5333 | highestPriorityClient->notifyClientSharedAccessPriorityChanged(true); |
| 5334 | } |
| 5335 | } |
| 5336 | } |
| 5337 | } |
| 5338 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5339 | std::string CameraService::CameraClientManager::toString() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5340 | auto all = getAll(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5341 | std::ostringstream ret; |
| 5342 | ret << "["; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5343 | bool hasAny = false; |
| 5344 | for (auto& i : all) { |
| 5345 | hasAny = true; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5346 | std::string key = i->getKey(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5347 | int32_t cost = i->getCost(); |
| 5348 | int32_t pid = i->getOwnerId(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 5349 | int32_t score = i->getPriority().getScore(); |
| 5350 | int32_t state = i->getPriority().getState(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5351 | auto conflicting = i->getConflicting(); |
| 5352 | auto clientSp = i->getValue(); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5353 | std::string packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 5354 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5355 | if (clientSp.get() != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5356 | packageName = clientSp->getPackageName(); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5357 | uid_t clientUid = clientSp->getClientUid(); |
| 5358 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5359 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5360 | ret << fmt::sprintf("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" |
| 5361 | PRId32 ", State: %" PRId32, key.c_str(), cost, pid, score, state); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5362 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5363 | if (clientSp.get() != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5364 | ret << fmt::sprintf("User Id: %d, ", clientUserId); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 5365 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5366 | if (packageName.size() != 0) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5367 | ret << fmt::sprintf("Client Package Name: %s", packageName.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5368 | } |
| 5369 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5370 | ret << ", Conflicting Client Devices: {"; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5371 | for (auto& j : conflicting) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5372 | ret << fmt::sprintf("%s, ", j.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5373 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5374 | ret << "})"; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5375 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5376 | if (hasAny) ret << "\n"; |
| 5377 | ret << "]\n"; |
Yi Kong | 3ac211f | 2024-08-12 07:31:44 +0800 | [diff] [blame] | 5378 | return ret.str(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5382 | const std::string& key, const sp<BasicClient>& value, int32_t cost, |
| 5383 | const std::set<std::string>& conflictingKeys, int32_t score, int32_t ownerId, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 5384 | int32_t state, int32_t oomScoreOffset, bool systemNativeClient, bool sharedMode) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5385 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5386 | int32_t score_adj = systemNativeClient ? kSystemNativeClientScore : score; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5387 | int32_t state_adj = systemNativeClient ? kSystemNativeClientState : state; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 5388 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5389 | return std::make_shared<resource_policy::ClientDescriptor<std::string, sp<BasicClient>>>( |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5390 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 5391 | systemNativeClient, oomScoreOffset, sharedMode); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 5395 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5396 | int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5397 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 5398 | partial->getConflicting(), partial->getPriority().getScore(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 5399 | partial->getOwnerId(), partial->getPriority().getState(), oomScoreOffset, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 5400 | systemNativeClient, partial->getSharedMode()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5401 | } |
| 5402 | |
| 5403 | // ---------------------------------------------------------------------------- |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5404 | // InjectionStatusListener |
| 5405 | // ---------------------------------------------------------------------------- |
| 5406 | |
| 5407 | void CameraService::InjectionStatusListener::addListener( |
| 5408 | const sp<ICameraInjectionCallback>& callback) { |
| 5409 | Mutex::Autolock lock(mListenerLock); |
| 5410 | if (mCameraInjectionCallback) return; |
| 5411 | status_t res = IInterface::asBinder(callback)->linkToDeath(this); |
| 5412 | if (res == OK) { |
| 5413 | mCameraInjectionCallback = callback; |
| 5414 | } |
| 5415 | } |
| 5416 | |
| 5417 | void CameraService::InjectionStatusListener::removeListener() { |
| 5418 | Mutex::Autolock lock(mListenerLock); |
| 5419 | if (mCameraInjectionCallback == nullptr) { |
| 5420 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 5421 | return; |
| 5422 | } |
| 5423 | IInterface::asBinder(mCameraInjectionCallback)->unlinkToDeath(this); |
| 5424 | mCameraInjectionCallback = nullptr; |
| 5425 | } |
| 5426 | |
| 5427 | void CameraService::InjectionStatusListener::notifyInjectionError( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5428 | const std::string &injectedCamId, status_t err) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5429 | if (mCameraInjectionCallback == nullptr) { |
| 5430 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 5431 | return; |
| 5432 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5433 | |
| 5434 | switch (err) { |
| 5435 | case -ENODEV: |
| 5436 | mCameraInjectionCallback->onInjectionError( |
| 5437 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5438 | ALOGE("No camera device with ID \"%s\" currently available!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5439 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5440 | break; |
| 5441 | case -EBUSY: |
| 5442 | mCameraInjectionCallback->onInjectionError( |
| 5443 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5444 | ALOGE("Higher-priority client using camera, ID \"%s\" currently unavailable!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5445 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5446 | break; |
| 5447 | case DEAD_OBJECT: |
| 5448 | mCameraInjectionCallback->onInjectionError( |
| 5449 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5450 | ALOGE("Camera ID \"%s\" object is dead!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5451 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5452 | break; |
| 5453 | case INVALID_OPERATION: |
| 5454 | mCameraInjectionCallback->onInjectionError( |
| 5455 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 5456 | ALOGE("Camera ID \"%s\" encountered an operating or internal error!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5457 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5458 | break; |
| 5459 | case UNKNOWN_TRANSACTION: |
| 5460 | mCameraInjectionCallback->onInjectionError( |
| 5461 | ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED); |
| 5462 | ALOGE("Camera ID \"%s\" method doesn't support!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5463 | injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5464 | break; |
| 5465 | default: |
| 5466 | mCameraInjectionCallback->onInjectionError( |
| 5467 | ICameraInjectionCallback::ERROR_INJECTION_INVALID_ERROR); |
| 5468 | ALOGE("Unexpected error %s (%d) opening camera \"%s\"!", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5469 | strerror(-err), err, injectedCamId.c_str()); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5470 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5471 | } |
| 5472 | |
| 5473 | void CameraService::InjectionStatusListener::binderDied( |
| 5474 | const wp<IBinder>& /*who*/) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5475 | ALOGV("InjectionStatusListener: ICameraInjectionCallback has died"); |
| 5476 | auto parent = mParent.promote(); |
| 5477 | if (parent != nullptr) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5478 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 5479 | if (clientDescriptor != nullptr) { |
| 5480 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 5481 | baseClientPtr->stopInjection(); |
| 5482 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 5483 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5484 | } |
| 5485 | } |
| 5486 | |
| 5487 | // ---------------------------------------------------------------------------- |
| 5488 | // CameraInjectionSession |
| 5489 | // ---------------------------------------------------------------------------- |
| 5490 | |
| 5491 | binder::Status CameraService::CameraInjectionSession::stopInjection() { |
| 5492 | Mutex::Autolock lock(mInjectionSessionLock); |
| 5493 | auto parent = mParent.promote(); |
| 5494 | if (parent == nullptr) { |
| 5495 | ALOGE("CameraInjectionSession: Parent is gone"); |
| 5496 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SERVICE, |
| 5497 | "Camera service encountered error"); |
| 5498 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5499 | |
| 5500 | status_t res = NO_ERROR; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5501 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 5502 | if (clientDescriptor != nullptr) { |
| 5503 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 5504 | res = baseClientPtr->stopInjection(); |
| 5505 | if (res != OK) { |
| 5506 | ALOGE("CameraInjectionSession: Failed to stop the injection camera!" |
| 5507 | " ret != NO_ERROR: %d", res); |
| 5508 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SESSION, |
| 5509 | "Camera session encountered error"); |
| 5510 | } |
| 5511 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 5512 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5513 | return binder::Status::ok(); |
| 5514 | } |
| 5515 | |
| 5516 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5517 | |
| 5518 | static const int kDumpLockRetries = 50; |
| 5519 | static const int kDumpLockSleep = 60000; |
| 5520 | |
| 5521 | static bool tryLock(Mutex& mutex) |
| 5522 | { |
| 5523 | bool locked = false; |
| 5524 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 5525 | if (mutex.tryLock() == NO_ERROR) { |
| 5526 | locked = true; |
| 5527 | break; |
| 5528 | } |
| 5529 | usleep(kDumpLockSleep); |
| 5530 | } |
| 5531 | return locked; |
| 5532 | } |
| 5533 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5534 | void CameraService::cacheDump() { |
| 5535 | if (mMemFd != -1) { |
| 5536 | const Vector<String16> args; |
| 5537 | ATRACE_CALL(); |
| 5538 | // Acquiring service lock here will avoid the deadlock since |
| 5539 | // cacheDump will not be called during the second disconnect. |
| 5540 | Mutex::Autolock lock(mServiceLock); |
| 5541 | |
| 5542 | Mutex::Autolock l(mCameraStatesLock); |
| 5543 | // Start collecting the info for open sessions and store it in temp file. |
| 5544 | for (const auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5545 | std::string cameraId = state.first; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5546 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 5547 | if (clientDescriptor != nullptr) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5548 | dprintf(mMemFd, "== Camera device %s dynamic info: ==\n", cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5549 | // Log the current open session info before device is disconnected. |
| 5550 | dumpOpenSessionClientLogs(mMemFd, args, cameraId); |
| 5551 | } |
| 5552 | } |
| 5553 | } |
| 5554 | } |
| 5555 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5556 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 5557 | ATRACE_CALL(); |
| 5558 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5559 | if (checkCallingPermission(toString16(sDumpPermission)) == false) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5560 | 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] | 5561 | getCallingPid(), |
| 5562 | getCallingUid()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5563 | return NO_ERROR; |
| 5564 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5565 | bool locked = tryLock(mServiceLock); |
| 5566 | // failed to lock - CameraService is probably deadlocked |
| 5567 | if (!locked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5568 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5569 | } |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5570 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5571 | if (!mInitialized) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5572 | dprintf(fd, "!! No camera HAL available !!\n"); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 5573 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5574 | // Dump event log for error information |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5575 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5576 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5577 | if (locked) mServiceLock.unlock(); |
| 5578 | return NO_ERROR; |
| 5579 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5580 | dprintf(fd, "\n== Service global info: ==\n\n"); |
| 5581 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 5582 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 5583 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", |
| 5584 | mNormalDeviceIdsWithoutSystemCamera.size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 5585 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { |
| 5586 | dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); |
| 5587 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5588 | std::string activeClientString = mActiveClientManager.toString(); |
| 5589 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.c_str()); |
| 5590 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).c_str()); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5591 | if (mStreamUseCaseOverrides.size() > 0) { |
| 5592 | dprintf(fd, "Active stream use case overrides:"); |
| 5593 | for (int64_t useCaseOverride : mStreamUseCaseOverrides) { |
| 5594 | dprintf(fd, " %" PRId64, useCaseOverride); |
| 5595 | } |
| 5596 | dprintf(fd, "\n"); |
| 5597 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5598 | |
| 5599 | dumpEventLog(fd); |
| 5600 | |
| 5601 | bool stateLocked = tryLock(mCameraStatesLock); |
| 5602 | if (!stateLocked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5603 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5604 | } |
| 5605 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5606 | int argSize = args.size(); |
| 5607 | for (int i = 0; i < argSize; i++) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5608 | if (args[i] == toString16(TagMonitor::kMonitorOption)) { |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5609 | if (i + 1 < argSize) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5610 | mMonitorTags = toStdString(args[i + 1]); |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 5611 | } |
| 5612 | break; |
| 5613 | } |
| 5614 | } |
| 5615 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5616 | for (auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5617 | const std::string &cameraId = state.first; |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5618 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5619 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.c_str()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5620 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5621 | CameraParameters p = state.second->getShimParams(); |
| 5622 | if (!p.isEmpty()) { |
| 5623 | dprintf(fd, " Camera1 API shim is using parameters:\n "); |
| 5624 | p.dump(fd, args); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5625 | } |
| 5626 | |
| 5627 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 5628 | if (clientDescriptor != nullptr) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5629 | // log the current open session info |
| 5630 | dumpOpenSessionClientLogs(fd, args, cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 5631 | } else { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5632 | dumpClosedSessionClientLogs(fd, cameraId); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5633 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5634 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5635 | } |
| 5636 | |
| 5637 | if (stateLocked) mCameraStatesLock.unlock(); |
| 5638 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5639 | if (locked) mServiceLock.unlock(); |
| 5640 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 5641 | mCameraProviderManager->dump(fd, args); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5642 | |
| 5643 | dprintf(fd, "\n== Vendor tags: ==\n\n"); |
| 5644 | |
| 5645 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 5646 | if (desc == NULL) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 5647 | sp<VendorTagDescriptorCache> cache = |
| 5648 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 5649 | if (cache == NULL) { |
| 5650 | dprintf(fd, "No vendor tags.\n"); |
| 5651 | } else { |
| 5652 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 5653 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5654 | } else { |
| 5655 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 5656 | } |
| 5657 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5658 | // Dump camera traces if there were any |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5659 | dprintf(fd, "\n"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5660 | camera3::CameraTraces::dump(fd); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5661 | |
| 5662 | // Process dump arguments, if any |
| 5663 | int n = args.size(); |
| 5664 | String16 verboseOption("-v"); |
| 5665 | String16 unreachableOption("--unreachable"); |
| 5666 | for (int i = 0; i < n; i++) { |
| 5667 | if (args[i] == verboseOption) { |
| 5668 | // change logging level |
| 5669 | if (i + 1 >= n) continue; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5670 | std::string levelStr = toStdString(args[i+1]); |
| 5671 | int level = atoi(levelStr.c_str()); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5672 | dprintf(fd, "\nSetting log level to %d.\n", level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5673 | setLogLevel(level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5674 | } else if (args[i] == unreachableOption) { |
| 5675 | // Dump memory analysis |
| 5676 | // TODO - should limit be an argument parameter? |
| 5677 | UnreachableMemoryInfo info; |
| 5678 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); |
| 5679 | if (!success) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5680 | dprintf(fd, "\n== Unable to dump unreachable memory. " |
| 5681 | "Try disabling SELinux enforcement. ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5682 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5683 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 5684 | std::string s = info.ToString(/*log_contents*/ true); |
| 5685 | write(fd, s.c_str(), s.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5686 | } |
| 5687 | } |
| 5688 | } |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5689 | |
| 5690 | bool serviceLocked = tryLock(mServiceLock); |
| 5691 | |
| 5692 | // Dump info from previous open sessions. |
| 5693 | // Reposition the offset to beginning of the file before reading |
| 5694 | |
| 5695 | if ((mMemFd >= 0) && (lseek(mMemFd, 0, SEEK_SET) != -1)) { |
| 5696 | dprintf(fd, "\n**********Dumpsys from previous open session**********\n"); |
| 5697 | ssize_t size_read; |
| 5698 | char buf[4096]; |
| 5699 | while ((size_read = read(mMemFd, buf, (sizeof(buf) - 1))) > 0) { |
| 5700 | // Read data from file to a small buffer and write it to fd. |
| 5701 | write(fd, buf, size_read); |
| 5702 | if (size_read == -1) { |
| 5703 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 5704 | break; |
| 5705 | } |
| 5706 | } |
| 5707 | dprintf(fd, "\n**********End of Dumpsys from previous open session**********\n"); |
| 5708 | } else { |
| 5709 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 5710 | } |
| 5711 | |
| 5712 | if (serviceLocked) mServiceLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5713 | return NO_ERROR; |
| 5714 | } |
| 5715 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5716 | void CameraService::dumpOpenSessionClientLogs(int fd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5717 | const Vector<String16>& args, const std::string& cameraId) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5718 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Rucha Katakwar | 71a0e05 | 2022-04-07 15:44:18 -0700 | [diff] [blame] | 5719 | dprintf(fd, " %s : Device %s is open. Client instance dump:\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5720 | getFormattedCurrentTime().c_str(), |
| 5721 | cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5722 | dprintf(fd, " Client priority score: %d state: %d\n", |
| 5723 | clientDescriptor->getPriority().getScore(), |
| 5724 | clientDescriptor->getPriority().getState()); |
| 5725 | dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 5726 | |
| 5727 | auto client = clientDescriptor->getValue(); |
| 5728 | dprintf(fd, " Client package: %s\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5729 | client->getPackageName().c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5730 | |
| 5731 | client->dumpClient(fd, args); |
| 5732 | } |
| 5733 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5734 | void CameraService::dumpClosedSessionClientLogs(int fd, const std::string& cameraId) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5735 | dprintf(fd, " Device %s is closed, no client instance\n", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5736 | cameraId.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 5737 | } |
| 5738 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5739 | void CameraService::dumpEventLog(int fd) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5740 | 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] | 5741 | |
| 5742 | Mutex::Autolock l(mLogLock); |
| 5743 | for (const auto& msg : mEventLog) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5744 | dprintf(fd, " %s\n", msg.c_str()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5745 | } |
| 5746 | |
| 5747 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5748 | dprintf(fd, " ...\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5749 | } else if (mEventLog.size() == 0) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5750 | dprintf(fd, " [no events yet]\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5751 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 5752 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 5753 | } |
| 5754 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5755 | void CameraService::cacheClientTagDumpIfNeeded(const std::string &cameraId, BasicClient* client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5756 | Mutex::Autolock lock(mLogLock); |
| 5757 | if (!isClientWatchedLocked(client)) { return; } |
| 5758 | |
| 5759 | std::vector<std::string> dumpVector; |
| 5760 | client->dumpWatchedEventsToVector(dumpVector); |
| 5761 | |
| 5762 | if (dumpVector.empty()) { return; } |
| 5763 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5764 | std::ostringstream dumpString; |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 5765 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5766 | std::string currentTime = getFormattedCurrentTime(); |
| 5767 | dumpString << "Cached @ "; |
| 5768 | dumpString << currentTime; |
| 5769 | dumpString << "\n"; // First line is the timestamp of when client is cached. |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 5770 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5771 | size_t i = dumpVector.size(); |
| 5772 | |
| 5773 | // Store the string in reverse order (latest last) |
| 5774 | while (i > 0) { |
| 5775 | i--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5776 | dumpString << cameraId; |
| 5777 | dumpString << ":"; |
| 5778 | dumpString << client->getPackageName(); |
| 5779 | dumpString << " "; |
| 5780 | dumpString << dumpVector[i]; // implicitly ends with '\n' |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5781 | } |
| 5782 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5783 | mWatchedClientsDumpCache[client->getPackageName()] = dumpString.str(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5784 | } |
| 5785 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5786 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5787 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5788 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 5789 | if (mTorchClientMap[i] == who) { |
| 5790 | // turn off the torch mode that was turned on by dead client |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5791 | std::string cameraId = mTorchClientMap.keyAt(i); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5792 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 5793 | if (res) { |
| 5794 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 5795 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 5796 | return; |
| 5797 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5798 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5799 | break; |
| 5800 | } |
| 5801 | } |
| 5802 | } |
| 5803 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5804 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 5805 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 5806 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 5807 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 5808 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 5809 | */ |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 5810 | // PID here is approximate and can be wrong. |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 5811 | logClientDied(getCallingPid(), "Binder died unexpectedly"); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 5812 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5813 | // check torch client |
| 5814 | handleTorchClientBinderDied(who); |
| 5815 | |
| 5816 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5817 | if(!evictClientIdByRemote(who)) { |
| 5818 | 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] | 5819 | return; |
| 5820 | } |
| 5821 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5822 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 5823 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 5824 | } |
| 5825 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5826 | void CameraService::updateStatus(StatusInternal status, const std::string& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5827 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 5828 | } |
| 5829 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5830 | void CameraService::updateStatus(StatusInternal status, const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5831 | std::initializer_list<StatusInternal> rejectSourceStates) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5832 | // Do not lock mServiceLock here or can get into a deadlock from |
| 5833 | // connect() -> disconnect -> updateStatus |
| 5834 | |
| 5835 | auto state = getCameraState(cameraId); |
| 5836 | |
| 5837 | if (state == nullptr) { |
| 5838 | 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] | 5839 | cameraId.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5840 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 5841 | } |
| 5842 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5843 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 5844 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 5845 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5846 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.c_str()); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5847 | return; |
| 5848 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5849 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5850 | if (vd_flags::camera_device_awareness() && status == StatusInternal::PRESENT) { |
| 5851 | CameraMetadata cameraInfo; |
| 5852 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 5853 | cameraId, false, &cameraInfo, hardware::ICameraService::ROTATION_OVERRIDE_NONE); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5854 | if (res != OK) { |
| 5855 | ALOGW("%s: Not able to get camera characteristics for camera id %s", |
| 5856 | __FUNCTION__, cameraId.c_str()); |
| 5857 | } else { |
| 5858 | int32_t deviceId = getDeviceId(cameraInfo); |
| 5859 | if (deviceId != kDefaultDeviceId) { |
| 5860 | const auto &lensFacingEntry = cameraInfo.find(ANDROID_LENS_FACING); |
| 5861 | camera_metadata_enum_android_lens_facing_t androidLensFacing = |
| 5862 | static_cast<camera_metadata_enum_android_lens_facing_t>( |
| 5863 | lensFacingEntry.data.u8[0]); |
| 5864 | std::string mappedCameraId; |
| 5865 | if (androidLensFacing == ANDROID_LENS_FACING_BACK) { |
| 5866 | mappedCameraId = kVirtualDeviceBackCameraId; |
| 5867 | } else if (androidLensFacing == ANDROID_LENS_FACING_FRONT) { |
| 5868 | mappedCameraId = kVirtualDeviceFrontCameraId; |
| 5869 | } else { |
| 5870 | ALOGD("%s: Not adding entry for an external camera of a virtual device", |
| 5871 | __func__); |
| 5872 | } |
| 5873 | if (!mappedCameraId.empty()) { |
| 5874 | mVirtualDeviceCameraIdMapper.addCamera(cameraId, deviceId, mappedCameraId); |
| 5875 | } |
| 5876 | } |
| 5877 | } |
| 5878 | } |
| 5879 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5880 | // Collect the logical cameras without holding mStatusLock in updateStatus |
| 5881 | // as that can lead to a deadlock(b/162192331). |
| 5882 | auto logicalCameraIds = getLogicalCameras(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5883 | // 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] | 5884 | // of the listeners with both the mStatusLock and mStatusListenerLock held |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 5885 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5886 | &logicalCameraIds] |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5887 | (const std::string& cameraId, StatusInternal status) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5888 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 5889 | auto [deviceId, mappedCameraId] = |
| 5890 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5891 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5892 | if (status != StatusInternal::ENUMERATING) { |
| 5893 | // Update torch status if it has a flash unit. |
| 5894 | Mutex::Autolock al(mTorchStatusMutex); |
| 5895 | TorchModeStatus torchStatus; |
| 5896 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 5897 | NAME_NOT_FOUND) { |
| 5898 | TorchModeStatus newTorchStatus = |
| 5899 | status == StatusInternal::PRESENT ? |
| 5900 | TorchModeStatus::AVAILABLE_OFF : |
| 5901 | TorchModeStatus::NOT_AVAILABLE; |
| 5902 | if (torchStatus != newTorchStatus) { |
| 5903 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); |
| 5904 | } |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 5905 | } |
| 5906 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5907 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5908 | Mutex::Autolock lock(mStatusListenerLock); |
| 5909 | notifyPhysicalCameraStatusLocked(mapToInterface(status), mappedCameraId, |
| 5910 | logicalCameraIds, deviceKind, deviceId); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5911 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5912 | for (auto& listener : mListenerList) { |
| 5913 | bool isVendorListener = listener->isVendorListener(); |
| 5914 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, |
| 5915 | listener->getListenerPid(), listener->getListenerUid())) { |
| 5916 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 5917 | cameraId.c_str()); |
| 5918 | continue; |
| 5919 | } |
| 5920 | |
| 5921 | auto ret = listener->getListener()->onStatusChanged(mapToInterface(status), |
| 5922 | mappedCameraId, deviceId); |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5923 | listener->handleBinderStatus(ret, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5924 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", |
malikakash | 82ed435 | 2023-07-21 22:44:34 +0000 | [diff] [blame] | 5925 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 5926 | ret.exceptionCode()); |
| 5927 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5928 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 5929 | } |
| 5930 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5931 | void CameraService::updateOpenCloseStatus(const std::string& cameraId, bool open, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 5932 | const std::string& clientPackageName, bool sharedMode) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5933 | auto state = getCameraState(cameraId); |
| 5934 | if (state == nullptr) { |
| 5935 | 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] | 5936 | cameraId.c_str()); |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5937 | return; |
| 5938 | } |
| 5939 | if (open) { |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 5940 | if (flags::camera_multi_client() && sharedMode) { |
| 5941 | state->addClientPackage(clientPackageName); |
| 5942 | } else { |
| 5943 | state->setClientPackage(clientPackageName); |
| 5944 | } |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5945 | } else { |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 5946 | if (flags::camera_multi_client() && sharedMode) { |
| 5947 | state->removeClientPackage(clientPackageName); |
| 5948 | } else { |
| 5949 | state->setClientPackage(std::string()); |
| 5950 | } |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5951 | } |
| 5952 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5953 | // Get the device id and app-visible camera id for the given HAL-visible camera id. |
| 5954 | auto [deviceId, mappedCameraId] = |
| 5955 | mVirtualDeviceCameraIdMapper.getDeviceIdAndMappedCameraIdPair(cameraId); |
| 5956 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5957 | Mutex::Autolock lock(mStatusListenerLock); |
| 5958 | |
| 5959 | for (const auto& it : mListenerList) { |
| 5960 | if (!it->isOpenCloseCallbackAllowed()) { |
| 5961 | continue; |
| 5962 | } |
| 5963 | |
| 5964 | binder::Status ret; |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5965 | if (open) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 5966 | ret = it->getListener()->onCameraOpened(mappedCameraId, clientPackageName, |
| 5967 | deviceId); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5968 | } else { |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame^] | 5969 | if (!flags::camera_multi_client() || !sharedMode || (sharedMode && |
| 5970 | mActiveClientManager.getCameraClient(cameraId) == nullptr)) { |
| 5971 | ret = it->getListener()->onCameraClosed(mappedCameraId, deviceId); |
| 5972 | } |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5973 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5974 | |
| 5975 | it->handleBinderStatus(ret, |
| 5976 | "%s: Failed to trigger onCameraOpened/onCameraClosed callback for %d:%d: %d", |
| 5977 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5978 | } |
| 5979 | } |
| 5980 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5981 | template<class Func> |
| 5982 | void CameraService::CameraState::updateStatus(StatusInternal status, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 5983 | const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5984 | std::initializer_list<StatusInternal> rejectSourceStates, |
| 5985 | Func onStatusUpdatedLocked) { |
| 5986 | Mutex::Autolock lock(mStatusLock); |
| 5987 | StatusInternal oldStatus = mStatus; |
| 5988 | mStatus = status; |
| 5989 | |
| 5990 | if (oldStatus == status) { |
| 5991 | return; |
| 5992 | } |
| 5993 | |
| 5994 | 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] | 5995 | cameraId.c_str(), eToI(oldStatus), eToI(status)); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5996 | |
| 5997 | if (oldStatus == StatusInternal::NOT_PRESENT && |
| 5998 | (status != StatusInternal::PRESENT && |
| 5999 | status != StatusInternal::ENUMERATING)) { |
| 6000 | |
| 6001 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 6002 | __FUNCTION__); |
| 6003 | mStatus = oldStatus; |
| 6004 | return; |
| 6005 | } |
| 6006 | |
| 6007 | /** |
| 6008 | * Sometimes we want to conditionally do a transition. |
| 6009 | * For example if a client disconnects, we want to go to PRESENT |
| 6010 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 6011 | */ |
| 6012 | for (auto& rejectStatus : rejectSourceStates) { |
| 6013 | if (oldStatus == rejectStatus) { |
| 6014 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6015 | "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] | 6016 | mStatus = oldStatus; |
| 6017 | return; |
| 6018 | } |
| 6019 | } |
| 6020 | |
| 6021 | onStatusUpdatedLocked(cameraId, status); |
| 6022 | } |
| 6023 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 6024 | status_t CameraService::getTorchStatusLocked( |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6025 | const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 6026 | TorchModeStatus *status) const { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 6027 | if (!status) { |
| 6028 | return BAD_VALUE; |
| 6029 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 6030 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 6031 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 6032 | // invalid camera ID or the camera doesn't have a flash unit |
| 6033 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 6034 | } |
| 6035 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 6036 | *status = mTorchStatusMap.valueAt(index); |
| 6037 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 6038 | } |
| 6039 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6040 | status_t CameraService::setTorchStatusLocked(const std::string& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 6041 | TorchModeStatus status) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 6042 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 6043 | if (index == NAME_NOT_FOUND) { |
| 6044 | return BAD_VALUE; |
| 6045 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 6046 | mTorchStatusMap.editValueAt(index) = status; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 6047 | |
| 6048 | return OK; |
| 6049 | } |
| 6050 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6051 | std::list<std::string> CameraService::getLogicalCameras( |
| 6052 | const std::string& physicalCameraId) { |
| 6053 | std::list<std::string> retList; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 6054 | Mutex::Autolock lock(mCameraStatesLock); |
| 6055 | for (const auto& state : mCameraStates) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6056 | if (state.second->containsPhysicalCamera(physicalCameraId)) { |
| 6057 | retList.emplace_back(state.first); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 6058 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 6059 | } |
| 6060 | return retList; |
| 6061 | } |
| 6062 | |
| 6063 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6064 | const std::string& physicalCameraId, const std::list<std::string>& logicalCameraIds, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 6065 | SystemCameraKind deviceKind, int32_t deviceId) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 6066 | // mStatusListenerLock is expected to be locked |
| 6067 | for (const auto& logicalCameraId : logicalCameraIds) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 6068 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 6069 | // Note: we check only the deviceKind of the physical camera id |
| 6070 | // since, logical camera ids and their physical camera ids are |
| 6071 | // guaranteed to have the same system camera kind. |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 6072 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 6073 | listener->getListenerPid(), listener->getListenerUid())) { |
| 6074 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6075 | physicalCameraId.c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 6076 | continue; |
| 6077 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 6078 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged(status, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 6079 | logicalCameraId, physicalCameraId, deviceId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 6080 | listener->handleBinderStatus(ret, |
| 6081 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 6082 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 6083 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 6084 | } |
| 6085 | } |
| 6086 | } |
| 6087 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6088 | void CameraService::blockClientsForUid(uid_t uid) { |
| 6089 | const auto clients = mActiveClientManager.getAll(); |
| 6090 | for (auto& current : clients) { |
| 6091 | if (current != nullptr) { |
| 6092 | const auto basicClient = current->getValue(); |
| 6093 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { |
| 6094 | basicClient->block(); |
| 6095 | } |
| 6096 | } |
| 6097 | } |
| 6098 | } |
| 6099 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 6100 | void CameraService::blockAllClients() { |
| 6101 | const auto clients = mActiveClientManager.getAll(); |
| 6102 | for (auto& current : clients) { |
| 6103 | if (current != nullptr) { |
| 6104 | const auto basicClient = current->getValue(); |
| 6105 | if (basicClient.get() != nullptr) { |
| 6106 | basicClient->block(); |
| 6107 | } |
| 6108 | } |
| 6109 | } |
| 6110 | } |
| 6111 | |
Jyoti Bhayana | c05a119 | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 6112 | void CameraService::blockPrivacyEnabledClients() { |
| 6113 | const auto clients = mActiveClientManager.getAll(); |
| 6114 | for (auto& current : clients) { |
| 6115 | if (current != nullptr) { |
| 6116 | const auto basicClient = current->getValue(); |
| 6117 | if (basicClient.get() != nullptr) { |
| 6118 | std::string pkgName = basicClient->getPackageName(); |
| 6119 | bool cameraPrivacyEnabled = |
| 6120 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(toString16(pkgName)); |
| 6121 | if (cameraPrivacyEnabled) { |
| 6122 | basicClient->block(); |
| 6123 | } |
| 6124 | } |
| 6125 | } |
| 6126 | } |
| 6127 | } |
| 6128 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6129 | // NOTE: This is a remote API - make sure all args are validated |
| 6130 | 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] | 6131 | if (!checkCallingPermission(toString16(sManageCameraPermission), nullptr, nullptr)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6132 | return PERMISSION_DENIED; |
| 6133 | } |
| 6134 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 6135 | return BAD_VALUE; |
| 6136 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6137 | if (args.size() >= 3 && args[0] == toString16("set-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6138 | return handleSetUidState(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6139 | } else if (args.size() >= 2 && args[0] == toString16("reset-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6140 | return handleResetUidState(args, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6141 | } else if (args.size() >= 2 && args[0] == toString16("get-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6142 | return handleGetUidState(args, out, err); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6143 | } 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] | 6144 | return handleSetRotateAndCrop(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6145 | } 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] | 6146 | return handleGetRotateAndCrop(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6147 | } else if (args.size() >= 2 && args[0] == toString16("set-autoframing")) { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6148 | return handleSetAutoframing(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6149 | } else if (args.size() >= 1 && args[0] == toString16("get-autoframing")) { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6150 | return handleGetAutoframing(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6151 | } else if (args.size() >= 2 && args[0] == toString16("set-image-dump-mask")) { |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6152 | return handleSetImageDumpMask(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6153 | } else if (args.size() >= 1 && args[0] == toString16("get-image-dump-mask")) { |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6154 | return handleGetImageDumpMask(out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6155 | } else if (args.size() >= 2 && args[0] == toString16("set-camera-mute")) { |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6156 | return handleSetCameraMute(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6157 | } 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] | 6158 | return handleSetStreamUseCaseOverrides(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6159 | } 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] | 6160 | handleClearStreamUseCaseOverrides(); |
| 6161 | return OK; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6162 | } else if (args.size() >= 1 && args[0] == toString16("set-zoom-override")) { |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6163 | return handleSetZoomOverride(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6164 | } else if (args.size() >= 2 && args[0] == toString16("watch")) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6165 | return handleWatchCommand(args, in, out); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6166 | } else if (args.size() >= 2 && args[0] == toString16("set-watchdog")) { |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6167 | return handleSetCameraServiceWatchdog(args); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6168 | } else if (args.size() == 1 && args[0] == toString16("help")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6169 | printHelp(out); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6170 | return OK; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6171 | } |
| 6172 | printHelp(err); |
| 6173 | return BAD_VALUE; |
| 6174 | } |
| 6175 | |
| 6176 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6177 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6178 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6179 | bool active = false; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6180 | if (args[2] == toString16("active")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6181 | active = true; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6182 | } else if ((args[2] != toString16("idle"))) { |
| 6183 | 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] | 6184 | return BAD_VALUE; |
| 6185 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6186 | |
| 6187 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6188 | if (args.size() >= 5 && args[3] == toString16("--user")) { |
| 6189 | userId = atoi(toStdString(args[4]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6190 | } |
| 6191 | |
| 6192 | uid_t uid; |
| 6193 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
| 6194 | return BAD_VALUE; |
| 6195 | } |
| 6196 | |
| 6197 | mUidPolicy->addOverrideUid(uid, packageName, active); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6198 | return NO_ERROR; |
| 6199 | } |
| 6200 | |
| 6201 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6202 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6203 | |
| 6204 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6205 | if (args.size() >= 4 && args[2] == toString16("--user")) { |
| 6206 | userId = atoi(toStdString(args[3]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6207 | } |
| 6208 | |
| 6209 | uid_t uid; |
| 6210 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6211 | return BAD_VALUE; |
| 6212 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6213 | |
| 6214 | mUidPolicy->removeOverrideUid(uid, packageName); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6215 | return NO_ERROR; |
| 6216 | } |
| 6217 | |
| 6218 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6219 | std::string packageName = toStdString(args[1]); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6220 | |
| 6221 | int userId = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6222 | if (args.size() >= 4 && args[2] == toString16("--user")) { |
| 6223 | userId = atoi(toStdString(args[3]).c_str()); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6224 | } |
| 6225 | |
| 6226 | uid_t uid; |
| 6227 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6228 | return BAD_VALUE; |
| 6229 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6230 | |
| 6231 | if (mUidPolicy->isUidActive(uid, packageName)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6232 | return dprintf(out, "active\n"); |
| 6233 | } else { |
| 6234 | return dprintf(out, "idle\n"); |
| 6235 | } |
| 6236 | } |
| 6237 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6238 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6239 | int rotateValue = atoi(toStdString(args[1]).c_str()); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6240 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || |
| 6241 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; |
| 6242 | Mutex::Autolock lock(mServiceLock); |
| 6243 | |
| 6244 | mOverrideRotateAndCropMode = rotateValue; |
| 6245 | |
| 6246 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; |
| 6247 | |
| 6248 | const auto clients = mActiveClientManager.getAll(); |
| 6249 | for (auto& current : clients) { |
| 6250 | if (current != nullptr) { |
| 6251 | const auto basicClient = current->getValue(); |
| 6252 | if (basicClient.get() != nullptr) { |
| 6253 | basicClient->setRotateAndCropOverride(rotateValue); |
| 6254 | } |
| 6255 | } |
| 6256 | } |
| 6257 | |
| 6258 | return OK; |
| 6259 | } |
| 6260 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6261 | status_t CameraService::handleSetAutoframing(const Vector<String16>& args) { |
| 6262 | char* end; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6263 | int autoframingValue = (int) strtol(toStdString(args[1]).c_str(), &end, /*base=*/10); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6264 | if ((*end != '\0') || |
| 6265 | (autoframingValue != ANDROID_CONTROL_AUTOFRAMING_OFF && |
| 6266 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_ON && |
| 6267 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_AUTO)) { |
| 6268 | return BAD_VALUE; |
| 6269 | } |
| 6270 | |
| 6271 | Mutex::Autolock lock(mServiceLock); |
| 6272 | mOverrideAutoframingMode = autoframingValue; |
| 6273 | |
| 6274 | if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) return OK; |
| 6275 | |
| 6276 | const auto clients = mActiveClientManager.getAll(); |
| 6277 | for (auto& current : clients) { |
| 6278 | if (current != nullptr) { |
| 6279 | const auto basicClient = current->getValue(); |
| 6280 | if (basicClient.get() != nullptr) { |
| 6281 | basicClient->setAutoframingOverride(autoframingValue); |
| 6282 | } |
| 6283 | } |
| 6284 | } |
| 6285 | |
| 6286 | return OK; |
| 6287 | } |
| 6288 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6289 | status_t CameraService::handleSetCameraServiceWatchdog(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6290 | int enableWatchdog = atoi(toStdString(args[1]).c_str()); |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 6291 | |
| 6292 | if (enableWatchdog < 0 || enableWatchdog > 1) return BAD_VALUE; |
| 6293 | |
| 6294 | Mutex::Autolock lock(mServiceLock); |
| 6295 | |
| 6296 | mCameraServiceWatchdogEnabled = enableWatchdog; |
| 6297 | |
| 6298 | const auto clients = mActiveClientManager.getAll(); |
| 6299 | for (auto& current : clients) { |
| 6300 | if (current != nullptr) { |
| 6301 | const auto basicClient = current->getValue(); |
| 6302 | if (basicClient.get() != nullptr) { |
| 6303 | basicClient->setCameraServiceWatchdog(enableWatchdog); |
| 6304 | } |
| 6305 | } |
| 6306 | } |
| 6307 | |
| 6308 | return OK; |
| 6309 | } |
| 6310 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 6311 | status_t CameraService::handleGetRotateAndCrop(int out) { |
| 6312 | Mutex::Autolock lock(mServiceLock); |
| 6313 | |
| 6314 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); |
| 6315 | } |
| 6316 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 6317 | status_t CameraService::handleGetAutoframing(int out) { |
| 6318 | Mutex::Autolock lock(mServiceLock); |
| 6319 | |
| 6320 | return dprintf(out, "autoframing override: %d\n", mOverrideAutoframingMode); |
| 6321 | } |
| 6322 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6323 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { |
| 6324 | char *endPtr; |
| 6325 | errno = 0; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6326 | std::string maskString = toStdString(args[1]); |
| 6327 | long maskValue = strtol(maskString.c_str(), &endPtr, 10); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6328 | |
| 6329 | if (errno != 0) return BAD_VALUE; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6330 | if (endPtr != maskString.c_str() + maskString.size()) return BAD_VALUE; |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6331 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; |
| 6332 | |
| 6333 | Mutex::Autolock lock(mServiceLock); |
| 6334 | |
| 6335 | mImageDumpMask = maskValue; |
| 6336 | |
| 6337 | return OK; |
| 6338 | } |
| 6339 | |
| 6340 | status_t CameraService::handleGetImageDumpMask(int out) { |
| 6341 | Mutex::Autolock lock(mServiceLock); |
| 6342 | |
| 6343 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); |
| 6344 | } |
| 6345 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6346 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6347 | int muteValue = strtol(toStdString(args[1]).c_str(), nullptr, 10); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 6348 | if (errno != 0) return BAD_VALUE; |
| 6349 | |
| 6350 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; |
| 6351 | Mutex::Autolock lock(mServiceLock); |
| 6352 | |
| 6353 | mOverrideCameraMuteMode = (muteValue == 1); |
| 6354 | |
| 6355 | const auto clients = mActiveClientManager.getAll(); |
| 6356 | for (auto& current : clients) { |
| 6357 | if (current != nullptr) { |
| 6358 | const auto basicClient = current->getValue(); |
| 6359 | if (basicClient.get() != nullptr) { |
| 6360 | if (basicClient->supportsCameraMute()) { |
| 6361 | basicClient->setCameraMute(mOverrideCameraMuteMode); |
| 6362 | } |
| 6363 | } |
| 6364 | } |
| 6365 | } |
| 6366 | |
| 6367 | return OK; |
| 6368 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6369 | |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6370 | status_t CameraService::handleSetStreamUseCaseOverrides(const Vector<String16>& args) { |
| 6371 | std::vector<int64_t> useCasesOverride; |
| 6372 | for (size_t i = 1; i < args.size(); i++) { |
| 6373 | int64_t useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6374 | std::string arg = toStdString(args[i]); |
| 6375 | if (arg == "DEFAULT") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6376 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6377 | } else if (arg == "PREVIEW") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6378 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6379 | } else if (arg == "STILL_CAPTURE") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6380 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_STILL_CAPTURE; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6381 | } else if (arg == "VIDEO_RECORD") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6382 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_RECORD; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6383 | } else if (arg == "PREVIEW_VIDEO_STILL") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6384 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW_VIDEO_STILL; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6385 | } else if (arg == "VIDEO_CALL") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6386 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6387 | } else if (arg == "CROPPED_RAW") { |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6388 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_CROPPED_RAW; |
| 6389 | } else { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6390 | ALOGE("%s: Invalid stream use case %s", __FUNCTION__, arg.c_str()); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6391 | return BAD_VALUE; |
| 6392 | } |
| 6393 | useCasesOverride.push_back(useCase); |
| 6394 | } |
| 6395 | |
| 6396 | Mutex::Autolock lock(mServiceLock); |
| 6397 | mStreamUseCaseOverrides = std::move(useCasesOverride); |
| 6398 | |
| 6399 | return OK; |
| 6400 | } |
| 6401 | |
| 6402 | void CameraService::handleClearStreamUseCaseOverrides() { |
| 6403 | Mutex::Autolock lock(mServiceLock); |
| 6404 | mStreamUseCaseOverrides.clear(); |
| 6405 | } |
| 6406 | |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6407 | status_t CameraService::handleSetZoomOverride(const Vector<String16>& args) { |
| 6408 | char* end; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6409 | int zoomOverrideValue = strtol(toStdString(args[1]).c_str(), &end, /*base=*/10); |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6410 | if ((*end != '\0') || |
| 6411 | (zoomOverrideValue != -1 && |
| 6412 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF && |
| 6413 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM)) { |
| 6414 | return BAD_VALUE; |
| 6415 | } |
| 6416 | |
| 6417 | Mutex::Autolock lock(mServiceLock); |
| 6418 | mZoomOverrideValue = zoomOverrideValue; |
| 6419 | |
| 6420 | const auto clients = mActiveClientManager.getAll(); |
| 6421 | for (auto& current : clients) { |
| 6422 | if (current != nullptr) { |
| 6423 | const auto basicClient = current->getValue(); |
| 6424 | if (basicClient.get() != nullptr) { |
| 6425 | if (basicClient->supportsZoomOverride()) { |
| 6426 | basicClient->setZoomOverride(mZoomOverrideValue); |
| 6427 | } |
| 6428 | } |
| 6429 | } |
| 6430 | } |
| 6431 | |
| 6432 | return OK; |
| 6433 | } |
| 6434 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6435 | status_t CameraService::handleWatchCommand(const Vector<String16>& args, int inFd, int outFd) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6436 | if (args.size() >= 3 && args[1] == toString16("start")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6437 | return startWatchingTags(args, outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6438 | } else if (args.size() == 2 && args[1] == toString16("stop")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6439 | return stopWatchingTags(outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6440 | } else if (args.size() == 2 && args[1] == toString16("dump")) { |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6441 | return printWatchedTags(outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6442 | } else if (args.size() >= 2 && args[1] == toString16("live")) { |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6443 | return printWatchedTagsUntilInterrupt(args, inFd, outFd); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6444 | } else if (args.size() == 2 && args[1] == toString16("clear")) { |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6445 | return clearCachedMonitoredTagDumps(outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6446 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6447 | dprintf(outFd, "Camera service watch commands:\n" |
| 6448 | " start -m <comma_separated_tag_list> [-c <comma_separated_client_list>]\n" |
| 6449 | " starts watching the provided tags for clients with provided package\n" |
| 6450 | " recognizes tag shorthands like '3a'\n" |
| 6451 | " 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] | 6452 | " dump dumps the monitoring information and exits\n" |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6453 | " stop stops watching all tags\n" |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6454 | " live [-n <refresh_interval_ms>]\n" |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6455 | " prints the monitored information in real time\n" |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6456 | " Hit return to exit\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6457 | " clear clears all buffers storing information for watch command"); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 6458 | return BAD_VALUE; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6459 | } |
| 6460 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6461 | status_t CameraService::startWatchingTags(const Vector<String16> &args, int outFd) { |
| 6462 | Mutex::Autolock lock(mLogLock); |
| 6463 | size_t tagsIdx; // index of '-m' |
| 6464 | String16 tags(""); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6465 | for (tagsIdx = 2; tagsIdx < args.size() && args[tagsIdx] != toString16("-m"); tagsIdx++); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6466 | if (tagsIdx < args.size() - 1) { |
| 6467 | tags = args[tagsIdx + 1]; |
| 6468 | } else { |
| 6469 | dprintf(outFd, "No tags provided.\n"); |
| 6470 | return BAD_VALUE; |
| 6471 | } |
| 6472 | |
| 6473 | size_t clientsIdx; // index of '-c' |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6474 | // watch all clients if no clients are provided |
| 6475 | String16 clients = toString16(kWatchAllClientsFlag); |
| 6476 | for (clientsIdx = 2; clientsIdx < args.size() && args[clientsIdx] != toString16("-c"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6477 | clientsIdx++); |
| 6478 | if (clientsIdx < args.size() - 1) { |
| 6479 | clients = args[clientsIdx + 1]; |
| 6480 | } |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6481 | parseClientsToWatchLocked(toStdString(clients)); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6482 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6483 | // track tags to initialize future clients with the monitoring information |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6484 | mMonitorTags = toStdString(tags); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6485 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6486 | bool serviceLock = tryLock(mServiceLock); |
| 6487 | int numWatchedClients = 0; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6488 | auto cameraClients = mActiveClientManager.getAll(); |
| 6489 | for (const auto &clientDescriptor: cameraClients) { |
| 6490 | if (clientDescriptor == nullptr) { continue; } |
| 6491 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6492 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6493 | |
| 6494 | if (isClientWatchedLocked(client.get())) { |
| 6495 | client->startWatchingTags(mMonitorTags, outFd); |
| 6496 | numWatchedClients++; |
| 6497 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6498 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6499 | dprintf(outFd, "Started watching %d active clients\n", numWatchedClients); |
| 6500 | |
| 6501 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6502 | return OK; |
| 6503 | } |
| 6504 | |
| 6505 | status_t CameraService::stopWatchingTags(int outFd) { |
| 6506 | // clear mMonitorTags to prevent new clients from monitoring tags at initialization |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6507 | Mutex::Autolock lock(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6508 | mMonitorTags = ""; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6509 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6510 | mWatchedClientPackages.clear(); |
| 6511 | mWatchedClientsDumpCache.clear(); |
| 6512 | |
| 6513 | bool serviceLock = tryLock(mServiceLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6514 | auto cameraClients = mActiveClientManager.getAll(); |
| 6515 | for (const auto &clientDescriptor : cameraClients) { |
| 6516 | if (clientDescriptor == nullptr) { continue; } |
| 6517 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6518 | if (client.get() == nullptr) { continue; } |
| 6519 | client->stopWatchingTags(outFd); |
| 6520 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6521 | dprintf(outFd, "Stopped watching all clients.\n"); |
| 6522 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6523 | return OK; |
| 6524 | } |
| 6525 | |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6526 | status_t CameraService::clearCachedMonitoredTagDumps(int outFd) { |
| 6527 | Mutex::Autolock lock(mLogLock); |
| 6528 | size_t clearedSize = mWatchedClientsDumpCache.size(); |
| 6529 | mWatchedClientsDumpCache.clear(); |
| 6530 | dprintf(outFd, "Cleared tag information of %zu cached clients.\n", clearedSize); |
| 6531 | return OK; |
| 6532 | } |
| 6533 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6534 | status_t CameraService::printWatchedTags(int outFd) { |
| 6535 | Mutex::Autolock logLock(mLogLock); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6536 | std::set<std::string> connectedMonitoredClients; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6537 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6538 | bool printedSomething = false; // tracks if any monitoring information was printed |
| 6539 | // (from either cached or active clients) |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6540 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6541 | bool serviceLock = tryLock(mServiceLock); |
| 6542 | // get all watched clients that are currently connected |
| 6543 | for (const auto &clientDescriptor: mActiveClientManager.getAll()) { |
| 6544 | if (clientDescriptor == nullptr) { continue; } |
| 6545 | |
| 6546 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6547 | if (client.get() == nullptr) { continue; } |
| 6548 | if (!isClientWatchedLocked(client.get())) { continue; } |
| 6549 | |
| 6550 | std::vector<std::string> dumpVector; |
| 6551 | client->dumpWatchedEventsToVector(dumpVector); |
| 6552 | |
| 6553 | size_t printIdx = dumpVector.size(); |
| 6554 | if (printIdx == 0) { |
| 6555 | continue; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6556 | } |
| 6557 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6558 | // Print tag dumps for active client |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6559 | const std::string &cameraId = clientDescriptor->getKey(); |
| 6560 | dprintf(outFd, "Client: %s (active)\n", client->getPackageName().c_str()); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6561 | while(printIdx > 0) { |
| 6562 | printIdx--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6563 | dprintf(outFd, "%s:%s %s", cameraId.c_str(), client->getPackageName().c_str(), |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6564 | dumpVector[printIdx].c_str()); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6565 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6566 | dprintf(outFd, "\n"); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6567 | printedSomething = true; |
| 6568 | |
| 6569 | connectedMonitoredClients.emplace(client->getPackageName()); |
| 6570 | } |
| 6571 | if (serviceLock) { mServiceLock.unlock(); } |
| 6572 | |
| 6573 | // Print entries in mWatchedClientsDumpCache for clients that are not connected |
| 6574 | for (const auto &kv: mWatchedClientsDumpCache) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6575 | const std::string &package = kv.first; |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6576 | if (connectedMonitoredClients.find(package) != connectedMonitoredClients.end()) { |
| 6577 | continue; |
| 6578 | } |
| 6579 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6580 | dprintf(outFd, "Client: %s (cached)\n", package.c_str()); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6581 | dprintf(outFd, "%s\n", kv.second.c_str()); |
| 6582 | printedSomething = true; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6583 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6584 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6585 | if (!printedSomething) { |
| 6586 | dprintf(outFd, "No monitoring information to print.\n"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6587 | } |
| 6588 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6589 | return OK; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6590 | } |
| 6591 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6592 | // Print all events in vector `events' that came after lastPrintedEvent |
| 6593 | void printNewWatchedEvents(int outFd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6594 | const std::string &cameraId, |
| 6595 | const std::string &packageName, |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6596 | const std::vector<std::string> &events, |
| 6597 | const std::string &lastPrintedEvent) { |
| 6598 | if (events.empty()) { return; } |
| 6599 | |
| 6600 | // index of lastPrintedEvent in events. |
| 6601 | // lastPrintedIdx = events.size() if lastPrintedEvent is not in events |
| 6602 | size_t lastPrintedIdx; |
| 6603 | for (lastPrintedIdx = 0; |
| 6604 | lastPrintedIdx < events.size() && lastPrintedEvent != events[lastPrintedIdx]; |
| 6605 | lastPrintedIdx++); |
| 6606 | |
| 6607 | if (lastPrintedIdx == 0) { return; } // early exit if no new event in `events` |
| 6608 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6609 | // print events in chronological order (latest event last) |
| 6610 | size_t idxToPrint = lastPrintedIdx; |
| 6611 | do { |
| 6612 | idxToPrint--; |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6613 | dprintf(outFd, "%s:%s %s", cameraId.c_str(), packageName.c_str(), |
| 6614 | events[idxToPrint].c_str()); |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6615 | } while (idxToPrint != 0); |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6616 | } |
| 6617 | |
| 6618 | // Returns true if adb shell cmd watch should be interrupted based on data in inFd. The watch |
| 6619 | // command should be interrupted if the user presses the return key, or if user loses any way to |
| 6620 | // signal interrupt. |
| 6621 | // If timeoutMs == 0, this function will always return false |
| 6622 | bool shouldInterruptWatchCommand(int inFd, int outFd, long timeoutMs) { |
| 6623 | struct timeval startTime; |
| 6624 | int startTimeError = gettimeofday(&startTime, nullptr); |
| 6625 | if (startTimeError) { |
| 6626 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 6627 | return true; |
| 6628 | } |
| 6629 | |
| 6630 | const nfds_t numFds = 1; |
| 6631 | struct pollfd pollFd = { .fd = inFd, .events = POLLIN, .revents = 0 }; |
| 6632 | |
| 6633 | struct timeval currTime; |
| 6634 | char buffer[2]; |
| 6635 | while(true) { |
| 6636 | int currTimeError = gettimeofday(&currTime, nullptr); |
| 6637 | if (currTimeError) { |
| 6638 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 6639 | return true; |
| 6640 | } |
| 6641 | |
| 6642 | long elapsedTimeMs = ((currTime.tv_sec - startTime.tv_sec) * 1000L) |
| 6643 | + ((currTime.tv_usec - startTime.tv_usec) / 1000L); |
| 6644 | int remainingTimeMs = (int) (timeoutMs - elapsedTimeMs); |
| 6645 | |
| 6646 | if (remainingTimeMs <= 0) { |
| 6647 | // No user interrupt within timeoutMs, don't interrupt watch command |
| 6648 | return false; |
| 6649 | } |
| 6650 | |
| 6651 | int numFdsUpdated = poll(&pollFd, numFds, remainingTimeMs); |
| 6652 | if (numFdsUpdated < 0) { |
| 6653 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 6654 | return true; |
| 6655 | } |
| 6656 | |
| 6657 | if (numFdsUpdated == 0) { |
| 6658 | // No user input within timeoutMs, don't interrupt watch command |
| 6659 | return false; |
| 6660 | } |
| 6661 | |
| 6662 | if (!(pollFd.revents & POLLIN)) { |
| 6663 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 6664 | return true; |
| 6665 | } |
| 6666 | |
| 6667 | ssize_t sizeRead = read(inFd, buffer, sizeof(buffer) - 1); |
| 6668 | if (sizeRead < 0) { |
| 6669 | dprintf(outFd, "Error reading user input. Exiting.\n"); |
| 6670 | return true; |
| 6671 | } |
| 6672 | |
| 6673 | if (sizeRead == 0) { |
| 6674 | // Reached end of input fd (can happen if input is piped) |
| 6675 | // User has no way to signal an interrupt, so interrupt here |
| 6676 | return true; |
| 6677 | } |
| 6678 | |
| 6679 | if (buffer[0] == '\n') { |
| 6680 | // User pressed return, interrupt watch command. |
| 6681 | return true; |
| 6682 | } |
| 6683 | } |
| 6684 | } |
| 6685 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6686 | status_t CameraService::printWatchedTagsUntilInterrupt(const Vector<String16> &args, |
| 6687 | int inFd, int outFd) { |
| 6688 | // Figure out refresh interval, if present in args |
| 6689 | long refreshTimeoutMs = 1000L; // refresh every 1s by default |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6690 | if (args.size() > 2) { |
| 6691 | size_t intervalIdx; // index of '-n' |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6692 | for (intervalIdx = 2; intervalIdx < args.size() && toString16("-n") != args[intervalIdx]; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6693 | intervalIdx++); |
| 6694 | |
| 6695 | size_t intervalValIdx = intervalIdx + 1; |
| 6696 | if (intervalValIdx < args.size()) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6697 | refreshTimeoutMs = strtol(toStdString(args[intervalValIdx]).c_str(), nullptr, 10); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6698 | if (errno) { return BAD_VALUE; } |
| 6699 | } |
| 6700 | } |
| 6701 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6702 | // Set min timeout of 10ms. This prevents edge cases in polling when timeout of 0 is passed. |
| 6703 | refreshTimeoutMs = refreshTimeoutMs < 10 ? 10 : refreshTimeoutMs; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6704 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6705 | dprintf(outFd, "Press return to exit...\n\n"); |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6706 | std::map<std::string, std::string> packageNameToLastEvent; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6707 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6708 | while (true) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6709 | bool serviceLock = tryLock(mServiceLock); |
| 6710 | auto cameraClients = mActiveClientManager.getAll(); |
| 6711 | if (serviceLock) { mServiceLock.unlock(); } |
| 6712 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6713 | for (const auto& clientDescriptor : cameraClients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6714 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6715 | if (clientDescriptor == nullptr) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6716 | |
| 6717 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 6718 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6719 | if (!isClientWatchedLocked(client.get())) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6720 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6721 | const std::string &packageName = client->getPackageName(); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6722 | // This also initializes the map entries with an empty string |
| 6723 | const std::string& lastPrintedEvent = packageNameToLastEvent[packageName]; |
| 6724 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6725 | std::vector<std::string> latestEvents; |
| 6726 | client->dumpWatchedEventsToVector(latestEvents); |
| 6727 | |
| 6728 | if (!latestEvents.empty()) { |
| 6729 | printNewWatchedEvents(outFd, |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6730 | clientDescriptor->getKey(), |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6731 | packageName, |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6732 | latestEvents, |
| 6733 | lastPrintedEvent); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 6734 | packageNameToLastEvent[packageName] = latestEvents[0]; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6735 | } |
| 6736 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 6737 | if (shouldInterruptWatchCommand(inFd, outFd, refreshTimeoutMs)) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 6738 | break; |
| 6739 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 6740 | } |
| 6741 | return OK; |
| 6742 | } |
| 6743 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6744 | void CameraService::parseClientsToWatchLocked(const std::string &clients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6745 | mWatchedClientPackages.clear(); |
| 6746 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6747 | std::istringstream iss(clients); |
| 6748 | std::string nextClient; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6749 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6750 | while (std::getline(iss, nextClient, ',')) { |
| 6751 | if (nextClient == kWatchAllClientsFlag) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6752 | // Don't need to track any other package if 'all' is present |
| 6753 | mWatchedClientPackages.clear(); |
| 6754 | mWatchedClientPackages.emplace(kWatchAllClientsFlag); |
| 6755 | break; |
| 6756 | } |
| 6757 | |
| 6758 | // track package names |
| 6759 | mWatchedClientPackages.emplace(nextClient); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6760 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6761 | } |
| 6762 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 6763 | status_t CameraService::printHelp(int out) { |
| 6764 | return dprintf(out, "Camera service commands:\n" |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 6765 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 6766 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 6767 | " 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] | 6768 | " set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" |
| 6769 | " Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" |
| 6770 | " 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] | 6771 | " set-autoframing <VALUE> overrides the autoframing value for AUTO\n" |
| 6772 | " Valid values 0=false, 1=true, 2=auto\n" |
| 6773 | " get-autoframing returns the current override autoframing value\n" |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 6774 | " set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" |
| 6775 | " Valid values 0=OFF, 1=ON for JPEG\n" |
| 6776 | " 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] | 6777 | " set-camera-mute <0/1> enable or disable camera muting\n" |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 6778 | " set-stream-use-case-override <usecase1> <usecase2> ... override stream use cases\n" |
| 6779 | " Use cases applied in descending resolutions. So usecase1 is assigned to the\n" |
| 6780 | " largest resolution, usecase2 is assigned to the 2nd largest resolution, and so\n" |
| 6781 | " on. In case the number of usecases is smaller than the number of streams, the\n" |
| 6782 | " last use case is assigned to all the remaining streams. In case of multiple\n" |
| 6783 | " streams with the same resolution, the tie-breaker is (JPEG, RAW, YUV, and PRIV)\n" |
| 6784 | " Valid values are (case sensitive): DEFAULT, PREVIEW, STILL_CAPTURE, VIDEO_RECORD,\n" |
| 6785 | " PREVIEW_VIDEO_STILL, VIDEO_CALL, CROPPED_RAW\n" |
| 6786 | " clear-stream-use-case-override clear the stream use case override\n" |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 6787 | " set-zoom-override <-1/0/1> enable or disable zoom override\n" |
| 6788 | " 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] | 6789 | " set-watchdog <VALUE> enables or disables the camera service watchdog\n" |
| 6790 | " Valid values 0=disable, 1=enable\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 6791 | " 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] | 6792 | " help print this message\n"); |
| 6793 | } |
| 6794 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 6795 | bool CameraService::isClientWatched(const BasicClient *client) { |
| 6796 | Mutex::Autolock lock(mLogLock); |
| 6797 | return isClientWatchedLocked(client); |
| 6798 | } |
| 6799 | |
| 6800 | bool CameraService::isClientWatchedLocked(const BasicClient *client) { |
| 6801 | return mWatchedClientPackages.find(kWatchAllClientsFlag) != mWatchedClientPackages.end() || |
| 6802 | mWatchedClientPackages.find(client->getPackageName()) != mWatchedClientPackages.end(); |
| 6803 | } |
| 6804 | |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 6805 | int32_t CameraService::updateAudioRestriction() { |
| 6806 | Mutex::Autolock lock(mServiceLock); |
| 6807 | return updateAudioRestrictionLocked(); |
| 6808 | } |
| 6809 | |
| 6810 | int32_t CameraService::updateAudioRestrictionLocked() { |
| 6811 | int32_t mode = 0; |
| 6812 | // iterate through all active client |
| 6813 | for (const auto& i : mActiveClientManager.getAll()) { |
| 6814 | const auto clientSp = i->getValue(); |
| 6815 | mode |= clientSp->getAudioRestriction(); |
| 6816 | } |
| 6817 | |
| 6818 | bool modeChanged = (mAudioRestriction != mode); |
| 6819 | mAudioRestriction = mode; |
| 6820 | if (modeChanged) { |
| 6821 | mAppOps.setCameraAudioRestriction(mode); |
| 6822 | } |
| 6823 | return mode; |
| 6824 | } |
| 6825 | |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6826 | status_t CameraService::checkIfInjectionCameraIsPresent(const std::string& externalCamId, |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6827 | sp<BasicClient> clientSp) { |
| 6828 | std::unique_ptr<AutoConditionLock> lock = |
| 6829 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 6830 | status_t res = NO_ERROR; |
| 6831 | if ((res = checkIfDeviceIsUsable(externalCamId)) != NO_ERROR) { |
Austin Borger | ed99f64 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 6832 | ALOGW("Device %s is not usable!", externalCamId.c_str()); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6833 | mInjectionStatusListener->notifyInjectionError( |
| 6834 | externalCamId, UNKNOWN_TRANSACTION); |
| 6835 | clientSp->notifyError( |
| 6836 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 6837 | CaptureResultExtras()); |
| 6838 | |
| 6839 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 6840 | // other clients from connecting in mServiceLockWrapper if held |
| 6841 | mServiceLock.unlock(); |
| 6842 | |
| 6843 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 6844 | int64_t token = clearCallingIdentity(); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6845 | clientSp->disconnect(); |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 6846 | restoreCallingIdentity(token); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6847 | |
| 6848 | // Reacquire mServiceLock |
| 6849 | mServiceLock.lock(); |
| 6850 | } |
| 6851 | |
| 6852 | return res; |
| 6853 | } |
| 6854 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 6855 | void CameraService::clearInjectionParameters() { |
| 6856 | { |
| 6857 | Mutex::Autolock lock(mInjectionParametersLock); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6858 | mInjectionInitPending = false; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 6859 | mInjectionInternalCamId = ""; |
| 6860 | } |
| 6861 | mInjectionExternalCamId = ""; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 6862 | mInjectionStatusListener->removeListener(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 6863 | } |
| 6864 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 6865 | } // namespace android |