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 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 36 | #include <android-base/macros.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 37 | #include <android-base/parseint.h> |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 38 | #include <android-base/stringprintf.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 39 | #include <binder/ActivityManager.h> |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 40 | #include <binder/AppOpsManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 41 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | #include <binder/MemoryBase.h> |
| 43 | #include <binder/MemoryHeapBase.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 44 | #include <binder/PermissionController.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 45 | #include <binder/IResultReceiver.h> |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 46 | #include <binderthreadstate/CallerUtils.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 47 | #include <cutils/atomic.h> |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 48 | #include <cutils/properties.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 49 | #include <cutils/misc.h> |
Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 50 | #include <gui/Surface.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 51 | #include <hardware/hardware.h> |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 52 | #include "hidl/HidlCameraService.h" |
| 53 | #include <hidl/HidlTransportSupport.h> |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 54 | #include <hwbinder/IPCThreadState.h> |
Eino-Ville Talvala | d89821e | 2016-04-20 11:23:50 -0700 | [diff] [blame] | 55 | #include <memunreachable/memunreachable.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 56 | #include <media/AudioSystem.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 57 | #include <media/IMediaHTTPService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 | #include <media/mediaplayer.h> |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 59 | #include <mediautils/BatteryNotifier.h> |
Steven Moreland | 886d732 | 2021-04-02 04:19:45 +0000 | [diff] [blame] | 60 | #include <processinfo/ProcessInfoService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 61 | #include <utils/Errors.h> |
| 62 | #include <utils/Log.h> |
| 63 | #include <utils/String16.h> |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 64 | #include <utils/SystemClock.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 65 | #include <utils/Trace.h> |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 66 | #include <utils/CallStack.h> |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 67 | #include <private/android_filesystem_config.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 68 | #include <system/camera_vendor_tags.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 69 | #include <system/camera_metadata.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 70 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 71 | #include <system/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 72 | |
| 73 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 74 | #include "api1/Camera2Client.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 75 | #include "api2/CameraDeviceClient.h" |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 76 | #include "utils/CameraTraces.h" |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 77 | #include "utils/TagMonitor.h" |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 78 | #include "utils/CameraThreadState.h" |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 79 | #include "utils/CameraServiceProxyWrapper.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 80 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 81 | namespace { |
| 82 | const char* kPermissionServiceName = "permission"; |
| 83 | }; // namespace anonymous |
| 84 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 85 | namespace android { |
| 86 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 87 | using base::StringPrintf; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 88 | using binder::Status; |
Austin Borger | ea93124 | 2021-12-13 23:10:41 +0000 | [diff] [blame] | 89 | using namespace camera3; |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 90 | using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 91 | using hardware::ICamera; |
| 92 | using hardware::ICameraClient; |
| 93 | using hardware::ICameraServiceListener; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 94 | using hardware::camera2::ICameraInjectionCallback; |
| 95 | using hardware::camera2::ICameraInjectionSession; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 96 | using hardware::camera2::utils::CameraIdAndSessionConfiguration; |
| 97 | using hardware::camera2::utils::ConcurrentCameraIdCombination; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 98 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 99 | // ---------------------------------------------------------------------------- |
| 100 | // Logging support -- this is for debugging only |
| 101 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 102 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 103 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 104 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 105 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 106 | |
| 107 | static void setLogLevel(int level) { |
| 108 | android_atomic_write(level, &gLogLevel); |
| 109 | } |
| 110 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 111 | // Convenience methods for constructing binder::Status objects for error returns |
| 112 | |
| 113 | #define STATUS_ERROR(errorCode, errorString) \ |
| 114 | binder::Status::fromServiceSpecificError(errorCode, \ |
| 115 | String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString)) |
| 116 | |
| 117 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ |
| 118 | binder::Status::fromServiceSpecificError(errorCode, \ |
| 119 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \ |
| 120 | __VA_ARGS__)) |
| 121 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 122 | // ---------------------------------------------------------------------------- |
| 123 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 124 | static const String16 sDumpPermission("android.permission.DUMP"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 125 | static const String16 sManageCameraPermission("android.permission.MANAGE_CAMERA"); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 126 | static const String16 sCameraPermission("android.permission.CAMERA"); |
| 127 | static const String16 sSystemCameraPermission("android.permission.SYSTEM_CAMERA"); |
| 128 | static const String16 |
| 129 | sCameraSendSystemEventsPermission("android.permission.CAMERA_SEND_SYSTEM_EVENTS"); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 130 | static const String16 sCameraOpenCloseListenerPermission( |
| 131 | "android.permission.CAMERA_OPEN_CLOSE_LISTENER"); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 132 | static const String16 |
| 133 | sCameraInjectExternalCameraPermission("android.permission.CAMERA_INJECT_EXTERNAL_CAMERA"); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 134 | const char *sFileName = "lastOpenSessionDumpFile"; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 135 | static constexpr int32_t kSystemNativeClientScore = resource_policy::PERCEPTIBLE_APP_ADJ; |
| 136 | static constexpr int32_t kSystemNativeClientState = |
| 137 | ActivityManager::PROCESS_STATE_PERSISTENT_UI; |
Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame] | 138 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 139 | const String8 CameraService::kOfflineDevice("offline-"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 140 | const String16 CameraService::kWatchAllClientsFlag("all"); |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 141 | |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 142 | // Set to keep track of logged service error events. |
| 143 | static std::set<String8> sServiceErrorEventSet; |
| 144 | |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 145 | CameraService::CameraService( |
| 146 | std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper) : |
| 147 | mCameraServiceProxyWrapper(cameraServiceProxyWrapper == nullptr ? |
| 148 | std::make_shared<CameraServiceProxyWrapper>() : cameraServiceProxyWrapper), |
Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 149 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 150 | mNumberOfCameras(0), |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 151 | mNumberOfCamerasWithoutSystemCamera(0), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 152 | mSoundRef(0), mInitialized(false), |
| 153 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 154 | ALOGI("CameraService started (pid=%d)", getpid()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 155 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 156 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 157 | if (mMemFd == -1) { |
| 158 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 159 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 162 | // The word 'System' here does not refer to clients only on the system |
| 163 | // partition. They just need to have a android system uid. |
| 164 | static bool doesClientHaveSystemUid() { |
| 165 | return (CameraThreadState::getCallingUid() < AID_APP_START); |
| 166 | } |
| 167 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 168 | void CameraService::onFirstRef() |
| 169 | { |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 170 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 171 | ALOGI("CameraService process starting"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 172 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 173 | BnCameraService::onFirstRef(); |
| 174 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 175 | // Update battery life tracking if service is restarting |
| 176 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 177 | notifier.noteResetCamera(); |
| 178 | notifier.noteResetFlashlight(); |
| 179 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 180 | status_t res = INVALID_OPERATION; |
Eino-Ville Talvala | 9cbbc83 | 2017-01-23 15:39:53 -0800 | [diff] [blame] | 181 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 182 | res = enumerateProviders(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 183 | if (res == OK) { |
| 184 | mInitialized = true; |
| 185 | } |
| 186 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 187 | mUidPolicy = new UidPolicy(this); |
| 188 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 189 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this); |
| 190 | mSensorPrivacyPolicy->registerSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 191 | mInjectionStatusListener = new InjectionStatusListener(this); |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 192 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 193 | sp<HidlCameraService> hcs = HidlCameraService::getInstance(this); |
| 194 | if (hcs->registerAsService() != android::OK) { |
| 195 | ALOGE("%s: Failed to register default android.frameworks.cameraservice.service@1.0", |
| 196 | __FUNCTION__); |
| 197 | } |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 198 | |
| 199 | // This needs to be last call in this function, so that it's as close to |
| 200 | // ServiceManager::addService() as possible. |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 201 | mCameraServiceProxyWrapper->pingCameraServiceProxy(); |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 202 | ALOGI("CameraService pinged cameraservice proxy"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 205 | status_t CameraService::enumerateProviders() { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 206 | status_t res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 207 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 208 | std::vector<std::string> deviceIds; |
| 209 | { |
| 210 | Mutex::Autolock l(mServiceLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 211 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 212 | if (nullptr == mCameraProviderManager.get()) { |
| 213 | mCameraProviderManager = new CameraProviderManager(); |
| 214 | res = mCameraProviderManager->initialize(this); |
| 215 | if (res != OK) { |
| 216 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", |
| 217 | __FUNCTION__, strerror(-res), res); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 218 | logServiceError(String8::format("Unable to initialize camera provider manager"), |
| 219 | ERROR_DISCONNECTED); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 220 | return res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 224 | |
| 225 | // Setup vendor tags before we call get_camera_info the first time |
| 226 | // because HAL might need to setup static vendor keys in get_camera_info |
| 227 | // TODO: maybe put this into CameraProviderManager::initialize()? |
| 228 | mCameraProviderManager->setUpVendorTags(); |
| 229 | |
| 230 | if (nullptr == mFlashlight.get()) { |
| 231 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 234 | res = mFlashlight->findFlashUnits(); |
| 235 | if (res != OK) { |
| 236 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); |
| 237 | } |
| 238 | |
| 239 | deviceIds = mCameraProviderManager->getCameraDeviceIds(); |
| 240 | } |
| 241 | |
| 242 | |
| 243 | for (auto& cameraId : deviceIds) { |
| 244 | String8 id8 = String8(cameraId.c_str()); |
Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 245 | if (getCameraState(id8) == nullptr) { |
| 246 | onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT); |
| 247 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 250 | // Derive primary rear/front cameras, and filter their charactierstics. |
| 251 | // 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] | 252 | if (SessionConfigurationUtils::IS_PERF_CLASS) { |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 253 | // Assume internal cameras are advertised from the same |
| 254 | // provider. If multiple providers are registered at different time, |
| 255 | // and each provider contains multiple internal color cameras, the current |
| 256 | // logic may filter the characteristics of more than one front/rear color |
| 257 | // cameras. |
| 258 | Mutex::Autolock l(mServiceLock); |
| 259 | filterSPerfClassCharacteristicsLocked(); |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 260 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 261 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 262 | return OK; |
| 263 | } |
| 264 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 265 | void CameraService::broadcastTorchModeStatus(const String8& cameraId, TorchModeStatus status, |
| 266 | SystemCameraKind systemCameraKind) { |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 267 | Mutex::Autolock lock(mStatusListenerLock); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 268 | for (auto& i : mListenerList) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 269 | if (shouldSkipStatusUpdates(systemCameraKind, i->isVendorListener(), i->getListenerPid(), |
| 270 | i->getListenerUid())) { |
| 271 | ALOGV("Skipping torch callback for system-only camera device %s", |
| 272 | cameraId.c_str()); |
| 273 | continue; |
| 274 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 275 | auto ret = i->getListener()->onTorchStatusChanged(mapToInterface(status), |
| 276 | String16{cameraId}); |
| 277 | i->handleBinderStatus(ret, "%s: Failed to trigger onTorchStatusChanged for %d:%d: %d", |
| 278 | __FUNCTION__, i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 282 | CameraService::~CameraService() { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 283 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 284 | mUidPolicy->unregisterSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 285 | mSensorPrivacyPolicy->unregisterSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 286 | mInjectionStatusListener->removeListener(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 289 | void CameraService::onNewProviderRegistered() { |
| 290 | enumerateProviders(); |
| 291 | } |
| 292 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 293 | void CameraService::filterAPI1SystemCameraLocked( |
| 294 | const std::vector<std::string> &normalDeviceIds) { |
| 295 | mNormalDeviceIdsWithoutSystemCamera.clear(); |
| 296 | for (auto &deviceId : normalDeviceIds) { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 297 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 298 | if (getSystemCameraKind(String8(deviceId.c_str()), &deviceKind) != OK) { |
| 299 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, deviceId.c_str()); |
| 300 | continue; |
| 301 | } |
| 302 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 303 | // All system camera ids will necessarily come after public camera |
| 304 | // device ids as per the HAL interface contract. |
| 305 | break; |
| 306 | } |
| 307 | mNormalDeviceIdsWithoutSystemCamera.push_back(deviceId); |
| 308 | } |
| 309 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, |
| 310 | mNormalDeviceIdsWithoutSystemCamera.size()); |
| 311 | } |
| 312 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 313 | status_t CameraService::getSystemCameraKind(const String8& cameraId, SystemCameraKind *kind) const { |
| 314 | auto state = getCameraState(cameraId); |
| 315 | if (state != nullptr) { |
| 316 | *kind = state->getSystemCameraKind(); |
| 317 | return OK; |
| 318 | } |
| 319 | // Hidden physical camera ids won't have CameraState |
| 320 | return mCameraProviderManager->getSystemCameraKind(cameraId.c_str(), kind); |
| 321 | } |
| 322 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 323 | void CameraService::updateCameraNumAndIds() { |
| 324 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 325 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); |
| 326 | // Excludes hidden secure cameras |
| 327 | mNumberOfCameras = |
| 328 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; |
| 329 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 330 | mNormalDeviceIds = |
| 331 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 332 | filterAPI1SystemCameraLocked(mNormalDeviceIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 335 | void CameraService::filterSPerfClassCharacteristicsLocked() { |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 336 | // To claim to be S Performance primary cameras, the cameras must be |
| 337 | // backward compatible. So performance class primary camera Ids must be API1 |
| 338 | // compatible. |
| 339 | bool firstRearCameraSeen = false, firstFrontCameraSeen = false; |
| 340 | for (const auto& cameraId : mNormalDeviceIdsWithoutSystemCamera) { |
| 341 | int facing = -1; |
| 342 | int orientation = 0; |
| 343 | String8 cameraId8(cameraId.c_str()); |
| 344 | getDeviceVersion(cameraId8, /*out*/&facing, /*out*/&orientation); |
| 345 | if (facing == -1) { |
| 346 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.c_str()); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | if ((facing == hardware::CAMERA_FACING_BACK && !firstRearCameraSeen) || |
| 351 | (facing == hardware::CAMERA_FACING_FRONT && !firstFrontCameraSeen)) { |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 352 | status_t res = mCameraProviderManager->filterSmallJpegSizes(cameraId); |
| 353 | if (res == OK) { |
| 354 | mPerfClassPrimaryCameraIds.insert(cameraId); |
| 355 | } else { |
| 356 | ALOGE("%s: Failed to filter small JPEG sizes for performance class primary " |
| 357 | "camera %s: %s(%d)", __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
| 358 | break; |
| 359 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 360 | |
| 361 | if (facing == hardware::CAMERA_FACING_BACK) { |
| 362 | firstRearCameraSeen = true; |
| 363 | } |
| 364 | if (facing == hardware::CAMERA_FACING_FRONT) { |
| 365 | firstFrontCameraSeen = true; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | if (firstRearCameraSeen && firstFrontCameraSeen) { |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 375 | void CameraService::addStates(const String8 id) { |
| 376 | std::string cameraId(id.c_str()); |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 377 | CameraResourceCost cost; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 378 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); |
| 379 | if (res != OK) { |
| 380 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); |
| 381 | return; |
| 382 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 383 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 384 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); |
| 385 | if (res != OK) { |
| 386 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); |
| 387 | return; |
| 388 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 389 | std::vector<std::string> physicalCameraIds; |
| 390 | mCameraProviderManager->isLogicalCamera(cameraId, &physicalCameraIds); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 391 | std::set<String8> conflicting; |
| 392 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { |
| 393 | conflicting.emplace(String8(cost.conflictingDevices[i].c_str())); |
| 394 | } |
| 395 | |
| 396 | { |
| 397 | Mutex::Autolock lock(mCameraStatesLock); |
| 398 | mCameraStates.emplace(id, std::make_shared<CameraState>(id, cost.resourceCost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 399 | conflicting, deviceKind, physicalCameraIds)); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 403 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 404 | mTorchStatusMap.add(id, TorchModeStatus::AVAILABLE_OFF); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 405 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 406 | broadcastTorchModeStatus(id, TorchModeStatus::AVAILABLE_OFF, deviceKind); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 407 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 408 | |
| 409 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 410 | logDeviceAdded(id, "Device added"); |
| 411 | } |
| 412 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 413 | void CameraService::removeStates(const String8 id) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 414 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 415 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 416 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 417 | mTorchStatusMap.removeItem(id); |
| 418 | } |
| 419 | |
| 420 | { |
| 421 | Mutex::Autolock lock(mCameraStatesLock); |
| 422 | mCameraStates.erase(id); |
| 423 | } |
| 424 | } |
| 425 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 426 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 427 | CameraDeviceStatus newHalStatus) { |
| 428 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, |
| 429 | id.string(), newHalStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 430 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 431 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 432 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 433 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 434 | |
| 435 | if (state == nullptr) { |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 436 | if (newStatus == StatusInternal::PRESENT) { |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 437 | ALOGI("%s: Unknown camera ID %s, a new camera is added", |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 438 | __FUNCTION__, id.string()); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 439 | |
| 440 | // First add as absent to make sure clients are notified below |
| 441 | addStates(id); |
| 442 | |
| 443 | updateStatus(newStatus, id); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 444 | } else { |
| 445 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string()); |
| 446 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 447 | return; |
| 448 | } |
| 449 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 450 | StatusInternal oldStatus = state->getStatus(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 451 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 452 | if (oldStatus == newStatus) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 453 | 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] | 454 | return; |
| 455 | } |
| 456 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 457 | if (newStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 458 | logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 459 | newStatus)); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 460 | |
| 461 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 462 | // to this device until the status changes |
| 463 | updateStatus(StatusInternal::NOT_PRESENT, id); |
| 464 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 465 | sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 466 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 467 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 468 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 469 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 470 | // Remove cached shim parameters |
| 471 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 472 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 473 | // Remove online as well as offline client from the list of active clients, |
| 474 | // if they are present |
| 475 | clientToDisconnectOnline = removeClientLocked(id); |
| 476 | clientToDisconnectOffline = removeClientLocked(kOfflineDevice + id); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 477 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 478 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 479 | disconnectClient(id, clientToDisconnectOnline); |
| 480 | disconnectClient(kOfflineDevice + id, clientToDisconnectOffline); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 481 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 482 | removeStates(id); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 483 | } else { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 484 | if (oldStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 485 | logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 486 | newStatus)); |
| 487 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 488 | updateStatus(newStatus, id); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 489 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 490 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 491 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 492 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 493 | const String8& physicalId, |
| 494 | CameraDeviceStatus newHalStatus) { |
| 495 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", |
| 496 | __FUNCTION__, id.string(), physicalId.string(), newHalStatus); |
| 497 | |
| 498 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 499 | |
| 500 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 501 | |
| 502 | if (state == nullptr) { |
| 503 | ALOGE("%s: Physical camera id %s status change on a non-present ID %s", |
| 504 | __FUNCTION__, id.string(), physicalId.string()); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | StatusInternal logicalCameraStatus = state->getStatus(); |
| 509 | if (logicalCameraStatus != StatusInternal::PRESENT && |
| 510 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 511 | ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d", |
| 512 | __FUNCTION__, physicalId.string(), newHalStatus, logicalCameraStatus); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | bool updated = false; |
| 517 | if (newStatus == StatusInternal::PRESENT) { |
| 518 | updated = state->removeUnavailablePhysicalId(physicalId); |
| 519 | } else { |
| 520 | updated = state->addUnavailablePhysicalId(physicalId); |
| 521 | } |
| 522 | |
| 523 | if (updated) { |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 524 | String8 idCombo = id + " : " + physicalId; |
| 525 | if (newStatus == StatusInternal::PRESENT) { |
| 526 | logDeviceAdded(idCombo, |
| 527 | String8::format("Device status changed to %d", newStatus)); |
| 528 | } else { |
| 529 | logDeviceRemoved(idCombo, |
| 530 | String8::format("Device status changed to %d", newStatus)); |
| 531 | } |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 532 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 533 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 534 | if (getSystemCameraKind(id, &deviceKind) != OK) { |
| 535 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.string()); |
| 536 | return; |
| 537 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 538 | String16 id16(id), physicalId16(physicalId); |
| 539 | Mutex::Autolock lock(mStatusListenerLock); |
| 540 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 541 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 542 | listener->getListenerPid(), listener->getListenerUid())) { |
| 543 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 544 | id.c_str()); |
| 545 | continue; |
| 546 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 547 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged( |
| 548 | mapToInterface(newStatus), id16, physicalId16); |
| 549 | listener->handleBinderStatus(ret, |
| 550 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 551 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 552 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 557 | void CameraService::disconnectClient(const String8& id, sp<BasicClient> clientToDisconnect) { |
| 558 | if (clientToDisconnect.get() != nullptr) { |
| 559 | ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL", |
| 560 | __FUNCTION__, id.string()); |
| 561 | // Notify the client of disconnection |
| 562 | clientToDisconnect->notifyError( |
| 563 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 564 | CaptureResultExtras{}); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 565 | clientToDisconnect->disconnect(); |
| 566 | } |
| 567 | } |
| 568 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 569 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 570 | TorchModeStatus newStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 571 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 572 | status_t res = getSystemCameraKind(cameraId, &systemCameraKind); |
| 573 | if (res != OK) { |
| 574 | ALOGE("%s: Could not get system camera kind for camera id %s", __FUNCTION__, |
| 575 | cameraId.string()); |
| 576 | return; |
| 577 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 578 | Mutex::Autolock al(mTorchStatusMutex); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 579 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Jayant Chowdhary | 46ef0f5 | 2021-10-05 14:36:13 -0700 | [diff] [blame] | 582 | |
| 583 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
| 584 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
| 585 | Mutex::Autolock al(mTorchStatusMutex); |
| 586 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
| 587 | } |
| 588 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 589 | void CameraService::broadcastTorchStrengthLevel(const String8& cameraId, |
| 590 | int32_t newStrengthLevel) { |
| 591 | Mutex::Autolock lock(mStatusListenerLock); |
| 592 | for (auto& i : mListenerList) { |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 593 | auto ret = i->getListener()->onTorchStrengthLevelChanged(String16{cameraId}, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 594 | newStrengthLevel); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 595 | i->handleBinderStatus(ret, |
| 596 | "%s: Failed to trigger onTorchStrengthLevelChanged for %d:%d: %d", __FUNCTION__, |
| 597 | i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 601 | void CameraService::onTorchStatusChangedLocked(const String8& cameraId, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 602 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 603 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
| 604 | __FUNCTION__, cameraId.string(), newStatus); |
| 605 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 606 | TorchModeStatus status; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 607 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 608 | if (res) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 609 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", |
| 610 | __FUNCTION__, cameraId.string(), strerror(-res), res); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 611 | return; |
| 612 | } |
| 613 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 617 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 618 | if (res) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 619 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, |
| 620 | (uint32_t)newStatus, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 621 | return; |
| 622 | } |
| 623 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 624 | { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 625 | // Update battery life logging for flashlight |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 626 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 627 | auto iter = mTorchUidMap.find(cameraId); |
| 628 | if (iter != mTorchUidMap.end()) { |
| 629 | int oldUid = iter->second.second; |
| 630 | int newUid = iter->second.first; |
| 631 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 632 | if (oldUid != newUid) { |
| 633 | // 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] | 634 | if (status == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 635 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 636 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 637 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 638 | notifier.noteFlashlightOn(cameraId, newUid); |
| 639 | } |
| 640 | iter->second.second = newUid; |
| 641 | } else { |
| 642 | // If the UID has not changed, log the status |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 643 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 644 | notifier.noteFlashlightOn(cameraId, oldUid); |
| 645 | } else { |
| 646 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 651 | broadcastTorchModeStatus(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 654 | static bool hasPermissionsForSystemCamera(int callingPid, int callingUid, |
| 655 | bool logPermissionFailure = false) { |
| 656 | return checkPermission(sSystemCameraPermission, callingPid, callingUid, |
| 657 | logPermissionFailure) && |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 658 | checkPermission(sCameraPermission, callingPid, callingUid); |
| 659 | } |
| 660 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 661 | Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 662 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 663 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 664 | bool hasSystemCameraPermissions = |
| 665 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), |
| 666 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 667 | switch (type) { |
| 668 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 669 | if (hasSystemCameraPermissions) { |
| 670 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); |
| 671 | } else { |
| 672 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); |
| 673 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 674 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 675 | case CAMERA_TYPE_ALL: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 676 | if (hasSystemCameraPermissions) { |
| 677 | *numCameras = mNumberOfCameras; |
| 678 | } else { |
| 679 | *numCameras = mNumberOfCamerasWithoutSystemCamera; |
| 680 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 681 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 682 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 683 | ALOGW("%s: Unknown camera type %d", |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 684 | __FUNCTION__, type); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 685 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 686 | "Unknown camera type %d", type); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 687 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 688 | return Status::ok(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 691 | Status CameraService::getCameraInfo(int cameraId, |
| 692 | CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 693 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 694 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 695 | std::string cameraIdStr = cameraIdIntToStrLocked(cameraId); |
| 696 | if (shouldRejectSystemCameraConnection(String8(cameraIdStr.c_str()))) { |
| 697 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 698 | "characteristics for system only device %s: ", cameraIdStr.c_str()); |
| 699 | } |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 700 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 701 | if (!mInitialized) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 702 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 703 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 704 | "Camera subsystem is not available"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 705 | } |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 706 | bool hasSystemCameraPermissions = |
| 707 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), |
| 708 | CameraThreadState::getCallingUid()); |
| 709 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; |
| 710 | if (hasSystemCameraPermissions) { |
| 711 | cameraIdBound = mNumberOfCameras; |
| 712 | } |
| 713 | if (cameraId < 0 || cameraId >= cameraIdBound) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 714 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 715 | "CameraId is not valid"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 718 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 719 | status_t err = mCameraProviderManager->getCameraInfo( |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 720 | cameraIdStr.c_str(), cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 721 | if (err != OK) { |
| 722 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 723 | "Error retrieving camera info from device %d: %s (%d)", cameraId, |
| 724 | strerror(-err), err); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 725 | logServiceError(String8::format("Error retrieving camera info from device %d",cameraId), |
| 726 | ERROR_INVALID_OPERATION); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 727 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 728 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 729 | return ret; |
| 730 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 731 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 732 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 733 | const std::vector<std::string> *deviceIds = &mNormalDeviceIdsWithoutSystemCamera; |
| 734 | auto callingPid = CameraThreadState::getCallingPid(); |
| 735 | auto callingUid = CameraThreadState::getCallingUid(); |
Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 736 | if (checkPermission(sSystemCameraPermission, callingPid, callingUid, |
| 737 | /*logPermissionFailure*/false) || getpid() == callingPid) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 738 | deviceIds = &mNormalDeviceIds; |
| 739 | } |
| 740 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(deviceIds->size())) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 741 | ALOGE("%s: input id %d invalid: valid range (0, %zu)", |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 742 | __FUNCTION__, cameraIdInt, deviceIds->size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 743 | return std::string{}; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 744 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 745 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 746 | return (*deviceIds)[cameraIdInt]; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | String8 CameraService::cameraIdIntToStr(int cameraIdInt) { |
| 750 | Mutex::Autolock lock(mServiceLock); |
| 751 | return String8(cameraIdIntToStrLocked(cameraIdInt).c_str()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | Status CameraService::getCameraCharacteristics(const String16& cameraId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 755 | int targetSdkVersion, CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 756 | ATRACE_CALL(); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 757 | if (!cameraInfo) { |
| 758 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 759 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 762 | if (!mInitialized) { |
| 763 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 764 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 765 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 766 | "Camera subsystem is not available");; |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 769 | if (shouldRejectSystemCameraConnection(String8(cameraId))) { |
| 770 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 771 | "characteristics for system only device %s: ", String8(cameraId).string()); |
| 772 | } |
| 773 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 774 | Status ret{}; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 775 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 776 | |
| 777 | std::string cameraIdStr = String8(cameraId).string(); |
| 778 | bool overrideForPerfClass = |
| 779 | SessionConfigurationUtils::targetPerfClassPrimaryCamera(mPerfClassPrimaryCameraIds, |
| 780 | cameraIdStr, targetSdkVersion); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 781 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 782 | cameraIdStr, overrideForPerfClass, cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 783 | if (res != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 784 | if (res == NAME_NOT_FOUND) { |
| 785 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " |
| 786 | "characteristics for unknown device %s: %s (%d)", String8(cameraId).string(), |
| 787 | strerror(-res), res); |
| 788 | } else { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 789 | logServiceError(String8::format("Unable to retrieve camera characteristics for " |
| 790 | "device %s.", String8(cameraId).string()),ERROR_INVALID_OPERATION); |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 791 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " |
| 792 | "characteristics for device %s: %s (%d)", String8(cameraId).string(), |
| 793 | strerror(-res), res); |
| 794 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 795 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 796 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 797 | if (getSystemCameraKind(String8(cameraId), &deviceKind) != OK) { |
| 798 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, String8(cameraId).string()); |
| 799 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera kind " |
| 800 | "for device %s", String8(cameraId).string()); |
| 801 | } |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 802 | int callingPid = CameraThreadState::getCallingPid(); |
| 803 | int callingUid = CameraThreadState::getCallingUid(); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 804 | std::vector<int32_t> tagsRemoved; |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 805 | // If it's not calling from cameraserver, check the permission only if |
| 806 | // android.permission.CAMERA is required. If android.permission.SYSTEM_CAMERA was needed, |
| 807 | // it would've already been checked in shouldRejectSystemCameraConnection. |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 808 | if ((callingPid != getpid()) && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 809 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 810 | !checkPermission(sCameraPermission, callingPid, callingUid)) { |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 811 | res = cameraInfo->removePermissionEntries( |
| 812 | mCameraProviderManager->getProviderTagIdLocked(String8(cameraId).string()), |
| 813 | &tagsRemoved); |
| 814 | if (res != OK) { |
| 815 | cameraInfo->clear(); |
| 816 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to remove camera" |
| 817 | " characteristics needing camera permission for device %s: %s (%d)", |
| 818 | String8(cameraId).string(), strerror(-res), res); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | if (!tagsRemoved.empty()) { |
| 823 | res = cameraInfo->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, |
| 824 | tagsRemoved.data(), tagsRemoved.size()); |
| 825 | if (res != OK) { |
| 826 | cameraInfo->clear(); |
| 827 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to insert camera " |
| 828 | "keys needing permission for device %s: %s (%d)", String8(cameraId).string(), |
| 829 | strerror(-res), res); |
| 830 | } |
| 831 | } |
| 832 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 833 | return ret; |
| 834 | } |
| 835 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 836 | Status CameraService::getTorchStrengthLevel(const String16& cameraId, |
| 837 | int32_t* torchStrength) { |
| 838 | ATRACE_CALL(); |
| 839 | Mutex::Autolock l(mServiceLock); |
| 840 | if (!mInitialized) { |
| 841 | ALOGE("%s: Camera HAL couldn't be initialized.", __FUNCTION__); |
| 842 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera HAL couldn't be initialized."); |
| 843 | } |
| 844 | |
| 845 | if(torchStrength == NULL) { |
| 846 | ALOGE("%s: strength level must not be null.", __FUNCTION__); |
| 847 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Strength level should not be null."); |
| 848 | } |
| 849 | |
| 850 | status_t res = mCameraProviderManager->getTorchStrengthLevel(String8(cameraId).string(), |
| 851 | torchStrength); |
| 852 | if (res != OK) { |
| 853 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve torch " |
| 854 | "strength level for device %s: %s (%d)", String8(cameraId).string(), |
| 855 | strerror(-res), res); |
| 856 | } |
| 857 | ALOGI("%s: Torch strength level is: %d", __FUNCTION__, *torchStrength); |
| 858 | return Status::ok(); |
| 859 | } |
| 860 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 861 | String8 CameraService::getFormattedCurrentTime() { |
| 862 | time_t now = time(nullptr); |
| 863 | char formattedTime[64]; |
| 864 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
| 865 | return String8(formattedTime); |
| 866 | } |
| 867 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 868 | Status CameraService::getCameraVendorTagDescriptor( |
| 869 | /*out*/ |
| 870 | hardware::camera2::params::VendorTagDescriptor* desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 871 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 872 | if (!mInitialized) { |
| 873 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 874 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 875 | } |
Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 876 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 877 | if (globalDescriptor != nullptr) { |
| 878 | *desc = *(globalDescriptor.get()); |
| 879 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 880 | return Status::ok(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 881 | } |
| 882 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 883 | Status CameraService::getCameraVendorTagCache( |
| 884 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { |
| 885 | ATRACE_CALL(); |
| 886 | if (!mInitialized) { |
| 887 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 888 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 889 | "Camera subsystem not available"); |
| 890 | } |
| 891 | sp<VendorTagDescriptorCache> globalCache = |
| 892 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 893 | if (globalCache != nullptr) { |
| 894 | *cache = *(globalCache.get()); |
| 895 | } |
| 896 | return Status::ok(); |
| 897 | } |
| 898 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 899 | void CameraService::clearCachedVariables() { |
| 900 | BasicClient::BasicClient::sCameraService = nullptr; |
| 901 | } |
| 902 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 903 | std::pair<int, IPCTransport> CameraService::getDeviceVersion(const String8& cameraId, int* facing, |
| 904 | int* orientation) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 905 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 906 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 907 | int deviceVersion = 0; |
| 908 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 909 | status_t res; |
| 910 | hardware::hidl_version maxVersion{0,0}; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 911 | IPCTransport transport = IPCTransport::INVALID; |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 912 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(), |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 913 | &maxVersion, &transport); |
| 914 | if (res != OK || transport == IPCTransport::INVALID) { |
| 915 | ALOGE("%s: Unable to get highest supported version for camera id %s", __FUNCTION__, |
| 916 | cameraId.string()); |
| 917 | return std::make_pair(-1, IPCTransport::INVALID) ; |
| 918 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 919 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 920 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 921 | hardware::CameraInfo info; |
| 922 | if (facing) { |
| 923 | res = mCameraProviderManager->getCameraInfo(cameraId.string(), &info); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 924 | if (res != OK) { |
| 925 | return std::make_pair(-1, IPCTransport::INVALID); |
| 926 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 927 | *facing = info.facing; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 928 | if (orientation) { |
| 929 | *orientation = info.orientation; |
| 930 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 931 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 932 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 933 | return std::make_pair(deviceVersion, transport); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 934 | } |
| 935 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 936 | Status CameraService::filterGetInfoErrorCode(status_t err) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 937 | switch(err) { |
| 938 | case NO_ERROR: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 939 | return Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 940 | case BAD_VALUE: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 941 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 942 | "CameraId is not valid for HAL module"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 943 | case NO_INIT: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 944 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 945 | "Camera device not available"); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 946 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 947 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 948 | "Camera HAL encountered error %d: %s", |
| 949 | err, strerror(-err)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 950 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 953 | Status CameraService::makeClient(const sp<CameraService>& cameraService, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 954 | const sp<IInterface>& cameraCb, const String16& packageName, bool systemNativeClient, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 955 | const std::optional<String16>& featureId, const String8& cameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 956 | int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid, |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 957 | int servicePid, std::pair<int, IPCTransport> deviceVersionAndTransport, |
| 958 | apiLevel effectiveApiLevel, bool overrideForPerfClass, /*out*/sp<BasicClient>* client) { |
| 959 | // For HIDL devices |
| 960 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 961 | // Create CameraClient based on device version reported by the HAL. |
| 962 | int deviceVersion = deviceVersionAndTransport.first; |
| 963 | switch(deviceVersion) { |
| 964 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 965 | ALOGE("Camera using old HAL version: %d", deviceVersion); |
| 966 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, |
| 967 | "Camera device \"%s\" HAL version %d no longer supported", |
| 968 | cameraId.string(), deviceVersion); |
| 969 | break; |
| 970 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 971 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 972 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 973 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 974 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 975 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 976 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 977 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 978 | break; |
| 979 | default: |
| 980 | // Should not be reachable |
| 981 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 982 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 983 | "Camera device \"%s\" has unknown HAL version %d", |
| 984 | cameraId.string(), deviceVersion); |
| 985 | } |
| 986 | } |
| 987 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 988 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 989 | *client = new Camera2Client(cameraService, tmp, cameraService->mCameraServiceProxyWrapper, |
| 990 | packageName, featureId, cameraId, api1CameraId, facing, sensorOrientation, |
| 991 | clientPid, clientUid, servicePid, overrideForPerfClass); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 992 | } else { // Camera2 API route |
| 993 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = |
| 994 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 995 | *client = new CameraDeviceClient(cameraService, tmp, |
| 996 | cameraService->mCameraServiceProxyWrapper, packageName, systemNativeClient, |
| 997 | featureId, cameraId, facing, sensorOrientation, clientPid, clientUid, servicePid, |
| 998 | overrideForPerfClass); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 999 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1000 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1003 | String8 CameraService::toString(std::set<userid_t> intSet) { |
| 1004 | String8 s(""); |
| 1005 | bool first = true; |
| 1006 | for (userid_t i : intSet) { |
| 1007 | if (first) { |
| 1008 | s.appendFormat("%d", i); |
| 1009 | first = false; |
| 1010 | } else { |
| 1011 | s.appendFormat(", %d", i); |
| 1012 | } |
| 1013 | } |
| 1014 | return s; |
| 1015 | } |
| 1016 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1017 | int32_t CameraService::mapToInterface(TorchModeStatus status) { |
| 1018 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1019 | switch (status) { |
| 1020 | case TorchModeStatus::NOT_AVAILABLE: |
| 1021 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1022 | break; |
| 1023 | case TorchModeStatus::AVAILABLE_OFF: |
| 1024 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 1025 | break; |
| 1026 | case TorchModeStatus::AVAILABLE_ON: |
| 1027 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
| 1028 | break; |
| 1029 | default: |
| 1030 | ALOGW("Unknown new flash status: %d", status); |
| 1031 | } |
| 1032 | return serviceStatus; |
| 1033 | } |
| 1034 | |
| 1035 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { |
| 1036 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; |
| 1037 | switch (status) { |
| 1038 | case CameraDeviceStatus::NOT_PRESENT: |
| 1039 | serviceStatus = StatusInternal::NOT_PRESENT; |
| 1040 | break; |
| 1041 | case CameraDeviceStatus::PRESENT: |
| 1042 | serviceStatus = StatusInternal::PRESENT; |
| 1043 | break; |
| 1044 | case CameraDeviceStatus::ENUMERATING: |
| 1045 | serviceStatus = StatusInternal::ENUMERATING; |
| 1046 | break; |
| 1047 | default: |
| 1048 | ALOGW("Unknown new HAL device status: %d", status); |
| 1049 | } |
| 1050 | return serviceStatus; |
| 1051 | } |
| 1052 | |
| 1053 | int32_t CameraService::mapToInterface(StatusInternal status) { |
| 1054 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1055 | switch (status) { |
| 1056 | case StatusInternal::NOT_PRESENT: |
| 1057 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1058 | break; |
| 1059 | case StatusInternal::PRESENT: |
| 1060 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; |
| 1061 | break; |
| 1062 | case StatusInternal::ENUMERATING: |
| 1063 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; |
| 1064 | break; |
| 1065 | case StatusInternal::NOT_AVAILABLE: |
| 1066 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; |
| 1067 | break; |
| 1068 | case StatusInternal::UNKNOWN: |
| 1069 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; |
| 1070 | break; |
| 1071 | default: |
| 1072 | ALOGW("Unknown new internal device status: %d", status); |
| 1073 | } |
| 1074 | return serviceStatus; |
| 1075 | } |
| 1076 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1077 | Status CameraService::initializeShimMetadata(int cameraId) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1078 | int uid = CameraThreadState::getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1079 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1080 | String16 internalPackageName("cameraserver"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1081 | String8 id = String8::format("%d", cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1082 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1083 | sp<Client> tmp = nullptr; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1084 | if (!(ret = connectHelper<ICameraClient,Client>( |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1085 | sp<ICameraClient>{nullptr}, id, cameraId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1086 | internalPackageName, /*systemNativeClient*/ false, {}, uid, USE_CALLING_PID, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1087 | API_1, /*shimUpdateOnly*/ true, /*oomScoreOffset*/ 0, |
| 1088 | /*targetSdkVersion*/ __ANDROID_API_FUTURE__, /*out*/ tmp) |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1089 | ).isOk()) { |
| 1090 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string()); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1091 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1092 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1095 | Status CameraService::getLegacyParametersLazy(int cameraId, |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1096 | /*out*/ |
| 1097 | CameraParameters* parameters) { |
| 1098 | |
| 1099 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 1100 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1101 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1102 | |
| 1103 | if (parameters == NULL) { |
| 1104 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1105 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1108 | String8 id = String8::format("%d", cameraId); |
| 1109 | |
| 1110 | // Check if we already have parameters |
| 1111 | { |
| 1112 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1113 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1114 | auto cameraState = getCameraState(id); |
| 1115 | if (cameraState == nullptr) { |
| 1116 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1117 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1118 | "Invalid camera ID: %s", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1119 | } |
| 1120 | CameraParameters p = cameraState->getShimParams(); |
| 1121 | if (!p.isEmpty()) { |
| 1122 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1123 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1127 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1128 | ret = initializeShimMetadata(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1129 | CameraThreadState::restoreCallingIdentity(token); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1130 | if (!ret.isOk()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1131 | // Error already logged by callee |
| 1132 | return ret; |
| 1133 | } |
| 1134 | |
| 1135 | // Check for parameters again |
| 1136 | { |
| 1137 | // Scope for service lock |
| 1138 | Mutex::Autolock lock(mServiceLock); |
| 1139 | auto cameraState = getCameraState(id); |
| 1140 | if (cameraState == nullptr) { |
| 1141 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1142 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1143 | "Invalid camera ID: %s", id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1144 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1145 | CameraParameters p = cameraState->getShimParams(); |
| 1146 | if (!p.isEmpty()) { |
| 1147 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1148 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1152 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 1153 | __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1154 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1157 | // Can camera service trust the caller based on the calling UID? |
| 1158 | static bool isTrustedCallingUid(uid_t uid) { |
| 1159 | switch (uid) { |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1160 | case AID_MEDIA: // mediaserver |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1161 | case AID_CAMERASERVER: // cameraserver |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1162 | case AID_RADIO: // telephony |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1163 | return true; |
| 1164 | default: |
| 1165 | return false; |
| 1166 | } |
| 1167 | } |
| 1168 | |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 1169 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { |
| 1170 | PermissionController pc; |
| 1171 | uid = pc.getPackageUid(packageName, 0); |
| 1172 | if (uid <= 0) { |
| 1173 | ALOGE("Unknown package: '%s'", String8(packageName).string()); |
| 1174 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).string()); |
| 1175 | return BAD_VALUE; |
| 1176 | } |
| 1177 | |
| 1178 | if (userId < 0) { |
| 1179 | ALOGE("Invalid user: %d", userId); |
| 1180 | dprintf(err, "Invalid user: %d\n", userId); |
| 1181 | return BAD_VALUE; |
| 1182 | } |
| 1183 | |
| 1184 | uid = multiuser_get_uid(userId, uid); |
| 1185 | return NO_ERROR; |
| 1186 | } |
| 1187 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1188 | Status CameraService::validateConnectLocked(const String8& cameraId, |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1189 | const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, |
| 1190 | /*out*/int& originalClientPid) const { |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1191 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1192 | #ifdef __BRILLO__ |
| 1193 | UNUSED(clientName8); |
| 1194 | UNUSED(clientUid); |
| 1195 | UNUSED(clientPid); |
| 1196 | UNUSED(originalClientPid); |
| 1197 | #else |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1198 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, |
| 1199 | originalClientPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1200 | if (!allowed.isOk()) { |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1201 | return allowed; |
| 1202 | } |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1203 | #endif // __BRILLO__ |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1204 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1205 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1206 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1207 | if (!mInitialized) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1208 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 1209 | callingPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1210 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1211 | "No camera HAL module available to open camera device \"%s\"", cameraId.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1214 | if (getCameraState(cameraId) == nullptr) { |
| 1215 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1216 | cameraId.string()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1217 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1218 | "No camera device with ID \"%s\" available", cameraId.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1221 | status_t err = checkIfDeviceIsUsable(cameraId); |
| 1222 | if (err != NO_ERROR) { |
| 1223 | switch(err) { |
| 1224 | case -ENODEV: |
| 1225 | case -EBUSY: |
| 1226 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1227 | "No camera device with ID \"%s\" currently available", cameraId.string()); |
| 1228 | default: |
| 1229 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1230 | "Unknown error connecting to ID \"%s\"", cameraId.string()); |
| 1231 | } |
| 1232 | } |
| 1233 | return Status::ok(); |
Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1234 | } |
| 1235 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1236 | Status CameraService::validateClientPermissionsLocked(const String8& cameraId, |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1237 | const String8& clientName8, int& clientUid, int& clientPid, |
| 1238 | /*out*/int& originalClientPid) const { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1239 | int callingPid = CameraThreadState::getCallingPid(); |
| 1240 | int callingUid = CameraThreadState::getCallingUid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1241 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1242 | // Check if we can trust clientUid |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1243 | if (clientUid == USE_CALLING_UID) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1244 | clientUid = callingUid; |
| 1245 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1246 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1247 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1248 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1249 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1250 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1251 | callingPid, callingUid, cameraId.string(), |
| 1252 | clientName8.string(), clientUid, clientPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1255 | // Check if we can trust clientPid |
| 1256 | if (clientPid == USE_CALLING_PID) { |
| 1257 | clientPid = callingPid; |
| 1258 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1259 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1260 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1261 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1262 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1263 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1264 | callingPid, callingUid, cameraId.string(), |
| 1265 | clientName8.string(), clientUid, clientPid); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1266 | } |
| 1267 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1268 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1269 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", |
| 1270 | cameraId.c_str()); |
| 1271 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, "No camera device with ID \"%s\" is" |
| 1272 | "available", cameraId.string()); |
| 1273 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1274 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1275 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 1276 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 1277 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "No camera device with ID \"%s\"" |
| 1278 | "found while trying to query device kind", cameraId.string()); |
| 1279 | |
| 1280 | } |
| 1281 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1282 | // If it's not calling from cameraserver, check the permission if the |
| 1283 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for |
| 1284 | // android.permission.SYSTEM_CAMERA for system only camera devices). |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1285 | if (callingPid != getpid() && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1286 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1287 | !checkPermission(sCameraPermission, clientPid, clientUid)) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1288 | 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] | 1289 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1290 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission", |
| 1291 | clientName8.string(), clientUid, clientPid, cameraId.string()); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1294 | // 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] | 1295 | if (!mUidPolicy->isUidActive(callingUid, String16(clientName8))) { |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1296 | int32_t procState = mUidPolicy->getProcState(callingUid); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1297 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", |
| 1298 | clientPid, clientUid); |
| 1299 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1300 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" |
| 1301 | "calling UID %d proc state %" PRId32 ")", |
| 1302 | clientName8.string(), clientUid, clientPid, cameraId.string(), |
| 1303 | callingUid, procState); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1304 | } |
| 1305 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1306 | // If sensor privacy is enabled then prevent access to the camera |
| 1307 | if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
| 1308 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); |
| 1309 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1310 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" when sensor privacy " |
| 1311 | "is enabled", clientName8.string(), clientUid, clientPid, cameraId.string()); |
| 1312 | } |
| 1313 | |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1314 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's |
| 1315 | // connected to camera service directly. |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1316 | originalClientPid = clientPid; |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1317 | clientPid = callingPid; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1318 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1319 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1320 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1321 | // For non-system clients : Only allow clients who are being used by the current foreground |
| 1322 | // device user, unless calling from our own process. |
| 1323 | if (!doesClientHaveSystemUid() && callingPid != getpid() && |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1324 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1325 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 1326 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
| 1327 | toString(mAllowedUsers).string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1328 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1329 | "Callers from device user %d are not currently allowed to connect to camera \"%s\"", |
| 1330 | clientUserId, cameraId.string()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1333 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const { |
| 1337 | auto cameraState = getCameraState(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1338 | int callingPid = CameraThreadState::getCallingPid(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1339 | if (cameraState == nullptr) { |
| 1340 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1341 | cameraId.string()); |
| 1342 | return -ENODEV; |
| 1343 | } |
| 1344 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1345 | StatusInternal currentStatus = cameraState->getStatus(); |
| 1346 | if (currentStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1347 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
| 1348 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1349 | return -ENODEV; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1350 | } else if (currentStatus == StatusInternal::ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1351 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
| 1352 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1353 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1354 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1355 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1356 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1357 | } |
| 1358 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1359 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1360 | const CameraService::DescriptorPtr& desc, int oomScoreOffset, bool systemNativeClient) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1361 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1362 | // Make a descriptor for the incoming client |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1363 | auto clientDescriptor = |
| 1364 | CameraService::CameraClientManager::makeClientDescriptor(client, desc, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1365 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1366 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 1367 | |
| 1368 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
| 1369 | String8(client->getPackageName())); |
| 1370 | |
| 1371 | if (evicted.size() > 0) { |
| 1372 | // This should never happen - clients should already have been removed in disconnect |
| 1373 | for (auto& i : evicted) { |
| 1374 | ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect", |
| 1375 | __FUNCTION__, i->getKey().string()); |
| 1376 | } |
| 1377 | |
| 1378 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 1379 | __FUNCTION__); |
| 1380 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1381 | |
| 1382 | // And register a death notification for the client callback. Do |
| 1383 | // this last to avoid Binder policy where a nested Binder |
| 1384 | // transaction might be pre-empted to service the client death |
| 1385 | // notification if the client process dies before linkToDeath is |
| 1386 | // invoked. |
| 1387 | sp<IBinder> remoteCallback = client->getRemote(); |
| 1388 | if (remoteCallback != nullptr) { |
| 1389 | remoteCallback->linkToDeath(this); |
| 1390 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid, |
| 1394 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1395 | int oomScoreOffset, bool systemNativeClient, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1396 | /*out*/ |
| 1397 | sp<BasicClient>* client, |
| 1398 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1399 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1400 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1401 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1402 | DescriptorPtr clientDescriptor; |
| 1403 | { |
| 1404 | if (effectiveApiLevel == API_1) { |
| 1405 | // If we are using API1, any existing client for this camera ID with the same remote |
| 1406 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 1407 | |
| 1408 | auto current = mActiveClientManager.get(cameraId); |
| 1409 | if (current != nullptr) { |
| 1410 | auto clientSp = current->getValue(); |
| 1411 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1412 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
Shuzhen Wang | b2d43f6 | 2021-08-25 14:01:11 -0700 | [diff] [blame] | 1413 | ALOGW("CameraService connect called with a different" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1414 | " API level, evicting prior client..."); |
| 1415 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1416 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1417 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1418 | *client = clientSp; |
| 1419 | return NO_ERROR; |
| 1420 | } |
| 1421 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1422 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1423 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 1424 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1425 | // Get current active client PIDs |
| 1426 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 1427 | ownerPids.push_back(clientPid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1428 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1429 | std::vector<int> priorityScores(ownerPids.size()); |
| 1430 | std::vector<int> states(ownerPids.size()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1431 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1432 | // Get priority scores of all active PIDs |
| 1433 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids( |
| 1434 | ownerPids.size(), &ownerPids[0], /*out*/&states[0], |
| 1435 | /*out*/&priorityScores[0]); |
| 1436 | if (err != OK) { |
| 1437 | ALOGE("%s: Priority score query failed: %d", |
| 1438 | __FUNCTION__, err); |
| 1439 | return err; |
| 1440 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1441 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1442 | // Update all active clients' priorities |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1443 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1444 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1445 | pidToPriorityMap.emplace(ownerPids[i], |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 1446 | resource_policy::ClientPriority(priorityScores[i], states[i], |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1447 | /* isVendorClient won't get copied over*/ false, |
| 1448 | /* oomScoreOffset won't get copied over*/ 0)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1449 | } |
| 1450 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1451 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1452 | // Get state for the given cameraId |
| 1453 | auto state = getCameraState(cameraId); |
| 1454 | if (state == nullptr) { |
| 1455 | ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", |
| 1456 | clientPid, cameraId.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1457 | // Should never get here because validateConnectLocked should have errored out |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1458 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1459 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1460 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1461 | int32_t actualScore = priorityScores[priorityScores.size() - 1]; |
| 1462 | int32_t actualState = states[states.size() - 1]; |
| 1463 | |
| 1464 | // Make descriptor for incoming client. We store the oomScoreOffset |
| 1465 | // since we might need it later on new handleEvictionsLocked and |
| 1466 | // ProcessInfoService would not take that into account. |
Shuzhen Wang | 2db86ff | 2018-04-25 01:11:17 +0000 | [diff] [blame] | 1467 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1468 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1469 | state->getConflicting(), actualScore, clientPid, actualState, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1470 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1471 | |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1472 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); |
| 1473 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1474 | // Find clients that would be evicted |
| 1475 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 1476 | |
| 1477 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 1478 | // background, so we cannot do evictions |
| 1479 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 1480 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 1481 | " priority).", clientPid); |
| 1482 | |
| 1483 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1484 | String8 curTime = getFormattedCurrentTime(); |
| 1485 | auto incompatibleClients = |
| 1486 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 1487 | |
| 1488 | 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] | 1489 | "(PID %d, score %d state %d) due to eviction policy", curTime.string(), |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1490 | cameraId.string(), packageName.string(), clientPid, |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1491 | clientPriority.getScore(), clientPriority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1492 | |
| 1493 | for (auto& i : incompatibleClients) { |
| 1494 | msg.appendFormat("\n - Blocked by existing device %s client for package %s" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1495 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", |
| 1496 | i->getKey().string(), |
| 1497 | String8{i->getValue()->getPackageName()}.string(), |
| 1498 | i->getOwnerId(), i->getPriority().getScore(), |
| 1499 | i->getPriority().getState()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1500 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1501 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(), |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1502 | String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1503 | i->getPriority().getScore(), i->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1507 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1508 | mEventLog.add(msg); |
| 1509 | |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1510 | auto current = mActiveClientManager.get(cameraId); |
| 1511 | if (current != nullptr) { |
| 1512 | return -EBUSY; // CAMERA_IN_USE |
| 1513 | } else { |
| 1514 | return -EUSERS; // MAX_CAMERAS_IN_USE |
| 1515 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | for (auto& i : evicted) { |
| 1519 | sp<BasicClient> clientSp = i->getValue(); |
| 1520 | if (clientSp.get() == nullptr) { |
| 1521 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 1522 | |
| 1523 | // TODO: Remove this |
| 1524 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 1525 | __FUNCTION__); |
| 1526 | mActiveClientManager.remove(i); |
| 1527 | continue; |
| 1528 | } |
| 1529 | |
| 1530 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
| 1531 | i->getKey().string()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1532 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1533 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1534 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1535 | logEvent(String8::format("EVICT device %s client held by package %s (PID" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1536 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" |
| 1537 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1538 | i->getKey().string(), String8{clientSp->getPackageName()}.string(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1539 | i->getOwnerId(), i->getPriority().getScore(), |
| 1540 | i->getPriority().getState(), cameraId.string(), |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1541 | packageName.string(), clientPid, clientPriority.getScore(), |
| 1542 | clientPriority.getState())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1543 | |
| 1544 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1545 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1546 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1547 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1548 | } |
| 1549 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1550 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 1551 | // other clients from connecting in mServiceLockWrapper if held |
| 1552 | mServiceLock.unlock(); |
| 1553 | |
| 1554 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1555 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1556 | |
| 1557 | // Destroy evicted clients |
| 1558 | for (auto& i : evictedClients) { |
| 1559 | // 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] | 1560 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1561 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1562 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1563 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1564 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1565 | for (const auto& i : evictedClients) { |
| 1566 | ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")", |
| 1567 | __FUNCTION__, i->getKey().string(), i->getOwnerId()); |
| 1568 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 1569 | if (ret == TIMED_OUT) { |
| 1570 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
| 1571 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), |
| 1572 | mActiveClientManager.toString().string()); |
| 1573 | return -EBUSY; |
| 1574 | } |
| 1575 | if (ret != NO_ERROR) { |
| 1576 | ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), " |
| 1577 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret), |
| 1578 | ret, mActiveClientManager.toString().string()); |
| 1579 | return ret; |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | evictedClients.clear(); |
| 1584 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1585 | // Once clients have been disconnected, relock |
| 1586 | mServiceLock.lock(); |
| 1587 | |
| 1588 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 1589 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 1590 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1593 | *partial = clientDescriptor; |
| 1594 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1595 | } |
| 1596 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1597 | Status CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1598 | const sp<ICameraClient>& cameraClient, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1599 | int api1CameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1600 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1601 | int clientUid, |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1602 | int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1603 | int targetSdkVersion, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1604 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1605 | sp<ICamera>* device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1606 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1607 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1608 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1609 | |
| 1610 | String8 id = cameraIdIntToStr(api1CameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1611 | sp<Client> client = nullptr; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1612 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1613 | clientPackageName,/*systemNativeClient*/ false, {}, clientUid, clientPid, API_1, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1614 | /*shimUpdateOnly*/ false, /*oomScoreOffset*/ 0, targetSdkVersion, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1615 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1616 | if(!ret.isOk()) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1617 | logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageName), |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1618 | ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1619 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1622 | *device = client; |
| 1623 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1626 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, |
| 1627 | bool isVendorListener, int clientPid, int clientUid) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1628 | // If the client is not a vendor client, don't add listener if |
| 1629 | // a) the camera is a publicly hidden secure camera OR |
| 1630 | // b) the camera is a system only camera and the client doesn't |
| 1631 | // have android.permission.SYSTEM_CAMERA permissions. |
| 1632 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || |
| 1633 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
| 1634 | !hasPermissionsForSystemCamera(clientPid, clientUid)))) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 1635 | return true; |
| 1636 | } |
| 1637 | return false; |
| 1638 | } |
| 1639 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1640 | bool CameraService::shouldRejectSystemCameraConnection(const String8& cameraId) const { |
| 1641 | // Rules for rejection: |
| 1642 | // 1) If cameraserver tries to access this camera device, accept the |
| 1643 | // connection. |
| 1644 | // 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] | 1645 | // non system component is trying to access it. |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1646 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY |
| 1647 | // and the serving thread is a non hwbinder thread, the client must have |
| 1648 | // android.permission.SYSTEM_CAMERA permissions to connect. |
| 1649 | |
| 1650 | int cPid = CameraThreadState::getCallingPid(); |
| 1651 | int cUid = CameraThreadState::getCallingUid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1652 | bool systemClient = doesClientHaveSystemUid(); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1653 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 1654 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1655 | // This isn't a known camera ID, so it's not a system camera |
| 1656 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); |
| 1657 | return false; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1658 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1659 | |
| 1660 | // (1) Cameraserver trying to connect, accept. |
| 1661 | if (CameraThreadState::getCallingPid() == getpid()) { |
| 1662 | return false; |
| 1663 | } |
| 1664 | // (2) |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1665 | if (!systemClient && systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1666 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); |
| 1667 | return true; |
| 1668 | } |
| 1669 | // (3) Here we only check for permissions if it is a system only camera device. This is since |
| 1670 | // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some |
| 1671 | // characteristics) even if clients don't have android.permission.CAMERA. We do not want the |
| 1672 | // same behavior for system camera devices. |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1673 | if (!systemClient && systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 1674 | !hasPermissionsForSystemCamera(cPid, cUid, /*logPermissionFailure*/true)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1675 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", |
| 1676 | cameraId.c_str()); |
| 1677 | return true; |
| 1678 | } |
| 1679 | |
| 1680 | return false; |
| 1681 | } |
| 1682 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1683 | Status CameraService::connectDevice( |
| 1684 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1685 | const String16& cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1686 | const String16& clientPackageName, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1687 | const std::optional<String16>& clientFeatureId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1688 | int clientUid, int oomScoreOffset, int targetSdkVersion, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1689 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1690 | sp<hardware::camera2::ICameraDeviceUser>* device) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1691 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1692 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1693 | Status ret = Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1694 | String8 id = String8(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1695 | sp<CameraDeviceClient> client = nullptr; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1696 | String16 clientPackageNameAdj = clientPackageName; |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1697 | int callingPid = CameraThreadState::getCallingPid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1698 | bool systemNativeClient = false; |
| 1699 | if (doesClientHaveSystemUid() && (clientPackageNameAdj.size() == 0)) { |
| 1700 | std::string systemClient = |
| 1701 | StringPrintf("client.pid<%d>", CameraThreadState::getCallingPid()); |
| 1702 | clientPackageNameAdj = String16(systemClient.c_str()); |
| 1703 | systemNativeClient = true; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1704 | } |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1705 | |
| 1706 | if (oomScoreOffset < 0) { |
| 1707 | String8 msg = |
| 1708 | String8::format("Cannot increase the priority of a client %s pid %d for " |
| 1709 | "camera id %s", String8(clientPackageNameAdj).string(), callingPid, |
| 1710 | id.string()); |
| 1711 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1712 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 1713 | } |
| 1714 | |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 1715 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 1716 | int callingUid = CameraThreadState::getCallingUid(); |
| 1717 | if (clientUid == USE_CALLING_UID) { |
| 1718 | clientUserId = multiuser_get_user_id(callingUid); |
| 1719 | } |
| 1720 | |
| 1721 | if (mCameraServiceProxyWrapper->isCameraDisabled(clientUserId)) { |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 1722 | String8 msg = |
| 1723 | String8::format("Camera disabled by device policy"); |
| 1724 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1725 | return STATUS_ERROR(ERROR_DISABLED, msg.string()); |
| 1726 | } |
| 1727 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1728 | // enforce system camera permissions |
| 1729 | if (oomScoreOffset > 0 && |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 1730 | !hasPermissionsForSystemCamera(callingPid, CameraThreadState::getCallingUid()) && |
| 1731 | !isTrustedCallingUid(CameraThreadState::getCallingUid())) { |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1732 | String8 msg = |
| 1733 | String8::format("Cannot change the priority of a client %s pid %d for " |
| 1734 | "camera id %s without SYSTEM_CAMERA permissions", |
| 1735 | String8(clientPackageNameAdj).string(), callingPid, id.string()); |
| 1736 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1737 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, msg.string()); |
| 1738 | } |
| 1739 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1740 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1741 | /*api1CameraId*/-1, clientPackageNameAdj, systemNativeClient,clientFeatureId, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1742 | clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, oomScoreOffset, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1743 | targetSdkVersion, /*out*/client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1744 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1745 | if(!ret.isOk()) { |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1746 | logRejected(id, callingPid, String8(clientPackageNameAdj), ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1747 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1748 | } |
| 1749 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1750 | *device = client; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 1751 | Mutex::Autolock lock(mServiceLock); |
| 1752 | |
| 1753 | // Clear the previous cached logs and reposition the |
| 1754 | // file offset to beginning of the file to log new data. |
| 1755 | // If either truncate or lseek fails, close the previous file and create a new one. |
| 1756 | if ((ftruncate(mMemFd, 0) == -1) || (lseek(mMemFd, 0, SEEK_SET) == -1)) { |
| 1757 | ALOGE("%s: Error while truncating the file: %s", __FUNCTION__, sFileName); |
| 1758 | // Close the previous memfd. |
| 1759 | close(mMemFd); |
| 1760 | // If failure to wipe the data, then create a new file and |
| 1761 | // assign the new value to mMemFd. |
| 1762 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 1763 | if (mMemFd == -1) { |
| 1764 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 1765 | } |
| 1766 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1767 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1768 | } |
| 1769 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1770 | String16 CameraService::getPackageNameFromUid(int clientUid) { |
| 1771 | String16 packageName(""); |
| 1772 | |
| 1773 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1774 | sp<IBinder> binder = sm->getService(String16(kPermissionServiceName)); |
| 1775 | if (binder == 0) { |
| 1776 | ALOGE("Cannot get permission service"); |
| 1777 | // Return empty package name and the further interaction |
| 1778 | // with camera will likely fail |
| 1779 | return packageName; |
| 1780 | } |
| 1781 | |
| 1782 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 1783 | Vector<String16> packages; |
| 1784 | |
| 1785 | permCtrl->getPackagesForUid(clientUid, packages); |
| 1786 | |
| 1787 | if (packages.isEmpty()) { |
| 1788 | ALOGE("No packages for calling UID %d", clientUid); |
| 1789 | // Return empty package name and the further interaction |
| 1790 | // with camera will likely fail |
| 1791 | return packageName; |
| 1792 | } |
| 1793 | |
| 1794 | // Arbitrarily pick the first name in the list |
| 1795 | packageName = packages[0]; |
| 1796 | |
| 1797 | return packageName; |
| 1798 | } |
| 1799 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1800 | template<class CALLBACK, class CLIENT> |
| 1801 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1802 | int api1CameraId, const String16& clientPackageNameMaybe, bool systemNativeClient, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1803 | const std::optional<String16>& clientFeatureId, int clientUid, int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1804 | apiLevel effectiveApiLevel, bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1805 | /*out*/sp<CLIENT>& device) { |
| 1806 | binder::Status ret = binder::Status::ok(); |
| 1807 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1808 | bool isNonSystemNdk = false; |
| 1809 | String16 clientPackageName; |
| 1810 | if (clientPackageNameMaybe.size() <= 0) { |
| 1811 | // NDK calls don't come with package names, but we need one for various cases. |
| 1812 | // Generally, there's a 1:1 mapping between UID and package name, but shared UIDs |
| 1813 | // do exist. For all authentication cases, all packages under the same UID get the |
| 1814 | // same permissions, so picking any associated package name is sufficient. For some |
| 1815 | // other cases, this may give inaccurate names for clients in logs. |
| 1816 | isNonSystemNdk = true; |
| 1817 | int packageUid = (clientUid == USE_CALLING_UID) ? |
| 1818 | CameraThreadState::getCallingUid() : clientUid; |
| 1819 | clientPackageName = getPackageNameFromUid(packageUid); |
| 1820 | } else { |
| 1821 | clientPackageName = clientPackageNameMaybe; |
| 1822 | } |
| 1823 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1824 | String8 clientName8(clientPackageName); |
| 1825 | |
| 1826 | int originalClientPid = 0; |
| 1827 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1828 | int packagePid = (clientPid == USE_CALLING_PID) ? |
| 1829 | CameraThreadState::getCallingPid() : clientPid; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1830 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1831 | "Camera API version %d", packagePid, clientName8.string(), cameraId.string(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1832 | static_cast<int>(effectiveApiLevel)); |
| 1833 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1834 | nsecs_t openTimeNs = systemTime(); |
| 1835 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1836 | sp<CLIENT> client = nullptr; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1837 | int facing = -1; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1838 | int orientation = 0; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1839 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1840 | { |
| 1841 | // Acquire mServiceLock and prevent other clients from connecting |
| 1842 | std::unique_ptr<AutoConditionLock> lock = |
| 1843 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 1844 | |
| 1845 | if (lock == nullptr) { |
| 1846 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." |
| 1847 | , clientPid); |
| 1848 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1849 | "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting", |
| 1850 | cameraId.string(), clientName8.string(), clientPid); |
| 1851 | } |
| 1852 | |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 1853 | // Enforce client permissions and do basic validity checks |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1854 | if(!(ret = validateConnectLocked(cameraId, clientName8, |
| 1855 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { |
| 1856 | return ret; |
| 1857 | } |
| 1858 | |
| 1859 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 1860 | // we were doing a shim update, return immediately |
| 1861 | if (shimUpdateOnly) { |
| 1862 | auto cameraState = getCameraState(cameraId); |
| 1863 | if (cameraState != nullptr) { |
| 1864 | if (!cameraState->getShimParams().isEmpty()) return ret; |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | status_t err; |
| 1869 | |
| 1870 | sp<BasicClient> clientTmp = nullptr; |
| 1871 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial; |
| 1872 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1873 | IInterface::asBinder(cameraCb), clientName8, oomScoreOffset, systemNativeClient, |
| 1874 | /*out*/&clientTmp, /*out*/&partial)) != NO_ERROR) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1875 | switch (err) { |
| 1876 | case -ENODEV: |
| 1877 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1878 | "No camera device with ID \"%s\" currently available", |
| 1879 | cameraId.string()); |
| 1880 | case -EBUSY: |
| 1881 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 1882 | "Higher-priority client using camera, ID \"%s\" currently unavailable", |
| 1883 | cameraId.string()); |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1884 | case -EUSERS: |
| 1885 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1886 | "Too many cameras already open, cannot open camera \"%s\"", |
| 1887 | cameraId.string()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1888 | default: |
| 1889 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1890 | "Unexpected error %s (%d) opening camera \"%s\"", |
| 1891 | strerror(-err), err, cameraId.string()); |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | if (clientTmp.get() != nullptr) { |
| 1896 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 1897 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 1898 | return ret; |
| 1899 | } |
| 1900 | |
| 1901 | // give flashlight a chance to close devices if necessary. |
| 1902 | mFlashlight->prepareDeviceOpen(cameraId); |
| 1903 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1904 | auto deviceVersionAndTransport = |
| 1905 | getDeviceVersion(cameraId, /*out*/&facing, /*out*/&orientation); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 1906 | if (facing == -1) { |
| 1907 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string()); |
| 1908 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1909 | "Unable to get camera device \"%s\" facing", cameraId.string()); |
| 1910 | } |
| 1911 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1912 | sp<BasicClient> tmp = nullptr; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1913 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
| 1914 | mPerfClassPrimaryCameraIds, cameraId.string(), targetSdkVersion); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1915 | if(!(ret = makeClient(this, cameraCb, clientPackageName, systemNativeClient, |
| 1916 | clientFeatureId, cameraId, api1CameraId, facing, orientation, |
Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 1917 | clientPid, clientUid, getpid(), |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1918 | deviceVersionAndTransport, effectiveApiLevel, overrideForPerfClass, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1919 | /*out*/&tmp)).isOk()) { |
| 1920 | return ret; |
| 1921 | } |
| 1922 | client = static_cast<CLIENT*>(tmp.get()); |
| 1923 | |
| 1924 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 1925 | __FUNCTION__); |
| 1926 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 1927 | String8 monitorTags = isClientWatched(client.get()) ? mMonitorTags : String8(""); |
| 1928 | err = client->initialize(mCameraProviderManager, monitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1929 | if (err != OK) { |
| 1930 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1931 | // Errors could be from the HAL module open call or from AppOpsManager |
| 1932 | switch(err) { |
| 1933 | case BAD_VALUE: |
| 1934 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1935 | "Illegal argument to HAL module for camera \"%s\"", cameraId.string()); |
| 1936 | case -EBUSY: |
| 1937 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 1938 | "Camera \"%s\" is already open", cameraId.string()); |
| 1939 | case -EUSERS: |
| 1940 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1941 | "Too many cameras already open, cannot open camera \"%s\"", |
| 1942 | cameraId.string()); |
| 1943 | case PERMISSION_DENIED: |
| 1944 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1945 | "No permission to open camera \"%s\"", cameraId.string()); |
| 1946 | case -EACCES: |
| 1947 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1948 | "Camera \"%s\" disabled by policy", cameraId.string()); |
| 1949 | case -ENODEV: |
| 1950 | default: |
| 1951 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1952 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(), |
| 1953 | strerror(-err), err); |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | // Update shim paremeters for legacy clients |
| 1958 | if (effectiveApiLevel == API_1) { |
| 1959 | // Assume we have always received a Client subclass for API1 |
| 1960 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 1961 | String8 rawParams = shimClient->getParameters(); |
| 1962 | CameraParameters params(rawParams); |
| 1963 | |
| 1964 | auto cameraState = getCameraState(cameraId); |
| 1965 | if (cameraState != nullptr) { |
| 1966 | cameraState->setShimParams(params); |
| 1967 | } else { |
| 1968 | ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.", |
| 1969 | __FUNCTION__, cameraId.string()); |
| 1970 | } |
| 1971 | } |
| 1972 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 1973 | // Enable/disable camera service watchdog |
| 1974 | client->setCameraServiceWatchdog(mCameraServiceWatchdogEnabled); |
| 1975 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1976 | // Set rotate-and-crop override behavior |
| 1977 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { |
| 1978 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame] | 1979 | } else { |
Emilian Peev | 065b2c1 | 2021-11-23 13:12:57 -0800 | [diff] [blame] | 1980 | client->setRotateAndCropOverride( |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1981 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
Emilian Peev | 065b2c1 | 2021-11-23 13:12:57 -0800 | [diff] [blame] | 1982 | clientPackageName, facing, multiuser_get_user_id(clientUid))); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1983 | } |
| 1984 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 1985 | // Set camera muting behavior |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 1986 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 1987 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 1988 | if (client->supportsCameraMute()) { |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 1989 | client->setCameraMute( |
| 1990 | mOverrideCameraMuteMode || isCameraPrivacyEnabled); |
| 1991 | } else if (isCameraPrivacyEnabled) { |
| 1992 | // no camera mute supported, but privacy is on! => disconnect |
| 1993 | ALOGI("Camera mute not supported for package: %s, camera id: %s", |
| 1994 | String8(client->getPackageName()).string(), cameraId.string()); |
| 1995 | // Do not hold mServiceLock while disconnecting clients, but |
| 1996 | // retain the condition blocking other clients from connecting |
| 1997 | // in mServiceLockWrapper if held. |
| 1998 | mServiceLock.unlock(); |
| 1999 | // Clear caller identity temporarily so client disconnect PID |
| 2000 | // checks work correctly |
| 2001 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 2002 | // Note AppOp to trigger the "Unblock" dialog |
| 2003 | client->noteAppOp(); |
| 2004 | client->disconnect(); |
| 2005 | CameraThreadState::restoreCallingIdentity(token); |
| 2006 | // Reacquire mServiceLock |
| 2007 | mServiceLock.lock(); |
| 2008 | |
| 2009 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 2010 | "Camera \"%s\" disabled due to camera mute", cameraId.string()); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2011 | } |
| 2012 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2013 | if (shimUpdateOnly) { |
| 2014 | // If only updating legacy shim parameters, immediately disconnect client |
| 2015 | mServiceLock.unlock(); |
| 2016 | client->disconnect(); |
| 2017 | mServiceLock.lock(); |
| 2018 | } else { |
| 2019 | // Otherwise, add client to active clients list |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2020 | finishConnectLocked(client, partial, oomScoreOffset, systemNativeClient); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2021 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 2022 | |
| 2023 | client->setImageDumpMask(mImageDumpMask); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2024 | } // lock is destroyed, allow further connect calls |
| 2025 | |
| 2026 | // Important: release the mutex here so the client can call back into the service from its |
| 2027 | // destructor (can be at the end of the call) |
| 2028 | device = client; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2029 | |
| 2030 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 2031 | mCameraServiceProxyWrapper->logOpen(cameraId, facing, clientPackageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2032 | effectiveApiLevel, isNonSystemNdk, openLatencyMs); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2033 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2034 | { |
| 2035 | Mutex::Autolock lock(mInjectionParametersLock); |
| 2036 | if (cameraId == mInjectionInternalCamId && mInjectionInitPending) { |
| 2037 | mInjectionInitPending = false; |
| 2038 | status_t res = NO_ERROR; |
| 2039 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 2040 | if (clientDescriptor != nullptr) { |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2041 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 2042 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 2043 | if(res != OK) { |
| 2044 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2045 | "No camera device with ID \"%s\" currently available", |
| 2046 | mInjectionExternalCamId.string()); |
| 2047 | } |
| 2048 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2049 | if (res != OK) { |
| 2050 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2051 | } |
| 2052 | } else { |
| 2053 | ALOGE("%s: Internal camera ID = %s 's client does not exist!", |
| 2054 | __FUNCTION__, mInjectionInternalCamId.string()); |
| 2055 | res = NO_INIT; |
| 2056 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2057 | } |
| 2058 | } |
| 2059 | } |
| 2060 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2061 | return ret; |
| 2062 | } |
| 2063 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2064 | status_t CameraService::addOfflineClient(String8 cameraId, sp<BasicClient> offlineClient) { |
| 2065 | if (offlineClient.get() == nullptr) { |
| 2066 | return BAD_VALUE; |
| 2067 | } |
| 2068 | |
| 2069 | { |
| 2070 | // Acquire mServiceLock and prevent other clients from connecting |
| 2071 | std::unique_ptr<AutoConditionLock> lock = |
| 2072 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2073 | |
| 2074 | if (lock == nullptr) { |
| 2075 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." |
| 2076 | , __FUNCTION__, offlineClient->getClientPid()); |
| 2077 | return TIMED_OUT; |
| 2078 | } |
| 2079 | |
| 2080 | auto onlineClientDesc = mActiveClientManager.get(cameraId); |
| 2081 | if (onlineClientDesc.get() == nullptr) { |
| 2082 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, |
| 2083 | cameraId.c_str()); |
| 2084 | return BAD_VALUE; |
| 2085 | } |
| 2086 | |
| 2087 | // Offline clients do not evict or conflict with other online devices. Resource sharing |
| 2088 | // conflicts are handled by the camera provider which will either succeed or fail before |
| 2089 | // reaching this method. |
| 2090 | const auto& onlinePriority = onlineClientDesc->getPriority(); |
| 2091 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( |
| 2092 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, |
| 2093 | /*conflictingKeys*/ std::set<String8>(), onlinePriority.getScore(), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2094 | onlineClientDesc->getOwnerId(), onlinePriority.getState(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2095 | // native clients don't have offline processing support. |
| 2096 | /*ommScoreOffset*/ 0, /*systemNativeClient*/false); |
Guillaume Bailey | 417e43d | 2022-11-02 15:30:24 +0100 | [diff] [blame^] | 2097 | if (offlineClientDesc == nullptr) { |
| 2098 | ALOGE("%s: Offline client descriptor was NULL", __FUNCTION__); |
| 2099 | return BAD_VALUE; |
| 2100 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2101 | |
| 2102 | // Allow only one offline device per camera |
| 2103 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); |
| 2104 | if (!incompatibleClients.empty()) { |
| 2105 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); |
| 2106 | return BAD_VALUE; |
| 2107 | } |
| 2108 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2109 | String8 monitorTags = isClientWatched(offlineClient.get()) ? mMonitorTags : String8(""); |
| 2110 | auto err = offlineClient->initialize(mCameraProviderManager, monitorTags); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2111 | if (err != OK) { |
| 2112 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); |
| 2113 | return err; |
| 2114 | } |
| 2115 | |
| 2116 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); |
| 2117 | if (evicted.size() > 0) { |
| 2118 | for (auto& i : evicted) { |
| 2119 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", |
| 2120 | __FUNCTION__, i->getKey().string()); |
| 2121 | } |
| 2122 | |
| 2123 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " |
| 2124 | "properly", __FUNCTION__); |
| 2125 | |
| 2126 | return BAD_VALUE; |
| 2127 | } |
| 2128 | |
| 2129 | logConnectedOffline(offlineClientDesc->getKey(), |
| 2130 | static_cast<int>(offlineClientDesc->getOwnerId()), |
| 2131 | String8(offlineClient->getPackageName())); |
| 2132 | |
| 2133 | sp<IBinder> remoteCallback = offlineClient->getRemote(); |
| 2134 | if (remoteCallback != nullptr) { |
| 2135 | remoteCallback->linkToDeath(this); |
| 2136 | } |
| 2137 | } // lock is destroyed, allow further connect calls |
| 2138 | |
| 2139 | return OK; |
| 2140 | } |
| 2141 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2142 | Status CameraService::turnOnTorchWithStrengthLevel(const String16& cameraId, int32_t torchStrength, |
| 2143 | const sp<IBinder>& clientBinder) { |
| 2144 | Mutex::Autolock lock(mServiceLock); |
| 2145 | |
| 2146 | ATRACE_CALL(); |
| 2147 | if (clientBinder == nullptr) { |
| 2148 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
| 2149 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2150 | "Torch client binder in null."); |
| 2151 | } |
| 2152 | |
| 2153 | String8 id = String8(cameraId.string()); |
| 2154 | int uid = CameraThreadState::getCallingUid(); |
| 2155 | |
| 2156 | if (shouldRejectSystemCameraConnection(id)) { |
| 2157 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to change the strength level" |
| 2158 | "for system only device %s: ", id.string()); |
| 2159 | } |
| 2160 | |
| 2161 | // verify id is valid |
| 2162 | auto state = getCameraState(id); |
| 2163 | if (state == nullptr) { |
| 2164 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
| 2165 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2166 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
| 2167 | } |
| 2168 | |
| 2169 | StatusInternal cameraStatus = state->getStatus(); |
| 2170 | if (cameraStatus != StatusInternal::NOT_AVAILABLE && |
| 2171 | cameraStatus != StatusInternal::PRESENT) { |
| 2172 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), |
| 2173 | (int)cameraStatus); |
| 2174 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2175 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
| 2176 | } |
| 2177 | |
| 2178 | { |
| 2179 | Mutex::Autolock al(mTorchStatusMutex); |
| 2180 | TorchModeStatus status; |
| 2181 | status_t err = getTorchStatusLocked(id, &status); |
| 2182 | if (err != OK) { |
| 2183 | if (err == NAME_NOT_FOUND) { |
| 2184 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2185 | "Camera \"%s\" does not have a flash unit", id.string()); |
| 2186 | } |
| 2187 | ALOGE("%s: getting current torch status failed for camera %s", |
| 2188 | __FUNCTION__, id.string()); |
| 2189 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2190 | "Error changing torch strength level for camera \"%s\": %s (%d)", |
| 2191 | id.string(), strerror(-err), err); |
| 2192 | } |
| 2193 | |
| 2194 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
| 2195 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
| 2196 | ALOGE("%s: torch mode of camera %s is not available because " |
| 2197 | "camera is in use.", __FUNCTION__, id.string()); |
| 2198 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2199 | "Torch for camera \"%s\" is not available due to an existing camera user", |
| 2200 | id.string()); |
| 2201 | } else { |
| 2202 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 2203 | "insufficient resources", __FUNCTION__, id.string()); |
| 2204 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2205 | "Torch for camera \"%s\" is not available due to insufficient resources", |
| 2206 | id.string()); |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | { |
| 2212 | Mutex::Autolock al(mTorchUidMapMutex); |
| 2213 | updateTorchUidMapLocked(cameraId, uid); |
| 2214 | } |
| 2215 | // Check if the current torch strength level is same as the new one. |
| 2216 | bool shouldSkipTorchStrengthUpdates = mCameraProviderManager->shouldSkipTorchStrengthUpdate( |
| 2217 | id.string(), torchStrength); |
| 2218 | |
| 2219 | status_t err = mFlashlight->turnOnTorchWithStrengthLevel(id, torchStrength); |
| 2220 | |
| 2221 | if (err != OK) { |
| 2222 | int32_t errorCode; |
| 2223 | String8 msg; |
| 2224 | switch (err) { |
| 2225 | case -ENOSYS: |
| 2226 | msg = String8::format("Camera \"%s\" has no flashlight.", |
| 2227 | id.string()); |
| 2228 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2229 | break; |
| 2230 | case -EBUSY: |
| 2231 | msg = String8::format("Camera \"%s\" is in use", |
| 2232 | id.string()); |
| 2233 | errorCode = ERROR_CAMERA_IN_USE; |
| 2234 | break; |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 2235 | case -EINVAL: |
| 2236 | msg = String8::format("Torch strength level %d is not within the " |
| 2237 | "valid range.", torchStrength); |
| 2238 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2239 | break; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2240 | default: |
| 2241 | msg = String8::format("Changing torch strength level failed."); |
| 2242 | errorCode = ERROR_INVALID_OPERATION; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2243 | } |
| 2244 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2245 | return STATUS_ERROR(errorCode, msg.string()); |
| 2246 | } |
| 2247 | |
| 2248 | { |
| 2249 | // update the link to client's death |
| 2250 | // Store the last client that turns on each camera's torch mode. |
| 2251 | Mutex::Autolock al(mTorchClientMapMutex); |
| 2252 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 2253 | if (index == NAME_NOT_FOUND) { |
| 2254 | mTorchClientMap.add(id, clientBinder); |
| 2255 | } else { |
| 2256 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
| 2257 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2258 | } |
| 2259 | clientBinder->linkToDeath(this); |
| 2260 | } |
| 2261 | |
| 2262 | int clientPid = CameraThreadState::getCallingPid(); |
| 2263 | const char *id_cstr = id.c_str(); |
| 2264 | ALOGI("%s: Torch strength for camera id %s changed to %d for client PID %d", |
| 2265 | __FUNCTION__, id_cstr, torchStrength, clientPid); |
| 2266 | if (!shouldSkipTorchStrengthUpdates) { |
| 2267 | broadcastTorchStrengthLevel(id, torchStrength); |
| 2268 | } |
| 2269 | return Status::ok(); |
| 2270 | } |
| 2271 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2272 | Status CameraService::setTorchMode(const String16& cameraId, bool enabled, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2273 | const sp<IBinder>& clientBinder) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2274 | Mutex::Autolock lock(mServiceLock); |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2275 | |
| 2276 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2277 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2278 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2279 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2280 | "Torch client Binder is null"); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2283 | String8 id = String8(cameraId.string()); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2284 | int uid = CameraThreadState::getCallingUid(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2285 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2286 | if (shouldRejectSystemCameraConnection(id)) { |
| 2287 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" |
| 2288 | " for system only device %s: ", id.string()); |
| 2289 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2290 | // verify id is valid. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2291 | auto state = getCameraState(id); |
| 2292 | if (state == nullptr) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 2293 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2294 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2295 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2296 | } |
| 2297 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2298 | StatusInternal cameraStatus = state->getStatus(); |
| 2299 | if (cameraStatus != StatusInternal::PRESENT && |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2300 | cameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 2301 | 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] | 2302 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2303 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | { |
| 2307 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2308 | TorchModeStatus status; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2309 | status_t err = getTorchStatusLocked(id, &status); |
| 2310 | if (err != OK) { |
| 2311 | if (err == NAME_NOT_FOUND) { |
| 2312 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2313 | "Camera \"%s\" does not have a flash unit", id.string()); |
| 2314 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2315 | ALOGE("%s: getting current torch status failed for camera %s", |
| 2316 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2317 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2318 | "Error updating torch status for camera \"%s\": %s (%d)", id.string(), |
| 2319 | strerror(-err), err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2320 | } |
| 2321 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2322 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2323 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2324 | ALOGE("%s: torch mode of camera %s is not available because " |
| 2325 | "camera is in use", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2326 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2327 | "Torch for camera \"%s\" is not available due to an existing camera user", |
| 2328 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2329 | } else { |
| 2330 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 2331 | "insufficient resources", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2332 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2333 | "Torch for camera \"%s\" is not available due to insufficient resources", |
| 2334 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2335 | } |
| 2336 | } |
| 2337 | } |
| 2338 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2339 | { |
| 2340 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 2341 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 2342 | Mutex::Autolock al(mTorchUidMapMutex); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2343 | updateTorchUidMapLocked(cameraId, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2344 | } |
| 2345 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2346 | status_t err = mFlashlight->setTorchMode(id, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2347 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2348 | if (err != OK) { |
| 2349 | int32_t errorCode; |
| 2350 | String8 msg; |
| 2351 | switch (err) { |
| 2352 | case -ENOSYS: |
| 2353 | msg = String8::format("Camera \"%s\" has no flashlight", |
| 2354 | id.string()); |
| 2355 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2356 | break; |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 2357 | case -EBUSY: |
| 2358 | msg = String8::format("Camera \"%s\" is in use", |
| 2359 | id.string()); |
| 2360 | errorCode = ERROR_CAMERA_IN_USE; |
| 2361 | break; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2362 | default: |
| 2363 | msg = String8::format( |
| 2364 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", |
| 2365 | id.string(), enabled, strerror(-err), err); |
| 2366 | errorCode = ERROR_INVALID_OPERATION; |
| 2367 | } |
| 2368 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2369 | logServiceError(msg,errorCode); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2370 | return STATUS_ERROR(errorCode, msg.string()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2371 | } |
| 2372 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2373 | { |
| 2374 | // update the link to client's death |
| 2375 | Mutex::Autolock al(mTorchClientMapMutex); |
| 2376 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 2377 | if (enabled) { |
| 2378 | if (index == NAME_NOT_FOUND) { |
| 2379 | mTorchClientMap.add(id, clientBinder); |
| 2380 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2381 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2382 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2383 | } |
| 2384 | clientBinder->linkToDeath(this); |
| 2385 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2386 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2387 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2388 | } |
| 2389 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 2390 | int clientPid = CameraThreadState::getCallingPid(); |
| 2391 | const char *id_cstr = id.c_str(); |
| 2392 | const char *torchState = enabled ? "on" : "off"; |
| 2393 | ALOGI("Torch for camera id %s turned %s for client PID %d", id_cstr, torchState, clientPid); |
| 2394 | logTorchEvent(id_cstr, torchState , clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2395 | return Status::ok(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2396 | } |
| 2397 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2398 | void CameraService::updateTorchUidMapLocked(const String16& cameraId, int uid) { |
| 2399 | String8 id = String8(cameraId.string()); |
| 2400 | if (mTorchUidMap.find(id) == mTorchUidMap.end()) { |
| 2401 | mTorchUidMap[id].first = uid; |
| 2402 | mTorchUidMap[id].second = uid; |
| 2403 | } else { |
| 2404 | // Set the pending UID |
| 2405 | mTorchUidMap[id].first = uid; |
| 2406 | } |
| 2407 | } |
| 2408 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2409 | Status CameraService::notifySystemEvent(int32_t eventId, |
| 2410 | const std::vector<int32_t>& args) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2411 | const int pid = CameraThreadState::getCallingPid(); |
| 2412 | const int selfPid = getpid(); |
| 2413 | |
| 2414 | // Permission checks |
| 2415 | if (pid != selfPid) { |
| 2416 | // Ensure we're being called by system_server, or similar process with |
| 2417 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2418 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2419 | const int uid = CameraThreadState::getCallingUid(); |
| 2420 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 2421 | " events from pid=%d, uid=%d", pid, uid); |
| 2422 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 2423 | "No permission to send updates to camera service about system events" |
| 2424 | " from pid=%d, uid=%d", pid, uid); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2425 | } |
| 2426 | } |
| 2427 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2428 | ATRACE_CALL(); |
| 2429 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2430 | switch(eventId) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2431 | case ICameraService::EVENT_USER_SWITCHED: { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2432 | // 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] | 2433 | // from a system server crash |
| 2434 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2435 | mSensorPrivacyPolicy->registerSelf(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2436 | doUserSwitch(/*newUserIds*/ args); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2437 | break; |
| 2438 | } |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 2439 | case ICameraService::EVENT_USB_DEVICE_ATTACHED: |
| 2440 | case ICameraService::EVENT_USB_DEVICE_DETACHED: { |
| 2441 | // Notify CameraProviderManager for lazy HALs |
| 2442 | mCameraProviderManager->notifyUsbDeviceEvent(eventId, |
| 2443 | std::to_string(args[0])); |
| 2444 | break; |
| 2445 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2446 | case ICameraService::EVENT_NONE: |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2447 | default: { |
| 2448 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 2449 | eventId); |
| 2450 | break; |
| 2451 | } |
| 2452 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2453 | return Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2454 | } |
| 2455 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2456 | void CameraService::notifyMonitoredUids() { |
| 2457 | Mutex::Autolock lock(mStatusListenerLock); |
| 2458 | |
| 2459 | for (const auto& it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2460 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 2461 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 2462 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2463 | } |
| 2464 | } |
| 2465 | |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2466 | Status CameraService::notifyDeviceStateChange(int64_t newState) { |
| 2467 | const int pid = CameraThreadState::getCallingPid(); |
| 2468 | const int selfPid = getpid(); |
| 2469 | |
| 2470 | // Permission checks |
| 2471 | if (pid != selfPid) { |
| 2472 | // Ensure we're being called by system_server, or similar process with |
| 2473 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2474 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2475 | const int uid = CameraThreadState::getCallingUid(); |
| 2476 | ALOGE("Permission Denial: cannot send updates to camera service about device" |
| 2477 | " state changes from pid=%d, uid=%d", pid, uid); |
| 2478 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2479 | "No permission to send updates to camera service about device state" |
| 2480 | " changes from pid=%d, uid=%d", pid, uid); |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | ATRACE_CALL(); |
| 2485 | |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 2486 | mCameraProviderManager->notifyDeviceStateChange(newState); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2487 | |
| 2488 | return Status::ok(); |
| 2489 | } |
| 2490 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2491 | Status CameraService::notifyDisplayConfigurationChange() { |
| 2492 | ATRACE_CALL(); |
| 2493 | const int callingPid = CameraThreadState::getCallingPid(); |
| 2494 | const int selfPid = getpid(); |
| 2495 | |
| 2496 | // Permission checks |
| 2497 | if (callingPid != selfPid) { |
| 2498 | // Ensure we're being called by system_server, or similar process with |
| 2499 | // permissions to notify the camera service about system events |
| 2500 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
| 2501 | const int uid = CameraThreadState::getCallingUid(); |
| 2502 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" |
| 2503 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2504 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2505 | "No permission to send updates to camera service about orientation" |
| 2506 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2507 | } |
| 2508 | } |
| 2509 | |
| 2510 | Mutex::Autolock lock(mServiceLock); |
| 2511 | |
| 2512 | // Don't do anything if rotate-and-crop override via cmd is active |
| 2513 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); |
| 2514 | |
| 2515 | const auto clients = mActiveClientManager.getAll(); |
| 2516 | for (auto& current : clients) { |
| 2517 | if (current != nullptr) { |
| 2518 | const auto basicClient = current->getValue(); |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame] | 2519 | if (basicClient.get() != nullptr) { |
Emilian Peev | 065b2c1 | 2021-11-23 13:12:57 -0800 | [diff] [blame] | 2520 | basicClient->setRotateAndCropOverride( |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 2521 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
Emilian Peev | 065b2c1 | 2021-11-23 13:12:57 -0800 | [diff] [blame] | 2522 | basicClient->getPackageName(), |
| 2523 | basicClient->getCameraFacing(), |
| 2524 | multiuser_get_user_id(basicClient->getClientUid()))); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2525 | } |
| 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | return Status::ok(); |
| 2530 | } |
| 2531 | |
| 2532 | Status CameraService::getConcurrentCameraIds( |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2533 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { |
| 2534 | ATRACE_CALL(); |
| 2535 | if (!concurrentCameraIds) { |
| 2536 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); |
| 2537 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); |
| 2538 | } |
| 2539 | |
| 2540 | if (!mInitialized) { |
| 2541 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2542 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2543 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2544 | "Camera subsystem is not available"); |
| 2545 | } |
| 2546 | // First call into the provider and get the set of concurrent camera |
| 2547 | // combinations |
| 2548 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = |
Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 2549 | mCameraProviderManager->getConcurrentCameraIds(); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2550 | for (auto &combination : concurrentCameraCombinations) { |
| 2551 | std::vector<std::string> validCombination; |
| 2552 | for (auto &cameraId : combination) { |
| 2553 | // if the camera state is not present, skip |
| 2554 | String8 cameraIdStr(cameraId.c_str()); |
| 2555 | auto state = getCameraState(cameraIdStr); |
| 2556 | if (state == nullptr) { |
| 2557 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); |
| 2558 | continue; |
| 2559 | } |
| 2560 | StatusInternal status = state->getStatus(); |
| 2561 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { |
| 2562 | continue; |
| 2563 | } |
| 2564 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { |
| 2565 | continue; |
| 2566 | } |
| 2567 | validCombination.push_back(cameraId); |
| 2568 | } |
| 2569 | if (validCombination.size() != 0) { |
| 2570 | concurrentCameraIds->push_back(std::move(validCombination)); |
| 2571 | } |
| 2572 | } |
| 2573 | return Status::ok(); |
| 2574 | } |
| 2575 | |
| 2576 | Status CameraService::isConcurrentSessionConfigurationSupported( |
| 2577 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2578 | int targetSdkVersion, /*out*/bool* isSupported) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2579 | if (!isSupported) { |
| 2580 | ALOGE("%s: isSupported is NULL", __FUNCTION__); |
| 2581 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); |
| 2582 | } |
| 2583 | |
| 2584 | if (!mInitialized) { |
| 2585 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 2586 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2587 | "Camera subsystem is not available"); |
| 2588 | } |
| 2589 | |
| 2590 | // Check for camera permissions |
| 2591 | int callingPid = CameraThreadState::getCallingPid(); |
| 2592 | int callingUid = CameraThreadState::getCallingUid(); |
| 2593 | if ((callingPid != getpid()) && !checkPermission(sCameraPermission, callingPid, callingUid)) { |
| 2594 | ALOGE("%s: pid %d doesn't have camera permissions", __FUNCTION__, callingPid); |
| 2595 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 2596 | "android.permission.CAMERA needed to call" |
| 2597 | "isConcurrentSessionConfigurationSupported"); |
| 2598 | } |
| 2599 | |
| 2600 | status_t res = |
| 2601 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2602 | cameraIdsAndSessionConfigurations, mPerfClassPrimaryCameraIds, |
| 2603 | targetSdkVersion, isSupported); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2604 | if (res != OK) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2605 | logServiceError(String8::format("Unable to query session configuration support"), |
| 2606 | ERROR_INVALID_OPERATION); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2607 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " |
| 2608 | "support %s (%d)", strerror(-res), res); |
| 2609 | } |
| 2610 | return Status::ok(); |
| 2611 | } |
| 2612 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2613 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, |
| 2614 | /*out*/ |
| 2615 | std::vector<hardware::CameraStatus> *cameraStatuses) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2616 | return addListenerHelper(listener, cameraStatuses); |
| 2617 | } |
| 2618 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2619 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, |
| 2620 | std::vector<hardware::CameraStatus>* cameraStatuses) { |
| 2621 | return addListenerHelper(listener, cameraStatuses, false, true); |
| 2622 | } |
| 2623 | |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2624 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, |
| 2625 | /*out*/ |
| 2626 | std::vector<hardware::CameraStatus> *cameraStatuses, |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2627 | bool isVendorListener, bool isProcessLocalTest) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2628 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2629 | ATRACE_CALL(); |
| 2630 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2631 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2632 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 2633 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2634 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2635 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2636 | } |
| 2637 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2638 | auto clientUid = CameraThreadState::getCallingUid(); |
| 2639 | auto clientPid = CameraThreadState::getCallingPid(); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2640 | bool openCloseCallbackAllowed = checkPermission(sCameraOpenCloseListenerPermission, |
Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 2641 | clientPid, clientUid, /*logPermissionFailure*/false); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2642 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2643 | Mutex::Autolock lock(mServiceLock); |
| 2644 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2645 | { |
| 2646 | Mutex::Autolock lock(mStatusListenerLock); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2647 | for (const auto &it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2648 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2649 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 2650 | __FUNCTION__, listener.get()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2651 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2652 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2653 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2654 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2655 | sp<ServiceListener> serviceListener = |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2656 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, |
| 2657 | openCloseCallbackAllowed); |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2658 | auto ret = serviceListener->initialize(isProcessLocalTest); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2659 | if (ret != NO_ERROR) { |
| 2660 | String8 msg = String8::format("Failed to initialize service listener: %s (%d)", |
| 2661 | strerror(-ret), ret); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2662 | logServiceError(msg,ERROR_ILLEGAL_ARGUMENT); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2663 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2664 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 2665 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2666 | // The listener still needs to be added to the list of listeners, regardless of what |
| 2667 | // permissions the listener process has / whether it is a vendor listener. Since it might be |
| 2668 | // eligible to listen to other camera ids. |
| 2669 | mListenerList.emplace_back(serviceListener); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2670 | mUidPolicy->registerMonitorUid(clientUid); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2671 | } |
| 2672 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2673 | /* Collect current devices and status */ |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2674 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2675 | Mutex::Autolock lock(mCameraStatesLock); |
| 2676 | for (auto& i : mCameraStates) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 2677 | cameraStatuses->emplace_back(i.first, |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 2678 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds(), |
| 2679 | openCloseCallbackAllowed ? i.second->getClientPackage() : String8::empty()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2680 | } |
| 2681 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2682 | // Remove the camera statuses that should be hidden from the client, we do |
| 2683 | // this after collecting the states in order to avoid holding |
| 2684 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at |
| 2685 | // the same time. |
| 2686 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), |
| 2687 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { |
| 2688 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 2689 | if (getSystemCameraKind(s.cameraId, &deviceKind) != OK) { |
| 2690 | ALOGE("%s: Invalid camera id %s, skipping status update", |
| 2691 | __FUNCTION__, s.cameraId.c_str()); |
| 2692 | return true; |
| 2693 | } |
| 2694 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, |
| 2695 | clientUid);}), cameraStatuses->end()); |
| 2696 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2697 | //cameraStatuses will have non-eligible camera ids removed. |
| 2698 | std::set<String16> idsChosenForCallback; |
| 2699 | for (const auto &s : *cameraStatuses) { |
| 2700 | idsChosenForCallback.insert(String16(s.cameraId)); |
| 2701 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2702 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2703 | /* |
| 2704 | * Immediately signal current torch status to this listener only |
| 2705 | * This may be a subset of all the devices, so don't include it in the response directly |
| 2706 | */ |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2707 | { |
| 2708 | Mutex::Autolock al(mTorchStatusMutex); |
| 2709 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2710 | String16 id = String16(mTorchStatusMap.keyAt(i).string()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2711 | // The camera id is visible to the client. Fine to send torch |
| 2712 | // callback. |
| 2713 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { |
| 2714 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id); |
| 2715 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2716 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2717 | } |
| 2718 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2719 | return Status::ok(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2720 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2721 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2722 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2723 | ATRACE_CALL(); |
| 2724 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2725 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 2726 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2727 | if (listener == 0) { |
| 2728 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2729 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2730 | } |
| 2731 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2732 | Mutex::Autolock lock(mServiceLock); |
| 2733 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2734 | { |
| 2735 | Mutex::Autolock lock(mStatusListenerLock); |
| 2736 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2737 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { |
| 2738 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid()); |
| 2739 | IInterface::asBinder(listener)->unlinkToDeath(*it); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2740 | mListenerList.erase(it); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2741 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2742 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 2747 | __FUNCTION__, listener.get()); |
| 2748 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2749 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2750 | } |
| 2751 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2752 | Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2753 | |
| 2754 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2755 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 2756 | |
| 2757 | if (parameters == NULL) { |
| 2758 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2759 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2760 | } |
| 2761 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2762 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2763 | |
| 2764 | CameraParameters shimParams; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2765 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2766 | // Error logged by caller |
| 2767 | return ret; |
| 2768 | } |
| 2769 | |
| 2770 | String8 shimParamsString8 = shimParams.flatten(); |
| 2771 | String16 shimParamsString16 = String16(shimParamsString8); |
| 2772 | |
| 2773 | *parameters = shimParamsString16; |
| 2774 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2775 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2778 | Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion, |
| 2779 | /*out*/ bool *isSupported) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2780 | ATRACE_CALL(); |
| 2781 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2782 | const String8 id = String8(cameraId); |
| 2783 | |
| 2784 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2785 | |
| 2786 | switch (apiVersion) { |
| 2787 | case API_VERSION_1: |
| 2788 | case API_VERSION_2: |
| 2789 | break; |
| 2790 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2791 | String8 msg = String8::format("Unknown API version %d", apiVersion); |
| 2792 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2793 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2794 | } |
| 2795 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2796 | auto deviceVersionAndTransport = getDeviceVersion(id); |
| 2797 | if (deviceVersionAndTransport.first == -1) { |
| 2798 | String8 msg = String8::format("Unknown camera ID %s", id.string()); |
| 2799 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2800 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 2801 | } |
| 2802 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 2803 | int deviceVersion = deviceVersionAndTransport.first; |
| 2804 | switch (deviceVersion) { |
| 2805 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 2806 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 2807 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 2808 | if (apiVersion == API_VERSION_2) { |
| 2809 | ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without " |
| 2810 | "shim", __FUNCTION__, id.string(), deviceVersion); |
| 2811 | *isSupported = false; |
| 2812 | } else { // if (apiVersion == API_VERSION_1) { |
| 2813 | ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always " |
| 2814 | "supported", __FUNCTION__, id.string()); |
| 2815 | *isSupported = true; |
| 2816 | } |
| 2817 | break; |
| 2818 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 2819 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 2820 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 2821 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 2822 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 2823 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 2824 | 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] | 2825 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2826 | *isSupported = true; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2827 | break; |
| 2828 | default: { |
| 2829 | String8 msg = String8::format("Unknown device version %x for device %s", |
| 2830 | deviceVersion, id.string()); |
| 2831 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2832 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2833 | } |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2834 | } |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2835 | } else { |
| 2836 | *isSupported = true; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2837 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2838 | return Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2839 | } |
| 2840 | |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2841 | Status CameraService::isHiddenPhysicalCamera(const String16& cameraId, |
| 2842 | /*out*/ bool *isSupported) { |
| 2843 | ATRACE_CALL(); |
| 2844 | |
| 2845 | const String8 id = String8(cameraId); |
| 2846 | |
| 2847 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
| 2848 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(id.string()); |
| 2849 | |
| 2850 | return Status::ok(); |
| 2851 | } |
| 2852 | |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2853 | Status CameraService::injectCamera( |
| 2854 | const String16& packageName, const String16& internalCamId, |
| 2855 | const String16& externalCamId, |
| 2856 | const sp<ICameraInjectionCallback>& callback, |
| 2857 | /*out*/ |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2858 | sp<ICameraInjectionSession>* cameraInjectionSession) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2859 | ATRACE_CALL(); |
| 2860 | |
| 2861 | if (!checkCallingPermission(sCameraInjectExternalCameraPermission)) { |
| 2862 | const int pid = CameraThreadState::getCallingPid(); |
| 2863 | const int uid = CameraThreadState::getCallingUid(); |
| 2864 | ALOGE("Permission Denial: can't inject camera pid=%d, uid=%d", pid, uid); |
| 2865 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 2866 | "Permission Denial: no permission to inject camera"); |
| 2867 | } |
| 2868 | |
| 2869 | ALOGV( |
| 2870 | "%s: Package name = %s, Internal camera ID = %s, External camera ID = " |
| 2871 | "%s", |
| 2872 | __FUNCTION__, String8(packageName).string(), |
| 2873 | String8(internalCamId).string(), String8(externalCamId).string()); |
| 2874 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2875 | { |
| 2876 | Mutex::Autolock lock(mInjectionParametersLock); |
| 2877 | mInjectionInternalCamId = String8(internalCamId); |
| 2878 | mInjectionExternalCamId = String8(externalCamId); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2879 | mInjectionStatusListener->addListener(callback); |
| 2880 | *cameraInjectionSession = new CameraInjectionSession(this); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2881 | status_t res = NO_ERROR; |
| 2882 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 2883 | // If the client already exists, we can directly connect to the camera device through the |
| 2884 | // client's injectCamera(), otherwise we need to wait until the client is established |
| 2885 | // (execute connectHelper()) before injecting the camera to the camera device. |
| 2886 | if (clientDescriptor != nullptr) { |
| 2887 | mInjectionInitPending = false; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2888 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 2889 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 2890 | if(res != OK) { |
| 2891 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2892 | "No camera device with ID \"%s\" currently available", |
| 2893 | mInjectionExternalCamId.string()); |
| 2894 | } |
| 2895 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2896 | if(res != OK) { |
| 2897 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2898 | } |
| 2899 | } else { |
| 2900 | mInjectionInitPending = true; |
| 2901 | } |
| 2902 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2903 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2904 | return binder::Status::ok(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2905 | } |
| 2906 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2907 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2908 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2909 | for (auto& i : mActiveClientManager.getAll()) { |
| 2910 | auto clientSp = i->getValue(); |
| 2911 | if (clientSp.get() == client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2912 | cacheClientTagDumpIfNeeded(client->mCameraIdStr, clientSp.get()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2913 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2914 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2915 | } |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2916 | updateAudioRestrictionLocked(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2917 | } |
| 2918 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2919 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2920 | bool ret = false; |
| 2921 | { |
| 2922 | // Acquire mServiceLock and prevent other clients from connecting |
| 2923 | std::unique_ptr<AutoConditionLock> lock = |
| 2924 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2925 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2926 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2927 | std::vector<sp<BasicClient>> evicted; |
| 2928 | for (auto& i : mActiveClientManager.getAll()) { |
| 2929 | auto clientSp = i->getValue(); |
| 2930 | if (clientSp.get() == nullptr) { |
| 2931 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 2932 | mActiveClientManager.remove(i); |
| 2933 | continue; |
| 2934 | } |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 2935 | if (remote == clientSp->getRemote()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2936 | mActiveClientManager.remove(i); |
| 2937 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2938 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2939 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2940 | clientSp->notifyError( |
| 2941 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2942 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2943 | } |
| 2944 | } |
| 2945 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2946 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 2947 | // other clients from connecting in mServiceLockWrapper if held |
| 2948 | mServiceLock.unlock(); |
| 2949 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2950 | // Do not clear caller identity, remote caller should be client proccess |
| 2951 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2952 | for (auto& i : evicted) { |
| 2953 | if (i.get() != nullptr) { |
| 2954 | i->disconnect(); |
| 2955 | ret = true; |
| 2956 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2957 | } |
| 2958 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2959 | // Reacquire mServiceLock |
| 2960 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2961 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2962 | } // lock is destroyed, allow further connect calls |
| 2963 | |
| 2964 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2965 | } |
| 2966 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2967 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
| 2968 | const String8& cameraId) const { |
| 2969 | std::shared_ptr<CameraState> state; |
| 2970 | { |
| 2971 | Mutex::Autolock lock(mCameraStatesLock); |
| 2972 | auto iter = mCameraStates.find(cameraId); |
| 2973 | if (iter != mCameraStates.end()) { |
| 2974 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2975 | } |
| 2976 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2977 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2978 | } |
| 2979 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2980 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) { |
| 2981 | // Remove from active clients list |
| 2982 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 2983 | if (clientDescriptorPtr == nullptr) { |
| 2984 | ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__, |
| 2985 | cameraId.string()); |
| 2986 | return sp<BasicClient>{nullptr}; |
| 2987 | } |
| 2988 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2989 | sp<BasicClient> client = clientDescriptorPtr->getValue(); |
| 2990 | if (client.get() != nullptr) { |
| 2991 | cacheClientTagDumpIfNeeded(clientDescriptorPtr->getKey(), client.get()); |
| 2992 | } |
| 2993 | return client; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 2994 | } |
| 2995 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2996 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2997 | // Acquire mServiceLock and prevent other clients from connecting |
| 2998 | std::unique_ptr<AutoConditionLock> lock = |
| 2999 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 3000 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3001 | std::set<userid_t> newAllowedUsers; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3002 | for (size_t i = 0; i < newUserIds.size(); i++) { |
| 3003 | if (newUserIds[i] < 0) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3004 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3005 | __FUNCTION__, newUserIds[i]); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3006 | return; |
| 3007 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3008 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3011 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3012 | if (newAllowedUsers == mAllowedUsers) { |
| 3013 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 3014 | return; |
| 3015 | } |
| 3016 | |
| 3017 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 3018 | |
| 3019 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3020 | |
| 3021 | // Current user has switched, evict all current clients. |
| 3022 | std::vector<sp<BasicClient>> evicted; |
| 3023 | for (auto& i : mActiveClientManager.getAll()) { |
| 3024 | auto clientSp = i->getValue(); |
| 3025 | |
| 3026 | if (clientSp.get() == nullptr) { |
| 3027 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3028 | continue; |
| 3029 | } |
| 3030 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3031 | // Don't evict clients that are still allowed. |
| 3032 | uid_t clientUid = clientSp->getClientUid(); |
| 3033 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 3034 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 3035 | continue; |
| 3036 | } |
| 3037 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3038 | evicted.push_back(clientSp); |
| 3039 | |
| 3040 | String8 curTime = getFormattedCurrentTime(); |
| 3041 | |
| 3042 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
| 3043 | i->getKey().string()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3044 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3045 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3046 | logEvent(String8::format("EVICT device %s client held by package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3047 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due" |
| 3048 | " to user switch.", i->getKey().string(), |
| 3049 | String8{clientSp->getPackageName()}.string(), |
| 3050 | i->getOwnerId(), i->getPriority().getScore(), |
| 3051 | i->getPriority().getState())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3052 | |
| 3053 | } |
| 3054 | |
| 3055 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 3056 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 3057 | mServiceLock.unlock(); |
| 3058 | |
| 3059 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3060 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3061 | |
| 3062 | for (auto& i : evicted) { |
| 3063 | i->disconnect(); |
| 3064 | } |
| 3065 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3066 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3067 | |
| 3068 | // Reacquire mServiceLock |
| 3069 | mServiceLock.lock(); |
| 3070 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3071 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3072 | void CameraService::logEvent(const char* event) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3073 | String8 curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3074 | Mutex::Autolock l(mLogLock); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3075 | String8 msg = String8::format("%s : %s", curTime.string(), event); |
| 3076 | // For service error events, print the msg only once. |
| 3077 | if(!msg.contains("SERVICE ERROR")) { |
| 3078 | mEventLog.add(msg); |
| 3079 | } else if(sServiceErrorEventSet.find(msg) == sServiceErrorEventSet.end()) { |
| 3080 | // Error event not added to the dumpsys log before |
| 3081 | mEventLog.add(msg); |
| 3082 | sServiceErrorEventSet.insert(msg); |
| 3083 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3084 | } |
| 3085 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3086 | void CameraService::logDisconnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3087 | const char* clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3088 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3089 | 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] | 3090 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3091 | } |
| 3092 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3093 | void CameraService::logDisconnectedOffline(const char* cameraId, int clientPid, |
| 3094 | const char* clientPackage) { |
| 3095 | // Log the clients evicted |
| 3096 | logEvent(String8::format("DISCONNECT offline device %s client for package %s (PID %d)", |
| 3097 | cameraId, clientPackage, clientPid)); |
| 3098 | } |
| 3099 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3100 | void CameraService::logConnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3101 | const char* clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3102 | // Log the clients evicted |
| 3103 | 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] | 3104 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3105 | } |
| 3106 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3107 | void CameraService::logConnectedOffline(const char* cameraId, int clientPid, |
| 3108 | const char* clientPackage) { |
| 3109 | // Log the clients evicted |
| 3110 | logEvent(String8::format("CONNECT offline device %s client for package %s (PID %d)", cameraId, |
| 3111 | clientPackage, clientPid)); |
| 3112 | } |
| 3113 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3114 | void CameraService::logRejected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3115 | const char* clientPackage, const char* reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3116 | // Log the client rejected |
| 3117 | 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] | 3118 | cameraId, clientPackage, clientPid, reason)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3119 | } |
| 3120 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3121 | void CameraService::logTorchEvent(const char* cameraId, const char *torchState, int clientPid) { |
| 3122 | // Log torch event |
| 3123 | logEvent(String8::format("Torch for camera id %s turned %s for client PID %d", cameraId, |
| 3124 | torchState, clientPid)); |
| 3125 | } |
| 3126 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3127 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 3128 | const std::set<userid_t>& newUserIds) { |
| 3129 | String8 newUsers = toString(newUserIds); |
| 3130 | String8 oldUsers = toString(oldUserIds); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3131 | if (oldUsers.size() == 0) { |
| 3132 | oldUsers = "<None>"; |
| 3133 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3134 | // Log the new and old users |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3135 | 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] | 3136 | oldUsers.string(), newUsers.string())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) { |
| 3140 | // Log the device removal |
| 3141 | logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason)); |
| 3142 | } |
| 3143 | |
| 3144 | void CameraService::logDeviceAdded(const char* cameraId, const char* reason) { |
| 3145 | // Log the device removal |
| 3146 | logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason)); |
| 3147 | } |
| 3148 | |
| 3149 | void CameraService::logClientDied(int clientPid, const char* reason) { |
| 3150 | // Log the device removal |
| 3151 | 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] | 3152 | } |
| 3153 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3154 | void CameraService::logServiceError(const char* msg, int errorCode) { |
| 3155 | String8 curTime = getFormattedCurrentTime(); |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 3156 | 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] | 3157 | } |
| 3158 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3159 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 3160 | uint32_t flags) { |
| 3161 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3162 | // Permission checks |
| 3163 | switch (code) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3164 | case SHELL_COMMAND_TRANSACTION: { |
| 3165 | int in = data.readFileDescriptor(); |
| 3166 | int out = data.readFileDescriptor(); |
| 3167 | int err = data.readFileDescriptor(); |
| 3168 | int argc = data.readInt32(); |
| 3169 | Vector<String16> args; |
| 3170 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 3171 | args.add(data.readString16()); |
| 3172 | } |
| 3173 | sp<IBinder> unusedCallback; |
| 3174 | sp<IResultReceiver> resultReceiver; |
| 3175 | status_t status; |
| 3176 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 3177 | return status; |
| 3178 | } |
| 3179 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 3180 | return status; |
| 3181 | } |
| 3182 | status = shellCommand(in, out, err, args); |
| 3183 | if (resultReceiver != nullptr) { |
| 3184 | resultReceiver->send(status); |
| 3185 | } |
| 3186 | return NO_ERROR; |
| 3187 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | return BnCameraService::onTransact(code, data, reply, flags); |
| 3191 | } |
| 3192 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3193 | // We share the media players for shutter and recording sound for all clients. |
| 3194 | // A reference count is kept to determine when we will actually release the |
| 3195 | // media players. |
| 3196 | |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3197 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { |
| 3198 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 3199 | status_t error; |
| 3200 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 3201 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3202 | error = mp->prepare(); |
| 3203 | } |
| 3204 | if (error != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3205 | ALOGE("Failed to load CameraService sounds: %s", file); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3206 | mp->disconnect(); |
| 3207 | mp.clear(); |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3208 | return nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3209 | } |
| 3210 | return mp; |
| 3211 | } |
| 3212 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3213 | void CameraService::increaseSoundRef() { |
| 3214 | Mutex::Autolock lock(mSoundLock); |
| 3215 | mSoundRef++; |
| 3216 | } |
| 3217 | |
| 3218 | void CameraService::loadSoundLocked(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3219 | ATRACE_CALL(); |
| 3220 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3221 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); |
| 3222 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { |
| 3223 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); |
| 3224 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { |
| 3225 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 3226 | } |
| 3227 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) { |
| 3228 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); |
| 3229 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { |
| 3230 | mSoundPlayer[SOUND_RECORDING_START] = |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3231 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3232 | } |
| 3233 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { |
| 3234 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); |
| 3235 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { |
| 3236 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
| 3237 | } |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3238 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3239 | } |
| 3240 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3241 | void CameraService::decreaseSoundRef() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3242 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3243 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3244 | if (--mSoundRef) return; |
| 3245 | |
| 3246 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 3247 | if (mSoundPlayer[i] != 0) { |
| 3248 | mSoundPlayer[i]->disconnect(); |
| 3249 | mSoundPlayer[i].clear(); |
| 3250 | } |
| 3251 | } |
| 3252 | } |
| 3253 | |
| 3254 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3255 | ATRACE_CALL(); |
| 3256 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3257 | LOG1("playSound(%d)", kind); |
Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame] | 3258 | if (kind < 0 || kind >= NUM_SOUNDS) { |
| 3259 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); |
| 3260 | return; |
| 3261 | } |
| 3262 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3263 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3264 | loadSoundLocked(kind); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3265 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 3266 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 3267 | player->seekTo(0); |
| 3268 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | // ---------------------------------------------------------------------------- |
| 3273 | |
| 3274 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 3275 | const sp<ICameraClient>& cameraClient, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3276 | const String16& clientPackageName, bool systemNativeClient, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 3277 | const std::optional<String16>& clientFeatureId, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3278 | const String8& cameraIdStr, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3279 | int api1CameraId, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3280 | int clientPid, uid_t clientUid, |
| 3281 | int servicePid) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3282 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 3283 | IInterface::asBinder(cameraClient), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3284 | clientPackageName, systemNativeClient, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3285 | cameraIdStr, cameraFacing, sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3286 | clientPid, clientUid, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3287 | servicePid), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3288 | mCameraId(api1CameraId) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3289 | { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3290 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3291 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3292 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 3293 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3294 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3295 | cameraService->increaseSoundRef(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3296 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3297 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3298 | } |
| 3299 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3300 | // tear down the client |
| 3301 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3302 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3303 | mDestructionStarted = true; |
| 3304 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3305 | sCameraService->decreaseSoundRef(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3306 | // unconditionally disconnect. function is idempotent |
| 3307 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3308 | } |
| 3309 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3310 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; |
| 3311 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3312 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3313 | const sp<IBinder>& remoteCallback, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3314 | const String16& clientPackageName, bool nativeClient, |
| 3315 | const std::optional<String16>& clientFeatureId, const String8& cameraIdStr, |
| 3316 | int cameraFacing, int sensorOrientation, int clientPid, uid_t clientUid, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3317 | int servicePid): |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3318 | mDestructionStarted(false), |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3319 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), mOrientation(sensorOrientation), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3320 | mClientPackageName(clientPackageName), mSystemNativeClient(nativeClient), |
| 3321 | mClientFeatureId(clientFeatureId), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 3322 | mClientPid(clientPid), mClientUid(clientUid), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3323 | mServicePid(servicePid), |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3324 | mDisconnected(false), mUidIsTrusted(false), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3325 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3326 | mRemoteBinder(remoteCallback), |
| 3327 | mOpsActive(false), |
| 3328 | mOpsStreaming(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3329 | { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3330 | if (sCameraService == nullptr) { |
| 3331 | sCameraService = cameraService; |
| 3332 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 3333 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3334 | // There are 2 scenarios in which a client won't have AppOps operations |
| 3335 | // (both scenarios : native clients) |
| 3336 | // 1) It's an system native client*, the package name will be empty |
| 3337 | // and it will return from this function in the previous if condition |
| 3338 | // (This is the same as the previously existing behavior). |
| 3339 | // 2) It is a system native client, but its package name has been |
| 3340 | // modified for debugging, however it still must not use AppOps since |
| 3341 | // the package name is not a real one. |
| 3342 | // |
| 3343 | // * system native client - native client with UID < AID_APP_START. It |
| 3344 | // doesn't exclude clients not on the system partition. |
| 3345 | if (!mSystemNativeClient) { |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3346 | mAppOpsManager = std::make_unique<AppOpsManager>(); |
| 3347 | } |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3348 | |
| 3349 | mUidIsTrusted = isTrustedCallingUid(mClientUid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3353 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3354 | mDestructionStarted = true; |
| 3355 | } |
| 3356 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3357 | binder::Status CameraService::BasicClient::disconnect() { |
| 3358 | binder::Status res = Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3359 | if (mDisconnected) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3360 | return res; |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3361 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 3362 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3363 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3364 | sCameraService->removeByClient(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3365 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, String8(mClientPackageName)); |
Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 3366 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, |
| 3367 | mCameraIdStr.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3368 | |
| 3369 | sp<IBinder> remote = getRemote(); |
| 3370 | if (remote != nullptr) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3371 | remote->unlinkToDeath(sCameraService); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3372 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3373 | |
| 3374 | finishCameraOps(); |
Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 3375 | // Notify flashlight that a camera device is closed. |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3376 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); |
| 3377 | ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(), |
| 3378 | mClientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3379 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3380 | // client shouldn't be able to call into us anymore |
| 3381 | mClientPid = 0; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3382 | |
| 3383 | return res; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3384 | } |
| 3385 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3386 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 3387 | // No dumping of clients directly over Binder, |
| 3388 | // must go through CameraService::dump |
| 3389 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3390 | CameraThreadState::getCallingUid(), NULL, 0); |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3391 | return OK; |
| 3392 | } |
| 3393 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 3394 | status_t CameraService::BasicClient::startWatchingTags(const String8&, int) { |
| 3395 | // Can't watch tags directly, must go through CameraService::startWatchingTags |
| 3396 | return OK; |
| 3397 | } |
| 3398 | |
| 3399 | status_t CameraService::BasicClient::stopWatchingTags(int) { |
| 3400 | // Can't watch tags directly, must go through CameraService::stopWatchingTags |
| 3401 | return OK; |
| 3402 | } |
| 3403 | |
| 3404 | status_t CameraService::BasicClient::dumpWatchedEventsToVector(std::vector<std::string> &) { |
| 3405 | // Can't watch tags directly, must go through CameraService::dumpWatchedEventsToVector |
| 3406 | return OK; |
| 3407 | } |
| 3408 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3409 | String16 CameraService::BasicClient::getPackageName() const { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3410 | return mClientPackageName; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3411 | } |
| 3412 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3413 | int CameraService::BasicClient::getCameraFacing() const { |
| 3414 | return mCameraFacing; |
| 3415 | } |
| 3416 | |
| 3417 | int CameraService::BasicClient::getCameraOrientation() const { |
| 3418 | return mOrientation; |
| 3419 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3420 | |
| 3421 | int CameraService::BasicClient::getClientPid() const { |
| 3422 | return mClientPid; |
| 3423 | } |
| 3424 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3425 | uid_t CameraService::BasicClient::getClientUid() const { |
| 3426 | return mClientUid; |
| 3427 | } |
| 3428 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3429 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 3430 | // Defaults to API2. |
| 3431 | return level == API_2; |
| 3432 | } |
| 3433 | |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3434 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3435 | { |
| 3436 | Mutex::Autolock l(mAudioRestrictionLock); |
| 3437 | mAudioRestriction = mode; |
| 3438 | } |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3439 | sCameraService->updateAudioRestriction(); |
| 3440 | return OK; |
| 3441 | } |
| 3442 | |
| 3443 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3444 | return sCameraService->updateAudioRestriction(); |
| 3445 | } |
| 3446 | |
| 3447 | int32_t CameraService::BasicClient::getAudioRestriction() const { |
| 3448 | Mutex::Autolock l(mAudioRestrictionLock); |
| 3449 | return mAudioRestriction; |
| 3450 | } |
| 3451 | |
| 3452 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { |
| 3453 | switch (mode) { |
| 3454 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: |
| 3455 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: |
| 3456 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: |
| 3457 | return true; |
| 3458 | default: |
| 3459 | return false; |
| 3460 | } |
| 3461 | } |
| 3462 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3463 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { |
| 3464 | if (mode == AppOpsManager::MODE_ERRORED) { |
| 3465 | ALOGI("Camera %s: Access for \"%s\" has been revoked", |
| 3466 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 3467 | return PERMISSION_DENIED; |
| 3468 | } else if (!mUidIsTrusted && mode == AppOpsManager::MODE_IGNORED) { |
| 3469 | // If the calling Uid is trusted (a native service), the AppOpsManager could |
| 3470 | // return MODE_IGNORED. Do not treat such case as error. |
| 3471 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, |
| 3472 | mClientPackageName); |
| 3473 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3474 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3475 | if (!isUidActive || !isCameraPrivacyEnabled) { |
| 3476 | ALOGI("Camera %s: Access for \"%s\" has been restricted", |
| 3477 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 3478 | // Return the same error as for device policy manager rejection |
| 3479 | return -EACCES; |
| 3480 | } |
| 3481 | } |
| 3482 | return OK; |
| 3483 | } |
| 3484 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3485 | status_t CameraService::BasicClient::startCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3486 | ATRACE_CALL(); |
| 3487 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3488 | { |
| 3489 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3490 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3491 | } |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3492 | if (mAppOpsManager != nullptr) { |
| 3493 | // Notify app ops that the camera is not available |
| 3494 | mOpsCallback = new OpsCallback(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3495 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 3496 | mClientPackageName, mOpsCallback); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3497 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3498 | // Just check for camera acccess here on open - delay startOp until |
| 3499 | // camera frames start streaming in startCameraStreamingOps |
| 3500 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3501 | mClientPackageName); |
| 3502 | status_t res = handleAppOpMode(mode); |
| 3503 | if (res != OK) { |
| 3504 | return res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3505 | } |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 3506 | } |
| 3507 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3508 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3509 | |
| 3510 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3511 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3512 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3513 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid); |
| 3514 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3515 | // Notify listeners of camera open/close status |
| 3516 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); |
| 3517 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3518 | return OK; |
| 3519 | } |
| 3520 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3521 | status_t CameraService::BasicClient::startCameraStreamingOps() { |
| 3522 | ATRACE_CALL(); |
| 3523 | |
| 3524 | if (!mOpsActive) { |
| 3525 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3526 | return INVALID_OPERATION; |
| 3527 | } |
| 3528 | if (mOpsStreaming) { |
| 3529 | ALOGV("%s: Streaming already active!", __FUNCTION__); |
| 3530 | return OK; |
| 3531 | } |
| 3532 | |
| 3533 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", |
| 3534 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3535 | |
| 3536 | if (mAppOpsManager != nullptr) { |
| 3537 | int32_t mode = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, |
| 3538 | mClientPackageName, /*startIfModeDefault*/ false, mClientFeatureId, |
| 3539 | String16("start camera ") + String16(mCameraIdStr)); |
| 3540 | status_t res = handleAppOpMode(mode); |
| 3541 | if (res != OK) { |
| 3542 | return res; |
| 3543 | } |
| 3544 | } |
| 3545 | |
| 3546 | mOpsStreaming = true; |
| 3547 | |
| 3548 | return OK; |
| 3549 | } |
| 3550 | |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 3551 | status_t CameraService::BasicClient::noteAppOp() { |
| 3552 | ATRACE_CALL(); |
| 3553 | |
| 3554 | ALOGV("%s: Start camera noteAppOp, package name = %s, client UID = %d", |
| 3555 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3556 | |
| 3557 | // noteAppOp is only used for when camera mute is not supported, in order |
| 3558 | // to trigger the sensor privacy "Unblock" dialog |
| 3559 | if (mAppOpsManager != nullptr) { |
| 3560 | int32_t mode = mAppOpsManager->noteOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3561 | mClientPackageName, mClientFeatureId, |
| 3562 | String16("start camera ") + String16(mCameraIdStr)); |
| 3563 | status_t res = handleAppOpMode(mode); |
| 3564 | if (res != OK) { |
| 3565 | return res; |
| 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | return OK; |
| 3570 | } |
| 3571 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3572 | status_t CameraService::BasicClient::finishCameraStreamingOps() { |
| 3573 | ATRACE_CALL(); |
| 3574 | |
| 3575 | if (!mOpsActive) { |
| 3576 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3577 | return INVALID_OPERATION; |
| 3578 | } |
| 3579 | if (!mOpsStreaming) { |
| 3580 | ALOGV("%s: Streaming not active!", __FUNCTION__); |
| 3581 | return OK; |
| 3582 | } |
| 3583 | |
| 3584 | if (mAppOpsManager != nullptr) { |
| 3585 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3586 | mClientPackageName, mClientFeatureId); |
| 3587 | mOpsStreaming = false; |
| 3588 | } |
| 3589 | |
| 3590 | return OK; |
| 3591 | } |
| 3592 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3593 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3594 | ATRACE_CALL(); |
| 3595 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3596 | if (mOpsStreaming) { |
| 3597 | // Make sure we've notified everyone about camera stopping |
| 3598 | finishCameraStreamingOps(); |
| 3599 | } |
| 3600 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3601 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3602 | if (mOpsActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3603 | mOpsActive = false; |
| 3604 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3605 | // This function is called when a client disconnects. This should |
| 3606 | // release the camera, but actually only if it was in a proper |
| 3607 | // functional state, i.e. with status NOT_AVAILABLE |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3608 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3609 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3610 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3611 | // 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] | 3612 | sCameraService->updateStatus(StatusInternal::PRESENT, |
| 3613 | mCameraIdStr, rejected); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3614 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3615 | // Always stop watching, even if no camera op is active |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3616 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 3617 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3618 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3619 | mOpsCallback.clear(); |
| 3620 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3621 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid); |
| 3622 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3623 | // Notify listeners of camera open/close status |
| 3624 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); |
| 3625 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3626 | return OK; |
| 3627 | } |
| 3628 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3629 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3630 | ATRACE_CALL(); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3631 | if (mAppOpsManager == nullptr) { |
| 3632 | return; |
| 3633 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3634 | // TODO : add offline camera session case |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3635 | if (op != AppOpsManager::OP_CAMERA) { |
| 3636 | ALOGW("Unexpected app ops notification received: %d", op); |
| 3637 | return; |
| 3638 | } |
| 3639 | |
| 3640 | int32_t res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3641 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3642 | mClientUid, mClientPackageName); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3643 | ALOGV("checkOp returns: %d, %s ", res, |
| 3644 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 3645 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 3646 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 3647 | "UNKNOWN"); |
| 3648 | |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3649 | if (res == AppOpsManager::MODE_ERRORED) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3650 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(), |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3651 | String8(mClientPackageName).string()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3652 | block(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3653 | } else if (res == AppOpsManager::MODE_IGNORED) { |
| 3654 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3655 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3656 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3657 | ALOGI("Camera %s: Access for \"%s\" has been restricted, isUidTrusted %d, isUidActive %d", |
| 3658 | mCameraIdStr.string(), String8(mClientPackageName).string(), |
| 3659 | mUidIsTrusted, isUidActive); |
| 3660 | // If the calling Uid is trusted (a native service), or the client Uid is active (WAR for |
| 3661 | // b/175320666), the AppOpsManager could return MODE_IGNORED. Do not treat such cases as |
| 3662 | // error. |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 3663 | if (!mUidIsTrusted) { |
| 3664 | if (isUidActive && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 3665 | setCameraMute(true); |
| 3666 | } else if (!isUidActive |
| 3667 | || (isCameraPrivacyEnabled && !supportsCameraMute())) { |
| 3668 | block(); |
| 3669 | } |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3670 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3671 | } else if (res == AppOpsManager::MODE_ALLOWED) { |
| 3672 | setCameraMute(sCameraService->mOverrideCameraMuteMode); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3673 | } |
| 3674 | } |
| 3675 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3676 | void CameraService::BasicClient::block() { |
| 3677 | ATRACE_CALL(); |
| 3678 | |
| 3679 | // Reset the client PID to allow server-initiated disconnect, |
| 3680 | // and to prevent further calls by client. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3681 | mClientPid = CameraThreadState::getCallingPid(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3682 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 3683 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); |
| 3684 | disconnect(); |
| 3685 | } |
| 3686 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3687 | // ---------------------------------------------------------------------------- |
| 3688 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3689 | void CameraService::Client::notifyError(int32_t errorCode, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3690 | const CaptureResultExtras& resultExtras) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 3691 | (void) resultExtras; |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3692 | if (mRemoteCallback != NULL) { |
Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 3693 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; |
| 3694 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { |
| 3695 | api1ErrorCode = CAMERA_ERROR_DISABLED; |
| 3696 | } |
| 3697 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3698 | } else { |
| 3699 | ALOGE("mRemoteCallback is NULL!!"); |
| 3700 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3701 | } |
| 3702 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3703 | // NOTE: function is idempotent |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3704 | binder::Status CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3705 | ALOGV("Client::disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3706 | return BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 3707 | } |
| 3708 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3709 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 3710 | return level == API_1; |
| 3711 | } |
| 3712 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3713 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 3714 | mClient(client) { |
| 3715 | } |
| 3716 | |
| 3717 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 3718 | const String16& packageName) { |
| 3719 | sp<BasicClient> client = mClient.promote(); |
| 3720 | if (client != NULL) { |
| 3721 | client->opChanged(op, packageName); |
| 3722 | } |
| 3723 | } |
| 3724 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3725 | // ---------------------------------------------------------------------------- |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3726 | // UidPolicy |
| 3727 | // ---------------------------------------------------------------------------- |
| 3728 | |
| 3729 | void CameraService::UidPolicy::registerSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3730 | Mutex::Autolock _l(mUidLock); |
| 3731 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3732 | if (mRegistered) return; |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3733 | status_t res = mAm.linkToDeath(this); |
| 3734 | mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3735 | | ActivityManager::UID_OBSERVER_IDLE |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3736 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE |
| 3737 | | ActivityManager::UID_OBSERVER_PROC_OOM_ADJ, |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3738 | ActivityManager::PROCESS_STATE_UNKNOWN, |
| 3739 | String16("cameraserver")); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3740 | if (res == OK) { |
| 3741 | mRegistered = true; |
| 3742 | ALOGV("UidPolicy: Registered with ActivityManager"); |
| 3743 | } |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | void CameraService::UidPolicy::unregisterSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3747 | Mutex::Autolock _l(mUidLock); |
| 3748 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3749 | mAm.unregisterUidObserver(this); |
| 3750 | mAm.unlinkToDeath(this); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3751 | mRegistered = false; |
| 3752 | mActiveUids.clear(); |
| 3753 | ALOGV("UidPolicy: Unregistered with ActivityManager"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { |
| 3757 | onUidIdle(uid, disabled); |
| 3758 | } |
| 3759 | |
| 3760 | void CameraService::UidPolicy::onUidActive(uid_t uid) { |
| 3761 | Mutex::Autolock _l(mUidLock); |
| 3762 | mActiveUids.insert(uid); |
| 3763 | } |
| 3764 | |
| 3765 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { |
| 3766 | bool deleted = false; |
| 3767 | { |
| 3768 | Mutex::Autolock _l(mUidLock); |
| 3769 | if (mActiveUids.erase(uid) > 0) { |
| 3770 | deleted = true; |
| 3771 | } |
| 3772 | } |
| 3773 | if (deleted) { |
| 3774 | sp<CameraService> service = mService.promote(); |
| 3775 | if (service != nullptr) { |
| 3776 | service->blockClientsForUid(uid); |
| 3777 | } |
| 3778 | } |
| 3779 | } |
| 3780 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3781 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 3782 | int64_t procStateSeq __unused, int32_t capability __unused) { |
Austin Borger | c5585dc | 2022-05-12 00:48:17 +0000 | [diff] [blame] | 3783 | bool procStateChange = false; |
| 3784 | { |
| 3785 | Mutex::Autolock _l(mUidLock); |
| 3786 | if (mMonitoredUids.find(uid) != mMonitoredUids.end() && |
| 3787 | mMonitoredUids[uid].procState != procState) { |
| 3788 | mMonitoredUids[uid].procState = procState; |
| 3789 | procStateChange = true; |
| 3790 | } |
| 3791 | } |
| 3792 | |
| 3793 | if (procStateChange) { |
| 3794 | sp<CameraService> service = mService.promote(); |
| 3795 | if (service != nullptr) { |
| 3796 | service->notifyMonitoredUids(); |
| 3797 | } |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3798 | } |
| 3799 | } |
| 3800 | |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3801 | void CameraService::UidPolicy::onUidProcAdjChanged(uid_t uid) { |
| 3802 | bool procAdjChange = false; |
| 3803 | { |
| 3804 | Mutex::Autolock _l(mUidLock); |
| 3805 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
| 3806 | procAdjChange = true; |
| 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | if (procAdjChange) { |
| 3811 | sp<CameraService> service = mService.promote(); |
| 3812 | if (service != nullptr) { |
| 3813 | service->notifyMonitoredUids(); |
| 3814 | } |
| 3815 | } |
| 3816 | } |
| 3817 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3818 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid) { |
| 3819 | Mutex::Autolock _l(mUidLock); |
| 3820 | auto it = mMonitoredUids.find(uid); |
| 3821 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3822 | it->second.refCount++; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3823 | } else { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3824 | MonitoredUid monitoredUid; |
| 3825 | monitoredUid.procState = ActivityManager::PROCESS_STATE_NONEXISTENT; |
| 3826 | monitoredUid.refCount = 1; |
| 3827 | mMonitoredUids.emplace(std::pair<uid_t, MonitoredUid>(uid, monitoredUid)); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3828 | } |
| 3829 | } |
| 3830 | |
| 3831 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid) { |
| 3832 | Mutex::Autolock _l(mUidLock); |
| 3833 | auto it = mMonitoredUids.find(uid); |
| 3834 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3835 | it->second.refCount--; |
| 3836 | if (it->second.refCount == 0) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3837 | mMonitoredUids.erase(it); |
| 3838 | } |
| 3839 | } else { |
| 3840 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 3841 | } |
| 3842 | } |
| 3843 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3844 | bool CameraService::UidPolicy::isUidActive(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3845 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3846 | return isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3847 | } |
| 3848 | |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3849 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; |
| 3850 | static const int64_t kPollUidActiveTimeoutMillis = 50; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3851 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3852 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3853 | // Non-app UIDs are considered always active |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3854 | // If activity manager is unreachable, assume everything is active |
| 3855 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3856 | return true; |
| 3857 | } |
| 3858 | auto it = mOverrideUids.find(uid); |
| 3859 | if (it != mOverrideUids.end()) { |
| 3860 | return it->second; |
| 3861 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3862 | bool active = mActiveUids.find(uid) != mActiveUids.end(); |
| 3863 | if (!active) { |
| 3864 | // We want active UIDs to always access camera with their first attempt since |
| 3865 | // there is no guarantee the app is robustly written and would retry getting |
| 3866 | // the camera on failure. The inverse case is not a problem as we would take |
| 3867 | // camera away soon once we get the callback that the uid is no longer active. |
| 3868 | ActivityManager am; |
| 3869 | // Okay to access with a lock held as UID changes are dispatched without |
| 3870 | // a lock and we are a higher level component. |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3871 | int64_t startTimeMillis = 0; |
| 3872 | do { |
| 3873 | // TODO: Fix this b/109950150! |
| 3874 | // Okay this is a hack. There is a race between the UID turning active and |
| 3875 | // activity being resumed. The proper fix is very risky, so we temporary add |
| 3876 | // some polling which should happen pretty rarely anyway as the race is hard |
| 3877 | // to hit. |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3878 | active = mActiveUids.find(uid) != mActiveUids.end(); |
Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 3879 | if (!active) active = am.isUidActive(uid, callingPackage); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3880 | if (active) { |
| 3881 | break; |
| 3882 | } |
| 3883 | if (startTimeMillis <= 0) { |
| 3884 | startTimeMillis = uptimeMillis(); |
| 3885 | } |
| 3886 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3887 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3888 | if (remainingTimeMillis <= 0) { |
| 3889 | break; |
| 3890 | } |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3891 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); |
| 3892 | |
| 3893 | mUidLock.unlock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3894 | usleep(remainingTimeMillis * 1000); |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3895 | mUidLock.lock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3896 | } while (true); |
| 3897 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3898 | if (active) { |
| 3899 | // Now that we found out the UID is actually active, cache that |
| 3900 | mActiveUids.insert(uid); |
| 3901 | } |
| 3902 | } |
| 3903 | return active; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3904 | } |
| 3905 | |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 3906 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { |
| 3907 | Mutex::Autolock _l(mUidLock); |
| 3908 | return getProcStateLocked(uid); |
| 3909 | } |
| 3910 | |
| 3911 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { |
| 3912 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 3913 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3914 | procState = mMonitoredUids[uid].procState; |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 3915 | } |
| 3916 | return procState; |
| 3917 | } |
| 3918 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3919 | void CameraService::UidPolicy::UidPolicy::addOverrideUid(uid_t uid, |
| 3920 | String16 callingPackage, bool active) { |
| 3921 | updateOverrideUid(uid, callingPackage, active, true); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3922 | } |
| 3923 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3924 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, String16 callingPackage) { |
| 3925 | updateOverrideUid(uid, callingPackage, false, false); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3926 | } |
| 3927 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3928 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 3929 | Mutex::Autolock _l(mUidLock); |
| 3930 | ALOGV("UidPolicy: ActivityManager has died"); |
| 3931 | mRegistered = false; |
| 3932 | mActiveUids.clear(); |
| 3933 | } |
| 3934 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3935 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, String16 callingPackage, |
| 3936 | bool active, bool insert) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3937 | bool wasActive = false; |
| 3938 | bool isActive = false; |
| 3939 | { |
| 3940 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3941 | wasActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3942 | mOverrideUids.erase(uid); |
| 3943 | if (insert) { |
| 3944 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 3945 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3946 | isActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3947 | } |
| 3948 | if (wasActive != isActive && !isActive) { |
| 3949 | sp<CameraService> service = mService.promote(); |
| 3950 | if (service != nullptr) { |
| 3951 | service->blockClientsForUid(uid); |
| 3952 | } |
| 3953 | } |
| 3954 | } |
| 3955 | |
| 3956 | // ---------------------------------------------------------------------------- |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3957 | // SensorPrivacyPolicy |
| 3958 | // ---------------------------------------------------------------------------- |
| 3959 | void CameraService::SensorPrivacyPolicy::registerSelf() { |
| 3960 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3961 | if (mRegistered) { |
| 3962 | return; |
| 3963 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3964 | hasCameraPrivacyFeature(); // Called so the result is cached |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 3965 | mSpm.addSensorPrivacyListener(this); |
| 3966 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); |
| 3967 | status_t res = mSpm.linkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3968 | if (res == OK) { |
| 3969 | mRegistered = true; |
| 3970 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); |
| 3971 | } |
| 3972 | } |
| 3973 | |
| 3974 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { |
| 3975 | Mutex::Autolock _l(mSensorPrivacyLock); |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 3976 | mSpm.removeSensorPrivacyListener(this); |
| 3977 | mSpm.unlinkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3978 | mRegistered = false; |
| 3979 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); |
| 3980 | } |
| 3981 | |
| 3982 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
| 3983 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3984 | return mSensorPrivacyEnabled; |
| 3985 | } |
| 3986 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3987 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled() { |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3988 | if (!hasCameraPrivacyFeature()) { |
| 3989 | return false; |
| 3990 | } |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3991 | return mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3992 | } |
| 3993 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3994 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged( |
| 3995 | int toggleType __unused, int sensor __unused, bool enabled) { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3996 | { |
| 3997 | Mutex::Autolock _l(mSensorPrivacyLock); |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3998 | mSensorPrivacyEnabled = mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3999 | } |
| 4000 | // 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] | 4001 | if (enabled) { |
| 4002 | sp<CameraService> service = mService.promote(); |
| 4003 | if (service != nullptr) { |
| 4004 | service->blockAllClients(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4005 | } |
| 4006 | } |
| 4007 | return binder::Status::ok(); |
| 4008 | } |
| 4009 | |
| 4010 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 4011 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4012 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); |
| 4013 | mRegistered = false; |
| 4014 | } |
| 4015 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4016 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4017 | bool supportsSoftwareToggle = mSpm.supportsSensorToggle( |
| 4018 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 4019 | bool supportsHardwareToggle = mSpm.supportsSensorToggle( |
| 4020 | SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 4021 | return supportsSoftwareToggle || supportsHardwareToggle; |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4022 | } |
| 4023 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4024 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4025 | // CameraState |
| 4026 | // ---------------------------------------------------------------------------- |
| 4027 | |
| 4028 | CameraService::CameraState::CameraState(const String8& id, int cost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4029 | const std::set<String8>& conflicting, SystemCameraKind systemCameraKind, |
| 4030 | const std::vector<std::string>& physicalCameras) : mId(id), |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4031 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4032 | mSystemCameraKind(systemCameraKind), mPhysicalCameras(physicalCameras) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4033 | |
| 4034 | CameraService::CameraState::~CameraState() {} |
| 4035 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4036 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4037 | Mutex::Autolock lock(mStatusLock); |
| 4038 | return mStatus; |
| 4039 | } |
| 4040 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4041 | std::vector<String8> CameraService::CameraState::getUnavailablePhysicalIds() const { |
| 4042 | Mutex::Autolock lock(mStatusLock); |
| 4043 | std::vector<String8> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); |
| 4044 | return res; |
| 4045 | } |
| 4046 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4047 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 4048 | return mShimParams; |
| 4049 | } |
| 4050 | |
| 4051 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 4052 | mShimParams = params; |
| 4053 | } |
| 4054 | |
| 4055 | int CameraService::CameraState::getCost() const { |
| 4056 | return mCost; |
| 4057 | } |
| 4058 | |
| 4059 | std::set<String8> CameraService::CameraState::getConflicting() const { |
| 4060 | return mConflicting; |
| 4061 | } |
| 4062 | |
| 4063 | String8 CameraService::CameraState::getId() const { |
| 4064 | return mId; |
| 4065 | } |
| 4066 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4067 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { |
| 4068 | return mSystemCameraKind; |
| 4069 | } |
| 4070 | |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4071 | bool CameraService::CameraState::containsPhysicalCamera(const std::string& physicalCameraId) const { |
| 4072 | return std::find(mPhysicalCameras.begin(), mPhysicalCameras.end(), physicalCameraId) |
| 4073 | != mPhysicalCameras.end(); |
| 4074 | } |
| 4075 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4076 | bool CameraService::CameraState::addUnavailablePhysicalId(const String8& physicalId) { |
| 4077 | Mutex::Autolock lock(mStatusLock); |
| 4078 | auto result = mUnavailablePhysicalIds.insert(physicalId); |
| 4079 | return result.second; |
| 4080 | } |
| 4081 | |
| 4082 | bool CameraService::CameraState::removeUnavailablePhysicalId(const String8& physicalId) { |
| 4083 | Mutex::Autolock lock(mStatusLock); |
| 4084 | auto count = mUnavailablePhysicalIds.erase(physicalId); |
| 4085 | return count > 0; |
| 4086 | } |
| 4087 | |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 4088 | void CameraService::CameraState::setClientPackage(const String8& clientPackage) { |
| 4089 | Mutex::Autolock lock(mStatusLock); |
| 4090 | mClientPackage = clientPackage; |
| 4091 | } |
| 4092 | |
| 4093 | String8 CameraService::CameraState::getClientPackage() const { |
| 4094 | Mutex::Autolock lock(mStatusLock); |
| 4095 | return mClientPackage; |
| 4096 | } |
| 4097 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4098 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4099 | // ClientEventListener |
| 4100 | // ---------------------------------------------------------------------------- |
| 4101 | |
| 4102 | void CameraService::ClientEventListener::onClientAdded( |
| 4103 | const resource_policy::ClientDescriptor<String8, |
| 4104 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 4105 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4106 | if (basicClient.get() != nullptr) { |
| 4107 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 4108 | notifier.noteStartCamera(descriptor.getKey(), |
| 4109 | static_cast<int>(basicClient->getClientUid())); |
| 4110 | } |
| 4111 | } |
| 4112 | |
| 4113 | void CameraService::ClientEventListener::onClientRemoved( |
| 4114 | const resource_policy::ClientDescriptor<String8, |
| 4115 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 4116 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4117 | if (basicClient.get() != nullptr) { |
| 4118 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 4119 | notifier.noteStopCamera(descriptor.getKey(), |
| 4120 | static_cast<int>(basicClient->getClientUid())); |
| 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | |
| 4125 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4126 | // CameraClientManager |
| 4127 | // ---------------------------------------------------------------------------- |
| 4128 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4129 | CameraService::CameraClientManager::CameraClientManager() { |
| 4130 | setListener(std::make_shared<ClientEventListener>()); |
| 4131 | } |
| 4132 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4133 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 4134 | |
| 4135 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
| 4136 | const String8& id) const { |
| 4137 | auto descriptor = get(id); |
| 4138 | if (descriptor == nullptr) { |
| 4139 | return sp<BasicClient>{nullptr}; |
| 4140 | } |
| 4141 | return descriptor->getValue(); |
| 4142 | } |
| 4143 | |
| 4144 | String8 CameraService::CameraClientManager::toString() const { |
| 4145 | auto all = getAll(); |
| 4146 | String8 ret("["); |
| 4147 | bool hasAny = false; |
| 4148 | for (auto& i : all) { |
| 4149 | hasAny = true; |
| 4150 | String8 key = i->getKey(); |
| 4151 | int32_t cost = i->getCost(); |
| 4152 | int32_t pid = i->getOwnerId(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4153 | int32_t score = i->getPriority().getScore(); |
| 4154 | int32_t state = i->getPriority().getState(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4155 | auto conflicting = i->getConflicting(); |
| 4156 | auto clientSp = i->getValue(); |
| 4157 | String8 packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 4158 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4159 | if (clientSp.get() != nullptr) { |
| 4160 | packageName = String8{clientSp->getPackageName()}; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4161 | uid_t clientUid = clientSp->getClientUid(); |
| 4162 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4163 | } |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4164 | ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" |
| 4165 | PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4166 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4167 | if (clientSp.get() != nullptr) { |
| 4168 | ret.appendFormat("User Id: %d, ", clientUserId); |
| 4169 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4170 | if (packageName.size() != 0) { |
| 4171 | ret.appendFormat("Client Package Name: %s", packageName.string()); |
| 4172 | } |
| 4173 | |
| 4174 | ret.append(", Conflicting Client Devices: {"); |
| 4175 | for (auto& j : conflicting) { |
| 4176 | ret.appendFormat("%s, ", j.string()); |
| 4177 | } |
| 4178 | ret.append("})"); |
| 4179 | } |
| 4180 | if (hasAny) ret.append("\n"); |
| 4181 | ret.append("]\n"); |
| 4182 | return ret; |
| 4183 | } |
| 4184 | |
| 4185 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
| 4186 | const String8& key, const sp<BasicClient>& value, int32_t cost, |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4187 | const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4188 | int32_t state, int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4189 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4190 | int32_t score_adj = systemNativeClient ? kSystemNativeClientScore : score; |
| 4191 | int32_t state_adj = systemNativeClient ? kSystemNativeClientState: state; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 4192 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4193 | return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>( |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4194 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, |
| 4195 | systemNativeClient, oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 4199 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4200 | int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4201 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4202 | partial->getConflicting(), partial->getPriority().getScore(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4203 | partial->getOwnerId(), partial->getPriority().getState(), oomScoreOffset, |
| 4204 | systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | // ---------------------------------------------------------------------------- |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4208 | // InjectionStatusListener |
| 4209 | // ---------------------------------------------------------------------------- |
| 4210 | |
| 4211 | void CameraService::InjectionStatusListener::addListener( |
| 4212 | const sp<ICameraInjectionCallback>& callback) { |
| 4213 | Mutex::Autolock lock(mListenerLock); |
| 4214 | if (mCameraInjectionCallback) return; |
| 4215 | status_t res = IInterface::asBinder(callback)->linkToDeath(this); |
| 4216 | if (res == OK) { |
| 4217 | mCameraInjectionCallback = callback; |
| 4218 | } |
| 4219 | } |
| 4220 | |
| 4221 | void CameraService::InjectionStatusListener::removeListener() { |
| 4222 | Mutex::Autolock lock(mListenerLock); |
| 4223 | if (mCameraInjectionCallback == nullptr) { |
| 4224 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 4225 | return; |
| 4226 | } |
| 4227 | IInterface::asBinder(mCameraInjectionCallback)->unlinkToDeath(this); |
| 4228 | mCameraInjectionCallback = nullptr; |
| 4229 | } |
| 4230 | |
| 4231 | void CameraService::InjectionStatusListener::notifyInjectionError( |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4232 | String8 injectedCamId, status_t err) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4233 | if (mCameraInjectionCallback == nullptr) { |
| 4234 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 4235 | return; |
| 4236 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4237 | |
| 4238 | switch (err) { |
| 4239 | case -ENODEV: |
| 4240 | mCameraInjectionCallback->onInjectionError( |
| 4241 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4242 | ALOGE("No camera device with ID \"%s\" currently available!", |
| 4243 | injectedCamId.string()); |
| 4244 | break; |
| 4245 | case -EBUSY: |
| 4246 | mCameraInjectionCallback->onInjectionError( |
| 4247 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4248 | ALOGE("Higher-priority client using camera, ID \"%s\" currently unavailable!", |
| 4249 | injectedCamId.string()); |
| 4250 | break; |
| 4251 | case DEAD_OBJECT: |
| 4252 | mCameraInjectionCallback->onInjectionError( |
| 4253 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4254 | ALOGE("Camera ID \"%s\" object is dead!", |
| 4255 | injectedCamId.string()); |
| 4256 | break; |
| 4257 | case INVALID_OPERATION: |
| 4258 | mCameraInjectionCallback->onInjectionError( |
| 4259 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4260 | ALOGE("Camera ID \"%s\" encountered an operating or internal error!", |
| 4261 | injectedCamId.string()); |
| 4262 | break; |
| 4263 | case UNKNOWN_TRANSACTION: |
| 4264 | mCameraInjectionCallback->onInjectionError( |
| 4265 | ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED); |
| 4266 | ALOGE("Camera ID \"%s\" method doesn't support!", |
| 4267 | injectedCamId.string()); |
| 4268 | break; |
| 4269 | default: |
| 4270 | mCameraInjectionCallback->onInjectionError( |
| 4271 | ICameraInjectionCallback::ERROR_INJECTION_INVALID_ERROR); |
| 4272 | ALOGE("Unexpected error %s (%d) opening camera \"%s\"!", |
| 4273 | strerror(-err), err, injectedCamId.string()); |
| 4274 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4275 | } |
| 4276 | |
| 4277 | void CameraService::InjectionStatusListener::binderDied( |
| 4278 | const wp<IBinder>& /*who*/) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4279 | ALOGV("InjectionStatusListener: ICameraInjectionCallback has died"); |
| 4280 | auto parent = mParent.promote(); |
| 4281 | if (parent != nullptr) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4282 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 4283 | if (clientDescriptor != nullptr) { |
| 4284 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 4285 | baseClientPtr->stopInjection(); |
| 4286 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 4287 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4288 | } |
| 4289 | } |
| 4290 | |
| 4291 | // ---------------------------------------------------------------------------- |
| 4292 | // CameraInjectionSession |
| 4293 | // ---------------------------------------------------------------------------- |
| 4294 | |
| 4295 | binder::Status CameraService::CameraInjectionSession::stopInjection() { |
| 4296 | Mutex::Autolock lock(mInjectionSessionLock); |
| 4297 | auto parent = mParent.promote(); |
| 4298 | if (parent == nullptr) { |
| 4299 | ALOGE("CameraInjectionSession: Parent is gone"); |
| 4300 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SERVICE, |
| 4301 | "Camera service encountered error"); |
| 4302 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4303 | |
| 4304 | status_t res = NO_ERROR; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4305 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 4306 | if (clientDescriptor != nullptr) { |
| 4307 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 4308 | res = baseClientPtr->stopInjection(); |
| 4309 | if (res != OK) { |
| 4310 | ALOGE("CameraInjectionSession: Failed to stop the injection camera!" |
| 4311 | " ret != NO_ERROR: %d", res); |
| 4312 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SESSION, |
| 4313 | "Camera session encountered error"); |
| 4314 | } |
| 4315 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 4316 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4317 | return binder::Status::ok(); |
| 4318 | } |
| 4319 | |
| 4320 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4321 | |
| 4322 | static const int kDumpLockRetries = 50; |
| 4323 | static const int kDumpLockSleep = 60000; |
| 4324 | |
| 4325 | static bool tryLock(Mutex& mutex) |
| 4326 | { |
| 4327 | bool locked = false; |
| 4328 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 4329 | if (mutex.tryLock() == NO_ERROR) { |
| 4330 | locked = true; |
| 4331 | break; |
| 4332 | } |
| 4333 | usleep(kDumpLockSleep); |
| 4334 | } |
| 4335 | return locked; |
| 4336 | } |
| 4337 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4338 | void CameraService::cacheDump() { |
| 4339 | if (mMemFd != -1) { |
| 4340 | const Vector<String16> args; |
| 4341 | ATRACE_CALL(); |
| 4342 | // Acquiring service lock here will avoid the deadlock since |
| 4343 | // cacheDump will not be called during the second disconnect. |
| 4344 | Mutex::Autolock lock(mServiceLock); |
| 4345 | |
| 4346 | Mutex::Autolock l(mCameraStatesLock); |
| 4347 | // Start collecting the info for open sessions and store it in temp file. |
| 4348 | for (const auto& state : mCameraStates) { |
| 4349 | String8 cameraId = state.first; |
| 4350 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 4351 | if (clientDescriptor != nullptr) { |
| 4352 | dprintf(mMemFd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
| 4353 | // Log the current open session info before device is disconnected. |
| 4354 | dumpOpenSessionClientLogs(mMemFd, args, cameraId); |
| 4355 | } |
| 4356 | } |
| 4357 | } |
| 4358 | } |
| 4359 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4360 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4361 | ATRACE_CALL(); |
| 4362 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4363 | if (checkCallingPermission(sDumpPermission) == false) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4364 | 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] | 4365 | CameraThreadState::getCallingPid(), |
| 4366 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4367 | return NO_ERROR; |
| 4368 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4369 | bool locked = tryLock(mServiceLock); |
| 4370 | // failed to lock - CameraService is probably deadlocked |
| 4371 | if (!locked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4372 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4373 | } |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4374 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4375 | if (!mInitialized) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4376 | dprintf(fd, "!! No camera HAL available !!\n"); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 4377 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4378 | // Dump event log for error information |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4379 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4380 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4381 | if (locked) mServiceLock.unlock(); |
| 4382 | return NO_ERROR; |
| 4383 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4384 | dprintf(fd, "\n== Service global info: ==\n\n"); |
| 4385 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4386 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 4387 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", |
| 4388 | mNormalDeviceIdsWithoutSystemCamera.size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4389 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { |
| 4390 | dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); |
| 4391 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4392 | String8 activeClientString = mActiveClientManager.toString(); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4393 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string()); |
| 4394 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4395 | |
| 4396 | dumpEventLog(fd); |
| 4397 | |
| 4398 | bool stateLocked = tryLock(mCameraStatesLock); |
| 4399 | if (!stateLocked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4400 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4401 | } |
| 4402 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 4403 | int argSize = args.size(); |
| 4404 | for (int i = 0; i < argSize; i++) { |
| 4405 | if (args[i] == TagMonitor::kMonitorOption) { |
| 4406 | if (i + 1 < argSize) { |
| 4407 | mMonitorTags = String8(args[i + 1]); |
| 4408 | } |
| 4409 | break; |
| 4410 | } |
| 4411 | } |
| 4412 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4413 | for (auto& state : mCameraStates) { |
| 4414 | String8 cameraId = state.first; |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4415 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4416 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4417 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4418 | CameraParameters p = state.second->getShimParams(); |
| 4419 | if (!p.isEmpty()) { |
| 4420 | dprintf(fd, " Camera1 API shim is using parameters:\n "); |
| 4421 | p.dump(fd, args); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4425 | if (clientDescriptor != nullptr) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4426 | // log the current open session info |
| 4427 | dumpOpenSessionClientLogs(fd, args, cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4428 | } else { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4429 | dumpClosedSessionClientLogs(fd, cameraId); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4430 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4431 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | if (stateLocked) mCameraStatesLock.unlock(); |
| 4435 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4436 | if (locked) mServiceLock.unlock(); |
| 4437 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 4438 | mCameraProviderManager->dump(fd, args); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4439 | |
| 4440 | dprintf(fd, "\n== Vendor tags: ==\n\n"); |
| 4441 | |
| 4442 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 4443 | if (desc == NULL) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 4444 | sp<VendorTagDescriptorCache> cache = |
| 4445 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 4446 | if (cache == NULL) { |
| 4447 | dprintf(fd, "No vendor tags.\n"); |
| 4448 | } else { |
| 4449 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 4450 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4451 | } else { |
| 4452 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 4453 | } |
| 4454 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4455 | // Dump camera traces if there were any |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4456 | dprintf(fd, "\n"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4457 | camera3::CameraTraces::dump(fd); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4458 | |
| 4459 | // Process dump arguments, if any |
| 4460 | int n = args.size(); |
| 4461 | String16 verboseOption("-v"); |
| 4462 | String16 unreachableOption("--unreachable"); |
| 4463 | for (int i = 0; i < n; i++) { |
| 4464 | if (args[i] == verboseOption) { |
| 4465 | // change logging level |
| 4466 | if (i + 1 >= n) continue; |
| 4467 | String8 levelStr(args[i+1]); |
| 4468 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4469 | dprintf(fd, "\nSetting log level to %d.\n", level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4470 | setLogLevel(level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4471 | } else if (args[i] == unreachableOption) { |
| 4472 | // Dump memory analysis |
| 4473 | // TODO - should limit be an argument parameter? |
| 4474 | UnreachableMemoryInfo info; |
| 4475 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); |
| 4476 | if (!success) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4477 | dprintf(fd, "\n== Unable to dump unreachable memory. " |
| 4478 | "Try disabling SELinux enforcement. ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4479 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4480 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4481 | std::string s = info.ToString(/*log_contents*/ true); |
| 4482 | write(fd, s.c_str(), s.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4483 | } |
| 4484 | } |
| 4485 | } |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4486 | |
| 4487 | bool serviceLocked = tryLock(mServiceLock); |
| 4488 | |
| 4489 | // Dump info from previous open sessions. |
| 4490 | // Reposition the offset to beginning of the file before reading |
| 4491 | |
| 4492 | if ((mMemFd >= 0) && (lseek(mMemFd, 0, SEEK_SET) != -1)) { |
| 4493 | dprintf(fd, "\n**********Dumpsys from previous open session**********\n"); |
| 4494 | ssize_t size_read; |
| 4495 | char buf[4096]; |
| 4496 | while ((size_read = read(mMemFd, buf, (sizeof(buf) - 1))) > 0) { |
| 4497 | // Read data from file to a small buffer and write it to fd. |
| 4498 | write(fd, buf, size_read); |
| 4499 | if (size_read == -1) { |
| 4500 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 4501 | break; |
| 4502 | } |
| 4503 | } |
| 4504 | dprintf(fd, "\n**********End of Dumpsys from previous open session**********\n"); |
| 4505 | } else { |
| 4506 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 4507 | } |
| 4508 | |
| 4509 | if (serviceLocked) mServiceLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4510 | return NO_ERROR; |
| 4511 | } |
| 4512 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4513 | void CameraService::dumpOpenSessionClientLogs(int fd, |
| 4514 | const Vector<String16>& args, const String8& cameraId) { |
| 4515 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Rucha Katakwar | 71a0e05 | 2022-04-07 15:44:18 -0700 | [diff] [blame] | 4516 | dprintf(fd, " %s : Device %s is open. Client instance dump:\n", |
| 4517 | getFormattedCurrentTime().string(), |
| 4518 | cameraId.string()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4519 | dprintf(fd, " Client priority score: %d state: %d\n", |
| 4520 | clientDescriptor->getPriority().getScore(), |
| 4521 | clientDescriptor->getPriority().getState()); |
| 4522 | dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 4523 | |
| 4524 | auto client = clientDescriptor->getValue(); |
| 4525 | dprintf(fd, " Client package: %s\n", |
| 4526 | String8(client->getPackageName()).string()); |
| 4527 | |
| 4528 | client->dumpClient(fd, args); |
| 4529 | } |
| 4530 | |
| 4531 | void CameraService::dumpClosedSessionClientLogs(int fd, const String8& cameraId) { |
| 4532 | dprintf(fd, " Device %s is closed, no client instance\n", |
| 4533 | cameraId.string()); |
| 4534 | } |
| 4535 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4536 | void CameraService::dumpEventLog(int fd) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4537 | 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] | 4538 | |
| 4539 | Mutex::Autolock l(mLogLock); |
| 4540 | for (const auto& msg : mEventLog) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4541 | dprintf(fd, " %s\n", msg.string()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4542 | } |
| 4543 | |
| 4544 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4545 | dprintf(fd, " ...\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4546 | } else if (mEventLog.size() == 0) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4547 | dprintf(fd, " [no events yet]\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4548 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4549 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4550 | } |
| 4551 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4552 | void CameraService::cacheClientTagDumpIfNeeded(const char *cameraId, BasicClient* client) { |
| 4553 | Mutex::Autolock lock(mLogLock); |
| 4554 | if (!isClientWatchedLocked(client)) { return; } |
| 4555 | |
| 4556 | std::vector<std::string> dumpVector; |
| 4557 | client->dumpWatchedEventsToVector(dumpVector); |
| 4558 | |
| 4559 | if (dumpVector.empty()) { return; } |
| 4560 | |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 4561 | std::string dumpString; |
| 4562 | |
| 4563 | String8 currentTime = getFormattedCurrentTime(); |
| 4564 | dumpString += "Cached @ "; |
| 4565 | dumpString += currentTime.string(); |
| 4566 | dumpString += "\n"; // First line is the timestamp of when client is cached. |
| 4567 | |
| 4568 | |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4569 | const String16 &packageName = client->getPackageName(); |
| 4570 | |
| 4571 | String8 packageName8 = String8(packageName); |
| 4572 | const char *printablePackageName = packageName8.lockBuffer(packageName.size()); |
| 4573 | |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 4574 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4575 | size_t i = dumpVector.size(); |
| 4576 | |
| 4577 | // Store the string in reverse order (latest last) |
| 4578 | while (i > 0) { |
| 4579 | i--; |
| 4580 | dumpString += cameraId; |
| 4581 | dumpString += ":"; |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4582 | dumpString += printablePackageName; |
| 4583 | dumpString += " "; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4584 | dumpString += dumpVector[i]; // implicitly ends with '\n' |
| 4585 | } |
| 4586 | |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4587 | packageName8.unlockBuffer(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4588 | mWatchedClientsDumpCache[packageName] = dumpString; |
| 4589 | } |
| 4590 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4591 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4592 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4593 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 4594 | if (mTorchClientMap[i] == who) { |
| 4595 | // 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] | 4596 | String8 cameraId = mTorchClientMap.keyAt(i); |
| 4597 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 4598 | if (res) { |
| 4599 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 4600 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 4601 | return; |
| 4602 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4603 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4604 | break; |
| 4605 | } |
| 4606 | } |
| 4607 | } |
| 4608 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4609 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 4610 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 4611 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4612 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 4613 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 4614 | */ |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 4615 | // PID here is approximate and can be wrong. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 4616 | logClientDied(CameraThreadState::getCallingPid(), String8("Binder died unexpectedly")); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4617 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4618 | // check torch client |
| 4619 | handleTorchClientBinderDied(who); |
| 4620 | |
| 4621 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4622 | if(!evictClientIdByRemote(who)) { |
| 4623 | 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] | 4624 | return; |
| 4625 | } |
| 4626 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4627 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 4628 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 4629 | } |
| 4630 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4631 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4632 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 4633 | } |
| 4634 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4635 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId, |
| 4636 | std::initializer_list<StatusInternal> rejectSourceStates) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4637 | // Do not lock mServiceLock here or can get into a deadlock from |
| 4638 | // connect() -> disconnect -> updateStatus |
| 4639 | |
| 4640 | auto state = getCameraState(cameraId); |
| 4641 | |
| 4642 | if (state == nullptr) { |
| 4643 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 4644 | cameraId.string()); |
| 4645 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4646 | } |
| 4647 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4648 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 4649 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 4650 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 4651 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 4652 | return; |
| 4653 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4654 | |
| 4655 | // Collect the logical cameras without holding mStatusLock in updateStatus |
| 4656 | // as that can lead to a deadlock(b/162192331). |
| 4657 | auto logicalCameraIds = getLogicalCameras(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4658 | // 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] | 4659 | // of the listeners with both the mStatusLock and mStatusListenerLock held |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 4660 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4661 | &logicalCameraIds] |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4662 | (const String8& cameraId, StatusInternal status) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4663 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4664 | if (status != StatusInternal::ENUMERATING) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4665 | // Update torch status if it has a flash unit. |
| 4666 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4667 | TorchModeStatus torchStatus; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4668 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 4669 | NAME_NOT_FOUND) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4670 | TorchModeStatus newTorchStatus = |
| 4671 | status == StatusInternal::PRESENT ? |
| 4672 | TorchModeStatus::AVAILABLE_OFF : |
| 4673 | TorchModeStatus::NOT_AVAILABLE; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4674 | if (torchStatus != newTorchStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 4675 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4676 | } |
| 4677 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4678 | } |
| 4679 | |
| 4680 | Mutex::Autolock lock(mStatusListenerLock); |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4681 | notifyPhysicalCameraStatusLocked(mapToInterface(status), String16(cameraId), |
| 4682 | logicalCameraIds, deviceKind); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4683 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4684 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 4685 | bool isVendorListener = listener->isVendorListener(); |
| 4686 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, |
| 4687 | listener->getListenerPid(), listener->getListenerUid()) || |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 4688 | isVendorListener) { |
| 4689 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4690 | cameraId.c_str()); |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 4691 | continue; |
| 4692 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4693 | auto ret = listener->getListener()->onStatusChanged(mapToInterface(status), |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4694 | String16(cameraId)); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4695 | listener->handleBinderStatus(ret, |
| 4696 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", |
| 4697 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 4698 | ret.exceptionCode()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4699 | } |
| 4700 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4701 | } |
| 4702 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4703 | void CameraService::updateOpenCloseStatus(const String8& cameraId, bool open, |
| 4704 | const String16& clientPackageName) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 4705 | auto state = getCameraState(cameraId); |
| 4706 | if (state == nullptr) { |
| 4707 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 4708 | cameraId.string()); |
| 4709 | return; |
| 4710 | } |
| 4711 | if (open) { |
| 4712 | state->setClientPackage(String8(clientPackageName)); |
| 4713 | } else { |
| 4714 | state->setClientPackage(String8::empty()); |
| 4715 | } |
| 4716 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4717 | Mutex::Autolock lock(mStatusListenerLock); |
| 4718 | |
| 4719 | for (const auto& it : mListenerList) { |
| 4720 | if (!it->isOpenCloseCallbackAllowed()) { |
| 4721 | continue; |
| 4722 | } |
| 4723 | |
| 4724 | binder::Status ret; |
| 4725 | String16 cameraId64(cameraId); |
| 4726 | if (open) { |
| 4727 | ret = it->getListener()->onCameraOpened(cameraId64, clientPackageName); |
| 4728 | } else { |
| 4729 | ret = it->getListener()->onCameraClosed(cameraId64); |
| 4730 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4731 | |
| 4732 | it->handleBinderStatus(ret, |
| 4733 | "%s: Failed to trigger onCameraOpened/onCameraClosed callback for %d:%d: %d", |
| 4734 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4735 | } |
| 4736 | } |
| 4737 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4738 | template<class Func> |
| 4739 | void CameraService::CameraState::updateStatus(StatusInternal status, |
| 4740 | const String8& cameraId, |
| 4741 | std::initializer_list<StatusInternal> rejectSourceStates, |
| 4742 | Func onStatusUpdatedLocked) { |
| 4743 | Mutex::Autolock lock(mStatusLock); |
| 4744 | StatusInternal oldStatus = mStatus; |
| 4745 | mStatus = status; |
| 4746 | |
| 4747 | if (oldStatus == status) { |
| 4748 | return; |
| 4749 | } |
| 4750 | |
| 4751 | ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__, |
| 4752 | cameraId.string(), oldStatus, status); |
| 4753 | |
| 4754 | if (oldStatus == StatusInternal::NOT_PRESENT && |
| 4755 | (status != StatusInternal::PRESENT && |
| 4756 | status != StatusInternal::ENUMERATING)) { |
| 4757 | |
| 4758 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 4759 | __FUNCTION__); |
| 4760 | mStatus = oldStatus; |
| 4761 | return; |
| 4762 | } |
| 4763 | |
| 4764 | /** |
| 4765 | * Sometimes we want to conditionally do a transition. |
| 4766 | * For example if a client disconnects, we want to go to PRESENT |
| 4767 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 4768 | */ |
| 4769 | for (auto& rejectStatus : rejectSourceStates) { |
| 4770 | if (oldStatus == rejectStatus) { |
| 4771 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
| 4772 | "state was was in one of the bad states.", __FUNCTION__, cameraId.string()); |
| 4773 | mStatus = oldStatus; |
| 4774 | return; |
| 4775 | } |
| 4776 | } |
| 4777 | |
| 4778 | onStatusUpdatedLocked(cameraId, status); |
| 4779 | } |
| 4780 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4781 | status_t CameraService::getTorchStatusLocked( |
| 4782 | const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4783 | TorchModeStatus *status) const { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4784 | if (!status) { |
| 4785 | return BAD_VALUE; |
| 4786 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4787 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 4788 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4789 | // invalid camera ID or the camera doesn't have a flash unit |
| 4790 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4791 | } |
| 4792 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4793 | *status = mTorchStatusMap.valueAt(index); |
| 4794 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4795 | } |
| 4796 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4797 | status_t CameraService::setTorchStatusLocked(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4798 | TorchModeStatus status) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4799 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 4800 | if (index == NAME_NOT_FOUND) { |
| 4801 | return BAD_VALUE; |
| 4802 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4803 | mTorchStatusMap.editValueAt(index) = status; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4804 | |
| 4805 | return OK; |
| 4806 | } |
| 4807 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4808 | std::list<String16> CameraService::getLogicalCameras( |
| 4809 | const String8& physicalCameraId) { |
| 4810 | std::list<String16> retList; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4811 | Mutex::Autolock lock(mCameraStatesLock); |
| 4812 | for (const auto& state : mCameraStates) { |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4813 | if (state.second->containsPhysicalCamera(physicalCameraId.c_str())) { |
| 4814 | retList.emplace_back(String16(state.first)); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4815 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4816 | } |
| 4817 | return retList; |
| 4818 | } |
| 4819 | |
| 4820 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, |
| 4821 | const String16& physicalCameraId, const std::list<String16>& logicalCameraIds, |
| 4822 | SystemCameraKind deviceKind) { |
| 4823 | // mStatusListenerLock is expected to be locked |
| 4824 | for (const auto& logicalCameraId : logicalCameraIds) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4825 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4826 | // Note: we check only the deviceKind of the physical camera id |
| 4827 | // since, logical camera ids and their physical camera ids are |
| 4828 | // guaranteed to have the same system camera kind. |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 4829 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 4830 | listener->getListenerPid(), listener->getListenerUid())) { |
| 4831 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4832 | String8(physicalCameraId).c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 4833 | continue; |
| 4834 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4835 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged(status, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4836 | logicalCameraId, physicalCameraId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4837 | listener->handleBinderStatus(ret, |
| 4838 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 4839 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 4840 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4841 | } |
| 4842 | } |
| 4843 | } |
| 4844 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4845 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4846 | void CameraService::blockClientsForUid(uid_t uid) { |
| 4847 | const auto clients = mActiveClientManager.getAll(); |
| 4848 | for (auto& current : clients) { |
| 4849 | if (current != nullptr) { |
| 4850 | const auto basicClient = current->getValue(); |
| 4851 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { |
| 4852 | basicClient->block(); |
| 4853 | } |
| 4854 | } |
| 4855 | } |
| 4856 | } |
| 4857 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4858 | void CameraService::blockAllClients() { |
| 4859 | const auto clients = mActiveClientManager.getAll(); |
| 4860 | for (auto& current : clients) { |
| 4861 | if (current != nullptr) { |
| 4862 | const auto basicClient = current->getValue(); |
| 4863 | if (basicClient.get() != nullptr) { |
| 4864 | basicClient->block(); |
| 4865 | } |
| 4866 | } |
| 4867 | } |
| 4868 | } |
| 4869 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4870 | // NOTE: This is a remote API - make sure all args are validated |
| 4871 | status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) { |
| 4872 | if (!checkCallingPermission(sManageCameraPermission, nullptr, nullptr)) { |
| 4873 | return PERMISSION_DENIED; |
| 4874 | } |
| 4875 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 4876 | return BAD_VALUE; |
| 4877 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4878 | if (args.size() >= 3 && args[0] == String16("set-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4879 | return handleSetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4880 | } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4881 | return handleResetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4882 | } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4883 | return handleGetUidState(args, out, err); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4884 | } else if (args.size() >= 2 && args[0] == String16("set-rotate-and-crop")) { |
| 4885 | return handleSetRotateAndCrop(args); |
| 4886 | } else if (args.size() >= 1 && args[0] == String16("get-rotate-and-crop")) { |
| 4887 | return handleGetRotateAndCrop(out); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4888 | } else if (args.size() >= 2 && args[0] == String16("set-image-dump-mask")) { |
| 4889 | return handleSetImageDumpMask(args); |
| 4890 | } else if (args.size() >= 1 && args[0] == String16("get-image-dump-mask")) { |
| 4891 | return handleGetImageDumpMask(out); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 4892 | } else if (args.size() >= 2 && args[0] == String16("set-camera-mute")) { |
| 4893 | return handleSetCameraMute(args); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4894 | } else if (args.size() >= 2 && args[0] == String16("watch")) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 4895 | return handleWatchCommand(args, in, out); |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 4896 | } else if (args.size() >= 2 && args[0] == String16("set-watchdog")) { |
| 4897 | return handleSetCameraServiceWatchdog(args); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4898 | } else if (args.size() == 1 && args[0] == String16("help")) { |
| 4899 | printHelp(out); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4900 | return OK; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4901 | } |
| 4902 | printHelp(err); |
| 4903 | return BAD_VALUE; |
| 4904 | } |
| 4905 | |
| 4906 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4907 | String16 packageName = args[1]; |
| 4908 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4909 | bool active = false; |
| 4910 | if (args[2] == String16("active")) { |
| 4911 | active = true; |
| 4912 | } else if ((args[2] != String16("idle"))) { |
| 4913 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string()); |
| 4914 | return BAD_VALUE; |
| 4915 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4916 | |
| 4917 | int userId = 0; |
| 4918 | if (args.size() >= 5 && args[3] == String16("--user")) { |
| 4919 | userId = atoi(String8(args[4])); |
| 4920 | } |
| 4921 | |
| 4922 | uid_t uid; |
| 4923 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
| 4924 | return BAD_VALUE; |
| 4925 | } |
| 4926 | |
| 4927 | mUidPolicy->addOverrideUid(uid, packageName, active); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4928 | return NO_ERROR; |
| 4929 | } |
| 4930 | |
| 4931 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4932 | String16 packageName = args[1]; |
| 4933 | |
| 4934 | int userId = 0; |
| 4935 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 4936 | userId = atoi(String8(args[3])); |
| 4937 | } |
| 4938 | |
| 4939 | uid_t uid; |
| 4940 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4941 | return BAD_VALUE; |
| 4942 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4943 | |
| 4944 | mUidPolicy->removeOverrideUid(uid, packageName); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4945 | return NO_ERROR; |
| 4946 | } |
| 4947 | |
| 4948 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4949 | String16 packageName = args[1]; |
| 4950 | |
| 4951 | int userId = 0; |
| 4952 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 4953 | userId = atoi(String8(args[3])); |
| 4954 | } |
| 4955 | |
| 4956 | uid_t uid; |
| 4957 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4958 | return BAD_VALUE; |
| 4959 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4960 | |
| 4961 | if (mUidPolicy->isUidActive(uid, packageName)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4962 | return dprintf(out, "active\n"); |
| 4963 | } else { |
| 4964 | return dprintf(out, "idle\n"); |
| 4965 | } |
| 4966 | } |
| 4967 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4968 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { |
| 4969 | int rotateValue = atoi(String8(args[1])); |
| 4970 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || |
| 4971 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; |
| 4972 | Mutex::Autolock lock(mServiceLock); |
| 4973 | |
| 4974 | mOverrideRotateAndCropMode = rotateValue; |
| 4975 | |
| 4976 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; |
| 4977 | |
| 4978 | const auto clients = mActiveClientManager.getAll(); |
| 4979 | for (auto& current : clients) { |
| 4980 | if (current != nullptr) { |
| 4981 | const auto basicClient = current->getValue(); |
| 4982 | if (basicClient.get() != nullptr) { |
| 4983 | basicClient->setRotateAndCropOverride(rotateValue); |
| 4984 | } |
| 4985 | } |
| 4986 | } |
| 4987 | |
| 4988 | return OK; |
| 4989 | } |
| 4990 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 4991 | status_t CameraService::handleSetCameraServiceWatchdog(const Vector<String16>& args) { |
| 4992 | int enableWatchdog = atoi(String8(args[1])); |
| 4993 | |
| 4994 | if (enableWatchdog < 0 || enableWatchdog > 1) return BAD_VALUE; |
| 4995 | |
| 4996 | Mutex::Autolock lock(mServiceLock); |
| 4997 | |
| 4998 | mCameraServiceWatchdogEnabled = enableWatchdog; |
| 4999 | |
| 5000 | const auto clients = mActiveClientManager.getAll(); |
| 5001 | for (auto& current : clients) { |
| 5002 | if (current != nullptr) { |
| 5003 | const auto basicClient = current->getValue(); |
| 5004 | if (basicClient.get() != nullptr) { |
| 5005 | basicClient->setCameraServiceWatchdog(enableWatchdog); |
| 5006 | } |
| 5007 | } |
| 5008 | } |
| 5009 | |
| 5010 | return OK; |
| 5011 | } |
| 5012 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5013 | status_t CameraService::handleGetRotateAndCrop(int out) { |
| 5014 | Mutex::Autolock lock(mServiceLock); |
| 5015 | |
| 5016 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); |
| 5017 | } |
| 5018 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5019 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { |
| 5020 | char *endPtr; |
| 5021 | errno = 0; |
| 5022 | String8 maskString8 = String8(args[1]); |
| 5023 | long maskValue = strtol(maskString8.c_str(), &endPtr, 10); |
| 5024 | |
| 5025 | if (errno != 0) return BAD_VALUE; |
| 5026 | if (endPtr != maskString8.c_str() + maskString8.size()) return BAD_VALUE; |
| 5027 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; |
| 5028 | |
| 5029 | Mutex::Autolock lock(mServiceLock); |
| 5030 | |
| 5031 | mImageDumpMask = maskValue; |
| 5032 | |
| 5033 | return OK; |
| 5034 | } |
| 5035 | |
| 5036 | status_t CameraService::handleGetImageDumpMask(int out) { |
| 5037 | Mutex::Autolock lock(mServiceLock); |
| 5038 | |
| 5039 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); |
| 5040 | } |
| 5041 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 5042 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { |
| 5043 | int muteValue = strtol(String8(args[1]), nullptr, 10); |
| 5044 | if (errno != 0) return BAD_VALUE; |
| 5045 | |
| 5046 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; |
| 5047 | Mutex::Autolock lock(mServiceLock); |
| 5048 | |
| 5049 | mOverrideCameraMuteMode = (muteValue == 1); |
| 5050 | |
| 5051 | const auto clients = mActiveClientManager.getAll(); |
| 5052 | for (auto& current : clients) { |
| 5053 | if (current != nullptr) { |
| 5054 | const auto basicClient = current->getValue(); |
| 5055 | if (basicClient.get() != nullptr) { |
| 5056 | if (basicClient->supportsCameraMute()) { |
| 5057 | basicClient->setCameraMute(mOverrideCameraMuteMode); |
| 5058 | } |
| 5059 | } |
| 5060 | } |
| 5061 | } |
| 5062 | |
| 5063 | return OK; |
| 5064 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5065 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5066 | status_t CameraService::handleWatchCommand(const Vector<String16>& args, int inFd, int outFd) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5067 | if (args.size() >= 3 && args[1] == String16("start")) { |
| 5068 | return startWatchingTags(args, outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5069 | } else if (args.size() == 2 && args[1] == String16("stop")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5070 | return stopWatchingTags(outFd); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5071 | } else if (args.size() == 2 && args[1] == String16("dump")) { |
| 5072 | return printWatchedTags(outFd); |
| 5073 | } else if (args.size() >= 2 && args[1] == String16("live")) { |
| 5074 | return printWatchedTagsUntilInterrupt(args, inFd, outFd); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5075 | } else if (args.size() == 2 && args[1] == String16("clear")) { |
| 5076 | return clearCachedMonitoredTagDumps(outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5077 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5078 | dprintf(outFd, "Camera service watch commands:\n" |
| 5079 | " start -m <comma_separated_tag_list> [-c <comma_separated_client_list>]\n" |
| 5080 | " starts watching the provided tags for clients with provided package\n" |
| 5081 | " recognizes tag shorthands like '3a'\n" |
| 5082 | " 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] | 5083 | " dump dumps the monitoring information and exits\n" |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5084 | " stop stops watching all tags\n" |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5085 | " live [-n <refresh_interval_ms>]\n" |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5086 | " prints the monitored information in real time\n" |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5087 | " Hit return to exit\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5088 | " clear clears all buffers storing information for watch command"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5089 | return BAD_VALUE; |
| 5090 | } |
| 5091 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5092 | status_t CameraService::startWatchingTags(const Vector<String16> &args, int outFd) { |
| 5093 | Mutex::Autolock lock(mLogLock); |
| 5094 | size_t tagsIdx; // index of '-m' |
| 5095 | String16 tags(""); |
| 5096 | for (tagsIdx = 2; tagsIdx < args.size() && args[tagsIdx] != String16("-m"); tagsIdx++); |
| 5097 | if (tagsIdx < args.size() - 1) { |
| 5098 | tags = args[tagsIdx + 1]; |
| 5099 | } else { |
| 5100 | dprintf(outFd, "No tags provided.\n"); |
| 5101 | return BAD_VALUE; |
| 5102 | } |
| 5103 | |
| 5104 | size_t clientsIdx; // index of '-c' |
| 5105 | String16 clients = kWatchAllClientsFlag; // watch all clients if no clients are provided |
| 5106 | for (clientsIdx = 2; clientsIdx < args.size() && args[clientsIdx] != String16("-c"); |
| 5107 | clientsIdx++); |
| 5108 | if (clientsIdx < args.size() - 1) { |
| 5109 | clients = args[clientsIdx + 1]; |
| 5110 | } |
| 5111 | parseClientsToWatchLocked(String8(clients)); |
| 5112 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5113 | // track tags to initialize future clients with the monitoring information |
| 5114 | mMonitorTags = String8(tags); |
| 5115 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5116 | bool serviceLock = tryLock(mServiceLock); |
| 5117 | int numWatchedClients = 0; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5118 | auto cameraClients = mActiveClientManager.getAll(); |
| 5119 | for (const auto &clientDescriptor: cameraClients) { |
| 5120 | if (clientDescriptor == nullptr) { continue; } |
| 5121 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5122 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5123 | |
| 5124 | if (isClientWatchedLocked(client.get())) { |
| 5125 | client->startWatchingTags(mMonitorTags, outFd); |
| 5126 | numWatchedClients++; |
| 5127 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5128 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5129 | dprintf(outFd, "Started watching %d active clients\n", numWatchedClients); |
| 5130 | |
| 5131 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5132 | return OK; |
| 5133 | } |
| 5134 | |
| 5135 | status_t CameraService::stopWatchingTags(int outFd) { |
| 5136 | // clear mMonitorTags to prevent new clients from monitoring tags at initialization |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5137 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5138 | mMonitorTags = String8::empty(); |
| 5139 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5140 | mWatchedClientPackages.clear(); |
| 5141 | mWatchedClientsDumpCache.clear(); |
| 5142 | |
| 5143 | bool serviceLock = tryLock(mServiceLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5144 | auto cameraClients = mActiveClientManager.getAll(); |
| 5145 | for (const auto &clientDescriptor : cameraClients) { |
| 5146 | if (clientDescriptor == nullptr) { continue; } |
| 5147 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5148 | if (client.get() == nullptr) { continue; } |
| 5149 | client->stopWatchingTags(outFd); |
| 5150 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5151 | dprintf(outFd, "Stopped watching all clients.\n"); |
| 5152 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5153 | return OK; |
| 5154 | } |
| 5155 | |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5156 | status_t CameraService::clearCachedMonitoredTagDumps(int outFd) { |
| 5157 | Mutex::Autolock lock(mLogLock); |
| 5158 | size_t clearedSize = mWatchedClientsDumpCache.size(); |
| 5159 | mWatchedClientsDumpCache.clear(); |
| 5160 | dprintf(outFd, "Cleared tag information of %zu cached clients.\n", clearedSize); |
| 5161 | return OK; |
| 5162 | } |
| 5163 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5164 | status_t CameraService::printWatchedTags(int outFd) { |
| 5165 | Mutex::Autolock logLock(mLogLock); |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 5166 | std::set<String16> connectedMonitoredClients; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5167 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5168 | bool printedSomething = false; // tracks if any monitoring information was printed |
| 5169 | // (from either cached or active clients) |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5170 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5171 | bool serviceLock = tryLock(mServiceLock); |
| 5172 | // get all watched clients that are currently connected |
| 5173 | for (const auto &clientDescriptor: mActiveClientManager.getAll()) { |
| 5174 | if (clientDescriptor == nullptr) { continue; } |
| 5175 | |
| 5176 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5177 | if (client.get() == nullptr) { continue; } |
| 5178 | if (!isClientWatchedLocked(client.get())) { continue; } |
| 5179 | |
| 5180 | std::vector<std::string> dumpVector; |
| 5181 | client->dumpWatchedEventsToVector(dumpVector); |
| 5182 | |
| 5183 | size_t printIdx = dumpVector.size(); |
| 5184 | if (printIdx == 0) { |
| 5185 | continue; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5186 | } |
| 5187 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5188 | // Print tag dumps for active client |
| 5189 | const String8 &cameraId = clientDescriptor->getKey(); |
| 5190 | String8 packageName8 = String8(client->getPackageName()); |
| 5191 | const char *printablePackageName = packageName8.lockBuffer(packageName8.size()); |
| 5192 | dprintf(outFd, "Client: %s (active)\n", printablePackageName); |
| 5193 | while(printIdx > 0) { |
| 5194 | printIdx--; |
| 5195 | dprintf(outFd, "%s:%s %s", cameraId.string(), printablePackageName, |
| 5196 | dumpVector[printIdx].c_str()); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5197 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5198 | dprintf(outFd, "\n"); |
| 5199 | packageName8.unlockBuffer(); |
| 5200 | printedSomething = true; |
| 5201 | |
| 5202 | connectedMonitoredClients.emplace(client->getPackageName()); |
| 5203 | } |
| 5204 | if (serviceLock) { mServiceLock.unlock(); } |
| 5205 | |
| 5206 | // Print entries in mWatchedClientsDumpCache for clients that are not connected |
| 5207 | for (const auto &kv: mWatchedClientsDumpCache) { |
| 5208 | const String16 &package = kv.first; |
| 5209 | if (connectedMonitoredClients.find(package) != connectedMonitoredClients.end()) { |
| 5210 | continue; |
| 5211 | } |
| 5212 | |
| 5213 | dprintf(outFd, "Client: %s (cached)\n", String8(package).string()); |
| 5214 | dprintf(outFd, "%s\n", kv.second.c_str()); |
| 5215 | printedSomething = true; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5216 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5217 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5218 | if (!printedSomething) { |
| 5219 | dprintf(outFd, "No monitoring information to print.\n"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5220 | } |
| 5221 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5222 | return OK; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5223 | } |
| 5224 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5225 | // Print all events in vector `events' that came after lastPrintedEvent |
| 5226 | void printNewWatchedEvents(int outFd, |
| 5227 | const char *cameraId, |
| 5228 | const String16 &packageName, |
| 5229 | const std::vector<std::string> &events, |
| 5230 | const std::string &lastPrintedEvent) { |
| 5231 | if (events.empty()) { return; } |
| 5232 | |
| 5233 | // index of lastPrintedEvent in events. |
| 5234 | // lastPrintedIdx = events.size() if lastPrintedEvent is not in events |
| 5235 | size_t lastPrintedIdx; |
| 5236 | for (lastPrintedIdx = 0; |
| 5237 | lastPrintedIdx < events.size() && lastPrintedEvent != events[lastPrintedIdx]; |
| 5238 | lastPrintedIdx++); |
| 5239 | |
| 5240 | if (lastPrintedIdx == 0) { return; } // early exit if no new event in `events` |
| 5241 | |
| 5242 | String8 packageName8(packageName); |
| 5243 | const char *printablePackageName = packageName8.lockBuffer(packageName8.size()); |
| 5244 | |
| 5245 | // print events in chronological order (latest event last) |
| 5246 | size_t idxToPrint = lastPrintedIdx; |
| 5247 | do { |
| 5248 | idxToPrint--; |
| 5249 | dprintf(outFd, "%s:%s %s", cameraId, printablePackageName, events[idxToPrint].c_str()); |
| 5250 | } while (idxToPrint != 0); |
| 5251 | |
| 5252 | packageName8.unlockBuffer(); |
| 5253 | } |
| 5254 | |
| 5255 | // Returns true if adb shell cmd watch should be interrupted based on data in inFd. The watch |
| 5256 | // command should be interrupted if the user presses the return key, or if user loses any way to |
| 5257 | // signal interrupt. |
| 5258 | // If timeoutMs == 0, this function will always return false |
| 5259 | bool shouldInterruptWatchCommand(int inFd, int outFd, long timeoutMs) { |
| 5260 | struct timeval startTime; |
| 5261 | int startTimeError = gettimeofday(&startTime, nullptr); |
| 5262 | if (startTimeError) { |
| 5263 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 5264 | return true; |
| 5265 | } |
| 5266 | |
| 5267 | const nfds_t numFds = 1; |
| 5268 | struct pollfd pollFd = { .fd = inFd, .events = POLLIN, .revents = 0 }; |
| 5269 | |
| 5270 | struct timeval currTime; |
| 5271 | char buffer[2]; |
| 5272 | while(true) { |
| 5273 | int currTimeError = gettimeofday(&currTime, nullptr); |
| 5274 | if (currTimeError) { |
| 5275 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 5276 | return true; |
| 5277 | } |
| 5278 | |
| 5279 | long elapsedTimeMs = ((currTime.tv_sec - startTime.tv_sec) * 1000L) |
| 5280 | + ((currTime.tv_usec - startTime.tv_usec) / 1000L); |
| 5281 | int remainingTimeMs = (int) (timeoutMs - elapsedTimeMs); |
| 5282 | |
| 5283 | if (remainingTimeMs <= 0) { |
| 5284 | // No user interrupt within timeoutMs, don't interrupt watch command |
| 5285 | return false; |
| 5286 | } |
| 5287 | |
| 5288 | int numFdsUpdated = poll(&pollFd, numFds, remainingTimeMs); |
| 5289 | if (numFdsUpdated < 0) { |
| 5290 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 5291 | return true; |
| 5292 | } |
| 5293 | |
| 5294 | if (numFdsUpdated == 0) { |
| 5295 | // No user input within timeoutMs, don't interrupt watch command |
| 5296 | return false; |
| 5297 | } |
| 5298 | |
| 5299 | if (!(pollFd.revents & POLLIN)) { |
| 5300 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 5301 | return true; |
| 5302 | } |
| 5303 | |
| 5304 | ssize_t sizeRead = read(inFd, buffer, sizeof(buffer) - 1); |
| 5305 | if (sizeRead < 0) { |
| 5306 | dprintf(outFd, "Error reading user input. Exiting.\n"); |
| 5307 | return true; |
| 5308 | } |
| 5309 | |
| 5310 | if (sizeRead == 0) { |
| 5311 | // Reached end of input fd (can happen if input is piped) |
| 5312 | // User has no way to signal an interrupt, so interrupt here |
| 5313 | return true; |
| 5314 | } |
| 5315 | |
| 5316 | if (buffer[0] == '\n') { |
| 5317 | // User pressed return, interrupt watch command. |
| 5318 | return true; |
| 5319 | } |
| 5320 | } |
| 5321 | } |
| 5322 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5323 | status_t CameraService::printWatchedTagsUntilInterrupt(const Vector<String16> &args, |
| 5324 | int inFd, int outFd) { |
| 5325 | // Figure out refresh interval, if present in args |
| 5326 | long refreshTimeoutMs = 1000L; // refresh every 1s by default |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5327 | if (args.size() > 2) { |
| 5328 | size_t intervalIdx; // index of '-n' |
| 5329 | for (intervalIdx = 2; intervalIdx < args.size() && String16("-n") != args[intervalIdx]; |
| 5330 | intervalIdx++); |
| 5331 | |
| 5332 | size_t intervalValIdx = intervalIdx + 1; |
| 5333 | if (intervalValIdx < args.size()) { |
| 5334 | refreshTimeoutMs = strtol(String8(args[intervalValIdx].string()), nullptr, 10); |
| 5335 | if (errno) { return BAD_VALUE; } |
| 5336 | } |
| 5337 | } |
| 5338 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5339 | // Set min timeout of 10ms. This prevents edge cases in polling when timeout of 0 is passed. |
| 5340 | refreshTimeoutMs = refreshTimeoutMs < 10 ? 10 : refreshTimeoutMs; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5341 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5342 | dprintf(outFd, "Press return to exit...\n\n"); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5343 | std::map<String16, std::string> packageNameToLastEvent; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5344 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5345 | while (true) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5346 | bool serviceLock = tryLock(mServiceLock); |
| 5347 | auto cameraClients = mActiveClientManager.getAll(); |
| 5348 | if (serviceLock) { mServiceLock.unlock(); } |
| 5349 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5350 | for (const auto& clientDescriptor : cameraClients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5351 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5352 | if (clientDescriptor == nullptr) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5353 | |
| 5354 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5355 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5356 | if (!isClientWatchedLocked(client.get())) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5357 | |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5358 | const String16 &packageName = client->getPackageName(); |
| 5359 | // This also initializes the map entries with an empty string |
| 5360 | const std::string& lastPrintedEvent = packageNameToLastEvent[packageName]; |
| 5361 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5362 | std::vector<std::string> latestEvents; |
| 5363 | client->dumpWatchedEventsToVector(latestEvents); |
| 5364 | |
| 5365 | if (!latestEvents.empty()) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5366 | String8 cameraId = clientDescriptor->getKey(); |
| 5367 | const char *printableCameraId = cameraId.lockBuffer(cameraId.size()); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5368 | printNewWatchedEvents(outFd, |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5369 | printableCameraId, |
| 5370 | packageName, |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5371 | latestEvents, |
| 5372 | lastPrintedEvent); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5373 | packageNameToLastEvent[packageName] = latestEvents[0]; |
| 5374 | cameraId.unlockBuffer(); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5375 | } |
| 5376 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5377 | if (shouldInterruptWatchCommand(inFd, outFd, refreshTimeoutMs)) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5378 | break; |
| 5379 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5380 | } |
| 5381 | return OK; |
| 5382 | } |
| 5383 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5384 | void CameraService::parseClientsToWatchLocked(String8 clients) { |
| 5385 | mWatchedClientPackages.clear(); |
| 5386 | |
| 5387 | const char *allSentinel = String8(kWatchAllClientsFlag).string(); |
| 5388 | |
| 5389 | char *tokenized = clients.lockBuffer(clients.size()); |
| 5390 | char *savePtr; |
| 5391 | char *nextClient = strtok_r(tokenized, ",", &savePtr); |
| 5392 | |
| 5393 | while (nextClient != nullptr) { |
| 5394 | if (strcmp(nextClient, allSentinel) == 0) { |
| 5395 | // Don't need to track any other package if 'all' is present |
| 5396 | mWatchedClientPackages.clear(); |
| 5397 | mWatchedClientPackages.emplace(kWatchAllClientsFlag); |
| 5398 | break; |
| 5399 | } |
| 5400 | |
| 5401 | // track package names |
| 5402 | mWatchedClientPackages.emplace(nextClient); |
| 5403 | nextClient = strtok_r(nullptr, ",", &savePtr); |
| 5404 | } |
| 5405 | clients.unlockBuffer(); |
| 5406 | } |
| 5407 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5408 | status_t CameraService::printHelp(int out) { |
| 5409 | return dprintf(out, "Camera service commands:\n" |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5410 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 5411 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 5412 | " 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] | 5413 | " set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" |
| 5414 | " Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" |
| 5415 | " get-rotate-and-crop returns the current override rotate-and-crop value\n" |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5416 | " set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" |
| 5417 | " Valid values 0=OFF, 1=ON for JPEG\n" |
| 5418 | " 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] | 5419 | " set-camera-mute <0/1> enable or disable camera muting\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5420 | " 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] | 5421 | " help print this message\n"); |
| 5422 | } |
| 5423 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5424 | bool CameraService::isClientWatched(const BasicClient *client) { |
| 5425 | Mutex::Autolock lock(mLogLock); |
| 5426 | return isClientWatchedLocked(client); |
| 5427 | } |
| 5428 | |
| 5429 | bool CameraService::isClientWatchedLocked(const BasicClient *client) { |
| 5430 | return mWatchedClientPackages.find(kWatchAllClientsFlag) != mWatchedClientPackages.end() || |
| 5431 | mWatchedClientPackages.find(client->getPackageName()) != mWatchedClientPackages.end(); |
| 5432 | } |
| 5433 | |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 5434 | int32_t CameraService::updateAudioRestriction() { |
| 5435 | Mutex::Autolock lock(mServiceLock); |
| 5436 | return updateAudioRestrictionLocked(); |
| 5437 | } |
| 5438 | |
| 5439 | int32_t CameraService::updateAudioRestrictionLocked() { |
| 5440 | int32_t mode = 0; |
| 5441 | // iterate through all active client |
| 5442 | for (const auto& i : mActiveClientManager.getAll()) { |
| 5443 | const auto clientSp = i->getValue(); |
| 5444 | mode |= clientSp->getAudioRestriction(); |
| 5445 | } |
| 5446 | |
| 5447 | bool modeChanged = (mAudioRestriction != mode); |
| 5448 | mAudioRestriction = mode; |
| 5449 | if (modeChanged) { |
| 5450 | mAppOps.setCameraAudioRestriction(mode); |
| 5451 | } |
| 5452 | return mode; |
| 5453 | } |
| 5454 | |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 5455 | status_t CameraService::checkIfInjectionCameraIsPresent(const String8& externalCamId, |
| 5456 | sp<BasicClient> clientSp) { |
| 5457 | std::unique_ptr<AutoConditionLock> lock = |
| 5458 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 5459 | status_t res = NO_ERROR; |
| 5460 | if ((res = checkIfDeviceIsUsable(externalCamId)) != NO_ERROR) { |
| 5461 | ALOGW("Device %s is not usable!", externalCamId.string()); |
| 5462 | mInjectionStatusListener->notifyInjectionError( |
| 5463 | externalCamId, UNKNOWN_TRANSACTION); |
| 5464 | clientSp->notifyError( |
| 5465 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 5466 | CaptureResultExtras()); |
| 5467 | |
| 5468 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 5469 | // other clients from connecting in mServiceLockWrapper if held |
| 5470 | mServiceLock.unlock(); |
| 5471 | |
| 5472 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
| 5473 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 5474 | clientSp->disconnect(); |
| 5475 | CameraThreadState::restoreCallingIdentity(token); |
| 5476 | |
| 5477 | // Reacquire mServiceLock |
| 5478 | mServiceLock.lock(); |
| 5479 | } |
| 5480 | |
| 5481 | return res; |
| 5482 | } |
| 5483 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5484 | void CameraService::clearInjectionParameters() { |
| 5485 | { |
| 5486 | Mutex::Autolock lock(mInjectionParametersLock); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 5487 | mInjectionInitPending = false; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5488 | mInjectionInternalCamId = ""; |
| 5489 | } |
| 5490 | mInjectionExternalCamId = ""; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5491 | mInjectionStatusListener->removeListener(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5492 | } |
| 5493 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5494 | }; // namespace android |