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> |
| 27 | #include <string> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 28 | #include <sys/types.h> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 29 | #include <inttypes.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 30 | #include <pthread.h> |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 31 | #include <poll.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 32 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 33 | #include <android/hardware/ICamera.h> |
| 34 | #include <android/hardware/ICameraClient.h> |
| 35 | |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 36 | #include <aidl/AidlCameraService.h> |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 37 | #include <android-base/macros.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 38 | #include <android-base/parseint.h> |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 39 | #include <android-base/stringprintf.h> |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 40 | #include <android/permission/PermissionChecker.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 41 | #include <binder/ActivityManager.h> |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 42 | #include <binder/AppOpsManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 43 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 44 | #include <binder/MemoryBase.h> |
| 45 | #include <binder/MemoryHeapBase.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 46 | #include <binder/PermissionController.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 47 | #include <binder/IResultReceiver.h> |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 48 | #include <binderthreadstate/CallerUtils.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 49 | #include <cutils/atomic.h> |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 50 | #include <cutils/properties.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 51 | #include <cutils/misc.h> |
Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 52 | #include <gui/Surface.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 53 | #include <hardware/hardware.h> |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 54 | #include "hidl/HidlCameraService.h" |
| 55 | #include <hidl/HidlTransportSupport.h> |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 56 | #include <hwbinder/IPCThreadState.h> |
Eino-Ville Talvala | d89821e | 2016-04-20 11:23:50 -0700 | [diff] [blame] | 57 | #include <memunreachable/memunreachable.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 | #include <media/AudioSystem.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 59 | #include <media/IMediaHTTPService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 60 | #include <media/mediaplayer.h> |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 61 | #include <mediautils/BatteryNotifier.h> |
Steven Moreland | 886d732 | 2021-04-02 04:19:45 +0000 | [diff] [blame] | 62 | #include <processinfo/ProcessInfoService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 63 | #include <utils/Errors.h> |
| 64 | #include <utils/Log.h> |
| 65 | #include <utils/String16.h> |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 66 | #include <utils/SystemClock.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 67 | #include <utils/Trace.h> |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 68 | #include <utils/CallStack.h> |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 69 | #include <private/android_filesystem_config.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 70 | #include <system/camera_vendor_tags.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 71 | #include <system/camera_metadata.h> |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 72 | #include <binder/IServiceManager.h> |
| 73 | #include <binder/IActivityManager.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 74 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 75 | #include <system/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 76 | |
| 77 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 78 | #include "api1/Camera2Client.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 79 | #include "api2/CameraDeviceClient.h" |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 80 | #include "utils/CameraTraces.h" |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 81 | #include "utils/TagMonitor.h" |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 82 | #include "utils/CameraThreadState.h" |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 83 | #include "utils/CameraServiceProxyWrapper.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 84 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 85 | namespace { |
| 86 | const char* kPermissionServiceName = "permission"; |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 87 | const char* kActivityServiceName = "activity"; |
| 88 | const char* kSensorPrivacyServiceName = "sensor_privacy"; |
| 89 | const char* kAppopsServiceName = "appops"; |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame^] | 90 | const char* kProcessInfoServiceName = "processinfo"; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 91 | }; // namespace anonymous |
| 92 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 93 | namespace android { |
| 94 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 95 | using base::StringPrintf; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 96 | using binder::Status; |
Austin Borger | ea93124 | 2021-12-13 23:10:41 +0000 | [diff] [blame] | 97 | using namespace camera3; |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 98 | using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService; |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 99 | using frameworks::cameraservice::service::implementation::AidlCameraService; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 100 | using hardware::ICamera; |
| 101 | using hardware::ICameraClient; |
| 102 | using hardware::ICameraServiceListener; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 103 | using hardware::camera2::ICameraInjectionCallback; |
| 104 | using hardware::camera2::ICameraInjectionSession; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 105 | using hardware::camera2::utils::CameraIdAndSessionConfiguration; |
| 106 | using hardware::camera2::utils::ConcurrentCameraIdCombination; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 107 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 108 | // ---------------------------------------------------------------------------- |
| 109 | // Logging support -- this is for debugging only |
| 110 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 111 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 112 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 113 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 114 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 115 | |
| 116 | static void setLogLevel(int level) { |
| 117 | android_atomic_write(level, &gLogLevel); |
| 118 | } |
| 119 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 120 | // Convenience methods for constructing binder::Status objects for error returns |
| 121 | |
| 122 | #define STATUS_ERROR(errorCode, errorString) \ |
| 123 | binder::Status::fromServiceSpecificError(errorCode, \ |
| 124 | String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString)) |
| 125 | |
| 126 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ |
| 127 | binder::Status::fromServiceSpecificError(errorCode, \ |
| 128 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \ |
| 129 | __VA_ARGS__)) |
| 130 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 131 | // ---------------------------------------------------------------------------- |
| 132 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 133 | static const String16 sDumpPermission("android.permission.DUMP"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 134 | static const String16 sManageCameraPermission("android.permission.MANAGE_CAMERA"); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 135 | static const String16 sCameraPermission("android.permission.CAMERA"); |
| 136 | static const String16 sSystemCameraPermission("android.permission.SYSTEM_CAMERA"); |
| 137 | static const String16 |
| 138 | sCameraSendSystemEventsPermission("android.permission.CAMERA_SEND_SYSTEM_EVENTS"); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 139 | static const String16 sCameraOpenCloseListenerPermission( |
| 140 | "android.permission.CAMERA_OPEN_CLOSE_LISTENER"); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 141 | static const String16 |
| 142 | sCameraInjectExternalCameraPermission("android.permission.CAMERA_INJECT_EXTERNAL_CAMERA"); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 143 | // Constant integer for FGS Logging, used to denote the API type for logger |
| 144 | static const int LOG_FGS_CAMERA_API = 1; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 145 | const char *sFileName = "lastOpenSessionDumpFile"; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 146 | static constexpr int32_t kSystemNativeClientScore = resource_policy::PERCEPTIBLE_APP_ADJ; |
| 147 | static constexpr int32_t kSystemNativeClientState = |
| 148 | ActivityManager::PROCESS_STATE_PERSISTENT_UI; |
Austin Borger | b251892 | 2023-05-05 15:50:30 -0700 | [diff] [blame] | 149 | static const String16 kServiceName("cameraserver"); |
Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame] | 150 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 151 | const String8 CameraService::kOfflineDevice("offline-"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 152 | const String16 CameraService::kWatchAllClientsFlag("all"); |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 153 | |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 154 | // Set to keep track of logged service error events. |
| 155 | static std::set<String8> sServiceErrorEventSet; |
| 156 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 157 | CameraService::CameraService( |
| 158 | std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper) : |
| 159 | mCameraServiceProxyWrapper(cameraServiceProxyWrapper == nullptr ? |
| 160 | std::make_shared<CameraServiceProxyWrapper>() : cameraServiceProxyWrapper), |
Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 161 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 162 | mNumberOfCameras(0), |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 163 | mNumberOfCamerasWithoutSystemCamera(0), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 164 | mSoundRef(0), mInitialized(false), |
| 165 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 166 | ALOGI("CameraService started (pid=%d)", getpid()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 167 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 168 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 169 | if (mMemFd == -1) { |
| 170 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 171 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 174 | // The word 'System' here does not refer to clients only on the system |
| 175 | // partition. They just need to have a android system uid. |
| 176 | static bool doesClientHaveSystemUid() { |
| 177 | return (CameraThreadState::getCallingUid() < AID_APP_START); |
| 178 | } |
| 179 | |
Charles Chen | a7b613c | 2023-01-24 21:57:33 +0000 | [diff] [blame] | 180 | // Enable processes with isolated AID to request the binder |
| 181 | void CameraService::instantiate() { |
| 182 | CameraService::publish(true); |
| 183 | } |
| 184 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 185 | void CameraService::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
| 186 | if (name != String16(kAppopsServiceName)) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | ALOGV("appops service registered. setting camera audio restriction"); |
| 191 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
| 192 | } |
| 193 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 194 | void CameraService::onFirstRef() |
| 195 | { |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 196 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 197 | ALOGI("CameraService process starting"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 198 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 199 | BnCameraService::onFirstRef(); |
| 200 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 201 | // Update battery life tracking if service is restarting |
| 202 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 203 | notifier.noteResetCamera(); |
| 204 | notifier.noteResetFlashlight(); |
| 205 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 206 | status_t res = INVALID_OPERATION; |
Eino-Ville Talvala | 9cbbc83 | 2017-01-23 15:39:53 -0800 | [diff] [blame] | 207 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 208 | res = enumerateProviders(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 209 | if (res == OK) { |
| 210 | mInitialized = true; |
| 211 | } |
| 212 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 213 | mUidPolicy = new UidPolicy(this); |
| 214 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 215 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this); |
| 216 | mSensorPrivacyPolicy->registerSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 217 | mInjectionStatusListener = new InjectionStatusListener(this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 218 | |
| 219 | // appops function setCamerAudioRestriction uses getService which |
| 220 | // is blocking till the appops service is ready. To enable early |
| 221 | // boot availability for cameraservice, use checkService which is |
| 222 | // non blocking and register for notifications |
| 223 | sp<IServiceManager> sm = defaultServiceManager(); |
| 224 | sp<IBinder> binder = sm->checkService(String16(kAppopsServiceName)); |
| 225 | if (!binder) { |
| 226 | sm->registerForNotifications(String16(kAppopsServiceName), this); |
| 227 | } else { |
| 228 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
| 229 | } |
| 230 | |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 231 | sp<HidlCameraService> hcs = HidlCameraService::getInstance(this); |
| 232 | if (hcs->registerAsService() != android::OK) { |
Devin Moore | a1350e7 | 2023-01-11 23:40:42 +0000 | [diff] [blame] | 233 | // Deprecated, so it will fail to register on newer devices |
| 234 | ALOGW("%s: Did not register default android.frameworks.cameraservice.service@2.2", |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 235 | __FUNCTION__); |
| 236 | } |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 237 | |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 238 | if (!AidlCameraService::registerService(this)) { |
| 239 | ALOGE("%s: Failed to register default AIDL VNDK CameraService", __FUNCTION__); |
| 240 | } |
| 241 | |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 242 | // This needs to be last call in this function, so that it's as close to |
| 243 | // ServiceManager::addService() as possible. |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 244 | mCameraServiceProxyWrapper->pingCameraServiceProxy(); |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 245 | ALOGI("CameraService pinged cameraservice proxy"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 248 | status_t CameraService::enumerateProviders() { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 249 | status_t res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 250 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 251 | std::vector<std::string> deviceIds; |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 252 | std::unordered_map<std::string, std::set<std::string>> unavailPhysicalIds; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 253 | { |
| 254 | Mutex::Autolock l(mServiceLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 255 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 256 | if (nullptr == mCameraProviderManager.get()) { |
| 257 | mCameraProviderManager = new CameraProviderManager(); |
| 258 | res = mCameraProviderManager->initialize(this); |
| 259 | if (res != OK) { |
| 260 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", |
| 261 | __FUNCTION__, strerror(-res), res); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 262 | logServiceError(String8::format("Unable to initialize camera provider manager"), |
| 263 | ERROR_DISCONNECTED); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 264 | return res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 268 | |
| 269 | // Setup vendor tags before we call get_camera_info the first time |
| 270 | // because HAL might need to setup static vendor keys in get_camera_info |
| 271 | // TODO: maybe put this into CameraProviderManager::initialize()? |
| 272 | mCameraProviderManager->setUpVendorTags(); |
| 273 | |
| 274 | if (nullptr == mFlashlight.get()) { |
| 275 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 278 | res = mFlashlight->findFlashUnits(); |
| 279 | if (res != OK) { |
| 280 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); |
| 281 | } |
| 282 | |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 283 | deviceIds = mCameraProviderManager->getCameraDeviceIds(&unavailPhysicalIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
| 287 | for (auto& cameraId : deviceIds) { |
| 288 | String8 id8 = String8(cameraId.c_str()); |
Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 289 | if (getCameraState(id8) == nullptr) { |
| 290 | onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT); |
| 291 | } |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 292 | if (unavailPhysicalIds.count(cameraId) > 0) { |
| 293 | for (const auto& physicalId : unavailPhysicalIds[cameraId]) { |
| 294 | String8 physicalId8 = String8(physicalId.c_str()); |
| 295 | onDeviceStatusChanged(id8, physicalId8, CameraDeviceStatus::NOT_PRESENT); |
| 296 | } |
| 297 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 300 | // Derive primary rear/front cameras, and filter their charactierstics. |
| 301 | // 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] | 302 | if (SessionConfigurationUtils::IS_PERF_CLASS) { |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 303 | // Assume internal cameras are advertised from the same |
| 304 | // provider. If multiple providers are registered at different time, |
| 305 | // and each provider contains multiple internal color cameras, the current |
| 306 | // logic may filter the characteristics of more than one front/rear color |
| 307 | // cameras. |
| 308 | Mutex::Autolock l(mServiceLock); |
| 309 | filterSPerfClassCharacteristicsLocked(); |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 310 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 311 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 312 | return OK; |
| 313 | } |
| 314 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 315 | void CameraService::broadcastTorchModeStatus(const String8& cameraId, TorchModeStatus status, |
| 316 | SystemCameraKind systemCameraKind) { |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 317 | Mutex::Autolock lock(mStatusListenerLock); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 318 | for (auto& i : mListenerList) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 319 | if (shouldSkipStatusUpdates(systemCameraKind, i->isVendorListener(), i->getListenerPid(), |
| 320 | i->getListenerUid())) { |
| 321 | ALOGV("Skipping torch callback for system-only camera device %s", |
| 322 | cameraId.c_str()); |
| 323 | continue; |
| 324 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 325 | auto ret = i->getListener()->onTorchStatusChanged(mapToInterface(status), |
| 326 | String16{cameraId}); |
| 327 | i->handleBinderStatus(ret, "%s: Failed to trigger onTorchStatusChanged for %d:%d: %d", |
| 328 | __FUNCTION__, i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 332 | CameraService::~CameraService() { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 333 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 334 | mUidPolicy->unregisterSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 335 | mSensorPrivacyPolicy->unregisterSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 336 | mInjectionStatusListener->removeListener(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 339 | void CameraService::onNewProviderRegistered() { |
| 340 | enumerateProviders(); |
| 341 | } |
| 342 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 343 | void CameraService::filterAPI1SystemCameraLocked( |
| 344 | const std::vector<std::string> &normalDeviceIds) { |
| 345 | mNormalDeviceIdsWithoutSystemCamera.clear(); |
| 346 | for (auto &deviceId : normalDeviceIds) { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 347 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 348 | if (getSystemCameraKind(String8(deviceId.c_str()), &deviceKind) != OK) { |
| 349 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, deviceId.c_str()); |
| 350 | continue; |
| 351 | } |
| 352 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 353 | // All system camera ids will necessarily come after public camera |
| 354 | // device ids as per the HAL interface contract. |
| 355 | break; |
| 356 | } |
| 357 | mNormalDeviceIdsWithoutSystemCamera.push_back(deviceId); |
| 358 | } |
| 359 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, |
| 360 | mNormalDeviceIdsWithoutSystemCamera.size()); |
| 361 | } |
| 362 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 363 | status_t CameraService::getSystemCameraKind(const String8& cameraId, SystemCameraKind *kind) const { |
| 364 | auto state = getCameraState(cameraId); |
| 365 | if (state != nullptr) { |
| 366 | *kind = state->getSystemCameraKind(); |
| 367 | return OK; |
| 368 | } |
| 369 | // Hidden physical camera ids won't have CameraState |
| 370 | return mCameraProviderManager->getSystemCameraKind(cameraId.c_str(), kind); |
| 371 | } |
| 372 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 373 | void CameraService::updateCameraNumAndIds() { |
| 374 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 375 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); |
| 376 | // Excludes hidden secure cameras |
| 377 | mNumberOfCameras = |
| 378 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; |
| 379 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 380 | mNormalDeviceIds = |
| 381 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 382 | filterAPI1SystemCameraLocked(mNormalDeviceIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 385 | void CameraService::filterSPerfClassCharacteristicsLocked() { |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 386 | // To claim to be S Performance primary cameras, the cameras must be |
| 387 | // backward compatible. So performance class primary camera Ids must be API1 |
| 388 | // compatible. |
| 389 | bool firstRearCameraSeen = false, firstFrontCameraSeen = false; |
| 390 | for (const auto& cameraId : mNormalDeviceIdsWithoutSystemCamera) { |
| 391 | int facing = -1; |
| 392 | int orientation = 0; |
| 393 | String8 cameraId8(cameraId.c_str()); |
Shuzhen Wang | f221e8d | 2022-12-15 13:26:29 -0800 | [diff] [blame] | 394 | int portraitRotation; |
| 395 | getDeviceVersion(cameraId8, /*overrideToPortrait*/false, /*out*/&portraitRotation, |
| 396 | /*out*/&facing, /*out*/&orientation); |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 397 | if (facing == -1) { |
| 398 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.c_str()); |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | if ((facing == hardware::CAMERA_FACING_BACK && !firstRearCameraSeen) || |
| 403 | (facing == hardware::CAMERA_FACING_FRONT && !firstFrontCameraSeen)) { |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 404 | status_t res = mCameraProviderManager->filterSmallJpegSizes(cameraId); |
| 405 | if (res == OK) { |
| 406 | mPerfClassPrimaryCameraIds.insert(cameraId); |
| 407 | } else { |
| 408 | ALOGE("%s: Failed to filter small JPEG sizes for performance class primary " |
| 409 | "camera %s: %s(%d)", __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
| 410 | break; |
| 411 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 412 | |
| 413 | if (facing == hardware::CAMERA_FACING_BACK) { |
| 414 | firstRearCameraSeen = true; |
| 415 | } |
| 416 | if (facing == hardware::CAMERA_FACING_FRONT) { |
| 417 | firstFrontCameraSeen = true; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (firstRearCameraSeen && firstFrontCameraSeen) { |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 427 | void CameraService::addStates(const String8 id) { |
| 428 | std::string cameraId(id.c_str()); |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 429 | CameraResourceCost cost; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 430 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); |
| 431 | if (res != OK) { |
| 432 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); |
| 433 | return; |
| 434 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 435 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 436 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); |
| 437 | if (res != OK) { |
| 438 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); |
| 439 | return; |
| 440 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 441 | std::vector<std::string> physicalCameraIds; |
| 442 | mCameraProviderManager->isLogicalCamera(cameraId, &physicalCameraIds); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 443 | std::set<String8> conflicting; |
| 444 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { |
| 445 | conflicting.emplace(String8(cost.conflictingDevices[i].c_str())); |
| 446 | } |
| 447 | |
| 448 | { |
| 449 | Mutex::Autolock lock(mCameraStatesLock); |
| 450 | mCameraStates.emplace(id, std::make_shared<CameraState>(id, cost.resourceCost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 451 | conflicting, deviceKind, physicalCameraIds)); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 455 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 456 | mTorchStatusMap.add(id, TorchModeStatus::AVAILABLE_OFF); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 457 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 458 | broadcastTorchModeStatus(id, TorchModeStatus::AVAILABLE_OFF, deviceKind); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 459 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 460 | |
| 461 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 462 | logDeviceAdded(id, "Device added"); |
| 463 | } |
| 464 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 465 | void CameraService::removeStates(const String8 id) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 466 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 467 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 468 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 469 | mTorchStatusMap.removeItem(id); |
| 470 | } |
| 471 | |
| 472 | { |
| 473 | Mutex::Autolock lock(mCameraStatesLock); |
| 474 | mCameraStates.erase(id); |
| 475 | } |
| 476 | } |
| 477 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 478 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 479 | CameraDeviceStatus newHalStatus) { |
| 480 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, |
| 481 | id.string(), newHalStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 482 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 483 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 484 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 485 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 486 | |
| 487 | if (state == nullptr) { |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 488 | if (newStatus == StatusInternal::PRESENT) { |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 489 | ALOGI("%s: Unknown camera ID %s, a new camera is added", |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 490 | __FUNCTION__, id.string()); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 491 | |
| 492 | // First add as absent to make sure clients are notified below |
| 493 | addStates(id); |
| 494 | |
| 495 | updateStatus(newStatus, id); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 496 | } else { |
| 497 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string()); |
| 498 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 502 | StatusInternal oldStatus = state->getStatus(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 503 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 504 | if (oldStatus == newStatus) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 505 | ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 506 | return; |
| 507 | } |
| 508 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 509 | if (newStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 510 | logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 511 | newStatus)); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 512 | |
| 513 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 514 | // to this device until the status changes |
| 515 | updateStatus(StatusInternal::NOT_PRESENT, id); |
| 516 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 517 | sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 518 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 519 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 520 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 521 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 522 | // Remove cached shim parameters |
| 523 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 524 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 525 | // Remove online as well as offline client from the list of active clients, |
| 526 | // if they are present |
| 527 | clientToDisconnectOnline = removeClientLocked(id); |
| 528 | clientToDisconnectOffline = removeClientLocked(kOfflineDevice + id); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 529 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 530 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 531 | disconnectClient(id, clientToDisconnectOnline); |
| 532 | disconnectClient(kOfflineDevice + id, clientToDisconnectOffline); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 533 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 534 | removeStates(id); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 535 | } else { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 536 | if (oldStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 537 | logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 538 | newStatus)); |
| 539 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 540 | updateStatus(newStatus, id); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 541 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 542 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 543 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 544 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 545 | const String8& physicalId, |
| 546 | CameraDeviceStatus newHalStatus) { |
| 547 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", |
| 548 | __FUNCTION__, id.string(), physicalId.string(), newHalStatus); |
| 549 | |
| 550 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 551 | |
| 552 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 553 | |
| 554 | if (state == nullptr) { |
| 555 | ALOGE("%s: Physical camera id %s status change on a non-present ID %s", |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 556 | __FUNCTION__, physicalId.string(), id.string()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 557 | return; |
| 558 | } |
| 559 | |
| 560 | StatusInternal logicalCameraStatus = state->getStatus(); |
| 561 | if (logicalCameraStatus != StatusInternal::PRESENT && |
| 562 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 563 | ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d", |
| 564 | __FUNCTION__, physicalId.string(), newHalStatus, logicalCameraStatus); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | bool updated = false; |
| 569 | if (newStatus == StatusInternal::PRESENT) { |
| 570 | updated = state->removeUnavailablePhysicalId(physicalId); |
| 571 | } else { |
| 572 | updated = state->addUnavailablePhysicalId(physicalId); |
| 573 | } |
| 574 | |
| 575 | if (updated) { |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 576 | String8 idCombo = id + " : " + physicalId; |
| 577 | if (newStatus == StatusInternal::PRESENT) { |
| 578 | logDeviceAdded(idCombo, |
| 579 | String8::format("Device status changed to %d", newStatus)); |
| 580 | } else { |
| 581 | logDeviceRemoved(idCombo, |
| 582 | String8::format("Device status changed to %d", newStatus)); |
| 583 | } |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 584 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 585 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 586 | if (getSystemCameraKind(id, &deviceKind) != OK) { |
| 587 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.string()); |
| 588 | return; |
| 589 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 590 | String16 id16(id), physicalId16(physicalId); |
| 591 | Mutex::Autolock lock(mStatusListenerLock); |
| 592 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 593 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 594 | listener->getListenerPid(), listener->getListenerUid())) { |
| 595 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 596 | id.c_str()); |
| 597 | continue; |
| 598 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 599 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged( |
| 600 | mapToInterface(newStatus), id16, physicalId16); |
| 601 | listener->handleBinderStatus(ret, |
| 602 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 603 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 604 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 605 | } |
| 606 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 609 | void CameraService::disconnectClient(const String8& id, sp<BasicClient> clientToDisconnect) { |
| 610 | if (clientToDisconnect.get() != nullptr) { |
| 611 | ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL", |
| 612 | __FUNCTION__, id.string()); |
| 613 | // Notify the client of disconnection |
| 614 | clientToDisconnect->notifyError( |
| 615 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 616 | CaptureResultExtras{}); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 617 | clientToDisconnect->disconnect(); |
| 618 | } |
| 619 | } |
| 620 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 621 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 622 | TorchModeStatus newStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 623 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 624 | status_t res = getSystemCameraKind(cameraId, &systemCameraKind); |
| 625 | if (res != OK) { |
| 626 | ALOGE("%s: Could not get system camera kind for camera id %s", __FUNCTION__, |
| 627 | cameraId.string()); |
| 628 | return; |
| 629 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 630 | Mutex::Autolock al(mTorchStatusMutex); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 631 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 632 | } |
| 633 | |
Jayant Chowdhary | 46ef0f5 | 2021-10-05 14:36:13 -0700 | [diff] [blame] | 634 | |
| 635 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
| 636 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
| 637 | Mutex::Autolock al(mTorchStatusMutex); |
| 638 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
| 639 | } |
| 640 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 641 | void CameraService::broadcastTorchStrengthLevel(const String8& cameraId, |
| 642 | int32_t newStrengthLevel) { |
| 643 | Mutex::Autolock lock(mStatusListenerLock); |
| 644 | for (auto& i : mListenerList) { |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 645 | auto ret = i->getListener()->onTorchStrengthLevelChanged(String16{cameraId}, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 646 | newStrengthLevel); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 647 | i->handleBinderStatus(ret, |
| 648 | "%s: Failed to trigger onTorchStrengthLevelChanged for %d:%d: %d", __FUNCTION__, |
| 649 | i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 653 | void CameraService::onTorchStatusChangedLocked(const String8& cameraId, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 654 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 655 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
| 656 | __FUNCTION__, cameraId.string(), newStatus); |
| 657 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 658 | TorchModeStatus status; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 659 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 660 | if (res) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 661 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", |
| 662 | __FUNCTION__, cameraId.string(), strerror(-res), res); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 663 | return; |
| 664 | } |
| 665 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 666 | return; |
| 667 | } |
| 668 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 669 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 670 | if (res) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 671 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, |
| 672 | (uint32_t)newStatus, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 673 | return; |
| 674 | } |
| 675 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 676 | { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 677 | // Update battery life logging for flashlight |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 678 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 679 | auto iter = mTorchUidMap.find(cameraId); |
| 680 | if (iter != mTorchUidMap.end()) { |
| 681 | int oldUid = iter->second.second; |
| 682 | int newUid = iter->second.first; |
| 683 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 684 | if (oldUid != newUid) { |
| 685 | // 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] | 686 | if (status == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 687 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 688 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 689 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 690 | notifier.noteFlashlightOn(cameraId, newUid); |
| 691 | } |
| 692 | iter->second.second = newUid; |
| 693 | } else { |
| 694 | // If the UID has not changed, log the status |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 695 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 696 | notifier.noteFlashlightOn(cameraId, oldUid); |
| 697 | } else { |
| 698 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 703 | broadcastTorchModeStatus(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 704 | } |
| 705 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 706 | static bool isAutomotiveDevice() { |
| 707 | // Checks the property ro.hardware.type and returns true if it is |
| 708 | // automotive. |
| 709 | char value[PROPERTY_VALUE_MAX] = {0}; |
| 710 | property_get("ro.hardware.type", value, ""); |
| 711 | return strncmp(value, "automotive", PROPERTY_VALUE_MAX) == 0; |
| 712 | } |
| 713 | |
| 714 | static bool isAutomotivePrivilegedClient(int32_t uid) { |
| 715 | // Returns false if this is not an automotive device type. |
| 716 | if (!isAutomotiveDevice()) |
| 717 | return false; |
| 718 | |
| 719 | // Returns true if the uid is AID_AUTOMOTIVE_EVS which is a |
| 720 | // privileged client uid used for safety critical use cases such as |
| 721 | // rear view and surround view. |
| 722 | return uid == AID_AUTOMOTIVE_EVS; |
| 723 | } |
| 724 | |
| 725 | bool CameraService::isAutomotiveExteriorSystemCamera(const String8& cam_id) const{ |
| 726 | // Returns false if this is not an automotive device type. |
| 727 | if (!isAutomotiveDevice()) |
| 728 | return false; |
| 729 | |
| 730 | // Returns false if no camera id is provided. |
| 731 | if (cam_id.isEmpty()) |
| 732 | return false; |
| 733 | |
| 734 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 735 | if (getSystemCameraKind(cam_id, &systemCameraKind) != OK) { |
| 736 | // This isn't a known camera ID, so it's not a system camera. |
| 737 | ALOGE("%s: Unknown camera id %s, ", __FUNCTION__, cam_id.c_str()); |
| 738 | return false; |
| 739 | } |
| 740 | |
| 741 | if (systemCameraKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
| 742 | ALOGE("%s: camera id %s is not a system camera", __FUNCTION__, cam_id.c_str()); |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | CameraMetadata cameraInfo; |
| 747 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
| 748 | cam_id.string(), false, &cameraInfo, false); |
| 749 | if (res != OK){ |
| 750 | ALOGE("%s: Not able to get camera characteristics for camera id %s",__FUNCTION__, |
| 751 | cam_id.c_str()); |
| 752 | return false; |
| 753 | } |
| 754 | |
| 755 | camera_metadata_entry auto_location = cameraInfo.find(ANDROID_AUTOMOTIVE_LOCATION); |
| 756 | if (auto_location.count != 1) |
| 757 | return false; |
| 758 | |
| 759 | uint8_t location = auto_location.data.u8[0]; |
| 760 | if ((location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_FRONT) && |
| 761 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_REAR) && |
| 762 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_LEFT) && |
| 763 | (location != ANDROID_AUTOMOTIVE_LOCATION_EXTERIOR_RIGHT)) { |
| 764 | return false; |
| 765 | } |
| 766 | |
| 767 | return true; |
| 768 | } |
| 769 | |
| 770 | bool CameraService::checkPermission(const String8& cam_id, const String16& permission, |
| 771 | const AttributionSourceState& attributionSource,const String16& message, |
| 772 | int32_t attributedOpCode) const{ |
| 773 | if (isAutomotivePrivilegedClient(attributionSource.uid)) { |
| 774 | // If cam_id is empty, then it means that this check is not used for the |
| 775 | // purpose of accessing a specific camera, hence grant permission just |
| 776 | // based on uid to the automotive privileged client. |
| 777 | if (cam_id.isEmpty()) |
| 778 | return true; |
| 779 | // If this call is used for accessing a specific camera then cam_id must be provided. |
| 780 | // In that case, only pre-grants the permission for accessing the exterior system only |
| 781 | // camera. |
| 782 | return isAutomotiveExteriorSystemCamera(cam_id); |
| 783 | |
| 784 | } |
| 785 | |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 786 | permission::PermissionChecker permissionChecker; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 787 | return permissionChecker.checkPermissionForPreflight(permission, attributionSource, |
| 788 | message, attributedOpCode) != permission::PermissionChecker::PERMISSION_HARD_DENIED; |
| 789 | } |
| 790 | |
| 791 | bool CameraService::hasPermissionsForSystemCamera(const String8& cam_id, int callingPid, |
| 792 | int callingUid) const{ |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 793 | AttributionSourceState attributionSource{}; |
| 794 | attributionSource.pid = callingPid; |
| 795 | attributionSource.uid = callingUid; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 796 | bool checkPermissionForSystemCamera = checkPermission(cam_id, |
| 797 | sSystemCameraPermission, attributionSource, String16(), AppOpsManager::OP_NONE); |
| 798 | bool checkPermissionForCamera = checkPermission(cam_id, |
| 799 | sCameraPermission, attributionSource, String16(), AppOpsManager::OP_NONE); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 800 | return checkPermissionForSystemCamera && checkPermissionForCamera; |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 803 | Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 804 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 805 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 806 | bool hasSystemCameraPermissions = |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 807 | hasPermissionsForSystemCamera(String8(), CameraThreadState::getCallingPid(), |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 808 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 809 | switch (type) { |
| 810 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 811 | if (hasSystemCameraPermissions) { |
| 812 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); |
| 813 | } else { |
| 814 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); |
| 815 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 816 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 817 | case CAMERA_TYPE_ALL: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 818 | if (hasSystemCameraPermissions) { |
| 819 | *numCameras = mNumberOfCameras; |
| 820 | } else { |
| 821 | *numCameras = mNumberOfCamerasWithoutSystemCamera; |
| 822 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 823 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 824 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 825 | ALOGW("%s: Unknown camera type %d", |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 826 | __FUNCTION__, type); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 827 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 828 | "Unknown camera type %d", type); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 829 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 830 | return Status::ok(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 833 | Status CameraService::getCameraInfo(int cameraId, bool overrideToPortrait, |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 834 | CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 835 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 836 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 837 | std::string cameraIdStr = cameraIdIntToStrLocked(cameraId); |
| 838 | if (shouldRejectSystemCameraConnection(String8(cameraIdStr.c_str()))) { |
| 839 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 840 | "characteristics for system only device %s: ", cameraIdStr.c_str()); |
| 841 | } |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 842 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 843 | if (!mInitialized) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 844 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 845 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 846 | "Camera subsystem is not available"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 847 | } |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 848 | bool hasSystemCameraPermissions = hasPermissionsForSystemCamera(String8::format("%d", cameraId), |
| 849 | CameraThreadState::getCallingPid(), CameraThreadState::getCallingUid()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 850 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; |
| 851 | if (hasSystemCameraPermissions) { |
| 852 | cameraIdBound = mNumberOfCameras; |
| 853 | } |
| 854 | if (cameraId < 0 || cameraId >= cameraIdBound) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 855 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 856 | "CameraId is not valid"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 859 | Status ret = Status::ok(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 860 | int portraitRotation; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 861 | status_t err = mCameraProviderManager->getCameraInfo( |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 862 | cameraIdStr.c_str(), overrideToPortrait, &portraitRotation, cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 863 | if (err != OK) { |
| 864 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 865 | "Error retrieving camera info from device %d: %s (%d)", cameraId, |
| 866 | strerror(-err), err); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 867 | logServiceError(String8::format("Error retrieving camera info from device %d",cameraId), |
| 868 | ERROR_INVALID_OPERATION); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 869 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 870 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 871 | return ret; |
| 872 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 873 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 874 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 875 | const std::vector<std::string> *deviceIds = &mNormalDeviceIdsWithoutSystemCamera; |
| 876 | auto callingPid = CameraThreadState::getCallingPid(); |
| 877 | auto callingUid = CameraThreadState::getCallingUid(); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 878 | AttributionSourceState attributionSource{}; |
| 879 | attributionSource.pid = callingPid; |
| 880 | attributionSource.uid = callingUid; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 881 | bool checkPermissionForSystemCamera = checkPermission(String8::format("%d", cameraIdInt), |
| 882 | sSystemCameraPermission, attributionSource, String16(), AppOpsManager::OP_NONE); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 883 | if (checkPermissionForSystemCamera || getpid() == callingPid) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 884 | deviceIds = &mNormalDeviceIds; |
| 885 | } |
| 886 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(deviceIds->size())) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 887 | ALOGE("%s: input id %d invalid: valid range (0, %zu)", |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 888 | __FUNCTION__, cameraIdInt, deviceIds->size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 889 | return std::string{}; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 890 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 891 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 892 | return (*deviceIds)[cameraIdInt]; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | String8 CameraService::cameraIdIntToStr(int cameraIdInt) { |
| 896 | Mutex::Autolock lock(mServiceLock); |
| 897 | return String8(cameraIdIntToStrLocked(cameraIdInt).c_str()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | Status CameraService::getCameraCharacteristics(const String16& cameraId, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 901 | int targetSdkVersion, bool overrideToPortrait, CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 902 | ATRACE_CALL(); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 903 | if (!cameraInfo) { |
| 904 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 905 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 908 | if (!mInitialized) { |
| 909 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 910 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 911 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 912 | "Camera subsystem is not available");; |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 915 | if (shouldRejectSystemCameraConnection(String8(cameraId))) { |
| 916 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 917 | "characteristics for system only device %s: ", String8(cameraId).string()); |
| 918 | } |
| 919 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 920 | Status ret{}; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 921 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 922 | |
| 923 | std::string cameraIdStr = String8(cameraId).string(); |
| 924 | bool overrideForPerfClass = |
| 925 | SessionConfigurationUtils::targetPerfClassPrimaryCamera(mPerfClassPrimaryCameraIds, |
| 926 | cameraIdStr, targetSdkVersion); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 927 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 928 | cameraIdStr, overrideForPerfClass, cameraInfo, overrideToPortrait); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 929 | if (res != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 930 | if (res == NAME_NOT_FOUND) { |
| 931 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " |
| 932 | "characteristics for unknown device %s: %s (%d)", String8(cameraId).string(), |
| 933 | strerror(-res), res); |
| 934 | } else { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 935 | logServiceError(String8::format("Unable to retrieve camera characteristics for " |
| 936 | "device %s.", String8(cameraId).string()),ERROR_INVALID_OPERATION); |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 937 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " |
| 938 | "characteristics for device %s: %s (%d)", String8(cameraId).string(), |
| 939 | strerror(-res), res); |
| 940 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 941 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 942 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 943 | if (getSystemCameraKind(String8(cameraId), &deviceKind) != OK) { |
| 944 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, String8(cameraId).string()); |
| 945 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera kind " |
| 946 | "for device %s", String8(cameraId).string()); |
| 947 | } |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 948 | int callingPid = CameraThreadState::getCallingPid(); |
| 949 | int callingUid = CameraThreadState::getCallingUid(); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 950 | std::vector<int32_t> tagsRemoved; |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 951 | // If it's not calling from cameraserver, check the permission only if |
| 952 | // android.permission.CAMERA is required. If android.permission.SYSTEM_CAMERA was needed, |
| 953 | // it would've already been checked in shouldRejectSystemCameraConnection. |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 954 | AttributionSourceState attributionSource{}; |
| 955 | attributionSource.pid = callingPid; |
| 956 | attributionSource.uid = callingUid; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 957 | bool checkPermissionForCamera = checkPermission(String8(cameraId), sCameraPermission, |
| 958 | attributionSource, String16(), AppOpsManager::OP_NONE); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 959 | if ((callingPid != getpid()) && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 960 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 961 | !checkPermissionForCamera) { |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 962 | res = cameraInfo->removePermissionEntries( |
| 963 | mCameraProviderManager->getProviderTagIdLocked(String8(cameraId).string()), |
| 964 | &tagsRemoved); |
| 965 | if (res != OK) { |
| 966 | cameraInfo->clear(); |
| 967 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to remove camera" |
| 968 | " characteristics needing camera permission for device %s: %s (%d)", |
| 969 | String8(cameraId).string(), strerror(-res), res); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | if (!tagsRemoved.empty()) { |
| 974 | res = cameraInfo->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, |
| 975 | tagsRemoved.data(), tagsRemoved.size()); |
| 976 | if (res != OK) { |
| 977 | cameraInfo->clear(); |
| 978 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to insert camera " |
| 979 | "keys needing permission for device %s: %s (%d)", String8(cameraId).string(), |
| 980 | strerror(-res), res); |
| 981 | } |
| 982 | } |
| 983 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 984 | return ret; |
| 985 | } |
| 986 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 987 | Status CameraService::getTorchStrengthLevel(const String16& cameraId, |
| 988 | int32_t* torchStrength) { |
| 989 | ATRACE_CALL(); |
| 990 | Mutex::Autolock l(mServiceLock); |
| 991 | if (!mInitialized) { |
| 992 | ALOGE("%s: Camera HAL couldn't be initialized.", __FUNCTION__); |
| 993 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera HAL couldn't be initialized."); |
| 994 | } |
| 995 | |
| 996 | if(torchStrength == NULL) { |
| 997 | ALOGE("%s: strength level must not be null.", __FUNCTION__); |
| 998 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Strength level should not be null."); |
| 999 | } |
| 1000 | |
| 1001 | status_t res = mCameraProviderManager->getTorchStrengthLevel(String8(cameraId).string(), |
| 1002 | torchStrength); |
| 1003 | if (res != OK) { |
| 1004 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve torch " |
| 1005 | "strength level for device %s: %s (%d)", String8(cameraId).string(), |
| 1006 | strerror(-res), res); |
| 1007 | } |
| 1008 | ALOGI("%s: Torch strength level is: %d", __FUNCTION__, *torchStrength); |
| 1009 | return Status::ok(); |
| 1010 | } |
| 1011 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1012 | String8 CameraService::getFormattedCurrentTime() { |
| 1013 | time_t now = time(nullptr); |
| 1014 | char formattedTime[64]; |
| 1015 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
| 1016 | return String8(formattedTime); |
| 1017 | } |
| 1018 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1019 | Status CameraService::getCameraVendorTagDescriptor( |
| 1020 | /*out*/ |
| 1021 | hardware::camera2::params::VendorTagDescriptor* desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1022 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1023 | if (!mInitialized) { |
| 1024 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1025 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 1026 | } |
Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 1027 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 1028 | if (globalDescriptor != nullptr) { |
| 1029 | *desc = *(globalDescriptor.get()); |
| 1030 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1031 | return Status::ok(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 1034 | Status CameraService::getCameraVendorTagCache( |
| 1035 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { |
| 1036 | ATRACE_CALL(); |
| 1037 | if (!mInitialized) { |
| 1038 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 1039 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1040 | "Camera subsystem not available"); |
| 1041 | } |
| 1042 | sp<VendorTagDescriptorCache> globalCache = |
| 1043 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 1044 | if (globalCache != nullptr) { |
| 1045 | *cache = *(globalCache.get()); |
| 1046 | } |
| 1047 | return Status::ok(); |
| 1048 | } |
| 1049 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 1050 | void CameraService::clearCachedVariables() { |
| 1051 | BasicClient::BasicClient::sCameraService = nullptr; |
| 1052 | } |
| 1053 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1054 | std::pair<int, IPCTransport> CameraService::getDeviceVersion(const String8& cameraId, |
| 1055 | bool overrideToPortrait, int* portraitRotation, int* facing, int* orientation) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1056 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1057 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1058 | int deviceVersion = 0; |
| 1059 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1060 | status_t res; |
| 1061 | hardware::hidl_version maxVersion{0,0}; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1062 | IPCTransport transport = IPCTransport::INVALID; |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1063 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(), |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1064 | &maxVersion, &transport); |
| 1065 | if (res != OK || transport == IPCTransport::INVALID) { |
| 1066 | ALOGE("%s: Unable to get highest supported version for camera id %s", __FUNCTION__, |
| 1067 | cameraId.string()); |
| 1068 | return std::make_pair(-1, IPCTransport::INVALID) ; |
| 1069 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1070 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1071 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1072 | hardware::CameraInfo info; |
| 1073 | if (facing) { |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1074 | res = mCameraProviderManager->getCameraInfo(cameraId.string(), overrideToPortrait, |
| 1075 | portraitRotation, &info); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1076 | if (res != OK) { |
| 1077 | return std::make_pair(-1, IPCTransport::INVALID); |
| 1078 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1079 | *facing = info.facing; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1080 | if (orientation) { |
| 1081 | *orientation = info.orientation; |
| 1082 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1083 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1084 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1085 | return std::make_pair(deviceVersion, transport); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1086 | } |
| 1087 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1088 | Status CameraService::filterGetInfoErrorCode(status_t err) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1089 | switch(err) { |
| 1090 | case NO_ERROR: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1091 | return Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1092 | case BAD_VALUE: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1093 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1094 | "CameraId is not valid for HAL module"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1095 | case NO_INIT: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1096 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1097 | "Camera device not available"); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1098 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1099 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1100 | "Camera HAL encountered error %d: %s", |
| 1101 | err, strerror(-err)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1102 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1105 | Status CameraService::makeClient(const sp<CameraService>& cameraService, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1106 | const sp<IInterface>& cameraCb, const String16& packageName, bool systemNativeClient, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1107 | const std::optional<String16>& featureId, const String8& cameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 1108 | int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid, |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1109 | int servicePid, std::pair<int, IPCTransport> deviceVersionAndTransport, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1110 | apiLevel effectiveApiLevel, bool overrideForPerfClass, bool overrideToPortrait, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1111 | bool forceSlowJpegMode, /*out*/sp<BasicClient>* client) { |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1112 | // For HIDL devices |
| 1113 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 1114 | // Create CameraClient based on device version reported by the HAL. |
| 1115 | int deviceVersion = deviceVersionAndTransport.first; |
| 1116 | switch(deviceVersion) { |
| 1117 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 1118 | ALOGE("Camera using old HAL version: %d", deviceVersion); |
| 1119 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, |
| 1120 | "Camera device \"%s\" HAL version %d no longer supported", |
| 1121 | cameraId.string(), deviceVersion); |
| 1122 | break; |
| 1123 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 1124 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 1125 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 1126 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 1127 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 1128 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 1129 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 1130 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 1131 | break; |
| 1132 | default: |
| 1133 | // Should not be reachable |
| 1134 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 1135 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1136 | "Camera device \"%s\" has unknown HAL version %d", |
| 1137 | cameraId.string(), deviceVersion); |
| 1138 | } |
| 1139 | } |
| 1140 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 1141 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1142 | *client = new Camera2Client(cameraService, tmp, cameraService->mCameraServiceProxyWrapper, |
| 1143 | packageName, featureId, cameraId, api1CameraId, facing, sensorOrientation, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1144 | clientPid, clientUid, servicePid, overrideForPerfClass, overrideToPortrait, |
| 1145 | forceSlowJpegMode); |
| 1146 | ALOGI("%s: Camera1 API (legacy), override to portrait %d, forceSlowJpegMode %d", |
| 1147 | __FUNCTION__, overrideToPortrait, forceSlowJpegMode); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1148 | } else { // Camera2 API route |
| 1149 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = |
| 1150 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1151 | *client = new CameraDeviceClient(cameraService, tmp, |
| 1152 | cameraService->mCameraServiceProxyWrapper, packageName, systemNativeClient, |
| 1153 | featureId, cameraId, facing, sensorOrientation, clientPid, clientUid, servicePid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1154 | overrideForPerfClass, overrideToPortrait); |
| 1155 | ALOGI("%s: Camera2 API, override to portrait %d", __FUNCTION__, overrideToPortrait); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1156 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1157 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1160 | String8 CameraService::toString(std::set<userid_t> intSet) { |
| 1161 | String8 s(""); |
| 1162 | bool first = true; |
| 1163 | for (userid_t i : intSet) { |
| 1164 | if (first) { |
| 1165 | s.appendFormat("%d", i); |
| 1166 | first = false; |
| 1167 | } else { |
| 1168 | s.appendFormat(", %d", i); |
| 1169 | } |
| 1170 | } |
| 1171 | return s; |
| 1172 | } |
| 1173 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1174 | int32_t CameraService::mapToInterface(TorchModeStatus status) { |
| 1175 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1176 | switch (status) { |
| 1177 | case TorchModeStatus::NOT_AVAILABLE: |
| 1178 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1179 | break; |
| 1180 | case TorchModeStatus::AVAILABLE_OFF: |
| 1181 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 1182 | break; |
| 1183 | case TorchModeStatus::AVAILABLE_ON: |
| 1184 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
| 1185 | break; |
| 1186 | default: |
| 1187 | ALOGW("Unknown new flash status: %d", status); |
| 1188 | } |
| 1189 | return serviceStatus; |
| 1190 | } |
| 1191 | |
| 1192 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { |
| 1193 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; |
| 1194 | switch (status) { |
| 1195 | case CameraDeviceStatus::NOT_PRESENT: |
| 1196 | serviceStatus = StatusInternal::NOT_PRESENT; |
| 1197 | break; |
| 1198 | case CameraDeviceStatus::PRESENT: |
| 1199 | serviceStatus = StatusInternal::PRESENT; |
| 1200 | break; |
| 1201 | case CameraDeviceStatus::ENUMERATING: |
| 1202 | serviceStatus = StatusInternal::ENUMERATING; |
| 1203 | break; |
| 1204 | default: |
| 1205 | ALOGW("Unknown new HAL device status: %d", status); |
| 1206 | } |
| 1207 | return serviceStatus; |
| 1208 | } |
| 1209 | |
| 1210 | int32_t CameraService::mapToInterface(StatusInternal status) { |
| 1211 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1212 | switch (status) { |
| 1213 | case StatusInternal::NOT_PRESENT: |
| 1214 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1215 | break; |
| 1216 | case StatusInternal::PRESENT: |
| 1217 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; |
| 1218 | break; |
| 1219 | case StatusInternal::ENUMERATING: |
| 1220 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; |
| 1221 | break; |
| 1222 | case StatusInternal::NOT_AVAILABLE: |
| 1223 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; |
| 1224 | break; |
| 1225 | case StatusInternal::UNKNOWN: |
| 1226 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; |
| 1227 | break; |
| 1228 | default: |
| 1229 | ALOGW("Unknown new internal device status: %d", status); |
| 1230 | } |
| 1231 | return serviceStatus; |
| 1232 | } |
| 1233 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1234 | Status CameraService::initializeShimMetadata(int cameraId) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1235 | int uid = CameraThreadState::getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1236 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1237 | String8 id = String8::format("%d", cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1238 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1239 | sp<Client> tmp = nullptr; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1240 | if (!(ret = connectHelper<ICameraClient,Client>( |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1241 | sp<ICameraClient>{nullptr}, id, cameraId, |
Austin Borger | b251892 | 2023-05-05 15:50:30 -0700 | [diff] [blame] | 1242 | kServiceName, /*systemNativeClient*/ false, {}, uid, USE_CALLING_PID, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1243 | API_1, /*shimUpdateOnly*/ true, /*oomScoreOffset*/ 0, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1244 | /*targetSdkVersion*/ __ANDROID_API_FUTURE__, /*overrideToPortrait*/ true, |
| 1245 | /*forceSlowJpegMode*/false, /*out*/ tmp) |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1246 | ).isOk()) { |
| 1247 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string()); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1248 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1249 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1252 | Status CameraService::getLegacyParametersLazy(int cameraId, |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1253 | /*out*/ |
| 1254 | CameraParameters* parameters) { |
| 1255 | |
| 1256 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 1257 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1258 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1259 | |
| 1260 | if (parameters == NULL) { |
| 1261 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1262 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1265 | String8 id = String8::format("%d", cameraId); |
| 1266 | |
| 1267 | // Check if we already have parameters |
| 1268 | { |
| 1269 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1270 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1271 | auto cameraState = getCameraState(id); |
| 1272 | if (cameraState == nullptr) { |
| 1273 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1274 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1275 | "Invalid camera ID: %s", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1276 | } |
| 1277 | CameraParameters p = cameraState->getShimParams(); |
| 1278 | if (!p.isEmpty()) { |
| 1279 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1280 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1284 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1285 | ret = initializeShimMetadata(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1286 | CameraThreadState::restoreCallingIdentity(token); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1287 | if (!ret.isOk()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1288 | // Error already logged by callee |
| 1289 | return ret; |
| 1290 | } |
| 1291 | |
| 1292 | // Check for parameters again |
| 1293 | { |
| 1294 | // Scope for service lock |
| 1295 | Mutex::Autolock lock(mServiceLock); |
| 1296 | auto cameraState = getCameraState(id); |
| 1297 | if (cameraState == nullptr) { |
| 1298 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1299 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1300 | "Invalid camera ID: %s", id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1301 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1302 | CameraParameters p = cameraState->getShimParams(); |
| 1303 | if (!p.isEmpty()) { |
| 1304 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1305 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1309 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 1310 | __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1311 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1314 | // Can camera service trust the caller based on the calling UID? |
| 1315 | static bool isTrustedCallingUid(uid_t uid) { |
| 1316 | switch (uid) { |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1317 | case AID_MEDIA: // mediaserver |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1318 | case AID_CAMERASERVER: // cameraserver |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1319 | case AID_RADIO: // telephony |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1320 | return true; |
| 1321 | default: |
| 1322 | return false; |
| 1323 | } |
| 1324 | } |
| 1325 | |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 1326 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { |
| 1327 | PermissionController pc; |
| 1328 | uid = pc.getPackageUid(packageName, 0); |
| 1329 | if (uid <= 0) { |
| 1330 | ALOGE("Unknown package: '%s'", String8(packageName).string()); |
| 1331 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).string()); |
| 1332 | return BAD_VALUE; |
| 1333 | } |
| 1334 | |
| 1335 | if (userId < 0) { |
| 1336 | ALOGE("Invalid user: %d", userId); |
| 1337 | dprintf(err, "Invalid user: %d\n", userId); |
| 1338 | return BAD_VALUE; |
| 1339 | } |
| 1340 | |
| 1341 | uid = multiuser_get_uid(userId, uid); |
| 1342 | return NO_ERROR; |
| 1343 | } |
| 1344 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1345 | Status CameraService::validateConnectLocked(const String8& cameraId, |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1346 | const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, |
| 1347 | /*out*/int& originalClientPid) const { |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1348 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1349 | #ifdef __BRILLO__ |
| 1350 | UNUSED(clientName8); |
| 1351 | UNUSED(clientUid); |
| 1352 | UNUSED(clientPid); |
| 1353 | UNUSED(originalClientPid); |
| 1354 | #else |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1355 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, |
| 1356 | originalClientPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1357 | if (!allowed.isOk()) { |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1358 | return allowed; |
| 1359 | } |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1360 | #endif // __BRILLO__ |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1361 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1362 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1363 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1364 | if (!mInitialized) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1365 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 1366 | callingPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1367 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1368 | "No camera HAL module available to open camera device \"%s\"", cameraId.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1371 | if (getCameraState(cameraId) == nullptr) { |
| 1372 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1373 | cameraId.string()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1374 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1375 | "No camera device with ID \"%s\" available", cameraId.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1378 | status_t err = checkIfDeviceIsUsable(cameraId); |
| 1379 | if (err != NO_ERROR) { |
| 1380 | switch(err) { |
| 1381 | case -ENODEV: |
| 1382 | case -EBUSY: |
| 1383 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1384 | "No camera device with ID \"%s\" currently available", cameraId.string()); |
| 1385 | default: |
| 1386 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1387 | "Unknown error connecting to ID \"%s\"", cameraId.string()); |
| 1388 | } |
| 1389 | } |
| 1390 | return Status::ok(); |
Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1393 | Status CameraService::validateClientPermissionsLocked(const String8& cameraId, |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1394 | const String8& clientName8, int& clientUid, int& clientPid, |
| 1395 | /*out*/int& originalClientPid) const { |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1396 | AttributionSourceState attributionSource{}; |
| 1397 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1398 | int callingPid = CameraThreadState::getCallingPid(); |
| 1399 | int callingUid = CameraThreadState::getCallingUid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1400 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1401 | // Check if we can trust clientUid |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1402 | if (clientUid == USE_CALLING_UID) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1403 | clientUid = callingUid; |
| 1404 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1405 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1406 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1407 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1408 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1409 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1410 | callingPid, callingUid, cameraId.string(), |
| 1411 | clientName8.string(), clientUid, clientPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1412 | } |
| 1413 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1414 | // Check if we can trust clientPid |
| 1415 | if (clientPid == USE_CALLING_PID) { |
| 1416 | clientPid = callingPid; |
| 1417 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1418 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1419 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1420 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1421 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1422 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1423 | callingPid, callingUid, cameraId.string(), |
| 1424 | clientName8.string(), clientUid, clientPid); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1425 | } |
| 1426 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1427 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1428 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", |
| 1429 | cameraId.c_str()); |
| 1430 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, "No camera device with ID \"%s\" is" |
| 1431 | "available", cameraId.string()); |
| 1432 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1433 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1434 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 1435 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 1436 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "No camera device with ID \"%s\"" |
| 1437 | "found while trying to query device kind", cameraId.string()); |
| 1438 | |
| 1439 | } |
| 1440 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1441 | // If it's not calling from cameraserver, check the permission if the |
| 1442 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for |
| 1443 | // android.permission.SYSTEM_CAMERA for system only camera devices). |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1444 | attributionSource.pid = clientPid; |
| 1445 | attributionSource.uid = clientUid; |
| 1446 | attributionSource.packageName = clientName8; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1447 | bool checkPermissionForCamera = checkPermission(cameraId, sCameraPermission, attributionSource, |
| 1448 | String16(), AppOpsManager::OP_NONE); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1449 | if (callingPid != getpid() && |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1450 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && !checkPermissionForCamera) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1451 | 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] | 1452 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1453 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission", |
| 1454 | clientName8.string(), clientUid, clientPid, cameraId.string()); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1455 | } |
| 1456 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1457 | // Make sure the UID is in an active state to use the camera |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 1458 | if (!mUidPolicy->isUidActive(callingUid, String16(clientName8))) { |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1459 | int32_t procState = mUidPolicy->getProcState(callingUid); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1460 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", |
| 1461 | clientPid, clientUid); |
| 1462 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1463 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" |
| 1464 | "calling UID %d proc state %" PRId32 ")", |
| 1465 | clientName8.string(), clientUid, clientPid, cameraId.string(), |
| 1466 | callingUid, procState); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1467 | } |
| 1468 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1469 | |
| 1470 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use cases |
| 1471 | // such as rear view and surround view cannot be disabled and are exempt from sensor privacy |
| 1472 | // policy. In all other cases,if sensor privacy is enabled then prevent access to the camera. |
| 1473 | if ((!isAutomotivePrivilegedClient(callingUid) || |
| 1474 | !isAutomotiveExteriorSystemCamera(cameraId)) && |
| 1475 | mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1476 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); |
| 1477 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1478 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" when sensor privacy " |
| 1479 | "is enabled", clientName8.string(), clientUid, clientPid, cameraId.string()); |
| 1480 | } |
| 1481 | |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1482 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's |
| 1483 | // connected to camera service directly. |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1484 | originalClientPid = clientPid; |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1485 | clientPid = callingPid; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1486 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1487 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1488 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1489 | // For non-system clients : Only allow clients who are being used by the current foreground |
| 1490 | // device user, unless calling from our own process. |
| 1491 | if (!doesClientHaveSystemUid() && callingPid != getpid() && |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1492 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1493 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 1494 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
| 1495 | toString(mAllowedUsers).string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1496 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1497 | "Callers from device user %d are not currently allowed to connect to camera \"%s\"", |
| 1498 | clientUserId, cameraId.string()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1501 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const { |
| 1505 | auto cameraState = getCameraState(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1506 | int callingPid = CameraThreadState::getCallingPid(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1507 | if (cameraState == nullptr) { |
| 1508 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1509 | cameraId.string()); |
| 1510 | return -ENODEV; |
| 1511 | } |
| 1512 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1513 | StatusInternal currentStatus = cameraState->getStatus(); |
| 1514 | if (currentStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1515 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
| 1516 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1517 | return -ENODEV; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1518 | } else if (currentStatus == StatusInternal::ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1519 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
| 1520 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1521 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1522 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1523 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1524 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1525 | } |
| 1526 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1527 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1528 | const CameraService::DescriptorPtr& desc, int oomScoreOffset, bool systemNativeClient) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1529 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1530 | // Make a descriptor for the incoming client |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1531 | auto clientDescriptor = |
| 1532 | CameraService::CameraClientManager::makeClientDescriptor(client, desc, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1533 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1534 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 1535 | |
| 1536 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
| 1537 | String8(client->getPackageName())); |
| 1538 | |
| 1539 | if (evicted.size() > 0) { |
| 1540 | // This should never happen - clients should already have been removed in disconnect |
| 1541 | for (auto& i : evicted) { |
| 1542 | ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect", |
| 1543 | __FUNCTION__, i->getKey().string()); |
| 1544 | } |
| 1545 | |
| 1546 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 1547 | __FUNCTION__); |
| 1548 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1549 | |
| 1550 | // And register a death notification for the client callback. Do |
| 1551 | // this last to avoid Binder policy where a nested Binder |
| 1552 | // transaction might be pre-empted to service the client death |
| 1553 | // notification if the client process dies before linkToDeath is |
| 1554 | // invoked. |
| 1555 | sp<IBinder> remoteCallback = client->getRemote(); |
| 1556 | if (remoteCallback != nullptr) { |
| 1557 | remoteCallback->linkToDeath(this); |
| 1558 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid, |
| 1562 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1563 | int oomScoreOffset, bool systemNativeClient, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1564 | /*out*/ |
| 1565 | sp<BasicClient>* client, |
| 1566 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1567 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1568 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1569 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1570 | DescriptorPtr clientDescriptor; |
| 1571 | { |
| 1572 | if (effectiveApiLevel == API_1) { |
| 1573 | // If we are using API1, any existing client for this camera ID with the same remote |
| 1574 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 1575 | |
| 1576 | auto current = mActiveClientManager.get(cameraId); |
| 1577 | if (current != nullptr) { |
| 1578 | auto clientSp = current->getValue(); |
| 1579 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1580 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
Shuzhen Wang | b2d43f6 | 2021-08-25 14:01:11 -0700 | [diff] [blame] | 1581 | ALOGW("CameraService connect called with a different" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1582 | " API level, evicting prior client..."); |
| 1583 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1584 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1585 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1586 | *client = clientSp; |
| 1587 | return NO_ERROR; |
| 1588 | } |
| 1589 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1590 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1591 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 1592 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1593 | // Get state for the given cameraId |
| 1594 | auto state = getCameraState(cameraId); |
| 1595 | if (state == nullptr) { |
| 1596 | ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", |
| 1597 | clientPid, cameraId.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1598 | // Should never get here because validateConnectLocked should have errored out |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1599 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1600 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1601 | |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame^] | 1602 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1603 | sp<IBinder> binder = sm->checkService(String16(kProcessInfoServiceName)); |
| 1604 | if (!binder && isAutomotivePrivilegedClient(CameraThreadState::getCallingUid())) { |
| 1605 | // If processinfo service is not available and the client is automotive privileged |
| 1606 | // client used for safety critical uses cases such as rear-view and surround-view which |
| 1607 | // needs to be available before android boot completes, then use the hardcoded values |
| 1608 | // for the process state and priority score. As this scenario is before android system |
| 1609 | // services are up and client is native client, hence using NATIVE_ADJ as the priority |
| 1610 | // score and state as PROCESS_STATE_BOUND_TOP as such automotive apps need to be |
| 1611 | // visible on the top. |
| 1612 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 1613 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 1614 | state->getConflicting(), resource_policy::NATIVE_ADJ, clientPid, |
| 1615 | ActivityManager::PROCESS_STATE_BOUND_TOP, oomScoreOffset, systemNativeClient); |
| 1616 | } else { |
| 1617 | // Get current active client PIDs |
| 1618 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 1619 | ownerPids.push_back(clientPid); |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1620 | |
Jyoti Bhayana | da519ab | 2023-05-15 15:49:15 -0700 | [diff] [blame^] | 1621 | std::vector<int> priorityScores(ownerPids.size()); |
| 1622 | std::vector<int> states(ownerPids.size()); |
| 1623 | |
| 1624 | // Get priority scores of all active PIDs |
| 1625 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids(ownerPids.size(), |
| 1626 | &ownerPids[0], /*out*/&states[0], /*out*/&priorityScores[0]); |
| 1627 | if (err != OK) { |
| 1628 | ALOGE("%s: Priority score query failed: %d", __FUNCTION__, err); |
| 1629 | return err; |
| 1630 | } |
| 1631 | |
| 1632 | // Update all active clients' priorities |
| 1633 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; |
| 1634 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
| 1635 | pidToPriorityMap.emplace(ownerPids[i], |
| 1636 | resource_policy::ClientPriority(priorityScores[i], states[i], |
| 1637 | /* isVendorClient won't get copied over*/ false, |
| 1638 | /* oomScoreOffset won't get copied over*/ 0)); |
| 1639 | } |
| 1640 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
| 1641 | |
| 1642 | int32_t actualScore = priorityScores[priorityScores.size() - 1]; |
| 1643 | int32_t actualState = states[states.size() - 1]; |
| 1644 | |
| 1645 | // Make descriptor for incoming client. We store the oomScoreOffset |
| 1646 | // since we might need it later on new handleEvictionsLocked and |
| 1647 | // ProcessInfoService would not take that into account. |
| 1648 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 1649 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 1650 | state->getConflicting(), actualScore, clientPid, actualState, |
| 1651 | oomScoreOffset, systemNativeClient); |
| 1652 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1653 | |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1654 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); |
| 1655 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1656 | // Find clients that would be evicted |
| 1657 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 1658 | |
| 1659 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 1660 | // background, so we cannot do evictions |
| 1661 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 1662 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 1663 | " priority).", clientPid); |
| 1664 | |
| 1665 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1666 | String8 curTime = getFormattedCurrentTime(); |
| 1667 | auto incompatibleClients = |
| 1668 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 1669 | |
| 1670 | String8 msg = String8::format("%s : DENIED connect device %s client for package %s " |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1671 | "(PID %d, score %d state %d) due to eviction policy", curTime.string(), |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1672 | cameraId.string(), packageName.string(), clientPid, |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1673 | clientPriority.getScore(), clientPriority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1674 | |
| 1675 | for (auto& i : incompatibleClients) { |
| 1676 | msg.appendFormat("\n - Blocked by existing device %s client for package %s" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1677 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", |
| 1678 | i->getKey().string(), |
| 1679 | String8{i->getValue()->getPackageName()}.string(), |
| 1680 | i->getOwnerId(), i->getPriority().getScore(), |
| 1681 | i->getPriority().getState()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1682 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1683 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(), |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1684 | String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1685 | i->getPriority().getScore(), i->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1689 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1690 | mEventLog.add(msg); |
| 1691 | |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1692 | auto current = mActiveClientManager.get(cameraId); |
| 1693 | if (current != nullptr) { |
| 1694 | return -EBUSY; // CAMERA_IN_USE |
| 1695 | } else { |
| 1696 | return -EUSERS; // MAX_CAMERAS_IN_USE |
| 1697 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | for (auto& i : evicted) { |
| 1701 | sp<BasicClient> clientSp = i->getValue(); |
| 1702 | if (clientSp.get() == nullptr) { |
| 1703 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 1704 | |
| 1705 | // TODO: Remove this |
| 1706 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 1707 | __FUNCTION__); |
| 1708 | mActiveClientManager.remove(i); |
| 1709 | continue; |
| 1710 | } |
| 1711 | |
| 1712 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
| 1713 | i->getKey().string()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1714 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1715 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1716 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1717 | logEvent(String8::format("EVICT device %s client held by package %s (PID" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1718 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" |
| 1719 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1720 | i->getKey().string(), String8{clientSp->getPackageName()}.string(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1721 | i->getOwnerId(), i->getPriority().getScore(), |
| 1722 | i->getPriority().getState(), cameraId.string(), |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1723 | packageName.string(), clientPid, clientPriority.getScore(), |
| 1724 | clientPriority.getState())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1725 | |
| 1726 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1727 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1728 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1729 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1730 | } |
| 1731 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1732 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 1733 | // other clients from connecting in mServiceLockWrapper if held |
| 1734 | mServiceLock.unlock(); |
| 1735 | |
| 1736 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1737 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1738 | |
| 1739 | // Destroy evicted clients |
| 1740 | for (auto& i : evictedClients) { |
| 1741 | // 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] | 1742 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1743 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1744 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1745 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1746 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1747 | for (const auto& i : evictedClients) { |
| 1748 | ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")", |
| 1749 | __FUNCTION__, i->getKey().string(), i->getOwnerId()); |
| 1750 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 1751 | if (ret == TIMED_OUT) { |
| 1752 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
| 1753 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), |
| 1754 | mActiveClientManager.toString().string()); |
| 1755 | return -EBUSY; |
| 1756 | } |
| 1757 | if (ret != NO_ERROR) { |
| 1758 | ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), " |
| 1759 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret), |
| 1760 | ret, mActiveClientManager.toString().string()); |
| 1761 | return ret; |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | evictedClients.clear(); |
| 1766 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1767 | // Once clients have been disconnected, relock |
| 1768 | mServiceLock.lock(); |
| 1769 | |
| 1770 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 1771 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 1772 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1775 | *partial = clientDescriptor; |
| 1776 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1777 | } |
| 1778 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1779 | Status CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1780 | const sp<ICameraClient>& cameraClient, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1781 | int api1CameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1782 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1783 | int clientUid, |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1784 | int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1785 | int targetSdkVersion, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1786 | bool overrideToPortrait, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1787 | bool forceSlowJpegMode, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1788 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1789 | sp<ICamera>* device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1790 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1791 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1792 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1793 | |
| 1794 | String8 id = cameraIdIntToStr(api1CameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1795 | sp<Client> client = nullptr; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1796 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1797 | clientPackageName,/*systemNativeClient*/ false, {}, clientUid, clientPid, API_1, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1798 | /*shimUpdateOnly*/ false, /*oomScoreOffset*/ 0, targetSdkVersion, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1799 | overrideToPortrait, forceSlowJpegMode, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1800 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1801 | if(!ret.isOk()) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1802 | logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageName), |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1803 | ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1804 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1807 | *device = client; |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 1808 | |
| 1809 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 1810 | const auto& mActivityManager = getActivityManager(); |
| 1811 | if (mActivityManager) { |
| 1812 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
| 1813 | CameraThreadState::getCallingUid(), |
| 1814 | CameraThreadState::getCallingPid()); |
| 1815 | } |
| 1816 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1817 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1818 | } |
| 1819 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1820 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, |
| 1821 | bool isVendorListener, int clientPid, int clientUid) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1822 | // If the client is not a vendor client, don't add listener if |
| 1823 | // a) the camera is a publicly hidden secure camera OR |
| 1824 | // b) the camera is a system only camera and the client doesn't |
| 1825 | // have android.permission.SYSTEM_CAMERA permissions. |
| 1826 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || |
| 1827 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1828 | !hasPermissionsForSystemCamera(String8(), clientPid, clientUid)))) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 1829 | return true; |
| 1830 | } |
| 1831 | return false; |
| 1832 | } |
| 1833 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1834 | bool CameraService::shouldRejectSystemCameraConnection(const String8& cameraId) const { |
| 1835 | // Rules for rejection: |
| 1836 | // 1) If cameraserver tries to access this camera device, accept the |
| 1837 | // connection. |
| 1838 | // 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] | 1839 | // non system component is trying to access it. |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1840 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY |
| 1841 | // and the serving thread is a non hwbinder thread, the client must have |
| 1842 | // android.permission.SYSTEM_CAMERA permissions to connect. |
| 1843 | |
| 1844 | int cPid = CameraThreadState::getCallingPid(); |
| 1845 | int cUid = CameraThreadState::getCallingUid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1846 | bool systemClient = doesClientHaveSystemUid(); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1847 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 1848 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1849 | // This isn't a known camera ID, so it's not a system camera |
| 1850 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); |
| 1851 | return false; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1852 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1853 | |
| 1854 | // (1) Cameraserver trying to connect, accept. |
| 1855 | if (CameraThreadState::getCallingPid() == getpid()) { |
| 1856 | return false; |
| 1857 | } |
| 1858 | // (2) |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1859 | if (!systemClient && systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1860 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); |
| 1861 | return true; |
| 1862 | } |
| 1863 | // (3) Here we only check for permissions if it is a system only camera device. This is since |
| 1864 | // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some |
| 1865 | // characteristics) even if clients don't have android.permission.CAMERA. We do not want the |
| 1866 | // same behavior for system camera devices. |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1867 | if (!systemClient && systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1868 | !hasPermissionsForSystemCamera(cameraId, cPid, cUid)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1869 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", |
| 1870 | cameraId.c_str()); |
| 1871 | return true; |
| 1872 | } |
| 1873 | |
| 1874 | return false; |
| 1875 | } |
| 1876 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1877 | Status CameraService::connectDevice( |
| 1878 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1879 | const String16& cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1880 | const String16& clientPackageName, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1881 | const std::optional<String16>& clientFeatureId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1882 | int clientUid, int oomScoreOffset, int targetSdkVersion, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1883 | bool overrideToPortrait, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1884 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1885 | sp<hardware::camera2::ICameraDeviceUser>* device) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1886 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1887 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1888 | Status ret = Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1889 | String8 id = String8(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1890 | sp<CameraDeviceClient> client = nullptr; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1891 | String16 clientPackageNameAdj = clientPackageName; |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1892 | int callingPid = CameraThreadState::getCallingPid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1893 | bool systemNativeClient = false; |
| 1894 | if (doesClientHaveSystemUid() && (clientPackageNameAdj.size() == 0)) { |
| 1895 | std::string systemClient = |
| 1896 | StringPrintf("client.pid<%d>", CameraThreadState::getCallingPid()); |
| 1897 | clientPackageNameAdj = String16(systemClient.c_str()); |
| 1898 | systemNativeClient = true; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1899 | } |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1900 | |
| 1901 | if (oomScoreOffset < 0) { |
| 1902 | String8 msg = |
| 1903 | String8::format("Cannot increase the priority of a client %s pid %d for " |
| 1904 | "camera id %s", String8(clientPackageNameAdj).string(), callingPid, |
| 1905 | id.string()); |
| 1906 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1907 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 1908 | } |
| 1909 | |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 1910 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 1911 | int callingUid = CameraThreadState::getCallingUid(); |
| 1912 | if (clientUid == USE_CALLING_UID) { |
| 1913 | clientUserId = multiuser_get_user_id(callingUid); |
| 1914 | } |
| 1915 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1916 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use cases |
| 1917 | // such as rear view and surround view cannot be disabled. |
| 1918 | if ((!isAutomotivePrivilegedClient(callingUid) || !isAutomotiveExteriorSystemCamera(id)) && |
| 1919 | mCameraServiceProxyWrapper->isCameraDisabled(clientUserId)) { |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 1920 | String8 msg = |
| 1921 | String8::format("Camera disabled by device policy"); |
| 1922 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1923 | return STATUS_ERROR(ERROR_DISABLED, msg.string()); |
| 1924 | } |
| 1925 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1926 | // enforce system camera permissions |
| 1927 | if (oomScoreOffset > 0 && |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 1928 | !hasPermissionsForSystemCamera(id, callingPid, CameraThreadState::getCallingUid()) && |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 1929 | !isTrustedCallingUid(CameraThreadState::getCallingUid())) { |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1930 | String8 msg = |
| 1931 | String8::format("Cannot change the priority of a client %s pid %d for " |
| 1932 | "camera id %s without SYSTEM_CAMERA permissions", |
| 1933 | String8(clientPackageNameAdj).string(), callingPid, id.string()); |
| 1934 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1935 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, msg.string()); |
| 1936 | } |
| 1937 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1938 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1939 | /*api1CameraId*/-1, clientPackageNameAdj, systemNativeClient,clientFeatureId, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1940 | clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, oomScoreOffset, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1941 | targetSdkVersion, overrideToPortrait, /*forceSlowJpegMode*/false, |
| 1942 | /*out*/client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1943 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1944 | if(!ret.isOk()) { |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1945 | logRejected(id, callingPid, String8(clientPackageNameAdj), ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1946 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1947 | } |
| 1948 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1949 | *device = client; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 1950 | Mutex::Autolock lock(mServiceLock); |
| 1951 | |
| 1952 | // Clear the previous cached logs and reposition the |
| 1953 | // file offset to beginning of the file to log new data. |
| 1954 | // If either truncate or lseek fails, close the previous file and create a new one. |
| 1955 | if ((ftruncate(mMemFd, 0) == -1) || (lseek(mMemFd, 0, SEEK_SET) == -1)) { |
| 1956 | ALOGE("%s: Error while truncating the file: %s", __FUNCTION__, sFileName); |
| 1957 | // Close the previous memfd. |
| 1958 | close(mMemFd); |
| 1959 | // If failure to wipe the data, then create a new file and |
| 1960 | // assign the new value to mMemFd. |
| 1961 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 1962 | if (mMemFd == -1) { |
| 1963 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 1964 | } |
| 1965 | } |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 1966 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 1967 | const auto& mActivityManager = getActivityManager(); |
| 1968 | if (mActivityManager) { |
| 1969 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
| 1970 | CameraThreadState::getCallingUid(), |
| 1971 | CameraThreadState::getCallingPid()); |
| 1972 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1973 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1974 | } |
| 1975 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1976 | String16 CameraService::getPackageNameFromUid(int clientUid) { |
| 1977 | String16 packageName(""); |
| 1978 | |
| 1979 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1980 | sp<IBinder> binder = sm->getService(String16(kPermissionServiceName)); |
| 1981 | if (binder == 0) { |
| 1982 | ALOGE("Cannot get permission service"); |
| 1983 | // Return empty package name and the further interaction |
| 1984 | // with camera will likely fail |
| 1985 | return packageName; |
| 1986 | } |
| 1987 | |
| 1988 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 1989 | Vector<String16> packages; |
| 1990 | |
| 1991 | permCtrl->getPackagesForUid(clientUid, packages); |
| 1992 | |
| 1993 | if (packages.isEmpty()) { |
| 1994 | ALOGE("No packages for calling UID %d", clientUid); |
| 1995 | // Return empty package name and the further interaction |
| 1996 | // with camera will likely fail |
| 1997 | return packageName; |
| 1998 | } |
| 1999 | |
| 2000 | // Arbitrarily pick the first name in the list |
| 2001 | packageName = packages[0]; |
| 2002 | |
| 2003 | return packageName; |
| 2004 | } |
| 2005 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2006 | template<class CALLBACK, class CLIENT> |
| 2007 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2008 | int api1CameraId, const String16& clientPackageNameMaybe, bool systemNativeClient, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 2009 | const std::optional<String16>& clientFeatureId, int clientUid, int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2010 | apiLevel effectiveApiLevel, bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2011 | bool overrideToPortrait, bool forceSlowJpegMode, |
| 2012 | /*out*/sp<CLIENT>& device) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2013 | binder::Status ret = binder::Status::ok(); |
| 2014 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2015 | bool isNonSystemNdk = false; |
| 2016 | String16 clientPackageName; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2017 | int packageUid = (clientUid == USE_CALLING_UID) ? |
| 2018 | CameraThreadState::getCallingUid() : clientUid; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2019 | if (clientPackageNameMaybe.size() <= 0) { |
| 2020 | // NDK calls don't come with package names, but we need one for various cases. |
| 2021 | // Generally, there's a 1:1 mapping between UID and package name, but shared UIDs |
| 2022 | // do exist. For all authentication cases, all packages under the same UID get the |
| 2023 | // same permissions, so picking any associated package name is sufficient. For some |
| 2024 | // other cases, this may give inaccurate names for clients in logs. |
| 2025 | isNonSystemNdk = true; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2026 | clientPackageName = getPackageNameFromUid(packageUid); |
| 2027 | } else { |
| 2028 | clientPackageName = clientPackageNameMaybe; |
| 2029 | } |
| 2030 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2031 | String8 clientName8(clientPackageName); |
| 2032 | |
| 2033 | int originalClientPid = 0; |
| 2034 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2035 | int packagePid = (clientPid == USE_CALLING_PID) ? |
| 2036 | CameraThreadState::getCallingPid() : clientPid; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 2037 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2038 | "Camera API version %d", packagePid, clientName8.string(), cameraId.string(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2039 | static_cast<int>(effectiveApiLevel)); |
| 2040 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2041 | nsecs_t openTimeNs = systemTime(); |
| 2042 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2043 | sp<CLIENT> client = nullptr; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2044 | int facing = -1; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 2045 | int orientation = 0; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 2046 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2047 | { |
| 2048 | // Acquire mServiceLock and prevent other clients from connecting |
| 2049 | std::unique_ptr<AutoConditionLock> lock = |
| 2050 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2051 | |
| 2052 | if (lock == nullptr) { |
| 2053 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." |
| 2054 | , clientPid); |
| 2055 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2056 | "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting", |
| 2057 | cameraId.string(), clientName8.string(), clientPid); |
| 2058 | } |
| 2059 | |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 2060 | // Enforce client permissions and do basic validity checks |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2061 | if(!(ret = validateConnectLocked(cameraId, clientName8, |
| 2062 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { |
| 2063 | return ret; |
| 2064 | } |
| 2065 | |
| 2066 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 2067 | // we were doing a shim update, return immediately |
| 2068 | if (shimUpdateOnly) { |
| 2069 | auto cameraState = getCameraState(cameraId); |
| 2070 | if (cameraState != nullptr) { |
| 2071 | if (!cameraState->getShimParams().isEmpty()) return ret; |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | status_t err; |
| 2076 | |
| 2077 | sp<BasicClient> clientTmp = nullptr; |
| 2078 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial; |
| 2079 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2080 | IInterface::asBinder(cameraCb), clientName8, oomScoreOffset, systemNativeClient, |
| 2081 | /*out*/&clientTmp, /*out*/&partial)) != NO_ERROR) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2082 | switch (err) { |
| 2083 | case -ENODEV: |
| 2084 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2085 | "No camera device with ID \"%s\" currently available", |
| 2086 | cameraId.string()); |
| 2087 | case -EBUSY: |
| 2088 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2089 | "Higher-priority client using camera, ID \"%s\" currently unavailable", |
| 2090 | cameraId.string()); |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 2091 | case -EUSERS: |
| 2092 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2093 | "Too many cameras already open, cannot open camera \"%s\"", |
| 2094 | cameraId.string()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2095 | default: |
| 2096 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2097 | "Unexpected error %s (%d) opening camera \"%s\"", |
| 2098 | strerror(-err), err, cameraId.string()); |
| 2099 | } |
| 2100 | } |
| 2101 | |
| 2102 | if (clientTmp.get() != nullptr) { |
| 2103 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 2104 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 2105 | return ret; |
| 2106 | } |
| 2107 | |
| 2108 | // give flashlight a chance to close devices if necessary. |
| 2109 | mFlashlight->prepareDeviceOpen(cameraId); |
| 2110 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2111 | int portraitRotation; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2112 | auto deviceVersionAndTransport = |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2113 | getDeviceVersion(cameraId, overrideToPortrait, /*out*/&portraitRotation, |
| 2114 | /*out*/&facing, /*out*/&orientation); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2115 | if (facing == -1) { |
| 2116 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string()); |
| 2117 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2118 | "Unable to get camera device \"%s\" facing", cameraId.string()); |
| 2119 | } |
| 2120 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2121 | sp<BasicClient> tmp = nullptr; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2122 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
| 2123 | mPerfClassPrimaryCameraIds, cameraId.string(), targetSdkVersion); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2124 | if(!(ret = makeClient(this, cameraCb, clientPackageName, systemNativeClient, |
| 2125 | clientFeatureId, cameraId, api1CameraId, facing, orientation, |
Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 2126 | clientPid, clientUid, getpid(), |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2127 | deviceVersionAndTransport, effectiveApiLevel, overrideForPerfClass, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2128 | overrideToPortrait, forceSlowJpegMode, |
| 2129 | /*out*/&tmp)).isOk()) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2130 | return ret; |
| 2131 | } |
| 2132 | client = static_cast<CLIENT*>(tmp.get()); |
| 2133 | |
| 2134 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 2135 | __FUNCTION__); |
| 2136 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2137 | String8 monitorTags = isClientWatched(client.get()) ? mMonitorTags : String8(""); |
| 2138 | err = client->initialize(mCameraProviderManager, monitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2139 | if (err != OK) { |
| 2140 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2141 | // Errors could be from the HAL module open call or from AppOpsManager |
| 2142 | switch(err) { |
| 2143 | case BAD_VALUE: |
| 2144 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2145 | "Illegal argument to HAL module for camera \"%s\"", cameraId.string()); |
| 2146 | case -EBUSY: |
| 2147 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2148 | "Camera \"%s\" is already open", cameraId.string()); |
| 2149 | case -EUSERS: |
| 2150 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2151 | "Too many cameras already open, cannot open camera \"%s\"", |
| 2152 | cameraId.string()); |
| 2153 | case PERMISSION_DENIED: |
| 2154 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2155 | "No permission to open camera \"%s\"", cameraId.string()); |
| 2156 | case -EACCES: |
| 2157 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 2158 | "Camera \"%s\" disabled by policy", cameraId.string()); |
| 2159 | case -ENODEV: |
| 2160 | default: |
| 2161 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2162 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(), |
| 2163 | strerror(-err), err); |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | // Update shim paremeters for legacy clients |
| 2168 | if (effectiveApiLevel == API_1) { |
| 2169 | // Assume we have always received a Client subclass for API1 |
| 2170 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 2171 | String8 rawParams = shimClient->getParameters(); |
| 2172 | CameraParameters params(rawParams); |
| 2173 | |
| 2174 | auto cameraState = getCameraState(cameraId); |
| 2175 | if (cameraState != nullptr) { |
| 2176 | cameraState->setShimParams(params); |
| 2177 | } else { |
| 2178 | ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.", |
| 2179 | __FUNCTION__, cameraId.string()); |
| 2180 | } |
| 2181 | } |
| 2182 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 2183 | // Enable/disable camera service watchdog |
| 2184 | client->setCameraServiceWatchdog(mCameraServiceWatchdogEnabled); |
| 2185 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 2186 | // Set rotate-and-crop override behavior |
| 2187 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { |
| 2188 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2189 | } else if (overrideToPortrait && portraitRotation != 0) { |
| 2190 | uint8_t rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_AUTO; |
| 2191 | switch (portraitRotation) { |
| 2192 | case 90: |
| 2193 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_90; |
| 2194 | break; |
| 2195 | case 180: |
| 2196 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_180; |
| 2197 | break; |
| 2198 | case 270: |
| 2199 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_270; |
| 2200 | break; |
| 2201 | default: |
| 2202 | ALOGE("Unexpected portrait rotation: %d", portraitRotation); |
| 2203 | break; |
| 2204 | } |
| 2205 | client->setRotateAndCropOverride(rotateAndCropMode); |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame] | 2206 | } else { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 2207 | client->setRotateAndCropOverride( |
| 2208 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 2209 | clientPackageName, facing, multiuser_get_user_id(clientUid))); |
| 2210 | } |
| 2211 | |
| 2212 | // Set autoframing override behaviour |
| 2213 | if (mOverrideAutoframingMode != ANDROID_CONTROL_AUTOFRAMING_AUTO) { |
| 2214 | client->setAutoframingOverride(mOverrideAutoframingMode); |
| 2215 | } else { |
| 2216 | client->setAutoframingOverride( |
| 2217 | mCameraServiceProxyWrapper->getAutoframingOverride( |
| 2218 | clientPackageName)); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 2219 | } |
| 2220 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2221 | // Automotive privileged client AID_AUTOMOTIVE_EVS using exterior system camera for use |
| 2222 | // cases such as rear view and surround view cannot be disabled and are exempt from camera |
| 2223 | // privacy policy. |
| 2224 | if ((!isAutomotivePrivilegedClient(packageUid) || |
| 2225 | !isAutomotiveExteriorSystemCamera(cameraId))) { |
| 2226 | // Set camera muting behavior. |
| 2227 | bool isCameraPrivacyEnabled = |
| 2228 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
| 2229 | if (client->supportsCameraMute()) { |
| 2230 | client->setCameraMute( |
| 2231 | mOverrideCameraMuteMode || isCameraPrivacyEnabled); |
| 2232 | } else if (isCameraPrivacyEnabled) { |
| 2233 | // no camera mute supported, but privacy is on! => disconnect |
| 2234 | ALOGI("Camera mute not supported for package: %s, camera id: %s", |
| 2235 | String8(client->getPackageName()).string(), cameraId.string()); |
| 2236 | // Do not hold mServiceLock while disconnecting clients, but |
| 2237 | // retain the condition blocking other clients from connecting |
| 2238 | // in mServiceLockWrapper if held. |
| 2239 | mServiceLock.unlock(); |
| 2240 | // Clear caller identity temporarily so client disconnect PID |
| 2241 | // checks work correctly |
| 2242 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 2243 | // Note AppOp to trigger the "Unblock" dialog |
| 2244 | client->noteAppOp(); |
| 2245 | client->disconnect(); |
| 2246 | CameraThreadState::restoreCallingIdentity(token); |
| 2247 | // Reacquire mServiceLock |
| 2248 | mServiceLock.lock(); |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 2249 | |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2250 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 2251 | "Camera \"%s\" disabled due to camera mute", cameraId.string()); |
| 2252 | } |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2253 | } |
| 2254 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2255 | if (shimUpdateOnly) { |
| 2256 | // If only updating legacy shim parameters, immediately disconnect client |
| 2257 | mServiceLock.unlock(); |
| 2258 | client->disconnect(); |
| 2259 | mServiceLock.lock(); |
| 2260 | } else { |
| 2261 | // Otherwise, add client to active clients list |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2262 | finishConnectLocked(client, partial, oomScoreOffset, systemNativeClient); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2263 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 2264 | |
| 2265 | client->setImageDumpMask(mImageDumpMask); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 2266 | client->setStreamUseCaseOverrides(mStreamUseCaseOverrides); |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 2267 | client->setZoomOverride(mZoomOverrideValue); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2268 | } // lock is destroyed, allow further connect calls |
| 2269 | |
| 2270 | // Important: release the mutex here so the client can call back into the service from its |
| 2271 | // destructor (can be at the end of the call) |
| 2272 | device = client; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2273 | |
| 2274 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 2275 | mCameraServiceProxyWrapper->logOpen(cameraId, facing, clientPackageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2276 | effectiveApiLevel, isNonSystemNdk, openLatencyMs); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2277 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2278 | { |
| 2279 | Mutex::Autolock lock(mInjectionParametersLock); |
| 2280 | if (cameraId == mInjectionInternalCamId && mInjectionInitPending) { |
| 2281 | mInjectionInitPending = false; |
| 2282 | status_t res = NO_ERROR; |
| 2283 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 2284 | if (clientDescriptor != nullptr) { |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2285 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 2286 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 2287 | if(res != OK) { |
| 2288 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2289 | "No camera device with ID \"%s\" currently available", |
| 2290 | mInjectionExternalCamId.string()); |
| 2291 | } |
| 2292 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2293 | if (res != OK) { |
| 2294 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2295 | } |
| 2296 | } else { |
| 2297 | ALOGE("%s: Internal camera ID = %s 's client does not exist!", |
| 2298 | __FUNCTION__, mInjectionInternalCamId.string()); |
| 2299 | res = NO_INIT; |
| 2300 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2301 | } |
| 2302 | } |
| 2303 | } |
| 2304 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2305 | return ret; |
| 2306 | } |
| 2307 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2308 | status_t CameraService::addOfflineClient(String8 cameraId, sp<BasicClient> offlineClient) { |
| 2309 | if (offlineClient.get() == nullptr) { |
| 2310 | return BAD_VALUE; |
| 2311 | } |
| 2312 | |
| 2313 | { |
| 2314 | // Acquire mServiceLock and prevent other clients from connecting |
| 2315 | std::unique_ptr<AutoConditionLock> lock = |
| 2316 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2317 | |
| 2318 | if (lock == nullptr) { |
| 2319 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." |
| 2320 | , __FUNCTION__, offlineClient->getClientPid()); |
| 2321 | return TIMED_OUT; |
| 2322 | } |
| 2323 | |
| 2324 | auto onlineClientDesc = mActiveClientManager.get(cameraId); |
| 2325 | if (onlineClientDesc.get() == nullptr) { |
| 2326 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, |
| 2327 | cameraId.c_str()); |
| 2328 | return BAD_VALUE; |
| 2329 | } |
| 2330 | |
| 2331 | // Offline clients do not evict or conflict with other online devices. Resource sharing |
| 2332 | // conflicts are handled by the camera provider which will either succeed or fail before |
| 2333 | // reaching this method. |
| 2334 | const auto& onlinePriority = onlineClientDesc->getPriority(); |
| 2335 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( |
| 2336 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, |
| 2337 | /*conflictingKeys*/ std::set<String8>(), onlinePriority.getScore(), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2338 | onlineClientDesc->getOwnerId(), onlinePriority.getState(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2339 | // native clients don't have offline processing support. |
| 2340 | /*ommScoreOffset*/ 0, /*systemNativeClient*/false); |
Guillaume Bailey | 417e43d | 2022-11-02 15:30:24 +0100 | [diff] [blame] | 2341 | if (offlineClientDesc == nullptr) { |
| 2342 | ALOGE("%s: Offline client descriptor was NULL", __FUNCTION__); |
| 2343 | return BAD_VALUE; |
| 2344 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2345 | |
| 2346 | // Allow only one offline device per camera |
| 2347 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); |
| 2348 | if (!incompatibleClients.empty()) { |
| 2349 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); |
| 2350 | return BAD_VALUE; |
| 2351 | } |
| 2352 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2353 | String8 monitorTags = isClientWatched(offlineClient.get()) ? mMonitorTags : String8(""); |
| 2354 | auto err = offlineClient->initialize(mCameraProviderManager, monitorTags); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2355 | if (err != OK) { |
| 2356 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); |
| 2357 | return err; |
| 2358 | } |
| 2359 | |
| 2360 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); |
| 2361 | if (evicted.size() > 0) { |
| 2362 | for (auto& i : evicted) { |
| 2363 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", |
| 2364 | __FUNCTION__, i->getKey().string()); |
| 2365 | } |
| 2366 | |
| 2367 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " |
| 2368 | "properly", __FUNCTION__); |
| 2369 | |
| 2370 | return BAD_VALUE; |
| 2371 | } |
| 2372 | |
| 2373 | logConnectedOffline(offlineClientDesc->getKey(), |
| 2374 | static_cast<int>(offlineClientDesc->getOwnerId()), |
| 2375 | String8(offlineClient->getPackageName())); |
| 2376 | |
| 2377 | sp<IBinder> remoteCallback = offlineClient->getRemote(); |
| 2378 | if (remoteCallback != nullptr) { |
| 2379 | remoteCallback->linkToDeath(this); |
| 2380 | } |
| 2381 | } // lock is destroyed, allow further connect calls |
| 2382 | |
| 2383 | return OK; |
| 2384 | } |
| 2385 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2386 | Status CameraService::turnOnTorchWithStrengthLevel(const String16& cameraId, int32_t torchStrength, |
| 2387 | const sp<IBinder>& clientBinder) { |
| 2388 | Mutex::Autolock lock(mServiceLock); |
| 2389 | |
| 2390 | ATRACE_CALL(); |
| 2391 | if (clientBinder == nullptr) { |
| 2392 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
| 2393 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2394 | "Torch client binder in null."); |
| 2395 | } |
| 2396 | |
| 2397 | String8 id = String8(cameraId.string()); |
| 2398 | int uid = CameraThreadState::getCallingUid(); |
| 2399 | |
| 2400 | if (shouldRejectSystemCameraConnection(id)) { |
| 2401 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to change the strength level" |
| 2402 | "for system only device %s: ", id.string()); |
| 2403 | } |
| 2404 | |
| 2405 | // verify id is valid |
| 2406 | auto state = getCameraState(id); |
| 2407 | if (state == nullptr) { |
| 2408 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
| 2409 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2410 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
| 2411 | } |
| 2412 | |
| 2413 | StatusInternal cameraStatus = state->getStatus(); |
| 2414 | if (cameraStatus != StatusInternal::NOT_AVAILABLE && |
| 2415 | cameraStatus != StatusInternal::PRESENT) { |
| 2416 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), |
| 2417 | (int)cameraStatus); |
| 2418 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2419 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
| 2420 | } |
| 2421 | |
| 2422 | { |
| 2423 | Mutex::Autolock al(mTorchStatusMutex); |
| 2424 | TorchModeStatus status; |
| 2425 | status_t err = getTorchStatusLocked(id, &status); |
| 2426 | if (err != OK) { |
| 2427 | if (err == NAME_NOT_FOUND) { |
| 2428 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2429 | "Camera \"%s\" does not have a flash unit", id.string()); |
| 2430 | } |
| 2431 | ALOGE("%s: getting current torch status failed for camera %s", |
| 2432 | __FUNCTION__, id.string()); |
| 2433 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2434 | "Error changing torch strength level for camera \"%s\": %s (%d)", |
| 2435 | id.string(), strerror(-err), err); |
| 2436 | } |
| 2437 | |
| 2438 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
| 2439 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
| 2440 | ALOGE("%s: torch mode of camera %s is not available because " |
| 2441 | "camera is in use.", __FUNCTION__, id.string()); |
| 2442 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2443 | "Torch for camera \"%s\" is not available due to an existing camera user", |
| 2444 | id.string()); |
| 2445 | } else { |
| 2446 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 2447 | "insufficient resources", __FUNCTION__, id.string()); |
| 2448 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2449 | "Torch for camera \"%s\" is not available due to insufficient resources", |
| 2450 | id.string()); |
| 2451 | } |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | { |
| 2456 | Mutex::Autolock al(mTorchUidMapMutex); |
| 2457 | updateTorchUidMapLocked(cameraId, uid); |
| 2458 | } |
| 2459 | // Check if the current torch strength level is same as the new one. |
| 2460 | bool shouldSkipTorchStrengthUpdates = mCameraProviderManager->shouldSkipTorchStrengthUpdate( |
| 2461 | id.string(), torchStrength); |
| 2462 | |
| 2463 | status_t err = mFlashlight->turnOnTorchWithStrengthLevel(id, torchStrength); |
| 2464 | |
| 2465 | if (err != OK) { |
| 2466 | int32_t errorCode; |
| 2467 | String8 msg; |
| 2468 | switch (err) { |
| 2469 | case -ENOSYS: |
| 2470 | msg = String8::format("Camera \"%s\" has no flashlight.", |
| 2471 | id.string()); |
| 2472 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2473 | break; |
| 2474 | case -EBUSY: |
| 2475 | msg = String8::format("Camera \"%s\" is in use", |
| 2476 | id.string()); |
| 2477 | errorCode = ERROR_CAMERA_IN_USE; |
| 2478 | break; |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 2479 | case -EINVAL: |
| 2480 | msg = String8::format("Torch strength level %d is not within the " |
| 2481 | "valid range.", torchStrength); |
| 2482 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2483 | break; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2484 | default: |
| 2485 | msg = String8::format("Changing torch strength level failed."); |
| 2486 | errorCode = ERROR_INVALID_OPERATION; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2487 | } |
| 2488 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2489 | return STATUS_ERROR(errorCode, msg.string()); |
| 2490 | } |
| 2491 | |
| 2492 | { |
| 2493 | // update the link to client's death |
| 2494 | // Store the last client that turns on each camera's torch mode. |
| 2495 | Mutex::Autolock al(mTorchClientMapMutex); |
| 2496 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 2497 | if (index == NAME_NOT_FOUND) { |
| 2498 | mTorchClientMap.add(id, clientBinder); |
| 2499 | } else { |
| 2500 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
| 2501 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2502 | } |
| 2503 | clientBinder->linkToDeath(this); |
| 2504 | } |
| 2505 | |
| 2506 | int clientPid = CameraThreadState::getCallingPid(); |
| 2507 | const char *id_cstr = id.c_str(); |
| 2508 | ALOGI("%s: Torch strength for camera id %s changed to %d for client PID %d", |
| 2509 | __FUNCTION__, id_cstr, torchStrength, clientPid); |
| 2510 | if (!shouldSkipTorchStrengthUpdates) { |
| 2511 | broadcastTorchStrengthLevel(id, torchStrength); |
| 2512 | } |
| 2513 | return Status::ok(); |
| 2514 | } |
| 2515 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2516 | Status CameraService::setTorchMode(const String16& cameraId, bool enabled, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2517 | const sp<IBinder>& clientBinder) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2518 | Mutex::Autolock lock(mServiceLock); |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2519 | |
| 2520 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2521 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2522 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2523 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2524 | "Torch client Binder is null"); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2525 | } |
| 2526 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2527 | String8 id = String8(cameraId.string()); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2528 | int uid = CameraThreadState::getCallingUid(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2529 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2530 | if (shouldRejectSystemCameraConnection(id)) { |
| 2531 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" |
| 2532 | " for system only device %s: ", id.string()); |
| 2533 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2534 | // verify id is valid. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2535 | auto state = getCameraState(id); |
| 2536 | if (state == nullptr) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 2537 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2538 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2539 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2540 | } |
| 2541 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2542 | StatusInternal cameraStatus = state->getStatus(); |
| 2543 | if (cameraStatus != StatusInternal::PRESENT && |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2544 | cameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 2545 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2546 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2547 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2548 | } |
| 2549 | |
| 2550 | { |
| 2551 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2552 | TorchModeStatus status; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2553 | status_t err = getTorchStatusLocked(id, &status); |
| 2554 | if (err != OK) { |
| 2555 | if (err == NAME_NOT_FOUND) { |
| 2556 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2557 | "Camera \"%s\" does not have a flash unit", id.string()); |
| 2558 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2559 | ALOGE("%s: getting current torch status failed for camera %s", |
| 2560 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2561 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2562 | "Error updating torch status for camera \"%s\": %s (%d)", id.string(), |
| 2563 | strerror(-err), err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2564 | } |
| 2565 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2566 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2567 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2568 | ALOGE("%s: torch mode of camera %s is not available because " |
| 2569 | "camera is in use", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2570 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2571 | "Torch for camera \"%s\" is not available due to an existing camera user", |
| 2572 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2573 | } else { |
| 2574 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 2575 | "insufficient resources", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2576 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2577 | "Torch for camera \"%s\" is not available due to insufficient resources", |
| 2578 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2579 | } |
| 2580 | } |
| 2581 | } |
| 2582 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2583 | { |
| 2584 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 2585 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 2586 | Mutex::Autolock al(mTorchUidMapMutex); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2587 | updateTorchUidMapLocked(cameraId, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2588 | } |
| 2589 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2590 | status_t err = mFlashlight->setTorchMode(id, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2591 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2592 | if (err != OK) { |
| 2593 | int32_t errorCode; |
| 2594 | String8 msg; |
| 2595 | switch (err) { |
| 2596 | case -ENOSYS: |
| 2597 | msg = String8::format("Camera \"%s\" has no flashlight", |
| 2598 | id.string()); |
| 2599 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2600 | break; |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 2601 | case -EBUSY: |
| 2602 | msg = String8::format("Camera \"%s\" is in use", |
| 2603 | id.string()); |
| 2604 | errorCode = ERROR_CAMERA_IN_USE; |
| 2605 | break; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2606 | default: |
| 2607 | msg = String8::format( |
| 2608 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", |
| 2609 | id.string(), enabled, strerror(-err), err); |
| 2610 | errorCode = ERROR_INVALID_OPERATION; |
| 2611 | } |
| 2612 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2613 | logServiceError(msg,errorCode); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2614 | return STATUS_ERROR(errorCode, msg.string()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2615 | } |
| 2616 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2617 | { |
| 2618 | // update the link to client's death |
| 2619 | Mutex::Autolock al(mTorchClientMapMutex); |
| 2620 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 2621 | if (enabled) { |
| 2622 | if (index == NAME_NOT_FOUND) { |
| 2623 | mTorchClientMap.add(id, clientBinder); |
| 2624 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2625 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2626 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2627 | } |
| 2628 | clientBinder->linkToDeath(this); |
| 2629 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2630 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2631 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2632 | } |
| 2633 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 2634 | int clientPid = CameraThreadState::getCallingPid(); |
| 2635 | const char *id_cstr = id.c_str(); |
| 2636 | const char *torchState = enabled ? "on" : "off"; |
| 2637 | ALOGI("Torch for camera id %s turned %s for client PID %d", id_cstr, torchState, clientPid); |
| 2638 | logTorchEvent(id_cstr, torchState , clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2639 | return Status::ok(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2640 | } |
| 2641 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2642 | void CameraService::updateTorchUidMapLocked(const String16& cameraId, int uid) { |
| 2643 | String8 id = String8(cameraId.string()); |
| 2644 | if (mTorchUidMap.find(id) == mTorchUidMap.end()) { |
| 2645 | mTorchUidMap[id].first = uid; |
| 2646 | mTorchUidMap[id].second = uid; |
| 2647 | } else { |
| 2648 | // Set the pending UID |
| 2649 | mTorchUidMap[id].first = uid; |
| 2650 | } |
| 2651 | } |
| 2652 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2653 | Status CameraService::notifySystemEvent(int32_t eventId, |
| 2654 | const std::vector<int32_t>& args) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2655 | const int pid = CameraThreadState::getCallingPid(); |
| 2656 | const int selfPid = getpid(); |
| 2657 | |
| 2658 | // Permission checks |
| 2659 | if (pid != selfPid) { |
| 2660 | // Ensure we're being called by system_server, or similar process with |
| 2661 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2662 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2663 | const int uid = CameraThreadState::getCallingUid(); |
| 2664 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 2665 | " events from pid=%d, uid=%d", pid, uid); |
| 2666 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 2667 | "No permission to send updates to camera service about system events" |
| 2668 | " from pid=%d, uid=%d", pid, uid); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2669 | } |
| 2670 | } |
| 2671 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2672 | ATRACE_CALL(); |
| 2673 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2674 | switch(eventId) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2675 | case ICameraService::EVENT_USER_SWITCHED: { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2676 | // 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] | 2677 | // from a system server crash |
| 2678 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2679 | mSensorPrivacyPolicy->registerSelf(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2680 | doUserSwitch(/*newUserIds*/ args); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2681 | break; |
| 2682 | } |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 2683 | case ICameraService::EVENT_USB_DEVICE_ATTACHED: |
| 2684 | case ICameraService::EVENT_USB_DEVICE_DETACHED: { |
| 2685 | // Notify CameraProviderManager for lazy HALs |
| 2686 | mCameraProviderManager->notifyUsbDeviceEvent(eventId, |
| 2687 | std::to_string(args[0])); |
| 2688 | break; |
| 2689 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2690 | case ICameraService::EVENT_NONE: |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2691 | default: { |
| 2692 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 2693 | eventId); |
| 2694 | break; |
| 2695 | } |
| 2696 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2697 | return Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2698 | } |
| 2699 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2700 | void CameraService::notifyMonitoredUids() { |
| 2701 | Mutex::Autolock lock(mStatusListenerLock); |
| 2702 | |
| 2703 | for (const auto& it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2704 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 2705 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 2706 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2707 | } |
| 2708 | } |
| 2709 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 2710 | void CameraService::notifyMonitoredUids(const std::unordered_set<uid_t> ¬ifyUidSet) { |
| 2711 | Mutex::Autolock lock(mStatusListenerLock); |
| 2712 | |
| 2713 | for (const auto& it : mListenerList) { |
| 2714 | if (notifyUidSet.find(it->getListenerUid()) != notifyUidSet.end()) { |
| 2715 | ALOGV("%s: notifying uid %d", __FUNCTION__, it->getListenerUid()); |
| 2716 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
| 2717 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 2718 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
| 2719 | } |
| 2720 | } |
| 2721 | } |
| 2722 | |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2723 | Status CameraService::notifyDeviceStateChange(int64_t newState) { |
| 2724 | const int pid = CameraThreadState::getCallingPid(); |
| 2725 | const int selfPid = getpid(); |
| 2726 | |
| 2727 | // Permission checks |
| 2728 | if (pid != selfPid) { |
| 2729 | // Ensure we're being called by system_server, or similar process with |
| 2730 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2731 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2732 | const int uid = CameraThreadState::getCallingUid(); |
| 2733 | ALOGE("Permission Denial: cannot send updates to camera service about device" |
| 2734 | " state changes from pid=%d, uid=%d", pid, uid); |
| 2735 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2736 | "No permission to send updates to camera service about device state" |
| 2737 | " changes from pid=%d, uid=%d", pid, uid); |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | ATRACE_CALL(); |
| 2742 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2743 | { |
| 2744 | Mutex::Autolock lock(mServiceLock); |
| 2745 | mDeviceState = newState; |
| 2746 | } |
| 2747 | |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 2748 | mCameraProviderManager->notifyDeviceStateChange(newState); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2749 | |
| 2750 | return Status::ok(); |
| 2751 | } |
| 2752 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2753 | Status CameraService::notifyDisplayConfigurationChange() { |
| 2754 | ATRACE_CALL(); |
| 2755 | const int callingPid = CameraThreadState::getCallingPid(); |
| 2756 | const int selfPid = getpid(); |
| 2757 | |
| 2758 | // Permission checks |
| 2759 | if (callingPid != selfPid) { |
| 2760 | // Ensure we're being called by system_server, or similar process with |
| 2761 | // permissions to notify the camera service about system events |
| 2762 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
| 2763 | const int uid = CameraThreadState::getCallingUid(); |
| 2764 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" |
| 2765 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2766 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2767 | "No permission to send updates to camera service about orientation" |
| 2768 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | Mutex::Autolock lock(mServiceLock); |
| 2773 | |
| 2774 | // Don't do anything if rotate-and-crop override via cmd is active |
| 2775 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); |
| 2776 | |
| 2777 | const auto clients = mActiveClientManager.getAll(); |
| 2778 | for (auto& current : clients) { |
| 2779 | if (current != nullptr) { |
| 2780 | const auto basicClient = current->getValue(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2781 | if (basicClient.get() != nullptr && !basicClient->getOverrideToPortrait()) { |
| 2782 | basicClient->setRotateAndCropOverride( |
| 2783 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 2784 | basicClient->getPackageName(), |
| 2785 | basicClient->getCameraFacing(), |
| 2786 | multiuser_get_user_id(basicClient->getClientUid()))); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2787 | } |
| 2788 | } |
| 2789 | } |
| 2790 | |
| 2791 | return Status::ok(); |
| 2792 | } |
| 2793 | |
| 2794 | Status CameraService::getConcurrentCameraIds( |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2795 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { |
| 2796 | ATRACE_CALL(); |
| 2797 | if (!concurrentCameraIds) { |
| 2798 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); |
| 2799 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); |
| 2800 | } |
| 2801 | |
| 2802 | if (!mInitialized) { |
| 2803 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2804 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2805 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2806 | "Camera subsystem is not available"); |
| 2807 | } |
| 2808 | // First call into the provider and get the set of concurrent camera |
| 2809 | // combinations |
| 2810 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = |
Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 2811 | mCameraProviderManager->getConcurrentCameraIds(); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2812 | for (auto &combination : concurrentCameraCombinations) { |
| 2813 | std::vector<std::string> validCombination; |
| 2814 | for (auto &cameraId : combination) { |
| 2815 | // if the camera state is not present, skip |
| 2816 | String8 cameraIdStr(cameraId.c_str()); |
| 2817 | auto state = getCameraState(cameraIdStr); |
| 2818 | if (state == nullptr) { |
| 2819 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); |
| 2820 | continue; |
| 2821 | } |
| 2822 | StatusInternal status = state->getStatus(); |
| 2823 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { |
| 2824 | continue; |
| 2825 | } |
| 2826 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { |
| 2827 | continue; |
| 2828 | } |
| 2829 | validCombination.push_back(cameraId); |
| 2830 | } |
| 2831 | if (validCombination.size() != 0) { |
| 2832 | concurrentCameraIds->push_back(std::move(validCombination)); |
| 2833 | } |
| 2834 | } |
| 2835 | return Status::ok(); |
| 2836 | } |
| 2837 | |
| 2838 | Status CameraService::isConcurrentSessionConfigurationSupported( |
| 2839 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2840 | int targetSdkVersion, /*out*/bool* isSupported) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2841 | if (!isSupported) { |
| 2842 | ALOGE("%s: isSupported is NULL", __FUNCTION__); |
| 2843 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); |
| 2844 | } |
| 2845 | |
| 2846 | if (!mInitialized) { |
| 2847 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 2848 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2849 | "Camera subsystem is not available"); |
| 2850 | } |
| 2851 | |
| 2852 | // Check for camera permissions |
| 2853 | int callingPid = CameraThreadState::getCallingPid(); |
| 2854 | int callingUid = CameraThreadState::getCallingUid(); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 2855 | AttributionSourceState attributionSource{}; |
| 2856 | attributionSource.pid = callingPid; |
| 2857 | attributionSource.uid = callingUid; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2858 | bool checkPermissionForCamera = checkPermission(String8(), |
| 2859 | sCameraPermission, attributionSource, String16(), AppOpsManager::OP_NONE); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 2860 | if ((callingPid != getpid()) && !checkPermissionForCamera) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2861 | ALOGE("%s: pid %d doesn't have camera permissions", __FUNCTION__, callingPid); |
| 2862 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 2863 | "android.permission.CAMERA needed to call" |
| 2864 | "isConcurrentSessionConfigurationSupported"); |
| 2865 | } |
| 2866 | |
| 2867 | status_t res = |
| 2868 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2869 | cameraIdsAndSessionConfigurations, mPerfClassPrimaryCameraIds, |
| 2870 | targetSdkVersion, isSupported); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2871 | if (res != OK) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2872 | logServiceError(String8::format("Unable to query session configuration support"), |
| 2873 | ERROR_INVALID_OPERATION); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2874 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " |
| 2875 | "support %s (%d)", strerror(-res), res); |
| 2876 | } |
| 2877 | return Status::ok(); |
| 2878 | } |
| 2879 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2880 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, |
| 2881 | /*out*/ |
| 2882 | std::vector<hardware::CameraStatus> *cameraStatuses) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2883 | return addListenerHelper(listener, cameraStatuses); |
| 2884 | } |
| 2885 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2886 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, |
| 2887 | std::vector<hardware::CameraStatus>* cameraStatuses) { |
| 2888 | return addListenerHelper(listener, cameraStatuses, false, true); |
| 2889 | } |
| 2890 | |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2891 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, |
| 2892 | /*out*/ |
| 2893 | std::vector<hardware::CameraStatus> *cameraStatuses, |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2894 | bool isVendorListener, bool isProcessLocalTest) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2895 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2896 | ATRACE_CALL(); |
| 2897 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2898 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2899 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 2900 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2901 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2902 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2903 | } |
| 2904 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2905 | auto clientUid = CameraThreadState::getCallingUid(); |
| 2906 | auto clientPid = CameraThreadState::getCallingPid(); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 2907 | AttributionSourceState attributionSource{}; |
| 2908 | attributionSource.uid = clientUid; |
| 2909 | attributionSource.pid = clientPid; |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2910 | |
| 2911 | bool openCloseCallbackAllowed = checkPermission(String8(), |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 2912 | sCameraOpenCloseListenerPermission, attributionSource, String16(), |
Jyoti Bhayana | feb7392 | 2023-03-16 13:01:38 -0700 | [diff] [blame] | 2913 | AppOpsManager::OP_NONE); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2914 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2915 | Mutex::Autolock lock(mServiceLock); |
| 2916 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2917 | { |
| 2918 | Mutex::Autolock lock(mStatusListenerLock); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2919 | for (const auto &it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2920 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2921 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 2922 | __FUNCTION__, listener.get()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2923 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2924 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2925 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2926 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2927 | sp<ServiceListener> serviceListener = |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2928 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, |
| 2929 | openCloseCallbackAllowed); |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2930 | auto ret = serviceListener->initialize(isProcessLocalTest); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2931 | if (ret != NO_ERROR) { |
| 2932 | String8 msg = String8::format("Failed to initialize service listener: %s (%d)", |
| 2933 | strerror(-ret), ret); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2934 | logServiceError(msg,ERROR_ILLEGAL_ARGUMENT); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2935 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2936 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 2937 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2938 | // The listener still needs to be added to the list of listeners, regardless of what |
| 2939 | // permissions the listener process has / whether it is a vendor listener. Since it might be |
| 2940 | // eligible to listen to other camera ids. |
| 2941 | mListenerList.emplace_back(serviceListener); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 2942 | mUidPolicy->registerMonitorUid(clientUid, /*openCamera*/false); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2943 | } |
| 2944 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2945 | /* Collect current devices and status */ |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2946 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2947 | Mutex::Autolock lock(mCameraStatesLock); |
| 2948 | for (auto& i : mCameraStates) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 2949 | cameraStatuses->emplace_back(i.first, |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 2950 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds(), |
| 2951 | openCloseCallbackAllowed ? i.second->getClientPackage() : String8::empty()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2952 | } |
| 2953 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2954 | // Remove the camera statuses that should be hidden from the client, we do |
| 2955 | // this after collecting the states in order to avoid holding |
| 2956 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at |
| 2957 | // the same time. |
| 2958 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), |
| 2959 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { |
| 2960 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 2961 | if (getSystemCameraKind(s.cameraId, &deviceKind) != OK) { |
| 2962 | ALOGE("%s: Invalid camera id %s, skipping status update", |
| 2963 | __FUNCTION__, s.cameraId.c_str()); |
| 2964 | return true; |
| 2965 | } |
| 2966 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, |
| 2967 | clientUid);}), cameraStatuses->end()); |
| 2968 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2969 | //cameraStatuses will have non-eligible camera ids removed. |
| 2970 | std::set<String16> idsChosenForCallback; |
| 2971 | for (const auto &s : *cameraStatuses) { |
| 2972 | idsChosenForCallback.insert(String16(s.cameraId)); |
| 2973 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2974 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2975 | /* |
| 2976 | * Immediately signal current torch status to this listener only |
| 2977 | * This may be a subset of all the devices, so don't include it in the response directly |
| 2978 | */ |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2979 | { |
| 2980 | Mutex::Autolock al(mTorchStatusMutex); |
| 2981 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2982 | String16 id = String16(mTorchStatusMap.keyAt(i).string()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2983 | // The camera id is visible to the client. Fine to send torch |
| 2984 | // callback. |
| 2985 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { |
| 2986 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id); |
| 2987 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2988 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2989 | } |
| 2990 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2991 | return Status::ok(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2992 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2993 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2994 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2995 | ATRACE_CALL(); |
| 2996 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2997 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 2998 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2999 | if (listener == 0) { |
| 3000 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3001 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 3002 | } |
| 3003 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3004 | Mutex::Autolock lock(mServiceLock); |
| 3005 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3006 | { |
| 3007 | Mutex::Autolock lock(mStatusListenerLock); |
| 3008 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3009 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3010 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid(), /*closeCamera*/false); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3011 | IInterface::asBinder(listener)->unlinkToDeath(*it); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3012 | mListenerList.erase(it); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3013 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3014 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | |
| 3018 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 3019 | __FUNCTION__, listener.get()); |
| 3020 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3021 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3022 | } |
| 3023 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3024 | Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3025 | |
| 3026 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3027 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 3028 | |
| 3029 | if (parameters == NULL) { |
| 3030 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3031 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3032 | } |
| 3033 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3034 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3035 | |
| 3036 | CameraParameters shimParams; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3037 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3038 | // Error logged by caller |
| 3039 | return ret; |
| 3040 | } |
| 3041 | |
| 3042 | String8 shimParamsString8 = shimParams.flatten(); |
| 3043 | String16 shimParamsString16 = String16(shimParamsString8); |
| 3044 | |
| 3045 | *parameters = shimParamsString16; |
| 3046 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3047 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3048 | } |
| 3049 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3050 | Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion, |
| 3051 | /*out*/ bool *isSupported) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3052 | ATRACE_CALL(); |
| 3053 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3054 | const String8 id = String8(cameraId); |
| 3055 | |
| 3056 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3057 | |
| 3058 | switch (apiVersion) { |
| 3059 | case API_VERSION_1: |
| 3060 | case API_VERSION_2: |
| 3061 | break; |
| 3062 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3063 | String8 msg = String8::format("Unknown API version %d", apiVersion); |
| 3064 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 3065 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3066 | } |
| 3067 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3068 | int portraitRotation; |
| 3069 | auto deviceVersionAndTransport = getDeviceVersion(id, false, &portraitRotation); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3070 | if (deviceVersionAndTransport.first == -1) { |
| 3071 | String8 msg = String8::format("Unknown camera ID %s", id.string()); |
| 3072 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 3073 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 3074 | } |
| 3075 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 3076 | int deviceVersion = deviceVersionAndTransport.first; |
| 3077 | switch (deviceVersion) { |
| 3078 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 3079 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 3080 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 3081 | if (apiVersion == API_VERSION_2) { |
| 3082 | ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without " |
| 3083 | "shim", __FUNCTION__, id.string(), deviceVersion); |
| 3084 | *isSupported = false; |
| 3085 | } else { // if (apiVersion == API_VERSION_1) { |
| 3086 | ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always " |
| 3087 | "supported", __FUNCTION__, id.string()); |
| 3088 | *isSupported = true; |
| 3089 | } |
| 3090 | break; |
| 3091 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 3092 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 3093 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 3094 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 3095 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 3096 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 3097 | ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly", |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3098 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3099 | *isSupported = true; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3100 | break; |
| 3101 | default: { |
| 3102 | String8 msg = String8::format("Unknown device version %x for device %s", |
| 3103 | deviceVersion, id.string()); |
| 3104 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 3105 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3106 | } |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3107 | } |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3108 | } else { |
| 3109 | *isSupported = true; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3110 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3111 | return Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3112 | } |
| 3113 | |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3114 | Status CameraService::isHiddenPhysicalCamera(const String16& cameraId, |
| 3115 | /*out*/ bool *isSupported) { |
| 3116 | ATRACE_CALL(); |
| 3117 | |
| 3118 | const String8 id = String8(cameraId); |
| 3119 | |
| 3120 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
| 3121 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(id.string()); |
| 3122 | |
| 3123 | return Status::ok(); |
| 3124 | } |
| 3125 | |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3126 | Status CameraService::injectCamera( |
| 3127 | const String16& packageName, const String16& internalCamId, |
| 3128 | const String16& externalCamId, |
| 3129 | const sp<ICameraInjectionCallback>& callback, |
| 3130 | /*out*/ |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3131 | sp<ICameraInjectionSession>* cameraInjectionSession) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3132 | ATRACE_CALL(); |
| 3133 | |
| 3134 | if (!checkCallingPermission(sCameraInjectExternalCameraPermission)) { |
| 3135 | const int pid = CameraThreadState::getCallingPid(); |
| 3136 | const int uid = CameraThreadState::getCallingUid(); |
| 3137 | ALOGE("Permission Denial: can't inject camera pid=%d, uid=%d", pid, uid); |
| 3138 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 3139 | "Permission Denial: no permission to inject camera"); |
| 3140 | } |
| 3141 | |
| 3142 | ALOGV( |
| 3143 | "%s: Package name = %s, Internal camera ID = %s, External camera ID = " |
| 3144 | "%s", |
| 3145 | __FUNCTION__, String8(packageName).string(), |
| 3146 | String8(internalCamId).string(), String8(externalCamId).string()); |
| 3147 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3148 | { |
| 3149 | Mutex::Autolock lock(mInjectionParametersLock); |
| 3150 | mInjectionInternalCamId = String8(internalCamId); |
| 3151 | mInjectionExternalCamId = String8(externalCamId); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3152 | mInjectionStatusListener->addListener(callback); |
| 3153 | *cameraInjectionSession = new CameraInjectionSession(this); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3154 | status_t res = NO_ERROR; |
| 3155 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 3156 | // If the client already exists, we can directly connect to the camera device through the |
| 3157 | // client's injectCamera(), otherwise we need to wait until the client is established |
| 3158 | // (execute connectHelper()) before injecting the camera to the camera device. |
| 3159 | if (clientDescriptor != nullptr) { |
| 3160 | mInjectionInitPending = false; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3161 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 3162 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 3163 | if(res != OK) { |
| 3164 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 3165 | "No camera device with ID \"%s\" currently available", |
| 3166 | mInjectionExternalCamId.string()); |
| 3167 | } |
| 3168 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3169 | if(res != OK) { |
| 3170 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 3171 | } |
| 3172 | } else { |
| 3173 | mInjectionInitPending = true; |
| 3174 | } |
| 3175 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3176 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3177 | return binder::Status::ok(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3178 | } |
| 3179 | |
Avichal Rakesh | 6e57a2b | 2023-05-01 17:53:37 -0700 | [diff] [blame] | 3180 | Status CameraService::reportExtensionSessionStats( |
| 3181 | const hardware::CameraExtensionSessionStats& stats, String16* sessionKey /*out*/) { |
| 3182 | ALOGV("%s: reported %s", __FUNCTION__, stats.toString().c_str()); |
| 3183 | *sessionKey = mCameraServiceProxyWrapper->updateExtensionStats(stats); |
| 3184 | return Status::ok(); |
| 3185 | } |
| 3186 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3187 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3188 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3189 | for (auto& i : mActiveClientManager.getAll()) { |
| 3190 | auto clientSp = i->getValue(); |
| 3191 | if (clientSp.get() == client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3192 | cacheClientTagDumpIfNeeded(client->mCameraIdStr, clientSp.get()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3193 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3194 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3195 | } |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3196 | updateAudioRestrictionLocked(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3197 | } |
| 3198 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3199 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3200 | bool ret = false; |
| 3201 | { |
| 3202 | // Acquire mServiceLock and prevent other clients from connecting |
| 3203 | std::unique_ptr<AutoConditionLock> lock = |
| 3204 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3205 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3206 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3207 | std::vector<sp<BasicClient>> evicted; |
| 3208 | for (auto& i : mActiveClientManager.getAll()) { |
| 3209 | auto clientSp = i->getValue(); |
| 3210 | if (clientSp.get() == nullptr) { |
| 3211 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3212 | mActiveClientManager.remove(i); |
| 3213 | continue; |
| 3214 | } |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 3215 | if (remote == clientSp->getRemote()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3216 | mActiveClientManager.remove(i); |
| 3217 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3218 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3219 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3220 | clientSp->notifyError( |
| 3221 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3222 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3223 | } |
| 3224 | } |
| 3225 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3226 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 3227 | // other clients from connecting in mServiceLockWrapper if held |
| 3228 | mServiceLock.unlock(); |
| 3229 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3230 | // Do not clear caller identity, remote caller should be client proccess |
| 3231 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3232 | for (auto& i : evicted) { |
| 3233 | if (i.get() != nullptr) { |
| 3234 | i->disconnect(); |
| 3235 | ret = true; |
| 3236 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3237 | } |
| 3238 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3239 | // Reacquire mServiceLock |
| 3240 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3241 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3242 | } // lock is destroyed, allow further connect calls |
| 3243 | |
| 3244 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3245 | } |
| 3246 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3247 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
| 3248 | const String8& cameraId) const { |
| 3249 | std::shared_ptr<CameraState> state; |
| 3250 | { |
| 3251 | Mutex::Autolock lock(mCameraStatesLock); |
| 3252 | auto iter = mCameraStates.find(cameraId); |
| 3253 | if (iter != mCameraStates.end()) { |
| 3254 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3255 | } |
| 3256 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3257 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3258 | } |
| 3259 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3260 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) { |
| 3261 | // Remove from active clients list |
| 3262 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 3263 | if (clientDescriptorPtr == nullptr) { |
| 3264 | ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__, |
| 3265 | cameraId.string()); |
| 3266 | return sp<BasicClient>{nullptr}; |
| 3267 | } |
| 3268 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3269 | sp<BasicClient> client = clientDescriptorPtr->getValue(); |
| 3270 | if (client.get() != nullptr) { |
| 3271 | cacheClientTagDumpIfNeeded(clientDescriptorPtr->getKey(), client.get()); |
| 3272 | } |
| 3273 | return client; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 3274 | } |
| 3275 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3276 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3277 | // Acquire mServiceLock and prevent other clients from connecting |
| 3278 | std::unique_ptr<AutoConditionLock> lock = |
| 3279 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 3280 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3281 | std::set<userid_t> newAllowedUsers; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3282 | for (size_t i = 0; i < newUserIds.size(); i++) { |
| 3283 | if (newUserIds[i] < 0) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3284 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3285 | __FUNCTION__, newUserIds[i]); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3286 | return; |
| 3287 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3288 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3289 | } |
| 3290 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3291 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3292 | if (newAllowedUsers == mAllowedUsers) { |
| 3293 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 3294 | return; |
| 3295 | } |
| 3296 | |
| 3297 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 3298 | |
| 3299 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3300 | |
| 3301 | // Current user has switched, evict all current clients. |
| 3302 | std::vector<sp<BasicClient>> evicted; |
| 3303 | for (auto& i : mActiveClientManager.getAll()) { |
| 3304 | auto clientSp = i->getValue(); |
| 3305 | |
| 3306 | if (clientSp.get() == nullptr) { |
| 3307 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3308 | continue; |
| 3309 | } |
| 3310 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3311 | // Don't evict clients that are still allowed. |
| 3312 | uid_t clientUid = clientSp->getClientUid(); |
| 3313 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 3314 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 3315 | continue; |
| 3316 | } |
| 3317 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3318 | evicted.push_back(clientSp); |
| 3319 | |
| 3320 | String8 curTime = getFormattedCurrentTime(); |
| 3321 | |
| 3322 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
| 3323 | i->getKey().string()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3324 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3325 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3326 | logEvent(String8::format("EVICT device %s client held by package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3327 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due" |
| 3328 | " to user switch.", i->getKey().string(), |
| 3329 | String8{clientSp->getPackageName()}.string(), |
| 3330 | i->getOwnerId(), i->getPriority().getScore(), |
| 3331 | i->getPriority().getState())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3332 | |
| 3333 | } |
| 3334 | |
| 3335 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 3336 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 3337 | mServiceLock.unlock(); |
| 3338 | |
| 3339 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3340 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3341 | |
| 3342 | for (auto& i : evicted) { |
| 3343 | i->disconnect(); |
| 3344 | } |
| 3345 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3346 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3347 | |
| 3348 | // Reacquire mServiceLock |
| 3349 | mServiceLock.lock(); |
| 3350 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3351 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3352 | void CameraService::logEvent(const char* event) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3353 | String8 curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3354 | Mutex::Autolock l(mLogLock); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3355 | String8 msg = String8::format("%s : %s", curTime.string(), event); |
| 3356 | // For service error events, print the msg only once. |
| 3357 | if(!msg.contains("SERVICE ERROR")) { |
| 3358 | mEventLog.add(msg); |
| 3359 | } else if(sServiceErrorEventSet.find(msg) == sServiceErrorEventSet.end()) { |
| 3360 | // Error event not added to the dumpsys log before |
| 3361 | mEventLog.add(msg); |
| 3362 | sServiceErrorEventSet.insert(msg); |
| 3363 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3364 | } |
| 3365 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3366 | void CameraService::logDisconnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3367 | const char* clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3368 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3369 | logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3370 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3371 | } |
| 3372 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3373 | void CameraService::logDisconnectedOffline(const char* cameraId, int clientPid, |
| 3374 | const char* clientPackage) { |
| 3375 | // Log the clients evicted |
| 3376 | logEvent(String8::format("DISCONNECT offline device %s client for package %s (PID %d)", |
| 3377 | cameraId, clientPackage, clientPid)); |
| 3378 | } |
| 3379 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3380 | void CameraService::logConnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3381 | const char* clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3382 | // Log the clients evicted |
| 3383 | logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3384 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3385 | } |
| 3386 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3387 | void CameraService::logConnectedOffline(const char* cameraId, int clientPid, |
| 3388 | const char* clientPackage) { |
| 3389 | // Log the clients evicted |
| 3390 | logEvent(String8::format("CONNECT offline device %s client for package %s (PID %d)", cameraId, |
| 3391 | clientPackage, clientPid)); |
| 3392 | } |
| 3393 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3394 | void CameraService::logRejected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3395 | const char* clientPackage, const char* reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3396 | // Log the client rejected |
| 3397 | logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)", |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3398 | cameraId, clientPackage, clientPid, reason)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3399 | } |
| 3400 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3401 | void CameraService::logTorchEvent(const char* cameraId, const char *torchState, int clientPid) { |
| 3402 | // Log torch event |
| 3403 | logEvent(String8::format("Torch for camera id %s turned %s for client PID %d", cameraId, |
| 3404 | torchState, clientPid)); |
| 3405 | } |
| 3406 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3407 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 3408 | const std::set<userid_t>& newUserIds) { |
| 3409 | String8 newUsers = toString(newUserIds); |
| 3410 | String8 oldUsers = toString(oldUserIds); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3411 | if (oldUsers.size() == 0) { |
| 3412 | oldUsers = "<None>"; |
| 3413 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3414 | // Log the new and old users |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3415 | logEvent(String8::format("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s", |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3416 | oldUsers.string(), newUsers.string())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3417 | } |
| 3418 | |
| 3419 | void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) { |
| 3420 | // Log the device removal |
| 3421 | logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason)); |
| 3422 | } |
| 3423 | |
| 3424 | void CameraService::logDeviceAdded(const char* cameraId, const char* reason) { |
| 3425 | // Log the device removal |
| 3426 | logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason)); |
| 3427 | } |
| 3428 | |
| 3429 | void CameraService::logClientDied(int clientPid, const char* reason) { |
| 3430 | // Log the device removal |
| 3431 | logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason)); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3432 | } |
| 3433 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3434 | void CameraService::logServiceError(const char* msg, int errorCode) { |
| 3435 | String8 curTime = getFormattedCurrentTime(); |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 3436 | logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode))); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3437 | } |
| 3438 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3439 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 3440 | uint32_t flags) { |
| 3441 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3442 | // Permission checks |
| 3443 | switch (code) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3444 | case SHELL_COMMAND_TRANSACTION: { |
| 3445 | int in = data.readFileDescriptor(); |
| 3446 | int out = data.readFileDescriptor(); |
| 3447 | int err = data.readFileDescriptor(); |
| 3448 | int argc = data.readInt32(); |
| 3449 | Vector<String16> args; |
| 3450 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 3451 | args.add(data.readString16()); |
| 3452 | } |
| 3453 | sp<IBinder> unusedCallback; |
| 3454 | sp<IResultReceiver> resultReceiver; |
| 3455 | status_t status; |
| 3456 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 3457 | return status; |
| 3458 | } |
| 3459 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 3460 | return status; |
| 3461 | } |
| 3462 | status = shellCommand(in, out, err, args); |
| 3463 | if (resultReceiver != nullptr) { |
| 3464 | resultReceiver->send(status); |
| 3465 | } |
| 3466 | return NO_ERROR; |
| 3467 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | return BnCameraService::onTransact(code, data, reply, flags); |
| 3471 | } |
| 3472 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3473 | // We share the media players for shutter and recording sound for all clients. |
| 3474 | // A reference count is kept to determine when we will actually release the |
| 3475 | // media players. |
| 3476 | |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3477 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { |
| 3478 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 3479 | status_t error; |
| 3480 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 3481 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3482 | error = mp->prepare(); |
| 3483 | } |
| 3484 | if (error != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3485 | ALOGE("Failed to load CameraService sounds: %s", file); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3486 | mp->disconnect(); |
| 3487 | mp.clear(); |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3488 | return nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3489 | } |
| 3490 | return mp; |
| 3491 | } |
| 3492 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3493 | void CameraService::increaseSoundRef() { |
| 3494 | Mutex::Autolock lock(mSoundLock); |
| 3495 | mSoundRef++; |
| 3496 | } |
| 3497 | |
| 3498 | void CameraService::loadSoundLocked(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3499 | ATRACE_CALL(); |
| 3500 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3501 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); |
| 3502 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { |
| 3503 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); |
| 3504 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { |
| 3505 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 3506 | } |
| 3507 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) { |
| 3508 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); |
| 3509 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { |
| 3510 | mSoundPlayer[SOUND_RECORDING_START] = |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3511 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3512 | } |
| 3513 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { |
| 3514 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); |
| 3515 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { |
| 3516 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
| 3517 | } |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3518 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3519 | } |
| 3520 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3521 | void CameraService::decreaseSoundRef() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3522 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3523 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3524 | if (--mSoundRef) return; |
| 3525 | |
| 3526 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 3527 | if (mSoundPlayer[i] != 0) { |
| 3528 | mSoundPlayer[i]->disconnect(); |
| 3529 | mSoundPlayer[i].clear(); |
| 3530 | } |
| 3531 | } |
| 3532 | } |
| 3533 | |
| 3534 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3535 | ATRACE_CALL(); |
| 3536 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3537 | LOG1("playSound(%d)", kind); |
Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame] | 3538 | if (kind < 0 || kind >= NUM_SOUNDS) { |
| 3539 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); |
| 3540 | return; |
| 3541 | } |
| 3542 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3543 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3544 | loadSoundLocked(kind); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3545 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 3546 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 3547 | player->seekTo(0); |
| 3548 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3549 | } |
| 3550 | } |
| 3551 | |
| 3552 | // ---------------------------------------------------------------------------- |
| 3553 | |
| 3554 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 3555 | const sp<ICameraClient>& cameraClient, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3556 | const String16& clientPackageName, bool systemNativeClient, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 3557 | const std::optional<String16>& clientFeatureId, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3558 | const String8& cameraIdStr, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3559 | int api1CameraId, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3560 | int clientPid, uid_t clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3561 | int servicePid, bool overrideToPortrait) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3562 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 3563 | IInterface::asBinder(cameraClient), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3564 | clientPackageName, systemNativeClient, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3565 | cameraIdStr, cameraFacing, sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3566 | clientPid, clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3567 | servicePid, overrideToPortrait), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3568 | mCameraId(api1CameraId) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3569 | { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3570 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3571 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3572 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 3573 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3574 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3575 | cameraService->increaseSoundRef(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3576 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3577 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3578 | } |
| 3579 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3580 | // tear down the client |
| 3581 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3582 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3583 | mDestructionStarted = true; |
| 3584 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3585 | sCameraService->decreaseSoundRef(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3586 | // unconditionally disconnect. function is idempotent |
| 3587 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3588 | } |
| 3589 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3590 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; |
| 3591 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3592 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3593 | const sp<IBinder>& remoteCallback, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3594 | const String16& clientPackageName, bool nativeClient, |
| 3595 | const std::optional<String16>& clientFeatureId, const String8& cameraIdStr, |
| 3596 | int cameraFacing, int sensorOrientation, int clientPid, uid_t clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3597 | int servicePid, bool overrideToPortrait): |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3598 | mDestructionStarted(false), |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3599 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), mOrientation(sensorOrientation), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3600 | mClientPackageName(clientPackageName), mSystemNativeClient(nativeClient), |
| 3601 | mClientFeatureId(clientFeatureId), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 3602 | mClientPid(clientPid), mClientUid(clientUid), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3603 | mServicePid(servicePid), |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3604 | mDisconnected(false), mUidIsTrusted(false), |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3605 | mOverrideToPortrait(overrideToPortrait), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3606 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3607 | mRemoteBinder(remoteCallback), |
| 3608 | mOpsActive(false), |
| 3609 | mOpsStreaming(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3610 | { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3611 | if (sCameraService == nullptr) { |
| 3612 | sCameraService = cameraService; |
| 3613 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 3614 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3615 | // There are 2 scenarios in which a client won't have AppOps operations |
| 3616 | // (both scenarios : native clients) |
| 3617 | // 1) It's an system native client*, the package name will be empty |
| 3618 | // and it will return from this function in the previous if condition |
| 3619 | // (This is the same as the previously existing behavior). |
| 3620 | // 2) It is a system native client, but its package name has been |
| 3621 | // modified for debugging, however it still must not use AppOps since |
| 3622 | // the package name is not a real one. |
| 3623 | // |
| 3624 | // * system native client - native client with UID < AID_APP_START. It |
| 3625 | // doesn't exclude clients not on the system partition. |
| 3626 | if (!mSystemNativeClient) { |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3627 | mAppOpsManager = std::make_unique<AppOpsManager>(); |
| 3628 | } |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3629 | |
| 3630 | mUidIsTrusted = isTrustedCallingUid(mClientUid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3631 | } |
| 3632 | |
| 3633 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3634 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3635 | mDestructionStarted = true; |
| 3636 | } |
| 3637 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3638 | binder::Status CameraService::BasicClient::disconnect() { |
| 3639 | binder::Status res = Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3640 | if (mDisconnected) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3641 | return res; |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3642 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 3643 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3644 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3645 | sCameraService->removeByClient(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3646 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, String8(mClientPackageName)); |
Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 3647 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, |
| 3648 | mCameraIdStr.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3649 | |
| 3650 | sp<IBinder> remote = getRemote(); |
| 3651 | if (remote != nullptr) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3652 | remote->unlinkToDeath(sCameraService); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3653 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3654 | |
| 3655 | finishCameraOps(); |
Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 3656 | // Notify flashlight that a camera device is closed. |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3657 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); |
| 3658 | ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(), |
| 3659 | mClientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3660 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3661 | // client shouldn't be able to call into us anymore |
| 3662 | mClientPid = 0; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3663 | |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 3664 | const auto& mActivityManager = getActivityManager(); |
| 3665 | if (mActivityManager) { |
| 3666 | mActivityManager->logFgsApiEnd(LOG_FGS_CAMERA_API, |
| 3667 | CameraThreadState::getCallingUid(), |
| 3668 | CameraThreadState::getCallingPid()); |
| 3669 | } |
| 3670 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3671 | return res; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3672 | } |
| 3673 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3674 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 3675 | // No dumping of clients directly over Binder, |
| 3676 | // must go through CameraService::dump |
| 3677 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3678 | CameraThreadState::getCallingUid(), NULL, 0); |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3679 | return OK; |
| 3680 | } |
| 3681 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 3682 | status_t CameraService::BasicClient::startWatchingTags(const String8&, int) { |
| 3683 | // Can't watch tags directly, must go through CameraService::startWatchingTags |
| 3684 | return OK; |
| 3685 | } |
| 3686 | |
| 3687 | status_t CameraService::BasicClient::stopWatchingTags(int) { |
| 3688 | // Can't watch tags directly, must go through CameraService::stopWatchingTags |
| 3689 | return OK; |
| 3690 | } |
| 3691 | |
| 3692 | status_t CameraService::BasicClient::dumpWatchedEventsToVector(std::vector<std::string> &) { |
| 3693 | // Can't watch tags directly, must go through CameraService::dumpWatchedEventsToVector |
| 3694 | return OK; |
| 3695 | } |
| 3696 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3697 | String16 CameraService::BasicClient::getPackageName() const { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3698 | return mClientPackageName; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3699 | } |
| 3700 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3701 | int CameraService::BasicClient::getCameraFacing() const { |
| 3702 | return mCameraFacing; |
| 3703 | } |
| 3704 | |
| 3705 | int CameraService::BasicClient::getCameraOrientation() const { |
| 3706 | return mOrientation; |
| 3707 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3708 | |
| 3709 | int CameraService::BasicClient::getClientPid() const { |
| 3710 | return mClientPid; |
| 3711 | } |
| 3712 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3713 | uid_t CameraService::BasicClient::getClientUid() const { |
| 3714 | return mClientUid; |
| 3715 | } |
| 3716 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3717 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 3718 | // Defaults to API2. |
| 3719 | return level == API_2; |
| 3720 | } |
| 3721 | |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3722 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3723 | { |
| 3724 | Mutex::Autolock l(mAudioRestrictionLock); |
| 3725 | mAudioRestriction = mode; |
| 3726 | } |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3727 | sCameraService->updateAudioRestriction(); |
| 3728 | return OK; |
| 3729 | } |
| 3730 | |
| 3731 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3732 | return sCameraService->updateAudioRestriction(); |
| 3733 | } |
| 3734 | |
| 3735 | int32_t CameraService::BasicClient::getAudioRestriction() const { |
| 3736 | Mutex::Autolock l(mAudioRestrictionLock); |
| 3737 | return mAudioRestriction; |
| 3738 | } |
| 3739 | |
| 3740 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { |
| 3741 | switch (mode) { |
| 3742 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: |
| 3743 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: |
| 3744 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: |
| 3745 | return true; |
| 3746 | default: |
| 3747 | return false; |
| 3748 | } |
| 3749 | } |
| 3750 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3751 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { |
| 3752 | if (mode == AppOpsManager::MODE_ERRORED) { |
| 3753 | ALOGI("Camera %s: Access for \"%s\" has been revoked", |
| 3754 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 3755 | return PERMISSION_DENIED; |
| 3756 | } else if (!mUidIsTrusted && mode == AppOpsManager::MODE_IGNORED) { |
| 3757 | // If the calling Uid is trusted (a native service), the AppOpsManager could |
| 3758 | // return MODE_IGNORED. Do not treat such case as error. |
| 3759 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, |
| 3760 | mClientPackageName); |
| 3761 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3762 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | 8143e57 | 2023-01-09 08:46:49 -0800 | [diff] [blame] | 3763 | // We don't want to return EACCESS if the CameraPrivacy is enabled. |
| 3764 | // We prefer to successfully open the camera and perform camera muting |
| 3765 | // or blocking in connectHelper as handleAppOpMode can be called before the |
| 3766 | // connection has been fully established and at that time camera muting |
| 3767 | // capabilities are unknown. |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3768 | if (!isUidActive || !isCameraPrivacyEnabled) { |
| 3769 | ALOGI("Camera %s: Access for \"%s\" has been restricted", |
| 3770 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 3771 | // Return the same error as for device policy manager rejection |
| 3772 | return -EACCES; |
| 3773 | } |
| 3774 | } |
| 3775 | return OK; |
| 3776 | } |
| 3777 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3778 | status_t CameraService::BasicClient::startCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3779 | ATRACE_CALL(); |
| 3780 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3781 | { |
| 3782 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3783 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3784 | } |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3785 | if (mAppOpsManager != nullptr) { |
| 3786 | // Notify app ops that the camera is not available |
| 3787 | mOpsCallback = new OpsCallback(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3788 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 3789 | mClientPackageName, mOpsCallback); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3790 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3791 | // Just check for camera acccess here on open - delay startOp until |
| 3792 | // camera frames start streaming in startCameraStreamingOps |
| 3793 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3794 | mClientPackageName); |
| 3795 | status_t res = handleAppOpMode(mode); |
| 3796 | if (res != OK) { |
| 3797 | return res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3798 | } |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 3799 | } |
| 3800 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3801 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3802 | |
| 3803 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3804 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3805 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3806 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid, /*openCamera*/true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3807 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3808 | // Notify listeners of camera open/close status |
| 3809 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); |
| 3810 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3811 | return OK; |
| 3812 | } |
| 3813 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3814 | status_t CameraService::BasicClient::startCameraStreamingOps() { |
| 3815 | ATRACE_CALL(); |
| 3816 | |
| 3817 | if (!mOpsActive) { |
| 3818 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3819 | return INVALID_OPERATION; |
| 3820 | } |
| 3821 | if (mOpsStreaming) { |
| 3822 | ALOGV("%s: Streaming already active!", __FUNCTION__); |
| 3823 | return OK; |
| 3824 | } |
| 3825 | |
| 3826 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", |
| 3827 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3828 | |
| 3829 | if (mAppOpsManager != nullptr) { |
| 3830 | int32_t mode = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, |
| 3831 | mClientPackageName, /*startIfModeDefault*/ false, mClientFeatureId, |
| 3832 | String16("start camera ") + String16(mCameraIdStr)); |
| 3833 | status_t res = handleAppOpMode(mode); |
| 3834 | if (res != OK) { |
| 3835 | return res; |
| 3836 | } |
| 3837 | } |
| 3838 | |
| 3839 | mOpsStreaming = true; |
| 3840 | |
| 3841 | return OK; |
| 3842 | } |
| 3843 | |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 3844 | status_t CameraService::BasicClient::noteAppOp() { |
| 3845 | ATRACE_CALL(); |
| 3846 | |
| 3847 | ALOGV("%s: Start camera noteAppOp, package name = %s, client UID = %d", |
| 3848 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3849 | |
| 3850 | // noteAppOp is only used for when camera mute is not supported, in order |
| 3851 | // to trigger the sensor privacy "Unblock" dialog |
| 3852 | if (mAppOpsManager != nullptr) { |
| 3853 | int32_t mode = mAppOpsManager->noteOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3854 | mClientPackageName, mClientFeatureId, |
| 3855 | String16("start camera ") + String16(mCameraIdStr)); |
| 3856 | status_t res = handleAppOpMode(mode); |
| 3857 | if (res != OK) { |
| 3858 | return res; |
| 3859 | } |
| 3860 | } |
| 3861 | |
| 3862 | return OK; |
| 3863 | } |
| 3864 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3865 | status_t CameraService::BasicClient::finishCameraStreamingOps() { |
| 3866 | ATRACE_CALL(); |
| 3867 | |
| 3868 | if (!mOpsActive) { |
| 3869 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3870 | return INVALID_OPERATION; |
| 3871 | } |
| 3872 | if (!mOpsStreaming) { |
| 3873 | ALOGV("%s: Streaming not active!", __FUNCTION__); |
| 3874 | return OK; |
| 3875 | } |
| 3876 | |
| 3877 | if (mAppOpsManager != nullptr) { |
| 3878 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3879 | mClientPackageName, mClientFeatureId); |
| 3880 | mOpsStreaming = false; |
| 3881 | } |
| 3882 | |
| 3883 | return OK; |
| 3884 | } |
| 3885 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3886 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3887 | ATRACE_CALL(); |
| 3888 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3889 | if (mOpsStreaming) { |
| 3890 | // Make sure we've notified everyone about camera stopping |
| 3891 | finishCameraStreamingOps(); |
| 3892 | } |
| 3893 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3894 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3895 | if (mOpsActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3896 | mOpsActive = false; |
| 3897 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3898 | // This function is called when a client disconnects. This should |
| 3899 | // release the camera, but actually only if it was in a proper |
| 3900 | // functional state, i.e. with status NOT_AVAILABLE |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3901 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3902 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3903 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3904 | // Transition to PRESENT if the camera is not in either of the rejected states |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3905 | sCameraService->updateStatus(StatusInternal::PRESENT, |
| 3906 | mCameraIdStr, rejected); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3907 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3908 | // Always stop watching, even if no camera op is active |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3909 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 3910 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3911 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3912 | mOpsCallback.clear(); |
| 3913 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3914 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid, /*closeCamera*/true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3915 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3916 | // Notify listeners of camera open/close status |
| 3917 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); |
| 3918 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3919 | return OK; |
| 3920 | } |
| 3921 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3922 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3923 | ATRACE_CALL(); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3924 | if (mAppOpsManager == nullptr) { |
| 3925 | return; |
| 3926 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3927 | // TODO : add offline camera session case |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3928 | if (op != AppOpsManager::OP_CAMERA) { |
| 3929 | ALOGW("Unexpected app ops notification received: %d", op); |
| 3930 | return; |
| 3931 | } |
| 3932 | |
| 3933 | int32_t res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3934 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3935 | mClientUid, mClientPackageName); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3936 | ALOGV("checkOp returns: %d, %s ", res, |
| 3937 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 3938 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 3939 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 3940 | "UNKNOWN"); |
| 3941 | |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3942 | if (res == AppOpsManager::MODE_ERRORED) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3943 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(), |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3944 | String8(mClientPackageName).string()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3945 | block(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3946 | } else if (res == AppOpsManager::MODE_IGNORED) { |
| 3947 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3948 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3949 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3950 | ALOGI("Camera %s: Access for \"%s\" has been restricted, isUidTrusted %d, isUidActive %d", |
| 3951 | mCameraIdStr.string(), String8(mClientPackageName).string(), |
| 3952 | mUidIsTrusted, isUidActive); |
| 3953 | // If the calling Uid is trusted (a native service), or the client Uid is active (WAR for |
| 3954 | // b/175320666), the AppOpsManager could return MODE_IGNORED. Do not treat such cases as |
| 3955 | // error. |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 3956 | if (!mUidIsTrusted) { |
| 3957 | if (isUidActive && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 3958 | setCameraMute(true); |
| 3959 | } else if (!isUidActive |
| 3960 | || (isCameraPrivacyEnabled && !supportsCameraMute())) { |
| 3961 | block(); |
| 3962 | } |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3963 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3964 | } else if (res == AppOpsManager::MODE_ALLOWED) { |
| 3965 | setCameraMute(sCameraService->mOverrideCameraMuteMode); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3966 | } |
| 3967 | } |
| 3968 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3969 | void CameraService::BasicClient::block() { |
| 3970 | ATRACE_CALL(); |
| 3971 | |
| 3972 | // Reset the client PID to allow server-initiated disconnect, |
| 3973 | // and to prevent further calls by client. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3974 | mClientPid = CameraThreadState::getCallingPid(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3975 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 3976 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); |
| 3977 | disconnect(); |
| 3978 | } |
| 3979 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3980 | // ---------------------------------------------------------------------------- |
| 3981 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3982 | void CameraService::Client::notifyError(int32_t errorCode, |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 3983 | [[maybe_unused]] const CaptureResultExtras& resultExtras) { |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3984 | if (mRemoteCallback != NULL) { |
Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 3985 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; |
| 3986 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { |
| 3987 | api1ErrorCode = CAMERA_ERROR_DISABLED; |
| 3988 | } |
| 3989 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3990 | } else { |
| 3991 | ALOGE("mRemoteCallback is NULL!!"); |
| 3992 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3993 | } |
| 3994 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3995 | // NOTE: function is idempotent |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3996 | binder::Status CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3997 | ALOGV("Client::disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3998 | return BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 3999 | } |
| 4000 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 4001 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 4002 | return level == API_1; |
| 4003 | } |
| 4004 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 4005 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 4006 | mClient(client) { |
| 4007 | } |
| 4008 | |
| 4009 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 4010 | const String16& packageName) { |
| 4011 | sp<BasicClient> client = mClient.promote(); |
| 4012 | if (client != NULL) { |
| 4013 | client->opChanged(op, packageName); |
| 4014 | } |
| 4015 | } |
| 4016 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4017 | // ---------------------------------------------------------------------------- |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4018 | // UidPolicy |
| 4019 | // ---------------------------------------------------------------------------- |
| 4020 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4021 | void CameraService::UidPolicy::registerWithActivityManager() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4022 | Mutex::Autolock _l(mUidLock); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4023 | int32_t emptyUidArray[] = { }; |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4024 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4025 | if (mRegistered) return; |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 4026 | status_t res = mAm.linkToDeath(this); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4027 | mAm.registerUidObserverForUids(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4028 | | ActivityManager::UID_OBSERVER_IDLE |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4029 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE |
| 4030 | | ActivityManager::UID_OBSERVER_PROC_OOM_ADJ, |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4031 | ActivityManager::PROCESS_STATE_UNKNOWN, |
Austin Borger | b251892 | 2023-05-05 15:50:30 -0700 | [diff] [blame] | 4032 | kServiceName, emptyUidArray, 0, mObserverToken); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4033 | if (res == OK) { |
| 4034 | mRegistered = true; |
| 4035 | ALOGV("UidPolicy: Registered with ActivityManager"); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4036 | } else { |
| 4037 | ALOGE("UidPolicy: Failed to register with ActivityManager: 0x%08x", res); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4038 | } |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4039 | } |
| 4040 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4041 | void CameraService::UidPolicy::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
| 4042 | if (name != String16(kActivityServiceName)) { |
| 4043 | return; |
| 4044 | } |
| 4045 | |
| 4046 | registerWithActivityManager(); |
| 4047 | } |
| 4048 | |
| 4049 | void CameraService::UidPolicy::registerSelf() { |
| 4050 | // Use check service to see if the activity service is available |
| 4051 | // If not available then register for notifications, instead of blocking |
| 4052 | // till the service is ready |
| 4053 | sp<IServiceManager> sm = defaultServiceManager(); |
| 4054 | sp<IBinder> binder = sm->checkService(String16(kActivityServiceName)); |
| 4055 | if (!binder) { |
| 4056 | sm->registerForNotifications(String16(kActivityServiceName), this); |
| 4057 | } else { |
| 4058 | registerWithActivityManager(); |
| 4059 | } |
| 4060 | } |
| 4061 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4062 | void CameraService::UidPolicy::unregisterSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4063 | Mutex::Autolock _l(mUidLock); |
| 4064 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 4065 | mAm.unregisterUidObserver(this); |
| 4066 | mAm.unlinkToDeath(this); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4067 | mRegistered = false; |
| 4068 | mActiveUids.clear(); |
| 4069 | ALOGV("UidPolicy: Unregistered with ActivityManager"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { |
| 4073 | onUidIdle(uid, disabled); |
| 4074 | } |
| 4075 | |
| 4076 | void CameraService::UidPolicy::onUidActive(uid_t uid) { |
| 4077 | Mutex::Autolock _l(mUidLock); |
| 4078 | mActiveUids.insert(uid); |
| 4079 | } |
| 4080 | |
| 4081 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { |
| 4082 | bool deleted = false; |
| 4083 | { |
| 4084 | Mutex::Autolock _l(mUidLock); |
| 4085 | if (mActiveUids.erase(uid) > 0) { |
| 4086 | deleted = true; |
| 4087 | } |
| 4088 | } |
| 4089 | if (deleted) { |
| 4090 | sp<CameraService> service = mService.promote(); |
| 4091 | if (service != nullptr) { |
| 4092 | service->blockClientsForUid(uid); |
| 4093 | } |
| 4094 | } |
| 4095 | } |
| 4096 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4097 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 4098 | int64_t procStateSeq __unused, int32_t capability __unused) { |
Austin Borger | c5585dc | 2022-05-12 00:48:17 +0000 | [diff] [blame] | 4099 | bool procStateChange = false; |
| 4100 | { |
| 4101 | Mutex::Autolock _l(mUidLock); |
| 4102 | if (mMonitoredUids.find(uid) != mMonitoredUids.end() && |
| 4103 | mMonitoredUids[uid].procState != procState) { |
| 4104 | mMonitoredUids[uid].procState = procState; |
| 4105 | procStateChange = true; |
| 4106 | } |
| 4107 | } |
| 4108 | |
| 4109 | if (procStateChange) { |
| 4110 | sp<CameraService> service = mService.promote(); |
| 4111 | if (service != nullptr) { |
| 4112 | service->notifyMonitoredUids(); |
| 4113 | } |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4114 | } |
| 4115 | } |
| 4116 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4117 | /** |
| 4118 | * When the OOM adj of the uid owning the camera changes, a different uid waiting on camera |
| 4119 | * privileges may take precedence if the owner's new OOM adj is greater than the waiting package. |
| 4120 | * Here, we track which monitoredUid has the camera, and track its adj relative to other |
| 4121 | * monitoredUids. If it is revised above some other monitoredUid, signal |
| 4122 | * onCameraAccessPrioritiesChanged. This only needs to capture the case where there are two |
| 4123 | * foreground apps in split screen - state changes will capture all other cases. |
| 4124 | */ |
| 4125 | void CameraService::UidPolicy::onUidProcAdjChanged(uid_t uid, int32_t adj) { |
| 4126 | std::unordered_set<uid_t> notifyUidSet; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4127 | { |
| 4128 | Mutex::Autolock _l(mUidLock); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4129 | auto it = mMonitoredUids.find(uid); |
| 4130 | |
| 4131 | if (it != mMonitoredUids.end()) { |
| 4132 | if (it->second.hasCamera) { |
| 4133 | for (auto &monitoredUid : mMonitoredUids) { |
| 4134 | if (monitoredUid.first != uid && adj > monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4135 | ALOGV("%s: notify uid %d", __FUNCTION__, monitoredUid.first); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4136 | notifyUidSet.emplace(monitoredUid.first); |
| 4137 | } |
| 4138 | } |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4139 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4140 | notifyUidSet.emplace(uid); |
| 4141 | } else { |
| 4142 | for (auto &monitoredUid : mMonitoredUids) { |
| 4143 | if (monitoredUid.second.hasCamera && adj < monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4144 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4145 | notifyUidSet.emplace(uid); |
| 4146 | } |
| 4147 | } |
| 4148 | } |
| 4149 | it->second.procAdj = adj; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4150 | } |
| 4151 | } |
| 4152 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4153 | if (notifyUidSet.size() > 0) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4154 | sp<CameraService> service = mService.promote(); |
| 4155 | if (service != nullptr) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4156 | service->notifyMonitoredUids(notifyUidSet); |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4157 | } |
| 4158 | } |
| 4159 | } |
| 4160 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4161 | /** |
| 4162 | * Register a uid for monitoring, and note whether it owns a camera. |
| 4163 | */ |
| 4164 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid, bool openCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4165 | Mutex::Autolock _l(mUidLock); |
| 4166 | auto it = mMonitoredUids.find(uid); |
| 4167 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4168 | it->second.refCount++; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4169 | } else { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4170 | MonitoredUid monitoredUid; |
| 4171 | monitoredUid.procState = ActivityManager::PROCESS_STATE_NONEXISTENT; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4172 | monitoredUid.procAdj = resource_policy::UNKNOWN_ADJ; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4173 | monitoredUid.refCount = 1; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4174 | it = mMonitoredUids.emplace(std::pair<uid_t, MonitoredUid>(uid, monitoredUid)).first; |
Austin Borger | b251892 | 2023-05-05 15:50:30 -0700 | [diff] [blame] | 4175 | status_t res = mAm.addUidToObserver(mObserverToken, kServiceName, uid); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4176 | if (res != OK) { |
| 4177 | ALOGE("UidPolicy: Failed to add uid to observer: 0x%08x", res); |
| 4178 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | if (openCamera) { |
| 4182 | it->second.hasCamera = true; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4183 | } |
| 4184 | } |
| 4185 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4186 | /** |
| 4187 | * Unregister a uid for monitoring, and note whether it lost ownership of a camera. |
| 4188 | */ |
| 4189 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid, bool closeCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4190 | Mutex::Autolock _l(mUidLock); |
| 4191 | auto it = mMonitoredUids.find(uid); |
| 4192 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4193 | it->second.refCount--; |
| 4194 | if (it->second.refCount == 0) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4195 | mMonitoredUids.erase(it); |
Austin Borger | b251892 | 2023-05-05 15:50:30 -0700 | [diff] [blame] | 4196 | status_t res = mAm.removeUidFromObserver(mObserverToken, kServiceName, uid); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4197 | if (res != OK) { |
| 4198 | ALOGE("UidPolicy: Failed to remove uid from observer: 0x%08x", res); |
| 4199 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4200 | } else if (closeCamera) { |
| 4201 | it->second.hasCamera = false; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4202 | } |
| 4203 | } else { |
| 4204 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 4205 | } |
| 4206 | } |
| 4207 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4208 | bool CameraService::UidPolicy::isUidActive(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4209 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4210 | return isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4211 | } |
| 4212 | |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4213 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; |
| 4214 | static const int64_t kPollUidActiveTimeoutMillis = 50; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4215 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4216 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4217 | // Non-app UIDs are considered always active |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4218 | // If activity manager is unreachable, assume everything is active |
| 4219 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4220 | return true; |
| 4221 | } |
| 4222 | auto it = mOverrideUids.find(uid); |
| 4223 | if (it != mOverrideUids.end()) { |
| 4224 | return it->second; |
| 4225 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4226 | bool active = mActiveUids.find(uid) != mActiveUids.end(); |
| 4227 | if (!active) { |
| 4228 | // We want active UIDs to always access camera with their first attempt since |
| 4229 | // there is no guarantee the app is robustly written and would retry getting |
| 4230 | // the camera on failure. The inverse case is not a problem as we would take |
| 4231 | // camera away soon once we get the callback that the uid is no longer active. |
| 4232 | ActivityManager am; |
| 4233 | // Okay to access with a lock held as UID changes are dispatched without |
| 4234 | // a lock and we are a higher level component. |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4235 | int64_t startTimeMillis = 0; |
| 4236 | do { |
| 4237 | // TODO: Fix this b/109950150! |
| 4238 | // Okay this is a hack. There is a race between the UID turning active and |
| 4239 | // activity being resumed. The proper fix is very risky, so we temporary add |
| 4240 | // some polling which should happen pretty rarely anyway as the race is hard |
| 4241 | // to hit. |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4242 | active = mActiveUids.find(uid) != mActiveUids.end(); |
Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 4243 | if (!active) active = am.isUidActive(uid, callingPackage); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4244 | if (active) { |
| 4245 | break; |
| 4246 | } |
| 4247 | if (startTimeMillis <= 0) { |
| 4248 | startTimeMillis = uptimeMillis(); |
| 4249 | } |
| 4250 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4251 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4252 | if (remainingTimeMillis <= 0) { |
| 4253 | break; |
| 4254 | } |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4255 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); |
| 4256 | |
| 4257 | mUidLock.unlock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4258 | usleep(remainingTimeMillis * 1000); |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4259 | mUidLock.lock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4260 | } while (true); |
| 4261 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4262 | if (active) { |
| 4263 | // Now that we found out the UID is actually active, cache that |
| 4264 | mActiveUids.insert(uid); |
| 4265 | } |
| 4266 | } |
| 4267 | return active; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4268 | } |
| 4269 | |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4270 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { |
| 4271 | Mutex::Autolock _l(mUidLock); |
| 4272 | return getProcStateLocked(uid); |
| 4273 | } |
| 4274 | |
| 4275 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { |
| 4276 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 4277 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4278 | procState = mMonitoredUids[uid].procState; |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4279 | } |
| 4280 | return procState; |
| 4281 | } |
| 4282 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4283 | void CameraService::UidPolicy::UidPolicy::addOverrideUid(uid_t uid, |
| 4284 | String16 callingPackage, bool active) { |
| 4285 | updateOverrideUid(uid, callingPackage, active, true); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4286 | } |
| 4287 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4288 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, String16 callingPackage) { |
| 4289 | updateOverrideUid(uid, callingPackage, false, false); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4290 | } |
| 4291 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4292 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 4293 | Mutex::Autolock _l(mUidLock); |
| 4294 | ALOGV("UidPolicy: ActivityManager has died"); |
| 4295 | mRegistered = false; |
| 4296 | mActiveUids.clear(); |
| 4297 | } |
| 4298 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4299 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, String16 callingPackage, |
| 4300 | bool active, bool insert) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4301 | bool wasActive = false; |
| 4302 | bool isActive = false; |
| 4303 | { |
| 4304 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4305 | wasActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4306 | mOverrideUids.erase(uid); |
| 4307 | if (insert) { |
| 4308 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 4309 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4310 | isActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4311 | } |
| 4312 | if (wasActive != isActive && !isActive) { |
| 4313 | sp<CameraService> service = mService.promote(); |
| 4314 | if (service != nullptr) { |
| 4315 | service->blockClientsForUid(uid); |
| 4316 | } |
| 4317 | } |
| 4318 | } |
| 4319 | |
| 4320 | // ---------------------------------------------------------------------------- |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4321 | // SensorPrivacyPolicy |
| 4322 | // ---------------------------------------------------------------------------- |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4323 | |
| 4324 | void CameraService::SensorPrivacyPolicy::registerWithSensorPrivacyManager() |
| 4325 | { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4326 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4327 | if (mRegistered) { |
| 4328 | return; |
| 4329 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4330 | hasCameraPrivacyFeature(); // Called so the result is cached |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4331 | mSpm.addSensorPrivacyListener(this); |
| 4332 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); |
| 4333 | status_t res = mSpm.linkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4334 | if (res == OK) { |
| 4335 | mRegistered = true; |
| 4336 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); |
| 4337 | } |
| 4338 | } |
| 4339 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4340 | void CameraService::SensorPrivacyPolicy::onServiceRegistration(const String16& name, |
| 4341 | const sp<IBinder>&) { |
| 4342 | if (name != String16(kSensorPrivacyServiceName)) { |
| 4343 | return; |
| 4344 | } |
| 4345 | |
| 4346 | registerWithSensorPrivacyManager(); |
| 4347 | } |
| 4348 | |
| 4349 | void CameraService::SensorPrivacyPolicy::registerSelf() { |
| 4350 | // Use checkservice to see if the sensor_privacy service is available |
| 4351 | // If service is not available then register for notification |
| 4352 | sp<IServiceManager> sm = defaultServiceManager(); |
| 4353 | sp<IBinder> binder = sm->checkService(String16(kSensorPrivacyServiceName)); |
| 4354 | if (!binder) { |
| 4355 | sm->registerForNotifications(String16(kSensorPrivacyServiceName),this); |
| 4356 | } else { |
| 4357 | registerWithSensorPrivacyManager(); |
| 4358 | } |
| 4359 | } |
| 4360 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4361 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { |
| 4362 | Mutex::Autolock _l(mSensorPrivacyLock); |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4363 | mSpm.removeSensorPrivacyListener(this); |
| 4364 | mSpm.unlinkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4365 | mRegistered = false; |
| 4366 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); |
| 4367 | } |
| 4368 | |
| 4369 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4370 | if (!mRegistered) { |
| 4371 | registerWithSensorPrivacyManager(); |
| 4372 | } |
| 4373 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4374 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4375 | return mSensorPrivacyEnabled; |
| 4376 | } |
| 4377 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4378 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled() { |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4379 | if (!hasCameraPrivacyFeature()) { |
| 4380 | return false; |
| 4381 | } |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4382 | return mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4383 | } |
| 4384 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4385 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged( |
| 4386 | int toggleType __unused, int sensor __unused, bool enabled) { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4387 | { |
| 4388 | Mutex::Autolock _l(mSensorPrivacyLock); |
Kwangkyu Park | e3af299 | 2022-11-05 02:46:52 +0900 | [diff] [blame] | 4389 | mSensorPrivacyEnabled = enabled; |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4390 | } |
| 4391 | // if sensor privacy is enabled then block all clients from accessing the camera |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4392 | if (enabled) { |
| 4393 | sp<CameraService> service = mService.promote(); |
| 4394 | if (service != nullptr) { |
| 4395 | service->blockAllClients(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4396 | } |
| 4397 | } |
| 4398 | return binder::Status::ok(); |
| 4399 | } |
| 4400 | |
| 4401 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 4402 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4403 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); |
| 4404 | mRegistered = false; |
| 4405 | } |
| 4406 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4407 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4408 | bool supportsSoftwareToggle = mSpm.supportsSensorToggle( |
| 4409 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 4410 | bool supportsHardwareToggle = mSpm.supportsSensorToggle( |
| 4411 | SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 4412 | return supportsSoftwareToggle || supportsHardwareToggle; |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4413 | } |
| 4414 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4415 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4416 | // CameraState |
| 4417 | // ---------------------------------------------------------------------------- |
| 4418 | |
| 4419 | CameraService::CameraState::CameraState(const String8& id, int cost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4420 | const std::set<String8>& conflicting, SystemCameraKind systemCameraKind, |
| 4421 | const std::vector<std::string>& physicalCameras) : mId(id), |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4422 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4423 | mSystemCameraKind(systemCameraKind), mPhysicalCameras(physicalCameras) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4424 | |
| 4425 | CameraService::CameraState::~CameraState() {} |
| 4426 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4427 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4428 | Mutex::Autolock lock(mStatusLock); |
| 4429 | return mStatus; |
| 4430 | } |
| 4431 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4432 | std::vector<String8> CameraService::CameraState::getUnavailablePhysicalIds() const { |
| 4433 | Mutex::Autolock lock(mStatusLock); |
| 4434 | std::vector<String8> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); |
| 4435 | return res; |
| 4436 | } |
| 4437 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4438 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 4439 | return mShimParams; |
| 4440 | } |
| 4441 | |
| 4442 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 4443 | mShimParams = params; |
| 4444 | } |
| 4445 | |
| 4446 | int CameraService::CameraState::getCost() const { |
| 4447 | return mCost; |
| 4448 | } |
| 4449 | |
| 4450 | std::set<String8> CameraService::CameraState::getConflicting() const { |
| 4451 | return mConflicting; |
| 4452 | } |
| 4453 | |
| 4454 | String8 CameraService::CameraState::getId() const { |
| 4455 | return mId; |
| 4456 | } |
| 4457 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4458 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { |
| 4459 | return mSystemCameraKind; |
| 4460 | } |
| 4461 | |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4462 | bool CameraService::CameraState::containsPhysicalCamera(const std::string& physicalCameraId) const { |
| 4463 | return std::find(mPhysicalCameras.begin(), mPhysicalCameras.end(), physicalCameraId) |
| 4464 | != mPhysicalCameras.end(); |
| 4465 | } |
| 4466 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4467 | bool CameraService::CameraState::addUnavailablePhysicalId(const String8& physicalId) { |
| 4468 | Mutex::Autolock lock(mStatusLock); |
| 4469 | auto result = mUnavailablePhysicalIds.insert(physicalId); |
| 4470 | return result.second; |
| 4471 | } |
| 4472 | |
| 4473 | bool CameraService::CameraState::removeUnavailablePhysicalId(const String8& physicalId) { |
| 4474 | Mutex::Autolock lock(mStatusLock); |
| 4475 | auto count = mUnavailablePhysicalIds.erase(physicalId); |
| 4476 | return count > 0; |
| 4477 | } |
| 4478 | |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 4479 | void CameraService::CameraState::setClientPackage(const String8& clientPackage) { |
| 4480 | Mutex::Autolock lock(mStatusLock); |
| 4481 | mClientPackage = clientPackage; |
| 4482 | } |
| 4483 | |
| 4484 | String8 CameraService::CameraState::getClientPackage() const { |
| 4485 | Mutex::Autolock lock(mStatusLock); |
| 4486 | return mClientPackage; |
| 4487 | } |
| 4488 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4489 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4490 | // ClientEventListener |
| 4491 | // ---------------------------------------------------------------------------- |
| 4492 | |
| 4493 | void CameraService::ClientEventListener::onClientAdded( |
| 4494 | const resource_policy::ClientDescriptor<String8, |
| 4495 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 4496 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4497 | if (basicClient.get() != nullptr) { |
| 4498 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 4499 | notifier.noteStartCamera(descriptor.getKey(), |
| 4500 | static_cast<int>(basicClient->getClientUid())); |
| 4501 | } |
| 4502 | } |
| 4503 | |
| 4504 | void CameraService::ClientEventListener::onClientRemoved( |
| 4505 | const resource_policy::ClientDescriptor<String8, |
| 4506 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 4507 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4508 | if (basicClient.get() != nullptr) { |
| 4509 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 4510 | notifier.noteStopCamera(descriptor.getKey(), |
| 4511 | static_cast<int>(basicClient->getClientUid())); |
| 4512 | } |
| 4513 | } |
| 4514 | |
| 4515 | |
| 4516 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4517 | // CameraClientManager |
| 4518 | // ---------------------------------------------------------------------------- |
| 4519 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4520 | CameraService::CameraClientManager::CameraClientManager() { |
| 4521 | setListener(std::make_shared<ClientEventListener>()); |
| 4522 | } |
| 4523 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4524 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 4525 | |
| 4526 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
| 4527 | const String8& id) const { |
| 4528 | auto descriptor = get(id); |
| 4529 | if (descriptor == nullptr) { |
| 4530 | return sp<BasicClient>{nullptr}; |
| 4531 | } |
| 4532 | return descriptor->getValue(); |
| 4533 | } |
| 4534 | |
| 4535 | String8 CameraService::CameraClientManager::toString() const { |
| 4536 | auto all = getAll(); |
| 4537 | String8 ret("["); |
| 4538 | bool hasAny = false; |
| 4539 | for (auto& i : all) { |
| 4540 | hasAny = true; |
| 4541 | String8 key = i->getKey(); |
| 4542 | int32_t cost = i->getCost(); |
| 4543 | int32_t pid = i->getOwnerId(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4544 | int32_t score = i->getPriority().getScore(); |
| 4545 | int32_t state = i->getPriority().getState(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4546 | auto conflicting = i->getConflicting(); |
| 4547 | auto clientSp = i->getValue(); |
| 4548 | String8 packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 4549 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4550 | if (clientSp.get() != nullptr) { |
| 4551 | packageName = String8{clientSp->getPackageName()}; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4552 | uid_t clientUid = clientSp->getClientUid(); |
| 4553 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4554 | } |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4555 | ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" |
| 4556 | PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4557 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4558 | if (clientSp.get() != nullptr) { |
| 4559 | ret.appendFormat("User Id: %d, ", clientUserId); |
| 4560 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4561 | if (packageName.size() != 0) { |
| 4562 | ret.appendFormat("Client Package Name: %s", packageName.string()); |
| 4563 | } |
| 4564 | |
| 4565 | ret.append(", Conflicting Client Devices: {"); |
| 4566 | for (auto& j : conflicting) { |
| 4567 | ret.appendFormat("%s, ", j.string()); |
| 4568 | } |
| 4569 | ret.append("})"); |
| 4570 | } |
| 4571 | if (hasAny) ret.append("\n"); |
| 4572 | ret.append("]\n"); |
| 4573 | return ret; |
| 4574 | } |
| 4575 | |
| 4576 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
| 4577 | const String8& key, const sp<BasicClient>& value, int32_t cost, |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4578 | const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4579 | int32_t state, int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4580 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4581 | int32_t score_adj = systemNativeClient ? kSystemNativeClientScore : score; |
| 4582 | int32_t state_adj = systemNativeClient ? kSystemNativeClientState: state; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 4583 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4584 | return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>( |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4585 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, |
| 4586 | systemNativeClient, oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 4590 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4591 | int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4592 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4593 | partial->getConflicting(), partial->getPriority().getScore(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4594 | partial->getOwnerId(), partial->getPriority().getState(), oomScoreOffset, |
| 4595 | systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | // ---------------------------------------------------------------------------- |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4599 | // InjectionStatusListener |
| 4600 | // ---------------------------------------------------------------------------- |
| 4601 | |
| 4602 | void CameraService::InjectionStatusListener::addListener( |
| 4603 | const sp<ICameraInjectionCallback>& callback) { |
| 4604 | Mutex::Autolock lock(mListenerLock); |
| 4605 | if (mCameraInjectionCallback) return; |
| 4606 | status_t res = IInterface::asBinder(callback)->linkToDeath(this); |
| 4607 | if (res == OK) { |
| 4608 | mCameraInjectionCallback = callback; |
| 4609 | } |
| 4610 | } |
| 4611 | |
| 4612 | void CameraService::InjectionStatusListener::removeListener() { |
| 4613 | Mutex::Autolock lock(mListenerLock); |
| 4614 | if (mCameraInjectionCallback == nullptr) { |
| 4615 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 4616 | return; |
| 4617 | } |
| 4618 | IInterface::asBinder(mCameraInjectionCallback)->unlinkToDeath(this); |
| 4619 | mCameraInjectionCallback = nullptr; |
| 4620 | } |
| 4621 | |
| 4622 | void CameraService::InjectionStatusListener::notifyInjectionError( |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4623 | String8 injectedCamId, status_t err) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4624 | if (mCameraInjectionCallback == nullptr) { |
| 4625 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 4626 | return; |
| 4627 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4628 | |
| 4629 | switch (err) { |
| 4630 | case -ENODEV: |
| 4631 | mCameraInjectionCallback->onInjectionError( |
| 4632 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4633 | ALOGE("No camera device with ID \"%s\" currently available!", |
| 4634 | injectedCamId.string()); |
| 4635 | break; |
| 4636 | case -EBUSY: |
| 4637 | mCameraInjectionCallback->onInjectionError( |
| 4638 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4639 | ALOGE("Higher-priority client using camera, ID \"%s\" currently unavailable!", |
| 4640 | injectedCamId.string()); |
| 4641 | break; |
| 4642 | case DEAD_OBJECT: |
| 4643 | mCameraInjectionCallback->onInjectionError( |
| 4644 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4645 | ALOGE("Camera ID \"%s\" object is dead!", |
| 4646 | injectedCamId.string()); |
| 4647 | break; |
| 4648 | case INVALID_OPERATION: |
| 4649 | mCameraInjectionCallback->onInjectionError( |
| 4650 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4651 | ALOGE("Camera ID \"%s\" encountered an operating or internal error!", |
| 4652 | injectedCamId.string()); |
| 4653 | break; |
| 4654 | case UNKNOWN_TRANSACTION: |
| 4655 | mCameraInjectionCallback->onInjectionError( |
| 4656 | ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED); |
| 4657 | ALOGE("Camera ID \"%s\" method doesn't support!", |
| 4658 | injectedCamId.string()); |
| 4659 | break; |
| 4660 | default: |
| 4661 | mCameraInjectionCallback->onInjectionError( |
| 4662 | ICameraInjectionCallback::ERROR_INJECTION_INVALID_ERROR); |
| 4663 | ALOGE("Unexpected error %s (%d) opening camera \"%s\"!", |
| 4664 | strerror(-err), err, injectedCamId.string()); |
| 4665 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4666 | } |
| 4667 | |
| 4668 | void CameraService::InjectionStatusListener::binderDied( |
| 4669 | const wp<IBinder>& /*who*/) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4670 | ALOGV("InjectionStatusListener: ICameraInjectionCallback has died"); |
| 4671 | auto parent = mParent.promote(); |
| 4672 | if (parent != nullptr) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4673 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 4674 | if (clientDescriptor != nullptr) { |
| 4675 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 4676 | baseClientPtr->stopInjection(); |
| 4677 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 4678 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4679 | } |
| 4680 | } |
| 4681 | |
| 4682 | // ---------------------------------------------------------------------------- |
| 4683 | // CameraInjectionSession |
| 4684 | // ---------------------------------------------------------------------------- |
| 4685 | |
| 4686 | binder::Status CameraService::CameraInjectionSession::stopInjection() { |
| 4687 | Mutex::Autolock lock(mInjectionSessionLock); |
| 4688 | auto parent = mParent.promote(); |
| 4689 | if (parent == nullptr) { |
| 4690 | ALOGE("CameraInjectionSession: Parent is gone"); |
| 4691 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SERVICE, |
| 4692 | "Camera service encountered error"); |
| 4693 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4694 | |
| 4695 | status_t res = NO_ERROR; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4696 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 4697 | if (clientDescriptor != nullptr) { |
| 4698 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 4699 | res = baseClientPtr->stopInjection(); |
| 4700 | if (res != OK) { |
| 4701 | ALOGE("CameraInjectionSession: Failed to stop the injection camera!" |
| 4702 | " ret != NO_ERROR: %d", res); |
| 4703 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SESSION, |
| 4704 | "Camera session encountered error"); |
| 4705 | } |
| 4706 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 4707 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4708 | return binder::Status::ok(); |
| 4709 | } |
| 4710 | |
| 4711 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4712 | |
| 4713 | static const int kDumpLockRetries = 50; |
| 4714 | static const int kDumpLockSleep = 60000; |
| 4715 | |
| 4716 | static bool tryLock(Mutex& mutex) |
| 4717 | { |
| 4718 | bool locked = false; |
| 4719 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 4720 | if (mutex.tryLock() == NO_ERROR) { |
| 4721 | locked = true; |
| 4722 | break; |
| 4723 | } |
| 4724 | usleep(kDumpLockSleep); |
| 4725 | } |
| 4726 | return locked; |
| 4727 | } |
| 4728 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4729 | void CameraService::cacheDump() { |
| 4730 | if (mMemFd != -1) { |
| 4731 | const Vector<String16> args; |
| 4732 | ATRACE_CALL(); |
| 4733 | // Acquiring service lock here will avoid the deadlock since |
| 4734 | // cacheDump will not be called during the second disconnect. |
| 4735 | Mutex::Autolock lock(mServiceLock); |
| 4736 | |
| 4737 | Mutex::Autolock l(mCameraStatesLock); |
| 4738 | // Start collecting the info for open sessions and store it in temp file. |
| 4739 | for (const auto& state : mCameraStates) { |
| 4740 | String8 cameraId = state.first; |
| 4741 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 4742 | if (clientDescriptor != nullptr) { |
| 4743 | dprintf(mMemFd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
| 4744 | // Log the current open session info before device is disconnected. |
| 4745 | dumpOpenSessionClientLogs(mMemFd, args, cameraId); |
| 4746 | } |
| 4747 | } |
| 4748 | } |
| 4749 | } |
| 4750 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4751 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4752 | ATRACE_CALL(); |
| 4753 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4754 | if (checkCallingPermission(sDumpPermission) == false) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4755 | dprintf(fd, "Permission Denial: can't dump CameraService from pid=%d, uid=%d\n", |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 4756 | CameraThreadState::getCallingPid(), |
| 4757 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4758 | return NO_ERROR; |
| 4759 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4760 | bool locked = tryLock(mServiceLock); |
| 4761 | // failed to lock - CameraService is probably deadlocked |
| 4762 | if (!locked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4763 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4764 | } |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4765 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4766 | if (!mInitialized) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4767 | dprintf(fd, "!! No camera HAL available !!\n"); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 4768 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4769 | // Dump event log for error information |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4770 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4771 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4772 | if (locked) mServiceLock.unlock(); |
| 4773 | return NO_ERROR; |
| 4774 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4775 | dprintf(fd, "\n== Service global info: ==\n\n"); |
| 4776 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4777 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 4778 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", |
| 4779 | mNormalDeviceIdsWithoutSystemCamera.size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4780 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { |
| 4781 | dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); |
| 4782 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4783 | String8 activeClientString = mActiveClientManager.toString(); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4784 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string()); |
| 4785 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string()); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 4786 | if (mStreamUseCaseOverrides.size() > 0) { |
| 4787 | dprintf(fd, "Active stream use case overrides:"); |
| 4788 | for (int64_t useCaseOverride : mStreamUseCaseOverrides) { |
| 4789 | dprintf(fd, " %" PRId64, useCaseOverride); |
| 4790 | } |
| 4791 | dprintf(fd, "\n"); |
| 4792 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4793 | |
| 4794 | dumpEventLog(fd); |
| 4795 | |
| 4796 | bool stateLocked = tryLock(mCameraStatesLock); |
| 4797 | if (!stateLocked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4798 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4799 | } |
| 4800 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 4801 | int argSize = args.size(); |
| 4802 | for (int i = 0; i < argSize; i++) { |
| 4803 | if (args[i] == TagMonitor::kMonitorOption) { |
| 4804 | if (i + 1 < argSize) { |
| 4805 | mMonitorTags = String8(args[i + 1]); |
| 4806 | } |
| 4807 | break; |
| 4808 | } |
| 4809 | } |
| 4810 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4811 | for (auto& state : mCameraStates) { |
| 4812 | String8 cameraId = state.first; |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4813 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4814 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4815 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4816 | CameraParameters p = state.second->getShimParams(); |
| 4817 | if (!p.isEmpty()) { |
| 4818 | dprintf(fd, " Camera1 API shim is using parameters:\n "); |
| 4819 | p.dump(fd, args); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4823 | if (clientDescriptor != nullptr) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4824 | // log the current open session info |
| 4825 | dumpOpenSessionClientLogs(fd, args, cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4826 | } else { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4827 | dumpClosedSessionClientLogs(fd, cameraId); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4828 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4829 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | if (stateLocked) mCameraStatesLock.unlock(); |
| 4833 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4834 | if (locked) mServiceLock.unlock(); |
| 4835 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 4836 | mCameraProviderManager->dump(fd, args); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4837 | |
| 4838 | dprintf(fd, "\n== Vendor tags: ==\n\n"); |
| 4839 | |
| 4840 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 4841 | if (desc == NULL) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 4842 | sp<VendorTagDescriptorCache> cache = |
| 4843 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 4844 | if (cache == NULL) { |
| 4845 | dprintf(fd, "No vendor tags.\n"); |
| 4846 | } else { |
| 4847 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 4848 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4849 | } else { |
| 4850 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 4851 | } |
| 4852 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4853 | // Dump camera traces if there were any |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4854 | dprintf(fd, "\n"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4855 | camera3::CameraTraces::dump(fd); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4856 | |
| 4857 | // Process dump arguments, if any |
| 4858 | int n = args.size(); |
| 4859 | String16 verboseOption("-v"); |
| 4860 | String16 unreachableOption("--unreachable"); |
| 4861 | for (int i = 0; i < n; i++) { |
| 4862 | if (args[i] == verboseOption) { |
| 4863 | // change logging level |
| 4864 | if (i + 1 >= n) continue; |
| 4865 | String8 levelStr(args[i+1]); |
| 4866 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4867 | dprintf(fd, "\nSetting log level to %d.\n", level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4868 | setLogLevel(level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4869 | } else if (args[i] == unreachableOption) { |
| 4870 | // Dump memory analysis |
| 4871 | // TODO - should limit be an argument parameter? |
| 4872 | UnreachableMemoryInfo info; |
| 4873 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); |
| 4874 | if (!success) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4875 | dprintf(fd, "\n== Unable to dump unreachable memory. " |
| 4876 | "Try disabling SELinux enforcement. ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4877 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4878 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4879 | std::string s = info.ToString(/*log_contents*/ true); |
| 4880 | write(fd, s.c_str(), s.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4881 | } |
| 4882 | } |
| 4883 | } |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4884 | |
| 4885 | bool serviceLocked = tryLock(mServiceLock); |
| 4886 | |
| 4887 | // Dump info from previous open sessions. |
| 4888 | // Reposition the offset to beginning of the file before reading |
| 4889 | |
| 4890 | if ((mMemFd >= 0) && (lseek(mMemFd, 0, SEEK_SET) != -1)) { |
| 4891 | dprintf(fd, "\n**********Dumpsys from previous open session**********\n"); |
| 4892 | ssize_t size_read; |
| 4893 | char buf[4096]; |
| 4894 | while ((size_read = read(mMemFd, buf, (sizeof(buf) - 1))) > 0) { |
| 4895 | // Read data from file to a small buffer and write it to fd. |
| 4896 | write(fd, buf, size_read); |
| 4897 | if (size_read == -1) { |
| 4898 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 4899 | break; |
| 4900 | } |
| 4901 | } |
| 4902 | dprintf(fd, "\n**********End of Dumpsys from previous open session**********\n"); |
| 4903 | } else { |
| 4904 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 4905 | } |
| 4906 | |
| 4907 | if (serviceLocked) mServiceLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4908 | return NO_ERROR; |
| 4909 | } |
| 4910 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4911 | void CameraService::dumpOpenSessionClientLogs(int fd, |
| 4912 | const Vector<String16>& args, const String8& cameraId) { |
| 4913 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Rucha Katakwar | 71a0e05 | 2022-04-07 15:44:18 -0700 | [diff] [blame] | 4914 | dprintf(fd, " %s : Device %s is open. Client instance dump:\n", |
| 4915 | getFormattedCurrentTime().string(), |
| 4916 | cameraId.string()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4917 | dprintf(fd, " Client priority score: %d state: %d\n", |
| 4918 | clientDescriptor->getPriority().getScore(), |
| 4919 | clientDescriptor->getPriority().getState()); |
| 4920 | dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 4921 | |
| 4922 | auto client = clientDescriptor->getValue(); |
| 4923 | dprintf(fd, " Client package: %s\n", |
| 4924 | String8(client->getPackageName()).string()); |
| 4925 | |
| 4926 | client->dumpClient(fd, args); |
| 4927 | } |
| 4928 | |
| 4929 | void CameraService::dumpClosedSessionClientLogs(int fd, const String8& cameraId) { |
| 4930 | dprintf(fd, " Device %s is closed, no client instance\n", |
| 4931 | cameraId.string()); |
| 4932 | } |
| 4933 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4934 | void CameraService::dumpEventLog(int fd) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4935 | 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] | 4936 | |
| 4937 | Mutex::Autolock l(mLogLock); |
| 4938 | for (const auto& msg : mEventLog) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4939 | dprintf(fd, " %s\n", msg.string()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4943 | dprintf(fd, " ...\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4944 | } else if (mEventLog.size() == 0) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4945 | dprintf(fd, " [no events yet]\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4946 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4947 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4948 | } |
| 4949 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4950 | void CameraService::cacheClientTagDumpIfNeeded(const char *cameraId, BasicClient* client) { |
| 4951 | Mutex::Autolock lock(mLogLock); |
| 4952 | if (!isClientWatchedLocked(client)) { return; } |
| 4953 | |
| 4954 | std::vector<std::string> dumpVector; |
| 4955 | client->dumpWatchedEventsToVector(dumpVector); |
| 4956 | |
| 4957 | if (dumpVector.empty()) { return; } |
| 4958 | |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 4959 | std::string dumpString; |
| 4960 | |
| 4961 | String8 currentTime = getFormattedCurrentTime(); |
| 4962 | dumpString += "Cached @ "; |
| 4963 | dumpString += currentTime.string(); |
| 4964 | dumpString += "\n"; // First line is the timestamp of when client is cached. |
| 4965 | |
| 4966 | |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4967 | const String16 &packageName = client->getPackageName(); |
| 4968 | |
| 4969 | String8 packageName8 = String8(packageName); |
| 4970 | const char *printablePackageName = packageName8.lockBuffer(packageName.size()); |
| 4971 | |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 4972 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4973 | size_t i = dumpVector.size(); |
| 4974 | |
| 4975 | // Store the string in reverse order (latest last) |
| 4976 | while (i > 0) { |
| 4977 | i--; |
| 4978 | dumpString += cameraId; |
| 4979 | dumpString += ":"; |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4980 | dumpString += printablePackageName; |
| 4981 | dumpString += " "; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4982 | dumpString += dumpVector[i]; // implicitly ends with '\n' |
| 4983 | } |
| 4984 | |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4985 | packageName8.unlockBuffer(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4986 | mWatchedClientsDumpCache[packageName] = dumpString; |
| 4987 | } |
| 4988 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4989 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4990 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4991 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 4992 | if (mTorchClientMap[i] == who) { |
| 4993 | // turn off the torch mode that was turned on by dead client |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4994 | String8 cameraId = mTorchClientMap.keyAt(i); |
| 4995 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 4996 | if (res) { |
| 4997 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 4998 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 4999 | return; |
| 5000 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5001 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5002 | break; |
| 5003 | } |
| 5004 | } |
| 5005 | } |
| 5006 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5007 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 5008 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 5009 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 5010 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 5011 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 5012 | */ |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 5013 | // PID here is approximate and can be wrong. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 5014 | logClientDied(CameraThreadState::getCallingPid(), String8("Binder died unexpectedly")); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 5015 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5016 | // check torch client |
| 5017 | handleTorchClientBinderDied(who); |
| 5018 | |
| 5019 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5020 | if(!evictClientIdByRemote(who)) { |
| 5021 | 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] | 5022 | return; |
| 5023 | } |
| 5024 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5025 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 5026 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 5027 | } |
| 5028 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5029 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5030 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 5031 | } |
| 5032 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5033 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId, |
| 5034 | std::initializer_list<StatusInternal> rejectSourceStates) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5035 | // Do not lock mServiceLock here or can get into a deadlock from |
| 5036 | // connect() -> disconnect -> updateStatus |
| 5037 | |
| 5038 | auto state = getCameraState(cameraId); |
| 5039 | |
| 5040 | if (state == nullptr) { |
| 5041 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 5042 | cameraId.string()); |
| 5043 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 5044 | } |
| 5045 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 5046 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 5047 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 5048 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 5049 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 5050 | return; |
| 5051 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5052 | |
| 5053 | // Collect the logical cameras without holding mStatusLock in updateStatus |
| 5054 | // as that can lead to a deadlock(b/162192331). |
| 5055 | auto logicalCameraIds = getLogicalCameras(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5056 | // 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] | 5057 | // of the listeners with both the mStatusLock and mStatusListenerLock held |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 5058 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5059 | &logicalCameraIds] |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5060 | (const String8& cameraId, StatusInternal status) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5061 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5062 | if (status != StatusInternal::ENUMERATING) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 5063 | // Update torch status if it has a flash unit. |
| 5064 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5065 | TorchModeStatus torchStatus; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 5066 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 5067 | NAME_NOT_FOUND) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5068 | TorchModeStatus newTorchStatus = |
| 5069 | status == StatusInternal::PRESENT ? |
| 5070 | TorchModeStatus::AVAILABLE_OFF : |
| 5071 | TorchModeStatus::NOT_AVAILABLE; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 5072 | if (torchStatus != newTorchStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 5073 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 5074 | } |
| 5075 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | Mutex::Autolock lock(mStatusListenerLock); |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5079 | notifyPhysicalCameraStatusLocked(mapToInterface(status), String16(cameraId), |
| 5080 | logicalCameraIds, deviceKind); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5081 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5082 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 5083 | bool isVendorListener = listener->isVendorListener(); |
| 5084 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, |
| 5085 | listener->getListenerPid(), listener->getListenerUid()) || |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 5086 | isVendorListener) { |
| 5087 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 5088 | cameraId.c_str()); |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 5089 | continue; |
| 5090 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5091 | auto ret = listener->getListener()->onStatusChanged(mapToInterface(status), |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 5092 | String16(cameraId)); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5093 | listener->handleBinderStatus(ret, |
| 5094 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", |
| 5095 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 5096 | ret.exceptionCode()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 5097 | } |
| 5098 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 5099 | } |
| 5100 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5101 | void CameraService::updateOpenCloseStatus(const String8& cameraId, bool open, |
| 5102 | const String16& clientPackageName) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 5103 | auto state = getCameraState(cameraId); |
| 5104 | if (state == nullptr) { |
| 5105 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 5106 | cameraId.string()); |
| 5107 | return; |
| 5108 | } |
| 5109 | if (open) { |
| 5110 | state->setClientPackage(String8(clientPackageName)); |
| 5111 | } else { |
| 5112 | state->setClientPackage(String8::empty()); |
| 5113 | } |
| 5114 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5115 | Mutex::Autolock lock(mStatusListenerLock); |
| 5116 | |
| 5117 | for (const auto& it : mListenerList) { |
| 5118 | if (!it->isOpenCloseCallbackAllowed()) { |
| 5119 | continue; |
| 5120 | } |
| 5121 | |
| 5122 | binder::Status ret; |
| 5123 | String16 cameraId64(cameraId); |
| 5124 | if (open) { |
| 5125 | ret = it->getListener()->onCameraOpened(cameraId64, clientPackageName); |
| 5126 | } else { |
| 5127 | ret = it->getListener()->onCameraClosed(cameraId64); |
| 5128 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5129 | |
| 5130 | it->handleBinderStatus(ret, |
| 5131 | "%s: Failed to trigger onCameraOpened/onCameraClosed callback for %d:%d: %d", |
| 5132 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5133 | } |
| 5134 | } |
| 5135 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5136 | template<class Func> |
| 5137 | void CameraService::CameraState::updateStatus(StatusInternal status, |
| 5138 | const String8& cameraId, |
| 5139 | std::initializer_list<StatusInternal> rejectSourceStates, |
| 5140 | Func onStatusUpdatedLocked) { |
| 5141 | Mutex::Autolock lock(mStatusLock); |
| 5142 | StatusInternal oldStatus = mStatus; |
| 5143 | mStatus = status; |
| 5144 | |
| 5145 | if (oldStatus == status) { |
| 5146 | return; |
| 5147 | } |
| 5148 | |
| 5149 | ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__, |
| 5150 | cameraId.string(), oldStatus, status); |
| 5151 | |
| 5152 | if (oldStatus == StatusInternal::NOT_PRESENT && |
| 5153 | (status != StatusInternal::PRESENT && |
| 5154 | status != StatusInternal::ENUMERATING)) { |
| 5155 | |
| 5156 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 5157 | __FUNCTION__); |
| 5158 | mStatus = oldStatus; |
| 5159 | return; |
| 5160 | } |
| 5161 | |
| 5162 | /** |
| 5163 | * Sometimes we want to conditionally do a transition. |
| 5164 | * For example if a client disconnects, we want to go to PRESENT |
| 5165 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 5166 | */ |
| 5167 | for (auto& rejectStatus : rejectSourceStates) { |
| 5168 | if (oldStatus == rejectStatus) { |
| 5169 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
| 5170 | "state was was in one of the bad states.", __FUNCTION__, cameraId.string()); |
| 5171 | mStatus = oldStatus; |
| 5172 | return; |
| 5173 | } |
| 5174 | } |
| 5175 | |
| 5176 | onStatusUpdatedLocked(cameraId, status); |
| 5177 | } |
| 5178 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5179 | status_t CameraService::getTorchStatusLocked( |
| 5180 | const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5181 | TorchModeStatus *status) const { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5182 | if (!status) { |
| 5183 | return BAD_VALUE; |
| 5184 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5185 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 5186 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5187 | // invalid camera ID or the camera doesn't have a flash unit |
| 5188 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5189 | } |
| 5190 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5191 | *status = mTorchStatusMap.valueAt(index); |
| 5192 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5193 | } |
| 5194 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5195 | status_t CameraService::setTorchStatusLocked(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5196 | TorchModeStatus status) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5197 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 5198 | if (index == NAME_NOT_FOUND) { |
| 5199 | return BAD_VALUE; |
| 5200 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5201 | mTorchStatusMap.editValueAt(index) = status; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5202 | |
| 5203 | return OK; |
| 5204 | } |
| 5205 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5206 | std::list<String16> CameraService::getLogicalCameras( |
| 5207 | const String8& physicalCameraId) { |
| 5208 | std::list<String16> retList; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5209 | Mutex::Autolock lock(mCameraStatesLock); |
| 5210 | for (const auto& state : mCameraStates) { |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5211 | if (state.second->containsPhysicalCamera(physicalCameraId.c_str())) { |
| 5212 | retList.emplace_back(String16(state.first)); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5213 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5214 | } |
| 5215 | return retList; |
| 5216 | } |
| 5217 | |
| 5218 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, |
| 5219 | const String16& physicalCameraId, const std::list<String16>& logicalCameraIds, |
| 5220 | SystemCameraKind deviceKind) { |
| 5221 | // mStatusListenerLock is expected to be locked |
| 5222 | for (const auto& logicalCameraId : logicalCameraIds) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5223 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5224 | // Note: we check only the deviceKind of the physical camera id |
| 5225 | // since, logical camera ids and their physical camera ids are |
| 5226 | // guaranteed to have the same system camera kind. |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 5227 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 5228 | listener->getListenerPid(), listener->getListenerUid())) { |
| 5229 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5230 | String8(physicalCameraId).c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 5231 | continue; |
| 5232 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5233 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged(status, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5234 | logicalCameraId, physicalCameraId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5235 | listener->handleBinderStatus(ret, |
| 5236 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 5237 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 5238 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5239 | } |
| 5240 | } |
| 5241 | } |
| 5242 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5243 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5244 | void CameraService::blockClientsForUid(uid_t uid) { |
| 5245 | const auto clients = mActiveClientManager.getAll(); |
| 5246 | for (auto& current : clients) { |
| 5247 | if (current != nullptr) { |
| 5248 | const auto basicClient = current->getValue(); |
| 5249 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { |
| 5250 | basicClient->block(); |
| 5251 | } |
| 5252 | } |
| 5253 | } |
| 5254 | } |
| 5255 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5256 | void CameraService::blockAllClients() { |
| 5257 | const auto clients = mActiveClientManager.getAll(); |
| 5258 | for (auto& current : clients) { |
| 5259 | if (current != nullptr) { |
| 5260 | const auto basicClient = current->getValue(); |
| 5261 | if (basicClient.get() != nullptr) { |
| 5262 | basicClient->block(); |
| 5263 | } |
| 5264 | } |
| 5265 | } |
| 5266 | } |
| 5267 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5268 | // NOTE: This is a remote API - make sure all args are validated |
| 5269 | status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) { |
| 5270 | if (!checkCallingPermission(sManageCameraPermission, nullptr, nullptr)) { |
| 5271 | return PERMISSION_DENIED; |
| 5272 | } |
| 5273 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 5274 | return BAD_VALUE; |
| 5275 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5276 | if (args.size() >= 3 && args[0] == String16("set-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5277 | return handleSetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5278 | } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5279 | return handleResetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5280 | } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5281 | return handleGetUidState(args, out, err); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5282 | } else if (args.size() >= 2 && args[0] == String16("set-rotate-and-crop")) { |
| 5283 | return handleSetRotateAndCrop(args); |
| 5284 | } else if (args.size() >= 1 && args[0] == String16("get-rotate-and-crop")) { |
| 5285 | return handleGetRotateAndCrop(out); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5286 | } else if (args.size() >= 2 && args[0] == String16("set-autoframing")) { |
| 5287 | return handleSetAutoframing(args); |
| 5288 | } else if (args.size() >= 1 && args[0] == String16("get-autoframing")) { |
| 5289 | return handleGetAutoframing(out); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5290 | } else if (args.size() >= 2 && args[0] == String16("set-image-dump-mask")) { |
| 5291 | return handleSetImageDumpMask(args); |
| 5292 | } else if (args.size() >= 1 && args[0] == String16("get-image-dump-mask")) { |
| 5293 | return handleGetImageDumpMask(out); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 5294 | } else if (args.size() >= 2 && args[0] == String16("set-camera-mute")) { |
| 5295 | return handleSetCameraMute(args); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5296 | } else if (args.size() >= 2 && args[0] == String16("set-stream-use-case-override")) { |
| 5297 | return handleSetStreamUseCaseOverrides(args); |
| 5298 | } else if (args.size() >= 1 && args[0] == String16("clear-stream-use-case-override")) { |
| 5299 | handleClearStreamUseCaseOverrides(); |
| 5300 | return OK; |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 5301 | } else if (args.size() >= 1 && args[0] == String16("set-zoom-override")) { |
| 5302 | return handleSetZoomOverride(args); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5303 | } else if (args.size() >= 2 && args[0] == String16("watch")) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5304 | return handleWatchCommand(args, in, out); |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 5305 | } else if (args.size() >= 2 && args[0] == String16("set-watchdog")) { |
| 5306 | return handleSetCameraServiceWatchdog(args); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5307 | } else if (args.size() == 1 && args[0] == String16("help")) { |
| 5308 | printHelp(out); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5309 | return OK; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5310 | } |
| 5311 | printHelp(err); |
| 5312 | return BAD_VALUE; |
| 5313 | } |
| 5314 | |
| 5315 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5316 | String16 packageName = args[1]; |
| 5317 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5318 | bool active = false; |
| 5319 | if (args[2] == String16("active")) { |
| 5320 | active = true; |
| 5321 | } else if ((args[2] != String16("idle"))) { |
| 5322 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string()); |
| 5323 | return BAD_VALUE; |
| 5324 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5325 | |
| 5326 | int userId = 0; |
| 5327 | if (args.size() >= 5 && args[3] == String16("--user")) { |
| 5328 | userId = atoi(String8(args[4])); |
| 5329 | } |
| 5330 | |
| 5331 | uid_t uid; |
| 5332 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
| 5333 | return BAD_VALUE; |
| 5334 | } |
| 5335 | |
| 5336 | mUidPolicy->addOverrideUid(uid, packageName, active); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5337 | return NO_ERROR; |
| 5338 | } |
| 5339 | |
| 5340 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5341 | String16 packageName = args[1]; |
| 5342 | |
| 5343 | int userId = 0; |
| 5344 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 5345 | userId = atoi(String8(args[3])); |
| 5346 | } |
| 5347 | |
| 5348 | uid_t uid; |
| 5349 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5350 | return BAD_VALUE; |
| 5351 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5352 | |
| 5353 | mUidPolicy->removeOverrideUid(uid, packageName); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5354 | return NO_ERROR; |
| 5355 | } |
| 5356 | |
| 5357 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5358 | String16 packageName = args[1]; |
| 5359 | |
| 5360 | int userId = 0; |
| 5361 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 5362 | userId = atoi(String8(args[3])); |
| 5363 | } |
| 5364 | |
| 5365 | uid_t uid; |
| 5366 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5367 | return BAD_VALUE; |
| 5368 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5369 | |
| 5370 | if (mUidPolicy->isUidActive(uid, packageName)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5371 | return dprintf(out, "active\n"); |
| 5372 | } else { |
| 5373 | return dprintf(out, "idle\n"); |
| 5374 | } |
| 5375 | } |
| 5376 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5377 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { |
| 5378 | int rotateValue = atoi(String8(args[1])); |
| 5379 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || |
| 5380 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; |
| 5381 | Mutex::Autolock lock(mServiceLock); |
| 5382 | |
| 5383 | mOverrideRotateAndCropMode = rotateValue; |
| 5384 | |
| 5385 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; |
| 5386 | |
| 5387 | const auto clients = mActiveClientManager.getAll(); |
| 5388 | for (auto& current : clients) { |
| 5389 | if (current != nullptr) { |
| 5390 | const auto basicClient = current->getValue(); |
| 5391 | if (basicClient.get() != nullptr) { |
| 5392 | basicClient->setRotateAndCropOverride(rotateValue); |
| 5393 | } |
| 5394 | } |
| 5395 | } |
| 5396 | |
| 5397 | return OK; |
| 5398 | } |
| 5399 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5400 | status_t CameraService::handleSetAutoframing(const Vector<String16>& args) { |
| 5401 | char* end; |
| 5402 | int autoframingValue = (int) strtol(String8(args[1]), &end, /*base=*/10); |
| 5403 | if ((*end != '\0') || |
| 5404 | (autoframingValue != ANDROID_CONTROL_AUTOFRAMING_OFF && |
| 5405 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_ON && |
| 5406 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_AUTO)) { |
| 5407 | return BAD_VALUE; |
| 5408 | } |
| 5409 | |
| 5410 | Mutex::Autolock lock(mServiceLock); |
| 5411 | mOverrideAutoframingMode = autoframingValue; |
| 5412 | |
| 5413 | if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) return OK; |
| 5414 | |
| 5415 | const auto clients = mActiveClientManager.getAll(); |
| 5416 | for (auto& current : clients) { |
| 5417 | if (current != nullptr) { |
| 5418 | const auto basicClient = current->getValue(); |
| 5419 | if (basicClient.get() != nullptr) { |
| 5420 | basicClient->setAutoframingOverride(autoframingValue); |
| 5421 | } |
| 5422 | } |
| 5423 | } |
| 5424 | |
| 5425 | return OK; |
| 5426 | } |
| 5427 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 5428 | status_t CameraService::handleSetCameraServiceWatchdog(const Vector<String16>& args) { |
| 5429 | int enableWatchdog = atoi(String8(args[1])); |
| 5430 | |
| 5431 | if (enableWatchdog < 0 || enableWatchdog > 1) return BAD_VALUE; |
| 5432 | |
| 5433 | Mutex::Autolock lock(mServiceLock); |
| 5434 | |
| 5435 | mCameraServiceWatchdogEnabled = enableWatchdog; |
| 5436 | |
| 5437 | const auto clients = mActiveClientManager.getAll(); |
| 5438 | for (auto& current : clients) { |
| 5439 | if (current != nullptr) { |
| 5440 | const auto basicClient = current->getValue(); |
| 5441 | if (basicClient.get() != nullptr) { |
| 5442 | basicClient->setCameraServiceWatchdog(enableWatchdog); |
| 5443 | } |
| 5444 | } |
| 5445 | } |
| 5446 | |
| 5447 | return OK; |
| 5448 | } |
| 5449 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5450 | status_t CameraService::handleGetRotateAndCrop(int out) { |
| 5451 | Mutex::Autolock lock(mServiceLock); |
| 5452 | |
| 5453 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); |
| 5454 | } |
| 5455 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5456 | status_t CameraService::handleGetAutoframing(int out) { |
| 5457 | Mutex::Autolock lock(mServiceLock); |
| 5458 | |
| 5459 | return dprintf(out, "autoframing override: %d\n", mOverrideAutoframingMode); |
| 5460 | } |
| 5461 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5462 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { |
| 5463 | char *endPtr; |
| 5464 | errno = 0; |
| 5465 | String8 maskString8 = String8(args[1]); |
| 5466 | long maskValue = strtol(maskString8.c_str(), &endPtr, 10); |
| 5467 | |
| 5468 | if (errno != 0) return BAD_VALUE; |
| 5469 | if (endPtr != maskString8.c_str() + maskString8.size()) return BAD_VALUE; |
| 5470 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; |
| 5471 | |
| 5472 | Mutex::Autolock lock(mServiceLock); |
| 5473 | |
| 5474 | mImageDumpMask = maskValue; |
| 5475 | |
| 5476 | return OK; |
| 5477 | } |
| 5478 | |
| 5479 | status_t CameraService::handleGetImageDumpMask(int out) { |
| 5480 | Mutex::Autolock lock(mServiceLock); |
| 5481 | |
| 5482 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); |
| 5483 | } |
| 5484 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 5485 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { |
| 5486 | int muteValue = strtol(String8(args[1]), nullptr, 10); |
| 5487 | if (errno != 0) return BAD_VALUE; |
| 5488 | |
| 5489 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; |
| 5490 | Mutex::Autolock lock(mServiceLock); |
| 5491 | |
| 5492 | mOverrideCameraMuteMode = (muteValue == 1); |
| 5493 | |
| 5494 | const auto clients = mActiveClientManager.getAll(); |
| 5495 | for (auto& current : clients) { |
| 5496 | if (current != nullptr) { |
| 5497 | const auto basicClient = current->getValue(); |
| 5498 | if (basicClient.get() != nullptr) { |
| 5499 | if (basicClient->supportsCameraMute()) { |
| 5500 | basicClient->setCameraMute(mOverrideCameraMuteMode); |
| 5501 | } |
| 5502 | } |
| 5503 | } |
| 5504 | } |
| 5505 | |
| 5506 | return OK; |
| 5507 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5508 | |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5509 | status_t CameraService::handleSetStreamUseCaseOverrides(const Vector<String16>& args) { |
| 5510 | std::vector<int64_t> useCasesOverride; |
| 5511 | for (size_t i = 1; i < args.size(); i++) { |
| 5512 | int64_t useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
| 5513 | String8 arg8 = String8(args[i]); |
| 5514 | if (arg8 == "DEFAULT") { |
| 5515 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
| 5516 | } else if (arg8 == "PREVIEW") { |
| 5517 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW; |
| 5518 | } else if (arg8 == "STILL_CAPTURE") { |
| 5519 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_STILL_CAPTURE; |
| 5520 | } else if (arg8 == "VIDEO_RECORD") { |
| 5521 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_RECORD; |
| 5522 | } else if (arg8 == "PREVIEW_VIDEO_STILL") { |
| 5523 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW_VIDEO_STILL; |
| 5524 | } else if (arg8 == "VIDEO_CALL") { |
| 5525 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL; |
| 5526 | } else if (arg8 == "CROPPED_RAW") { |
| 5527 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_CROPPED_RAW; |
| 5528 | } else { |
| 5529 | ALOGE("%s: Invalid stream use case %s", __FUNCTION__, arg8.c_str()); |
| 5530 | return BAD_VALUE; |
| 5531 | } |
| 5532 | useCasesOverride.push_back(useCase); |
| 5533 | } |
| 5534 | |
| 5535 | Mutex::Autolock lock(mServiceLock); |
| 5536 | mStreamUseCaseOverrides = std::move(useCasesOverride); |
| 5537 | |
| 5538 | return OK; |
| 5539 | } |
| 5540 | |
| 5541 | void CameraService::handleClearStreamUseCaseOverrides() { |
| 5542 | Mutex::Autolock lock(mServiceLock); |
| 5543 | mStreamUseCaseOverrides.clear(); |
| 5544 | } |
| 5545 | |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 5546 | status_t CameraService::handleSetZoomOverride(const Vector<String16>& args) { |
| 5547 | char* end; |
| 5548 | int zoomOverrideValue = strtol(String8(args[1]), &end, /*base=*/10); |
| 5549 | if ((*end != '\0') || |
| 5550 | (zoomOverrideValue != -1 && |
| 5551 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF && |
| 5552 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM)) { |
| 5553 | return BAD_VALUE; |
| 5554 | } |
| 5555 | |
| 5556 | Mutex::Autolock lock(mServiceLock); |
| 5557 | mZoomOverrideValue = zoomOverrideValue; |
| 5558 | |
| 5559 | const auto clients = mActiveClientManager.getAll(); |
| 5560 | for (auto& current : clients) { |
| 5561 | if (current != nullptr) { |
| 5562 | const auto basicClient = current->getValue(); |
| 5563 | if (basicClient.get() != nullptr) { |
| 5564 | if (basicClient->supportsZoomOverride()) { |
| 5565 | basicClient->setZoomOverride(mZoomOverrideValue); |
| 5566 | } |
| 5567 | } |
| 5568 | } |
| 5569 | } |
| 5570 | |
| 5571 | return OK; |
| 5572 | } |
| 5573 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5574 | status_t CameraService::handleWatchCommand(const Vector<String16>& args, int inFd, int outFd) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5575 | if (args.size() >= 3 && args[1] == String16("start")) { |
| 5576 | return startWatchingTags(args, outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5577 | } else if (args.size() == 2 && args[1] == String16("stop")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5578 | return stopWatchingTags(outFd); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5579 | } else if (args.size() == 2 && args[1] == String16("dump")) { |
| 5580 | return printWatchedTags(outFd); |
| 5581 | } else if (args.size() >= 2 && args[1] == String16("live")) { |
| 5582 | return printWatchedTagsUntilInterrupt(args, inFd, outFd); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5583 | } else if (args.size() == 2 && args[1] == String16("clear")) { |
| 5584 | return clearCachedMonitoredTagDumps(outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5585 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5586 | dprintf(outFd, "Camera service watch commands:\n" |
| 5587 | " start -m <comma_separated_tag_list> [-c <comma_separated_client_list>]\n" |
| 5588 | " starts watching the provided tags for clients with provided package\n" |
| 5589 | " recognizes tag shorthands like '3a'\n" |
| 5590 | " 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] | 5591 | " dump dumps the monitoring information and exits\n" |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5592 | " stop stops watching all tags\n" |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5593 | " live [-n <refresh_interval_ms>]\n" |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5594 | " prints the monitored information in real time\n" |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5595 | " Hit return to exit\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5596 | " clear clears all buffers storing information for watch command"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5597 | return BAD_VALUE; |
| 5598 | } |
| 5599 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5600 | status_t CameraService::startWatchingTags(const Vector<String16> &args, int outFd) { |
| 5601 | Mutex::Autolock lock(mLogLock); |
| 5602 | size_t tagsIdx; // index of '-m' |
| 5603 | String16 tags(""); |
| 5604 | for (tagsIdx = 2; tagsIdx < args.size() && args[tagsIdx] != String16("-m"); tagsIdx++); |
| 5605 | if (tagsIdx < args.size() - 1) { |
| 5606 | tags = args[tagsIdx + 1]; |
| 5607 | } else { |
| 5608 | dprintf(outFd, "No tags provided.\n"); |
| 5609 | return BAD_VALUE; |
| 5610 | } |
| 5611 | |
| 5612 | size_t clientsIdx; // index of '-c' |
| 5613 | String16 clients = kWatchAllClientsFlag; // watch all clients if no clients are provided |
| 5614 | for (clientsIdx = 2; clientsIdx < args.size() && args[clientsIdx] != String16("-c"); |
| 5615 | clientsIdx++); |
| 5616 | if (clientsIdx < args.size() - 1) { |
| 5617 | clients = args[clientsIdx + 1]; |
| 5618 | } |
| 5619 | parseClientsToWatchLocked(String8(clients)); |
| 5620 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5621 | // track tags to initialize future clients with the monitoring information |
| 5622 | mMonitorTags = String8(tags); |
| 5623 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5624 | bool serviceLock = tryLock(mServiceLock); |
| 5625 | int numWatchedClients = 0; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5626 | auto cameraClients = mActiveClientManager.getAll(); |
| 5627 | for (const auto &clientDescriptor: cameraClients) { |
| 5628 | if (clientDescriptor == nullptr) { continue; } |
| 5629 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5630 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5631 | |
| 5632 | if (isClientWatchedLocked(client.get())) { |
| 5633 | client->startWatchingTags(mMonitorTags, outFd); |
| 5634 | numWatchedClients++; |
| 5635 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5636 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5637 | dprintf(outFd, "Started watching %d active clients\n", numWatchedClients); |
| 5638 | |
| 5639 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5640 | return OK; |
| 5641 | } |
| 5642 | |
| 5643 | status_t CameraService::stopWatchingTags(int outFd) { |
| 5644 | // clear mMonitorTags to prevent new clients from monitoring tags at initialization |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5645 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5646 | mMonitorTags = String8::empty(); |
| 5647 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5648 | mWatchedClientPackages.clear(); |
| 5649 | mWatchedClientsDumpCache.clear(); |
| 5650 | |
| 5651 | bool serviceLock = tryLock(mServiceLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5652 | auto cameraClients = mActiveClientManager.getAll(); |
| 5653 | for (const auto &clientDescriptor : cameraClients) { |
| 5654 | if (clientDescriptor == nullptr) { continue; } |
| 5655 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5656 | if (client.get() == nullptr) { continue; } |
| 5657 | client->stopWatchingTags(outFd); |
| 5658 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5659 | dprintf(outFd, "Stopped watching all clients.\n"); |
| 5660 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5661 | return OK; |
| 5662 | } |
| 5663 | |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5664 | status_t CameraService::clearCachedMonitoredTagDumps(int outFd) { |
| 5665 | Mutex::Autolock lock(mLogLock); |
| 5666 | size_t clearedSize = mWatchedClientsDumpCache.size(); |
| 5667 | mWatchedClientsDumpCache.clear(); |
| 5668 | dprintf(outFd, "Cleared tag information of %zu cached clients.\n", clearedSize); |
| 5669 | return OK; |
| 5670 | } |
| 5671 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5672 | status_t CameraService::printWatchedTags(int outFd) { |
| 5673 | Mutex::Autolock logLock(mLogLock); |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 5674 | std::set<String16> connectedMonitoredClients; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5675 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5676 | bool printedSomething = false; // tracks if any monitoring information was printed |
| 5677 | // (from either cached or active clients) |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5678 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5679 | bool serviceLock = tryLock(mServiceLock); |
| 5680 | // get all watched clients that are currently connected |
| 5681 | for (const auto &clientDescriptor: mActiveClientManager.getAll()) { |
| 5682 | if (clientDescriptor == nullptr) { continue; } |
| 5683 | |
| 5684 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5685 | if (client.get() == nullptr) { continue; } |
| 5686 | if (!isClientWatchedLocked(client.get())) { continue; } |
| 5687 | |
| 5688 | std::vector<std::string> dumpVector; |
| 5689 | client->dumpWatchedEventsToVector(dumpVector); |
| 5690 | |
| 5691 | size_t printIdx = dumpVector.size(); |
| 5692 | if (printIdx == 0) { |
| 5693 | continue; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5694 | } |
| 5695 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5696 | // Print tag dumps for active client |
| 5697 | const String8 &cameraId = clientDescriptor->getKey(); |
| 5698 | String8 packageName8 = String8(client->getPackageName()); |
| 5699 | const char *printablePackageName = packageName8.lockBuffer(packageName8.size()); |
| 5700 | dprintf(outFd, "Client: %s (active)\n", printablePackageName); |
| 5701 | while(printIdx > 0) { |
| 5702 | printIdx--; |
| 5703 | dprintf(outFd, "%s:%s %s", cameraId.string(), printablePackageName, |
| 5704 | dumpVector[printIdx].c_str()); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5705 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5706 | dprintf(outFd, "\n"); |
| 5707 | packageName8.unlockBuffer(); |
| 5708 | printedSomething = true; |
| 5709 | |
| 5710 | connectedMonitoredClients.emplace(client->getPackageName()); |
| 5711 | } |
| 5712 | if (serviceLock) { mServiceLock.unlock(); } |
| 5713 | |
| 5714 | // Print entries in mWatchedClientsDumpCache for clients that are not connected |
| 5715 | for (const auto &kv: mWatchedClientsDumpCache) { |
| 5716 | const String16 &package = kv.first; |
| 5717 | if (connectedMonitoredClients.find(package) != connectedMonitoredClients.end()) { |
| 5718 | continue; |
| 5719 | } |
| 5720 | |
| 5721 | dprintf(outFd, "Client: %s (cached)\n", String8(package).string()); |
| 5722 | dprintf(outFd, "%s\n", kv.second.c_str()); |
| 5723 | printedSomething = true; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5724 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5725 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5726 | if (!printedSomething) { |
| 5727 | dprintf(outFd, "No monitoring information to print.\n"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5728 | } |
| 5729 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5730 | return OK; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5731 | } |
| 5732 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5733 | // Print all events in vector `events' that came after lastPrintedEvent |
| 5734 | void printNewWatchedEvents(int outFd, |
| 5735 | const char *cameraId, |
| 5736 | const String16 &packageName, |
| 5737 | const std::vector<std::string> &events, |
| 5738 | const std::string &lastPrintedEvent) { |
| 5739 | if (events.empty()) { return; } |
| 5740 | |
| 5741 | // index of lastPrintedEvent in events. |
| 5742 | // lastPrintedIdx = events.size() if lastPrintedEvent is not in events |
| 5743 | size_t lastPrintedIdx; |
| 5744 | for (lastPrintedIdx = 0; |
| 5745 | lastPrintedIdx < events.size() && lastPrintedEvent != events[lastPrintedIdx]; |
| 5746 | lastPrintedIdx++); |
| 5747 | |
| 5748 | if (lastPrintedIdx == 0) { return; } // early exit if no new event in `events` |
| 5749 | |
| 5750 | String8 packageName8(packageName); |
| 5751 | const char *printablePackageName = packageName8.lockBuffer(packageName8.size()); |
| 5752 | |
| 5753 | // print events in chronological order (latest event last) |
| 5754 | size_t idxToPrint = lastPrintedIdx; |
| 5755 | do { |
| 5756 | idxToPrint--; |
| 5757 | dprintf(outFd, "%s:%s %s", cameraId, printablePackageName, events[idxToPrint].c_str()); |
| 5758 | } while (idxToPrint != 0); |
| 5759 | |
| 5760 | packageName8.unlockBuffer(); |
| 5761 | } |
| 5762 | |
| 5763 | // Returns true if adb shell cmd watch should be interrupted based on data in inFd. The watch |
| 5764 | // command should be interrupted if the user presses the return key, or if user loses any way to |
| 5765 | // signal interrupt. |
| 5766 | // If timeoutMs == 0, this function will always return false |
| 5767 | bool shouldInterruptWatchCommand(int inFd, int outFd, long timeoutMs) { |
| 5768 | struct timeval startTime; |
| 5769 | int startTimeError = gettimeofday(&startTime, nullptr); |
| 5770 | if (startTimeError) { |
| 5771 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 5772 | return true; |
| 5773 | } |
| 5774 | |
| 5775 | const nfds_t numFds = 1; |
| 5776 | struct pollfd pollFd = { .fd = inFd, .events = POLLIN, .revents = 0 }; |
| 5777 | |
| 5778 | struct timeval currTime; |
| 5779 | char buffer[2]; |
| 5780 | while(true) { |
| 5781 | int currTimeError = gettimeofday(&currTime, nullptr); |
| 5782 | if (currTimeError) { |
| 5783 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 5784 | return true; |
| 5785 | } |
| 5786 | |
| 5787 | long elapsedTimeMs = ((currTime.tv_sec - startTime.tv_sec) * 1000L) |
| 5788 | + ((currTime.tv_usec - startTime.tv_usec) / 1000L); |
| 5789 | int remainingTimeMs = (int) (timeoutMs - elapsedTimeMs); |
| 5790 | |
| 5791 | if (remainingTimeMs <= 0) { |
| 5792 | // No user interrupt within timeoutMs, don't interrupt watch command |
| 5793 | return false; |
| 5794 | } |
| 5795 | |
| 5796 | int numFdsUpdated = poll(&pollFd, numFds, remainingTimeMs); |
| 5797 | if (numFdsUpdated < 0) { |
| 5798 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 5799 | return true; |
| 5800 | } |
| 5801 | |
| 5802 | if (numFdsUpdated == 0) { |
| 5803 | // No user input within timeoutMs, don't interrupt watch command |
| 5804 | return false; |
| 5805 | } |
| 5806 | |
| 5807 | if (!(pollFd.revents & POLLIN)) { |
| 5808 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 5809 | return true; |
| 5810 | } |
| 5811 | |
| 5812 | ssize_t sizeRead = read(inFd, buffer, sizeof(buffer) - 1); |
| 5813 | if (sizeRead < 0) { |
| 5814 | dprintf(outFd, "Error reading user input. Exiting.\n"); |
| 5815 | return true; |
| 5816 | } |
| 5817 | |
| 5818 | if (sizeRead == 0) { |
| 5819 | // Reached end of input fd (can happen if input is piped) |
| 5820 | // User has no way to signal an interrupt, so interrupt here |
| 5821 | return true; |
| 5822 | } |
| 5823 | |
| 5824 | if (buffer[0] == '\n') { |
| 5825 | // User pressed return, interrupt watch command. |
| 5826 | return true; |
| 5827 | } |
| 5828 | } |
| 5829 | } |
| 5830 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5831 | status_t CameraService::printWatchedTagsUntilInterrupt(const Vector<String16> &args, |
| 5832 | int inFd, int outFd) { |
| 5833 | // Figure out refresh interval, if present in args |
| 5834 | long refreshTimeoutMs = 1000L; // refresh every 1s by default |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5835 | if (args.size() > 2) { |
| 5836 | size_t intervalIdx; // index of '-n' |
| 5837 | for (intervalIdx = 2; intervalIdx < args.size() && String16("-n") != args[intervalIdx]; |
| 5838 | intervalIdx++); |
| 5839 | |
| 5840 | size_t intervalValIdx = intervalIdx + 1; |
| 5841 | if (intervalValIdx < args.size()) { |
| 5842 | refreshTimeoutMs = strtol(String8(args[intervalValIdx].string()), nullptr, 10); |
| 5843 | if (errno) { return BAD_VALUE; } |
| 5844 | } |
| 5845 | } |
| 5846 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5847 | // Set min timeout of 10ms. This prevents edge cases in polling when timeout of 0 is passed. |
| 5848 | refreshTimeoutMs = refreshTimeoutMs < 10 ? 10 : refreshTimeoutMs; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5849 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5850 | dprintf(outFd, "Press return to exit...\n\n"); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5851 | std::map<String16, std::string> packageNameToLastEvent; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5852 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5853 | while (true) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5854 | bool serviceLock = tryLock(mServiceLock); |
| 5855 | auto cameraClients = mActiveClientManager.getAll(); |
| 5856 | if (serviceLock) { mServiceLock.unlock(); } |
| 5857 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5858 | for (const auto& clientDescriptor : cameraClients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5859 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5860 | if (clientDescriptor == nullptr) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5861 | |
| 5862 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5863 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5864 | if (!isClientWatchedLocked(client.get())) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5865 | |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5866 | const String16 &packageName = client->getPackageName(); |
| 5867 | // This also initializes the map entries with an empty string |
| 5868 | const std::string& lastPrintedEvent = packageNameToLastEvent[packageName]; |
| 5869 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5870 | std::vector<std::string> latestEvents; |
| 5871 | client->dumpWatchedEventsToVector(latestEvents); |
| 5872 | |
| 5873 | if (!latestEvents.empty()) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5874 | String8 cameraId = clientDescriptor->getKey(); |
| 5875 | const char *printableCameraId = cameraId.lockBuffer(cameraId.size()); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5876 | printNewWatchedEvents(outFd, |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5877 | printableCameraId, |
| 5878 | packageName, |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5879 | latestEvents, |
| 5880 | lastPrintedEvent); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5881 | packageNameToLastEvent[packageName] = latestEvents[0]; |
| 5882 | cameraId.unlockBuffer(); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5883 | } |
| 5884 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5885 | if (shouldInterruptWatchCommand(inFd, outFd, refreshTimeoutMs)) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5886 | break; |
| 5887 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5888 | } |
| 5889 | return OK; |
| 5890 | } |
| 5891 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5892 | void CameraService::parseClientsToWatchLocked(String8 clients) { |
| 5893 | mWatchedClientPackages.clear(); |
| 5894 | |
| 5895 | const char *allSentinel = String8(kWatchAllClientsFlag).string(); |
| 5896 | |
| 5897 | char *tokenized = clients.lockBuffer(clients.size()); |
| 5898 | char *savePtr; |
| 5899 | char *nextClient = strtok_r(tokenized, ",", &savePtr); |
| 5900 | |
| 5901 | while (nextClient != nullptr) { |
| 5902 | if (strcmp(nextClient, allSentinel) == 0) { |
| 5903 | // Don't need to track any other package if 'all' is present |
| 5904 | mWatchedClientPackages.clear(); |
| 5905 | mWatchedClientPackages.emplace(kWatchAllClientsFlag); |
| 5906 | break; |
| 5907 | } |
| 5908 | |
| 5909 | // track package names |
| 5910 | mWatchedClientPackages.emplace(nextClient); |
| 5911 | nextClient = strtok_r(nullptr, ",", &savePtr); |
| 5912 | } |
| 5913 | clients.unlockBuffer(); |
| 5914 | } |
| 5915 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5916 | status_t CameraService::printHelp(int out) { |
| 5917 | return dprintf(out, "Camera service commands:\n" |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5918 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 5919 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 5920 | " 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] | 5921 | " set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" |
| 5922 | " Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" |
| 5923 | " 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] | 5924 | " set-autoframing <VALUE> overrides the autoframing value for AUTO\n" |
| 5925 | " Valid values 0=false, 1=true, 2=auto\n" |
| 5926 | " get-autoframing returns the current override autoframing value\n" |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5927 | " set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" |
| 5928 | " Valid values 0=OFF, 1=ON for JPEG\n" |
| 5929 | " 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] | 5930 | " set-camera-mute <0/1> enable or disable camera muting\n" |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5931 | " set-stream-use-case-override <usecase1> <usecase2> ... override stream use cases\n" |
| 5932 | " Use cases applied in descending resolutions. So usecase1 is assigned to the\n" |
| 5933 | " largest resolution, usecase2 is assigned to the 2nd largest resolution, and so\n" |
| 5934 | " on. In case the number of usecases is smaller than the number of streams, the\n" |
| 5935 | " last use case is assigned to all the remaining streams. In case of multiple\n" |
| 5936 | " streams with the same resolution, the tie-breaker is (JPEG, RAW, YUV, and PRIV)\n" |
| 5937 | " Valid values are (case sensitive): DEFAULT, PREVIEW, STILL_CAPTURE, VIDEO_RECORD,\n" |
| 5938 | " PREVIEW_VIDEO_STILL, VIDEO_CALL, CROPPED_RAW\n" |
| 5939 | " clear-stream-use-case-override clear the stream use case override\n" |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 5940 | " set-zoom-override <-1/0/1> enable or disable zoom override\n" |
| 5941 | " Valid values -1: do not override, 0: override to OFF, 1: override to ZOOM\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5942 | " 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] | 5943 | " help print this message\n"); |
| 5944 | } |
| 5945 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5946 | bool CameraService::isClientWatched(const BasicClient *client) { |
| 5947 | Mutex::Autolock lock(mLogLock); |
| 5948 | return isClientWatchedLocked(client); |
| 5949 | } |
| 5950 | |
| 5951 | bool CameraService::isClientWatchedLocked(const BasicClient *client) { |
| 5952 | return mWatchedClientPackages.find(kWatchAllClientsFlag) != mWatchedClientPackages.end() || |
| 5953 | mWatchedClientPackages.find(client->getPackageName()) != mWatchedClientPackages.end(); |
| 5954 | } |
| 5955 | |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 5956 | int32_t CameraService::updateAudioRestriction() { |
| 5957 | Mutex::Autolock lock(mServiceLock); |
| 5958 | return updateAudioRestrictionLocked(); |
| 5959 | } |
| 5960 | |
| 5961 | int32_t CameraService::updateAudioRestrictionLocked() { |
| 5962 | int32_t mode = 0; |
| 5963 | // iterate through all active client |
| 5964 | for (const auto& i : mActiveClientManager.getAll()) { |
| 5965 | const auto clientSp = i->getValue(); |
| 5966 | mode |= clientSp->getAudioRestriction(); |
| 5967 | } |
| 5968 | |
| 5969 | bool modeChanged = (mAudioRestriction != mode); |
| 5970 | mAudioRestriction = mode; |
| 5971 | if (modeChanged) { |
| 5972 | mAppOps.setCameraAudioRestriction(mode); |
| 5973 | } |
| 5974 | return mode; |
| 5975 | } |
| 5976 | |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 5977 | status_t CameraService::checkIfInjectionCameraIsPresent(const String8& externalCamId, |
| 5978 | sp<BasicClient> clientSp) { |
| 5979 | std::unique_ptr<AutoConditionLock> lock = |
| 5980 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 5981 | status_t res = NO_ERROR; |
| 5982 | if ((res = checkIfDeviceIsUsable(externalCamId)) != NO_ERROR) { |
| 5983 | ALOGW("Device %s is not usable!", externalCamId.string()); |
| 5984 | mInjectionStatusListener->notifyInjectionError( |
| 5985 | externalCamId, UNKNOWN_TRANSACTION); |
| 5986 | clientSp->notifyError( |
| 5987 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 5988 | CaptureResultExtras()); |
| 5989 | |
| 5990 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 5991 | // other clients from connecting in mServiceLockWrapper if held |
| 5992 | mServiceLock.unlock(); |
| 5993 | |
| 5994 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
| 5995 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 5996 | clientSp->disconnect(); |
| 5997 | CameraThreadState::restoreCallingIdentity(token); |
| 5998 | |
| 5999 | // Reacquire mServiceLock |
| 6000 | mServiceLock.lock(); |
| 6001 | } |
| 6002 | |
| 6003 | return res; |
| 6004 | } |
| 6005 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 6006 | void CameraService::clearInjectionParameters() { |
| 6007 | { |
| 6008 | Mutex::Autolock lock(mInjectionParametersLock); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 6009 | mInjectionInitPending = false; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 6010 | mInjectionInternalCamId = ""; |
| 6011 | } |
| 6012 | mInjectionExternalCamId = ""; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 6013 | mInjectionStatusListener->removeListener(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 6014 | } |
| 6015 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6016 | }; // namespace android |