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"; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 90 | }; // namespace anonymous |
| 91 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 92 | namespace android { |
| 93 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 94 | using base::StringPrintf; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 95 | using binder::Status; |
Austin Borger | ea93124 | 2021-12-13 23:10:41 +0000 | [diff] [blame] | 96 | using namespace camera3; |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 97 | using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService; |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 98 | using frameworks::cameraservice::service::implementation::AidlCameraService; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 99 | using hardware::ICamera; |
| 100 | using hardware::ICameraClient; |
| 101 | using hardware::ICameraServiceListener; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 102 | using hardware::camera2::ICameraInjectionCallback; |
| 103 | using hardware::camera2::ICameraInjectionSession; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 104 | using hardware::camera2::utils::CameraIdAndSessionConfiguration; |
| 105 | using hardware::camera2::utils::ConcurrentCameraIdCombination; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 106 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 107 | // ---------------------------------------------------------------------------- |
| 108 | // Logging support -- this is for debugging only |
| 109 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 110 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 111 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 112 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 113 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 114 | |
| 115 | static void setLogLevel(int level) { |
| 116 | android_atomic_write(level, &gLogLevel); |
| 117 | } |
| 118 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 119 | // Convenience methods for constructing binder::Status objects for error returns |
| 120 | |
| 121 | #define STATUS_ERROR(errorCode, errorString) \ |
| 122 | binder::Status::fromServiceSpecificError(errorCode, \ |
| 123 | String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString)) |
| 124 | |
| 125 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ |
| 126 | binder::Status::fromServiceSpecificError(errorCode, \ |
| 127 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \ |
| 128 | __VA_ARGS__)) |
| 129 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 130 | // ---------------------------------------------------------------------------- |
| 131 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 132 | static const String16 sDumpPermission("android.permission.DUMP"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 133 | static const String16 sManageCameraPermission("android.permission.MANAGE_CAMERA"); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 134 | static const String16 sCameraPermission("android.permission.CAMERA"); |
| 135 | static const String16 sSystemCameraPermission("android.permission.SYSTEM_CAMERA"); |
| 136 | static const String16 |
| 137 | sCameraSendSystemEventsPermission("android.permission.CAMERA_SEND_SYSTEM_EVENTS"); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 138 | static const String16 sCameraOpenCloseListenerPermission( |
| 139 | "android.permission.CAMERA_OPEN_CLOSE_LISTENER"); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 140 | static const String16 |
| 141 | sCameraInjectExternalCameraPermission("android.permission.CAMERA_INJECT_EXTERNAL_CAMERA"); |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 142 | // Constant integer for FGS Logging, used to denote the API type for logger |
| 143 | static const int LOG_FGS_CAMERA_API = 1; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 144 | const char *sFileName = "lastOpenSessionDumpFile"; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 145 | static constexpr int32_t kSystemNativeClientScore = resource_policy::PERCEPTIBLE_APP_ADJ; |
| 146 | static constexpr int32_t kSystemNativeClientState = |
| 147 | ActivityManager::PROCESS_STATE_PERSISTENT_UI; |
Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame] | 148 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 149 | const String8 CameraService::kOfflineDevice("offline-"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 150 | const String16 CameraService::kWatchAllClientsFlag("all"); |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 151 | |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 152 | // Set to keep track of logged service error events. |
| 153 | static std::set<String8> sServiceErrorEventSet; |
| 154 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 155 | CameraService::CameraService( |
| 156 | std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper) : |
| 157 | mCameraServiceProxyWrapper(cameraServiceProxyWrapper == nullptr ? |
| 158 | std::make_shared<CameraServiceProxyWrapper>() : cameraServiceProxyWrapper), |
Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 159 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 160 | mNumberOfCameras(0), |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 161 | mNumberOfCamerasWithoutSystemCamera(0), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 162 | mSoundRef(0), mInitialized(false), |
| 163 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 164 | ALOGI("CameraService started (pid=%d)", getpid()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 165 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 166 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 167 | if (mMemFd == -1) { |
| 168 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 169 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 172 | // The word 'System' here does not refer to clients only on the system |
| 173 | // partition. They just need to have a android system uid. |
| 174 | static bool doesClientHaveSystemUid() { |
| 175 | return (CameraThreadState::getCallingUid() < AID_APP_START); |
| 176 | } |
| 177 | |
Charles Chen | a7b613c | 2023-01-24 21:57:33 +0000 | [diff] [blame] | 178 | // Enable processes with isolated AID to request the binder |
| 179 | void CameraService::instantiate() { |
| 180 | CameraService::publish(true); |
| 181 | } |
| 182 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 183 | void CameraService::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
| 184 | if (name != String16(kAppopsServiceName)) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | ALOGV("appops service registered. setting camera audio restriction"); |
| 189 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
| 190 | } |
| 191 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 192 | void CameraService::onFirstRef() |
| 193 | { |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 194 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 195 | ALOGI("CameraService process starting"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 196 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 197 | BnCameraService::onFirstRef(); |
| 198 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 199 | // Update battery life tracking if service is restarting |
| 200 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 201 | notifier.noteResetCamera(); |
| 202 | notifier.noteResetFlashlight(); |
| 203 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 204 | status_t res = INVALID_OPERATION; |
Eino-Ville Talvala | 9cbbc83 | 2017-01-23 15:39:53 -0800 | [diff] [blame] | 205 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 206 | res = enumerateProviders(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 207 | if (res == OK) { |
| 208 | mInitialized = true; |
| 209 | } |
| 210 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 211 | mUidPolicy = new UidPolicy(this); |
| 212 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 213 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this); |
| 214 | mSensorPrivacyPolicy->registerSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 215 | mInjectionStatusListener = new InjectionStatusListener(this); |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 216 | |
| 217 | // appops function setCamerAudioRestriction uses getService which |
| 218 | // is blocking till the appops service is ready. To enable early |
| 219 | // boot availability for cameraservice, use checkService which is |
| 220 | // non blocking and register for notifications |
| 221 | sp<IServiceManager> sm = defaultServiceManager(); |
| 222 | sp<IBinder> binder = sm->checkService(String16(kAppopsServiceName)); |
| 223 | if (!binder) { |
| 224 | sm->registerForNotifications(String16(kAppopsServiceName), this); |
| 225 | } else { |
| 226 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
| 227 | } |
| 228 | |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 229 | sp<HidlCameraService> hcs = HidlCameraService::getInstance(this); |
| 230 | if (hcs->registerAsService() != android::OK) { |
Devin Moore | a1350e7 | 2023-01-11 23:40:42 +0000 | [diff] [blame] | 231 | // Deprecated, so it will fail to register on newer devices |
| 232 | ALOGW("%s: Did not register default android.frameworks.cameraservice.service@2.2", |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 233 | __FUNCTION__); |
| 234 | } |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 235 | |
Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 236 | if (!AidlCameraService::registerService(this)) { |
| 237 | ALOGE("%s: Failed to register default AIDL VNDK CameraService", __FUNCTION__); |
| 238 | } |
| 239 | |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 240 | // This needs to be last call in this function, so that it's as close to |
| 241 | // ServiceManager::addService() as possible. |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 242 | mCameraServiceProxyWrapper->pingCameraServiceProxy(); |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 243 | ALOGI("CameraService pinged cameraservice proxy"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 246 | status_t CameraService::enumerateProviders() { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 247 | status_t res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 248 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 249 | std::vector<std::string> deviceIds; |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 250 | std::unordered_map<std::string, std::set<std::string>> unavailPhysicalIds; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 251 | { |
| 252 | Mutex::Autolock l(mServiceLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 253 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 254 | if (nullptr == mCameraProviderManager.get()) { |
| 255 | mCameraProviderManager = new CameraProviderManager(); |
| 256 | res = mCameraProviderManager->initialize(this); |
| 257 | if (res != OK) { |
| 258 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", |
| 259 | __FUNCTION__, strerror(-res), res); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 260 | logServiceError(String8::format("Unable to initialize camera provider manager"), |
| 261 | ERROR_DISCONNECTED); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 262 | return res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 266 | |
| 267 | // Setup vendor tags before we call get_camera_info the first time |
| 268 | // because HAL might need to setup static vendor keys in get_camera_info |
| 269 | // TODO: maybe put this into CameraProviderManager::initialize()? |
| 270 | mCameraProviderManager->setUpVendorTags(); |
| 271 | |
| 272 | if (nullptr == mFlashlight.get()) { |
| 273 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 276 | res = mFlashlight->findFlashUnits(); |
| 277 | if (res != OK) { |
| 278 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); |
| 279 | } |
| 280 | |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 281 | deviceIds = mCameraProviderManager->getCameraDeviceIds(&unavailPhysicalIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | |
| 285 | for (auto& cameraId : deviceIds) { |
| 286 | String8 id8 = String8(cameraId.c_str()); |
Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 287 | if (getCameraState(id8) == nullptr) { |
| 288 | onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT); |
| 289 | } |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 290 | if (unavailPhysicalIds.count(cameraId) > 0) { |
| 291 | for (const auto& physicalId : unavailPhysicalIds[cameraId]) { |
| 292 | String8 physicalId8 = String8(physicalId.c_str()); |
| 293 | onDeviceStatusChanged(id8, physicalId8, CameraDeviceStatus::NOT_PRESENT); |
| 294 | } |
| 295 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 298 | // Derive primary rear/front cameras, and filter their charactierstics. |
| 299 | // 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] | 300 | if (SessionConfigurationUtils::IS_PERF_CLASS) { |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 301 | // Assume internal cameras are advertised from the same |
| 302 | // provider. If multiple providers are registered at different time, |
| 303 | // and each provider contains multiple internal color cameras, the current |
| 304 | // logic may filter the characteristics of more than one front/rear color |
| 305 | // cameras. |
| 306 | Mutex::Autolock l(mServiceLock); |
| 307 | filterSPerfClassCharacteristicsLocked(); |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 308 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 309 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 310 | return OK; |
| 311 | } |
| 312 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 313 | void CameraService::broadcastTorchModeStatus(const String8& cameraId, TorchModeStatus status, |
| 314 | SystemCameraKind systemCameraKind) { |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 315 | Mutex::Autolock lock(mStatusListenerLock); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 316 | for (auto& i : mListenerList) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 317 | if (shouldSkipStatusUpdates(systemCameraKind, i->isVendorListener(), i->getListenerPid(), |
| 318 | i->getListenerUid())) { |
| 319 | ALOGV("Skipping torch callback for system-only camera device %s", |
| 320 | cameraId.c_str()); |
| 321 | continue; |
| 322 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 323 | auto ret = i->getListener()->onTorchStatusChanged(mapToInterface(status), |
| 324 | String16{cameraId}); |
| 325 | i->handleBinderStatus(ret, "%s: Failed to trigger onTorchStatusChanged for %d:%d: %d", |
| 326 | __FUNCTION__, i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 330 | CameraService::~CameraService() { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 331 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 332 | mUidPolicy->unregisterSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 333 | mSensorPrivacyPolicy->unregisterSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 334 | mInjectionStatusListener->removeListener(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 337 | void CameraService::onNewProviderRegistered() { |
| 338 | enumerateProviders(); |
| 339 | } |
| 340 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 341 | void CameraService::filterAPI1SystemCameraLocked( |
| 342 | const std::vector<std::string> &normalDeviceIds) { |
| 343 | mNormalDeviceIdsWithoutSystemCamera.clear(); |
| 344 | for (auto &deviceId : normalDeviceIds) { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 345 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 346 | if (getSystemCameraKind(String8(deviceId.c_str()), &deviceKind) != OK) { |
| 347 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, deviceId.c_str()); |
| 348 | continue; |
| 349 | } |
| 350 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 351 | // All system camera ids will necessarily come after public camera |
| 352 | // device ids as per the HAL interface contract. |
| 353 | break; |
| 354 | } |
| 355 | mNormalDeviceIdsWithoutSystemCamera.push_back(deviceId); |
| 356 | } |
| 357 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, |
| 358 | mNormalDeviceIdsWithoutSystemCamera.size()); |
| 359 | } |
| 360 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 361 | status_t CameraService::getSystemCameraKind(const String8& cameraId, SystemCameraKind *kind) const { |
| 362 | auto state = getCameraState(cameraId); |
| 363 | if (state != nullptr) { |
| 364 | *kind = state->getSystemCameraKind(); |
| 365 | return OK; |
| 366 | } |
| 367 | // Hidden physical camera ids won't have CameraState |
| 368 | return mCameraProviderManager->getSystemCameraKind(cameraId.c_str(), kind); |
| 369 | } |
| 370 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 371 | void CameraService::updateCameraNumAndIds() { |
| 372 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 373 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); |
| 374 | // Excludes hidden secure cameras |
| 375 | mNumberOfCameras = |
| 376 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; |
| 377 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 378 | mNormalDeviceIds = |
| 379 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 380 | filterAPI1SystemCameraLocked(mNormalDeviceIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 383 | void CameraService::filterSPerfClassCharacteristicsLocked() { |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 384 | // To claim to be S Performance primary cameras, the cameras must be |
| 385 | // backward compatible. So performance class primary camera Ids must be API1 |
| 386 | // compatible. |
| 387 | bool firstRearCameraSeen = false, firstFrontCameraSeen = false; |
| 388 | for (const auto& cameraId : mNormalDeviceIdsWithoutSystemCamera) { |
| 389 | int facing = -1; |
| 390 | int orientation = 0; |
| 391 | String8 cameraId8(cameraId.c_str()); |
Shuzhen Wang | f221e8d | 2022-12-15 13:26:29 -0800 | [diff] [blame] | 392 | int portraitRotation; |
| 393 | getDeviceVersion(cameraId8, /*overrideToPortrait*/false, /*out*/&portraitRotation, |
| 394 | /*out*/&facing, /*out*/&orientation); |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 395 | if (facing == -1) { |
| 396 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.c_str()); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | if ((facing == hardware::CAMERA_FACING_BACK && !firstRearCameraSeen) || |
| 401 | (facing == hardware::CAMERA_FACING_FRONT && !firstFrontCameraSeen)) { |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 402 | status_t res = mCameraProviderManager->filterSmallJpegSizes(cameraId); |
| 403 | if (res == OK) { |
| 404 | mPerfClassPrimaryCameraIds.insert(cameraId); |
| 405 | } else { |
| 406 | ALOGE("%s: Failed to filter small JPEG sizes for performance class primary " |
| 407 | "camera %s: %s(%d)", __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
| 408 | break; |
| 409 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 410 | |
| 411 | if (facing == hardware::CAMERA_FACING_BACK) { |
| 412 | firstRearCameraSeen = true; |
| 413 | } |
| 414 | if (facing == hardware::CAMERA_FACING_FRONT) { |
| 415 | firstFrontCameraSeen = true; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (firstRearCameraSeen && firstFrontCameraSeen) { |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 425 | void CameraService::addStates(const String8 id) { |
| 426 | std::string cameraId(id.c_str()); |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 427 | CameraResourceCost cost; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 428 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); |
| 429 | if (res != OK) { |
| 430 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); |
| 431 | return; |
| 432 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 433 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 434 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); |
| 435 | if (res != OK) { |
| 436 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); |
| 437 | return; |
| 438 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 439 | std::vector<std::string> physicalCameraIds; |
| 440 | mCameraProviderManager->isLogicalCamera(cameraId, &physicalCameraIds); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 441 | std::set<String8> conflicting; |
| 442 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { |
| 443 | conflicting.emplace(String8(cost.conflictingDevices[i].c_str())); |
| 444 | } |
| 445 | |
| 446 | { |
| 447 | Mutex::Autolock lock(mCameraStatesLock); |
| 448 | mCameraStates.emplace(id, std::make_shared<CameraState>(id, cost.resourceCost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 449 | conflicting, deviceKind, physicalCameraIds)); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 453 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 454 | mTorchStatusMap.add(id, TorchModeStatus::AVAILABLE_OFF); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 455 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 456 | broadcastTorchModeStatus(id, TorchModeStatus::AVAILABLE_OFF, deviceKind); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 457 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 458 | |
| 459 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 460 | logDeviceAdded(id, "Device added"); |
| 461 | } |
| 462 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 463 | void CameraService::removeStates(const String8 id) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 464 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 465 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 466 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 467 | mTorchStatusMap.removeItem(id); |
| 468 | } |
| 469 | |
| 470 | { |
| 471 | Mutex::Autolock lock(mCameraStatesLock); |
| 472 | mCameraStates.erase(id); |
| 473 | } |
| 474 | } |
| 475 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 476 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 477 | CameraDeviceStatus newHalStatus) { |
| 478 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, |
| 479 | id.string(), newHalStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 480 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 481 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 482 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 483 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 484 | |
| 485 | if (state == nullptr) { |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 486 | if (newStatus == StatusInternal::PRESENT) { |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 487 | ALOGI("%s: Unknown camera ID %s, a new camera is added", |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 488 | __FUNCTION__, id.string()); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 489 | |
| 490 | // First add as absent to make sure clients are notified below |
| 491 | addStates(id); |
| 492 | |
| 493 | updateStatus(newStatus, id); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 494 | } else { |
| 495 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string()); |
| 496 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 500 | StatusInternal oldStatus = state->getStatus(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 501 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 502 | if (oldStatus == newStatus) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 503 | 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] | 504 | return; |
| 505 | } |
| 506 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 507 | if (newStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 508 | logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 509 | newStatus)); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 510 | |
| 511 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 512 | // to this device until the status changes |
| 513 | updateStatus(StatusInternal::NOT_PRESENT, id); |
| 514 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 515 | sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 516 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 517 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 518 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 519 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 520 | // Remove cached shim parameters |
| 521 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 522 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 523 | // Remove online as well as offline client from the list of active clients, |
| 524 | // if they are present |
| 525 | clientToDisconnectOnline = removeClientLocked(id); |
| 526 | clientToDisconnectOffline = removeClientLocked(kOfflineDevice + id); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 527 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 528 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 529 | disconnectClient(id, clientToDisconnectOnline); |
| 530 | disconnectClient(kOfflineDevice + id, clientToDisconnectOffline); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 531 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 532 | removeStates(id); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 533 | } else { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 534 | if (oldStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 535 | logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 536 | newStatus)); |
| 537 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 538 | updateStatus(newStatus, id); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 539 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 540 | } |
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 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 543 | const String8& physicalId, |
| 544 | CameraDeviceStatus newHalStatus) { |
| 545 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", |
| 546 | __FUNCTION__, id.string(), physicalId.string(), newHalStatus); |
| 547 | |
| 548 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 549 | |
| 550 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 551 | |
| 552 | if (state == nullptr) { |
| 553 | 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] | 554 | __FUNCTION__, physicalId.string(), id.string()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 555 | return; |
| 556 | } |
| 557 | |
| 558 | StatusInternal logicalCameraStatus = state->getStatus(); |
| 559 | if (logicalCameraStatus != StatusInternal::PRESENT && |
| 560 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 561 | ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d", |
| 562 | __FUNCTION__, physicalId.string(), newHalStatus, logicalCameraStatus); |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | bool updated = false; |
| 567 | if (newStatus == StatusInternal::PRESENT) { |
| 568 | updated = state->removeUnavailablePhysicalId(physicalId); |
| 569 | } else { |
| 570 | updated = state->addUnavailablePhysicalId(physicalId); |
| 571 | } |
| 572 | |
| 573 | if (updated) { |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 574 | String8 idCombo = id + " : " + physicalId; |
| 575 | if (newStatus == StatusInternal::PRESENT) { |
| 576 | logDeviceAdded(idCombo, |
| 577 | String8::format("Device status changed to %d", newStatus)); |
| 578 | } else { |
| 579 | logDeviceRemoved(idCombo, |
| 580 | String8::format("Device status changed to %d", newStatus)); |
| 581 | } |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 582 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 583 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 584 | if (getSystemCameraKind(id, &deviceKind) != OK) { |
| 585 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.string()); |
| 586 | return; |
| 587 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 588 | String16 id16(id), physicalId16(physicalId); |
| 589 | Mutex::Autolock lock(mStatusListenerLock); |
| 590 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 591 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 592 | listener->getListenerPid(), listener->getListenerUid())) { |
| 593 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 594 | id.c_str()); |
| 595 | continue; |
| 596 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 597 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged( |
| 598 | mapToInterface(newStatus), id16, physicalId16); |
| 599 | listener->handleBinderStatus(ret, |
| 600 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 601 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 602 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 603 | } |
| 604 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 607 | void CameraService::disconnectClient(const String8& id, sp<BasicClient> clientToDisconnect) { |
| 608 | if (clientToDisconnect.get() != nullptr) { |
| 609 | ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL", |
| 610 | __FUNCTION__, id.string()); |
| 611 | // Notify the client of disconnection |
| 612 | clientToDisconnect->notifyError( |
| 613 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 614 | CaptureResultExtras{}); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 615 | clientToDisconnect->disconnect(); |
| 616 | } |
| 617 | } |
| 618 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 619 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 620 | TorchModeStatus newStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 621 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 622 | status_t res = getSystemCameraKind(cameraId, &systemCameraKind); |
| 623 | if (res != OK) { |
| 624 | ALOGE("%s: Could not get system camera kind for camera id %s", __FUNCTION__, |
| 625 | cameraId.string()); |
| 626 | return; |
| 627 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 628 | Mutex::Autolock al(mTorchStatusMutex); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 629 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Jayant Chowdhary | 46ef0f5 | 2021-10-05 14:36:13 -0700 | [diff] [blame] | 632 | |
| 633 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
| 634 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
| 635 | Mutex::Autolock al(mTorchStatusMutex); |
| 636 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
| 637 | } |
| 638 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 639 | void CameraService::broadcastTorchStrengthLevel(const String8& cameraId, |
| 640 | int32_t newStrengthLevel) { |
| 641 | Mutex::Autolock lock(mStatusListenerLock); |
| 642 | for (auto& i : mListenerList) { |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 643 | auto ret = i->getListener()->onTorchStrengthLevelChanged(String16{cameraId}, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 644 | newStrengthLevel); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 645 | i->handleBinderStatus(ret, |
| 646 | "%s: Failed to trigger onTorchStrengthLevelChanged for %d:%d: %d", __FUNCTION__, |
| 647 | i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 651 | void CameraService::onTorchStatusChangedLocked(const String8& cameraId, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 652 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 653 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
| 654 | __FUNCTION__, cameraId.string(), newStatus); |
| 655 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 656 | TorchModeStatus status; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 657 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 658 | if (res) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 659 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", |
| 660 | __FUNCTION__, cameraId.string(), strerror(-res), res); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 661 | return; |
| 662 | } |
| 663 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 664 | return; |
| 665 | } |
| 666 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 667 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 668 | if (res) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 669 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, |
| 670 | (uint32_t)newStatus, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 671 | return; |
| 672 | } |
| 673 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 674 | { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 675 | // Update battery life logging for flashlight |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 676 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 677 | auto iter = mTorchUidMap.find(cameraId); |
| 678 | if (iter != mTorchUidMap.end()) { |
| 679 | int oldUid = iter->second.second; |
| 680 | int newUid = iter->second.first; |
| 681 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 682 | if (oldUid != newUid) { |
| 683 | // 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] | 684 | if (status == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 685 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 686 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 687 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 688 | notifier.noteFlashlightOn(cameraId, newUid); |
| 689 | } |
| 690 | iter->second.second = newUid; |
| 691 | } else { |
| 692 | // If the UID has not changed, log the status |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 693 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 694 | notifier.noteFlashlightOn(cameraId, oldUid); |
| 695 | } else { |
| 696 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 701 | broadcastTorchModeStatus(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 702 | } |
| 703 | |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 704 | static bool hasPermissionsForSystemCamera(int callingPid, int callingUid) { |
| 705 | permission::PermissionChecker permissionChecker; |
| 706 | AttributionSourceState attributionSource{}; |
| 707 | attributionSource.pid = callingPid; |
| 708 | attributionSource.uid = callingUid; |
| 709 | bool checkPermissionForSystemCamera = permissionChecker.checkPermissionForPreflight( |
| 710 | sSystemCameraPermission, attributionSource, String16(), AppOpsManager::OP_NONE) |
| 711 | != permission::PermissionChecker::PERMISSION_HARD_DENIED; |
| 712 | bool checkPermissionForCamera = permissionChecker.checkPermissionForPreflight( |
| 713 | sCameraPermission, attributionSource, String16(), AppOpsManager::OP_NONE) |
| 714 | != permission::PermissionChecker::PERMISSION_HARD_DENIED; |
| 715 | return checkPermissionForSystemCamera && checkPermissionForCamera; |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 718 | Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 719 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 720 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 721 | bool hasSystemCameraPermissions = |
| 722 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), |
| 723 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 724 | switch (type) { |
| 725 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 726 | if (hasSystemCameraPermissions) { |
| 727 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); |
| 728 | } else { |
| 729 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); |
| 730 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 731 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 732 | case CAMERA_TYPE_ALL: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 733 | if (hasSystemCameraPermissions) { |
| 734 | *numCameras = mNumberOfCameras; |
| 735 | } else { |
| 736 | *numCameras = mNumberOfCamerasWithoutSystemCamera; |
| 737 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 738 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 739 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 740 | ALOGW("%s: Unknown camera type %d", |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 741 | __FUNCTION__, type); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 742 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 743 | "Unknown camera type %d", type); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 744 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 745 | return Status::ok(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 748 | Status CameraService::getCameraInfo(int cameraId, bool overrideToPortrait, |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 749 | CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 750 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 751 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 752 | std::string cameraIdStr = cameraIdIntToStrLocked(cameraId); |
| 753 | if (shouldRejectSystemCameraConnection(String8(cameraIdStr.c_str()))) { |
| 754 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 755 | "characteristics for system only device %s: ", cameraIdStr.c_str()); |
| 756 | } |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 757 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 758 | if (!mInitialized) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 759 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 760 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 761 | "Camera subsystem is not available"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 762 | } |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 763 | bool hasSystemCameraPermissions = |
| 764 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), |
| 765 | CameraThreadState::getCallingUid()); |
| 766 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; |
| 767 | if (hasSystemCameraPermissions) { |
| 768 | cameraIdBound = mNumberOfCameras; |
| 769 | } |
| 770 | if (cameraId < 0 || cameraId >= cameraIdBound) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 771 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 772 | "CameraId is not valid"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 775 | Status ret = Status::ok(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 776 | int portraitRotation; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 777 | status_t err = mCameraProviderManager->getCameraInfo( |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 778 | cameraIdStr.c_str(), overrideToPortrait, &portraitRotation, cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 779 | if (err != OK) { |
| 780 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 781 | "Error retrieving camera info from device %d: %s (%d)", cameraId, |
| 782 | strerror(-err), err); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 783 | logServiceError(String8::format("Error retrieving camera info from device %d",cameraId), |
| 784 | ERROR_INVALID_OPERATION); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 785 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 786 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 787 | return ret; |
| 788 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 789 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 790 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 791 | const std::vector<std::string> *deviceIds = &mNormalDeviceIdsWithoutSystemCamera; |
| 792 | auto callingPid = CameraThreadState::getCallingPid(); |
| 793 | auto callingUid = CameraThreadState::getCallingUid(); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 794 | permission::PermissionChecker permissionChecker; |
| 795 | AttributionSourceState attributionSource{}; |
| 796 | attributionSource.pid = callingPid; |
| 797 | attributionSource.uid = callingUid; |
| 798 | bool checkPermissionForSystemCamera = permissionChecker.checkPermissionForPreflight( |
| 799 | sSystemCameraPermission, attributionSource, String16(), AppOpsManager::OP_NONE) |
| 800 | != permission::PermissionChecker::PERMISSION_HARD_DENIED; |
| 801 | if (checkPermissionForSystemCamera || getpid() == callingPid) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 802 | deviceIds = &mNormalDeviceIds; |
| 803 | } |
| 804 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(deviceIds->size())) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 805 | ALOGE("%s: input id %d invalid: valid range (0, %zu)", |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 806 | __FUNCTION__, cameraIdInt, deviceIds->size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 807 | return std::string{}; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 808 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 809 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 810 | return (*deviceIds)[cameraIdInt]; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | String8 CameraService::cameraIdIntToStr(int cameraIdInt) { |
| 814 | Mutex::Autolock lock(mServiceLock); |
| 815 | return String8(cameraIdIntToStrLocked(cameraIdInt).c_str()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | Status CameraService::getCameraCharacteristics(const String16& cameraId, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 819 | int targetSdkVersion, bool overrideToPortrait, CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 820 | ATRACE_CALL(); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 821 | if (!cameraInfo) { |
| 822 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 823 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 826 | if (!mInitialized) { |
| 827 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 828 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 829 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 830 | "Camera subsystem is not available");; |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 833 | if (shouldRejectSystemCameraConnection(String8(cameraId))) { |
| 834 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 835 | "characteristics for system only device %s: ", String8(cameraId).string()); |
| 836 | } |
| 837 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 838 | Status ret{}; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 839 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 840 | |
| 841 | std::string cameraIdStr = String8(cameraId).string(); |
| 842 | bool overrideForPerfClass = |
| 843 | SessionConfigurationUtils::targetPerfClassPrimaryCamera(mPerfClassPrimaryCameraIds, |
| 844 | cameraIdStr, targetSdkVersion); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 845 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 846 | cameraIdStr, overrideForPerfClass, cameraInfo, overrideToPortrait); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 847 | if (res != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 848 | if (res == NAME_NOT_FOUND) { |
| 849 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " |
| 850 | "characteristics for unknown device %s: %s (%d)", String8(cameraId).string(), |
| 851 | strerror(-res), res); |
| 852 | } else { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 853 | logServiceError(String8::format("Unable to retrieve camera characteristics for " |
| 854 | "device %s.", String8(cameraId).string()),ERROR_INVALID_OPERATION); |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 855 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " |
| 856 | "characteristics for device %s: %s (%d)", String8(cameraId).string(), |
| 857 | strerror(-res), res); |
| 858 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 859 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 860 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 861 | if (getSystemCameraKind(String8(cameraId), &deviceKind) != OK) { |
| 862 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, String8(cameraId).string()); |
| 863 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera kind " |
| 864 | "for device %s", String8(cameraId).string()); |
| 865 | } |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 866 | int callingPid = CameraThreadState::getCallingPid(); |
| 867 | int callingUid = CameraThreadState::getCallingUid(); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 868 | std::vector<int32_t> tagsRemoved; |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 869 | // If it's not calling from cameraserver, check the permission only if |
| 870 | // android.permission.CAMERA is required. If android.permission.SYSTEM_CAMERA was needed, |
| 871 | // it would've already been checked in shouldRejectSystemCameraConnection. |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 872 | permission::PermissionChecker permissionChecker; |
| 873 | AttributionSourceState attributionSource{}; |
| 874 | attributionSource.pid = callingPid; |
| 875 | attributionSource.uid = callingUid; |
| 876 | bool checkPermissionForCamera = permissionChecker.checkPermissionForPreflight( |
| 877 | sCameraPermission, attributionSource, String16(), AppOpsManager::OP_NONE) |
| 878 | != permission::PermissionChecker::PERMISSION_HARD_DENIED; |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 879 | if ((callingPid != getpid()) && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 880 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 881 | !checkPermissionForCamera) { |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 882 | res = cameraInfo->removePermissionEntries( |
| 883 | mCameraProviderManager->getProviderTagIdLocked(String8(cameraId).string()), |
| 884 | &tagsRemoved); |
| 885 | if (res != OK) { |
| 886 | cameraInfo->clear(); |
| 887 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to remove camera" |
| 888 | " characteristics needing camera permission for device %s: %s (%d)", |
| 889 | String8(cameraId).string(), strerror(-res), res); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | if (!tagsRemoved.empty()) { |
| 894 | res = cameraInfo->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, |
| 895 | tagsRemoved.data(), tagsRemoved.size()); |
| 896 | if (res != OK) { |
| 897 | cameraInfo->clear(); |
| 898 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to insert camera " |
| 899 | "keys needing permission for device %s: %s (%d)", String8(cameraId).string(), |
| 900 | strerror(-res), res); |
| 901 | } |
| 902 | } |
| 903 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 904 | return ret; |
| 905 | } |
| 906 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 907 | Status CameraService::getTorchStrengthLevel(const String16& cameraId, |
| 908 | int32_t* torchStrength) { |
| 909 | ATRACE_CALL(); |
| 910 | Mutex::Autolock l(mServiceLock); |
| 911 | if (!mInitialized) { |
| 912 | ALOGE("%s: Camera HAL couldn't be initialized.", __FUNCTION__); |
| 913 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera HAL couldn't be initialized."); |
| 914 | } |
| 915 | |
| 916 | if(torchStrength == NULL) { |
| 917 | ALOGE("%s: strength level must not be null.", __FUNCTION__); |
| 918 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Strength level should not be null."); |
| 919 | } |
| 920 | |
| 921 | status_t res = mCameraProviderManager->getTorchStrengthLevel(String8(cameraId).string(), |
| 922 | torchStrength); |
| 923 | if (res != OK) { |
| 924 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve torch " |
| 925 | "strength level for device %s: %s (%d)", String8(cameraId).string(), |
| 926 | strerror(-res), res); |
| 927 | } |
| 928 | ALOGI("%s: Torch strength level is: %d", __FUNCTION__, *torchStrength); |
| 929 | return Status::ok(); |
| 930 | } |
| 931 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 932 | String8 CameraService::getFormattedCurrentTime() { |
| 933 | time_t now = time(nullptr); |
| 934 | char formattedTime[64]; |
| 935 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
| 936 | return String8(formattedTime); |
| 937 | } |
| 938 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 939 | Status CameraService::getCameraVendorTagDescriptor( |
| 940 | /*out*/ |
| 941 | hardware::camera2::params::VendorTagDescriptor* desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 942 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 943 | if (!mInitialized) { |
| 944 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 945 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 946 | } |
Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 947 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 948 | if (globalDescriptor != nullptr) { |
| 949 | *desc = *(globalDescriptor.get()); |
| 950 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 951 | return Status::ok(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 952 | } |
| 953 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 954 | Status CameraService::getCameraVendorTagCache( |
| 955 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { |
| 956 | ATRACE_CALL(); |
| 957 | if (!mInitialized) { |
| 958 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 959 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 960 | "Camera subsystem not available"); |
| 961 | } |
| 962 | sp<VendorTagDescriptorCache> globalCache = |
| 963 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 964 | if (globalCache != nullptr) { |
| 965 | *cache = *(globalCache.get()); |
| 966 | } |
| 967 | return Status::ok(); |
| 968 | } |
| 969 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 970 | void CameraService::clearCachedVariables() { |
| 971 | BasicClient::BasicClient::sCameraService = nullptr; |
| 972 | } |
| 973 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 974 | std::pair<int, IPCTransport> CameraService::getDeviceVersion(const String8& cameraId, |
| 975 | bool overrideToPortrait, int* portraitRotation, int* facing, int* orientation) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 976 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 977 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 978 | int deviceVersion = 0; |
| 979 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 980 | status_t res; |
| 981 | hardware::hidl_version maxVersion{0,0}; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 982 | IPCTransport transport = IPCTransport::INVALID; |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 983 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(), |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 984 | &maxVersion, &transport); |
| 985 | if (res != OK || transport == IPCTransport::INVALID) { |
| 986 | ALOGE("%s: Unable to get highest supported version for camera id %s", __FUNCTION__, |
| 987 | cameraId.string()); |
| 988 | return std::make_pair(-1, IPCTransport::INVALID) ; |
| 989 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 990 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 991 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 992 | hardware::CameraInfo info; |
| 993 | if (facing) { |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 994 | res = mCameraProviderManager->getCameraInfo(cameraId.string(), overrideToPortrait, |
| 995 | portraitRotation, &info); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 996 | if (res != OK) { |
| 997 | return std::make_pair(-1, IPCTransport::INVALID); |
| 998 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 999 | *facing = info.facing; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1000 | if (orientation) { |
| 1001 | *orientation = info.orientation; |
| 1002 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1003 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 1004 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1005 | return std::make_pair(deviceVersion, transport); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1008 | Status CameraService::filterGetInfoErrorCode(status_t err) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1009 | switch(err) { |
| 1010 | case NO_ERROR: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1011 | return Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1012 | case BAD_VALUE: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1013 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1014 | "CameraId is not valid for HAL module"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1015 | case NO_INIT: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1016 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 1017 | "Camera device not available"); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1018 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1019 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1020 | "Camera HAL encountered error %d: %s", |
| 1021 | err, strerror(-err)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1022 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1025 | Status CameraService::makeClient(const sp<CameraService>& cameraService, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1026 | const sp<IInterface>& cameraCb, const String16& packageName, bool systemNativeClient, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1027 | const std::optional<String16>& featureId, const String8& cameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 1028 | int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid, |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1029 | int servicePid, std::pair<int, IPCTransport> deviceVersionAndTransport, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1030 | apiLevel effectiveApiLevel, bool overrideForPerfClass, bool overrideToPortrait, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1031 | bool forceSlowJpegMode, /*out*/sp<BasicClient>* client) { |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1032 | // For HIDL devices |
| 1033 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 1034 | // Create CameraClient based on device version reported by the HAL. |
| 1035 | int deviceVersion = deviceVersionAndTransport.first; |
| 1036 | switch(deviceVersion) { |
| 1037 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 1038 | ALOGE("Camera using old HAL version: %d", deviceVersion); |
| 1039 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, |
| 1040 | "Camera device \"%s\" HAL version %d no longer supported", |
| 1041 | cameraId.string(), deviceVersion); |
| 1042 | break; |
| 1043 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 1044 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 1045 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 1046 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 1047 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 1048 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 1049 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 1050 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 1051 | break; |
| 1052 | default: |
| 1053 | // Should not be reachable |
| 1054 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 1055 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1056 | "Camera device \"%s\" has unknown HAL version %d", |
| 1057 | cameraId.string(), deviceVersion); |
| 1058 | } |
| 1059 | } |
| 1060 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 1061 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1062 | *client = new Camera2Client(cameraService, tmp, cameraService->mCameraServiceProxyWrapper, |
| 1063 | packageName, featureId, cameraId, api1CameraId, facing, sensorOrientation, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1064 | clientPid, clientUid, servicePid, overrideForPerfClass, overrideToPortrait, |
| 1065 | forceSlowJpegMode); |
| 1066 | ALOGI("%s: Camera1 API (legacy), override to portrait %d, forceSlowJpegMode %d", |
| 1067 | __FUNCTION__, overrideToPortrait, forceSlowJpegMode); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1068 | } else { // Camera2 API route |
| 1069 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = |
| 1070 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1071 | *client = new CameraDeviceClient(cameraService, tmp, |
| 1072 | cameraService->mCameraServiceProxyWrapper, packageName, systemNativeClient, |
| 1073 | featureId, cameraId, facing, sensorOrientation, clientPid, clientUid, servicePid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1074 | overrideForPerfClass, overrideToPortrait); |
| 1075 | ALOGI("%s: Camera2 API, override to portrait %d", __FUNCTION__, overrideToPortrait); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1076 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1077 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1080 | String8 CameraService::toString(std::set<userid_t> intSet) { |
| 1081 | String8 s(""); |
| 1082 | bool first = true; |
| 1083 | for (userid_t i : intSet) { |
| 1084 | if (first) { |
| 1085 | s.appendFormat("%d", i); |
| 1086 | first = false; |
| 1087 | } else { |
| 1088 | s.appendFormat(", %d", i); |
| 1089 | } |
| 1090 | } |
| 1091 | return s; |
| 1092 | } |
| 1093 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1094 | int32_t CameraService::mapToInterface(TorchModeStatus status) { |
| 1095 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1096 | switch (status) { |
| 1097 | case TorchModeStatus::NOT_AVAILABLE: |
| 1098 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1099 | break; |
| 1100 | case TorchModeStatus::AVAILABLE_OFF: |
| 1101 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 1102 | break; |
| 1103 | case TorchModeStatus::AVAILABLE_ON: |
| 1104 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
| 1105 | break; |
| 1106 | default: |
| 1107 | ALOGW("Unknown new flash status: %d", status); |
| 1108 | } |
| 1109 | return serviceStatus; |
| 1110 | } |
| 1111 | |
| 1112 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { |
| 1113 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; |
| 1114 | switch (status) { |
| 1115 | case CameraDeviceStatus::NOT_PRESENT: |
| 1116 | serviceStatus = StatusInternal::NOT_PRESENT; |
| 1117 | break; |
| 1118 | case CameraDeviceStatus::PRESENT: |
| 1119 | serviceStatus = StatusInternal::PRESENT; |
| 1120 | break; |
| 1121 | case CameraDeviceStatus::ENUMERATING: |
| 1122 | serviceStatus = StatusInternal::ENUMERATING; |
| 1123 | break; |
| 1124 | default: |
| 1125 | ALOGW("Unknown new HAL device status: %d", status); |
| 1126 | } |
| 1127 | return serviceStatus; |
| 1128 | } |
| 1129 | |
| 1130 | int32_t CameraService::mapToInterface(StatusInternal status) { |
| 1131 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1132 | switch (status) { |
| 1133 | case StatusInternal::NOT_PRESENT: |
| 1134 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1135 | break; |
| 1136 | case StatusInternal::PRESENT: |
| 1137 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; |
| 1138 | break; |
| 1139 | case StatusInternal::ENUMERATING: |
| 1140 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; |
| 1141 | break; |
| 1142 | case StatusInternal::NOT_AVAILABLE: |
| 1143 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; |
| 1144 | break; |
| 1145 | case StatusInternal::UNKNOWN: |
| 1146 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; |
| 1147 | break; |
| 1148 | default: |
| 1149 | ALOGW("Unknown new internal device status: %d", status); |
| 1150 | } |
| 1151 | return serviceStatus; |
| 1152 | } |
| 1153 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1154 | Status CameraService::initializeShimMetadata(int cameraId) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1155 | int uid = CameraThreadState::getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1156 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1157 | String16 internalPackageName("cameraserver"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1158 | String8 id = String8::format("%d", cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1159 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1160 | sp<Client> tmp = nullptr; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1161 | if (!(ret = connectHelper<ICameraClient,Client>( |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1162 | sp<ICameraClient>{nullptr}, id, cameraId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1163 | internalPackageName, /*systemNativeClient*/ false, {}, uid, USE_CALLING_PID, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1164 | API_1, /*shimUpdateOnly*/ true, /*oomScoreOffset*/ 0, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1165 | /*targetSdkVersion*/ __ANDROID_API_FUTURE__, /*overrideToPortrait*/ true, |
| 1166 | /*forceSlowJpegMode*/false, /*out*/ tmp) |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1167 | ).isOk()) { |
| 1168 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string()); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1169 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1170 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1173 | Status CameraService::getLegacyParametersLazy(int cameraId, |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1174 | /*out*/ |
| 1175 | CameraParameters* parameters) { |
| 1176 | |
| 1177 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 1178 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1179 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1180 | |
| 1181 | if (parameters == NULL) { |
| 1182 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1183 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1186 | String8 id = String8::format("%d", cameraId); |
| 1187 | |
| 1188 | // Check if we already have parameters |
| 1189 | { |
| 1190 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1191 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1192 | auto cameraState = getCameraState(id); |
| 1193 | if (cameraState == nullptr) { |
| 1194 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1195 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1196 | "Invalid camera ID: %s", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1197 | } |
| 1198 | CameraParameters p = cameraState->getShimParams(); |
| 1199 | if (!p.isEmpty()) { |
| 1200 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1201 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1205 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1206 | ret = initializeShimMetadata(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1207 | CameraThreadState::restoreCallingIdentity(token); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1208 | if (!ret.isOk()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1209 | // Error already logged by callee |
| 1210 | return ret; |
| 1211 | } |
| 1212 | |
| 1213 | // Check for parameters again |
| 1214 | { |
| 1215 | // Scope for service lock |
| 1216 | Mutex::Autolock lock(mServiceLock); |
| 1217 | auto cameraState = getCameraState(id); |
| 1218 | if (cameraState == nullptr) { |
| 1219 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1220 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1221 | "Invalid camera ID: %s", id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1222 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1223 | CameraParameters p = cameraState->getShimParams(); |
| 1224 | if (!p.isEmpty()) { |
| 1225 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1226 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1230 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 1231 | __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1232 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1235 | // Can camera service trust the caller based on the calling UID? |
| 1236 | static bool isTrustedCallingUid(uid_t uid) { |
| 1237 | switch (uid) { |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1238 | case AID_MEDIA: // mediaserver |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1239 | case AID_CAMERASERVER: // cameraserver |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1240 | case AID_RADIO: // telephony |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1241 | return true; |
| 1242 | default: |
| 1243 | return false; |
| 1244 | } |
| 1245 | } |
| 1246 | |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 1247 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { |
| 1248 | PermissionController pc; |
| 1249 | uid = pc.getPackageUid(packageName, 0); |
| 1250 | if (uid <= 0) { |
| 1251 | ALOGE("Unknown package: '%s'", String8(packageName).string()); |
| 1252 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).string()); |
| 1253 | return BAD_VALUE; |
| 1254 | } |
| 1255 | |
| 1256 | if (userId < 0) { |
| 1257 | ALOGE("Invalid user: %d", userId); |
| 1258 | dprintf(err, "Invalid user: %d\n", userId); |
| 1259 | return BAD_VALUE; |
| 1260 | } |
| 1261 | |
| 1262 | uid = multiuser_get_uid(userId, uid); |
| 1263 | return NO_ERROR; |
| 1264 | } |
| 1265 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1266 | Status CameraService::validateConnectLocked(const String8& cameraId, |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1267 | const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, |
| 1268 | /*out*/int& originalClientPid) const { |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1269 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1270 | #ifdef __BRILLO__ |
| 1271 | UNUSED(clientName8); |
| 1272 | UNUSED(clientUid); |
| 1273 | UNUSED(clientPid); |
| 1274 | UNUSED(originalClientPid); |
| 1275 | #else |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1276 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, |
| 1277 | originalClientPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1278 | if (!allowed.isOk()) { |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1279 | return allowed; |
| 1280 | } |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1281 | #endif // __BRILLO__ |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1282 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1283 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1284 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1285 | if (!mInitialized) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1286 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 1287 | callingPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1288 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1289 | "No camera HAL module available to open camera device \"%s\"", cameraId.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1292 | if (getCameraState(cameraId) == nullptr) { |
| 1293 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1294 | cameraId.string()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1295 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1296 | "No camera device with ID \"%s\" available", cameraId.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1299 | status_t err = checkIfDeviceIsUsable(cameraId); |
| 1300 | if (err != NO_ERROR) { |
| 1301 | switch(err) { |
| 1302 | case -ENODEV: |
| 1303 | case -EBUSY: |
| 1304 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1305 | "No camera device with ID \"%s\" currently available", cameraId.string()); |
| 1306 | default: |
| 1307 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1308 | "Unknown error connecting to ID \"%s\"", cameraId.string()); |
| 1309 | } |
| 1310 | } |
| 1311 | return Status::ok(); |
Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1312 | } |
| 1313 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1314 | Status CameraService::validateClientPermissionsLocked(const String8& cameraId, |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1315 | const String8& clientName8, int& clientUid, int& clientPid, |
| 1316 | /*out*/int& originalClientPid) const { |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1317 | permission::PermissionChecker permissionChecker; |
| 1318 | AttributionSourceState attributionSource{}; |
| 1319 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1320 | int callingPid = CameraThreadState::getCallingPid(); |
| 1321 | int callingUid = CameraThreadState::getCallingUid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1322 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1323 | // Check if we can trust clientUid |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1324 | if (clientUid == USE_CALLING_UID) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1325 | clientUid = callingUid; |
| 1326 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1327 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1328 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1329 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1330 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1331 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1332 | callingPid, callingUid, cameraId.string(), |
| 1333 | clientName8.string(), clientUid, clientPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1336 | // Check if we can trust clientPid |
| 1337 | if (clientPid == USE_CALLING_PID) { |
| 1338 | clientPid = callingPid; |
| 1339 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1340 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1341 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1342 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1343 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1344 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1345 | callingPid, callingUid, cameraId.string(), |
| 1346 | clientName8.string(), clientUid, clientPid); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1347 | } |
| 1348 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1349 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1350 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", |
| 1351 | cameraId.c_str()); |
| 1352 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, "No camera device with ID \"%s\" is" |
| 1353 | "available", cameraId.string()); |
| 1354 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1355 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1356 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 1357 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 1358 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "No camera device with ID \"%s\"" |
| 1359 | "found while trying to query device kind", cameraId.string()); |
| 1360 | |
| 1361 | } |
| 1362 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1363 | // If it's not calling from cameraserver, check the permission if the |
| 1364 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for |
| 1365 | // android.permission.SYSTEM_CAMERA for system only camera devices). |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1366 | attributionSource.pid = clientPid; |
| 1367 | attributionSource.uid = clientUid; |
| 1368 | attributionSource.packageName = clientName8; |
| 1369 | bool checkPermissionForCamera = permissionChecker.checkPermissionForPreflight( |
| 1370 | sCameraPermission, attributionSource, String16(), AppOpsManager::OP_NONE) |
| 1371 | != permission::PermissionChecker::PERMISSION_HARD_DENIED; |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1372 | if (callingPid != getpid() && |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1373 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && !checkPermissionForCamera) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1374 | 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] | 1375 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1376 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission", |
| 1377 | clientName8.string(), clientUid, clientPid, cameraId.string()); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1378 | } |
| 1379 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1380 | // 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] | 1381 | if (!mUidPolicy->isUidActive(callingUid, String16(clientName8))) { |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1382 | int32_t procState = mUidPolicy->getProcState(callingUid); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1383 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", |
| 1384 | clientPid, clientUid); |
| 1385 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1386 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" |
| 1387 | "calling UID %d proc state %" PRId32 ")", |
| 1388 | clientName8.string(), clientUid, clientPid, cameraId.string(), |
| 1389 | callingUid, procState); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1390 | } |
| 1391 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1392 | // If sensor privacy is enabled then prevent access to the camera |
| 1393 | if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
| 1394 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); |
| 1395 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1396 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" when sensor privacy " |
| 1397 | "is enabled", clientName8.string(), clientUid, clientPid, cameraId.string()); |
| 1398 | } |
| 1399 | |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1400 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's |
| 1401 | // connected to camera service directly. |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1402 | originalClientPid = clientPid; |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1403 | clientPid = callingPid; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1404 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1405 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1406 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1407 | // For non-system clients : Only allow clients who are being used by the current foreground |
| 1408 | // device user, unless calling from our own process. |
| 1409 | if (!doesClientHaveSystemUid() && callingPid != getpid() && |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1410 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1411 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 1412 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
| 1413 | toString(mAllowedUsers).string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1414 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1415 | "Callers from device user %d are not currently allowed to connect to camera \"%s\"", |
| 1416 | clientUserId, cameraId.string()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1419 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const { |
| 1423 | auto cameraState = getCameraState(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1424 | int callingPid = CameraThreadState::getCallingPid(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1425 | if (cameraState == nullptr) { |
| 1426 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1427 | cameraId.string()); |
| 1428 | return -ENODEV; |
| 1429 | } |
| 1430 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1431 | StatusInternal currentStatus = cameraState->getStatus(); |
| 1432 | if (currentStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1433 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
| 1434 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1435 | return -ENODEV; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1436 | } else if (currentStatus == StatusInternal::ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1437 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
| 1438 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1439 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1440 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1441 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1442 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1443 | } |
| 1444 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1445 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1446 | const CameraService::DescriptorPtr& desc, int oomScoreOffset, bool systemNativeClient) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1447 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1448 | // Make a descriptor for the incoming client |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1449 | auto clientDescriptor = |
| 1450 | CameraService::CameraClientManager::makeClientDescriptor(client, desc, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1451 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1452 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 1453 | |
| 1454 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
| 1455 | String8(client->getPackageName())); |
| 1456 | |
| 1457 | if (evicted.size() > 0) { |
| 1458 | // This should never happen - clients should already have been removed in disconnect |
| 1459 | for (auto& i : evicted) { |
| 1460 | ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect", |
| 1461 | __FUNCTION__, i->getKey().string()); |
| 1462 | } |
| 1463 | |
| 1464 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 1465 | __FUNCTION__); |
| 1466 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1467 | |
| 1468 | // And register a death notification for the client callback. Do |
| 1469 | // this last to avoid Binder policy where a nested Binder |
| 1470 | // transaction might be pre-empted to service the client death |
| 1471 | // notification if the client process dies before linkToDeath is |
| 1472 | // invoked. |
| 1473 | sp<IBinder> remoteCallback = client->getRemote(); |
| 1474 | if (remoteCallback != nullptr) { |
| 1475 | remoteCallback->linkToDeath(this); |
| 1476 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid, |
| 1480 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1481 | int oomScoreOffset, bool systemNativeClient, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1482 | /*out*/ |
| 1483 | sp<BasicClient>* client, |
| 1484 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1485 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1486 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1487 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1488 | DescriptorPtr clientDescriptor; |
| 1489 | { |
| 1490 | if (effectiveApiLevel == API_1) { |
| 1491 | // If we are using API1, any existing client for this camera ID with the same remote |
| 1492 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 1493 | |
| 1494 | auto current = mActiveClientManager.get(cameraId); |
| 1495 | if (current != nullptr) { |
| 1496 | auto clientSp = current->getValue(); |
| 1497 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1498 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
Shuzhen Wang | b2d43f6 | 2021-08-25 14:01:11 -0700 | [diff] [blame] | 1499 | ALOGW("CameraService connect called with a different" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1500 | " API level, evicting prior client..."); |
| 1501 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1502 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1503 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1504 | *client = clientSp; |
| 1505 | return NO_ERROR; |
| 1506 | } |
| 1507 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1508 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1509 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 1510 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1511 | // Get current active client PIDs |
| 1512 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 1513 | ownerPids.push_back(clientPid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1514 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1515 | std::vector<int> priorityScores(ownerPids.size()); |
| 1516 | std::vector<int> states(ownerPids.size()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1517 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1518 | // Get priority scores of all active PIDs |
| 1519 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids( |
| 1520 | ownerPids.size(), &ownerPids[0], /*out*/&states[0], |
| 1521 | /*out*/&priorityScores[0]); |
| 1522 | if (err != OK) { |
| 1523 | ALOGE("%s: Priority score query failed: %d", |
| 1524 | __FUNCTION__, err); |
| 1525 | return err; |
| 1526 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1527 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1528 | // Update all active clients' priorities |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1529 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1530 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1531 | pidToPriorityMap.emplace(ownerPids[i], |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 1532 | resource_policy::ClientPriority(priorityScores[i], states[i], |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1533 | /* isVendorClient won't get copied over*/ false, |
| 1534 | /* oomScoreOffset won't get copied over*/ 0)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1535 | } |
| 1536 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1537 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1538 | // Get state for the given cameraId |
| 1539 | auto state = getCameraState(cameraId); |
| 1540 | if (state == nullptr) { |
| 1541 | ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", |
| 1542 | clientPid, cameraId.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1543 | // Should never get here because validateConnectLocked should have errored out |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1544 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1545 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1546 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1547 | int32_t actualScore = priorityScores[priorityScores.size() - 1]; |
| 1548 | int32_t actualState = states[states.size() - 1]; |
| 1549 | |
| 1550 | // Make descriptor for incoming client. We store the oomScoreOffset |
| 1551 | // since we might need it later on new handleEvictionsLocked and |
| 1552 | // ProcessInfoService would not take that into account. |
Shuzhen Wang | 2db86ff | 2018-04-25 01:11:17 +0000 | [diff] [blame] | 1553 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1554 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1555 | state->getConflicting(), actualScore, clientPid, actualState, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1556 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1557 | |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1558 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); |
| 1559 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1560 | // Find clients that would be evicted |
| 1561 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 1562 | |
| 1563 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 1564 | // background, so we cannot do evictions |
| 1565 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 1566 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 1567 | " priority).", clientPid); |
| 1568 | |
| 1569 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1570 | String8 curTime = getFormattedCurrentTime(); |
| 1571 | auto incompatibleClients = |
| 1572 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 1573 | |
| 1574 | 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] | 1575 | "(PID %d, score %d state %d) due to eviction policy", curTime.string(), |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1576 | cameraId.string(), packageName.string(), clientPid, |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1577 | clientPriority.getScore(), clientPriority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1578 | |
| 1579 | for (auto& i : incompatibleClients) { |
| 1580 | msg.appendFormat("\n - Blocked by existing device %s client for package %s" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1581 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", |
| 1582 | i->getKey().string(), |
| 1583 | String8{i->getValue()->getPackageName()}.string(), |
| 1584 | i->getOwnerId(), i->getPriority().getScore(), |
| 1585 | i->getPriority().getState()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1586 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1587 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(), |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1588 | String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1589 | i->getPriority().getScore(), i->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1593 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1594 | mEventLog.add(msg); |
| 1595 | |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1596 | auto current = mActiveClientManager.get(cameraId); |
| 1597 | if (current != nullptr) { |
| 1598 | return -EBUSY; // CAMERA_IN_USE |
| 1599 | } else { |
| 1600 | return -EUSERS; // MAX_CAMERAS_IN_USE |
| 1601 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | for (auto& i : evicted) { |
| 1605 | sp<BasicClient> clientSp = i->getValue(); |
| 1606 | if (clientSp.get() == nullptr) { |
| 1607 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 1608 | |
| 1609 | // TODO: Remove this |
| 1610 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 1611 | __FUNCTION__); |
| 1612 | mActiveClientManager.remove(i); |
| 1613 | continue; |
| 1614 | } |
| 1615 | |
| 1616 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
| 1617 | i->getKey().string()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1618 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1619 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1620 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1621 | logEvent(String8::format("EVICT device %s client held by package %s (PID" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1622 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" |
| 1623 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1624 | i->getKey().string(), String8{clientSp->getPackageName()}.string(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1625 | i->getOwnerId(), i->getPriority().getScore(), |
| 1626 | i->getPriority().getState(), cameraId.string(), |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1627 | packageName.string(), clientPid, clientPriority.getScore(), |
| 1628 | clientPriority.getState())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1629 | |
| 1630 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1631 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1632 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1633 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1636 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 1637 | // other clients from connecting in mServiceLockWrapper if held |
| 1638 | mServiceLock.unlock(); |
| 1639 | |
| 1640 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1641 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1642 | |
| 1643 | // Destroy evicted clients |
| 1644 | for (auto& i : evictedClients) { |
| 1645 | // 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] | 1646 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1647 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1648 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1649 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1650 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1651 | for (const auto& i : evictedClients) { |
| 1652 | ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")", |
| 1653 | __FUNCTION__, i->getKey().string(), i->getOwnerId()); |
| 1654 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 1655 | if (ret == TIMED_OUT) { |
| 1656 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
| 1657 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), |
| 1658 | mActiveClientManager.toString().string()); |
| 1659 | return -EBUSY; |
| 1660 | } |
| 1661 | if (ret != NO_ERROR) { |
| 1662 | ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), " |
| 1663 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret), |
| 1664 | ret, mActiveClientManager.toString().string()); |
| 1665 | return ret; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | evictedClients.clear(); |
| 1670 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1671 | // Once clients have been disconnected, relock |
| 1672 | mServiceLock.lock(); |
| 1673 | |
| 1674 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 1675 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 1676 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1677 | } |
| 1678 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1679 | *partial = clientDescriptor; |
| 1680 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1683 | Status CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1684 | const sp<ICameraClient>& cameraClient, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1685 | int api1CameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1686 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1687 | int clientUid, |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1688 | int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1689 | int targetSdkVersion, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1690 | bool overrideToPortrait, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1691 | bool forceSlowJpegMode, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1692 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1693 | sp<ICamera>* device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1694 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1695 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1696 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1697 | |
| 1698 | String8 id = cameraIdIntToStr(api1CameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1699 | sp<Client> client = nullptr; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1700 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1701 | clientPackageName,/*systemNativeClient*/ false, {}, clientUid, clientPid, API_1, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1702 | /*shimUpdateOnly*/ false, /*oomScoreOffset*/ 0, targetSdkVersion, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1703 | overrideToPortrait, forceSlowJpegMode, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1704 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1705 | if(!ret.isOk()) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1706 | logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageName), |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1707 | ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1708 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1711 | *device = client; |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 1712 | |
| 1713 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 1714 | const auto& mActivityManager = getActivityManager(); |
| 1715 | if (mActivityManager) { |
| 1716 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
| 1717 | CameraThreadState::getCallingUid(), |
| 1718 | CameraThreadState::getCallingPid()); |
| 1719 | } |
| 1720 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1721 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1724 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, |
| 1725 | bool isVendorListener, int clientPid, int clientUid) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1726 | // If the client is not a vendor client, don't add listener if |
| 1727 | // a) the camera is a publicly hidden secure camera OR |
| 1728 | // b) the camera is a system only camera and the client doesn't |
| 1729 | // have android.permission.SYSTEM_CAMERA permissions. |
| 1730 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || |
| 1731 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
| 1732 | !hasPermissionsForSystemCamera(clientPid, clientUid)))) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 1733 | return true; |
| 1734 | } |
| 1735 | return false; |
| 1736 | } |
| 1737 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1738 | bool CameraService::shouldRejectSystemCameraConnection(const String8& cameraId) const { |
| 1739 | // Rules for rejection: |
| 1740 | // 1) If cameraserver tries to access this camera device, accept the |
| 1741 | // connection. |
| 1742 | // 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] | 1743 | // non system component is trying to access it. |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1744 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY |
| 1745 | // and the serving thread is a non hwbinder thread, the client must have |
| 1746 | // android.permission.SYSTEM_CAMERA permissions to connect. |
| 1747 | |
| 1748 | int cPid = CameraThreadState::getCallingPid(); |
| 1749 | int cUid = CameraThreadState::getCallingUid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1750 | bool systemClient = doesClientHaveSystemUid(); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1751 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 1752 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1753 | // This isn't a known camera ID, so it's not a system camera |
| 1754 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); |
| 1755 | return false; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1756 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1757 | |
| 1758 | // (1) Cameraserver trying to connect, accept. |
| 1759 | if (CameraThreadState::getCallingPid() == getpid()) { |
| 1760 | return false; |
| 1761 | } |
| 1762 | // (2) |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1763 | if (!systemClient && systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1764 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); |
| 1765 | return true; |
| 1766 | } |
| 1767 | // (3) Here we only check for permissions if it is a system only camera device. This is since |
| 1768 | // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some |
| 1769 | // characteristics) even if clients don't have android.permission.CAMERA. We do not want the |
| 1770 | // same behavior for system camera devices. |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1771 | if (!systemClient && systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 1772 | !hasPermissionsForSystemCamera(cPid, cUid)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1773 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", |
| 1774 | cameraId.c_str()); |
| 1775 | return true; |
| 1776 | } |
| 1777 | |
| 1778 | return false; |
| 1779 | } |
| 1780 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1781 | Status CameraService::connectDevice( |
| 1782 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1783 | const String16& cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1784 | const String16& clientPackageName, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1785 | const std::optional<String16>& clientFeatureId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1786 | int clientUid, int oomScoreOffset, int targetSdkVersion, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1787 | bool overrideToPortrait, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1788 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1789 | sp<hardware::camera2::ICameraDeviceUser>* device) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -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(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1793 | String8 id = String8(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1794 | sp<CameraDeviceClient> client = nullptr; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1795 | String16 clientPackageNameAdj = clientPackageName; |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1796 | int callingPid = CameraThreadState::getCallingPid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1797 | bool systemNativeClient = false; |
| 1798 | if (doesClientHaveSystemUid() && (clientPackageNameAdj.size() == 0)) { |
| 1799 | std::string systemClient = |
| 1800 | StringPrintf("client.pid<%d>", CameraThreadState::getCallingPid()); |
| 1801 | clientPackageNameAdj = String16(systemClient.c_str()); |
| 1802 | systemNativeClient = true; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1803 | } |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1804 | |
| 1805 | if (oomScoreOffset < 0) { |
| 1806 | String8 msg = |
| 1807 | String8::format("Cannot increase the priority of a client %s pid %d for " |
| 1808 | "camera id %s", String8(clientPackageNameAdj).string(), callingPid, |
| 1809 | id.string()); |
| 1810 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1811 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 1812 | } |
| 1813 | |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 1814 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 1815 | int callingUid = CameraThreadState::getCallingUid(); |
| 1816 | if (clientUid == USE_CALLING_UID) { |
| 1817 | clientUserId = multiuser_get_user_id(callingUid); |
| 1818 | } |
| 1819 | |
| 1820 | if (mCameraServiceProxyWrapper->isCameraDisabled(clientUserId)) { |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 1821 | String8 msg = |
| 1822 | String8::format("Camera disabled by device policy"); |
| 1823 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1824 | return STATUS_ERROR(ERROR_DISABLED, msg.string()); |
| 1825 | } |
| 1826 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1827 | // enforce system camera permissions |
| 1828 | if (oomScoreOffset > 0 && |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 1829 | !hasPermissionsForSystemCamera(callingPid, CameraThreadState::getCallingUid()) && |
| 1830 | !isTrustedCallingUid(CameraThreadState::getCallingUid())) { |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1831 | String8 msg = |
| 1832 | String8::format("Cannot change the priority of a client %s pid %d for " |
| 1833 | "camera id %s without SYSTEM_CAMERA permissions", |
| 1834 | String8(clientPackageNameAdj).string(), callingPid, id.string()); |
| 1835 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1836 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, msg.string()); |
| 1837 | } |
| 1838 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1839 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1840 | /*api1CameraId*/-1, clientPackageNameAdj, systemNativeClient,clientFeatureId, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1841 | clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, oomScoreOffset, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1842 | targetSdkVersion, overrideToPortrait, /*forceSlowJpegMode*/false, |
| 1843 | /*out*/client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1844 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1845 | if(!ret.isOk()) { |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1846 | logRejected(id, callingPid, String8(clientPackageNameAdj), ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1847 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1848 | } |
| 1849 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1850 | *device = client; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 1851 | Mutex::Autolock lock(mServiceLock); |
| 1852 | |
| 1853 | // Clear the previous cached logs and reposition the |
| 1854 | // file offset to beginning of the file to log new data. |
| 1855 | // If either truncate or lseek fails, close the previous file and create a new one. |
| 1856 | if ((ftruncate(mMemFd, 0) == -1) || (lseek(mMemFd, 0, SEEK_SET) == -1)) { |
| 1857 | ALOGE("%s: Error while truncating the file: %s", __FUNCTION__, sFileName); |
| 1858 | // Close the previous memfd. |
| 1859 | close(mMemFd); |
| 1860 | // If failure to wipe the data, then create a new file and |
| 1861 | // assign the new value to mMemFd. |
| 1862 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 1863 | if (mMemFd == -1) { |
| 1864 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 1865 | } |
| 1866 | } |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 1867 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 1868 | const auto& mActivityManager = getActivityManager(); |
| 1869 | if (mActivityManager) { |
| 1870 | mActivityManager->logFgsApiBegin(LOG_FGS_CAMERA_API, |
| 1871 | CameraThreadState::getCallingUid(), |
| 1872 | CameraThreadState::getCallingPid()); |
| 1873 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1874 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1875 | } |
| 1876 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1877 | String16 CameraService::getPackageNameFromUid(int clientUid) { |
| 1878 | String16 packageName(""); |
| 1879 | |
| 1880 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1881 | sp<IBinder> binder = sm->getService(String16(kPermissionServiceName)); |
| 1882 | if (binder == 0) { |
| 1883 | ALOGE("Cannot get permission service"); |
| 1884 | // Return empty package name and the further interaction |
| 1885 | // with camera will likely fail |
| 1886 | return packageName; |
| 1887 | } |
| 1888 | |
| 1889 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 1890 | Vector<String16> packages; |
| 1891 | |
| 1892 | permCtrl->getPackagesForUid(clientUid, packages); |
| 1893 | |
| 1894 | if (packages.isEmpty()) { |
| 1895 | ALOGE("No packages for calling UID %d", clientUid); |
| 1896 | // Return empty package name and the further interaction |
| 1897 | // with camera will likely fail |
| 1898 | return packageName; |
| 1899 | } |
| 1900 | |
| 1901 | // Arbitrarily pick the first name in the list |
| 1902 | packageName = packages[0]; |
| 1903 | |
| 1904 | return packageName; |
| 1905 | } |
| 1906 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1907 | template<class CALLBACK, class CLIENT> |
| 1908 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1909 | int api1CameraId, const String16& clientPackageNameMaybe, bool systemNativeClient, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1910 | const std::optional<String16>& clientFeatureId, int clientUid, int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1911 | apiLevel effectiveApiLevel, bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 1912 | bool overrideToPortrait, bool forceSlowJpegMode, |
| 1913 | /*out*/sp<CLIENT>& device) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1914 | binder::Status ret = binder::Status::ok(); |
| 1915 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1916 | bool isNonSystemNdk = false; |
| 1917 | String16 clientPackageName; |
| 1918 | if (clientPackageNameMaybe.size() <= 0) { |
| 1919 | // NDK calls don't come with package names, but we need one for various cases. |
| 1920 | // Generally, there's a 1:1 mapping between UID and package name, but shared UIDs |
| 1921 | // do exist. For all authentication cases, all packages under the same UID get the |
| 1922 | // same permissions, so picking any associated package name is sufficient. For some |
| 1923 | // other cases, this may give inaccurate names for clients in logs. |
| 1924 | isNonSystemNdk = true; |
| 1925 | int packageUid = (clientUid == USE_CALLING_UID) ? |
| 1926 | CameraThreadState::getCallingUid() : clientUid; |
| 1927 | clientPackageName = getPackageNameFromUid(packageUid); |
| 1928 | } else { |
| 1929 | clientPackageName = clientPackageNameMaybe; |
| 1930 | } |
| 1931 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1932 | String8 clientName8(clientPackageName); |
| 1933 | |
| 1934 | int originalClientPid = 0; |
| 1935 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1936 | int packagePid = (clientPid == USE_CALLING_PID) ? |
| 1937 | CameraThreadState::getCallingPid() : clientPid; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1938 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1939 | "Camera API version %d", packagePid, clientName8.string(), cameraId.string(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1940 | static_cast<int>(effectiveApiLevel)); |
| 1941 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1942 | nsecs_t openTimeNs = systemTime(); |
| 1943 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1944 | sp<CLIENT> client = nullptr; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1945 | int facing = -1; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1946 | int orientation = 0; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1947 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1948 | { |
| 1949 | // Acquire mServiceLock and prevent other clients from connecting |
| 1950 | std::unique_ptr<AutoConditionLock> lock = |
| 1951 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 1952 | |
| 1953 | if (lock == nullptr) { |
| 1954 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." |
| 1955 | , clientPid); |
| 1956 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1957 | "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting", |
| 1958 | cameraId.string(), clientName8.string(), clientPid); |
| 1959 | } |
| 1960 | |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 1961 | // Enforce client permissions and do basic validity checks |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1962 | if(!(ret = validateConnectLocked(cameraId, clientName8, |
| 1963 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { |
| 1964 | return ret; |
| 1965 | } |
| 1966 | |
| 1967 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 1968 | // we were doing a shim update, return immediately |
| 1969 | if (shimUpdateOnly) { |
| 1970 | auto cameraState = getCameraState(cameraId); |
| 1971 | if (cameraState != nullptr) { |
| 1972 | if (!cameraState->getShimParams().isEmpty()) return ret; |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | status_t err; |
| 1977 | |
| 1978 | sp<BasicClient> clientTmp = nullptr; |
| 1979 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial; |
| 1980 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1981 | IInterface::asBinder(cameraCb), clientName8, oomScoreOffset, systemNativeClient, |
| 1982 | /*out*/&clientTmp, /*out*/&partial)) != NO_ERROR) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1983 | switch (err) { |
| 1984 | case -ENODEV: |
| 1985 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1986 | "No camera device with ID \"%s\" currently available", |
| 1987 | cameraId.string()); |
| 1988 | case -EBUSY: |
| 1989 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 1990 | "Higher-priority client using camera, ID \"%s\" currently unavailable", |
| 1991 | cameraId.string()); |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1992 | case -EUSERS: |
| 1993 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1994 | "Too many cameras already open, cannot open camera \"%s\"", |
| 1995 | cameraId.string()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1996 | default: |
| 1997 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1998 | "Unexpected error %s (%d) opening camera \"%s\"", |
| 1999 | strerror(-err), err, cameraId.string()); |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | if (clientTmp.get() != nullptr) { |
| 2004 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 2005 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 2006 | return ret; |
| 2007 | } |
| 2008 | |
| 2009 | // give flashlight a chance to close devices if necessary. |
| 2010 | mFlashlight->prepareDeviceOpen(cameraId); |
| 2011 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2012 | int portraitRotation; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2013 | auto deviceVersionAndTransport = |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2014 | getDeviceVersion(cameraId, overrideToPortrait, /*out*/&portraitRotation, |
| 2015 | /*out*/&facing, /*out*/&orientation); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 2016 | if (facing == -1) { |
| 2017 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string()); |
| 2018 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2019 | "Unable to get camera device \"%s\" facing", cameraId.string()); |
| 2020 | } |
| 2021 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2022 | sp<BasicClient> tmp = nullptr; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2023 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
| 2024 | mPerfClassPrimaryCameraIds, cameraId.string(), targetSdkVersion); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2025 | if(!(ret = makeClient(this, cameraCb, clientPackageName, systemNativeClient, |
| 2026 | clientFeatureId, cameraId, api1CameraId, facing, orientation, |
Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 2027 | clientPid, clientUid, getpid(), |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2028 | deviceVersionAndTransport, effectiveApiLevel, overrideForPerfClass, |
Chengfei Tao | be683db | 2023-01-31 18:52:49 +0000 | [diff] [blame] | 2029 | overrideToPortrait, forceSlowJpegMode, |
| 2030 | /*out*/&tmp)).isOk()) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2031 | return ret; |
| 2032 | } |
| 2033 | client = static_cast<CLIENT*>(tmp.get()); |
| 2034 | |
| 2035 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 2036 | __FUNCTION__); |
| 2037 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2038 | String8 monitorTags = isClientWatched(client.get()) ? mMonitorTags : String8(""); |
| 2039 | err = client->initialize(mCameraProviderManager, monitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2040 | if (err != OK) { |
| 2041 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2042 | // Errors could be from the HAL module open call or from AppOpsManager |
| 2043 | switch(err) { |
| 2044 | case BAD_VALUE: |
| 2045 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2046 | "Illegal argument to HAL module for camera \"%s\"", cameraId.string()); |
| 2047 | case -EBUSY: |
| 2048 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2049 | "Camera \"%s\" is already open", cameraId.string()); |
| 2050 | case -EUSERS: |
| 2051 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2052 | "Too many cameras already open, cannot open camera \"%s\"", |
| 2053 | cameraId.string()); |
| 2054 | case PERMISSION_DENIED: |
| 2055 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2056 | "No permission to open camera \"%s\"", cameraId.string()); |
| 2057 | case -EACCES: |
| 2058 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 2059 | "Camera \"%s\" disabled by policy", cameraId.string()); |
| 2060 | case -ENODEV: |
| 2061 | default: |
| 2062 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2063 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(), |
| 2064 | strerror(-err), err); |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | // Update shim paremeters for legacy clients |
| 2069 | if (effectiveApiLevel == API_1) { |
| 2070 | // Assume we have always received a Client subclass for API1 |
| 2071 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 2072 | String8 rawParams = shimClient->getParameters(); |
| 2073 | CameraParameters params(rawParams); |
| 2074 | |
| 2075 | auto cameraState = getCameraState(cameraId); |
| 2076 | if (cameraState != nullptr) { |
| 2077 | cameraState->setShimParams(params); |
| 2078 | } else { |
| 2079 | ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.", |
| 2080 | __FUNCTION__, cameraId.string()); |
| 2081 | } |
| 2082 | } |
| 2083 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 2084 | // Enable/disable camera service watchdog |
| 2085 | client->setCameraServiceWatchdog(mCameraServiceWatchdogEnabled); |
| 2086 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 2087 | // Set rotate-and-crop override behavior |
| 2088 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { |
| 2089 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2090 | } else if (overrideToPortrait && portraitRotation != 0) { |
| 2091 | uint8_t rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_AUTO; |
| 2092 | switch (portraitRotation) { |
| 2093 | case 90: |
| 2094 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_90; |
| 2095 | break; |
| 2096 | case 180: |
| 2097 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_180; |
| 2098 | break; |
| 2099 | case 270: |
| 2100 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_270; |
| 2101 | break; |
| 2102 | default: |
| 2103 | ALOGE("Unexpected portrait rotation: %d", portraitRotation); |
| 2104 | break; |
| 2105 | } |
| 2106 | client->setRotateAndCropOverride(rotateAndCropMode); |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame] | 2107 | } else { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 2108 | client->setRotateAndCropOverride( |
| 2109 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 2110 | clientPackageName, facing, multiuser_get_user_id(clientUid))); |
| 2111 | } |
| 2112 | |
| 2113 | // Set autoframing override behaviour |
| 2114 | if (mOverrideAutoframingMode != ANDROID_CONTROL_AUTOFRAMING_AUTO) { |
| 2115 | client->setAutoframingOverride(mOverrideAutoframingMode); |
| 2116 | } else { |
| 2117 | client->setAutoframingOverride( |
| 2118 | mCameraServiceProxyWrapper->getAutoframingOverride( |
| 2119 | clientPackageName)); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 2120 | } |
| 2121 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2122 | // Set camera muting behavior |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 2123 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 2124 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2125 | if (client->supportsCameraMute()) { |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 2126 | client->setCameraMute( |
| 2127 | mOverrideCameraMuteMode || isCameraPrivacyEnabled); |
| 2128 | } else if (isCameraPrivacyEnabled) { |
| 2129 | // no camera mute supported, but privacy is on! => disconnect |
| 2130 | ALOGI("Camera mute not supported for package: %s, camera id: %s", |
| 2131 | String8(client->getPackageName()).string(), cameraId.string()); |
| 2132 | // Do not hold mServiceLock while disconnecting clients, but |
| 2133 | // retain the condition blocking other clients from connecting |
| 2134 | // in mServiceLockWrapper if held. |
| 2135 | mServiceLock.unlock(); |
| 2136 | // Clear caller identity temporarily so client disconnect PID |
| 2137 | // checks work correctly |
| 2138 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 2139 | // Note AppOp to trigger the "Unblock" dialog |
| 2140 | client->noteAppOp(); |
| 2141 | client->disconnect(); |
| 2142 | CameraThreadState::restoreCallingIdentity(token); |
| 2143 | // Reacquire mServiceLock |
| 2144 | mServiceLock.lock(); |
| 2145 | |
| 2146 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 2147 | "Camera \"%s\" disabled due to camera mute", cameraId.string()); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2148 | } |
| 2149 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2150 | if (shimUpdateOnly) { |
| 2151 | // If only updating legacy shim parameters, immediately disconnect client |
| 2152 | mServiceLock.unlock(); |
| 2153 | client->disconnect(); |
| 2154 | mServiceLock.lock(); |
| 2155 | } else { |
| 2156 | // Otherwise, add client to active clients list |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2157 | finishConnectLocked(client, partial, oomScoreOffset, systemNativeClient); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2158 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 2159 | |
| 2160 | client->setImageDumpMask(mImageDumpMask); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 2161 | client->setStreamUseCaseOverrides(mStreamUseCaseOverrides); |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 2162 | client->setZoomOverride(mZoomOverrideValue); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2163 | } // lock is destroyed, allow further connect calls |
| 2164 | |
| 2165 | // Important: release the mutex here so the client can call back into the service from its |
| 2166 | // destructor (can be at the end of the call) |
| 2167 | device = client; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2168 | |
| 2169 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 2170 | mCameraServiceProxyWrapper->logOpen(cameraId, facing, clientPackageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2171 | effectiveApiLevel, isNonSystemNdk, openLatencyMs); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2172 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2173 | { |
| 2174 | Mutex::Autolock lock(mInjectionParametersLock); |
| 2175 | if (cameraId == mInjectionInternalCamId && mInjectionInitPending) { |
| 2176 | mInjectionInitPending = false; |
| 2177 | status_t res = NO_ERROR; |
| 2178 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 2179 | if (clientDescriptor != nullptr) { |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2180 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 2181 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 2182 | if(res != OK) { |
| 2183 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2184 | "No camera device with ID \"%s\" currently available", |
| 2185 | mInjectionExternalCamId.string()); |
| 2186 | } |
| 2187 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2188 | if (res != OK) { |
| 2189 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2190 | } |
| 2191 | } else { |
| 2192 | ALOGE("%s: Internal camera ID = %s 's client does not exist!", |
| 2193 | __FUNCTION__, mInjectionInternalCamId.string()); |
| 2194 | res = NO_INIT; |
| 2195 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2200 | return ret; |
| 2201 | } |
| 2202 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2203 | status_t CameraService::addOfflineClient(String8 cameraId, sp<BasicClient> offlineClient) { |
| 2204 | if (offlineClient.get() == nullptr) { |
| 2205 | return BAD_VALUE; |
| 2206 | } |
| 2207 | |
| 2208 | { |
| 2209 | // Acquire mServiceLock and prevent other clients from connecting |
| 2210 | std::unique_ptr<AutoConditionLock> lock = |
| 2211 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2212 | |
| 2213 | if (lock == nullptr) { |
| 2214 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." |
| 2215 | , __FUNCTION__, offlineClient->getClientPid()); |
| 2216 | return TIMED_OUT; |
| 2217 | } |
| 2218 | |
| 2219 | auto onlineClientDesc = mActiveClientManager.get(cameraId); |
| 2220 | if (onlineClientDesc.get() == nullptr) { |
| 2221 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, |
| 2222 | cameraId.c_str()); |
| 2223 | return BAD_VALUE; |
| 2224 | } |
| 2225 | |
| 2226 | // Offline clients do not evict or conflict with other online devices. Resource sharing |
| 2227 | // conflicts are handled by the camera provider which will either succeed or fail before |
| 2228 | // reaching this method. |
| 2229 | const auto& onlinePriority = onlineClientDesc->getPriority(); |
| 2230 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( |
| 2231 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, |
| 2232 | /*conflictingKeys*/ std::set<String8>(), onlinePriority.getScore(), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2233 | onlineClientDesc->getOwnerId(), onlinePriority.getState(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2234 | // native clients don't have offline processing support. |
| 2235 | /*ommScoreOffset*/ 0, /*systemNativeClient*/false); |
Guillaume Bailey | 417e43d | 2022-11-02 15:30:24 +0100 | [diff] [blame] | 2236 | if (offlineClientDesc == nullptr) { |
| 2237 | ALOGE("%s: Offline client descriptor was NULL", __FUNCTION__); |
| 2238 | return BAD_VALUE; |
| 2239 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2240 | |
| 2241 | // Allow only one offline device per camera |
| 2242 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); |
| 2243 | if (!incompatibleClients.empty()) { |
| 2244 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); |
| 2245 | return BAD_VALUE; |
| 2246 | } |
| 2247 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2248 | String8 monitorTags = isClientWatched(offlineClient.get()) ? mMonitorTags : String8(""); |
| 2249 | auto err = offlineClient->initialize(mCameraProviderManager, monitorTags); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2250 | if (err != OK) { |
| 2251 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); |
| 2252 | return err; |
| 2253 | } |
| 2254 | |
| 2255 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); |
| 2256 | if (evicted.size() > 0) { |
| 2257 | for (auto& i : evicted) { |
| 2258 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", |
| 2259 | __FUNCTION__, i->getKey().string()); |
| 2260 | } |
| 2261 | |
| 2262 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " |
| 2263 | "properly", __FUNCTION__); |
| 2264 | |
| 2265 | return BAD_VALUE; |
| 2266 | } |
| 2267 | |
| 2268 | logConnectedOffline(offlineClientDesc->getKey(), |
| 2269 | static_cast<int>(offlineClientDesc->getOwnerId()), |
| 2270 | String8(offlineClient->getPackageName())); |
| 2271 | |
| 2272 | sp<IBinder> remoteCallback = offlineClient->getRemote(); |
| 2273 | if (remoteCallback != nullptr) { |
| 2274 | remoteCallback->linkToDeath(this); |
| 2275 | } |
| 2276 | } // lock is destroyed, allow further connect calls |
| 2277 | |
| 2278 | return OK; |
| 2279 | } |
| 2280 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2281 | Status CameraService::turnOnTorchWithStrengthLevel(const String16& cameraId, int32_t torchStrength, |
| 2282 | const sp<IBinder>& clientBinder) { |
| 2283 | Mutex::Autolock lock(mServiceLock); |
| 2284 | |
| 2285 | ATRACE_CALL(); |
| 2286 | if (clientBinder == nullptr) { |
| 2287 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
| 2288 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2289 | "Torch client binder in null."); |
| 2290 | } |
| 2291 | |
| 2292 | String8 id = String8(cameraId.string()); |
| 2293 | int uid = CameraThreadState::getCallingUid(); |
| 2294 | |
| 2295 | if (shouldRejectSystemCameraConnection(id)) { |
| 2296 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to change the strength level" |
| 2297 | "for system only device %s: ", id.string()); |
| 2298 | } |
| 2299 | |
| 2300 | // verify id is valid |
| 2301 | auto state = getCameraState(id); |
| 2302 | if (state == nullptr) { |
| 2303 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
| 2304 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2305 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
| 2306 | } |
| 2307 | |
| 2308 | StatusInternal cameraStatus = state->getStatus(); |
| 2309 | if (cameraStatus != StatusInternal::NOT_AVAILABLE && |
| 2310 | cameraStatus != StatusInternal::PRESENT) { |
| 2311 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), |
| 2312 | (int)cameraStatus); |
| 2313 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2314 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
| 2315 | } |
| 2316 | |
| 2317 | { |
| 2318 | Mutex::Autolock al(mTorchStatusMutex); |
| 2319 | TorchModeStatus status; |
| 2320 | status_t err = getTorchStatusLocked(id, &status); |
| 2321 | if (err != OK) { |
| 2322 | if (err == NAME_NOT_FOUND) { |
| 2323 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2324 | "Camera \"%s\" does not have a flash unit", id.string()); |
| 2325 | } |
| 2326 | ALOGE("%s: getting current torch status failed for camera %s", |
| 2327 | __FUNCTION__, id.string()); |
| 2328 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2329 | "Error changing torch strength level for camera \"%s\": %s (%d)", |
| 2330 | id.string(), strerror(-err), err); |
| 2331 | } |
| 2332 | |
| 2333 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
| 2334 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
| 2335 | ALOGE("%s: torch mode of camera %s is not available because " |
| 2336 | "camera is in use.", __FUNCTION__, id.string()); |
| 2337 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2338 | "Torch for camera \"%s\" is not available due to an existing camera user", |
| 2339 | id.string()); |
| 2340 | } else { |
| 2341 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 2342 | "insufficient resources", __FUNCTION__, id.string()); |
| 2343 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2344 | "Torch for camera \"%s\" is not available due to insufficient resources", |
| 2345 | id.string()); |
| 2346 | } |
| 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | { |
| 2351 | Mutex::Autolock al(mTorchUidMapMutex); |
| 2352 | updateTorchUidMapLocked(cameraId, uid); |
| 2353 | } |
| 2354 | // Check if the current torch strength level is same as the new one. |
| 2355 | bool shouldSkipTorchStrengthUpdates = mCameraProviderManager->shouldSkipTorchStrengthUpdate( |
| 2356 | id.string(), torchStrength); |
| 2357 | |
| 2358 | status_t err = mFlashlight->turnOnTorchWithStrengthLevel(id, torchStrength); |
| 2359 | |
| 2360 | if (err != OK) { |
| 2361 | int32_t errorCode; |
| 2362 | String8 msg; |
| 2363 | switch (err) { |
| 2364 | case -ENOSYS: |
| 2365 | msg = String8::format("Camera \"%s\" has no flashlight.", |
| 2366 | id.string()); |
| 2367 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2368 | break; |
| 2369 | case -EBUSY: |
| 2370 | msg = String8::format("Camera \"%s\" is in use", |
| 2371 | id.string()); |
| 2372 | errorCode = ERROR_CAMERA_IN_USE; |
| 2373 | break; |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 2374 | case -EINVAL: |
| 2375 | msg = String8::format("Torch strength level %d is not within the " |
| 2376 | "valid range.", torchStrength); |
| 2377 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2378 | break; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2379 | default: |
| 2380 | msg = String8::format("Changing torch strength level failed."); |
| 2381 | errorCode = ERROR_INVALID_OPERATION; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2382 | } |
| 2383 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2384 | return STATUS_ERROR(errorCode, msg.string()); |
| 2385 | } |
| 2386 | |
| 2387 | { |
| 2388 | // update the link to client's death |
| 2389 | // Store the last client that turns on each camera's torch mode. |
| 2390 | Mutex::Autolock al(mTorchClientMapMutex); |
| 2391 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 2392 | if (index == NAME_NOT_FOUND) { |
| 2393 | mTorchClientMap.add(id, clientBinder); |
| 2394 | } else { |
| 2395 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
| 2396 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2397 | } |
| 2398 | clientBinder->linkToDeath(this); |
| 2399 | } |
| 2400 | |
| 2401 | int clientPid = CameraThreadState::getCallingPid(); |
| 2402 | const char *id_cstr = id.c_str(); |
| 2403 | ALOGI("%s: Torch strength for camera id %s changed to %d for client PID %d", |
| 2404 | __FUNCTION__, id_cstr, torchStrength, clientPid); |
| 2405 | if (!shouldSkipTorchStrengthUpdates) { |
| 2406 | broadcastTorchStrengthLevel(id, torchStrength); |
| 2407 | } |
| 2408 | return Status::ok(); |
| 2409 | } |
| 2410 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2411 | Status CameraService::setTorchMode(const String16& cameraId, bool enabled, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2412 | const sp<IBinder>& clientBinder) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2413 | Mutex::Autolock lock(mServiceLock); |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2414 | |
| 2415 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2416 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2417 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2418 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2419 | "Torch client Binder is null"); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2420 | } |
| 2421 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2422 | String8 id = String8(cameraId.string()); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2423 | int uid = CameraThreadState::getCallingUid(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2424 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2425 | if (shouldRejectSystemCameraConnection(id)) { |
| 2426 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" |
| 2427 | " for system only device %s: ", id.string()); |
| 2428 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2429 | // verify id is valid. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2430 | auto state = getCameraState(id); |
| 2431 | if (state == nullptr) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 2432 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2433 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2434 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2435 | } |
| 2436 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2437 | StatusInternal cameraStatus = state->getStatus(); |
| 2438 | if (cameraStatus != StatusInternal::PRESENT && |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2439 | cameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 2440 | 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] | 2441 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2442 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | { |
| 2446 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2447 | TorchModeStatus status; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2448 | status_t err = getTorchStatusLocked(id, &status); |
| 2449 | if (err != OK) { |
| 2450 | if (err == NAME_NOT_FOUND) { |
| 2451 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2452 | "Camera \"%s\" does not have a flash unit", id.string()); |
| 2453 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2454 | ALOGE("%s: getting current torch status failed for camera %s", |
| 2455 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2456 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2457 | "Error updating torch status for camera \"%s\": %s (%d)", id.string(), |
| 2458 | strerror(-err), err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2459 | } |
| 2460 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2461 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2462 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2463 | ALOGE("%s: torch mode of camera %s is not available because " |
| 2464 | "camera is in use", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2465 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2466 | "Torch for camera \"%s\" is not available due to an existing camera user", |
| 2467 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2468 | } else { |
| 2469 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 2470 | "insufficient resources", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2471 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2472 | "Torch for camera \"%s\" is not available due to insufficient resources", |
| 2473 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | } |
| 2477 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2478 | { |
| 2479 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 2480 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 2481 | Mutex::Autolock al(mTorchUidMapMutex); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2482 | updateTorchUidMapLocked(cameraId, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2483 | } |
| 2484 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2485 | status_t err = mFlashlight->setTorchMode(id, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2486 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2487 | if (err != OK) { |
| 2488 | int32_t errorCode; |
| 2489 | String8 msg; |
| 2490 | switch (err) { |
| 2491 | case -ENOSYS: |
| 2492 | msg = String8::format("Camera \"%s\" has no flashlight", |
| 2493 | id.string()); |
| 2494 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2495 | break; |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 2496 | case -EBUSY: |
| 2497 | msg = String8::format("Camera \"%s\" is in use", |
| 2498 | id.string()); |
| 2499 | errorCode = ERROR_CAMERA_IN_USE; |
| 2500 | break; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2501 | default: |
| 2502 | msg = String8::format( |
| 2503 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", |
| 2504 | id.string(), enabled, strerror(-err), err); |
| 2505 | errorCode = ERROR_INVALID_OPERATION; |
| 2506 | } |
| 2507 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2508 | logServiceError(msg,errorCode); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2509 | return STATUS_ERROR(errorCode, msg.string()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2510 | } |
| 2511 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2512 | { |
| 2513 | // update the link to client's death |
| 2514 | Mutex::Autolock al(mTorchClientMapMutex); |
| 2515 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 2516 | if (enabled) { |
| 2517 | if (index == NAME_NOT_FOUND) { |
| 2518 | mTorchClientMap.add(id, clientBinder); |
| 2519 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2520 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2521 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2522 | } |
| 2523 | clientBinder->linkToDeath(this); |
| 2524 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2525 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2526 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2527 | } |
| 2528 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 2529 | int clientPid = CameraThreadState::getCallingPid(); |
| 2530 | const char *id_cstr = id.c_str(); |
| 2531 | const char *torchState = enabled ? "on" : "off"; |
| 2532 | ALOGI("Torch for camera id %s turned %s for client PID %d", id_cstr, torchState, clientPid); |
| 2533 | logTorchEvent(id_cstr, torchState , clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2534 | return Status::ok(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2535 | } |
| 2536 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2537 | void CameraService::updateTorchUidMapLocked(const String16& cameraId, int uid) { |
| 2538 | String8 id = String8(cameraId.string()); |
| 2539 | if (mTorchUidMap.find(id) == mTorchUidMap.end()) { |
| 2540 | mTorchUidMap[id].first = uid; |
| 2541 | mTorchUidMap[id].second = uid; |
| 2542 | } else { |
| 2543 | // Set the pending UID |
| 2544 | mTorchUidMap[id].first = uid; |
| 2545 | } |
| 2546 | } |
| 2547 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2548 | Status CameraService::notifySystemEvent(int32_t eventId, |
| 2549 | const std::vector<int32_t>& args) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2550 | const int pid = CameraThreadState::getCallingPid(); |
| 2551 | const int selfPid = getpid(); |
| 2552 | |
| 2553 | // Permission checks |
| 2554 | if (pid != selfPid) { |
| 2555 | // Ensure we're being called by system_server, or similar process with |
| 2556 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2557 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2558 | const int uid = CameraThreadState::getCallingUid(); |
| 2559 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 2560 | " events from pid=%d, uid=%d", pid, uid); |
| 2561 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 2562 | "No permission to send updates to camera service about system events" |
| 2563 | " from pid=%d, uid=%d", pid, uid); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2564 | } |
| 2565 | } |
| 2566 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2567 | ATRACE_CALL(); |
| 2568 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2569 | switch(eventId) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2570 | case ICameraService::EVENT_USER_SWITCHED: { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2571 | // 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] | 2572 | // from a system server crash |
| 2573 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2574 | mSensorPrivacyPolicy->registerSelf(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2575 | doUserSwitch(/*newUserIds*/ args); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2576 | break; |
| 2577 | } |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 2578 | case ICameraService::EVENT_USB_DEVICE_ATTACHED: |
| 2579 | case ICameraService::EVENT_USB_DEVICE_DETACHED: { |
| 2580 | // Notify CameraProviderManager for lazy HALs |
| 2581 | mCameraProviderManager->notifyUsbDeviceEvent(eventId, |
| 2582 | std::to_string(args[0])); |
| 2583 | break; |
| 2584 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2585 | case ICameraService::EVENT_NONE: |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2586 | default: { |
| 2587 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 2588 | eventId); |
| 2589 | break; |
| 2590 | } |
| 2591 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2592 | return Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2593 | } |
| 2594 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2595 | void CameraService::notifyMonitoredUids() { |
| 2596 | Mutex::Autolock lock(mStatusListenerLock); |
| 2597 | |
| 2598 | for (const auto& it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2599 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 2600 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 2601 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2602 | } |
| 2603 | } |
| 2604 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 2605 | void CameraService::notifyMonitoredUids(const std::unordered_set<uid_t> ¬ifyUidSet) { |
| 2606 | Mutex::Autolock lock(mStatusListenerLock); |
| 2607 | |
| 2608 | for (const auto& it : mListenerList) { |
| 2609 | if (notifyUidSet.find(it->getListenerUid()) != notifyUidSet.end()) { |
| 2610 | ALOGV("%s: notifying uid %d", __FUNCTION__, it->getListenerUid()); |
| 2611 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
| 2612 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 2613 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
| 2614 | } |
| 2615 | } |
| 2616 | } |
| 2617 | |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2618 | Status CameraService::notifyDeviceStateChange(int64_t newState) { |
| 2619 | const int pid = CameraThreadState::getCallingPid(); |
| 2620 | const int selfPid = getpid(); |
| 2621 | |
| 2622 | // Permission checks |
| 2623 | if (pid != selfPid) { |
| 2624 | // Ensure we're being called by system_server, or similar process with |
| 2625 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2626 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2627 | const int uid = CameraThreadState::getCallingUid(); |
| 2628 | ALOGE("Permission Denial: cannot send updates to camera service about device" |
| 2629 | " state changes from pid=%d, uid=%d", pid, uid); |
| 2630 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2631 | "No permission to send updates to camera service about device state" |
| 2632 | " changes from pid=%d, uid=%d", pid, uid); |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | ATRACE_CALL(); |
| 2637 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2638 | { |
| 2639 | Mutex::Autolock lock(mServiceLock); |
| 2640 | mDeviceState = newState; |
| 2641 | } |
| 2642 | |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 2643 | mCameraProviderManager->notifyDeviceStateChange(newState); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2644 | |
| 2645 | return Status::ok(); |
| 2646 | } |
| 2647 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2648 | Status CameraService::notifyDisplayConfigurationChange() { |
| 2649 | ATRACE_CALL(); |
| 2650 | const int callingPid = CameraThreadState::getCallingPid(); |
| 2651 | const int selfPid = getpid(); |
| 2652 | |
| 2653 | // Permission checks |
| 2654 | if (callingPid != selfPid) { |
| 2655 | // Ensure we're being called by system_server, or similar process with |
| 2656 | // permissions to notify the camera service about system events |
| 2657 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
| 2658 | const int uid = CameraThreadState::getCallingUid(); |
| 2659 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" |
| 2660 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2661 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2662 | "No permission to send updates to camera service about orientation" |
| 2663 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | Mutex::Autolock lock(mServiceLock); |
| 2668 | |
| 2669 | // Don't do anything if rotate-and-crop override via cmd is active |
| 2670 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); |
| 2671 | |
| 2672 | const auto clients = mActiveClientManager.getAll(); |
| 2673 | for (auto& current : clients) { |
| 2674 | if (current != nullptr) { |
| 2675 | const auto basicClient = current->getValue(); |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2676 | if (basicClient.get() != nullptr && !basicClient->getOverrideToPortrait()) { |
| 2677 | basicClient->setRotateAndCropOverride( |
| 2678 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 2679 | basicClient->getPackageName(), |
| 2680 | basicClient->getCameraFacing(), |
| 2681 | multiuser_get_user_id(basicClient->getClientUid()))); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2682 | } |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | return Status::ok(); |
| 2687 | } |
| 2688 | |
| 2689 | Status CameraService::getConcurrentCameraIds( |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2690 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { |
| 2691 | ATRACE_CALL(); |
| 2692 | if (!concurrentCameraIds) { |
| 2693 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); |
| 2694 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); |
| 2695 | } |
| 2696 | |
| 2697 | if (!mInitialized) { |
| 2698 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2699 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2700 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2701 | "Camera subsystem is not available"); |
| 2702 | } |
| 2703 | // First call into the provider and get the set of concurrent camera |
| 2704 | // combinations |
| 2705 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = |
Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 2706 | mCameraProviderManager->getConcurrentCameraIds(); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2707 | for (auto &combination : concurrentCameraCombinations) { |
| 2708 | std::vector<std::string> validCombination; |
| 2709 | for (auto &cameraId : combination) { |
| 2710 | // if the camera state is not present, skip |
| 2711 | String8 cameraIdStr(cameraId.c_str()); |
| 2712 | auto state = getCameraState(cameraIdStr); |
| 2713 | if (state == nullptr) { |
| 2714 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); |
| 2715 | continue; |
| 2716 | } |
| 2717 | StatusInternal status = state->getStatus(); |
| 2718 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { |
| 2719 | continue; |
| 2720 | } |
| 2721 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { |
| 2722 | continue; |
| 2723 | } |
| 2724 | validCombination.push_back(cameraId); |
| 2725 | } |
| 2726 | if (validCombination.size() != 0) { |
| 2727 | concurrentCameraIds->push_back(std::move(validCombination)); |
| 2728 | } |
| 2729 | } |
| 2730 | return Status::ok(); |
| 2731 | } |
| 2732 | |
| 2733 | Status CameraService::isConcurrentSessionConfigurationSupported( |
| 2734 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2735 | int targetSdkVersion, /*out*/bool* isSupported) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2736 | if (!isSupported) { |
| 2737 | ALOGE("%s: isSupported is NULL", __FUNCTION__); |
| 2738 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); |
| 2739 | } |
| 2740 | |
| 2741 | if (!mInitialized) { |
| 2742 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 2743 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2744 | "Camera subsystem is not available"); |
| 2745 | } |
| 2746 | |
| 2747 | // Check for camera permissions |
| 2748 | int callingPid = CameraThreadState::getCallingPid(); |
| 2749 | int callingUid = CameraThreadState::getCallingUid(); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 2750 | permission::PermissionChecker permissionChecker; |
| 2751 | AttributionSourceState attributionSource{}; |
| 2752 | attributionSource.pid = callingPid; |
| 2753 | attributionSource.uid = callingUid; |
| 2754 | bool checkPermissionForCamera = permissionChecker.checkPermissionForPreflight( |
| 2755 | sCameraPermission, attributionSource, String16(), AppOpsManager::OP_NONE) |
| 2756 | != permission::PermissionChecker::PERMISSION_HARD_DENIED; |
| 2757 | if ((callingPid != getpid()) && !checkPermissionForCamera) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2758 | ALOGE("%s: pid %d doesn't have camera permissions", __FUNCTION__, callingPid); |
| 2759 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 2760 | "android.permission.CAMERA needed to call" |
| 2761 | "isConcurrentSessionConfigurationSupported"); |
| 2762 | } |
| 2763 | |
| 2764 | status_t res = |
| 2765 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2766 | cameraIdsAndSessionConfigurations, mPerfClassPrimaryCameraIds, |
| 2767 | targetSdkVersion, isSupported); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2768 | if (res != OK) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2769 | logServiceError(String8::format("Unable to query session configuration support"), |
| 2770 | ERROR_INVALID_OPERATION); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2771 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " |
| 2772 | "support %s (%d)", strerror(-res), res); |
| 2773 | } |
| 2774 | return Status::ok(); |
| 2775 | } |
| 2776 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2777 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, |
| 2778 | /*out*/ |
| 2779 | std::vector<hardware::CameraStatus> *cameraStatuses) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2780 | return addListenerHelper(listener, cameraStatuses); |
| 2781 | } |
| 2782 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2783 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, |
| 2784 | std::vector<hardware::CameraStatus>* cameraStatuses) { |
| 2785 | return addListenerHelper(listener, cameraStatuses, false, true); |
| 2786 | } |
| 2787 | |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2788 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, |
| 2789 | /*out*/ |
| 2790 | std::vector<hardware::CameraStatus> *cameraStatuses, |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2791 | bool isVendorListener, bool isProcessLocalTest) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2792 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2793 | ATRACE_CALL(); |
| 2794 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2795 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2796 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 2797 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2798 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2799 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2800 | } |
| 2801 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2802 | auto clientUid = CameraThreadState::getCallingUid(); |
| 2803 | auto clientPid = CameraThreadState::getCallingPid(); |
Joanne Chung | 02c13d0 | 2023-01-16 12:58:05 +0000 | [diff] [blame] | 2804 | permission::PermissionChecker permissionChecker; |
| 2805 | AttributionSourceState attributionSource{}; |
| 2806 | attributionSource.uid = clientUid; |
| 2807 | attributionSource.pid = clientPid; |
| 2808 | bool openCloseCallbackAllowed = permissionChecker.checkPermissionForPreflight( |
| 2809 | sCameraOpenCloseListenerPermission, attributionSource, String16(), |
| 2810 | AppOpsManager::OP_NONE) != permission::PermissionChecker::PERMISSION_HARD_DENIED; |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2811 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2812 | Mutex::Autolock lock(mServiceLock); |
| 2813 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2814 | { |
| 2815 | Mutex::Autolock lock(mStatusListenerLock); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2816 | for (const auto &it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2817 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2818 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 2819 | __FUNCTION__, listener.get()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2820 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2821 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2822 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2823 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2824 | sp<ServiceListener> serviceListener = |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2825 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, |
| 2826 | openCloseCallbackAllowed); |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2827 | auto ret = serviceListener->initialize(isProcessLocalTest); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2828 | if (ret != NO_ERROR) { |
| 2829 | String8 msg = String8::format("Failed to initialize service listener: %s (%d)", |
| 2830 | strerror(-ret), ret); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2831 | logServiceError(msg,ERROR_ILLEGAL_ARGUMENT); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2832 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2833 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 2834 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2835 | // The listener still needs to be added to the list of listeners, regardless of what |
| 2836 | // permissions the listener process has / whether it is a vendor listener. Since it might be |
| 2837 | // eligible to listen to other camera ids. |
| 2838 | mListenerList.emplace_back(serviceListener); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 2839 | mUidPolicy->registerMonitorUid(clientUid, /*openCamera*/false); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2840 | } |
| 2841 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2842 | /* Collect current devices and status */ |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2843 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2844 | Mutex::Autolock lock(mCameraStatesLock); |
| 2845 | for (auto& i : mCameraStates) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 2846 | cameraStatuses->emplace_back(i.first, |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 2847 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds(), |
| 2848 | openCloseCallbackAllowed ? i.second->getClientPackage() : String8::empty()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2849 | } |
| 2850 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2851 | // Remove the camera statuses that should be hidden from the client, we do |
| 2852 | // this after collecting the states in order to avoid holding |
| 2853 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at |
| 2854 | // the same time. |
| 2855 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), |
| 2856 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { |
| 2857 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 2858 | if (getSystemCameraKind(s.cameraId, &deviceKind) != OK) { |
| 2859 | ALOGE("%s: Invalid camera id %s, skipping status update", |
| 2860 | __FUNCTION__, s.cameraId.c_str()); |
| 2861 | return true; |
| 2862 | } |
| 2863 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, |
| 2864 | clientUid);}), cameraStatuses->end()); |
| 2865 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2866 | //cameraStatuses will have non-eligible camera ids removed. |
| 2867 | std::set<String16> idsChosenForCallback; |
| 2868 | for (const auto &s : *cameraStatuses) { |
| 2869 | idsChosenForCallback.insert(String16(s.cameraId)); |
| 2870 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2871 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2872 | /* |
| 2873 | * Immediately signal current torch status to this listener only |
| 2874 | * This may be a subset of all the devices, so don't include it in the response directly |
| 2875 | */ |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2876 | { |
| 2877 | Mutex::Autolock al(mTorchStatusMutex); |
| 2878 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2879 | String16 id = String16(mTorchStatusMap.keyAt(i).string()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2880 | // The camera id is visible to the client. Fine to send torch |
| 2881 | // callback. |
| 2882 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { |
| 2883 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id); |
| 2884 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2885 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2886 | } |
| 2887 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2888 | return Status::ok(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2889 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2890 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2891 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2892 | ATRACE_CALL(); |
| 2893 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2894 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 2895 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2896 | if (listener == 0) { |
| 2897 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2898 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2899 | } |
| 2900 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2901 | Mutex::Autolock lock(mServiceLock); |
| 2902 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2903 | { |
| 2904 | Mutex::Autolock lock(mStatusListenerLock); |
| 2905 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2906 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 2907 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid(), /*closeCamera*/false); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2908 | IInterface::asBinder(listener)->unlinkToDeath(*it); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2909 | mListenerList.erase(it); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2910 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2911 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 2916 | __FUNCTION__, listener.get()); |
| 2917 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2918 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2919 | } |
| 2920 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2921 | Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2922 | |
| 2923 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2924 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 2925 | |
| 2926 | if (parameters == NULL) { |
| 2927 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2928 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2929 | } |
| 2930 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2931 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2932 | |
| 2933 | CameraParameters shimParams; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2934 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2935 | // Error logged by caller |
| 2936 | return ret; |
| 2937 | } |
| 2938 | |
| 2939 | String8 shimParamsString8 = shimParams.flatten(); |
| 2940 | String16 shimParamsString16 = String16(shimParamsString8); |
| 2941 | |
| 2942 | *parameters = shimParamsString16; |
| 2943 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2944 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2945 | } |
| 2946 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2947 | Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion, |
| 2948 | /*out*/ bool *isSupported) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2949 | ATRACE_CALL(); |
| 2950 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2951 | const String8 id = String8(cameraId); |
| 2952 | |
| 2953 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2954 | |
| 2955 | switch (apiVersion) { |
| 2956 | case API_VERSION_1: |
| 2957 | case API_VERSION_2: |
| 2958 | break; |
| 2959 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2960 | String8 msg = String8::format("Unknown API version %d", apiVersion); |
| 2961 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2962 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2963 | } |
| 2964 | |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2965 | int portraitRotation; |
| 2966 | auto deviceVersionAndTransport = getDeviceVersion(id, false, &portraitRotation); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2967 | if (deviceVersionAndTransport.first == -1) { |
| 2968 | String8 msg = String8::format("Unknown camera ID %s", id.string()); |
| 2969 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2970 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 2971 | } |
| 2972 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 2973 | int deviceVersion = deviceVersionAndTransport.first; |
| 2974 | switch (deviceVersion) { |
| 2975 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 2976 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 2977 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 2978 | if (apiVersion == API_VERSION_2) { |
| 2979 | ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without " |
| 2980 | "shim", __FUNCTION__, id.string(), deviceVersion); |
| 2981 | *isSupported = false; |
| 2982 | } else { // if (apiVersion == API_VERSION_1) { |
| 2983 | ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always " |
| 2984 | "supported", __FUNCTION__, id.string()); |
| 2985 | *isSupported = true; |
| 2986 | } |
| 2987 | break; |
| 2988 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 2989 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 2990 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 2991 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 2992 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 2993 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 2994 | 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] | 2995 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2996 | *isSupported = true; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2997 | break; |
| 2998 | default: { |
| 2999 | String8 msg = String8::format("Unknown device version %x for device %s", |
| 3000 | deviceVersion, id.string()); |
| 3001 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 3002 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3003 | } |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3004 | } |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 3005 | } else { |
| 3006 | *isSupported = true; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3007 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3008 | return Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 3011 | Status CameraService::isHiddenPhysicalCamera(const String16& cameraId, |
| 3012 | /*out*/ bool *isSupported) { |
| 3013 | ATRACE_CALL(); |
| 3014 | |
| 3015 | const String8 id = String8(cameraId); |
| 3016 | |
| 3017 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
| 3018 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(id.string()); |
| 3019 | |
| 3020 | return Status::ok(); |
| 3021 | } |
| 3022 | |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3023 | Status CameraService::injectCamera( |
| 3024 | const String16& packageName, const String16& internalCamId, |
| 3025 | const String16& externalCamId, |
| 3026 | const sp<ICameraInjectionCallback>& callback, |
| 3027 | /*out*/ |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3028 | sp<ICameraInjectionSession>* cameraInjectionSession) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3029 | ATRACE_CALL(); |
| 3030 | |
| 3031 | if (!checkCallingPermission(sCameraInjectExternalCameraPermission)) { |
| 3032 | const int pid = CameraThreadState::getCallingPid(); |
| 3033 | const int uid = CameraThreadState::getCallingUid(); |
| 3034 | ALOGE("Permission Denial: can't inject camera pid=%d, uid=%d", pid, uid); |
| 3035 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 3036 | "Permission Denial: no permission to inject camera"); |
| 3037 | } |
| 3038 | |
| 3039 | ALOGV( |
| 3040 | "%s: Package name = %s, Internal camera ID = %s, External camera ID = " |
| 3041 | "%s", |
| 3042 | __FUNCTION__, String8(packageName).string(), |
| 3043 | String8(internalCamId).string(), String8(externalCamId).string()); |
| 3044 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3045 | { |
| 3046 | Mutex::Autolock lock(mInjectionParametersLock); |
| 3047 | mInjectionInternalCamId = String8(internalCamId); |
| 3048 | mInjectionExternalCamId = String8(externalCamId); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3049 | mInjectionStatusListener->addListener(callback); |
| 3050 | *cameraInjectionSession = new CameraInjectionSession(this); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3051 | status_t res = NO_ERROR; |
| 3052 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 3053 | // If the client already exists, we can directly connect to the camera device through the |
| 3054 | // client's injectCamera(), otherwise we need to wait until the client is established |
| 3055 | // (execute connectHelper()) before injecting the camera to the camera device. |
| 3056 | if (clientDescriptor != nullptr) { |
| 3057 | mInjectionInitPending = false; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 3058 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 3059 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 3060 | if(res != OK) { |
| 3061 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 3062 | "No camera device with ID \"%s\" currently available", |
| 3063 | mInjectionExternalCamId.string()); |
| 3064 | } |
| 3065 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3066 | if(res != OK) { |
| 3067 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 3068 | } |
| 3069 | } else { |
| 3070 | mInjectionInitPending = true; |
| 3071 | } |
| 3072 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3073 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 3074 | return binder::Status::ok(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3075 | } |
| 3076 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3077 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3078 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3079 | for (auto& i : mActiveClientManager.getAll()) { |
| 3080 | auto clientSp = i->getValue(); |
| 3081 | if (clientSp.get() == client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3082 | cacheClientTagDumpIfNeeded(client->mCameraIdStr, clientSp.get()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3083 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3084 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3085 | } |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3086 | updateAudioRestrictionLocked(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3087 | } |
| 3088 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3089 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3090 | bool ret = false; |
| 3091 | { |
| 3092 | // Acquire mServiceLock and prevent other clients from connecting |
| 3093 | std::unique_ptr<AutoConditionLock> lock = |
| 3094 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3095 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3096 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3097 | std::vector<sp<BasicClient>> evicted; |
| 3098 | for (auto& i : mActiveClientManager.getAll()) { |
| 3099 | auto clientSp = i->getValue(); |
| 3100 | if (clientSp.get() == nullptr) { |
| 3101 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3102 | mActiveClientManager.remove(i); |
| 3103 | continue; |
| 3104 | } |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 3105 | if (remote == clientSp->getRemote()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3106 | mActiveClientManager.remove(i); |
| 3107 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3108 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3109 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3110 | clientSp->notifyError( |
| 3111 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3112 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3113 | } |
| 3114 | } |
| 3115 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3116 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 3117 | // other clients from connecting in mServiceLockWrapper if held |
| 3118 | mServiceLock.unlock(); |
| 3119 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3120 | // Do not clear caller identity, remote caller should be client proccess |
| 3121 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3122 | for (auto& i : evicted) { |
| 3123 | if (i.get() != nullptr) { |
| 3124 | i->disconnect(); |
| 3125 | ret = true; |
| 3126 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3127 | } |
| 3128 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3129 | // Reacquire mServiceLock |
| 3130 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3131 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3132 | } // lock is destroyed, allow further connect calls |
| 3133 | |
| 3134 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3135 | } |
| 3136 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3137 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
| 3138 | const String8& cameraId) const { |
| 3139 | std::shared_ptr<CameraState> state; |
| 3140 | { |
| 3141 | Mutex::Autolock lock(mCameraStatesLock); |
| 3142 | auto iter = mCameraStates.find(cameraId); |
| 3143 | if (iter != mCameraStates.end()) { |
| 3144 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3145 | } |
| 3146 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3147 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3148 | } |
| 3149 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3150 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) { |
| 3151 | // Remove from active clients list |
| 3152 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 3153 | if (clientDescriptorPtr == nullptr) { |
| 3154 | ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__, |
| 3155 | cameraId.string()); |
| 3156 | return sp<BasicClient>{nullptr}; |
| 3157 | } |
| 3158 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3159 | sp<BasicClient> client = clientDescriptorPtr->getValue(); |
| 3160 | if (client.get() != nullptr) { |
| 3161 | cacheClientTagDumpIfNeeded(clientDescriptorPtr->getKey(), client.get()); |
| 3162 | } |
| 3163 | return client; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 3164 | } |
| 3165 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3166 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3167 | // Acquire mServiceLock and prevent other clients from connecting |
| 3168 | std::unique_ptr<AutoConditionLock> lock = |
| 3169 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 3170 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3171 | std::set<userid_t> newAllowedUsers; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3172 | for (size_t i = 0; i < newUserIds.size(); i++) { |
| 3173 | if (newUserIds[i] < 0) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3174 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3175 | __FUNCTION__, newUserIds[i]); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3176 | return; |
| 3177 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3178 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3179 | } |
| 3180 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3181 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3182 | if (newAllowedUsers == mAllowedUsers) { |
| 3183 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 3184 | return; |
| 3185 | } |
| 3186 | |
| 3187 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 3188 | |
| 3189 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3190 | |
| 3191 | // Current user has switched, evict all current clients. |
| 3192 | std::vector<sp<BasicClient>> evicted; |
| 3193 | for (auto& i : mActiveClientManager.getAll()) { |
| 3194 | auto clientSp = i->getValue(); |
| 3195 | |
| 3196 | if (clientSp.get() == nullptr) { |
| 3197 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3198 | continue; |
| 3199 | } |
| 3200 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3201 | // Don't evict clients that are still allowed. |
| 3202 | uid_t clientUid = clientSp->getClientUid(); |
| 3203 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 3204 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 3205 | continue; |
| 3206 | } |
| 3207 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3208 | evicted.push_back(clientSp); |
| 3209 | |
| 3210 | String8 curTime = getFormattedCurrentTime(); |
| 3211 | |
| 3212 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
| 3213 | i->getKey().string()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3214 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3215 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3216 | logEvent(String8::format("EVICT device %s client held by package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3217 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due" |
| 3218 | " to user switch.", i->getKey().string(), |
| 3219 | String8{clientSp->getPackageName()}.string(), |
| 3220 | i->getOwnerId(), i->getPriority().getScore(), |
| 3221 | i->getPriority().getState())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3222 | |
| 3223 | } |
| 3224 | |
| 3225 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 3226 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 3227 | mServiceLock.unlock(); |
| 3228 | |
| 3229 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3230 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3231 | |
| 3232 | for (auto& i : evicted) { |
| 3233 | i->disconnect(); |
| 3234 | } |
| 3235 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3236 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3237 | |
| 3238 | // Reacquire mServiceLock |
| 3239 | mServiceLock.lock(); |
| 3240 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3241 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3242 | void CameraService::logEvent(const char* event) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3243 | String8 curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3244 | Mutex::Autolock l(mLogLock); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3245 | String8 msg = String8::format("%s : %s", curTime.string(), event); |
| 3246 | // For service error events, print the msg only once. |
| 3247 | if(!msg.contains("SERVICE ERROR")) { |
| 3248 | mEventLog.add(msg); |
| 3249 | } else if(sServiceErrorEventSet.find(msg) == sServiceErrorEventSet.end()) { |
| 3250 | // Error event not added to the dumpsys log before |
| 3251 | mEventLog.add(msg); |
| 3252 | sServiceErrorEventSet.insert(msg); |
| 3253 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3254 | } |
| 3255 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3256 | void CameraService::logDisconnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3257 | const char* clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3258 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3259 | 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] | 3260 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3261 | } |
| 3262 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3263 | void CameraService::logDisconnectedOffline(const char* cameraId, int clientPid, |
| 3264 | const char* clientPackage) { |
| 3265 | // Log the clients evicted |
| 3266 | logEvent(String8::format("DISCONNECT offline device %s client for package %s (PID %d)", |
| 3267 | cameraId, clientPackage, clientPid)); |
| 3268 | } |
| 3269 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3270 | void CameraService::logConnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3271 | const char* clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3272 | // Log the clients evicted |
| 3273 | 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] | 3274 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3275 | } |
| 3276 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3277 | void CameraService::logConnectedOffline(const char* cameraId, int clientPid, |
| 3278 | const char* clientPackage) { |
| 3279 | // Log the clients evicted |
| 3280 | logEvent(String8::format("CONNECT offline device %s client for package %s (PID %d)", cameraId, |
| 3281 | clientPackage, clientPid)); |
| 3282 | } |
| 3283 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3284 | void CameraService::logRejected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3285 | const char* clientPackage, const char* reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3286 | // Log the client rejected |
| 3287 | 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] | 3288 | cameraId, clientPackage, clientPid, reason)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3289 | } |
| 3290 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3291 | void CameraService::logTorchEvent(const char* cameraId, const char *torchState, int clientPid) { |
| 3292 | // Log torch event |
| 3293 | logEvent(String8::format("Torch for camera id %s turned %s for client PID %d", cameraId, |
| 3294 | torchState, clientPid)); |
| 3295 | } |
| 3296 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3297 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 3298 | const std::set<userid_t>& newUserIds) { |
| 3299 | String8 newUsers = toString(newUserIds); |
| 3300 | String8 oldUsers = toString(oldUserIds); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3301 | if (oldUsers.size() == 0) { |
| 3302 | oldUsers = "<None>"; |
| 3303 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3304 | // Log the new and old users |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3305 | 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] | 3306 | oldUsers.string(), newUsers.string())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) { |
| 3310 | // Log the device removal |
| 3311 | logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason)); |
| 3312 | } |
| 3313 | |
| 3314 | void CameraService::logDeviceAdded(const char* cameraId, const char* reason) { |
| 3315 | // Log the device removal |
| 3316 | logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason)); |
| 3317 | } |
| 3318 | |
| 3319 | void CameraService::logClientDied(int clientPid, const char* reason) { |
| 3320 | // Log the device removal |
| 3321 | 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] | 3322 | } |
| 3323 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3324 | void CameraService::logServiceError(const char* msg, int errorCode) { |
| 3325 | String8 curTime = getFormattedCurrentTime(); |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 3326 | 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] | 3327 | } |
| 3328 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3329 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 3330 | uint32_t flags) { |
| 3331 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3332 | // Permission checks |
| 3333 | switch (code) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3334 | case SHELL_COMMAND_TRANSACTION: { |
| 3335 | int in = data.readFileDescriptor(); |
| 3336 | int out = data.readFileDescriptor(); |
| 3337 | int err = data.readFileDescriptor(); |
| 3338 | int argc = data.readInt32(); |
| 3339 | Vector<String16> args; |
| 3340 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 3341 | args.add(data.readString16()); |
| 3342 | } |
| 3343 | sp<IBinder> unusedCallback; |
| 3344 | sp<IResultReceiver> resultReceiver; |
| 3345 | status_t status; |
| 3346 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 3347 | return status; |
| 3348 | } |
| 3349 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 3350 | return status; |
| 3351 | } |
| 3352 | status = shellCommand(in, out, err, args); |
| 3353 | if (resultReceiver != nullptr) { |
| 3354 | resultReceiver->send(status); |
| 3355 | } |
| 3356 | return NO_ERROR; |
| 3357 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | return BnCameraService::onTransact(code, data, reply, flags); |
| 3361 | } |
| 3362 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3363 | // We share the media players for shutter and recording sound for all clients. |
| 3364 | // A reference count is kept to determine when we will actually release the |
| 3365 | // media players. |
| 3366 | |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3367 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { |
| 3368 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 3369 | status_t error; |
| 3370 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 3371 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3372 | error = mp->prepare(); |
| 3373 | } |
| 3374 | if (error != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3375 | ALOGE("Failed to load CameraService sounds: %s", file); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3376 | mp->disconnect(); |
| 3377 | mp.clear(); |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3378 | return nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3379 | } |
| 3380 | return mp; |
| 3381 | } |
| 3382 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3383 | void CameraService::increaseSoundRef() { |
| 3384 | Mutex::Autolock lock(mSoundLock); |
| 3385 | mSoundRef++; |
| 3386 | } |
| 3387 | |
| 3388 | void CameraService::loadSoundLocked(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3389 | ATRACE_CALL(); |
| 3390 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3391 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); |
| 3392 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { |
| 3393 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); |
| 3394 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { |
| 3395 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 3396 | } |
| 3397 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) { |
| 3398 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); |
| 3399 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { |
| 3400 | mSoundPlayer[SOUND_RECORDING_START] = |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3401 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3402 | } |
| 3403 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { |
| 3404 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); |
| 3405 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { |
| 3406 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
| 3407 | } |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3408 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3409 | } |
| 3410 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3411 | void CameraService::decreaseSoundRef() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3412 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3413 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3414 | if (--mSoundRef) return; |
| 3415 | |
| 3416 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 3417 | if (mSoundPlayer[i] != 0) { |
| 3418 | mSoundPlayer[i]->disconnect(); |
| 3419 | mSoundPlayer[i].clear(); |
| 3420 | } |
| 3421 | } |
| 3422 | } |
| 3423 | |
| 3424 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3425 | ATRACE_CALL(); |
| 3426 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3427 | LOG1("playSound(%d)", kind); |
Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame] | 3428 | if (kind < 0 || kind >= NUM_SOUNDS) { |
| 3429 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); |
| 3430 | return; |
| 3431 | } |
| 3432 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3433 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3434 | loadSoundLocked(kind); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3435 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 3436 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 3437 | player->seekTo(0); |
| 3438 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3439 | } |
| 3440 | } |
| 3441 | |
| 3442 | // ---------------------------------------------------------------------------- |
| 3443 | |
| 3444 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 3445 | const sp<ICameraClient>& cameraClient, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3446 | const String16& clientPackageName, bool systemNativeClient, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 3447 | const std::optional<String16>& clientFeatureId, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3448 | const String8& cameraIdStr, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3449 | int api1CameraId, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3450 | int clientPid, uid_t clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3451 | int servicePid, bool overrideToPortrait) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3452 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 3453 | IInterface::asBinder(cameraClient), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3454 | clientPackageName, systemNativeClient, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3455 | cameraIdStr, cameraFacing, sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3456 | clientPid, clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3457 | servicePid, overrideToPortrait), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3458 | mCameraId(api1CameraId) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3459 | { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3460 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3461 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3462 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 3463 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3464 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3465 | cameraService->increaseSoundRef(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3466 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3467 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3468 | } |
| 3469 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3470 | // tear down the client |
| 3471 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3472 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3473 | mDestructionStarted = true; |
| 3474 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3475 | sCameraService->decreaseSoundRef(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3476 | // unconditionally disconnect. function is idempotent |
| 3477 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3478 | } |
| 3479 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3480 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; |
| 3481 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3482 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3483 | const sp<IBinder>& remoteCallback, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3484 | const String16& clientPackageName, bool nativeClient, |
| 3485 | const std::optional<String16>& clientFeatureId, const String8& cameraIdStr, |
| 3486 | int cameraFacing, int sensorOrientation, int clientPid, uid_t clientUid, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3487 | int servicePid, bool overrideToPortrait): |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3488 | mDestructionStarted(false), |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3489 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), mOrientation(sensorOrientation), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3490 | mClientPackageName(clientPackageName), mSystemNativeClient(nativeClient), |
| 3491 | mClientFeatureId(clientFeatureId), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 3492 | mClientPid(clientPid), mClientUid(clientUid), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3493 | mServicePid(servicePid), |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3494 | mDisconnected(false), mUidIsTrusted(false), |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3495 | mOverrideToPortrait(overrideToPortrait), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3496 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3497 | mRemoteBinder(remoteCallback), |
| 3498 | mOpsActive(false), |
| 3499 | mOpsStreaming(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3500 | { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3501 | if (sCameraService == nullptr) { |
| 3502 | sCameraService = cameraService; |
| 3503 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 3504 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3505 | // There are 2 scenarios in which a client won't have AppOps operations |
| 3506 | // (both scenarios : native clients) |
| 3507 | // 1) It's an system native client*, the package name will be empty |
| 3508 | // and it will return from this function in the previous if condition |
| 3509 | // (This is the same as the previously existing behavior). |
| 3510 | // 2) It is a system native client, but its package name has been |
| 3511 | // modified for debugging, however it still must not use AppOps since |
| 3512 | // the package name is not a real one. |
| 3513 | // |
| 3514 | // * system native client - native client with UID < AID_APP_START. It |
| 3515 | // doesn't exclude clients not on the system partition. |
| 3516 | if (!mSystemNativeClient) { |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3517 | mAppOpsManager = std::make_unique<AppOpsManager>(); |
| 3518 | } |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3519 | |
| 3520 | mUidIsTrusted = isTrustedCallingUid(mClientUid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3524 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3525 | mDestructionStarted = true; |
| 3526 | } |
| 3527 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3528 | binder::Status CameraService::BasicClient::disconnect() { |
| 3529 | binder::Status res = Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3530 | if (mDisconnected) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3531 | return res; |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3532 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 3533 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3534 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3535 | sCameraService->removeByClient(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3536 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, String8(mClientPackageName)); |
Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 3537 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, |
| 3538 | mCameraIdStr.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3539 | |
| 3540 | sp<IBinder> remote = getRemote(); |
| 3541 | if (remote != nullptr) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3542 | remote->unlinkToDeath(sCameraService); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3543 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3544 | |
| 3545 | finishCameraOps(); |
Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 3546 | // Notify flashlight that a camera device is closed. |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3547 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); |
| 3548 | ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(), |
| 3549 | mClientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3550 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3551 | // client shouldn't be able to call into us anymore |
| 3552 | mClientPid = 0; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3553 | |
Kunal Malhotra | bfc9605 | 2023-02-28 23:25:34 +0000 | [diff] [blame] | 3554 | const auto& mActivityManager = getActivityManager(); |
| 3555 | if (mActivityManager) { |
| 3556 | mActivityManager->logFgsApiEnd(LOG_FGS_CAMERA_API, |
| 3557 | CameraThreadState::getCallingUid(), |
| 3558 | CameraThreadState::getCallingPid()); |
| 3559 | } |
| 3560 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3561 | return res; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3562 | } |
| 3563 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3564 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 3565 | // No dumping of clients directly over Binder, |
| 3566 | // must go through CameraService::dump |
| 3567 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3568 | CameraThreadState::getCallingUid(), NULL, 0); |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3569 | return OK; |
| 3570 | } |
| 3571 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 3572 | status_t CameraService::BasicClient::startWatchingTags(const String8&, int) { |
| 3573 | // Can't watch tags directly, must go through CameraService::startWatchingTags |
| 3574 | return OK; |
| 3575 | } |
| 3576 | |
| 3577 | status_t CameraService::BasicClient::stopWatchingTags(int) { |
| 3578 | // Can't watch tags directly, must go through CameraService::stopWatchingTags |
| 3579 | return OK; |
| 3580 | } |
| 3581 | |
| 3582 | status_t CameraService::BasicClient::dumpWatchedEventsToVector(std::vector<std::string> &) { |
| 3583 | // Can't watch tags directly, must go through CameraService::dumpWatchedEventsToVector |
| 3584 | return OK; |
| 3585 | } |
| 3586 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3587 | String16 CameraService::BasicClient::getPackageName() const { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3588 | return mClientPackageName; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3589 | } |
| 3590 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3591 | int CameraService::BasicClient::getCameraFacing() const { |
| 3592 | return mCameraFacing; |
| 3593 | } |
| 3594 | |
| 3595 | int CameraService::BasicClient::getCameraOrientation() const { |
| 3596 | return mOrientation; |
| 3597 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3598 | |
| 3599 | int CameraService::BasicClient::getClientPid() const { |
| 3600 | return mClientPid; |
| 3601 | } |
| 3602 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3603 | uid_t CameraService::BasicClient::getClientUid() const { |
| 3604 | return mClientUid; |
| 3605 | } |
| 3606 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3607 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 3608 | // Defaults to API2. |
| 3609 | return level == API_2; |
| 3610 | } |
| 3611 | |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3612 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3613 | { |
| 3614 | Mutex::Autolock l(mAudioRestrictionLock); |
| 3615 | mAudioRestriction = mode; |
| 3616 | } |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3617 | sCameraService->updateAudioRestriction(); |
| 3618 | return OK; |
| 3619 | } |
| 3620 | |
| 3621 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3622 | return sCameraService->updateAudioRestriction(); |
| 3623 | } |
| 3624 | |
| 3625 | int32_t CameraService::BasicClient::getAudioRestriction() const { |
| 3626 | Mutex::Autolock l(mAudioRestrictionLock); |
| 3627 | return mAudioRestriction; |
| 3628 | } |
| 3629 | |
| 3630 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { |
| 3631 | switch (mode) { |
| 3632 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: |
| 3633 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: |
| 3634 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: |
| 3635 | return true; |
| 3636 | default: |
| 3637 | return false; |
| 3638 | } |
| 3639 | } |
| 3640 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3641 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { |
| 3642 | if (mode == AppOpsManager::MODE_ERRORED) { |
| 3643 | ALOGI("Camera %s: Access for \"%s\" has been revoked", |
| 3644 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 3645 | return PERMISSION_DENIED; |
| 3646 | } else if (!mUidIsTrusted && mode == AppOpsManager::MODE_IGNORED) { |
| 3647 | // If the calling Uid is trusted (a native service), the AppOpsManager could |
| 3648 | // return MODE_IGNORED. Do not treat such case as error. |
| 3649 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, |
| 3650 | mClientPackageName); |
| 3651 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3652 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Jyoti Bhayana | 8143e57 | 2023-01-09 08:46:49 -0800 | [diff] [blame] | 3653 | // We don't want to return EACCESS if the CameraPrivacy is enabled. |
| 3654 | // We prefer to successfully open the camera and perform camera muting |
| 3655 | // or blocking in connectHelper as handleAppOpMode can be called before the |
| 3656 | // connection has been fully established and at that time camera muting |
| 3657 | // capabilities are unknown. |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3658 | if (!isUidActive || !isCameraPrivacyEnabled) { |
| 3659 | ALOGI("Camera %s: Access for \"%s\" has been restricted", |
| 3660 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 3661 | // Return the same error as for device policy manager rejection |
| 3662 | return -EACCES; |
| 3663 | } |
| 3664 | } |
| 3665 | return OK; |
| 3666 | } |
| 3667 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3668 | status_t CameraService::BasicClient::startCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3669 | ATRACE_CALL(); |
| 3670 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3671 | { |
| 3672 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3673 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3674 | } |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3675 | if (mAppOpsManager != nullptr) { |
| 3676 | // Notify app ops that the camera is not available |
| 3677 | mOpsCallback = new OpsCallback(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3678 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 3679 | mClientPackageName, mOpsCallback); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3680 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3681 | // Just check for camera acccess here on open - delay startOp until |
| 3682 | // camera frames start streaming in startCameraStreamingOps |
| 3683 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3684 | mClientPackageName); |
| 3685 | status_t res = handleAppOpMode(mode); |
| 3686 | if (res != OK) { |
| 3687 | return res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3688 | } |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 3689 | } |
| 3690 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3691 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3692 | |
| 3693 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3694 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3695 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3696 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid, /*openCamera*/true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3697 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3698 | // Notify listeners of camera open/close status |
| 3699 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); |
| 3700 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3701 | return OK; |
| 3702 | } |
| 3703 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3704 | status_t CameraService::BasicClient::startCameraStreamingOps() { |
| 3705 | ATRACE_CALL(); |
| 3706 | |
| 3707 | if (!mOpsActive) { |
| 3708 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3709 | return INVALID_OPERATION; |
| 3710 | } |
| 3711 | if (mOpsStreaming) { |
| 3712 | ALOGV("%s: Streaming already active!", __FUNCTION__); |
| 3713 | return OK; |
| 3714 | } |
| 3715 | |
| 3716 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", |
| 3717 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3718 | |
| 3719 | if (mAppOpsManager != nullptr) { |
| 3720 | int32_t mode = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, |
| 3721 | mClientPackageName, /*startIfModeDefault*/ false, mClientFeatureId, |
| 3722 | String16("start camera ") + String16(mCameraIdStr)); |
| 3723 | status_t res = handleAppOpMode(mode); |
| 3724 | if (res != OK) { |
| 3725 | return res; |
| 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | mOpsStreaming = true; |
| 3730 | |
| 3731 | return OK; |
| 3732 | } |
| 3733 | |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 3734 | status_t CameraService::BasicClient::noteAppOp() { |
| 3735 | ATRACE_CALL(); |
| 3736 | |
| 3737 | ALOGV("%s: Start camera noteAppOp, package name = %s, client UID = %d", |
| 3738 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3739 | |
| 3740 | // noteAppOp is only used for when camera mute is not supported, in order |
| 3741 | // to trigger the sensor privacy "Unblock" dialog |
| 3742 | if (mAppOpsManager != nullptr) { |
| 3743 | int32_t mode = mAppOpsManager->noteOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3744 | mClientPackageName, mClientFeatureId, |
| 3745 | String16("start camera ") + String16(mCameraIdStr)); |
| 3746 | status_t res = handleAppOpMode(mode); |
| 3747 | if (res != OK) { |
| 3748 | return res; |
| 3749 | } |
| 3750 | } |
| 3751 | |
| 3752 | return OK; |
| 3753 | } |
| 3754 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3755 | status_t CameraService::BasicClient::finishCameraStreamingOps() { |
| 3756 | ATRACE_CALL(); |
| 3757 | |
| 3758 | if (!mOpsActive) { |
| 3759 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3760 | return INVALID_OPERATION; |
| 3761 | } |
| 3762 | if (!mOpsStreaming) { |
| 3763 | ALOGV("%s: Streaming not active!", __FUNCTION__); |
| 3764 | return OK; |
| 3765 | } |
| 3766 | |
| 3767 | if (mAppOpsManager != nullptr) { |
| 3768 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3769 | mClientPackageName, mClientFeatureId); |
| 3770 | mOpsStreaming = false; |
| 3771 | } |
| 3772 | |
| 3773 | return OK; |
| 3774 | } |
| 3775 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3776 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3777 | ATRACE_CALL(); |
| 3778 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3779 | if (mOpsStreaming) { |
| 3780 | // Make sure we've notified everyone about camera stopping |
| 3781 | finishCameraStreamingOps(); |
| 3782 | } |
| 3783 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3784 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3785 | if (mOpsActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3786 | mOpsActive = false; |
| 3787 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3788 | // This function is called when a client disconnects. This should |
| 3789 | // release the camera, but actually only if it was in a proper |
| 3790 | // functional state, i.e. with status NOT_AVAILABLE |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3791 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3792 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3793 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3794 | // 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] | 3795 | sCameraService->updateStatus(StatusInternal::PRESENT, |
| 3796 | mCameraIdStr, rejected); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3797 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3798 | // Always stop watching, even if no camera op is active |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3799 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 3800 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3801 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3802 | mOpsCallback.clear(); |
| 3803 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 3804 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid, /*closeCamera*/true); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3805 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3806 | // Notify listeners of camera open/close status |
| 3807 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); |
| 3808 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3809 | return OK; |
| 3810 | } |
| 3811 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3812 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3813 | ATRACE_CALL(); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3814 | if (mAppOpsManager == nullptr) { |
| 3815 | return; |
| 3816 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3817 | // TODO : add offline camera session case |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3818 | if (op != AppOpsManager::OP_CAMERA) { |
| 3819 | ALOGW("Unexpected app ops notification received: %d", op); |
| 3820 | return; |
| 3821 | } |
| 3822 | |
| 3823 | int32_t res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3824 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3825 | mClientUid, mClientPackageName); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3826 | ALOGV("checkOp returns: %d, %s ", res, |
| 3827 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 3828 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 3829 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 3830 | "UNKNOWN"); |
| 3831 | |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3832 | if (res == AppOpsManager::MODE_ERRORED) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3833 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(), |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3834 | String8(mClientPackageName).string()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3835 | block(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3836 | } else if (res == AppOpsManager::MODE_IGNORED) { |
| 3837 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3838 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3839 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3840 | ALOGI("Camera %s: Access for \"%s\" has been restricted, isUidTrusted %d, isUidActive %d", |
| 3841 | mCameraIdStr.string(), String8(mClientPackageName).string(), |
| 3842 | mUidIsTrusted, isUidActive); |
| 3843 | // If the calling Uid is trusted (a native service), or the client Uid is active (WAR for |
| 3844 | // b/175320666), the AppOpsManager could return MODE_IGNORED. Do not treat such cases as |
| 3845 | // error. |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 3846 | if (!mUidIsTrusted) { |
| 3847 | if (isUidActive && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 3848 | setCameraMute(true); |
| 3849 | } else if (!isUidActive |
| 3850 | || (isCameraPrivacyEnabled && !supportsCameraMute())) { |
| 3851 | block(); |
| 3852 | } |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3853 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3854 | } else if (res == AppOpsManager::MODE_ALLOWED) { |
| 3855 | setCameraMute(sCameraService->mOverrideCameraMuteMode); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3856 | } |
| 3857 | } |
| 3858 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3859 | void CameraService::BasicClient::block() { |
| 3860 | ATRACE_CALL(); |
| 3861 | |
| 3862 | // Reset the client PID to allow server-initiated disconnect, |
| 3863 | // and to prevent further calls by client. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3864 | mClientPid = CameraThreadState::getCallingPid(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3865 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 3866 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); |
| 3867 | disconnect(); |
| 3868 | } |
| 3869 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3870 | // ---------------------------------------------------------------------------- |
| 3871 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3872 | void CameraService::Client::notifyError(int32_t errorCode, |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 3873 | [[maybe_unused]] const CaptureResultExtras& resultExtras) { |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3874 | if (mRemoteCallback != NULL) { |
Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 3875 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; |
| 3876 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { |
| 3877 | api1ErrorCode = CAMERA_ERROR_DISABLED; |
| 3878 | } |
| 3879 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3880 | } else { |
| 3881 | ALOGE("mRemoteCallback is NULL!!"); |
| 3882 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3883 | } |
| 3884 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3885 | // NOTE: function is idempotent |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3886 | binder::Status CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3887 | ALOGV("Client::disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3888 | return BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 3889 | } |
| 3890 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3891 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 3892 | return level == API_1; |
| 3893 | } |
| 3894 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3895 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 3896 | mClient(client) { |
| 3897 | } |
| 3898 | |
| 3899 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 3900 | const String16& packageName) { |
| 3901 | sp<BasicClient> client = mClient.promote(); |
| 3902 | if (client != NULL) { |
| 3903 | client->opChanged(op, packageName); |
| 3904 | } |
| 3905 | } |
| 3906 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3907 | // ---------------------------------------------------------------------------- |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3908 | // UidPolicy |
| 3909 | // ---------------------------------------------------------------------------- |
| 3910 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 3911 | void CameraService::UidPolicy::registerWithActivityManager() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3912 | Mutex::Autolock _l(mUidLock); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 3913 | int32_t emptyUidArray[] = { }; |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3914 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3915 | if (mRegistered) return; |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3916 | status_t res = mAm.linkToDeath(this); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 3917 | mAm.registerUidObserverForUids(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3918 | | ActivityManager::UID_OBSERVER_IDLE |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3919 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE |
| 3920 | | ActivityManager::UID_OBSERVER_PROC_OOM_ADJ, |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3921 | ActivityManager::PROCESS_STATE_UNKNOWN, |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 3922 | String16("cameraserver"), emptyUidArray, 0, mObserverToken); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3923 | if (res == OK) { |
| 3924 | mRegistered = true; |
| 3925 | ALOGV("UidPolicy: Registered with ActivityManager"); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 3926 | } else { |
| 3927 | ALOGE("UidPolicy: Failed to register with ActivityManager: 0x%08x", res); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3928 | } |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3929 | } |
| 3930 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 3931 | void CameraService::UidPolicy::onServiceRegistration(const String16& name, const sp<IBinder>&) { |
| 3932 | if (name != String16(kActivityServiceName)) { |
| 3933 | return; |
| 3934 | } |
| 3935 | |
| 3936 | registerWithActivityManager(); |
| 3937 | } |
| 3938 | |
| 3939 | void CameraService::UidPolicy::registerSelf() { |
| 3940 | // Use check service to see if the activity service is available |
| 3941 | // If not available then register for notifications, instead of blocking |
| 3942 | // till the service is ready |
| 3943 | sp<IServiceManager> sm = defaultServiceManager(); |
| 3944 | sp<IBinder> binder = sm->checkService(String16(kActivityServiceName)); |
| 3945 | if (!binder) { |
| 3946 | sm->registerForNotifications(String16(kActivityServiceName), this); |
| 3947 | } else { |
| 3948 | registerWithActivityManager(); |
| 3949 | } |
| 3950 | } |
| 3951 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3952 | void CameraService::UidPolicy::unregisterSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3953 | Mutex::Autolock _l(mUidLock); |
| 3954 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3955 | mAm.unregisterUidObserver(this); |
| 3956 | mAm.unlinkToDeath(this); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3957 | mRegistered = false; |
| 3958 | mActiveUids.clear(); |
| 3959 | ALOGV("UidPolicy: Unregistered with ActivityManager"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { |
| 3963 | onUidIdle(uid, disabled); |
| 3964 | } |
| 3965 | |
| 3966 | void CameraService::UidPolicy::onUidActive(uid_t uid) { |
| 3967 | Mutex::Autolock _l(mUidLock); |
| 3968 | mActiveUids.insert(uid); |
| 3969 | } |
| 3970 | |
| 3971 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { |
| 3972 | bool deleted = false; |
| 3973 | { |
| 3974 | Mutex::Autolock _l(mUidLock); |
| 3975 | if (mActiveUids.erase(uid) > 0) { |
| 3976 | deleted = true; |
| 3977 | } |
| 3978 | } |
| 3979 | if (deleted) { |
| 3980 | sp<CameraService> service = mService.promote(); |
| 3981 | if (service != nullptr) { |
| 3982 | service->blockClientsForUid(uid); |
| 3983 | } |
| 3984 | } |
| 3985 | } |
| 3986 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3987 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 3988 | int64_t procStateSeq __unused, int32_t capability __unused) { |
Austin Borger | c5585dc | 2022-05-12 00:48:17 +0000 | [diff] [blame] | 3989 | bool procStateChange = false; |
| 3990 | { |
| 3991 | Mutex::Autolock _l(mUidLock); |
| 3992 | if (mMonitoredUids.find(uid) != mMonitoredUids.end() && |
| 3993 | mMonitoredUids[uid].procState != procState) { |
| 3994 | mMonitoredUids[uid].procState = procState; |
| 3995 | procStateChange = true; |
| 3996 | } |
| 3997 | } |
| 3998 | |
| 3999 | if (procStateChange) { |
| 4000 | sp<CameraService> service = mService.promote(); |
| 4001 | if (service != nullptr) { |
| 4002 | service->notifyMonitoredUids(); |
| 4003 | } |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4004 | } |
| 4005 | } |
| 4006 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4007 | /** |
| 4008 | * When the OOM adj of the uid owning the camera changes, a different uid waiting on camera |
| 4009 | * privileges may take precedence if the owner's new OOM adj is greater than the waiting package. |
| 4010 | * Here, we track which monitoredUid has the camera, and track its adj relative to other |
| 4011 | * monitoredUids. If it is revised above some other monitoredUid, signal |
| 4012 | * onCameraAccessPrioritiesChanged. This only needs to capture the case where there are two |
| 4013 | * foreground apps in split screen - state changes will capture all other cases. |
| 4014 | */ |
| 4015 | void CameraService::UidPolicy::onUidProcAdjChanged(uid_t uid, int32_t adj) { |
| 4016 | std::unordered_set<uid_t> notifyUidSet; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4017 | { |
| 4018 | Mutex::Autolock _l(mUidLock); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4019 | auto it = mMonitoredUids.find(uid); |
| 4020 | |
| 4021 | if (it != mMonitoredUids.end()) { |
| 4022 | if (it->second.hasCamera) { |
| 4023 | for (auto &monitoredUid : mMonitoredUids) { |
| 4024 | if (monitoredUid.first != uid && adj > monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4025 | ALOGV("%s: notify uid %d", __FUNCTION__, monitoredUid.first); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4026 | notifyUidSet.emplace(monitoredUid.first); |
| 4027 | } |
| 4028 | } |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4029 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4030 | notifyUidSet.emplace(uid); |
| 4031 | } else { |
| 4032 | for (auto &monitoredUid : mMonitoredUids) { |
| 4033 | if (monitoredUid.second.hasCamera && adj < monitoredUid.second.procAdj) { |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4034 | ALOGV("%s: notify uid %d", __FUNCTION__, uid); |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4035 | notifyUidSet.emplace(uid); |
| 4036 | } |
| 4037 | } |
| 4038 | } |
| 4039 | it->second.procAdj = adj; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4040 | } |
| 4041 | } |
| 4042 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4043 | if (notifyUidSet.size() > 0) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4044 | sp<CameraService> service = mService.promote(); |
| 4045 | if (service != nullptr) { |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4046 | service->notifyMonitoredUids(notifyUidSet); |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4047 | } |
| 4048 | } |
| 4049 | } |
| 4050 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4051 | /** |
| 4052 | * Register a uid for monitoring, and note whether it owns a camera. |
| 4053 | */ |
| 4054 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid, bool openCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4055 | Mutex::Autolock _l(mUidLock); |
| 4056 | auto it = mMonitoredUids.find(uid); |
| 4057 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4058 | it->second.refCount++; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4059 | } else { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4060 | MonitoredUid monitoredUid; |
| 4061 | monitoredUid.procState = ActivityManager::PROCESS_STATE_NONEXISTENT; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4062 | monitoredUid.procAdj = resource_policy::UNKNOWN_ADJ; |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4063 | monitoredUid.refCount = 1; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4064 | it = mMonitoredUids.emplace(std::pair<uid_t, MonitoredUid>(uid, monitoredUid)).first; |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4065 | status_t res = mAm.addUidToObserver(mObserverToken, String16("cameraserver"), uid); |
| 4066 | if (res != OK) { |
| 4067 | ALOGE("UidPolicy: Failed to add uid to observer: 0x%08x", res); |
| 4068 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | if (openCamera) { |
| 4072 | it->second.hasCamera = true; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4073 | } |
| 4074 | } |
| 4075 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4076 | /** |
| 4077 | * Unregister a uid for monitoring, and note whether it lost ownership of a camera. |
| 4078 | */ |
| 4079 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid, bool closeCamera) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4080 | Mutex::Autolock _l(mUidLock); |
| 4081 | auto it = mMonitoredUids.find(uid); |
| 4082 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4083 | it->second.refCount--; |
| 4084 | if (it->second.refCount == 0) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4085 | mMonitoredUids.erase(it); |
Austin Borger | d0309d4 | 2023-04-21 20:07:18 -0700 | [diff] [blame] | 4086 | status_t res = mAm.removeUidFromObserver(mObserverToken, String16("cameraserver"), uid); |
| 4087 | if (res != OK) { |
| 4088 | ALOGE("UidPolicy: Failed to remove uid from observer: 0x%08x", res); |
| 4089 | } |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 4090 | } else if (closeCamera) { |
| 4091 | it->second.hasCamera = false; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4092 | } |
| 4093 | } else { |
| 4094 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 4095 | } |
| 4096 | } |
| 4097 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4098 | bool CameraService::UidPolicy::isUidActive(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4099 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4100 | return isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4101 | } |
| 4102 | |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4103 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; |
| 4104 | static const int64_t kPollUidActiveTimeoutMillis = 50; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4105 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4106 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4107 | // Non-app UIDs are considered always active |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4108 | // If activity manager is unreachable, assume everything is active |
| 4109 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4110 | return true; |
| 4111 | } |
| 4112 | auto it = mOverrideUids.find(uid); |
| 4113 | if (it != mOverrideUids.end()) { |
| 4114 | return it->second; |
| 4115 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4116 | bool active = mActiveUids.find(uid) != mActiveUids.end(); |
| 4117 | if (!active) { |
| 4118 | // We want active UIDs to always access camera with their first attempt since |
| 4119 | // there is no guarantee the app is robustly written and would retry getting |
| 4120 | // the camera on failure. The inverse case is not a problem as we would take |
| 4121 | // camera away soon once we get the callback that the uid is no longer active. |
| 4122 | ActivityManager am; |
| 4123 | // Okay to access with a lock held as UID changes are dispatched without |
| 4124 | // a lock and we are a higher level component. |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4125 | int64_t startTimeMillis = 0; |
| 4126 | do { |
| 4127 | // TODO: Fix this b/109950150! |
| 4128 | // Okay this is a hack. There is a race between the UID turning active and |
| 4129 | // activity being resumed. The proper fix is very risky, so we temporary add |
| 4130 | // some polling which should happen pretty rarely anyway as the race is hard |
| 4131 | // to hit. |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4132 | active = mActiveUids.find(uid) != mActiveUids.end(); |
Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 4133 | if (!active) active = am.isUidActive(uid, callingPackage); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4134 | if (active) { |
| 4135 | break; |
| 4136 | } |
| 4137 | if (startTimeMillis <= 0) { |
| 4138 | startTimeMillis = uptimeMillis(); |
| 4139 | } |
| 4140 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4141 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4142 | if (remainingTimeMillis <= 0) { |
| 4143 | break; |
| 4144 | } |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4145 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); |
| 4146 | |
| 4147 | mUidLock.unlock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4148 | usleep(remainingTimeMillis * 1000); |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 4149 | mUidLock.lock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 4150 | } while (true); |
| 4151 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4152 | if (active) { |
| 4153 | // Now that we found out the UID is actually active, cache that |
| 4154 | mActiveUids.insert(uid); |
| 4155 | } |
| 4156 | } |
| 4157 | return active; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4158 | } |
| 4159 | |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4160 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { |
| 4161 | Mutex::Autolock _l(mUidLock); |
| 4162 | return getProcStateLocked(uid); |
| 4163 | } |
| 4164 | |
| 4165 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { |
| 4166 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 4167 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 4168 | procState = mMonitoredUids[uid].procState; |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 4169 | } |
| 4170 | return procState; |
| 4171 | } |
| 4172 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4173 | void CameraService::UidPolicy::UidPolicy::addOverrideUid(uid_t uid, |
| 4174 | String16 callingPackage, bool active) { |
| 4175 | updateOverrideUid(uid, callingPackage, active, true); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4176 | } |
| 4177 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4178 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, String16 callingPackage) { |
| 4179 | updateOverrideUid(uid, callingPackage, false, false); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4180 | } |
| 4181 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 4182 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 4183 | Mutex::Autolock _l(mUidLock); |
| 4184 | ALOGV("UidPolicy: ActivityManager has died"); |
| 4185 | mRegistered = false; |
| 4186 | mActiveUids.clear(); |
| 4187 | } |
| 4188 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4189 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, String16 callingPackage, |
| 4190 | bool active, bool insert) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4191 | bool wasActive = false; |
| 4192 | bool isActive = false; |
| 4193 | { |
| 4194 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4195 | wasActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4196 | mOverrideUids.erase(uid); |
| 4197 | if (insert) { |
| 4198 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 4199 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4200 | isActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4201 | } |
| 4202 | if (wasActive != isActive && !isActive) { |
| 4203 | sp<CameraService> service = mService.promote(); |
| 4204 | if (service != nullptr) { |
| 4205 | service->blockClientsForUid(uid); |
| 4206 | } |
| 4207 | } |
| 4208 | } |
| 4209 | |
| 4210 | // ---------------------------------------------------------------------------- |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4211 | // SensorPrivacyPolicy |
| 4212 | // ---------------------------------------------------------------------------- |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4213 | |
| 4214 | void CameraService::SensorPrivacyPolicy::registerWithSensorPrivacyManager() |
| 4215 | { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4216 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4217 | if (mRegistered) { |
| 4218 | return; |
| 4219 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4220 | hasCameraPrivacyFeature(); // Called so the result is cached |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4221 | mSpm.addSensorPrivacyListener(this); |
| 4222 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); |
| 4223 | status_t res = mSpm.linkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4224 | if (res == OK) { |
| 4225 | mRegistered = true; |
| 4226 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); |
| 4227 | } |
| 4228 | } |
| 4229 | |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4230 | void CameraService::SensorPrivacyPolicy::onServiceRegistration(const String16& name, |
| 4231 | const sp<IBinder>&) { |
| 4232 | if (name != String16(kSensorPrivacyServiceName)) { |
| 4233 | return; |
| 4234 | } |
| 4235 | |
| 4236 | registerWithSensorPrivacyManager(); |
| 4237 | } |
| 4238 | |
| 4239 | void CameraService::SensorPrivacyPolicy::registerSelf() { |
| 4240 | // Use checkservice to see if the sensor_privacy service is available |
| 4241 | // If service is not available then register for notification |
| 4242 | sp<IServiceManager> sm = defaultServiceManager(); |
| 4243 | sp<IBinder> binder = sm->checkService(String16(kSensorPrivacyServiceName)); |
| 4244 | if (!binder) { |
| 4245 | sm->registerForNotifications(String16(kSensorPrivacyServiceName),this); |
| 4246 | } else { |
| 4247 | registerWithSensorPrivacyManager(); |
| 4248 | } |
| 4249 | } |
| 4250 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4251 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { |
| 4252 | Mutex::Autolock _l(mSensorPrivacyLock); |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4253 | mSpm.removeSensorPrivacyListener(this); |
| 4254 | mSpm.unlinkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4255 | mRegistered = false; |
| 4256 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); |
| 4257 | } |
| 4258 | |
| 4259 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
Jyoti Bhayana | cde601c | 2022-12-07 10:03:42 -0800 | [diff] [blame] | 4260 | if (!mRegistered) { |
| 4261 | registerWithSensorPrivacyManager(); |
| 4262 | } |
| 4263 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4264 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4265 | return mSensorPrivacyEnabled; |
| 4266 | } |
| 4267 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4268 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled() { |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4269 | if (!hasCameraPrivacyFeature()) { |
| 4270 | return false; |
| 4271 | } |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4272 | return mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4273 | } |
| 4274 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4275 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged( |
| 4276 | int toggleType __unused, int sensor __unused, bool enabled) { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4277 | { |
| 4278 | Mutex::Autolock _l(mSensorPrivacyLock); |
Kwangkyu Park | e3af299 | 2022-11-05 02:46:52 +0900 | [diff] [blame] | 4279 | mSensorPrivacyEnabled = enabled; |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4280 | } |
| 4281 | // 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] | 4282 | if (enabled) { |
| 4283 | sp<CameraService> service = mService.promote(); |
| 4284 | if (service != nullptr) { |
| 4285 | service->blockAllClients(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4286 | } |
| 4287 | } |
| 4288 | return binder::Status::ok(); |
| 4289 | } |
| 4290 | |
| 4291 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 4292 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4293 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); |
| 4294 | mRegistered = false; |
| 4295 | } |
| 4296 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4297 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4298 | bool supportsSoftwareToggle = mSpm.supportsSensorToggle( |
| 4299 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 4300 | bool supportsHardwareToggle = mSpm.supportsSensorToggle( |
| 4301 | SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 4302 | return supportsSoftwareToggle || supportsHardwareToggle; |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4303 | } |
| 4304 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4305 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4306 | // CameraState |
| 4307 | // ---------------------------------------------------------------------------- |
| 4308 | |
| 4309 | CameraService::CameraState::CameraState(const String8& id, int cost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4310 | const std::set<String8>& conflicting, SystemCameraKind systemCameraKind, |
| 4311 | const std::vector<std::string>& physicalCameras) : mId(id), |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4312 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4313 | mSystemCameraKind(systemCameraKind), mPhysicalCameras(physicalCameras) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4314 | |
| 4315 | CameraService::CameraState::~CameraState() {} |
| 4316 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4317 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4318 | Mutex::Autolock lock(mStatusLock); |
| 4319 | return mStatus; |
| 4320 | } |
| 4321 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4322 | std::vector<String8> CameraService::CameraState::getUnavailablePhysicalIds() const { |
| 4323 | Mutex::Autolock lock(mStatusLock); |
| 4324 | std::vector<String8> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); |
| 4325 | return res; |
| 4326 | } |
| 4327 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4328 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 4329 | return mShimParams; |
| 4330 | } |
| 4331 | |
| 4332 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 4333 | mShimParams = params; |
| 4334 | } |
| 4335 | |
| 4336 | int CameraService::CameraState::getCost() const { |
| 4337 | return mCost; |
| 4338 | } |
| 4339 | |
| 4340 | std::set<String8> CameraService::CameraState::getConflicting() const { |
| 4341 | return mConflicting; |
| 4342 | } |
| 4343 | |
| 4344 | String8 CameraService::CameraState::getId() const { |
| 4345 | return mId; |
| 4346 | } |
| 4347 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4348 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { |
| 4349 | return mSystemCameraKind; |
| 4350 | } |
| 4351 | |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4352 | bool CameraService::CameraState::containsPhysicalCamera(const std::string& physicalCameraId) const { |
| 4353 | return std::find(mPhysicalCameras.begin(), mPhysicalCameras.end(), physicalCameraId) |
| 4354 | != mPhysicalCameras.end(); |
| 4355 | } |
| 4356 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4357 | bool CameraService::CameraState::addUnavailablePhysicalId(const String8& physicalId) { |
| 4358 | Mutex::Autolock lock(mStatusLock); |
| 4359 | auto result = mUnavailablePhysicalIds.insert(physicalId); |
| 4360 | return result.second; |
| 4361 | } |
| 4362 | |
| 4363 | bool CameraService::CameraState::removeUnavailablePhysicalId(const String8& physicalId) { |
| 4364 | Mutex::Autolock lock(mStatusLock); |
| 4365 | auto count = mUnavailablePhysicalIds.erase(physicalId); |
| 4366 | return count > 0; |
| 4367 | } |
| 4368 | |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 4369 | void CameraService::CameraState::setClientPackage(const String8& clientPackage) { |
| 4370 | Mutex::Autolock lock(mStatusLock); |
| 4371 | mClientPackage = clientPackage; |
| 4372 | } |
| 4373 | |
| 4374 | String8 CameraService::CameraState::getClientPackage() const { |
| 4375 | Mutex::Autolock lock(mStatusLock); |
| 4376 | return mClientPackage; |
| 4377 | } |
| 4378 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4379 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4380 | // ClientEventListener |
| 4381 | // ---------------------------------------------------------------------------- |
| 4382 | |
| 4383 | void CameraService::ClientEventListener::onClientAdded( |
| 4384 | const resource_policy::ClientDescriptor<String8, |
| 4385 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 4386 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4387 | if (basicClient.get() != nullptr) { |
| 4388 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 4389 | notifier.noteStartCamera(descriptor.getKey(), |
| 4390 | static_cast<int>(basicClient->getClientUid())); |
| 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | void CameraService::ClientEventListener::onClientRemoved( |
| 4395 | const resource_policy::ClientDescriptor<String8, |
| 4396 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 4397 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4398 | if (basicClient.get() != nullptr) { |
| 4399 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 4400 | notifier.noteStopCamera(descriptor.getKey(), |
| 4401 | static_cast<int>(basicClient->getClientUid())); |
| 4402 | } |
| 4403 | } |
| 4404 | |
| 4405 | |
| 4406 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4407 | // CameraClientManager |
| 4408 | // ---------------------------------------------------------------------------- |
| 4409 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4410 | CameraService::CameraClientManager::CameraClientManager() { |
| 4411 | setListener(std::make_shared<ClientEventListener>()); |
| 4412 | } |
| 4413 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4414 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 4415 | |
| 4416 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
| 4417 | const String8& id) const { |
| 4418 | auto descriptor = get(id); |
| 4419 | if (descriptor == nullptr) { |
| 4420 | return sp<BasicClient>{nullptr}; |
| 4421 | } |
| 4422 | return descriptor->getValue(); |
| 4423 | } |
| 4424 | |
| 4425 | String8 CameraService::CameraClientManager::toString() const { |
| 4426 | auto all = getAll(); |
| 4427 | String8 ret("["); |
| 4428 | bool hasAny = false; |
| 4429 | for (auto& i : all) { |
| 4430 | hasAny = true; |
| 4431 | String8 key = i->getKey(); |
| 4432 | int32_t cost = i->getCost(); |
| 4433 | int32_t pid = i->getOwnerId(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4434 | int32_t score = i->getPriority().getScore(); |
| 4435 | int32_t state = i->getPriority().getState(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4436 | auto conflicting = i->getConflicting(); |
| 4437 | auto clientSp = i->getValue(); |
| 4438 | String8 packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 4439 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4440 | if (clientSp.get() != nullptr) { |
| 4441 | packageName = String8{clientSp->getPackageName()}; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4442 | uid_t clientUid = clientSp->getClientUid(); |
| 4443 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4444 | } |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4445 | ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" |
| 4446 | PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4447 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4448 | if (clientSp.get() != nullptr) { |
| 4449 | ret.appendFormat("User Id: %d, ", clientUserId); |
| 4450 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4451 | if (packageName.size() != 0) { |
| 4452 | ret.appendFormat("Client Package Name: %s", packageName.string()); |
| 4453 | } |
| 4454 | |
| 4455 | ret.append(", Conflicting Client Devices: {"); |
| 4456 | for (auto& j : conflicting) { |
| 4457 | ret.appendFormat("%s, ", j.string()); |
| 4458 | } |
| 4459 | ret.append("})"); |
| 4460 | } |
| 4461 | if (hasAny) ret.append("\n"); |
| 4462 | ret.append("]\n"); |
| 4463 | return ret; |
| 4464 | } |
| 4465 | |
| 4466 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
| 4467 | const String8& key, const sp<BasicClient>& value, int32_t cost, |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4468 | const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4469 | int32_t state, int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4470 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4471 | int32_t score_adj = systemNativeClient ? kSystemNativeClientScore : score; |
| 4472 | int32_t state_adj = systemNativeClient ? kSystemNativeClientState: state; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 4473 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4474 | return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>( |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4475 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, |
| 4476 | systemNativeClient, oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4477 | } |
| 4478 | |
| 4479 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 4480 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4481 | int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4482 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4483 | partial->getConflicting(), partial->getPriority().getScore(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4484 | partial->getOwnerId(), partial->getPriority().getState(), oomScoreOffset, |
| 4485 | systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | // ---------------------------------------------------------------------------- |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4489 | // InjectionStatusListener |
| 4490 | // ---------------------------------------------------------------------------- |
| 4491 | |
| 4492 | void CameraService::InjectionStatusListener::addListener( |
| 4493 | const sp<ICameraInjectionCallback>& callback) { |
| 4494 | Mutex::Autolock lock(mListenerLock); |
| 4495 | if (mCameraInjectionCallback) return; |
| 4496 | status_t res = IInterface::asBinder(callback)->linkToDeath(this); |
| 4497 | if (res == OK) { |
| 4498 | mCameraInjectionCallback = callback; |
| 4499 | } |
| 4500 | } |
| 4501 | |
| 4502 | void CameraService::InjectionStatusListener::removeListener() { |
| 4503 | Mutex::Autolock lock(mListenerLock); |
| 4504 | if (mCameraInjectionCallback == nullptr) { |
| 4505 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 4506 | return; |
| 4507 | } |
| 4508 | IInterface::asBinder(mCameraInjectionCallback)->unlinkToDeath(this); |
| 4509 | mCameraInjectionCallback = nullptr; |
| 4510 | } |
| 4511 | |
| 4512 | void CameraService::InjectionStatusListener::notifyInjectionError( |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4513 | String8 injectedCamId, status_t err) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4514 | if (mCameraInjectionCallback == nullptr) { |
| 4515 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 4516 | return; |
| 4517 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4518 | |
| 4519 | switch (err) { |
| 4520 | case -ENODEV: |
| 4521 | mCameraInjectionCallback->onInjectionError( |
| 4522 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4523 | ALOGE("No camera device with ID \"%s\" currently available!", |
| 4524 | injectedCamId.string()); |
| 4525 | break; |
| 4526 | case -EBUSY: |
| 4527 | mCameraInjectionCallback->onInjectionError( |
| 4528 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4529 | ALOGE("Higher-priority client using camera, ID \"%s\" currently unavailable!", |
| 4530 | injectedCamId.string()); |
| 4531 | break; |
| 4532 | case DEAD_OBJECT: |
| 4533 | mCameraInjectionCallback->onInjectionError( |
| 4534 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4535 | ALOGE("Camera ID \"%s\" object is dead!", |
| 4536 | injectedCamId.string()); |
| 4537 | break; |
| 4538 | case INVALID_OPERATION: |
| 4539 | mCameraInjectionCallback->onInjectionError( |
| 4540 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4541 | ALOGE("Camera ID \"%s\" encountered an operating or internal error!", |
| 4542 | injectedCamId.string()); |
| 4543 | break; |
| 4544 | case UNKNOWN_TRANSACTION: |
| 4545 | mCameraInjectionCallback->onInjectionError( |
| 4546 | ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED); |
| 4547 | ALOGE("Camera ID \"%s\" method doesn't support!", |
| 4548 | injectedCamId.string()); |
| 4549 | break; |
| 4550 | default: |
| 4551 | mCameraInjectionCallback->onInjectionError( |
| 4552 | ICameraInjectionCallback::ERROR_INJECTION_INVALID_ERROR); |
| 4553 | ALOGE("Unexpected error %s (%d) opening camera \"%s\"!", |
| 4554 | strerror(-err), err, injectedCamId.string()); |
| 4555 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | void CameraService::InjectionStatusListener::binderDied( |
| 4559 | const wp<IBinder>& /*who*/) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4560 | ALOGV("InjectionStatusListener: ICameraInjectionCallback has died"); |
| 4561 | auto parent = mParent.promote(); |
| 4562 | if (parent != nullptr) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4563 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 4564 | if (clientDescriptor != nullptr) { |
| 4565 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 4566 | baseClientPtr->stopInjection(); |
| 4567 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 4568 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4569 | } |
| 4570 | } |
| 4571 | |
| 4572 | // ---------------------------------------------------------------------------- |
| 4573 | // CameraInjectionSession |
| 4574 | // ---------------------------------------------------------------------------- |
| 4575 | |
| 4576 | binder::Status CameraService::CameraInjectionSession::stopInjection() { |
| 4577 | Mutex::Autolock lock(mInjectionSessionLock); |
| 4578 | auto parent = mParent.promote(); |
| 4579 | if (parent == nullptr) { |
| 4580 | ALOGE("CameraInjectionSession: Parent is gone"); |
| 4581 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SERVICE, |
| 4582 | "Camera service encountered error"); |
| 4583 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4584 | |
| 4585 | status_t res = NO_ERROR; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4586 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 4587 | if (clientDescriptor != nullptr) { |
| 4588 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 4589 | res = baseClientPtr->stopInjection(); |
| 4590 | if (res != OK) { |
| 4591 | ALOGE("CameraInjectionSession: Failed to stop the injection camera!" |
| 4592 | " ret != NO_ERROR: %d", res); |
| 4593 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SESSION, |
| 4594 | "Camera session encountered error"); |
| 4595 | } |
| 4596 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 4597 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4598 | return binder::Status::ok(); |
| 4599 | } |
| 4600 | |
| 4601 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4602 | |
| 4603 | static const int kDumpLockRetries = 50; |
| 4604 | static const int kDumpLockSleep = 60000; |
| 4605 | |
| 4606 | static bool tryLock(Mutex& mutex) |
| 4607 | { |
| 4608 | bool locked = false; |
| 4609 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 4610 | if (mutex.tryLock() == NO_ERROR) { |
| 4611 | locked = true; |
| 4612 | break; |
| 4613 | } |
| 4614 | usleep(kDumpLockSleep); |
| 4615 | } |
| 4616 | return locked; |
| 4617 | } |
| 4618 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4619 | void CameraService::cacheDump() { |
| 4620 | if (mMemFd != -1) { |
| 4621 | const Vector<String16> args; |
| 4622 | ATRACE_CALL(); |
| 4623 | // Acquiring service lock here will avoid the deadlock since |
| 4624 | // cacheDump will not be called during the second disconnect. |
| 4625 | Mutex::Autolock lock(mServiceLock); |
| 4626 | |
| 4627 | Mutex::Autolock l(mCameraStatesLock); |
| 4628 | // Start collecting the info for open sessions and store it in temp file. |
| 4629 | for (const auto& state : mCameraStates) { |
| 4630 | String8 cameraId = state.first; |
| 4631 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 4632 | if (clientDescriptor != nullptr) { |
| 4633 | dprintf(mMemFd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
| 4634 | // Log the current open session info before device is disconnected. |
| 4635 | dumpOpenSessionClientLogs(mMemFd, args, cameraId); |
| 4636 | } |
| 4637 | } |
| 4638 | } |
| 4639 | } |
| 4640 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4641 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4642 | ATRACE_CALL(); |
| 4643 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4644 | if (checkCallingPermission(sDumpPermission) == false) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4645 | 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] | 4646 | CameraThreadState::getCallingPid(), |
| 4647 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4648 | return NO_ERROR; |
| 4649 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4650 | bool locked = tryLock(mServiceLock); |
| 4651 | // failed to lock - CameraService is probably deadlocked |
| 4652 | if (!locked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4653 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4654 | } |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4655 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4656 | if (!mInitialized) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4657 | dprintf(fd, "!! No camera HAL available !!\n"); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 4658 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4659 | // Dump event log for error information |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4660 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4661 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4662 | if (locked) mServiceLock.unlock(); |
| 4663 | return NO_ERROR; |
| 4664 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4665 | dprintf(fd, "\n== Service global info: ==\n\n"); |
| 4666 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4667 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 4668 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", |
| 4669 | mNormalDeviceIdsWithoutSystemCamera.size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4670 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { |
| 4671 | dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); |
| 4672 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4673 | String8 activeClientString = mActiveClientManager.toString(); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4674 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string()); |
| 4675 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string()); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 4676 | if (mStreamUseCaseOverrides.size() > 0) { |
| 4677 | dprintf(fd, "Active stream use case overrides:"); |
| 4678 | for (int64_t useCaseOverride : mStreamUseCaseOverrides) { |
| 4679 | dprintf(fd, " %" PRId64, useCaseOverride); |
| 4680 | } |
| 4681 | dprintf(fd, "\n"); |
| 4682 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4683 | |
| 4684 | dumpEventLog(fd); |
| 4685 | |
| 4686 | bool stateLocked = tryLock(mCameraStatesLock); |
| 4687 | if (!stateLocked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4688 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4689 | } |
| 4690 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 4691 | int argSize = args.size(); |
| 4692 | for (int i = 0; i < argSize; i++) { |
| 4693 | if (args[i] == TagMonitor::kMonitorOption) { |
| 4694 | if (i + 1 < argSize) { |
| 4695 | mMonitorTags = String8(args[i + 1]); |
| 4696 | } |
| 4697 | break; |
| 4698 | } |
| 4699 | } |
| 4700 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4701 | for (auto& state : mCameraStates) { |
| 4702 | String8 cameraId = state.first; |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4703 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4704 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4705 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4706 | CameraParameters p = state.second->getShimParams(); |
| 4707 | if (!p.isEmpty()) { |
| 4708 | dprintf(fd, " Camera1 API shim is using parameters:\n "); |
| 4709 | p.dump(fd, args); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4713 | if (clientDescriptor != nullptr) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4714 | // log the current open session info |
| 4715 | dumpOpenSessionClientLogs(fd, args, cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4716 | } else { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4717 | dumpClosedSessionClientLogs(fd, cameraId); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4718 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4719 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | if (stateLocked) mCameraStatesLock.unlock(); |
| 4723 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4724 | if (locked) mServiceLock.unlock(); |
| 4725 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 4726 | mCameraProviderManager->dump(fd, args); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4727 | |
| 4728 | dprintf(fd, "\n== Vendor tags: ==\n\n"); |
| 4729 | |
| 4730 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 4731 | if (desc == NULL) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 4732 | sp<VendorTagDescriptorCache> cache = |
| 4733 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 4734 | if (cache == NULL) { |
| 4735 | dprintf(fd, "No vendor tags.\n"); |
| 4736 | } else { |
| 4737 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 4738 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4739 | } else { |
| 4740 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 4741 | } |
| 4742 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4743 | // Dump camera traces if there were any |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4744 | dprintf(fd, "\n"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4745 | camera3::CameraTraces::dump(fd); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4746 | |
| 4747 | // Process dump arguments, if any |
| 4748 | int n = args.size(); |
| 4749 | String16 verboseOption("-v"); |
| 4750 | String16 unreachableOption("--unreachable"); |
| 4751 | for (int i = 0; i < n; i++) { |
| 4752 | if (args[i] == verboseOption) { |
| 4753 | // change logging level |
| 4754 | if (i + 1 >= n) continue; |
| 4755 | String8 levelStr(args[i+1]); |
| 4756 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4757 | dprintf(fd, "\nSetting log level to %d.\n", level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4758 | setLogLevel(level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4759 | } else if (args[i] == unreachableOption) { |
| 4760 | // Dump memory analysis |
| 4761 | // TODO - should limit be an argument parameter? |
| 4762 | UnreachableMemoryInfo info; |
| 4763 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); |
| 4764 | if (!success) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4765 | dprintf(fd, "\n== Unable to dump unreachable memory. " |
| 4766 | "Try disabling SELinux enforcement. ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4767 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4768 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4769 | std::string s = info.ToString(/*log_contents*/ true); |
| 4770 | write(fd, s.c_str(), s.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4771 | } |
| 4772 | } |
| 4773 | } |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4774 | |
| 4775 | bool serviceLocked = tryLock(mServiceLock); |
| 4776 | |
| 4777 | // Dump info from previous open sessions. |
| 4778 | // Reposition the offset to beginning of the file before reading |
| 4779 | |
| 4780 | if ((mMemFd >= 0) && (lseek(mMemFd, 0, SEEK_SET) != -1)) { |
| 4781 | dprintf(fd, "\n**********Dumpsys from previous open session**********\n"); |
| 4782 | ssize_t size_read; |
| 4783 | char buf[4096]; |
| 4784 | while ((size_read = read(mMemFd, buf, (sizeof(buf) - 1))) > 0) { |
| 4785 | // Read data from file to a small buffer and write it to fd. |
| 4786 | write(fd, buf, size_read); |
| 4787 | if (size_read == -1) { |
| 4788 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 4789 | break; |
| 4790 | } |
| 4791 | } |
| 4792 | dprintf(fd, "\n**********End of Dumpsys from previous open session**********\n"); |
| 4793 | } else { |
| 4794 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 4795 | } |
| 4796 | |
| 4797 | if (serviceLocked) mServiceLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4798 | return NO_ERROR; |
| 4799 | } |
| 4800 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4801 | void CameraService::dumpOpenSessionClientLogs(int fd, |
| 4802 | const Vector<String16>& args, const String8& cameraId) { |
| 4803 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Rucha Katakwar | 71a0e05 | 2022-04-07 15:44:18 -0700 | [diff] [blame] | 4804 | dprintf(fd, " %s : Device %s is open. Client instance dump:\n", |
| 4805 | getFormattedCurrentTime().string(), |
| 4806 | cameraId.string()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4807 | dprintf(fd, " Client priority score: %d state: %d\n", |
| 4808 | clientDescriptor->getPriority().getScore(), |
| 4809 | clientDescriptor->getPriority().getState()); |
| 4810 | dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 4811 | |
| 4812 | auto client = clientDescriptor->getValue(); |
| 4813 | dprintf(fd, " Client package: %s\n", |
| 4814 | String8(client->getPackageName()).string()); |
| 4815 | |
| 4816 | client->dumpClient(fd, args); |
| 4817 | } |
| 4818 | |
| 4819 | void CameraService::dumpClosedSessionClientLogs(int fd, const String8& cameraId) { |
| 4820 | dprintf(fd, " Device %s is closed, no client instance\n", |
| 4821 | cameraId.string()); |
| 4822 | } |
| 4823 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4824 | void CameraService::dumpEventLog(int fd) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4825 | 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] | 4826 | |
| 4827 | Mutex::Autolock l(mLogLock); |
| 4828 | for (const auto& msg : mEventLog) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4829 | dprintf(fd, " %s\n", msg.string()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4833 | dprintf(fd, " ...\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4834 | } else if (mEventLog.size() == 0) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4835 | dprintf(fd, " [no events yet]\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4836 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4837 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4838 | } |
| 4839 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4840 | void CameraService::cacheClientTagDumpIfNeeded(const char *cameraId, BasicClient* client) { |
| 4841 | Mutex::Autolock lock(mLogLock); |
| 4842 | if (!isClientWatchedLocked(client)) { return; } |
| 4843 | |
| 4844 | std::vector<std::string> dumpVector; |
| 4845 | client->dumpWatchedEventsToVector(dumpVector); |
| 4846 | |
| 4847 | if (dumpVector.empty()) { return; } |
| 4848 | |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 4849 | std::string dumpString; |
| 4850 | |
| 4851 | String8 currentTime = getFormattedCurrentTime(); |
| 4852 | dumpString += "Cached @ "; |
| 4853 | dumpString += currentTime.string(); |
| 4854 | dumpString += "\n"; // First line is the timestamp of when client is cached. |
| 4855 | |
| 4856 | |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4857 | const String16 &packageName = client->getPackageName(); |
| 4858 | |
| 4859 | String8 packageName8 = String8(packageName); |
| 4860 | const char *printablePackageName = packageName8.lockBuffer(packageName.size()); |
| 4861 | |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 4862 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4863 | size_t i = dumpVector.size(); |
| 4864 | |
| 4865 | // Store the string in reverse order (latest last) |
| 4866 | while (i > 0) { |
| 4867 | i--; |
| 4868 | dumpString += cameraId; |
| 4869 | dumpString += ":"; |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4870 | dumpString += printablePackageName; |
| 4871 | dumpString += " "; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4872 | dumpString += dumpVector[i]; // implicitly ends with '\n' |
| 4873 | } |
| 4874 | |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4875 | packageName8.unlockBuffer(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4876 | mWatchedClientsDumpCache[packageName] = dumpString; |
| 4877 | } |
| 4878 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4879 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4880 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4881 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 4882 | if (mTorchClientMap[i] == who) { |
| 4883 | // 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] | 4884 | String8 cameraId = mTorchClientMap.keyAt(i); |
| 4885 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 4886 | if (res) { |
| 4887 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 4888 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 4889 | return; |
| 4890 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4891 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4892 | break; |
| 4893 | } |
| 4894 | } |
| 4895 | } |
| 4896 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4897 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 4898 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 4899 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4900 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 4901 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 4902 | */ |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 4903 | // PID here is approximate and can be wrong. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 4904 | logClientDied(CameraThreadState::getCallingPid(), String8("Binder died unexpectedly")); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4905 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4906 | // check torch client |
| 4907 | handleTorchClientBinderDied(who); |
| 4908 | |
| 4909 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4910 | if(!evictClientIdByRemote(who)) { |
| 4911 | 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] | 4912 | return; |
| 4913 | } |
| 4914 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4915 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 4916 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 4917 | } |
| 4918 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4919 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4920 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 4921 | } |
| 4922 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4923 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId, |
| 4924 | std::initializer_list<StatusInternal> rejectSourceStates) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4925 | // Do not lock mServiceLock here or can get into a deadlock from |
| 4926 | // connect() -> disconnect -> updateStatus |
| 4927 | |
| 4928 | auto state = getCameraState(cameraId); |
| 4929 | |
| 4930 | if (state == nullptr) { |
| 4931 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 4932 | cameraId.string()); |
| 4933 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4934 | } |
| 4935 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4936 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 4937 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 4938 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 4939 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 4940 | return; |
| 4941 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4942 | |
| 4943 | // Collect the logical cameras without holding mStatusLock in updateStatus |
| 4944 | // as that can lead to a deadlock(b/162192331). |
| 4945 | auto logicalCameraIds = getLogicalCameras(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4946 | // 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] | 4947 | // of the listeners with both the mStatusLock and mStatusListenerLock held |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 4948 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4949 | &logicalCameraIds] |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4950 | (const String8& cameraId, StatusInternal status) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4951 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4952 | if (status != StatusInternal::ENUMERATING) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4953 | // Update torch status if it has a flash unit. |
| 4954 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4955 | TorchModeStatus torchStatus; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4956 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 4957 | NAME_NOT_FOUND) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4958 | TorchModeStatus newTorchStatus = |
| 4959 | status == StatusInternal::PRESENT ? |
| 4960 | TorchModeStatus::AVAILABLE_OFF : |
| 4961 | TorchModeStatus::NOT_AVAILABLE; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4962 | if (torchStatus != newTorchStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 4963 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4964 | } |
| 4965 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4966 | } |
| 4967 | |
| 4968 | Mutex::Autolock lock(mStatusListenerLock); |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4969 | notifyPhysicalCameraStatusLocked(mapToInterface(status), String16(cameraId), |
| 4970 | logicalCameraIds, deviceKind); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4971 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4972 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 4973 | bool isVendorListener = listener->isVendorListener(); |
| 4974 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, |
| 4975 | listener->getListenerPid(), listener->getListenerUid()) || |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 4976 | isVendorListener) { |
| 4977 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4978 | cameraId.c_str()); |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 4979 | continue; |
| 4980 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4981 | auto ret = listener->getListener()->onStatusChanged(mapToInterface(status), |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4982 | String16(cameraId)); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4983 | listener->handleBinderStatus(ret, |
| 4984 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", |
| 4985 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 4986 | ret.exceptionCode()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4987 | } |
| 4988 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4989 | } |
| 4990 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4991 | void CameraService::updateOpenCloseStatus(const String8& cameraId, bool open, |
| 4992 | const String16& clientPackageName) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 4993 | auto state = getCameraState(cameraId); |
| 4994 | if (state == nullptr) { |
| 4995 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 4996 | cameraId.string()); |
| 4997 | return; |
| 4998 | } |
| 4999 | if (open) { |
| 5000 | state->setClientPackage(String8(clientPackageName)); |
| 5001 | } else { |
| 5002 | state->setClientPackage(String8::empty()); |
| 5003 | } |
| 5004 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5005 | Mutex::Autolock lock(mStatusListenerLock); |
| 5006 | |
| 5007 | for (const auto& it : mListenerList) { |
| 5008 | if (!it->isOpenCloseCallbackAllowed()) { |
| 5009 | continue; |
| 5010 | } |
| 5011 | |
| 5012 | binder::Status ret; |
| 5013 | String16 cameraId64(cameraId); |
| 5014 | if (open) { |
| 5015 | ret = it->getListener()->onCameraOpened(cameraId64, clientPackageName); |
| 5016 | } else { |
| 5017 | ret = it->getListener()->onCameraClosed(cameraId64); |
| 5018 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5019 | |
| 5020 | it->handleBinderStatus(ret, |
| 5021 | "%s: Failed to trigger onCameraOpened/onCameraClosed callback for %d:%d: %d", |
| 5022 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 5023 | } |
| 5024 | } |
| 5025 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5026 | template<class Func> |
| 5027 | void CameraService::CameraState::updateStatus(StatusInternal status, |
| 5028 | const String8& cameraId, |
| 5029 | std::initializer_list<StatusInternal> rejectSourceStates, |
| 5030 | Func onStatusUpdatedLocked) { |
| 5031 | Mutex::Autolock lock(mStatusLock); |
| 5032 | StatusInternal oldStatus = mStatus; |
| 5033 | mStatus = status; |
| 5034 | |
| 5035 | if (oldStatus == status) { |
| 5036 | return; |
| 5037 | } |
| 5038 | |
| 5039 | ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__, |
| 5040 | cameraId.string(), oldStatus, status); |
| 5041 | |
| 5042 | if (oldStatus == StatusInternal::NOT_PRESENT && |
| 5043 | (status != StatusInternal::PRESENT && |
| 5044 | status != StatusInternal::ENUMERATING)) { |
| 5045 | |
| 5046 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 5047 | __FUNCTION__); |
| 5048 | mStatus = oldStatus; |
| 5049 | return; |
| 5050 | } |
| 5051 | |
| 5052 | /** |
| 5053 | * Sometimes we want to conditionally do a transition. |
| 5054 | * For example if a client disconnects, we want to go to PRESENT |
| 5055 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 5056 | */ |
| 5057 | for (auto& rejectStatus : rejectSourceStates) { |
| 5058 | if (oldStatus == rejectStatus) { |
| 5059 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
| 5060 | "state was was in one of the bad states.", __FUNCTION__, cameraId.string()); |
| 5061 | mStatus = oldStatus; |
| 5062 | return; |
| 5063 | } |
| 5064 | } |
| 5065 | |
| 5066 | onStatusUpdatedLocked(cameraId, status); |
| 5067 | } |
| 5068 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5069 | status_t CameraService::getTorchStatusLocked( |
| 5070 | const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5071 | TorchModeStatus *status) const { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5072 | if (!status) { |
| 5073 | return BAD_VALUE; |
| 5074 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5075 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 5076 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5077 | // invalid camera ID or the camera doesn't have a flash unit |
| 5078 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5079 | } |
| 5080 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5081 | *status = mTorchStatusMap.valueAt(index); |
| 5082 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5083 | } |
| 5084 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 5085 | status_t CameraService::setTorchStatusLocked(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5086 | TorchModeStatus status) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5087 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 5088 | if (index == NAME_NOT_FOUND) { |
| 5089 | return BAD_VALUE; |
| 5090 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 5091 | mTorchStatusMap.editValueAt(index) = status; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 5092 | |
| 5093 | return OK; |
| 5094 | } |
| 5095 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5096 | std::list<String16> CameraService::getLogicalCameras( |
| 5097 | const String8& physicalCameraId) { |
| 5098 | std::list<String16> retList; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5099 | Mutex::Autolock lock(mCameraStatesLock); |
| 5100 | for (const auto& state : mCameraStates) { |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 5101 | if (state.second->containsPhysicalCamera(physicalCameraId.c_str())) { |
| 5102 | retList.emplace_back(String16(state.first)); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5103 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5104 | } |
| 5105 | return retList; |
| 5106 | } |
| 5107 | |
| 5108 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, |
| 5109 | const String16& physicalCameraId, const std::list<String16>& logicalCameraIds, |
| 5110 | SystemCameraKind deviceKind) { |
| 5111 | // mStatusListenerLock is expected to be locked |
| 5112 | for (const auto& logicalCameraId : logicalCameraIds) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5113 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5114 | // Note: we check only the deviceKind of the physical camera id |
| 5115 | // since, logical camera ids and their physical camera ids are |
| 5116 | // guaranteed to have the same system camera kind. |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 5117 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 5118 | listener->getListenerPid(), listener->getListenerUid())) { |
| 5119 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5120 | String8(physicalCameraId).c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 5121 | continue; |
| 5122 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5123 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged(status, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5124 | logicalCameraId, physicalCameraId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 5125 | listener->handleBinderStatus(ret, |
| 5126 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 5127 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 5128 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 5129 | } |
| 5130 | } |
| 5131 | } |
| 5132 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 5133 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5134 | void CameraService::blockClientsForUid(uid_t uid) { |
| 5135 | const auto clients = mActiveClientManager.getAll(); |
| 5136 | for (auto& current : clients) { |
| 5137 | if (current != nullptr) { |
| 5138 | const auto basicClient = current->getValue(); |
| 5139 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { |
| 5140 | basicClient->block(); |
| 5141 | } |
| 5142 | } |
| 5143 | } |
| 5144 | } |
| 5145 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 5146 | void CameraService::blockAllClients() { |
| 5147 | const auto clients = mActiveClientManager.getAll(); |
| 5148 | for (auto& current : clients) { |
| 5149 | if (current != nullptr) { |
| 5150 | const auto basicClient = current->getValue(); |
| 5151 | if (basicClient.get() != nullptr) { |
| 5152 | basicClient->block(); |
| 5153 | } |
| 5154 | } |
| 5155 | } |
| 5156 | } |
| 5157 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5158 | // NOTE: This is a remote API - make sure all args are validated |
| 5159 | status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) { |
| 5160 | if (!checkCallingPermission(sManageCameraPermission, nullptr, nullptr)) { |
| 5161 | return PERMISSION_DENIED; |
| 5162 | } |
| 5163 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 5164 | return BAD_VALUE; |
| 5165 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5166 | if (args.size() >= 3 && args[0] == String16("set-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5167 | return handleSetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5168 | } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5169 | return handleResetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5170 | } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5171 | return handleGetUidState(args, out, err); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5172 | } else if (args.size() >= 2 && args[0] == String16("set-rotate-and-crop")) { |
| 5173 | return handleSetRotateAndCrop(args); |
| 5174 | } else if (args.size() >= 1 && args[0] == String16("get-rotate-and-crop")) { |
| 5175 | return handleGetRotateAndCrop(out); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5176 | } else if (args.size() >= 2 && args[0] == String16("set-autoframing")) { |
| 5177 | return handleSetAutoframing(args); |
| 5178 | } else if (args.size() >= 1 && args[0] == String16("get-autoframing")) { |
| 5179 | return handleGetAutoframing(out); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5180 | } else if (args.size() >= 2 && args[0] == String16("set-image-dump-mask")) { |
| 5181 | return handleSetImageDumpMask(args); |
| 5182 | } else if (args.size() >= 1 && args[0] == String16("get-image-dump-mask")) { |
| 5183 | return handleGetImageDumpMask(out); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 5184 | } else if (args.size() >= 2 && args[0] == String16("set-camera-mute")) { |
| 5185 | return handleSetCameraMute(args); |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5186 | } else if (args.size() >= 2 && args[0] == String16("set-stream-use-case-override")) { |
| 5187 | return handleSetStreamUseCaseOverrides(args); |
| 5188 | } else if (args.size() >= 1 && args[0] == String16("clear-stream-use-case-override")) { |
| 5189 | handleClearStreamUseCaseOverrides(); |
| 5190 | return OK; |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 5191 | } else if (args.size() >= 1 && args[0] == String16("set-zoom-override")) { |
| 5192 | return handleSetZoomOverride(args); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5193 | } else if (args.size() >= 2 && args[0] == String16("watch")) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5194 | return handleWatchCommand(args, in, out); |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 5195 | } else if (args.size() >= 2 && args[0] == String16("set-watchdog")) { |
| 5196 | return handleSetCameraServiceWatchdog(args); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5197 | } else if (args.size() == 1 && args[0] == String16("help")) { |
| 5198 | printHelp(out); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5199 | return OK; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5200 | } |
| 5201 | printHelp(err); |
| 5202 | return BAD_VALUE; |
| 5203 | } |
| 5204 | |
| 5205 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5206 | String16 packageName = args[1]; |
| 5207 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5208 | bool active = false; |
| 5209 | if (args[2] == String16("active")) { |
| 5210 | active = true; |
| 5211 | } else if ((args[2] != String16("idle"))) { |
| 5212 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string()); |
| 5213 | return BAD_VALUE; |
| 5214 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5215 | |
| 5216 | int userId = 0; |
| 5217 | if (args.size() >= 5 && args[3] == String16("--user")) { |
| 5218 | userId = atoi(String8(args[4])); |
| 5219 | } |
| 5220 | |
| 5221 | uid_t uid; |
| 5222 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
| 5223 | return BAD_VALUE; |
| 5224 | } |
| 5225 | |
| 5226 | mUidPolicy->addOverrideUid(uid, packageName, active); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5227 | return NO_ERROR; |
| 5228 | } |
| 5229 | |
| 5230 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5231 | String16 packageName = args[1]; |
| 5232 | |
| 5233 | int userId = 0; |
| 5234 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 5235 | userId = atoi(String8(args[3])); |
| 5236 | } |
| 5237 | |
| 5238 | uid_t uid; |
| 5239 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5240 | return BAD_VALUE; |
| 5241 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5242 | |
| 5243 | mUidPolicy->removeOverrideUid(uid, packageName); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5244 | return NO_ERROR; |
| 5245 | } |
| 5246 | |
| 5247 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5248 | String16 packageName = args[1]; |
| 5249 | |
| 5250 | int userId = 0; |
| 5251 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 5252 | userId = atoi(String8(args[3])); |
| 5253 | } |
| 5254 | |
| 5255 | uid_t uid; |
| 5256 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5257 | return BAD_VALUE; |
| 5258 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5259 | |
| 5260 | if (mUidPolicy->isUidActive(uid, packageName)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5261 | return dprintf(out, "active\n"); |
| 5262 | } else { |
| 5263 | return dprintf(out, "idle\n"); |
| 5264 | } |
| 5265 | } |
| 5266 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5267 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { |
| 5268 | int rotateValue = atoi(String8(args[1])); |
| 5269 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || |
| 5270 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; |
| 5271 | Mutex::Autolock lock(mServiceLock); |
| 5272 | |
| 5273 | mOverrideRotateAndCropMode = rotateValue; |
| 5274 | |
| 5275 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; |
| 5276 | |
| 5277 | const auto clients = mActiveClientManager.getAll(); |
| 5278 | for (auto& current : clients) { |
| 5279 | if (current != nullptr) { |
| 5280 | const auto basicClient = current->getValue(); |
| 5281 | if (basicClient.get() != nullptr) { |
| 5282 | basicClient->setRotateAndCropOverride(rotateValue); |
| 5283 | } |
| 5284 | } |
| 5285 | } |
| 5286 | |
| 5287 | return OK; |
| 5288 | } |
| 5289 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5290 | status_t CameraService::handleSetAutoframing(const Vector<String16>& args) { |
| 5291 | char* end; |
| 5292 | int autoframingValue = (int) strtol(String8(args[1]), &end, /*base=*/10); |
| 5293 | if ((*end != '\0') || |
| 5294 | (autoframingValue != ANDROID_CONTROL_AUTOFRAMING_OFF && |
| 5295 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_ON && |
| 5296 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_AUTO)) { |
| 5297 | return BAD_VALUE; |
| 5298 | } |
| 5299 | |
| 5300 | Mutex::Autolock lock(mServiceLock); |
| 5301 | mOverrideAutoframingMode = autoframingValue; |
| 5302 | |
| 5303 | if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) return OK; |
| 5304 | |
| 5305 | const auto clients = mActiveClientManager.getAll(); |
| 5306 | for (auto& current : clients) { |
| 5307 | if (current != nullptr) { |
| 5308 | const auto basicClient = current->getValue(); |
| 5309 | if (basicClient.get() != nullptr) { |
| 5310 | basicClient->setAutoframingOverride(autoframingValue); |
| 5311 | } |
| 5312 | } |
| 5313 | } |
| 5314 | |
| 5315 | return OK; |
| 5316 | } |
| 5317 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 5318 | status_t CameraService::handleSetCameraServiceWatchdog(const Vector<String16>& args) { |
| 5319 | int enableWatchdog = atoi(String8(args[1])); |
| 5320 | |
| 5321 | if (enableWatchdog < 0 || enableWatchdog > 1) return BAD_VALUE; |
| 5322 | |
| 5323 | Mutex::Autolock lock(mServiceLock); |
| 5324 | |
| 5325 | mCameraServiceWatchdogEnabled = enableWatchdog; |
| 5326 | |
| 5327 | const auto clients = mActiveClientManager.getAll(); |
| 5328 | for (auto& current : clients) { |
| 5329 | if (current != nullptr) { |
| 5330 | const auto basicClient = current->getValue(); |
| 5331 | if (basicClient.get() != nullptr) { |
| 5332 | basicClient->setCameraServiceWatchdog(enableWatchdog); |
| 5333 | } |
| 5334 | } |
| 5335 | } |
| 5336 | |
| 5337 | return OK; |
| 5338 | } |
| 5339 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5340 | status_t CameraService::handleGetRotateAndCrop(int out) { |
| 5341 | Mutex::Autolock lock(mServiceLock); |
| 5342 | |
| 5343 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); |
| 5344 | } |
| 5345 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5346 | status_t CameraService::handleGetAutoframing(int out) { |
| 5347 | Mutex::Autolock lock(mServiceLock); |
| 5348 | |
| 5349 | return dprintf(out, "autoframing override: %d\n", mOverrideAutoframingMode); |
| 5350 | } |
| 5351 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5352 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { |
| 5353 | char *endPtr; |
| 5354 | errno = 0; |
| 5355 | String8 maskString8 = String8(args[1]); |
| 5356 | long maskValue = strtol(maskString8.c_str(), &endPtr, 10); |
| 5357 | |
| 5358 | if (errno != 0) return BAD_VALUE; |
| 5359 | if (endPtr != maskString8.c_str() + maskString8.size()) return BAD_VALUE; |
| 5360 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; |
| 5361 | |
| 5362 | Mutex::Autolock lock(mServiceLock); |
| 5363 | |
| 5364 | mImageDumpMask = maskValue; |
| 5365 | |
| 5366 | return OK; |
| 5367 | } |
| 5368 | |
| 5369 | status_t CameraService::handleGetImageDumpMask(int out) { |
| 5370 | Mutex::Autolock lock(mServiceLock); |
| 5371 | |
| 5372 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); |
| 5373 | } |
| 5374 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 5375 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { |
| 5376 | int muteValue = strtol(String8(args[1]), nullptr, 10); |
| 5377 | if (errno != 0) return BAD_VALUE; |
| 5378 | |
| 5379 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; |
| 5380 | Mutex::Autolock lock(mServiceLock); |
| 5381 | |
| 5382 | mOverrideCameraMuteMode = (muteValue == 1); |
| 5383 | |
| 5384 | const auto clients = mActiveClientManager.getAll(); |
| 5385 | for (auto& current : clients) { |
| 5386 | if (current != nullptr) { |
| 5387 | const auto basicClient = current->getValue(); |
| 5388 | if (basicClient.get() != nullptr) { |
| 5389 | if (basicClient->supportsCameraMute()) { |
| 5390 | basicClient->setCameraMute(mOverrideCameraMuteMode); |
| 5391 | } |
| 5392 | } |
| 5393 | } |
| 5394 | } |
| 5395 | |
| 5396 | return OK; |
| 5397 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5398 | |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5399 | status_t CameraService::handleSetStreamUseCaseOverrides(const Vector<String16>& args) { |
| 5400 | std::vector<int64_t> useCasesOverride; |
| 5401 | for (size_t i = 1; i < args.size(); i++) { |
| 5402 | int64_t useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
| 5403 | String8 arg8 = String8(args[i]); |
| 5404 | if (arg8 == "DEFAULT") { |
| 5405 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; |
| 5406 | } else if (arg8 == "PREVIEW") { |
| 5407 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW; |
| 5408 | } else if (arg8 == "STILL_CAPTURE") { |
| 5409 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_STILL_CAPTURE; |
| 5410 | } else if (arg8 == "VIDEO_RECORD") { |
| 5411 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_RECORD; |
| 5412 | } else if (arg8 == "PREVIEW_VIDEO_STILL") { |
| 5413 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW_VIDEO_STILL; |
| 5414 | } else if (arg8 == "VIDEO_CALL") { |
| 5415 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL; |
| 5416 | } else if (arg8 == "CROPPED_RAW") { |
| 5417 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_CROPPED_RAW; |
| 5418 | } else { |
| 5419 | ALOGE("%s: Invalid stream use case %s", __FUNCTION__, arg8.c_str()); |
| 5420 | return BAD_VALUE; |
| 5421 | } |
| 5422 | useCasesOverride.push_back(useCase); |
| 5423 | } |
| 5424 | |
| 5425 | Mutex::Autolock lock(mServiceLock); |
| 5426 | mStreamUseCaseOverrides = std::move(useCasesOverride); |
| 5427 | |
| 5428 | return OK; |
| 5429 | } |
| 5430 | |
| 5431 | void CameraService::handleClearStreamUseCaseOverrides() { |
| 5432 | Mutex::Autolock lock(mServiceLock); |
| 5433 | mStreamUseCaseOverrides.clear(); |
| 5434 | } |
| 5435 | |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 5436 | status_t CameraService::handleSetZoomOverride(const Vector<String16>& args) { |
| 5437 | char* end; |
| 5438 | int zoomOverrideValue = strtol(String8(args[1]), &end, /*base=*/10); |
| 5439 | if ((*end != '\0') || |
| 5440 | (zoomOverrideValue != -1 && |
| 5441 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF && |
| 5442 | zoomOverrideValue != ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM)) { |
| 5443 | return BAD_VALUE; |
| 5444 | } |
| 5445 | |
| 5446 | Mutex::Autolock lock(mServiceLock); |
| 5447 | mZoomOverrideValue = zoomOverrideValue; |
| 5448 | |
| 5449 | const auto clients = mActiveClientManager.getAll(); |
| 5450 | for (auto& current : clients) { |
| 5451 | if (current != nullptr) { |
| 5452 | const auto basicClient = current->getValue(); |
| 5453 | if (basicClient.get() != nullptr) { |
| 5454 | if (basicClient->supportsZoomOverride()) { |
| 5455 | basicClient->setZoomOverride(mZoomOverrideValue); |
| 5456 | } |
| 5457 | } |
| 5458 | } |
| 5459 | } |
| 5460 | |
| 5461 | return OK; |
| 5462 | } |
| 5463 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5464 | status_t CameraService::handleWatchCommand(const Vector<String16>& args, int inFd, int outFd) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5465 | if (args.size() >= 3 && args[1] == String16("start")) { |
| 5466 | return startWatchingTags(args, outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5467 | } else if (args.size() == 2 && args[1] == String16("stop")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5468 | return stopWatchingTags(outFd); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5469 | } else if (args.size() == 2 && args[1] == String16("dump")) { |
| 5470 | return printWatchedTags(outFd); |
| 5471 | } else if (args.size() >= 2 && args[1] == String16("live")) { |
| 5472 | return printWatchedTagsUntilInterrupt(args, inFd, outFd); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5473 | } else if (args.size() == 2 && args[1] == String16("clear")) { |
| 5474 | return clearCachedMonitoredTagDumps(outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5475 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5476 | dprintf(outFd, "Camera service watch commands:\n" |
| 5477 | " start -m <comma_separated_tag_list> [-c <comma_separated_client_list>]\n" |
| 5478 | " starts watching the provided tags for clients with provided package\n" |
| 5479 | " recognizes tag shorthands like '3a'\n" |
| 5480 | " 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] | 5481 | " dump dumps the monitoring information and exits\n" |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5482 | " stop stops watching all tags\n" |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5483 | " live [-n <refresh_interval_ms>]\n" |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5484 | " prints the monitored information in real time\n" |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5485 | " Hit return to exit\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5486 | " clear clears all buffers storing information for watch command"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5487 | return BAD_VALUE; |
| 5488 | } |
| 5489 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5490 | status_t CameraService::startWatchingTags(const Vector<String16> &args, int outFd) { |
| 5491 | Mutex::Autolock lock(mLogLock); |
| 5492 | size_t tagsIdx; // index of '-m' |
| 5493 | String16 tags(""); |
| 5494 | for (tagsIdx = 2; tagsIdx < args.size() && args[tagsIdx] != String16("-m"); tagsIdx++); |
| 5495 | if (tagsIdx < args.size() - 1) { |
| 5496 | tags = args[tagsIdx + 1]; |
| 5497 | } else { |
| 5498 | dprintf(outFd, "No tags provided.\n"); |
| 5499 | return BAD_VALUE; |
| 5500 | } |
| 5501 | |
| 5502 | size_t clientsIdx; // index of '-c' |
| 5503 | String16 clients = kWatchAllClientsFlag; // watch all clients if no clients are provided |
| 5504 | for (clientsIdx = 2; clientsIdx < args.size() && args[clientsIdx] != String16("-c"); |
| 5505 | clientsIdx++); |
| 5506 | if (clientsIdx < args.size() - 1) { |
| 5507 | clients = args[clientsIdx + 1]; |
| 5508 | } |
| 5509 | parseClientsToWatchLocked(String8(clients)); |
| 5510 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5511 | // track tags to initialize future clients with the monitoring information |
| 5512 | mMonitorTags = String8(tags); |
| 5513 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5514 | bool serviceLock = tryLock(mServiceLock); |
| 5515 | int numWatchedClients = 0; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5516 | auto cameraClients = mActiveClientManager.getAll(); |
| 5517 | for (const auto &clientDescriptor: cameraClients) { |
| 5518 | if (clientDescriptor == nullptr) { continue; } |
| 5519 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5520 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5521 | |
| 5522 | if (isClientWatchedLocked(client.get())) { |
| 5523 | client->startWatchingTags(mMonitorTags, outFd); |
| 5524 | numWatchedClients++; |
| 5525 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5526 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5527 | dprintf(outFd, "Started watching %d active clients\n", numWatchedClients); |
| 5528 | |
| 5529 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5530 | return OK; |
| 5531 | } |
| 5532 | |
| 5533 | status_t CameraService::stopWatchingTags(int outFd) { |
| 5534 | // clear mMonitorTags to prevent new clients from monitoring tags at initialization |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5535 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5536 | mMonitorTags = String8::empty(); |
| 5537 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5538 | mWatchedClientPackages.clear(); |
| 5539 | mWatchedClientsDumpCache.clear(); |
| 5540 | |
| 5541 | bool serviceLock = tryLock(mServiceLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5542 | auto cameraClients = mActiveClientManager.getAll(); |
| 5543 | for (const auto &clientDescriptor : cameraClients) { |
| 5544 | if (clientDescriptor == nullptr) { continue; } |
| 5545 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5546 | if (client.get() == nullptr) { continue; } |
| 5547 | client->stopWatchingTags(outFd); |
| 5548 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5549 | dprintf(outFd, "Stopped watching all clients.\n"); |
| 5550 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5551 | return OK; |
| 5552 | } |
| 5553 | |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5554 | status_t CameraService::clearCachedMonitoredTagDumps(int outFd) { |
| 5555 | Mutex::Autolock lock(mLogLock); |
| 5556 | size_t clearedSize = mWatchedClientsDumpCache.size(); |
| 5557 | mWatchedClientsDumpCache.clear(); |
| 5558 | dprintf(outFd, "Cleared tag information of %zu cached clients.\n", clearedSize); |
| 5559 | return OK; |
| 5560 | } |
| 5561 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5562 | status_t CameraService::printWatchedTags(int outFd) { |
| 5563 | Mutex::Autolock logLock(mLogLock); |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 5564 | std::set<String16> connectedMonitoredClients; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5565 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5566 | bool printedSomething = false; // tracks if any monitoring information was printed |
| 5567 | // (from either cached or active clients) |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5568 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5569 | bool serviceLock = tryLock(mServiceLock); |
| 5570 | // get all watched clients that are currently connected |
| 5571 | for (const auto &clientDescriptor: mActiveClientManager.getAll()) { |
| 5572 | if (clientDescriptor == nullptr) { continue; } |
| 5573 | |
| 5574 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5575 | if (client.get() == nullptr) { continue; } |
| 5576 | if (!isClientWatchedLocked(client.get())) { continue; } |
| 5577 | |
| 5578 | std::vector<std::string> dumpVector; |
| 5579 | client->dumpWatchedEventsToVector(dumpVector); |
| 5580 | |
| 5581 | size_t printIdx = dumpVector.size(); |
| 5582 | if (printIdx == 0) { |
| 5583 | continue; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5584 | } |
| 5585 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5586 | // Print tag dumps for active client |
| 5587 | const String8 &cameraId = clientDescriptor->getKey(); |
| 5588 | String8 packageName8 = String8(client->getPackageName()); |
| 5589 | const char *printablePackageName = packageName8.lockBuffer(packageName8.size()); |
| 5590 | dprintf(outFd, "Client: %s (active)\n", printablePackageName); |
| 5591 | while(printIdx > 0) { |
| 5592 | printIdx--; |
| 5593 | dprintf(outFd, "%s:%s %s", cameraId.string(), printablePackageName, |
| 5594 | dumpVector[printIdx].c_str()); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5595 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5596 | dprintf(outFd, "\n"); |
| 5597 | packageName8.unlockBuffer(); |
| 5598 | printedSomething = true; |
| 5599 | |
| 5600 | connectedMonitoredClients.emplace(client->getPackageName()); |
| 5601 | } |
| 5602 | if (serviceLock) { mServiceLock.unlock(); } |
| 5603 | |
| 5604 | // Print entries in mWatchedClientsDumpCache for clients that are not connected |
| 5605 | for (const auto &kv: mWatchedClientsDumpCache) { |
| 5606 | const String16 &package = kv.first; |
| 5607 | if (connectedMonitoredClients.find(package) != connectedMonitoredClients.end()) { |
| 5608 | continue; |
| 5609 | } |
| 5610 | |
| 5611 | dprintf(outFd, "Client: %s (cached)\n", String8(package).string()); |
| 5612 | dprintf(outFd, "%s\n", kv.second.c_str()); |
| 5613 | printedSomething = true; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5614 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5615 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5616 | if (!printedSomething) { |
| 5617 | dprintf(outFd, "No monitoring information to print.\n"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5618 | } |
| 5619 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5620 | return OK; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5621 | } |
| 5622 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5623 | // Print all events in vector `events' that came after lastPrintedEvent |
| 5624 | void printNewWatchedEvents(int outFd, |
| 5625 | const char *cameraId, |
| 5626 | const String16 &packageName, |
| 5627 | const std::vector<std::string> &events, |
| 5628 | const std::string &lastPrintedEvent) { |
| 5629 | if (events.empty()) { return; } |
| 5630 | |
| 5631 | // index of lastPrintedEvent in events. |
| 5632 | // lastPrintedIdx = events.size() if lastPrintedEvent is not in events |
| 5633 | size_t lastPrintedIdx; |
| 5634 | for (lastPrintedIdx = 0; |
| 5635 | lastPrintedIdx < events.size() && lastPrintedEvent != events[lastPrintedIdx]; |
| 5636 | lastPrintedIdx++); |
| 5637 | |
| 5638 | if (lastPrintedIdx == 0) { return; } // early exit if no new event in `events` |
| 5639 | |
| 5640 | String8 packageName8(packageName); |
| 5641 | const char *printablePackageName = packageName8.lockBuffer(packageName8.size()); |
| 5642 | |
| 5643 | // print events in chronological order (latest event last) |
| 5644 | size_t idxToPrint = lastPrintedIdx; |
| 5645 | do { |
| 5646 | idxToPrint--; |
| 5647 | dprintf(outFd, "%s:%s %s", cameraId, printablePackageName, events[idxToPrint].c_str()); |
| 5648 | } while (idxToPrint != 0); |
| 5649 | |
| 5650 | packageName8.unlockBuffer(); |
| 5651 | } |
| 5652 | |
| 5653 | // Returns true if adb shell cmd watch should be interrupted based on data in inFd. The watch |
| 5654 | // command should be interrupted if the user presses the return key, or if user loses any way to |
| 5655 | // signal interrupt. |
| 5656 | // If timeoutMs == 0, this function will always return false |
| 5657 | bool shouldInterruptWatchCommand(int inFd, int outFd, long timeoutMs) { |
| 5658 | struct timeval startTime; |
| 5659 | int startTimeError = gettimeofday(&startTime, nullptr); |
| 5660 | if (startTimeError) { |
| 5661 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 5662 | return true; |
| 5663 | } |
| 5664 | |
| 5665 | const nfds_t numFds = 1; |
| 5666 | struct pollfd pollFd = { .fd = inFd, .events = POLLIN, .revents = 0 }; |
| 5667 | |
| 5668 | struct timeval currTime; |
| 5669 | char buffer[2]; |
| 5670 | while(true) { |
| 5671 | int currTimeError = gettimeofday(&currTime, nullptr); |
| 5672 | if (currTimeError) { |
| 5673 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 5674 | return true; |
| 5675 | } |
| 5676 | |
| 5677 | long elapsedTimeMs = ((currTime.tv_sec - startTime.tv_sec) * 1000L) |
| 5678 | + ((currTime.tv_usec - startTime.tv_usec) / 1000L); |
| 5679 | int remainingTimeMs = (int) (timeoutMs - elapsedTimeMs); |
| 5680 | |
| 5681 | if (remainingTimeMs <= 0) { |
| 5682 | // No user interrupt within timeoutMs, don't interrupt watch command |
| 5683 | return false; |
| 5684 | } |
| 5685 | |
| 5686 | int numFdsUpdated = poll(&pollFd, numFds, remainingTimeMs); |
| 5687 | if (numFdsUpdated < 0) { |
| 5688 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 5689 | return true; |
| 5690 | } |
| 5691 | |
| 5692 | if (numFdsUpdated == 0) { |
| 5693 | // No user input within timeoutMs, don't interrupt watch command |
| 5694 | return false; |
| 5695 | } |
| 5696 | |
| 5697 | if (!(pollFd.revents & POLLIN)) { |
| 5698 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 5699 | return true; |
| 5700 | } |
| 5701 | |
| 5702 | ssize_t sizeRead = read(inFd, buffer, sizeof(buffer) - 1); |
| 5703 | if (sizeRead < 0) { |
| 5704 | dprintf(outFd, "Error reading user input. Exiting.\n"); |
| 5705 | return true; |
| 5706 | } |
| 5707 | |
| 5708 | if (sizeRead == 0) { |
| 5709 | // Reached end of input fd (can happen if input is piped) |
| 5710 | // User has no way to signal an interrupt, so interrupt here |
| 5711 | return true; |
| 5712 | } |
| 5713 | |
| 5714 | if (buffer[0] == '\n') { |
| 5715 | // User pressed return, interrupt watch command. |
| 5716 | return true; |
| 5717 | } |
| 5718 | } |
| 5719 | } |
| 5720 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5721 | status_t CameraService::printWatchedTagsUntilInterrupt(const Vector<String16> &args, |
| 5722 | int inFd, int outFd) { |
| 5723 | // Figure out refresh interval, if present in args |
| 5724 | long refreshTimeoutMs = 1000L; // refresh every 1s by default |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5725 | if (args.size() > 2) { |
| 5726 | size_t intervalIdx; // index of '-n' |
| 5727 | for (intervalIdx = 2; intervalIdx < args.size() && String16("-n") != args[intervalIdx]; |
| 5728 | intervalIdx++); |
| 5729 | |
| 5730 | size_t intervalValIdx = intervalIdx + 1; |
| 5731 | if (intervalValIdx < args.size()) { |
| 5732 | refreshTimeoutMs = strtol(String8(args[intervalValIdx].string()), nullptr, 10); |
| 5733 | if (errno) { return BAD_VALUE; } |
| 5734 | } |
| 5735 | } |
| 5736 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5737 | // Set min timeout of 10ms. This prevents edge cases in polling when timeout of 0 is passed. |
| 5738 | refreshTimeoutMs = refreshTimeoutMs < 10 ? 10 : refreshTimeoutMs; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5739 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5740 | dprintf(outFd, "Press return to exit...\n\n"); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5741 | std::map<String16, std::string> packageNameToLastEvent; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5742 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5743 | while (true) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5744 | bool serviceLock = tryLock(mServiceLock); |
| 5745 | auto cameraClients = mActiveClientManager.getAll(); |
| 5746 | if (serviceLock) { mServiceLock.unlock(); } |
| 5747 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5748 | for (const auto& clientDescriptor : cameraClients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5749 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5750 | if (clientDescriptor == nullptr) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5751 | |
| 5752 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5753 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5754 | if (!isClientWatchedLocked(client.get())) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5755 | |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5756 | const String16 &packageName = client->getPackageName(); |
| 5757 | // This also initializes the map entries with an empty string |
| 5758 | const std::string& lastPrintedEvent = packageNameToLastEvent[packageName]; |
| 5759 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5760 | std::vector<std::string> latestEvents; |
| 5761 | client->dumpWatchedEventsToVector(latestEvents); |
| 5762 | |
| 5763 | if (!latestEvents.empty()) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5764 | String8 cameraId = clientDescriptor->getKey(); |
| 5765 | const char *printableCameraId = cameraId.lockBuffer(cameraId.size()); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5766 | printNewWatchedEvents(outFd, |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5767 | printableCameraId, |
| 5768 | packageName, |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5769 | latestEvents, |
| 5770 | lastPrintedEvent); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5771 | packageNameToLastEvent[packageName] = latestEvents[0]; |
| 5772 | cameraId.unlockBuffer(); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5773 | } |
| 5774 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5775 | if (shouldInterruptWatchCommand(inFd, outFd, refreshTimeoutMs)) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5776 | break; |
| 5777 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5778 | } |
| 5779 | return OK; |
| 5780 | } |
| 5781 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5782 | void CameraService::parseClientsToWatchLocked(String8 clients) { |
| 5783 | mWatchedClientPackages.clear(); |
| 5784 | |
| 5785 | const char *allSentinel = String8(kWatchAllClientsFlag).string(); |
| 5786 | |
| 5787 | char *tokenized = clients.lockBuffer(clients.size()); |
| 5788 | char *savePtr; |
| 5789 | char *nextClient = strtok_r(tokenized, ",", &savePtr); |
| 5790 | |
| 5791 | while (nextClient != nullptr) { |
| 5792 | if (strcmp(nextClient, allSentinel) == 0) { |
| 5793 | // Don't need to track any other package if 'all' is present |
| 5794 | mWatchedClientPackages.clear(); |
| 5795 | mWatchedClientPackages.emplace(kWatchAllClientsFlag); |
| 5796 | break; |
| 5797 | } |
| 5798 | |
| 5799 | // track package names |
| 5800 | mWatchedClientPackages.emplace(nextClient); |
| 5801 | nextClient = strtok_r(nullptr, ",", &savePtr); |
| 5802 | } |
| 5803 | clients.unlockBuffer(); |
| 5804 | } |
| 5805 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5806 | status_t CameraService::printHelp(int out) { |
| 5807 | return dprintf(out, "Camera service commands:\n" |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5808 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 5809 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 5810 | " 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] | 5811 | " set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" |
| 5812 | " Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" |
| 5813 | " 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] | 5814 | " set-autoframing <VALUE> overrides the autoframing value for AUTO\n" |
| 5815 | " Valid values 0=false, 1=true, 2=auto\n" |
| 5816 | " get-autoframing returns the current override autoframing value\n" |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5817 | " set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" |
| 5818 | " Valid values 0=OFF, 1=ON for JPEG\n" |
| 5819 | " 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] | 5820 | " set-camera-mute <0/1> enable or disable camera muting\n" |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5821 | " set-stream-use-case-override <usecase1> <usecase2> ... override stream use cases\n" |
| 5822 | " Use cases applied in descending resolutions. So usecase1 is assigned to the\n" |
| 5823 | " largest resolution, usecase2 is assigned to the 2nd largest resolution, and so\n" |
| 5824 | " on. In case the number of usecases is smaller than the number of streams, the\n" |
| 5825 | " last use case is assigned to all the remaining streams. In case of multiple\n" |
| 5826 | " streams with the same resolution, the tie-breaker is (JPEG, RAW, YUV, and PRIV)\n" |
| 5827 | " Valid values are (case sensitive): DEFAULT, PREVIEW, STILL_CAPTURE, VIDEO_RECORD,\n" |
| 5828 | " PREVIEW_VIDEO_STILL, VIDEO_CALL, CROPPED_RAW\n" |
| 5829 | " clear-stream-use-case-override clear the stream use case override\n" |
Shuzhen Wang | af22e91 | 2023-04-11 16:03:17 -0700 | [diff] [blame] | 5830 | " set-zoom-override <-1/0/1> enable or disable zoom override\n" |
| 5831 | " 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] | 5832 | " 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] | 5833 | " help print this message\n"); |
| 5834 | } |
| 5835 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5836 | bool CameraService::isClientWatched(const BasicClient *client) { |
| 5837 | Mutex::Autolock lock(mLogLock); |
| 5838 | return isClientWatchedLocked(client); |
| 5839 | } |
| 5840 | |
| 5841 | bool CameraService::isClientWatchedLocked(const BasicClient *client) { |
| 5842 | return mWatchedClientPackages.find(kWatchAllClientsFlag) != mWatchedClientPackages.end() || |
| 5843 | mWatchedClientPackages.find(client->getPackageName()) != mWatchedClientPackages.end(); |
| 5844 | } |
| 5845 | |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 5846 | int32_t CameraService::updateAudioRestriction() { |
| 5847 | Mutex::Autolock lock(mServiceLock); |
| 5848 | return updateAudioRestrictionLocked(); |
| 5849 | } |
| 5850 | |
| 5851 | int32_t CameraService::updateAudioRestrictionLocked() { |
| 5852 | int32_t mode = 0; |
| 5853 | // iterate through all active client |
| 5854 | for (const auto& i : mActiveClientManager.getAll()) { |
| 5855 | const auto clientSp = i->getValue(); |
| 5856 | mode |= clientSp->getAudioRestriction(); |
| 5857 | } |
| 5858 | |
| 5859 | bool modeChanged = (mAudioRestriction != mode); |
| 5860 | mAudioRestriction = mode; |
| 5861 | if (modeChanged) { |
| 5862 | mAppOps.setCameraAudioRestriction(mode); |
| 5863 | } |
| 5864 | return mode; |
| 5865 | } |
| 5866 | |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 5867 | status_t CameraService::checkIfInjectionCameraIsPresent(const String8& externalCamId, |
| 5868 | sp<BasicClient> clientSp) { |
| 5869 | std::unique_ptr<AutoConditionLock> lock = |
| 5870 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 5871 | status_t res = NO_ERROR; |
| 5872 | if ((res = checkIfDeviceIsUsable(externalCamId)) != NO_ERROR) { |
| 5873 | ALOGW("Device %s is not usable!", externalCamId.string()); |
| 5874 | mInjectionStatusListener->notifyInjectionError( |
| 5875 | externalCamId, UNKNOWN_TRANSACTION); |
| 5876 | clientSp->notifyError( |
| 5877 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 5878 | CaptureResultExtras()); |
| 5879 | |
| 5880 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 5881 | // other clients from connecting in mServiceLockWrapper if held |
| 5882 | mServiceLock.unlock(); |
| 5883 | |
| 5884 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
| 5885 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 5886 | clientSp->disconnect(); |
| 5887 | CameraThreadState::restoreCallingIdentity(token); |
| 5888 | |
| 5889 | // Reacquire mServiceLock |
| 5890 | mServiceLock.lock(); |
| 5891 | } |
| 5892 | |
| 5893 | return res; |
| 5894 | } |
| 5895 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5896 | void CameraService::clearInjectionParameters() { |
| 5897 | { |
| 5898 | Mutex::Autolock lock(mInjectionParametersLock); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 5899 | mInjectionInitPending = false; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5900 | mInjectionInternalCamId = ""; |
| 5901 | } |
| 5902 | mInjectionExternalCamId = ""; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5903 | mInjectionStatusListener->removeListener(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5904 | } |
| 5905 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5906 | }; // namespace android |