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; |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 209 | std::unordered_map<std::string, std::set<std::string>> unavailPhysicalIds; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 210 | { |
| 211 | Mutex::Autolock l(mServiceLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 212 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 213 | if (nullptr == mCameraProviderManager.get()) { |
| 214 | mCameraProviderManager = new CameraProviderManager(); |
| 215 | res = mCameraProviderManager->initialize(this); |
| 216 | if (res != OK) { |
| 217 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", |
| 218 | __FUNCTION__, strerror(-res), res); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 219 | logServiceError(String8::format("Unable to initialize camera provider manager"), |
| 220 | ERROR_DISCONNECTED); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 221 | return res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 225 | |
| 226 | // Setup vendor tags before we call get_camera_info the first time |
| 227 | // because HAL might need to setup static vendor keys in get_camera_info |
| 228 | // TODO: maybe put this into CameraProviderManager::initialize()? |
| 229 | mCameraProviderManager->setUpVendorTags(); |
| 230 | |
| 231 | if (nullptr == mFlashlight.get()) { |
| 232 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 235 | res = mFlashlight->findFlashUnits(); |
| 236 | if (res != OK) { |
| 237 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); |
| 238 | } |
| 239 | |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 240 | deviceIds = mCameraProviderManager->getCameraDeviceIds(&unavailPhysicalIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | |
| 244 | for (auto& cameraId : deviceIds) { |
| 245 | String8 id8 = String8(cameraId.c_str()); |
Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 246 | if (getCameraState(id8) == nullptr) { |
| 247 | onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT); |
| 248 | } |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 249 | if (unavailPhysicalIds.count(cameraId) > 0) { |
| 250 | for (const auto& physicalId : unavailPhysicalIds[cameraId]) { |
| 251 | String8 physicalId8 = String8(physicalId.c_str()); |
| 252 | onDeviceStatusChanged(id8, physicalId8, CameraDeviceStatus::NOT_PRESENT); |
| 253 | } |
| 254 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 257 | // Derive primary rear/front cameras, and filter their charactierstics. |
| 258 | // 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] | 259 | if (SessionConfigurationUtils::IS_PERF_CLASS) { |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 260 | // Assume internal cameras are advertised from the same |
| 261 | // provider. If multiple providers are registered at different time, |
| 262 | // and each provider contains multiple internal color cameras, the current |
| 263 | // logic may filter the characteristics of more than one front/rear color |
| 264 | // cameras. |
| 265 | Mutex::Autolock l(mServiceLock); |
| 266 | filterSPerfClassCharacteristicsLocked(); |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 267 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 268 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 269 | return OK; |
| 270 | } |
| 271 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 272 | void CameraService::broadcastTorchModeStatus(const String8& cameraId, TorchModeStatus status, |
| 273 | SystemCameraKind systemCameraKind) { |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 274 | Mutex::Autolock lock(mStatusListenerLock); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 275 | for (auto& i : mListenerList) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 276 | if (shouldSkipStatusUpdates(systemCameraKind, i->isVendorListener(), i->getListenerPid(), |
| 277 | i->getListenerUid())) { |
| 278 | ALOGV("Skipping torch callback for system-only camera device %s", |
| 279 | cameraId.c_str()); |
| 280 | continue; |
| 281 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 282 | auto ret = i->getListener()->onTorchStatusChanged(mapToInterface(status), |
| 283 | String16{cameraId}); |
| 284 | i->handleBinderStatus(ret, "%s: Failed to trigger onTorchStatusChanged for %d:%d: %d", |
| 285 | __FUNCTION__, i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 289 | CameraService::~CameraService() { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 290 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 291 | mUidPolicy->unregisterSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 292 | mSensorPrivacyPolicy->unregisterSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 293 | mInjectionStatusListener->removeListener(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 296 | void CameraService::onNewProviderRegistered() { |
| 297 | enumerateProviders(); |
| 298 | } |
| 299 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 300 | void CameraService::filterAPI1SystemCameraLocked( |
| 301 | const std::vector<std::string> &normalDeviceIds) { |
| 302 | mNormalDeviceIdsWithoutSystemCamera.clear(); |
| 303 | for (auto &deviceId : normalDeviceIds) { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 304 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 305 | if (getSystemCameraKind(String8(deviceId.c_str()), &deviceKind) != OK) { |
| 306 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, deviceId.c_str()); |
| 307 | continue; |
| 308 | } |
| 309 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 310 | // All system camera ids will necessarily come after public camera |
| 311 | // device ids as per the HAL interface contract. |
| 312 | break; |
| 313 | } |
| 314 | mNormalDeviceIdsWithoutSystemCamera.push_back(deviceId); |
| 315 | } |
| 316 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, |
| 317 | mNormalDeviceIdsWithoutSystemCamera.size()); |
| 318 | } |
| 319 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 320 | status_t CameraService::getSystemCameraKind(const String8& cameraId, SystemCameraKind *kind) const { |
| 321 | auto state = getCameraState(cameraId); |
| 322 | if (state != nullptr) { |
| 323 | *kind = state->getSystemCameraKind(); |
| 324 | return OK; |
| 325 | } |
| 326 | // Hidden physical camera ids won't have CameraState |
| 327 | return mCameraProviderManager->getSystemCameraKind(cameraId.c_str(), kind); |
| 328 | } |
| 329 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 330 | void CameraService::updateCameraNumAndIds() { |
| 331 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 332 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); |
| 333 | // Excludes hidden secure cameras |
| 334 | mNumberOfCameras = |
| 335 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; |
| 336 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 337 | mNormalDeviceIds = |
| 338 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 339 | filterAPI1SystemCameraLocked(mNormalDeviceIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 342 | void CameraService::filterSPerfClassCharacteristicsLocked() { |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 343 | // To claim to be S Performance primary cameras, the cameras must be |
| 344 | // backward compatible. So performance class primary camera Ids must be API1 |
| 345 | // compatible. |
| 346 | bool firstRearCameraSeen = false, firstFrontCameraSeen = false; |
| 347 | for (const auto& cameraId : mNormalDeviceIdsWithoutSystemCamera) { |
| 348 | int facing = -1; |
| 349 | int orientation = 0; |
| 350 | String8 cameraId8(cameraId.c_str()); |
| 351 | getDeviceVersion(cameraId8, /*out*/&facing, /*out*/&orientation); |
| 352 | if (facing == -1) { |
| 353 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.c_str()); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | if ((facing == hardware::CAMERA_FACING_BACK && !firstRearCameraSeen) || |
| 358 | (facing == hardware::CAMERA_FACING_FRONT && !firstFrontCameraSeen)) { |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 359 | status_t res = mCameraProviderManager->filterSmallJpegSizes(cameraId); |
| 360 | if (res == OK) { |
| 361 | mPerfClassPrimaryCameraIds.insert(cameraId); |
| 362 | } else { |
| 363 | ALOGE("%s: Failed to filter small JPEG sizes for performance class primary " |
| 364 | "camera %s: %s(%d)", __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
| 365 | break; |
| 366 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 367 | |
| 368 | if (facing == hardware::CAMERA_FACING_BACK) { |
| 369 | firstRearCameraSeen = true; |
| 370 | } |
| 371 | if (facing == hardware::CAMERA_FACING_FRONT) { |
| 372 | firstFrontCameraSeen = true; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if (firstRearCameraSeen && firstFrontCameraSeen) { |
| 377 | break; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 382 | void CameraService::addStates(const String8 id) { |
| 383 | std::string cameraId(id.c_str()); |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 384 | CameraResourceCost cost; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 385 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); |
| 386 | if (res != OK) { |
| 387 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); |
| 388 | return; |
| 389 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 390 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 391 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); |
| 392 | if (res != OK) { |
| 393 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); |
| 394 | return; |
| 395 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 396 | std::vector<std::string> physicalCameraIds; |
| 397 | mCameraProviderManager->isLogicalCamera(cameraId, &physicalCameraIds); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 398 | std::set<String8> conflicting; |
| 399 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { |
| 400 | conflicting.emplace(String8(cost.conflictingDevices[i].c_str())); |
| 401 | } |
| 402 | |
| 403 | { |
| 404 | Mutex::Autolock lock(mCameraStatesLock); |
| 405 | mCameraStates.emplace(id, std::make_shared<CameraState>(id, cost.resourceCost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 406 | conflicting, deviceKind, physicalCameraIds)); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 410 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 411 | mTorchStatusMap.add(id, TorchModeStatus::AVAILABLE_OFF); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 412 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 413 | broadcastTorchModeStatus(id, TorchModeStatus::AVAILABLE_OFF, deviceKind); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 414 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 415 | |
| 416 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 417 | logDeviceAdded(id, "Device added"); |
| 418 | } |
| 419 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 420 | void CameraService::removeStates(const String8 id) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 421 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 422 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 423 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 424 | mTorchStatusMap.removeItem(id); |
| 425 | } |
| 426 | |
| 427 | { |
| 428 | Mutex::Autolock lock(mCameraStatesLock); |
| 429 | mCameraStates.erase(id); |
| 430 | } |
| 431 | } |
| 432 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 433 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 434 | CameraDeviceStatus newHalStatus) { |
| 435 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, |
| 436 | id.string(), newHalStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 437 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 438 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 439 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 440 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 441 | |
| 442 | if (state == nullptr) { |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 443 | if (newStatus == StatusInternal::PRESENT) { |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 444 | ALOGI("%s: Unknown camera ID %s, a new camera is added", |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 445 | __FUNCTION__, id.string()); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 446 | |
| 447 | // First add as absent to make sure clients are notified below |
| 448 | addStates(id); |
| 449 | |
| 450 | updateStatus(newStatus, id); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 451 | } else { |
| 452 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string()); |
| 453 | } |
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 | StatusInternal oldStatus = state->getStatus(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 458 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 459 | if (oldStatus == newStatus) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 460 | 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] | 461 | return; |
| 462 | } |
| 463 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 464 | if (newStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 465 | logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 466 | newStatus)); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 467 | |
| 468 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 469 | // to this device until the status changes |
| 470 | updateStatus(StatusInternal::NOT_PRESENT, id); |
| 471 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 472 | sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 473 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 474 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 475 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 476 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 477 | // Remove cached shim parameters |
| 478 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 479 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 480 | // Remove online as well as offline client from the list of active clients, |
| 481 | // if they are present |
| 482 | clientToDisconnectOnline = removeClientLocked(id); |
| 483 | clientToDisconnectOffline = removeClientLocked(kOfflineDevice + id); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 484 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 485 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 486 | disconnectClient(id, clientToDisconnectOnline); |
| 487 | disconnectClient(kOfflineDevice + id, clientToDisconnectOffline); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 488 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 489 | removeStates(id); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 490 | } else { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 491 | if (oldStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 492 | logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 493 | newStatus)); |
| 494 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 495 | updateStatus(newStatus, id); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 496 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 497 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 498 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 499 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 500 | const String8& physicalId, |
| 501 | CameraDeviceStatus newHalStatus) { |
| 502 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", |
| 503 | __FUNCTION__, id.string(), physicalId.string(), newHalStatus); |
| 504 | |
| 505 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 506 | |
| 507 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 508 | |
| 509 | if (state == nullptr) { |
| 510 | ALOGE("%s: Physical camera id %s status change on a non-present ID %s", |
Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 511 | __FUNCTION__, physicalId.string(), id.string()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 512 | return; |
| 513 | } |
| 514 | |
| 515 | StatusInternal logicalCameraStatus = state->getStatus(); |
| 516 | if (logicalCameraStatus != StatusInternal::PRESENT && |
| 517 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 518 | ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d", |
| 519 | __FUNCTION__, physicalId.string(), newHalStatus, logicalCameraStatus); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | bool updated = false; |
| 524 | if (newStatus == StatusInternal::PRESENT) { |
| 525 | updated = state->removeUnavailablePhysicalId(physicalId); |
| 526 | } else { |
| 527 | updated = state->addUnavailablePhysicalId(physicalId); |
| 528 | } |
| 529 | |
| 530 | if (updated) { |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 531 | String8 idCombo = id + " : " + physicalId; |
| 532 | if (newStatus == StatusInternal::PRESENT) { |
| 533 | logDeviceAdded(idCombo, |
| 534 | String8::format("Device status changed to %d", newStatus)); |
| 535 | } else { |
| 536 | logDeviceRemoved(idCombo, |
| 537 | String8::format("Device status changed to %d", newStatus)); |
| 538 | } |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 539 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 540 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 541 | if (getSystemCameraKind(id, &deviceKind) != OK) { |
| 542 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.string()); |
| 543 | return; |
| 544 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 545 | String16 id16(id), physicalId16(physicalId); |
| 546 | Mutex::Autolock lock(mStatusListenerLock); |
| 547 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 548 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 549 | listener->getListenerPid(), listener->getListenerUid())) { |
| 550 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 551 | id.c_str()); |
| 552 | continue; |
| 553 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 554 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged( |
| 555 | mapToInterface(newStatus), id16, physicalId16); |
| 556 | listener->handleBinderStatus(ret, |
| 557 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 558 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 559 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 560 | } |
| 561 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 564 | void CameraService::disconnectClient(const String8& id, sp<BasicClient> clientToDisconnect) { |
| 565 | if (clientToDisconnect.get() != nullptr) { |
| 566 | ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL", |
| 567 | __FUNCTION__, id.string()); |
| 568 | // Notify the client of disconnection |
| 569 | clientToDisconnect->notifyError( |
| 570 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 571 | CaptureResultExtras{}); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 572 | clientToDisconnect->disconnect(); |
| 573 | } |
| 574 | } |
| 575 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 576 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 577 | TorchModeStatus newStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 578 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 579 | status_t res = getSystemCameraKind(cameraId, &systemCameraKind); |
| 580 | if (res != OK) { |
| 581 | ALOGE("%s: Could not get system camera kind for camera id %s", __FUNCTION__, |
| 582 | cameraId.string()); |
| 583 | return; |
| 584 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 585 | Mutex::Autolock al(mTorchStatusMutex); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 586 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Jayant Chowdhary | 46ef0f5 | 2021-10-05 14:36:13 -0700 | [diff] [blame] | 589 | |
| 590 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
| 591 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
| 592 | Mutex::Autolock al(mTorchStatusMutex); |
| 593 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
| 594 | } |
| 595 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 596 | void CameraService::broadcastTorchStrengthLevel(const String8& cameraId, |
| 597 | int32_t newStrengthLevel) { |
| 598 | Mutex::Autolock lock(mStatusListenerLock); |
| 599 | for (auto& i : mListenerList) { |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 600 | auto ret = i->getListener()->onTorchStrengthLevelChanged(String16{cameraId}, |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 601 | newStrengthLevel); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 602 | i->handleBinderStatus(ret, |
| 603 | "%s: Failed to trigger onTorchStrengthLevelChanged for %d:%d: %d", __FUNCTION__, |
| 604 | i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 608 | void CameraService::onTorchStatusChangedLocked(const String8& cameraId, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 609 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 610 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
| 611 | __FUNCTION__, cameraId.string(), newStatus); |
| 612 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 613 | TorchModeStatus status; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 614 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 615 | if (res) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 616 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", |
| 617 | __FUNCTION__, cameraId.string(), strerror(-res), res); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 618 | return; |
| 619 | } |
| 620 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 621 | return; |
| 622 | } |
| 623 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 624 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 625 | if (res) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 626 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, |
| 627 | (uint32_t)newStatus, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 631 | { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 632 | // Update battery life logging for flashlight |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 633 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 634 | auto iter = mTorchUidMap.find(cameraId); |
| 635 | if (iter != mTorchUidMap.end()) { |
| 636 | int oldUid = iter->second.second; |
| 637 | int newUid = iter->second.first; |
| 638 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 639 | if (oldUid != newUid) { |
| 640 | // 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] | 641 | if (status == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 642 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 643 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 644 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 645 | notifier.noteFlashlightOn(cameraId, newUid); |
| 646 | } |
| 647 | iter->second.second = newUid; |
| 648 | } else { |
| 649 | // If the UID has not changed, log the status |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 650 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 651 | notifier.noteFlashlightOn(cameraId, oldUid); |
| 652 | } else { |
| 653 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 658 | broadcastTorchModeStatus(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 661 | static bool hasPermissionsForSystemCamera(int callingPid, int callingUid, |
| 662 | bool logPermissionFailure = false) { |
| 663 | return checkPermission(sSystemCameraPermission, callingPid, callingUid, |
| 664 | logPermissionFailure) && |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 665 | checkPermission(sCameraPermission, callingPid, callingUid); |
| 666 | } |
| 667 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 668 | Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 669 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 670 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 671 | bool hasSystemCameraPermissions = |
| 672 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), |
| 673 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 674 | switch (type) { |
| 675 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 676 | if (hasSystemCameraPermissions) { |
| 677 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); |
| 678 | } else { |
| 679 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); |
| 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 | case CAMERA_TYPE_ALL: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 683 | if (hasSystemCameraPermissions) { |
| 684 | *numCameras = mNumberOfCameras; |
| 685 | } else { |
| 686 | *numCameras = mNumberOfCamerasWithoutSystemCamera; |
| 687 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 688 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 689 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 690 | ALOGW("%s: Unknown camera type %d", |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 691 | __FUNCTION__, type); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 692 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 693 | "Unknown camera type %d", type); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 694 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 695 | return Status::ok(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 698 | Status CameraService::getCameraInfo(int cameraId, |
| 699 | CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 700 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 701 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 702 | std::string cameraIdStr = cameraIdIntToStrLocked(cameraId); |
| 703 | if (shouldRejectSystemCameraConnection(String8(cameraIdStr.c_str()))) { |
| 704 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 705 | "characteristics for system only device %s: ", cameraIdStr.c_str()); |
| 706 | } |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 707 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 708 | if (!mInitialized) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 709 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 710 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 711 | "Camera subsystem is not available"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 712 | } |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 713 | bool hasSystemCameraPermissions = |
| 714 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), |
| 715 | CameraThreadState::getCallingUid()); |
| 716 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; |
| 717 | if (hasSystemCameraPermissions) { |
| 718 | cameraIdBound = mNumberOfCameras; |
| 719 | } |
| 720 | if (cameraId < 0 || cameraId >= cameraIdBound) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 721 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 722 | "CameraId is not valid"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 725 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 726 | status_t err = mCameraProviderManager->getCameraInfo( |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 727 | cameraIdStr.c_str(), cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 728 | if (err != OK) { |
| 729 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 730 | "Error retrieving camera info from device %d: %s (%d)", cameraId, |
| 731 | strerror(-err), err); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 732 | logServiceError(String8::format("Error retrieving camera info from device %d",cameraId), |
| 733 | ERROR_INVALID_OPERATION); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 734 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 735 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 736 | return ret; |
| 737 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 738 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 739 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 740 | const std::vector<std::string> *deviceIds = &mNormalDeviceIdsWithoutSystemCamera; |
| 741 | auto callingPid = CameraThreadState::getCallingPid(); |
| 742 | auto callingUid = CameraThreadState::getCallingUid(); |
Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 743 | if (checkPermission(sSystemCameraPermission, callingPid, callingUid, |
| 744 | /*logPermissionFailure*/false) || getpid() == callingPid) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 745 | deviceIds = &mNormalDeviceIds; |
| 746 | } |
| 747 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(deviceIds->size())) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 748 | ALOGE("%s: input id %d invalid: valid range (0, %zu)", |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 749 | __FUNCTION__, cameraIdInt, deviceIds->size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 750 | return std::string{}; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 751 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 752 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 753 | return (*deviceIds)[cameraIdInt]; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | String8 CameraService::cameraIdIntToStr(int cameraIdInt) { |
| 757 | Mutex::Autolock lock(mServiceLock); |
| 758 | return String8(cameraIdIntToStrLocked(cameraIdInt).c_str()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | Status CameraService::getCameraCharacteristics(const String16& cameraId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 762 | int targetSdkVersion, CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 763 | ATRACE_CALL(); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 764 | if (!cameraInfo) { |
| 765 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 766 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 769 | if (!mInitialized) { |
| 770 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 771 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 772 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 773 | "Camera subsystem is not available");; |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 776 | if (shouldRejectSystemCameraConnection(String8(cameraId))) { |
| 777 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 778 | "characteristics for system only device %s: ", String8(cameraId).string()); |
| 779 | } |
| 780 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 781 | Status ret{}; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 782 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 783 | |
| 784 | std::string cameraIdStr = String8(cameraId).string(); |
| 785 | bool overrideForPerfClass = |
| 786 | SessionConfigurationUtils::targetPerfClassPrimaryCamera(mPerfClassPrimaryCameraIds, |
| 787 | cameraIdStr, targetSdkVersion); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 788 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 789 | cameraIdStr, overrideForPerfClass, cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 790 | if (res != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 791 | if (res == NAME_NOT_FOUND) { |
| 792 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " |
| 793 | "characteristics for unknown device %s: %s (%d)", String8(cameraId).string(), |
| 794 | strerror(-res), res); |
| 795 | } else { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 796 | logServiceError(String8::format("Unable to retrieve camera characteristics for " |
| 797 | "device %s.", String8(cameraId).string()),ERROR_INVALID_OPERATION); |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 798 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " |
| 799 | "characteristics for device %s: %s (%d)", String8(cameraId).string(), |
| 800 | strerror(-res), res); |
| 801 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 802 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 803 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 804 | if (getSystemCameraKind(String8(cameraId), &deviceKind) != OK) { |
| 805 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, String8(cameraId).string()); |
| 806 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera kind " |
| 807 | "for device %s", String8(cameraId).string()); |
| 808 | } |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 809 | int callingPid = CameraThreadState::getCallingPid(); |
| 810 | int callingUid = CameraThreadState::getCallingUid(); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 811 | std::vector<int32_t> tagsRemoved; |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 812 | // If it's not calling from cameraserver, check the permission only if |
| 813 | // android.permission.CAMERA is required. If android.permission.SYSTEM_CAMERA was needed, |
| 814 | // it would've already been checked in shouldRejectSystemCameraConnection. |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 815 | if ((callingPid != getpid()) && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 816 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 817 | !checkPermission(sCameraPermission, callingPid, callingUid)) { |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 818 | res = cameraInfo->removePermissionEntries( |
| 819 | mCameraProviderManager->getProviderTagIdLocked(String8(cameraId).string()), |
| 820 | &tagsRemoved); |
| 821 | if (res != OK) { |
| 822 | cameraInfo->clear(); |
| 823 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to remove camera" |
| 824 | " characteristics needing camera permission for device %s: %s (%d)", |
| 825 | String8(cameraId).string(), strerror(-res), res); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | if (!tagsRemoved.empty()) { |
| 830 | res = cameraInfo->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, |
| 831 | tagsRemoved.data(), tagsRemoved.size()); |
| 832 | if (res != OK) { |
| 833 | cameraInfo->clear(); |
| 834 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to insert camera " |
| 835 | "keys needing permission for device %s: %s (%d)", String8(cameraId).string(), |
| 836 | strerror(-res), res); |
| 837 | } |
| 838 | } |
| 839 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 840 | return ret; |
| 841 | } |
| 842 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 843 | Status CameraService::getTorchStrengthLevel(const String16& cameraId, |
| 844 | int32_t* torchStrength) { |
| 845 | ATRACE_CALL(); |
| 846 | Mutex::Autolock l(mServiceLock); |
| 847 | if (!mInitialized) { |
| 848 | ALOGE("%s: Camera HAL couldn't be initialized.", __FUNCTION__); |
| 849 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera HAL couldn't be initialized."); |
| 850 | } |
| 851 | |
| 852 | if(torchStrength == NULL) { |
| 853 | ALOGE("%s: strength level must not be null.", __FUNCTION__); |
| 854 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Strength level should not be null."); |
| 855 | } |
| 856 | |
| 857 | status_t res = mCameraProviderManager->getTorchStrengthLevel(String8(cameraId).string(), |
| 858 | torchStrength); |
| 859 | if (res != OK) { |
| 860 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve torch " |
| 861 | "strength level for device %s: %s (%d)", String8(cameraId).string(), |
| 862 | strerror(-res), res); |
| 863 | } |
| 864 | ALOGI("%s: Torch strength level is: %d", __FUNCTION__, *torchStrength); |
| 865 | return Status::ok(); |
| 866 | } |
| 867 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 868 | String8 CameraService::getFormattedCurrentTime() { |
| 869 | time_t now = time(nullptr); |
| 870 | char formattedTime[64]; |
| 871 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
| 872 | return String8(formattedTime); |
| 873 | } |
| 874 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 875 | Status CameraService::getCameraVendorTagDescriptor( |
| 876 | /*out*/ |
| 877 | hardware::camera2::params::VendorTagDescriptor* desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 878 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 879 | if (!mInitialized) { |
| 880 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 881 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 882 | } |
Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 883 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 884 | if (globalDescriptor != nullptr) { |
| 885 | *desc = *(globalDescriptor.get()); |
| 886 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 887 | return Status::ok(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 888 | } |
| 889 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 890 | Status CameraService::getCameraVendorTagCache( |
| 891 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { |
| 892 | ATRACE_CALL(); |
| 893 | if (!mInitialized) { |
| 894 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 895 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 896 | "Camera subsystem not available"); |
| 897 | } |
| 898 | sp<VendorTagDescriptorCache> globalCache = |
| 899 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 900 | if (globalCache != nullptr) { |
| 901 | *cache = *(globalCache.get()); |
| 902 | } |
| 903 | return Status::ok(); |
| 904 | } |
| 905 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 906 | void CameraService::clearCachedVariables() { |
| 907 | BasicClient::BasicClient::sCameraService = nullptr; |
| 908 | } |
| 909 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 910 | std::pair<int, IPCTransport> CameraService::getDeviceVersion(const String8& cameraId, int* facing, |
| 911 | int* orientation) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 912 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 913 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 914 | int deviceVersion = 0; |
| 915 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 916 | status_t res; |
| 917 | hardware::hidl_version maxVersion{0,0}; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 918 | IPCTransport transport = IPCTransport::INVALID; |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 919 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(), |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 920 | &maxVersion, &transport); |
| 921 | if (res != OK || transport == IPCTransport::INVALID) { |
| 922 | ALOGE("%s: Unable to get highest supported version for camera id %s", __FUNCTION__, |
| 923 | cameraId.string()); |
| 924 | return std::make_pair(-1, IPCTransport::INVALID) ; |
| 925 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 926 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 927 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 928 | hardware::CameraInfo info; |
| 929 | if (facing) { |
| 930 | res = mCameraProviderManager->getCameraInfo(cameraId.string(), &info); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 931 | if (res != OK) { |
| 932 | return std::make_pair(-1, IPCTransport::INVALID); |
| 933 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 934 | *facing = info.facing; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 935 | if (orientation) { |
| 936 | *orientation = info.orientation; |
| 937 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 938 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 939 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 940 | return std::make_pair(deviceVersion, transport); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 941 | } |
| 942 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 943 | Status CameraService::filterGetInfoErrorCode(status_t err) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 944 | switch(err) { |
| 945 | case NO_ERROR: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 946 | return Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 947 | case BAD_VALUE: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 948 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 949 | "CameraId is not valid for HAL module"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 950 | case NO_INIT: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 951 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 952 | "Camera device not available"); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 953 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 954 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 955 | "Camera HAL encountered error %d: %s", |
| 956 | err, strerror(-err)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 957 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 958 | } |
| 959 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 960 | Status CameraService::makeClient(const sp<CameraService>& cameraService, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 961 | const sp<IInterface>& cameraCb, const String16& packageName, bool systemNativeClient, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 962 | const std::optional<String16>& featureId, const String8& cameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 963 | int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid, |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 964 | int servicePid, std::pair<int, IPCTransport> deviceVersionAndTransport, |
| 965 | apiLevel effectiveApiLevel, bool overrideForPerfClass, /*out*/sp<BasicClient>* client) { |
| 966 | // For HIDL devices |
| 967 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 968 | // Create CameraClient based on device version reported by the HAL. |
| 969 | int deviceVersion = deviceVersionAndTransport.first; |
| 970 | switch(deviceVersion) { |
| 971 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 972 | ALOGE("Camera using old HAL version: %d", deviceVersion); |
| 973 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, |
| 974 | "Camera device \"%s\" HAL version %d no longer supported", |
| 975 | cameraId.string(), deviceVersion); |
| 976 | break; |
| 977 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 978 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 979 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 980 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 981 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 982 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 983 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 984 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 985 | break; |
| 986 | default: |
| 987 | // Should not be reachable |
| 988 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 989 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 990 | "Camera device \"%s\" has unknown HAL version %d", |
| 991 | cameraId.string(), deviceVersion); |
| 992 | } |
| 993 | } |
| 994 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 995 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 996 | *client = new Camera2Client(cameraService, tmp, cameraService->mCameraServiceProxyWrapper, |
| 997 | packageName, featureId, cameraId, api1CameraId, facing, sensorOrientation, |
| 998 | clientPid, clientUid, servicePid, overrideForPerfClass); |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 999 | } else { // Camera2 API route |
| 1000 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = |
| 1001 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1002 | *client = new CameraDeviceClient(cameraService, tmp, |
| 1003 | cameraService->mCameraServiceProxyWrapper, packageName, systemNativeClient, |
| 1004 | featureId, cameraId, facing, sensorOrientation, clientPid, clientUid, servicePid, |
| 1005 | overrideForPerfClass); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1006 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1007 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1010 | String8 CameraService::toString(std::set<userid_t> intSet) { |
| 1011 | String8 s(""); |
| 1012 | bool first = true; |
| 1013 | for (userid_t i : intSet) { |
| 1014 | if (first) { |
| 1015 | s.appendFormat("%d", i); |
| 1016 | first = false; |
| 1017 | } else { |
| 1018 | s.appendFormat(", %d", i); |
| 1019 | } |
| 1020 | } |
| 1021 | return s; |
| 1022 | } |
| 1023 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1024 | int32_t CameraService::mapToInterface(TorchModeStatus status) { |
| 1025 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1026 | switch (status) { |
| 1027 | case TorchModeStatus::NOT_AVAILABLE: |
| 1028 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 1029 | break; |
| 1030 | case TorchModeStatus::AVAILABLE_OFF: |
| 1031 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 1032 | break; |
| 1033 | case TorchModeStatus::AVAILABLE_ON: |
| 1034 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
| 1035 | break; |
| 1036 | default: |
| 1037 | ALOGW("Unknown new flash status: %d", status); |
| 1038 | } |
| 1039 | return serviceStatus; |
| 1040 | } |
| 1041 | |
| 1042 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { |
| 1043 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; |
| 1044 | switch (status) { |
| 1045 | case CameraDeviceStatus::NOT_PRESENT: |
| 1046 | serviceStatus = StatusInternal::NOT_PRESENT; |
| 1047 | break; |
| 1048 | case CameraDeviceStatus::PRESENT: |
| 1049 | serviceStatus = StatusInternal::PRESENT; |
| 1050 | break; |
| 1051 | case CameraDeviceStatus::ENUMERATING: |
| 1052 | serviceStatus = StatusInternal::ENUMERATING; |
| 1053 | break; |
| 1054 | default: |
| 1055 | ALOGW("Unknown new HAL device status: %d", status); |
| 1056 | } |
| 1057 | return serviceStatus; |
| 1058 | } |
| 1059 | |
| 1060 | int32_t CameraService::mapToInterface(StatusInternal status) { |
| 1061 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1062 | switch (status) { |
| 1063 | case StatusInternal::NOT_PRESENT: |
| 1064 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1065 | break; |
| 1066 | case StatusInternal::PRESENT: |
| 1067 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; |
| 1068 | break; |
| 1069 | case StatusInternal::ENUMERATING: |
| 1070 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; |
| 1071 | break; |
| 1072 | case StatusInternal::NOT_AVAILABLE: |
| 1073 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; |
| 1074 | break; |
| 1075 | case StatusInternal::UNKNOWN: |
| 1076 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; |
| 1077 | break; |
| 1078 | default: |
| 1079 | ALOGW("Unknown new internal device status: %d", status); |
| 1080 | } |
| 1081 | return serviceStatus; |
| 1082 | } |
| 1083 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1084 | Status CameraService::initializeShimMetadata(int cameraId) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1085 | int uid = CameraThreadState::getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1086 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1087 | String16 internalPackageName("cameraserver"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1088 | String8 id = String8::format("%d", cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1089 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1090 | sp<Client> tmp = nullptr; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1091 | if (!(ret = connectHelper<ICameraClient,Client>( |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1092 | sp<ICameraClient>{nullptr}, id, cameraId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1093 | internalPackageName, /*systemNativeClient*/ false, {}, uid, USE_CALLING_PID, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1094 | API_1, /*shimUpdateOnly*/ true, /*oomScoreOffset*/ 0, |
| 1095 | /*targetSdkVersion*/ __ANDROID_API_FUTURE__, /*out*/ tmp) |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1096 | ).isOk()) { |
| 1097 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string()); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1098 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1099 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1102 | Status CameraService::getLegacyParametersLazy(int cameraId, |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1103 | /*out*/ |
| 1104 | CameraParameters* parameters) { |
| 1105 | |
| 1106 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 1107 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1108 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1109 | |
| 1110 | if (parameters == NULL) { |
| 1111 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1112 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1115 | String8 id = String8::format("%d", cameraId); |
| 1116 | |
| 1117 | // Check if we already have parameters |
| 1118 | { |
| 1119 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1120 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1121 | auto cameraState = getCameraState(id); |
| 1122 | if (cameraState == nullptr) { |
| 1123 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1124 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1125 | "Invalid camera ID: %s", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1126 | } |
| 1127 | CameraParameters p = cameraState->getShimParams(); |
| 1128 | if (!p.isEmpty()) { |
| 1129 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1130 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1134 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1135 | ret = initializeShimMetadata(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1136 | CameraThreadState::restoreCallingIdentity(token); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1137 | if (!ret.isOk()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1138 | // Error already logged by callee |
| 1139 | return ret; |
| 1140 | } |
| 1141 | |
| 1142 | // Check for parameters again |
| 1143 | { |
| 1144 | // Scope for service lock |
| 1145 | Mutex::Autolock lock(mServiceLock); |
| 1146 | auto cameraState = getCameraState(id); |
| 1147 | if (cameraState == nullptr) { |
| 1148 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1149 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1150 | "Invalid camera ID: %s", id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1151 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1152 | CameraParameters p = cameraState->getShimParams(); |
| 1153 | if (!p.isEmpty()) { |
| 1154 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1155 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1156 | } |
| 1157 | } |
| 1158 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1159 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 1160 | __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1161 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1164 | // Can camera service trust the caller based on the calling UID? |
| 1165 | static bool isTrustedCallingUid(uid_t uid) { |
| 1166 | switch (uid) { |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1167 | case AID_MEDIA: // mediaserver |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1168 | case AID_CAMERASERVER: // cameraserver |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1169 | case AID_RADIO: // telephony |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1170 | return true; |
| 1171 | default: |
| 1172 | return false; |
| 1173 | } |
| 1174 | } |
| 1175 | |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 1176 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { |
| 1177 | PermissionController pc; |
| 1178 | uid = pc.getPackageUid(packageName, 0); |
| 1179 | if (uid <= 0) { |
| 1180 | ALOGE("Unknown package: '%s'", String8(packageName).string()); |
| 1181 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).string()); |
| 1182 | return BAD_VALUE; |
| 1183 | } |
| 1184 | |
| 1185 | if (userId < 0) { |
| 1186 | ALOGE("Invalid user: %d", userId); |
| 1187 | dprintf(err, "Invalid user: %d\n", userId); |
| 1188 | return BAD_VALUE; |
| 1189 | } |
| 1190 | |
| 1191 | uid = multiuser_get_uid(userId, uid); |
| 1192 | return NO_ERROR; |
| 1193 | } |
| 1194 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1195 | Status CameraService::validateConnectLocked(const String8& cameraId, |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1196 | const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, |
| 1197 | /*out*/int& originalClientPid) const { |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1198 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1199 | #ifdef __BRILLO__ |
| 1200 | UNUSED(clientName8); |
| 1201 | UNUSED(clientUid); |
| 1202 | UNUSED(clientPid); |
| 1203 | UNUSED(originalClientPid); |
| 1204 | #else |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1205 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, |
| 1206 | originalClientPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1207 | if (!allowed.isOk()) { |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1208 | return allowed; |
| 1209 | } |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1210 | #endif // __BRILLO__ |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1211 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1212 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1213 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1214 | if (!mInitialized) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1215 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 1216 | callingPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1217 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1218 | "No camera HAL module available to open camera device \"%s\"", cameraId.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1221 | if (getCameraState(cameraId) == nullptr) { |
| 1222 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1223 | cameraId.string()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1224 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1225 | "No camera device with ID \"%s\" available", cameraId.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1228 | status_t err = checkIfDeviceIsUsable(cameraId); |
| 1229 | if (err != NO_ERROR) { |
| 1230 | switch(err) { |
| 1231 | case -ENODEV: |
| 1232 | case -EBUSY: |
| 1233 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1234 | "No camera device with ID \"%s\" currently available", cameraId.string()); |
| 1235 | default: |
| 1236 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1237 | "Unknown error connecting to ID \"%s\"", cameraId.string()); |
| 1238 | } |
| 1239 | } |
| 1240 | return Status::ok(); |
Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1243 | Status CameraService::validateClientPermissionsLocked(const String8& cameraId, |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1244 | const String8& clientName8, int& clientUid, int& clientPid, |
| 1245 | /*out*/int& originalClientPid) const { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1246 | int callingPid = CameraThreadState::getCallingPid(); |
| 1247 | int callingUid = CameraThreadState::getCallingUid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1248 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1249 | // Check if we can trust clientUid |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1250 | if (clientUid == USE_CALLING_UID) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1251 | clientUid = callingUid; |
| 1252 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1253 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1254 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1255 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1256 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1257 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1258 | callingPid, callingUid, cameraId.string(), |
| 1259 | clientName8.string(), clientUid, clientPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1262 | // Check if we can trust clientPid |
| 1263 | if (clientPid == USE_CALLING_PID) { |
| 1264 | clientPid = callingPid; |
| 1265 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1266 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1267 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1268 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1269 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1270 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1271 | callingPid, callingUid, cameraId.string(), |
| 1272 | clientName8.string(), clientUid, clientPid); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1273 | } |
| 1274 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1275 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1276 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", |
| 1277 | cameraId.c_str()); |
| 1278 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, "No camera device with ID \"%s\" is" |
| 1279 | "available", cameraId.string()); |
| 1280 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1281 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1282 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 1283 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 1284 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "No camera device with ID \"%s\"" |
| 1285 | "found while trying to query device kind", cameraId.string()); |
| 1286 | |
| 1287 | } |
| 1288 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1289 | // If it's not calling from cameraserver, check the permission if the |
| 1290 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for |
| 1291 | // android.permission.SYSTEM_CAMERA for system only camera devices). |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1292 | if (callingPid != getpid() && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1293 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1294 | !checkPermission(sCameraPermission, clientPid, clientUid)) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1295 | 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] | 1296 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1297 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission", |
| 1298 | clientName8.string(), clientUid, clientPid, cameraId.string()); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1299 | } |
| 1300 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1301 | // 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] | 1302 | if (!mUidPolicy->isUidActive(callingUid, String16(clientName8))) { |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1303 | int32_t procState = mUidPolicy->getProcState(callingUid); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1304 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", |
| 1305 | clientPid, clientUid); |
| 1306 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1307 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" |
| 1308 | "calling UID %d proc state %" PRId32 ")", |
| 1309 | clientName8.string(), clientUid, clientPid, cameraId.string(), |
| 1310 | callingUid, procState); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1313 | // If sensor privacy is enabled then prevent access to the camera |
| 1314 | if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
| 1315 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); |
| 1316 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1317 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" when sensor privacy " |
| 1318 | "is enabled", clientName8.string(), clientUid, clientPid, cameraId.string()); |
| 1319 | } |
| 1320 | |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1321 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's |
| 1322 | // connected to camera service directly. |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1323 | originalClientPid = clientPid; |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1324 | clientPid = callingPid; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1325 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1326 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1327 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1328 | // For non-system clients : Only allow clients who are being used by the current foreground |
| 1329 | // device user, unless calling from our own process. |
| 1330 | if (!doesClientHaveSystemUid() && callingPid != getpid() && |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1331 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1332 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 1333 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
| 1334 | toString(mAllowedUsers).string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1335 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1336 | "Callers from device user %d are not currently allowed to connect to camera \"%s\"", |
| 1337 | clientUserId, cameraId.string()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1340 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const { |
| 1344 | auto cameraState = getCameraState(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1345 | int callingPid = CameraThreadState::getCallingPid(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1346 | if (cameraState == nullptr) { |
| 1347 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1348 | cameraId.string()); |
| 1349 | return -ENODEV; |
| 1350 | } |
| 1351 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1352 | StatusInternal currentStatus = cameraState->getStatus(); |
| 1353 | if (currentStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1354 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
| 1355 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1356 | return -ENODEV; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1357 | } else if (currentStatus == StatusInternal::ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1358 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
| 1359 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1360 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1361 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1362 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1363 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1366 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1367 | const CameraService::DescriptorPtr& desc, int oomScoreOffset, bool systemNativeClient) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1368 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1369 | // Make a descriptor for the incoming client |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1370 | auto clientDescriptor = |
| 1371 | CameraService::CameraClientManager::makeClientDescriptor(client, desc, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1372 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1373 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 1374 | |
| 1375 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
| 1376 | String8(client->getPackageName())); |
| 1377 | |
| 1378 | if (evicted.size() > 0) { |
| 1379 | // This should never happen - clients should already have been removed in disconnect |
| 1380 | for (auto& i : evicted) { |
| 1381 | ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect", |
| 1382 | __FUNCTION__, i->getKey().string()); |
| 1383 | } |
| 1384 | |
| 1385 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 1386 | __FUNCTION__); |
| 1387 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1388 | |
| 1389 | // And register a death notification for the client callback. Do |
| 1390 | // this last to avoid Binder policy where a nested Binder |
| 1391 | // transaction might be pre-empted to service the client death |
| 1392 | // notification if the client process dies before linkToDeath is |
| 1393 | // invoked. |
| 1394 | sp<IBinder> remoteCallback = client->getRemote(); |
| 1395 | if (remoteCallback != nullptr) { |
| 1396 | remoteCallback->linkToDeath(this); |
| 1397 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid, |
| 1401 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1402 | int oomScoreOffset, bool systemNativeClient, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1403 | /*out*/ |
| 1404 | sp<BasicClient>* client, |
| 1405 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1406 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1407 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1408 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1409 | DescriptorPtr clientDescriptor; |
| 1410 | { |
| 1411 | if (effectiveApiLevel == API_1) { |
| 1412 | // If we are using API1, any existing client for this camera ID with the same remote |
| 1413 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 1414 | |
| 1415 | auto current = mActiveClientManager.get(cameraId); |
| 1416 | if (current != nullptr) { |
| 1417 | auto clientSp = current->getValue(); |
| 1418 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1419 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
Shuzhen Wang | b2d43f6 | 2021-08-25 14:01:11 -0700 | [diff] [blame] | 1420 | ALOGW("CameraService connect called with a different" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1421 | " API level, evicting prior client..."); |
| 1422 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1423 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1424 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1425 | *client = clientSp; |
| 1426 | return NO_ERROR; |
| 1427 | } |
| 1428 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1429 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1430 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 1431 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1432 | // Get current active client PIDs |
| 1433 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 1434 | ownerPids.push_back(clientPid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1435 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1436 | std::vector<int> priorityScores(ownerPids.size()); |
| 1437 | std::vector<int> states(ownerPids.size()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1438 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1439 | // Get priority scores of all active PIDs |
| 1440 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids( |
| 1441 | ownerPids.size(), &ownerPids[0], /*out*/&states[0], |
| 1442 | /*out*/&priorityScores[0]); |
| 1443 | if (err != OK) { |
| 1444 | ALOGE("%s: Priority score query failed: %d", |
| 1445 | __FUNCTION__, err); |
| 1446 | return err; |
| 1447 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1448 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1449 | // Update all active clients' priorities |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1450 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1451 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1452 | pidToPriorityMap.emplace(ownerPids[i], |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 1453 | resource_policy::ClientPriority(priorityScores[i], states[i], |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1454 | /* isVendorClient won't get copied over*/ false, |
| 1455 | /* oomScoreOffset won't get copied over*/ 0)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1456 | } |
| 1457 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1458 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1459 | // Get state for the given cameraId |
| 1460 | auto state = getCameraState(cameraId); |
| 1461 | if (state == nullptr) { |
| 1462 | ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", |
| 1463 | clientPid, cameraId.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1464 | // Should never get here because validateConnectLocked should have errored out |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1465 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1466 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1467 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1468 | int32_t actualScore = priorityScores[priorityScores.size() - 1]; |
| 1469 | int32_t actualState = states[states.size() - 1]; |
| 1470 | |
| 1471 | // Make descriptor for incoming client. We store the oomScoreOffset |
| 1472 | // since we might need it later on new handleEvictionsLocked and |
| 1473 | // ProcessInfoService would not take that into account. |
Shuzhen Wang | 2db86ff | 2018-04-25 01:11:17 +0000 | [diff] [blame] | 1474 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1475 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1476 | state->getConflicting(), actualScore, clientPid, actualState, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1477 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1478 | |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1479 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); |
| 1480 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1481 | // Find clients that would be evicted |
| 1482 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 1483 | |
| 1484 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 1485 | // background, so we cannot do evictions |
| 1486 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 1487 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 1488 | " priority).", clientPid); |
| 1489 | |
| 1490 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1491 | String8 curTime = getFormattedCurrentTime(); |
| 1492 | auto incompatibleClients = |
| 1493 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 1494 | |
| 1495 | 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] | 1496 | "(PID %d, score %d state %d) due to eviction policy", curTime.string(), |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1497 | cameraId.string(), packageName.string(), clientPid, |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1498 | clientPriority.getScore(), clientPriority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1499 | |
| 1500 | for (auto& i : incompatibleClients) { |
| 1501 | msg.appendFormat("\n - Blocked by existing device %s client for package %s" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1502 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", |
| 1503 | i->getKey().string(), |
| 1504 | String8{i->getValue()->getPackageName()}.string(), |
| 1505 | i->getOwnerId(), i->getPriority().getScore(), |
| 1506 | i->getPriority().getState()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1507 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1508 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(), |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1509 | String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1510 | i->getPriority().getScore(), i->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1514 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1515 | mEventLog.add(msg); |
| 1516 | |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1517 | auto current = mActiveClientManager.get(cameraId); |
| 1518 | if (current != nullptr) { |
| 1519 | return -EBUSY; // CAMERA_IN_USE |
| 1520 | } else { |
| 1521 | return -EUSERS; // MAX_CAMERAS_IN_USE |
| 1522 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | for (auto& i : evicted) { |
| 1526 | sp<BasicClient> clientSp = i->getValue(); |
| 1527 | if (clientSp.get() == nullptr) { |
| 1528 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 1529 | |
| 1530 | // TODO: Remove this |
| 1531 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 1532 | __FUNCTION__); |
| 1533 | mActiveClientManager.remove(i); |
| 1534 | continue; |
| 1535 | } |
| 1536 | |
| 1537 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
| 1538 | i->getKey().string()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1539 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1540 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1541 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1542 | logEvent(String8::format("EVICT device %s client held by package %s (PID" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1543 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" |
| 1544 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1545 | i->getKey().string(), String8{clientSp->getPackageName()}.string(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1546 | i->getOwnerId(), i->getPriority().getScore(), |
| 1547 | i->getPriority().getState(), cameraId.string(), |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1548 | packageName.string(), clientPid, clientPriority.getScore(), |
| 1549 | clientPriority.getState())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1550 | |
| 1551 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1552 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1553 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1554 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1557 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 1558 | // other clients from connecting in mServiceLockWrapper if held |
| 1559 | mServiceLock.unlock(); |
| 1560 | |
| 1561 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1562 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1563 | |
| 1564 | // Destroy evicted clients |
| 1565 | for (auto& i : evictedClients) { |
| 1566 | // 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] | 1567 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1568 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1569 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1570 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1571 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1572 | for (const auto& i : evictedClients) { |
| 1573 | ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")", |
| 1574 | __FUNCTION__, i->getKey().string(), i->getOwnerId()); |
| 1575 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 1576 | if (ret == TIMED_OUT) { |
| 1577 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
| 1578 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), |
| 1579 | mActiveClientManager.toString().string()); |
| 1580 | return -EBUSY; |
| 1581 | } |
| 1582 | if (ret != NO_ERROR) { |
| 1583 | ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), " |
| 1584 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret), |
| 1585 | ret, mActiveClientManager.toString().string()); |
| 1586 | return ret; |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | evictedClients.clear(); |
| 1591 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1592 | // Once clients have been disconnected, relock |
| 1593 | mServiceLock.lock(); |
| 1594 | |
| 1595 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 1596 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 1597 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1600 | *partial = clientDescriptor; |
| 1601 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1604 | Status CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1605 | const sp<ICameraClient>& cameraClient, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1606 | int api1CameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1607 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1608 | int clientUid, |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1609 | int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1610 | int targetSdkVersion, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1611 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1612 | sp<ICamera>* device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1613 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1614 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1615 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1616 | |
| 1617 | String8 id = cameraIdIntToStr(api1CameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1618 | sp<Client> client = nullptr; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1619 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1620 | clientPackageName,/*systemNativeClient*/ false, {}, clientUid, clientPid, API_1, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1621 | /*shimUpdateOnly*/ false, /*oomScoreOffset*/ 0, targetSdkVersion, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1622 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1623 | if(!ret.isOk()) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1624 | logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageName), |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1625 | ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1626 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1629 | *device = client; |
| 1630 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1633 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, |
| 1634 | bool isVendorListener, int clientPid, int clientUid) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1635 | // If the client is not a vendor client, don't add listener if |
| 1636 | // a) the camera is a publicly hidden secure camera OR |
| 1637 | // b) the camera is a system only camera and the client doesn't |
| 1638 | // have android.permission.SYSTEM_CAMERA permissions. |
| 1639 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || |
| 1640 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
| 1641 | !hasPermissionsForSystemCamera(clientPid, clientUid)))) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 1642 | return true; |
| 1643 | } |
| 1644 | return false; |
| 1645 | } |
| 1646 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1647 | bool CameraService::shouldRejectSystemCameraConnection(const String8& cameraId) const { |
| 1648 | // Rules for rejection: |
| 1649 | // 1) If cameraserver tries to access this camera device, accept the |
| 1650 | // connection. |
| 1651 | // 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] | 1652 | // non system component is trying to access it. |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1653 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY |
| 1654 | // and the serving thread is a non hwbinder thread, the client must have |
| 1655 | // android.permission.SYSTEM_CAMERA permissions to connect. |
| 1656 | |
| 1657 | int cPid = CameraThreadState::getCallingPid(); |
| 1658 | int cUid = CameraThreadState::getCallingUid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1659 | bool systemClient = doesClientHaveSystemUid(); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1660 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 1661 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1662 | // This isn't a known camera ID, so it's not a system camera |
| 1663 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); |
| 1664 | return false; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1665 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1666 | |
| 1667 | // (1) Cameraserver trying to connect, accept. |
| 1668 | if (CameraThreadState::getCallingPid() == getpid()) { |
| 1669 | return false; |
| 1670 | } |
| 1671 | // (2) |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1672 | if (!systemClient && systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1673 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); |
| 1674 | return true; |
| 1675 | } |
| 1676 | // (3) Here we only check for permissions if it is a system only camera device. This is since |
| 1677 | // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some |
| 1678 | // characteristics) even if clients don't have android.permission.CAMERA. We do not want the |
| 1679 | // same behavior for system camera devices. |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1680 | if (!systemClient && systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 1681 | !hasPermissionsForSystemCamera(cPid, cUid, /*logPermissionFailure*/true)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1682 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", |
| 1683 | cameraId.c_str()); |
| 1684 | return true; |
| 1685 | } |
| 1686 | |
| 1687 | return false; |
| 1688 | } |
| 1689 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1690 | Status CameraService::connectDevice( |
| 1691 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1692 | const String16& cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1693 | const String16& clientPackageName, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1694 | const std::optional<String16>& clientFeatureId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1695 | int clientUid, int oomScoreOffset, int targetSdkVersion, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1696 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1697 | sp<hardware::camera2::ICameraDeviceUser>* device) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1698 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1699 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1700 | Status ret = Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1701 | String8 id = String8(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1702 | sp<CameraDeviceClient> client = nullptr; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1703 | String16 clientPackageNameAdj = clientPackageName; |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1704 | int callingPid = CameraThreadState::getCallingPid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1705 | bool systemNativeClient = false; |
| 1706 | if (doesClientHaveSystemUid() && (clientPackageNameAdj.size() == 0)) { |
| 1707 | std::string systemClient = |
| 1708 | StringPrintf("client.pid<%d>", CameraThreadState::getCallingPid()); |
| 1709 | clientPackageNameAdj = String16(systemClient.c_str()); |
| 1710 | systemNativeClient = true; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1711 | } |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1712 | |
| 1713 | if (oomScoreOffset < 0) { |
| 1714 | String8 msg = |
| 1715 | String8::format("Cannot increase the priority of a client %s pid %d for " |
| 1716 | "camera id %s", String8(clientPackageNameAdj).string(), callingPid, |
| 1717 | id.string()); |
| 1718 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1719 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 1720 | } |
| 1721 | |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 1722 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 1723 | int callingUid = CameraThreadState::getCallingUid(); |
| 1724 | if (clientUid == USE_CALLING_UID) { |
| 1725 | clientUserId = multiuser_get_user_id(callingUid); |
| 1726 | } |
| 1727 | |
| 1728 | if (mCameraServiceProxyWrapper->isCameraDisabled(clientUserId)) { |
Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 1729 | String8 msg = |
| 1730 | String8::format("Camera disabled by device policy"); |
| 1731 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1732 | return STATUS_ERROR(ERROR_DISABLED, msg.string()); |
| 1733 | } |
| 1734 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1735 | // enforce system camera permissions |
| 1736 | if (oomScoreOffset > 0 && |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 1737 | !hasPermissionsForSystemCamera(callingPid, CameraThreadState::getCallingUid()) && |
| 1738 | !isTrustedCallingUid(CameraThreadState::getCallingUid())) { |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1739 | String8 msg = |
| 1740 | String8::format("Cannot change the priority of a client %s pid %d for " |
| 1741 | "camera id %s without SYSTEM_CAMERA permissions", |
| 1742 | String8(clientPackageNameAdj).string(), callingPid, id.string()); |
| 1743 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1744 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, msg.string()); |
| 1745 | } |
| 1746 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1747 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1748 | /*api1CameraId*/-1, clientPackageNameAdj, systemNativeClient,clientFeatureId, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1749 | clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, oomScoreOffset, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1750 | targetSdkVersion, /*out*/client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1751 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1752 | if(!ret.isOk()) { |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1753 | logRejected(id, callingPid, String8(clientPackageNameAdj), ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1754 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1757 | *device = client; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 1758 | Mutex::Autolock lock(mServiceLock); |
| 1759 | |
| 1760 | // Clear the previous cached logs and reposition the |
| 1761 | // file offset to beginning of the file to log new data. |
| 1762 | // If either truncate or lseek fails, close the previous file and create a new one. |
| 1763 | if ((ftruncate(mMemFd, 0) == -1) || (lseek(mMemFd, 0, SEEK_SET) == -1)) { |
| 1764 | ALOGE("%s: Error while truncating the file: %s", __FUNCTION__, sFileName); |
| 1765 | // Close the previous memfd. |
| 1766 | close(mMemFd); |
| 1767 | // If failure to wipe the data, then create a new file and |
| 1768 | // assign the new value to mMemFd. |
| 1769 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 1770 | if (mMemFd == -1) { |
| 1771 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 1772 | } |
| 1773 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1774 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1775 | } |
| 1776 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1777 | String16 CameraService::getPackageNameFromUid(int clientUid) { |
| 1778 | String16 packageName(""); |
| 1779 | |
| 1780 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1781 | sp<IBinder> binder = sm->getService(String16(kPermissionServiceName)); |
| 1782 | if (binder == 0) { |
| 1783 | ALOGE("Cannot get permission service"); |
| 1784 | // Return empty package name and the further interaction |
| 1785 | // with camera will likely fail |
| 1786 | return packageName; |
| 1787 | } |
| 1788 | |
| 1789 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 1790 | Vector<String16> packages; |
| 1791 | |
| 1792 | permCtrl->getPackagesForUid(clientUid, packages); |
| 1793 | |
| 1794 | if (packages.isEmpty()) { |
| 1795 | ALOGE("No packages for calling UID %d", clientUid); |
| 1796 | // Return empty package name and the further interaction |
| 1797 | // with camera will likely fail |
| 1798 | return packageName; |
| 1799 | } |
| 1800 | |
| 1801 | // Arbitrarily pick the first name in the list |
| 1802 | packageName = packages[0]; |
| 1803 | |
| 1804 | return packageName; |
| 1805 | } |
| 1806 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1807 | template<class CALLBACK, class CLIENT> |
| 1808 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1809 | int api1CameraId, const String16& clientPackageNameMaybe, bool systemNativeClient, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1810 | const std::optional<String16>& clientFeatureId, int clientUid, int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1811 | apiLevel effectiveApiLevel, bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1812 | /*out*/sp<CLIENT>& device) { |
| 1813 | binder::Status ret = binder::Status::ok(); |
| 1814 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1815 | bool isNonSystemNdk = false; |
| 1816 | String16 clientPackageName; |
| 1817 | if (clientPackageNameMaybe.size() <= 0) { |
| 1818 | // NDK calls don't come with package names, but we need one for various cases. |
| 1819 | // Generally, there's a 1:1 mapping between UID and package name, but shared UIDs |
| 1820 | // do exist. For all authentication cases, all packages under the same UID get the |
| 1821 | // same permissions, so picking any associated package name is sufficient. For some |
| 1822 | // other cases, this may give inaccurate names for clients in logs. |
| 1823 | isNonSystemNdk = true; |
| 1824 | int packageUid = (clientUid == USE_CALLING_UID) ? |
| 1825 | CameraThreadState::getCallingUid() : clientUid; |
| 1826 | clientPackageName = getPackageNameFromUid(packageUid); |
| 1827 | } else { |
| 1828 | clientPackageName = clientPackageNameMaybe; |
| 1829 | } |
| 1830 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1831 | String8 clientName8(clientPackageName); |
| 1832 | |
| 1833 | int originalClientPid = 0; |
| 1834 | |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1835 | int packagePid = (clientPid == USE_CALLING_PID) ? |
| 1836 | CameraThreadState::getCallingPid() : clientPid; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1837 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1838 | "Camera API version %d", packagePid, clientName8.string(), cameraId.string(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1839 | static_cast<int>(effectiveApiLevel)); |
| 1840 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1841 | nsecs_t openTimeNs = systemTime(); |
| 1842 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1843 | sp<CLIENT> client = nullptr; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1844 | int facing = -1; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1845 | int orientation = 0; |
Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1846 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1847 | { |
| 1848 | // Acquire mServiceLock and prevent other clients from connecting |
| 1849 | std::unique_ptr<AutoConditionLock> lock = |
| 1850 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 1851 | |
| 1852 | if (lock == nullptr) { |
| 1853 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." |
| 1854 | , clientPid); |
| 1855 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1856 | "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting", |
| 1857 | cameraId.string(), clientName8.string(), clientPid); |
| 1858 | } |
| 1859 | |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 1860 | // Enforce client permissions and do basic validity checks |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1861 | if(!(ret = validateConnectLocked(cameraId, clientName8, |
| 1862 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { |
| 1863 | return ret; |
| 1864 | } |
| 1865 | |
| 1866 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 1867 | // we were doing a shim update, return immediately |
| 1868 | if (shimUpdateOnly) { |
| 1869 | auto cameraState = getCameraState(cameraId); |
| 1870 | if (cameraState != nullptr) { |
| 1871 | if (!cameraState->getShimParams().isEmpty()) return ret; |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | status_t err; |
| 1876 | |
| 1877 | sp<BasicClient> clientTmp = nullptr; |
| 1878 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial; |
| 1879 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1880 | IInterface::asBinder(cameraCb), clientName8, oomScoreOffset, systemNativeClient, |
| 1881 | /*out*/&clientTmp, /*out*/&partial)) != NO_ERROR) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1882 | switch (err) { |
| 1883 | case -ENODEV: |
| 1884 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1885 | "No camera device with ID \"%s\" currently available", |
| 1886 | cameraId.string()); |
| 1887 | case -EBUSY: |
| 1888 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 1889 | "Higher-priority client using camera, ID \"%s\" currently unavailable", |
| 1890 | cameraId.string()); |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1891 | case -EUSERS: |
| 1892 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1893 | "Too many cameras already open, cannot open camera \"%s\"", |
| 1894 | cameraId.string()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1895 | default: |
| 1896 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1897 | "Unexpected error %s (%d) opening camera \"%s\"", |
| 1898 | strerror(-err), err, cameraId.string()); |
| 1899 | } |
| 1900 | } |
| 1901 | |
| 1902 | if (clientTmp.get() != nullptr) { |
| 1903 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 1904 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 1905 | return ret; |
| 1906 | } |
| 1907 | |
| 1908 | // give flashlight a chance to close devices if necessary. |
| 1909 | mFlashlight->prepareDeviceOpen(cameraId); |
| 1910 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1911 | auto deviceVersionAndTransport = |
| 1912 | getDeviceVersion(cameraId, /*out*/&facing, /*out*/&orientation); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 1913 | if (facing == -1) { |
| 1914 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string()); |
| 1915 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1916 | "Unable to get camera device \"%s\" facing", cameraId.string()); |
| 1917 | } |
| 1918 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1919 | sp<BasicClient> tmp = nullptr; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1920 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
| 1921 | mPerfClassPrimaryCameraIds, cameraId.string(), targetSdkVersion); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1922 | if(!(ret = makeClient(this, cameraCb, clientPackageName, systemNativeClient, |
| 1923 | clientFeatureId, cameraId, api1CameraId, facing, orientation, |
Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 1924 | clientPid, clientUid, getpid(), |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1925 | deviceVersionAndTransport, effectiveApiLevel, overrideForPerfClass, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1926 | /*out*/&tmp)).isOk()) { |
| 1927 | return ret; |
| 1928 | } |
| 1929 | client = static_cast<CLIENT*>(tmp.get()); |
| 1930 | |
| 1931 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 1932 | __FUNCTION__); |
| 1933 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 1934 | String8 monitorTags = isClientWatched(client.get()) ? mMonitorTags : String8(""); |
| 1935 | err = client->initialize(mCameraProviderManager, monitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1936 | if (err != OK) { |
| 1937 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1938 | // Errors could be from the HAL module open call or from AppOpsManager |
| 1939 | switch(err) { |
| 1940 | case BAD_VALUE: |
| 1941 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1942 | "Illegal argument to HAL module for camera \"%s\"", cameraId.string()); |
| 1943 | case -EBUSY: |
| 1944 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 1945 | "Camera \"%s\" is already open", cameraId.string()); |
| 1946 | case -EUSERS: |
| 1947 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1948 | "Too many cameras already open, cannot open camera \"%s\"", |
| 1949 | cameraId.string()); |
| 1950 | case PERMISSION_DENIED: |
| 1951 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1952 | "No permission to open camera \"%s\"", cameraId.string()); |
| 1953 | case -EACCES: |
| 1954 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1955 | "Camera \"%s\" disabled by policy", cameraId.string()); |
| 1956 | case -ENODEV: |
| 1957 | default: |
| 1958 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1959 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(), |
| 1960 | strerror(-err), err); |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | // Update shim paremeters for legacy clients |
| 1965 | if (effectiveApiLevel == API_1) { |
| 1966 | // Assume we have always received a Client subclass for API1 |
| 1967 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 1968 | String8 rawParams = shimClient->getParameters(); |
| 1969 | CameraParameters params(rawParams); |
| 1970 | |
| 1971 | auto cameraState = getCameraState(cameraId); |
| 1972 | if (cameraState != nullptr) { |
| 1973 | cameraState->setShimParams(params); |
| 1974 | } else { |
| 1975 | ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.", |
| 1976 | __FUNCTION__, cameraId.string()); |
| 1977 | } |
| 1978 | } |
| 1979 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 1980 | // Enable/disable camera service watchdog |
| 1981 | client->setCameraServiceWatchdog(mCameraServiceWatchdogEnabled); |
| 1982 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1983 | // Set rotate-and-crop override behavior |
| 1984 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { |
| 1985 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame] | 1986 | } else { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame^] | 1987 | client->setRotateAndCropOverride( |
| 1988 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
| 1989 | clientPackageName, facing, multiuser_get_user_id(clientUid))); |
| 1990 | } |
| 1991 | |
| 1992 | // Set autoframing override behaviour |
| 1993 | if (mOverrideAutoframingMode != ANDROID_CONTROL_AUTOFRAMING_AUTO) { |
| 1994 | client->setAutoframingOverride(mOverrideAutoframingMode); |
| 1995 | } else { |
| 1996 | client->setAutoframingOverride( |
| 1997 | mCameraServiceProxyWrapper->getAutoframingOverride( |
| 1998 | clientPackageName)); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1999 | } |
| 2000 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2001 | // Set camera muting behavior |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 2002 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 2003 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2004 | if (client->supportsCameraMute()) { |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 2005 | client->setCameraMute( |
| 2006 | mOverrideCameraMuteMode || isCameraPrivacyEnabled); |
| 2007 | } else if (isCameraPrivacyEnabled) { |
| 2008 | // no camera mute supported, but privacy is on! => disconnect |
| 2009 | ALOGI("Camera mute not supported for package: %s, camera id: %s", |
| 2010 | String8(client->getPackageName()).string(), cameraId.string()); |
| 2011 | // Do not hold mServiceLock while disconnecting clients, but |
| 2012 | // retain the condition blocking other clients from connecting |
| 2013 | // in mServiceLockWrapper if held. |
| 2014 | mServiceLock.unlock(); |
| 2015 | // Clear caller identity temporarily so client disconnect PID |
| 2016 | // checks work correctly |
| 2017 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 2018 | // Note AppOp to trigger the "Unblock" dialog |
| 2019 | client->noteAppOp(); |
| 2020 | client->disconnect(); |
| 2021 | CameraThreadState::restoreCallingIdentity(token); |
| 2022 | // Reacquire mServiceLock |
| 2023 | mServiceLock.lock(); |
| 2024 | |
| 2025 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 2026 | "Camera \"%s\" disabled due to camera mute", cameraId.string()); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2027 | } |
| 2028 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2029 | if (shimUpdateOnly) { |
| 2030 | // If only updating legacy shim parameters, immediately disconnect client |
| 2031 | mServiceLock.unlock(); |
| 2032 | client->disconnect(); |
| 2033 | mServiceLock.lock(); |
| 2034 | } else { |
| 2035 | // Otherwise, add client to active clients list |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2036 | finishConnectLocked(client, partial, oomScoreOffset, systemNativeClient); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2037 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 2038 | |
| 2039 | client->setImageDumpMask(mImageDumpMask); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2040 | } // lock is destroyed, allow further connect calls |
| 2041 | |
| 2042 | // Important: release the mutex here so the client can call back into the service from its |
| 2043 | // destructor (can be at the end of the call) |
| 2044 | device = client; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2045 | |
| 2046 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 2047 | mCameraServiceProxyWrapper->logOpen(cameraId, facing, clientPackageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2048 | effectiveApiLevel, isNonSystemNdk, openLatencyMs); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2049 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2050 | { |
| 2051 | Mutex::Autolock lock(mInjectionParametersLock); |
| 2052 | if (cameraId == mInjectionInternalCamId && mInjectionInitPending) { |
| 2053 | mInjectionInitPending = false; |
| 2054 | status_t res = NO_ERROR; |
| 2055 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 2056 | if (clientDescriptor != nullptr) { |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2057 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 2058 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 2059 | if(res != OK) { |
| 2060 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2061 | "No camera device with ID \"%s\" currently available", |
| 2062 | mInjectionExternalCamId.string()); |
| 2063 | } |
| 2064 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2065 | if (res != OK) { |
| 2066 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2067 | } |
| 2068 | } else { |
| 2069 | ALOGE("%s: Internal camera ID = %s 's client does not exist!", |
| 2070 | __FUNCTION__, mInjectionInternalCamId.string()); |
| 2071 | res = NO_INIT; |
| 2072 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2073 | } |
| 2074 | } |
| 2075 | } |
| 2076 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2077 | return ret; |
| 2078 | } |
| 2079 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2080 | status_t CameraService::addOfflineClient(String8 cameraId, sp<BasicClient> offlineClient) { |
| 2081 | if (offlineClient.get() == nullptr) { |
| 2082 | return BAD_VALUE; |
| 2083 | } |
| 2084 | |
| 2085 | { |
| 2086 | // Acquire mServiceLock and prevent other clients from connecting |
| 2087 | std::unique_ptr<AutoConditionLock> lock = |
| 2088 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 2089 | |
| 2090 | if (lock == nullptr) { |
| 2091 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." |
| 2092 | , __FUNCTION__, offlineClient->getClientPid()); |
| 2093 | return TIMED_OUT; |
| 2094 | } |
| 2095 | |
| 2096 | auto onlineClientDesc = mActiveClientManager.get(cameraId); |
| 2097 | if (onlineClientDesc.get() == nullptr) { |
| 2098 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, |
| 2099 | cameraId.c_str()); |
| 2100 | return BAD_VALUE; |
| 2101 | } |
| 2102 | |
| 2103 | // Offline clients do not evict or conflict with other online devices. Resource sharing |
| 2104 | // conflicts are handled by the camera provider which will either succeed or fail before |
| 2105 | // reaching this method. |
| 2106 | const auto& onlinePriority = onlineClientDesc->getPriority(); |
| 2107 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( |
| 2108 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, |
| 2109 | /*conflictingKeys*/ std::set<String8>(), onlinePriority.getScore(), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2110 | onlineClientDesc->getOwnerId(), onlinePriority.getState(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2111 | // native clients don't have offline processing support. |
| 2112 | /*ommScoreOffset*/ 0, /*systemNativeClient*/false); |
Guillaume Bailey | 417e43d | 2022-11-02 15:30:24 +0100 | [diff] [blame] | 2113 | if (offlineClientDesc == nullptr) { |
| 2114 | ALOGE("%s: Offline client descriptor was NULL", __FUNCTION__); |
| 2115 | return BAD_VALUE; |
| 2116 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2117 | |
| 2118 | // Allow only one offline device per camera |
| 2119 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); |
| 2120 | if (!incompatibleClients.empty()) { |
| 2121 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); |
| 2122 | return BAD_VALUE; |
| 2123 | } |
| 2124 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2125 | String8 monitorTags = isClientWatched(offlineClient.get()) ? mMonitorTags : String8(""); |
| 2126 | auto err = offlineClient->initialize(mCameraProviderManager, monitorTags); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2127 | if (err != OK) { |
| 2128 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); |
| 2129 | return err; |
| 2130 | } |
| 2131 | |
| 2132 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); |
| 2133 | if (evicted.size() > 0) { |
| 2134 | for (auto& i : evicted) { |
| 2135 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", |
| 2136 | __FUNCTION__, i->getKey().string()); |
| 2137 | } |
| 2138 | |
| 2139 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " |
| 2140 | "properly", __FUNCTION__); |
| 2141 | |
| 2142 | return BAD_VALUE; |
| 2143 | } |
| 2144 | |
| 2145 | logConnectedOffline(offlineClientDesc->getKey(), |
| 2146 | static_cast<int>(offlineClientDesc->getOwnerId()), |
| 2147 | String8(offlineClient->getPackageName())); |
| 2148 | |
| 2149 | sp<IBinder> remoteCallback = offlineClient->getRemote(); |
| 2150 | if (remoteCallback != nullptr) { |
| 2151 | remoteCallback->linkToDeath(this); |
| 2152 | } |
| 2153 | } // lock is destroyed, allow further connect calls |
| 2154 | |
| 2155 | return OK; |
| 2156 | } |
| 2157 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2158 | Status CameraService::turnOnTorchWithStrengthLevel(const String16& cameraId, int32_t torchStrength, |
| 2159 | const sp<IBinder>& clientBinder) { |
| 2160 | Mutex::Autolock lock(mServiceLock); |
| 2161 | |
| 2162 | ATRACE_CALL(); |
| 2163 | if (clientBinder == nullptr) { |
| 2164 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
| 2165 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2166 | "Torch client binder in null."); |
| 2167 | } |
| 2168 | |
| 2169 | String8 id = String8(cameraId.string()); |
| 2170 | int uid = CameraThreadState::getCallingUid(); |
| 2171 | |
| 2172 | if (shouldRejectSystemCameraConnection(id)) { |
| 2173 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to change the strength level" |
| 2174 | "for system only device %s: ", id.string()); |
| 2175 | } |
| 2176 | |
| 2177 | // verify id is valid |
| 2178 | auto state = getCameraState(id); |
| 2179 | if (state == nullptr) { |
| 2180 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
| 2181 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2182 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
| 2183 | } |
| 2184 | |
| 2185 | StatusInternal cameraStatus = state->getStatus(); |
| 2186 | if (cameraStatus != StatusInternal::NOT_AVAILABLE && |
| 2187 | cameraStatus != StatusInternal::PRESENT) { |
| 2188 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), |
| 2189 | (int)cameraStatus); |
| 2190 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2191 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
| 2192 | } |
| 2193 | |
| 2194 | { |
| 2195 | Mutex::Autolock al(mTorchStatusMutex); |
| 2196 | TorchModeStatus status; |
| 2197 | status_t err = getTorchStatusLocked(id, &status); |
| 2198 | if (err != OK) { |
| 2199 | if (err == NAME_NOT_FOUND) { |
| 2200 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2201 | "Camera \"%s\" does not have a flash unit", id.string()); |
| 2202 | } |
| 2203 | ALOGE("%s: getting current torch status failed for camera %s", |
| 2204 | __FUNCTION__, id.string()); |
| 2205 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2206 | "Error changing torch strength level for camera \"%s\": %s (%d)", |
| 2207 | id.string(), strerror(-err), err); |
| 2208 | } |
| 2209 | |
| 2210 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
| 2211 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
| 2212 | ALOGE("%s: torch mode of camera %s is not available because " |
| 2213 | "camera is in use.", __FUNCTION__, id.string()); |
| 2214 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2215 | "Torch for camera \"%s\" is not available due to an existing camera user", |
| 2216 | id.string()); |
| 2217 | } else { |
| 2218 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 2219 | "insufficient resources", __FUNCTION__, id.string()); |
| 2220 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2221 | "Torch for camera \"%s\" is not available due to insufficient resources", |
| 2222 | id.string()); |
| 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | { |
| 2228 | Mutex::Autolock al(mTorchUidMapMutex); |
| 2229 | updateTorchUidMapLocked(cameraId, uid); |
| 2230 | } |
| 2231 | // Check if the current torch strength level is same as the new one. |
| 2232 | bool shouldSkipTorchStrengthUpdates = mCameraProviderManager->shouldSkipTorchStrengthUpdate( |
| 2233 | id.string(), torchStrength); |
| 2234 | |
| 2235 | status_t err = mFlashlight->turnOnTorchWithStrengthLevel(id, torchStrength); |
| 2236 | |
| 2237 | if (err != OK) { |
| 2238 | int32_t errorCode; |
| 2239 | String8 msg; |
| 2240 | switch (err) { |
| 2241 | case -ENOSYS: |
| 2242 | msg = String8::format("Camera \"%s\" has no flashlight.", |
| 2243 | id.string()); |
| 2244 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2245 | break; |
| 2246 | case -EBUSY: |
| 2247 | msg = String8::format("Camera \"%s\" is in use", |
| 2248 | id.string()); |
| 2249 | errorCode = ERROR_CAMERA_IN_USE; |
| 2250 | break; |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 2251 | case -EINVAL: |
| 2252 | msg = String8::format("Torch strength level %d is not within the " |
| 2253 | "valid range.", torchStrength); |
| 2254 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2255 | break; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2256 | default: |
| 2257 | msg = String8::format("Changing torch strength level failed."); |
| 2258 | errorCode = ERROR_INVALID_OPERATION; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2259 | } |
| 2260 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2261 | return STATUS_ERROR(errorCode, msg.string()); |
| 2262 | } |
| 2263 | |
| 2264 | { |
| 2265 | // update the link to client's death |
| 2266 | // Store the last client that turns on each camera's torch mode. |
| 2267 | Mutex::Autolock al(mTorchClientMapMutex); |
| 2268 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 2269 | if (index == NAME_NOT_FOUND) { |
| 2270 | mTorchClientMap.add(id, clientBinder); |
| 2271 | } else { |
| 2272 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
| 2273 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2274 | } |
| 2275 | clientBinder->linkToDeath(this); |
| 2276 | } |
| 2277 | |
| 2278 | int clientPid = CameraThreadState::getCallingPid(); |
| 2279 | const char *id_cstr = id.c_str(); |
| 2280 | ALOGI("%s: Torch strength for camera id %s changed to %d for client PID %d", |
| 2281 | __FUNCTION__, id_cstr, torchStrength, clientPid); |
| 2282 | if (!shouldSkipTorchStrengthUpdates) { |
| 2283 | broadcastTorchStrengthLevel(id, torchStrength); |
| 2284 | } |
| 2285 | return Status::ok(); |
| 2286 | } |
| 2287 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2288 | Status CameraService::setTorchMode(const String16& cameraId, bool enabled, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2289 | const sp<IBinder>& clientBinder) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2290 | Mutex::Autolock lock(mServiceLock); |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2291 | |
| 2292 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2293 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2294 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2295 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2296 | "Torch client Binder is null"); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2297 | } |
| 2298 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2299 | String8 id = String8(cameraId.string()); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2300 | int uid = CameraThreadState::getCallingUid(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2301 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2302 | if (shouldRejectSystemCameraConnection(id)) { |
| 2303 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" |
| 2304 | " for system only device %s: ", id.string()); |
| 2305 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2306 | // verify id is valid. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2307 | auto state = getCameraState(id); |
| 2308 | if (state == nullptr) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 2309 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2310 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2311 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2312 | } |
| 2313 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2314 | StatusInternal cameraStatus = state->getStatus(); |
| 2315 | if (cameraStatus != StatusInternal::PRESENT && |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2316 | cameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 2317 | 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] | 2318 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2319 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | { |
| 2323 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2324 | TorchModeStatus status; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2325 | status_t err = getTorchStatusLocked(id, &status); |
| 2326 | if (err != OK) { |
| 2327 | if (err == NAME_NOT_FOUND) { |
| 2328 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2329 | "Camera \"%s\" does not have a flash unit", id.string()); |
| 2330 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2331 | ALOGE("%s: getting current torch status failed for camera %s", |
| 2332 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2333 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2334 | "Error updating torch status for camera \"%s\": %s (%d)", id.string(), |
| 2335 | strerror(-err), err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2336 | } |
| 2337 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2338 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2339 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2340 | ALOGE("%s: torch mode of camera %s is not available because " |
| 2341 | "camera is in use", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2342 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2343 | "Torch for camera \"%s\" is not available due to an existing camera user", |
| 2344 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2345 | } else { |
| 2346 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 2347 | "insufficient resources", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2348 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2349 | "Torch for camera \"%s\" is not available due to insufficient resources", |
| 2350 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2351 | } |
| 2352 | } |
| 2353 | } |
| 2354 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2355 | { |
| 2356 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 2357 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 2358 | Mutex::Autolock al(mTorchUidMapMutex); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2359 | updateTorchUidMapLocked(cameraId, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2360 | } |
| 2361 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2362 | status_t err = mFlashlight->setTorchMode(id, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2363 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2364 | if (err != OK) { |
| 2365 | int32_t errorCode; |
| 2366 | String8 msg; |
| 2367 | switch (err) { |
| 2368 | case -ENOSYS: |
| 2369 | msg = String8::format("Camera \"%s\" has no flashlight", |
| 2370 | id.string()); |
| 2371 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2372 | break; |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 2373 | case -EBUSY: |
| 2374 | msg = String8::format("Camera \"%s\" is in use", |
| 2375 | id.string()); |
| 2376 | errorCode = ERROR_CAMERA_IN_USE; |
| 2377 | break; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2378 | default: |
| 2379 | msg = String8::format( |
| 2380 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", |
| 2381 | id.string(), enabled, strerror(-err), err); |
| 2382 | errorCode = ERROR_INVALID_OPERATION; |
| 2383 | } |
| 2384 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2385 | logServiceError(msg,errorCode); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2386 | return STATUS_ERROR(errorCode, msg.string()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2387 | } |
| 2388 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2389 | { |
| 2390 | // update the link to client's death |
| 2391 | Mutex::Autolock al(mTorchClientMapMutex); |
| 2392 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 2393 | if (enabled) { |
| 2394 | if (index == NAME_NOT_FOUND) { |
| 2395 | mTorchClientMap.add(id, clientBinder); |
| 2396 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2397 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2398 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2399 | } |
| 2400 | clientBinder->linkToDeath(this); |
| 2401 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2402 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2403 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2404 | } |
| 2405 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 2406 | int clientPid = CameraThreadState::getCallingPid(); |
| 2407 | const char *id_cstr = id.c_str(); |
| 2408 | const char *torchState = enabled ? "on" : "off"; |
| 2409 | ALOGI("Torch for camera id %s turned %s for client PID %d", id_cstr, torchState, clientPid); |
| 2410 | logTorchEvent(id_cstr, torchState , clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2411 | return Status::ok(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2412 | } |
| 2413 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2414 | void CameraService::updateTorchUidMapLocked(const String16& cameraId, int uid) { |
| 2415 | String8 id = String8(cameraId.string()); |
| 2416 | if (mTorchUidMap.find(id) == mTorchUidMap.end()) { |
| 2417 | mTorchUidMap[id].first = uid; |
| 2418 | mTorchUidMap[id].second = uid; |
| 2419 | } else { |
| 2420 | // Set the pending UID |
| 2421 | mTorchUidMap[id].first = uid; |
| 2422 | } |
| 2423 | } |
| 2424 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2425 | Status CameraService::notifySystemEvent(int32_t eventId, |
| 2426 | const std::vector<int32_t>& args) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2427 | const int pid = CameraThreadState::getCallingPid(); |
| 2428 | const int selfPid = getpid(); |
| 2429 | |
| 2430 | // Permission checks |
| 2431 | if (pid != selfPid) { |
| 2432 | // Ensure we're being called by system_server, or similar process with |
| 2433 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2434 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2435 | const int uid = CameraThreadState::getCallingUid(); |
| 2436 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 2437 | " events from pid=%d, uid=%d", pid, uid); |
| 2438 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 2439 | "No permission to send updates to camera service about system events" |
| 2440 | " from pid=%d, uid=%d", pid, uid); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2441 | } |
| 2442 | } |
| 2443 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2444 | ATRACE_CALL(); |
| 2445 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2446 | switch(eventId) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2447 | case ICameraService::EVENT_USER_SWITCHED: { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2448 | // 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] | 2449 | // from a system server crash |
| 2450 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2451 | mSensorPrivacyPolicy->registerSelf(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2452 | doUserSwitch(/*newUserIds*/ args); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2453 | break; |
| 2454 | } |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 2455 | case ICameraService::EVENT_USB_DEVICE_ATTACHED: |
| 2456 | case ICameraService::EVENT_USB_DEVICE_DETACHED: { |
| 2457 | // Notify CameraProviderManager for lazy HALs |
| 2458 | mCameraProviderManager->notifyUsbDeviceEvent(eventId, |
| 2459 | std::to_string(args[0])); |
| 2460 | break; |
| 2461 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2462 | case ICameraService::EVENT_NONE: |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2463 | default: { |
| 2464 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 2465 | eventId); |
| 2466 | break; |
| 2467 | } |
| 2468 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2469 | return Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2470 | } |
| 2471 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2472 | void CameraService::notifyMonitoredUids() { |
| 2473 | Mutex::Autolock lock(mStatusListenerLock); |
| 2474 | |
| 2475 | for (const auto& it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2476 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 2477 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", |
| 2478 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2479 | } |
| 2480 | } |
| 2481 | |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2482 | Status CameraService::notifyDeviceStateChange(int64_t newState) { |
| 2483 | const int pid = CameraThreadState::getCallingPid(); |
| 2484 | const int selfPid = getpid(); |
| 2485 | |
| 2486 | // Permission checks |
| 2487 | if (pid != selfPid) { |
| 2488 | // Ensure we're being called by system_server, or similar process with |
| 2489 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2490 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2491 | const int uid = CameraThreadState::getCallingUid(); |
| 2492 | ALOGE("Permission Denial: cannot send updates to camera service about device" |
| 2493 | " state changes from pid=%d, uid=%d", pid, uid); |
| 2494 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2495 | "No permission to send updates to camera service about device state" |
| 2496 | " changes from pid=%d, uid=%d", pid, uid); |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | ATRACE_CALL(); |
| 2501 | |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 2502 | mCameraProviderManager->notifyDeviceStateChange(newState); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2503 | |
| 2504 | return Status::ok(); |
| 2505 | } |
| 2506 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2507 | Status CameraService::notifyDisplayConfigurationChange() { |
| 2508 | ATRACE_CALL(); |
| 2509 | const int callingPid = CameraThreadState::getCallingPid(); |
| 2510 | const int selfPid = getpid(); |
| 2511 | |
| 2512 | // Permission checks |
| 2513 | if (callingPid != selfPid) { |
| 2514 | // Ensure we're being called by system_server, or similar process with |
| 2515 | // permissions to notify the camera service about system events |
| 2516 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
| 2517 | const int uid = CameraThreadState::getCallingUid(); |
| 2518 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" |
| 2519 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2520 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2521 | "No permission to send updates to camera service about orientation" |
| 2522 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | Mutex::Autolock lock(mServiceLock); |
| 2527 | |
| 2528 | // Don't do anything if rotate-and-crop override via cmd is active |
| 2529 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); |
| 2530 | |
| 2531 | const auto clients = mActiveClientManager.getAll(); |
| 2532 | for (auto& current : clients) { |
| 2533 | if (current != nullptr) { |
| 2534 | const auto basicClient = current->getValue(); |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame] | 2535 | if (basicClient.get() != nullptr) { |
Emilian Peev | 065b2c1 | 2021-11-23 13:12:57 -0800 | [diff] [blame] | 2536 | basicClient->setRotateAndCropOverride( |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 2537 | mCameraServiceProxyWrapper->getRotateAndCropOverride( |
Emilian Peev | 065b2c1 | 2021-11-23 13:12:57 -0800 | [diff] [blame] | 2538 | basicClient->getPackageName(), |
| 2539 | basicClient->getCameraFacing(), |
| 2540 | multiuser_get_user_id(basicClient->getClientUid()))); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | return Status::ok(); |
| 2546 | } |
| 2547 | |
| 2548 | Status CameraService::getConcurrentCameraIds( |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2549 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { |
| 2550 | ATRACE_CALL(); |
| 2551 | if (!concurrentCameraIds) { |
| 2552 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); |
| 2553 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); |
| 2554 | } |
| 2555 | |
| 2556 | if (!mInitialized) { |
| 2557 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2558 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2559 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2560 | "Camera subsystem is not available"); |
| 2561 | } |
| 2562 | // First call into the provider and get the set of concurrent camera |
| 2563 | // combinations |
| 2564 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = |
Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 2565 | mCameraProviderManager->getConcurrentCameraIds(); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2566 | for (auto &combination : concurrentCameraCombinations) { |
| 2567 | std::vector<std::string> validCombination; |
| 2568 | for (auto &cameraId : combination) { |
| 2569 | // if the camera state is not present, skip |
| 2570 | String8 cameraIdStr(cameraId.c_str()); |
| 2571 | auto state = getCameraState(cameraIdStr); |
| 2572 | if (state == nullptr) { |
| 2573 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); |
| 2574 | continue; |
| 2575 | } |
| 2576 | StatusInternal status = state->getStatus(); |
| 2577 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { |
| 2578 | continue; |
| 2579 | } |
| 2580 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { |
| 2581 | continue; |
| 2582 | } |
| 2583 | validCombination.push_back(cameraId); |
| 2584 | } |
| 2585 | if (validCombination.size() != 0) { |
| 2586 | concurrentCameraIds->push_back(std::move(validCombination)); |
| 2587 | } |
| 2588 | } |
| 2589 | return Status::ok(); |
| 2590 | } |
| 2591 | |
| 2592 | Status CameraService::isConcurrentSessionConfigurationSupported( |
| 2593 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2594 | int targetSdkVersion, /*out*/bool* isSupported) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2595 | if (!isSupported) { |
| 2596 | ALOGE("%s: isSupported is NULL", __FUNCTION__); |
| 2597 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); |
| 2598 | } |
| 2599 | |
| 2600 | if (!mInitialized) { |
| 2601 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 2602 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2603 | "Camera subsystem is not available"); |
| 2604 | } |
| 2605 | |
| 2606 | // Check for camera permissions |
| 2607 | int callingPid = CameraThreadState::getCallingPid(); |
| 2608 | int callingUid = CameraThreadState::getCallingUid(); |
| 2609 | if ((callingPid != getpid()) && !checkPermission(sCameraPermission, callingPid, callingUid)) { |
| 2610 | ALOGE("%s: pid %d doesn't have camera permissions", __FUNCTION__, callingPid); |
| 2611 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 2612 | "android.permission.CAMERA needed to call" |
| 2613 | "isConcurrentSessionConfigurationSupported"); |
| 2614 | } |
| 2615 | |
| 2616 | status_t res = |
| 2617 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2618 | cameraIdsAndSessionConfigurations, mPerfClassPrimaryCameraIds, |
| 2619 | targetSdkVersion, isSupported); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2620 | if (res != OK) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2621 | logServiceError(String8::format("Unable to query session configuration support"), |
| 2622 | ERROR_INVALID_OPERATION); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2623 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " |
| 2624 | "support %s (%d)", strerror(-res), res); |
| 2625 | } |
| 2626 | return Status::ok(); |
| 2627 | } |
| 2628 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2629 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, |
| 2630 | /*out*/ |
| 2631 | std::vector<hardware::CameraStatus> *cameraStatuses) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2632 | return addListenerHelper(listener, cameraStatuses); |
| 2633 | } |
| 2634 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2635 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, |
| 2636 | std::vector<hardware::CameraStatus>* cameraStatuses) { |
| 2637 | return addListenerHelper(listener, cameraStatuses, false, true); |
| 2638 | } |
| 2639 | |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2640 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, |
| 2641 | /*out*/ |
| 2642 | std::vector<hardware::CameraStatus> *cameraStatuses, |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2643 | bool isVendorListener, bool isProcessLocalTest) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2644 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2645 | ATRACE_CALL(); |
| 2646 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2647 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2648 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 2649 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2650 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2651 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2652 | } |
| 2653 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2654 | auto clientUid = CameraThreadState::getCallingUid(); |
| 2655 | auto clientPid = CameraThreadState::getCallingPid(); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2656 | bool openCloseCallbackAllowed = checkPermission(sCameraOpenCloseListenerPermission, |
Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 2657 | clientPid, clientUid, /*logPermissionFailure*/false); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2658 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2659 | Mutex::Autolock lock(mServiceLock); |
| 2660 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2661 | { |
| 2662 | Mutex::Autolock lock(mStatusListenerLock); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2663 | for (const auto &it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2664 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2665 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 2666 | __FUNCTION__, listener.get()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2667 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2668 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2669 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2670 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2671 | sp<ServiceListener> serviceListener = |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2672 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, |
| 2673 | openCloseCallbackAllowed); |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2674 | auto ret = serviceListener->initialize(isProcessLocalTest); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2675 | if (ret != NO_ERROR) { |
| 2676 | String8 msg = String8::format("Failed to initialize service listener: %s (%d)", |
| 2677 | strerror(-ret), ret); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2678 | logServiceError(msg,ERROR_ILLEGAL_ARGUMENT); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2679 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2680 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 2681 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2682 | // The listener still needs to be added to the list of listeners, regardless of what |
| 2683 | // permissions the listener process has / whether it is a vendor listener. Since it might be |
| 2684 | // eligible to listen to other camera ids. |
| 2685 | mListenerList.emplace_back(serviceListener); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2686 | mUidPolicy->registerMonitorUid(clientUid); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2687 | } |
| 2688 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2689 | /* Collect current devices and status */ |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2690 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2691 | Mutex::Autolock lock(mCameraStatesLock); |
| 2692 | for (auto& i : mCameraStates) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 2693 | cameraStatuses->emplace_back(i.first, |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 2694 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds(), |
| 2695 | openCloseCallbackAllowed ? i.second->getClientPackage() : String8::empty()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2696 | } |
| 2697 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2698 | // Remove the camera statuses that should be hidden from the client, we do |
| 2699 | // this after collecting the states in order to avoid holding |
| 2700 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at |
| 2701 | // the same time. |
| 2702 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), |
| 2703 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { |
| 2704 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 2705 | if (getSystemCameraKind(s.cameraId, &deviceKind) != OK) { |
| 2706 | ALOGE("%s: Invalid camera id %s, skipping status update", |
| 2707 | __FUNCTION__, s.cameraId.c_str()); |
| 2708 | return true; |
| 2709 | } |
| 2710 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, |
| 2711 | clientUid);}), cameraStatuses->end()); |
| 2712 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2713 | //cameraStatuses will have non-eligible camera ids removed. |
| 2714 | std::set<String16> idsChosenForCallback; |
| 2715 | for (const auto &s : *cameraStatuses) { |
| 2716 | idsChosenForCallback.insert(String16(s.cameraId)); |
| 2717 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2718 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2719 | /* |
| 2720 | * Immediately signal current torch status to this listener only |
| 2721 | * This may be a subset of all the devices, so don't include it in the response directly |
| 2722 | */ |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2723 | { |
| 2724 | Mutex::Autolock al(mTorchStatusMutex); |
| 2725 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2726 | String16 id = String16(mTorchStatusMap.keyAt(i).string()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2727 | // The camera id is visible to the client. Fine to send torch |
| 2728 | // callback. |
| 2729 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { |
| 2730 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id); |
| 2731 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2732 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2733 | } |
| 2734 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2735 | return Status::ok(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2736 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2737 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2738 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2739 | ATRACE_CALL(); |
| 2740 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2741 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 2742 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2743 | if (listener == 0) { |
| 2744 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2745 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2746 | } |
| 2747 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2748 | Mutex::Autolock lock(mServiceLock); |
| 2749 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2750 | { |
| 2751 | Mutex::Autolock lock(mStatusListenerLock); |
| 2752 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2753 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { |
| 2754 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid()); |
| 2755 | IInterface::asBinder(listener)->unlinkToDeath(*it); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2756 | mListenerList.erase(it); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2757 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2758 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 2763 | __FUNCTION__, listener.get()); |
| 2764 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2765 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2766 | } |
| 2767 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2768 | Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2769 | |
| 2770 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2771 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 2772 | |
| 2773 | if (parameters == NULL) { |
| 2774 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2775 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2778 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2779 | |
| 2780 | CameraParameters shimParams; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2781 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2782 | // Error logged by caller |
| 2783 | return ret; |
| 2784 | } |
| 2785 | |
| 2786 | String8 shimParamsString8 = shimParams.flatten(); |
| 2787 | String16 shimParamsString16 = String16(shimParamsString8); |
| 2788 | |
| 2789 | *parameters = shimParamsString16; |
| 2790 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2791 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2792 | } |
| 2793 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2794 | Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion, |
| 2795 | /*out*/ bool *isSupported) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2796 | ATRACE_CALL(); |
| 2797 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2798 | const String8 id = String8(cameraId); |
| 2799 | |
| 2800 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2801 | |
| 2802 | switch (apiVersion) { |
| 2803 | case API_VERSION_1: |
| 2804 | case API_VERSION_2: |
| 2805 | break; |
| 2806 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2807 | String8 msg = String8::format("Unknown API version %d", apiVersion); |
| 2808 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2809 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2810 | } |
| 2811 | |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2812 | auto deviceVersionAndTransport = getDeviceVersion(id); |
| 2813 | if (deviceVersionAndTransport.first == -1) { |
| 2814 | String8 msg = String8::format("Unknown camera ID %s", id.string()); |
| 2815 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2816 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 2817 | } |
| 2818 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { |
| 2819 | int deviceVersion = deviceVersionAndTransport.first; |
| 2820 | switch (deviceVersion) { |
| 2821 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 2822 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 2823 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 2824 | if (apiVersion == API_VERSION_2) { |
| 2825 | ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without " |
| 2826 | "shim", __FUNCTION__, id.string(), deviceVersion); |
| 2827 | *isSupported = false; |
| 2828 | } else { // if (apiVersion == API_VERSION_1) { |
| 2829 | ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always " |
| 2830 | "supported", __FUNCTION__, id.string()); |
| 2831 | *isSupported = true; |
| 2832 | } |
| 2833 | break; |
| 2834 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 2835 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 2836 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 2837 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 2838 | case CAMERA_DEVICE_API_VERSION_3_6: |
| 2839 | case CAMERA_DEVICE_API_VERSION_3_7: |
| 2840 | 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] | 2841 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2842 | *isSupported = true; |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2843 | break; |
| 2844 | default: { |
| 2845 | String8 msg = String8::format("Unknown device version %x for device %s", |
| 2846 | deviceVersion, id.string()); |
| 2847 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2848 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2849 | } |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2850 | } |
Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2851 | } else { |
| 2852 | *isSupported = true; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2853 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2854 | return Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2855 | } |
| 2856 | |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2857 | Status CameraService::isHiddenPhysicalCamera(const String16& cameraId, |
| 2858 | /*out*/ bool *isSupported) { |
| 2859 | ATRACE_CALL(); |
| 2860 | |
| 2861 | const String8 id = String8(cameraId); |
| 2862 | |
| 2863 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
| 2864 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(id.string()); |
| 2865 | |
| 2866 | return Status::ok(); |
| 2867 | } |
| 2868 | |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2869 | Status CameraService::injectCamera( |
| 2870 | const String16& packageName, const String16& internalCamId, |
| 2871 | const String16& externalCamId, |
| 2872 | const sp<ICameraInjectionCallback>& callback, |
| 2873 | /*out*/ |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2874 | sp<ICameraInjectionSession>* cameraInjectionSession) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2875 | ATRACE_CALL(); |
| 2876 | |
| 2877 | if (!checkCallingPermission(sCameraInjectExternalCameraPermission)) { |
| 2878 | const int pid = CameraThreadState::getCallingPid(); |
| 2879 | const int uid = CameraThreadState::getCallingUid(); |
| 2880 | ALOGE("Permission Denial: can't inject camera pid=%d, uid=%d", pid, uid); |
| 2881 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 2882 | "Permission Denial: no permission to inject camera"); |
| 2883 | } |
| 2884 | |
| 2885 | ALOGV( |
| 2886 | "%s: Package name = %s, Internal camera ID = %s, External camera ID = " |
| 2887 | "%s", |
| 2888 | __FUNCTION__, String8(packageName).string(), |
| 2889 | String8(internalCamId).string(), String8(externalCamId).string()); |
| 2890 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2891 | { |
| 2892 | Mutex::Autolock lock(mInjectionParametersLock); |
| 2893 | mInjectionInternalCamId = String8(internalCamId); |
| 2894 | mInjectionExternalCamId = String8(externalCamId); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2895 | mInjectionStatusListener->addListener(callback); |
| 2896 | *cameraInjectionSession = new CameraInjectionSession(this); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2897 | status_t res = NO_ERROR; |
| 2898 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 2899 | // If the client already exists, we can directly connect to the camera device through the |
| 2900 | // client's injectCamera(), otherwise we need to wait until the client is established |
| 2901 | // (execute connectHelper()) before injecting the camera to the camera device. |
| 2902 | if (clientDescriptor != nullptr) { |
| 2903 | mInjectionInitPending = false; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2904 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 2905 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 2906 | if(res != OK) { |
| 2907 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2908 | "No camera device with ID \"%s\" currently available", |
| 2909 | mInjectionExternalCamId.string()); |
| 2910 | } |
| 2911 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2912 | if(res != OK) { |
| 2913 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2914 | } |
| 2915 | } else { |
| 2916 | mInjectionInitPending = true; |
| 2917 | } |
| 2918 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2919 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2920 | return binder::Status::ok(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2921 | } |
| 2922 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2923 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2924 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2925 | for (auto& i : mActiveClientManager.getAll()) { |
| 2926 | auto clientSp = i->getValue(); |
| 2927 | if (clientSp.get() == client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2928 | cacheClientTagDumpIfNeeded(client->mCameraIdStr, clientSp.get()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2929 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2930 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2931 | } |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2932 | updateAudioRestrictionLocked(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2933 | } |
| 2934 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2935 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2936 | bool ret = false; |
| 2937 | { |
| 2938 | // Acquire mServiceLock and prevent other clients from connecting |
| 2939 | std::unique_ptr<AutoConditionLock> lock = |
| 2940 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2941 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2942 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2943 | std::vector<sp<BasicClient>> evicted; |
| 2944 | for (auto& i : mActiveClientManager.getAll()) { |
| 2945 | auto clientSp = i->getValue(); |
| 2946 | if (clientSp.get() == nullptr) { |
| 2947 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 2948 | mActiveClientManager.remove(i); |
| 2949 | continue; |
| 2950 | } |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 2951 | if (remote == clientSp->getRemote()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2952 | mActiveClientManager.remove(i); |
| 2953 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2954 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2955 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2956 | clientSp->notifyError( |
| 2957 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2958 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2959 | } |
| 2960 | } |
| 2961 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2962 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 2963 | // other clients from connecting in mServiceLockWrapper if held |
| 2964 | mServiceLock.unlock(); |
| 2965 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2966 | // Do not clear caller identity, remote caller should be client proccess |
| 2967 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2968 | for (auto& i : evicted) { |
| 2969 | if (i.get() != nullptr) { |
| 2970 | i->disconnect(); |
| 2971 | ret = true; |
| 2972 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2973 | } |
| 2974 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2975 | // Reacquire mServiceLock |
| 2976 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2977 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2978 | } // lock is destroyed, allow further connect calls |
| 2979 | |
| 2980 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2981 | } |
| 2982 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2983 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
| 2984 | const String8& cameraId) const { |
| 2985 | std::shared_ptr<CameraState> state; |
| 2986 | { |
| 2987 | Mutex::Autolock lock(mCameraStatesLock); |
| 2988 | auto iter = mCameraStates.find(cameraId); |
| 2989 | if (iter != mCameraStates.end()) { |
| 2990 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2991 | } |
| 2992 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2993 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2994 | } |
| 2995 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2996 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) { |
| 2997 | // Remove from active clients list |
| 2998 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 2999 | if (clientDescriptorPtr == nullptr) { |
| 3000 | ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__, |
| 3001 | cameraId.string()); |
| 3002 | return sp<BasicClient>{nullptr}; |
| 3003 | } |
| 3004 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3005 | sp<BasicClient> client = clientDescriptorPtr->getValue(); |
| 3006 | if (client.get() != nullptr) { |
| 3007 | cacheClientTagDumpIfNeeded(clientDescriptorPtr->getKey(), client.get()); |
| 3008 | } |
| 3009 | return client; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 3010 | } |
| 3011 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3012 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3013 | // Acquire mServiceLock and prevent other clients from connecting |
| 3014 | std::unique_ptr<AutoConditionLock> lock = |
| 3015 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 3016 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3017 | std::set<userid_t> newAllowedUsers; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3018 | for (size_t i = 0; i < newUserIds.size(); i++) { |
| 3019 | if (newUserIds[i] < 0) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3020 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3021 | __FUNCTION__, newUserIds[i]); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3022 | return; |
| 3023 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3024 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3025 | } |
| 3026 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3027 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3028 | if (newAllowedUsers == mAllowedUsers) { |
| 3029 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 3030 | return; |
| 3031 | } |
| 3032 | |
| 3033 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 3034 | |
| 3035 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3036 | |
| 3037 | // Current user has switched, evict all current clients. |
| 3038 | std::vector<sp<BasicClient>> evicted; |
| 3039 | for (auto& i : mActiveClientManager.getAll()) { |
| 3040 | auto clientSp = i->getValue(); |
| 3041 | |
| 3042 | if (clientSp.get() == nullptr) { |
| 3043 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 3044 | continue; |
| 3045 | } |
| 3046 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3047 | // Don't evict clients that are still allowed. |
| 3048 | uid_t clientUid = clientSp->getClientUid(); |
| 3049 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 3050 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 3051 | continue; |
| 3052 | } |
| 3053 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3054 | evicted.push_back(clientSp); |
| 3055 | |
| 3056 | String8 curTime = getFormattedCurrentTime(); |
| 3057 | |
| 3058 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
| 3059 | i->getKey().string()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3060 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3061 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3062 | logEvent(String8::format("EVICT device %s client held by package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3063 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due" |
| 3064 | " to user switch.", i->getKey().string(), |
| 3065 | String8{clientSp->getPackageName()}.string(), |
| 3066 | i->getOwnerId(), i->getPriority().getScore(), |
| 3067 | i->getPriority().getState())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3068 | |
| 3069 | } |
| 3070 | |
| 3071 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 3072 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 3073 | mServiceLock.unlock(); |
| 3074 | |
| 3075 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3076 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3077 | |
| 3078 | for (auto& i : evicted) { |
| 3079 | i->disconnect(); |
| 3080 | } |
| 3081 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3082 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3083 | |
| 3084 | // Reacquire mServiceLock |
| 3085 | mServiceLock.lock(); |
| 3086 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3087 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3088 | void CameraService::logEvent(const char* event) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3089 | String8 curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3090 | Mutex::Autolock l(mLogLock); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3091 | String8 msg = String8::format("%s : %s", curTime.string(), event); |
| 3092 | // For service error events, print the msg only once. |
| 3093 | if(!msg.contains("SERVICE ERROR")) { |
| 3094 | mEventLog.add(msg); |
| 3095 | } else if(sServiceErrorEventSet.find(msg) == sServiceErrorEventSet.end()) { |
| 3096 | // Error event not added to the dumpsys log before |
| 3097 | mEventLog.add(msg); |
| 3098 | sServiceErrorEventSet.insert(msg); |
| 3099 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3100 | } |
| 3101 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3102 | void CameraService::logDisconnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3103 | const char* clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3104 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3105 | 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] | 3106 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3107 | } |
| 3108 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3109 | void CameraService::logDisconnectedOffline(const char* cameraId, int clientPid, |
| 3110 | const char* clientPackage) { |
| 3111 | // Log the clients evicted |
| 3112 | logEvent(String8::format("DISCONNECT offline device %s client for package %s (PID %d)", |
| 3113 | cameraId, clientPackage, clientPid)); |
| 3114 | } |
| 3115 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3116 | void CameraService::logConnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3117 | const char* clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3118 | // Log the clients evicted |
| 3119 | 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] | 3120 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3121 | } |
| 3122 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3123 | void CameraService::logConnectedOffline(const char* cameraId, int clientPid, |
| 3124 | const char* clientPackage) { |
| 3125 | // Log the clients evicted |
| 3126 | logEvent(String8::format("CONNECT offline device %s client for package %s (PID %d)", cameraId, |
| 3127 | clientPackage, clientPid)); |
| 3128 | } |
| 3129 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3130 | void CameraService::logRejected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3131 | const char* clientPackage, const char* reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3132 | // Log the client rejected |
| 3133 | 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] | 3134 | cameraId, clientPackage, clientPid, reason)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3135 | } |
| 3136 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3137 | void CameraService::logTorchEvent(const char* cameraId, const char *torchState, int clientPid) { |
| 3138 | // Log torch event |
| 3139 | logEvent(String8::format("Torch for camera id %s turned %s for client PID %d", cameraId, |
| 3140 | torchState, clientPid)); |
| 3141 | } |
| 3142 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3143 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 3144 | const std::set<userid_t>& newUserIds) { |
| 3145 | String8 newUsers = toString(newUserIds); |
| 3146 | String8 oldUsers = toString(oldUserIds); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3147 | if (oldUsers.size() == 0) { |
| 3148 | oldUsers = "<None>"; |
| 3149 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3150 | // Log the new and old users |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3151 | 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] | 3152 | oldUsers.string(), newUsers.string())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) { |
| 3156 | // Log the device removal |
| 3157 | logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason)); |
| 3158 | } |
| 3159 | |
| 3160 | void CameraService::logDeviceAdded(const char* cameraId, const char* reason) { |
| 3161 | // Log the device removal |
| 3162 | logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason)); |
| 3163 | } |
| 3164 | |
| 3165 | void CameraService::logClientDied(int clientPid, const char* reason) { |
| 3166 | // Log the device removal |
| 3167 | 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] | 3168 | } |
| 3169 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3170 | void CameraService::logServiceError(const char* msg, int errorCode) { |
| 3171 | String8 curTime = getFormattedCurrentTime(); |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 3172 | 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] | 3173 | } |
| 3174 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3175 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 3176 | uint32_t flags) { |
| 3177 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3178 | // Permission checks |
| 3179 | switch (code) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3180 | case SHELL_COMMAND_TRANSACTION: { |
| 3181 | int in = data.readFileDescriptor(); |
| 3182 | int out = data.readFileDescriptor(); |
| 3183 | int err = data.readFileDescriptor(); |
| 3184 | int argc = data.readInt32(); |
| 3185 | Vector<String16> args; |
| 3186 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 3187 | args.add(data.readString16()); |
| 3188 | } |
| 3189 | sp<IBinder> unusedCallback; |
| 3190 | sp<IResultReceiver> resultReceiver; |
| 3191 | status_t status; |
| 3192 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 3193 | return status; |
| 3194 | } |
| 3195 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 3196 | return status; |
| 3197 | } |
| 3198 | status = shellCommand(in, out, err, args); |
| 3199 | if (resultReceiver != nullptr) { |
| 3200 | resultReceiver->send(status); |
| 3201 | } |
| 3202 | return NO_ERROR; |
| 3203 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | return BnCameraService::onTransact(code, data, reply, flags); |
| 3207 | } |
| 3208 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3209 | // We share the media players for shutter and recording sound for all clients. |
| 3210 | // A reference count is kept to determine when we will actually release the |
| 3211 | // media players. |
| 3212 | |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3213 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { |
| 3214 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 3215 | status_t error; |
| 3216 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 3217 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3218 | error = mp->prepare(); |
| 3219 | } |
| 3220 | if (error != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3221 | ALOGE("Failed to load CameraService sounds: %s", file); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3222 | mp->disconnect(); |
| 3223 | mp.clear(); |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3224 | return nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3225 | } |
| 3226 | return mp; |
| 3227 | } |
| 3228 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3229 | void CameraService::increaseSoundRef() { |
| 3230 | Mutex::Autolock lock(mSoundLock); |
| 3231 | mSoundRef++; |
| 3232 | } |
| 3233 | |
| 3234 | void CameraService::loadSoundLocked(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3235 | ATRACE_CALL(); |
| 3236 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3237 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); |
| 3238 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { |
| 3239 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); |
| 3240 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { |
| 3241 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 3242 | } |
| 3243 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) { |
| 3244 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); |
| 3245 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { |
| 3246 | mSoundPlayer[SOUND_RECORDING_START] = |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3247 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3248 | } |
| 3249 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { |
| 3250 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); |
| 3251 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { |
| 3252 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
| 3253 | } |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3254 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3255 | } |
| 3256 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3257 | void CameraService::decreaseSoundRef() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3258 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3259 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3260 | if (--mSoundRef) return; |
| 3261 | |
| 3262 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 3263 | if (mSoundPlayer[i] != 0) { |
| 3264 | mSoundPlayer[i]->disconnect(); |
| 3265 | mSoundPlayer[i].clear(); |
| 3266 | } |
| 3267 | } |
| 3268 | } |
| 3269 | |
| 3270 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3271 | ATRACE_CALL(); |
| 3272 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3273 | LOG1("playSound(%d)", kind); |
Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame] | 3274 | if (kind < 0 || kind >= NUM_SOUNDS) { |
| 3275 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); |
| 3276 | return; |
| 3277 | } |
| 3278 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3279 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3280 | loadSoundLocked(kind); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3281 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 3282 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 3283 | player->seekTo(0); |
| 3284 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | // ---------------------------------------------------------------------------- |
| 3289 | |
| 3290 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 3291 | const sp<ICameraClient>& cameraClient, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3292 | const String16& clientPackageName, bool systemNativeClient, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 3293 | const std::optional<String16>& clientFeatureId, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3294 | const String8& cameraIdStr, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3295 | int api1CameraId, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3296 | int clientPid, uid_t clientUid, |
| 3297 | int servicePid) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3298 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 3299 | IInterface::asBinder(cameraClient), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3300 | clientPackageName, systemNativeClient, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3301 | cameraIdStr, cameraFacing, sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3302 | clientPid, clientUid, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3303 | servicePid), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3304 | mCameraId(api1CameraId) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3305 | { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3306 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3307 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3308 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 3309 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3310 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3311 | cameraService->increaseSoundRef(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3312 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3313 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3314 | } |
| 3315 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3316 | // tear down the client |
| 3317 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3318 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3319 | mDestructionStarted = true; |
| 3320 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3321 | sCameraService->decreaseSoundRef(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3322 | // unconditionally disconnect. function is idempotent |
| 3323 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3324 | } |
| 3325 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3326 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; |
| 3327 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3328 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3329 | const sp<IBinder>& remoteCallback, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3330 | const String16& clientPackageName, bool nativeClient, |
| 3331 | const std::optional<String16>& clientFeatureId, const String8& cameraIdStr, |
| 3332 | int cameraFacing, int sensorOrientation, int clientPid, uid_t clientUid, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3333 | int servicePid): |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3334 | mDestructionStarted(false), |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3335 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), mOrientation(sensorOrientation), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3336 | mClientPackageName(clientPackageName), mSystemNativeClient(nativeClient), |
| 3337 | mClientFeatureId(clientFeatureId), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 3338 | mClientPid(clientPid), mClientUid(clientUid), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3339 | mServicePid(servicePid), |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3340 | mDisconnected(false), mUidIsTrusted(false), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3341 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3342 | mRemoteBinder(remoteCallback), |
| 3343 | mOpsActive(false), |
| 3344 | mOpsStreaming(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3345 | { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3346 | if (sCameraService == nullptr) { |
| 3347 | sCameraService = cameraService; |
| 3348 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 3349 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3350 | // There are 2 scenarios in which a client won't have AppOps operations |
| 3351 | // (both scenarios : native clients) |
| 3352 | // 1) It's an system native client*, the package name will be empty |
| 3353 | // and it will return from this function in the previous if condition |
| 3354 | // (This is the same as the previously existing behavior). |
| 3355 | // 2) It is a system native client, but its package name has been |
| 3356 | // modified for debugging, however it still must not use AppOps since |
| 3357 | // the package name is not a real one. |
| 3358 | // |
| 3359 | // * system native client - native client with UID < AID_APP_START. It |
| 3360 | // doesn't exclude clients not on the system partition. |
| 3361 | if (!mSystemNativeClient) { |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3362 | mAppOpsManager = std::make_unique<AppOpsManager>(); |
| 3363 | } |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3364 | |
| 3365 | mUidIsTrusted = isTrustedCallingUid(mClientUid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3369 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3370 | mDestructionStarted = true; |
| 3371 | } |
| 3372 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3373 | binder::Status CameraService::BasicClient::disconnect() { |
| 3374 | binder::Status res = Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3375 | if (mDisconnected) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3376 | return res; |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3377 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 3378 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3379 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3380 | sCameraService->removeByClient(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3381 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, String8(mClientPackageName)); |
Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 3382 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, |
| 3383 | mCameraIdStr.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3384 | |
| 3385 | sp<IBinder> remote = getRemote(); |
| 3386 | if (remote != nullptr) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3387 | remote->unlinkToDeath(sCameraService); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3388 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3389 | |
| 3390 | finishCameraOps(); |
Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 3391 | // Notify flashlight that a camera device is closed. |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3392 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); |
| 3393 | ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(), |
| 3394 | mClientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3395 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3396 | // client shouldn't be able to call into us anymore |
| 3397 | mClientPid = 0; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3398 | |
| 3399 | return res; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3400 | } |
| 3401 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3402 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 3403 | // No dumping of clients directly over Binder, |
| 3404 | // must go through CameraService::dump |
| 3405 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3406 | CameraThreadState::getCallingUid(), NULL, 0); |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3407 | return OK; |
| 3408 | } |
| 3409 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 3410 | status_t CameraService::BasicClient::startWatchingTags(const String8&, int) { |
| 3411 | // Can't watch tags directly, must go through CameraService::startWatchingTags |
| 3412 | return OK; |
| 3413 | } |
| 3414 | |
| 3415 | status_t CameraService::BasicClient::stopWatchingTags(int) { |
| 3416 | // Can't watch tags directly, must go through CameraService::stopWatchingTags |
| 3417 | return OK; |
| 3418 | } |
| 3419 | |
| 3420 | status_t CameraService::BasicClient::dumpWatchedEventsToVector(std::vector<std::string> &) { |
| 3421 | // Can't watch tags directly, must go through CameraService::dumpWatchedEventsToVector |
| 3422 | return OK; |
| 3423 | } |
| 3424 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3425 | String16 CameraService::BasicClient::getPackageName() const { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3426 | return mClientPackageName; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3427 | } |
| 3428 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3429 | int CameraService::BasicClient::getCameraFacing() const { |
| 3430 | return mCameraFacing; |
| 3431 | } |
| 3432 | |
| 3433 | int CameraService::BasicClient::getCameraOrientation() const { |
| 3434 | return mOrientation; |
| 3435 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3436 | |
| 3437 | int CameraService::BasicClient::getClientPid() const { |
| 3438 | return mClientPid; |
| 3439 | } |
| 3440 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3441 | uid_t CameraService::BasicClient::getClientUid() const { |
| 3442 | return mClientUid; |
| 3443 | } |
| 3444 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3445 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 3446 | // Defaults to API2. |
| 3447 | return level == API_2; |
| 3448 | } |
| 3449 | |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3450 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3451 | { |
| 3452 | Mutex::Autolock l(mAudioRestrictionLock); |
| 3453 | mAudioRestriction = mode; |
| 3454 | } |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3455 | sCameraService->updateAudioRestriction(); |
| 3456 | return OK; |
| 3457 | } |
| 3458 | |
| 3459 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3460 | return sCameraService->updateAudioRestriction(); |
| 3461 | } |
| 3462 | |
| 3463 | int32_t CameraService::BasicClient::getAudioRestriction() const { |
| 3464 | Mutex::Autolock l(mAudioRestrictionLock); |
| 3465 | return mAudioRestriction; |
| 3466 | } |
| 3467 | |
| 3468 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { |
| 3469 | switch (mode) { |
| 3470 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: |
| 3471 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: |
| 3472 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: |
| 3473 | return true; |
| 3474 | default: |
| 3475 | return false; |
| 3476 | } |
| 3477 | } |
| 3478 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3479 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { |
| 3480 | if (mode == AppOpsManager::MODE_ERRORED) { |
| 3481 | ALOGI("Camera %s: Access for \"%s\" has been revoked", |
| 3482 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 3483 | return PERMISSION_DENIED; |
| 3484 | } else if (!mUidIsTrusted && mode == AppOpsManager::MODE_IGNORED) { |
| 3485 | // If the calling Uid is trusted (a native service), the AppOpsManager could |
| 3486 | // return MODE_IGNORED. Do not treat such case as error. |
| 3487 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, |
| 3488 | mClientPackageName); |
| 3489 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3490 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3491 | if (!isUidActive || !isCameraPrivacyEnabled) { |
| 3492 | ALOGI("Camera %s: Access for \"%s\" has been restricted", |
| 3493 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 3494 | // Return the same error as for device policy manager rejection |
| 3495 | return -EACCES; |
| 3496 | } |
| 3497 | } |
| 3498 | return OK; |
| 3499 | } |
| 3500 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3501 | status_t CameraService::BasicClient::startCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3502 | ATRACE_CALL(); |
| 3503 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3504 | { |
| 3505 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3506 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3507 | } |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3508 | if (mAppOpsManager != nullptr) { |
| 3509 | // Notify app ops that the camera is not available |
| 3510 | mOpsCallback = new OpsCallback(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3511 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 3512 | mClientPackageName, mOpsCallback); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3513 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3514 | // Just check for camera acccess here on open - delay startOp until |
| 3515 | // camera frames start streaming in startCameraStreamingOps |
| 3516 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3517 | mClientPackageName); |
| 3518 | status_t res = handleAppOpMode(mode); |
| 3519 | if (res != OK) { |
| 3520 | return res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3521 | } |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 3522 | } |
| 3523 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3524 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3525 | |
| 3526 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3527 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3528 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3529 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid); |
| 3530 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3531 | // Notify listeners of camera open/close status |
| 3532 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); |
| 3533 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3534 | return OK; |
| 3535 | } |
| 3536 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3537 | status_t CameraService::BasicClient::startCameraStreamingOps() { |
| 3538 | ATRACE_CALL(); |
| 3539 | |
| 3540 | if (!mOpsActive) { |
| 3541 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3542 | return INVALID_OPERATION; |
| 3543 | } |
| 3544 | if (mOpsStreaming) { |
| 3545 | ALOGV("%s: Streaming already active!", __FUNCTION__); |
| 3546 | return OK; |
| 3547 | } |
| 3548 | |
| 3549 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", |
| 3550 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3551 | |
| 3552 | if (mAppOpsManager != nullptr) { |
| 3553 | int32_t mode = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, |
| 3554 | mClientPackageName, /*startIfModeDefault*/ false, mClientFeatureId, |
| 3555 | String16("start camera ") + String16(mCameraIdStr)); |
| 3556 | status_t res = handleAppOpMode(mode); |
| 3557 | if (res != OK) { |
| 3558 | return res; |
| 3559 | } |
| 3560 | } |
| 3561 | |
| 3562 | mOpsStreaming = true; |
| 3563 | |
| 3564 | return OK; |
| 3565 | } |
| 3566 | |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 3567 | status_t CameraService::BasicClient::noteAppOp() { |
| 3568 | ATRACE_CALL(); |
| 3569 | |
| 3570 | ALOGV("%s: Start camera noteAppOp, package name = %s, client UID = %d", |
| 3571 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3572 | |
| 3573 | // noteAppOp is only used for when camera mute is not supported, in order |
| 3574 | // to trigger the sensor privacy "Unblock" dialog |
| 3575 | if (mAppOpsManager != nullptr) { |
| 3576 | int32_t mode = mAppOpsManager->noteOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3577 | mClientPackageName, mClientFeatureId, |
| 3578 | String16("start camera ") + String16(mCameraIdStr)); |
| 3579 | status_t res = handleAppOpMode(mode); |
| 3580 | if (res != OK) { |
| 3581 | return res; |
| 3582 | } |
| 3583 | } |
| 3584 | |
| 3585 | return OK; |
| 3586 | } |
| 3587 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3588 | status_t CameraService::BasicClient::finishCameraStreamingOps() { |
| 3589 | ATRACE_CALL(); |
| 3590 | |
| 3591 | if (!mOpsActive) { |
| 3592 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3593 | return INVALID_OPERATION; |
| 3594 | } |
| 3595 | if (!mOpsStreaming) { |
| 3596 | ALOGV("%s: Streaming not active!", __FUNCTION__); |
| 3597 | return OK; |
| 3598 | } |
| 3599 | |
| 3600 | if (mAppOpsManager != nullptr) { |
| 3601 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3602 | mClientPackageName, mClientFeatureId); |
| 3603 | mOpsStreaming = false; |
| 3604 | } |
| 3605 | |
| 3606 | return OK; |
| 3607 | } |
| 3608 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3609 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3610 | ATRACE_CALL(); |
| 3611 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3612 | if (mOpsStreaming) { |
| 3613 | // Make sure we've notified everyone about camera stopping |
| 3614 | finishCameraStreamingOps(); |
| 3615 | } |
| 3616 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3617 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3618 | if (mOpsActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3619 | mOpsActive = false; |
| 3620 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3621 | // This function is called when a client disconnects. This should |
| 3622 | // release the camera, but actually only if it was in a proper |
| 3623 | // functional state, i.e. with status NOT_AVAILABLE |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3624 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3625 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3626 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3627 | // 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] | 3628 | sCameraService->updateStatus(StatusInternal::PRESENT, |
| 3629 | mCameraIdStr, rejected); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3630 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3631 | // Always stop watching, even if no camera op is active |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3632 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 3633 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3634 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3635 | mOpsCallback.clear(); |
| 3636 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3637 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid); |
| 3638 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3639 | // Notify listeners of camera open/close status |
| 3640 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); |
| 3641 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3642 | return OK; |
| 3643 | } |
| 3644 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3645 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3646 | ATRACE_CALL(); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3647 | if (mAppOpsManager == nullptr) { |
| 3648 | return; |
| 3649 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3650 | // TODO : add offline camera session case |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3651 | if (op != AppOpsManager::OP_CAMERA) { |
| 3652 | ALOGW("Unexpected app ops notification received: %d", op); |
| 3653 | return; |
| 3654 | } |
| 3655 | |
| 3656 | int32_t res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3657 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3658 | mClientUid, mClientPackageName); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3659 | ALOGV("checkOp returns: %d, %s ", res, |
| 3660 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 3661 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 3662 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 3663 | "UNKNOWN"); |
| 3664 | |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3665 | if (res == AppOpsManager::MODE_ERRORED) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3666 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(), |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3667 | String8(mClientPackageName).string()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3668 | block(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3669 | } else if (res == AppOpsManager::MODE_IGNORED) { |
| 3670 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3671 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3672 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3673 | ALOGI("Camera %s: Access for \"%s\" has been restricted, isUidTrusted %d, isUidActive %d", |
| 3674 | mCameraIdStr.string(), String8(mClientPackageName).string(), |
| 3675 | mUidIsTrusted, isUidActive); |
| 3676 | // If the calling Uid is trusted (a native service), or the client Uid is active (WAR for |
| 3677 | // b/175320666), the AppOpsManager could return MODE_IGNORED. Do not treat such cases as |
| 3678 | // error. |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 3679 | if (!mUidIsTrusted) { |
| 3680 | if (isUidActive && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 3681 | setCameraMute(true); |
| 3682 | } else if (!isUidActive |
| 3683 | || (isCameraPrivacyEnabled && !supportsCameraMute())) { |
| 3684 | block(); |
| 3685 | } |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3686 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3687 | } else if (res == AppOpsManager::MODE_ALLOWED) { |
| 3688 | setCameraMute(sCameraService->mOverrideCameraMuteMode); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3689 | } |
| 3690 | } |
| 3691 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3692 | void CameraService::BasicClient::block() { |
| 3693 | ATRACE_CALL(); |
| 3694 | |
| 3695 | // Reset the client PID to allow server-initiated disconnect, |
| 3696 | // and to prevent further calls by client. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3697 | mClientPid = CameraThreadState::getCallingPid(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3698 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 3699 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); |
| 3700 | disconnect(); |
| 3701 | } |
| 3702 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3703 | // ---------------------------------------------------------------------------- |
| 3704 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3705 | void CameraService::Client::notifyError(int32_t errorCode, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3706 | const CaptureResultExtras& resultExtras) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 3707 | (void) resultExtras; |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3708 | if (mRemoteCallback != NULL) { |
Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 3709 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; |
| 3710 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { |
| 3711 | api1ErrorCode = CAMERA_ERROR_DISABLED; |
| 3712 | } |
| 3713 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3714 | } else { |
| 3715 | ALOGE("mRemoteCallback is NULL!!"); |
| 3716 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3717 | } |
| 3718 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3719 | // NOTE: function is idempotent |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3720 | binder::Status CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3721 | ALOGV("Client::disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3722 | return BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 3723 | } |
| 3724 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3725 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 3726 | return level == API_1; |
| 3727 | } |
| 3728 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3729 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 3730 | mClient(client) { |
| 3731 | } |
| 3732 | |
| 3733 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 3734 | const String16& packageName) { |
| 3735 | sp<BasicClient> client = mClient.promote(); |
| 3736 | if (client != NULL) { |
| 3737 | client->opChanged(op, packageName); |
| 3738 | } |
| 3739 | } |
| 3740 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3741 | // ---------------------------------------------------------------------------- |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3742 | // UidPolicy |
| 3743 | // ---------------------------------------------------------------------------- |
| 3744 | |
| 3745 | void CameraService::UidPolicy::registerSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3746 | Mutex::Autolock _l(mUidLock); |
| 3747 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3748 | if (mRegistered) return; |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3749 | status_t res = mAm.linkToDeath(this); |
| 3750 | mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3751 | | ActivityManager::UID_OBSERVER_IDLE |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3752 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE |
| 3753 | | ActivityManager::UID_OBSERVER_PROC_OOM_ADJ, |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3754 | ActivityManager::PROCESS_STATE_UNKNOWN, |
| 3755 | String16("cameraserver")); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3756 | if (res == OK) { |
| 3757 | mRegistered = true; |
| 3758 | ALOGV("UidPolicy: Registered with ActivityManager"); |
| 3759 | } |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | void CameraService::UidPolicy::unregisterSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3763 | Mutex::Autolock _l(mUidLock); |
| 3764 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3765 | mAm.unregisterUidObserver(this); |
| 3766 | mAm.unlinkToDeath(this); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3767 | mRegistered = false; |
| 3768 | mActiveUids.clear(); |
| 3769 | ALOGV("UidPolicy: Unregistered with ActivityManager"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { |
| 3773 | onUidIdle(uid, disabled); |
| 3774 | } |
| 3775 | |
| 3776 | void CameraService::UidPolicy::onUidActive(uid_t uid) { |
| 3777 | Mutex::Autolock _l(mUidLock); |
| 3778 | mActiveUids.insert(uid); |
| 3779 | } |
| 3780 | |
| 3781 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { |
| 3782 | bool deleted = false; |
| 3783 | { |
| 3784 | Mutex::Autolock _l(mUidLock); |
| 3785 | if (mActiveUids.erase(uid) > 0) { |
| 3786 | deleted = true; |
| 3787 | } |
| 3788 | } |
| 3789 | if (deleted) { |
| 3790 | sp<CameraService> service = mService.promote(); |
| 3791 | if (service != nullptr) { |
| 3792 | service->blockClientsForUid(uid); |
| 3793 | } |
| 3794 | } |
| 3795 | } |
| 3796 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3797 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 3798 | int64_t procStateSeq __unused, int32_t capability __unused) { |
Austin Borger | c5585dc | 2022-05-12 00:48:17 +0000 | [diff] [blame] | 3799 | bool procStateChange = false; |
| 3800 | { |
| 3801 | Mutex::Autolock _l(mUidLock); |
| 3802 | if (mMonitoredUids.find(uid) != mMonitoredUids.end() && |
| 3803 | mMonitoredUids[uid].procState != procState) { |
| 3804 | mMonitoredUids[uid].procState = procState; |
| 3805 | procStateChange = true; |
| 3806 | } |
| 3807 | } |
| 3808 | |
| 3809 | if (procStateChange) { |
| 3810 | sp<CameraService> service = mService.promote(); |
| 3811 | if (service != nullptr) { |
| 3812 | service->notifyMonitoredUids(); |
| 3813 | } |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3814 | } |
| 3815 | } |
| 3816 | |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3817 | void CameraService::UidPolicy::onUidProcAdjChanged(uid_t uid) { |
| 3818 | bool procAdjChange = false; |
| 3819 | { |
| 3820 | Mutex::Autolock _l(mUidLock); |
| 3821 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
| 3822 | procAdjChange = true; |
| 3823 | } |
| 3824 | } |
| 3825 | |
| 3826 | if (procAdjChange) { |
| 3827 | sp<CameraService> service = mService.promote(); |
| 3828 | if (service != nullptr) { |
| 3829 | service->notifyMonitoredUids(); |
| 3830 | } |
| 3831 | } |
| 3832 | } |
| 3833 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3834 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid) { |
| 3835 | Mutex::Autolock _l(mUidLock); |
| 3836 | auto it = mMonitoredUids.find(uid); |
| 3837 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3838 | it->second.refCount++; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3839 | } else { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3840 | MonitoredUid monitoredUid; |
| 3841 | monitoredUid.procState = ActivityManager::PROCESS_STATE_NONEXISTENT; |
| 3842 | monitoredUid.refCount = 1; |
| 3843 | mMonitoredUids.emplace(std::pair<uid_t, MonitoredUid>(uid, monitoredUid)); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3844 | } |
| 3845 | } |
| 3846 | |
| 3847 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid) { |
| 3848 | Mutex::Autolock _l(mUidLock); |
| 3849 | auto it = mMonitoredUids.find(uid); |
| 3850 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3851 | it->second.refCount--; |
| 3852 | if (it->second.refCount == 0) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3853 | mMonitoredUids.erase(it); |
| 3854 | } |
| 3855 | } else { |
| 3856 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 3857 | } |
| 3858 | } |
| 3859 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3860 | bool CameraService::UidPolicy::isUidActive(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3861 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3862 | return isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3863 | } |
| 3864 | |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3865 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; |
| 3866 | static const int64_t kPollUidActiveTimeoutMillis = 50; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3867 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3868 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3869 | // Non-app UIDs are considered always active |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3870 | // If activity manager is unreachable, assume everything is active |
| 3871 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3872 | return true; |
| 3873 | } |
| 3874 | auto it = mOverrideUids.find(uid); |
| 3875 | if (it != mOverrideUids.end()) { |
| 3876 | return it->second; |
| 3877 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3878 | bool active = mActiveUids.find(uid) != mActiveUids.end(); |
| 3879 | if (!active) { |
| 3880 | // We want active UIDs to always access camera with their first attempt since |
| 3881 | // there is no guarantee the app is robustly written and would retry getting |
| 3882 | // the camera on failure. The inverse case is not a problem as we would take |
| 3883 | // camera away soon once we get the callback that the uid is no longer active. |
| 3884 | ActivityManager am; |
| 3885 | // Okay to access with a lock held as UID changes are dispatched without |
| 3886 | // a lock and we are a higher level component. |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3887 | int64_t startTimeMillis = 0; |
| 3888 | do { |
| 3889 | // TODO: Fix this b/109950150! |
| 3890 | // Okay this is a hack. There is a race between the UID turning active and |
| 3891 | // activity being resumed. The proper fix is very risky, so we temporary add |
| 3892 | // some polling which should happen pretty rarely anyway as the race is hard |
| 3893 | // to hit. |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3894 | active = mActiveUids.find(uid) != mActiveUids.end(); |
Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 3895 | if (!active) active = am.isUidActive(uid, callingPackage); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3896 | if (active) { |
| 3897 | break; |
| 3898 | } |
| 3899 | if (startTimeMillis <= 0) { |
| 3900 | startTimeMillis = uptimeMillis(); |
| 3901 | } |
| 3902 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3903 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3904 | if (remainingTimeMillis <= 0) { |
| 3905 | break; |
| 3906 | } |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3907 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); |
| 3908 | |
| 3909 | mUidLock.unlock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3910 | usleep(remainingTimeMillis * 1000); |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3911 | mUidLock.lock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3912 | } while (true); |
| 3913 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3914 | if (active) { |
| 3915 | // Now that we found out the UID is actually active, cache that |
| 3916 | mActiveUids.insert(uid); |
| 3917 | } |
| 3918 | } |
| 3919 | return active; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3920 | } |
| 3921 | |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 3922 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { |
| 3923 | Mutex::Autolock _l(mUidLock); |
| 3924 | return getProcStateLocked(uid); |
| 3925 | } |
| 3926 | |
| 3927 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { |
| 3928 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 3929 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3930 | procState = mMonitoredUids[uid].procState; |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 3931 | } |
| 3932 | return procState; |
| 3933 | } |
| 3934 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3935 | void CameraService::UidPolicy::UidPolicy::addOverrideUid(uid_t uid, |
| 3936 | String16 callingPackage, bool active) { |
| 3937 | updateOverrideUid(uid, callingPackage, active, true); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3938 | } |
| 3939 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3940 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, String16 callingPackage) { |
| 3941 | updateOverrideUid(uid, callingPackage, false, false); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3942 | } |
| 3943 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3944 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 3945 | Mutex::Autolock _l(mUidLock); |
| 3946 | ALOGV("UidPolicy: ActivityManager has died"); |
| 3947 | mRegistered = false; |
| 3948 | mActiveUids.clear(); |
| 3949 | } |
| 3950 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3951 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, String16 callingPackage, |
| 3952 | bool active, bool insert) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3953 | bool wasActive = false; |
| 3954 | bool isActive = false; |
| 3955 | { |
| 3956 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3957 | wasActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3958 | mOverrideUids.erase(uid); |
| 3959 | if (insert) { |
| 3960 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 3961 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3962 | isActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3963 | } |
| 3964 | if (wasActive != isActive && !isActive) { |
| 3965 | sp<CameraService> service = mService.promote(); |
| 3966 | if (service != nullptr) { |
| 3967 | service->blockClientsForUid(uid); |
| 3968 | } |
| 3969 | } |
| 3970 | } |
| 3971 | |
| 3972 | // ---------------------------------------------------------------------------- |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3973 | // SensorPrivacyPolicy |
| 3974 | // ---------------------------------------------------------------------------- |
| 3975 | void CameraService::SensorPrivacyPolicy::registerSelf() { |
| 3976 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3977 | if (mRegistered) { |
| 3978 | return; |
| 3979 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3980 | hasCameraPrivacyFeature(); // Called so the result is cached |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 3981 | mSpm.addSensorPrivacyListener(this); |
| 3982 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); |
| 3983 | status_t res = mSpm.linkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3984 | if (res == OK) { |
| 3985 | mRegistered = true; |
| 3986 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); |
| 3987 | } |
| 3988 | } |
| 3989 | |
| 3990 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { |
| 3991 | Mutex::Autolock _l(mSensorPrivacyLock); |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 3992 | mSpm.removeSensorPrivacyListener(this); |
| 3993 | mSpm.unlinkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3994 | mRegistered = false; |
| 3995 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); |
| 3996 | } |
| 3997 | |
| 3998 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
| 3999 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4000 | return mSensorPrivacyEnabled; |
| 4001 | } |
| 4002 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4003 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled() { |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4004 | if (!hasCameraPrivacyFeature()) { |
| 4005 | return false; |
| 4006 | } |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4007 | return mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4008 | } |
| 4009 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4010 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged( |
| 4011 | int toggleType __unused, int sensor __unused, bool enabled) { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4012 | { |
| 4013 | Mutex::Autolock _l(mSensorPrivacyLock); |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4014 | mSensorPrivacyEnabled = mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4015 | } |
| 4016 | // 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] | 4017 | if (enabled) { |
| 4018 | sp<CameraService> service = mService.promote(); |
| 4019 | if (service != nullptr) { |
| 4020 | service->blockAllClients(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4021 | } |
| 4022 | } |
| 4023 | return binder::Status::ok(); |
| 4024 | } |
| 4025 | |
| 4026 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 4027 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 4028 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); |
| 4029 | mRegistered = false; |
| 4030 | } |
| 4031 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4032 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4033 | bool supportsSoftwareToggle = mSpm.supportsSensorToggle( |
| 4034 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 4035 | bool supportsHardwareToggle = mSpm.supportsSensorToggle( |
| 4036 | SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 4037 | return supportsSoftwareToggle || supportsHardwareToggle; |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4038 | } |
| 4039 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4040 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4041 | // CameraState |
| 4042 | // ---------------------------------------------------------------------------- |
| 4043 | |
| 4044 | CameraService::CameraState::CameraState(const String8& id, int cost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4045 | const std::set<String8>& conflicting, SystemCameraKind systemCameraKind, |
| 4046 | const std::vector<std::string>& physicalCameras) : mId(id), |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4047 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4048 | mSystemCameraKind(systemCameraKind), mPhysicalCameras(physicalCameras) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4049 | |
| 4050 | CameraService::CameraState::~CameraState() {} |
| 4051 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4052 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4053 | Mutex::Autolock lock(mStatusLock); |
| 4054 | return mStatus; |
| 4055 | } |
| 4056 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4057 | std::vector<String8> CameraService::CameraState::getUnavailablePhysicalIds() const { |
| 4058 | Mutex::Autolock lock(mStatusLock); |
| 4059 | std::vector<String8> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); |
| 4060 | return res; |
| 4061 | } |
| 4062 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4063 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 4064 | return mShimParams; |
| 4065 | } |
| 4066 | |
| 4067 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 4068 | mShimParams = params; |
| 4069 | } |
| 4070 | |
| 4071 | int CameraService::CameraState::getCost() const { |
| 4072 | return mCost; |
| 4073 | } |
| 4074 | |
| 4075 | std::set<String8> CameraService::CameraState::getConflicting() const { |
| 4076 | return mConflicting; |
| 4077 | } |
| 4078 | |
| 4079 | String8 CameraService::CameraState::getId() const { |
| 4080 | return mId; |
| 4081 | } |
| 4082 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4083 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { |
| 4084 | return mSystemCameraKind; |
| 4085 | } |
| 4086 | |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4087 | bool CameraService::CameraState::containsPhysicalCamera(const std::string& physicalCameraId) const { |
| 4088 | return std::find(mPhysicalCameras.begin(), mPhysicalCameras.end(), physicalCameraId) |
| 4089 | != mPhysicalCameras.end(); |
| 4090 | } |
| 4091 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4092 | bool CameraService::CameraState::addUnavailablePhysicalId(const String8& physicalId) { |
| 4093 | Mutex::Autolock lock(mStatusLock); |
| 4094 | auto result = mUnavailablePhysicalIds.insert(physicalId); |
| 4095 | return result.second; |
| 4096 | } |
| 4097 | |
| 4098 | bool CameraService::CameraState::removeUnavailablePhysicalId(const String8& physicalId) { |
| 4099 | Mutex::Autolock lock(mStatusLock); |
| 4100 | auto count = mUnavailablePhysicalIds.erase(physicalId); |
| 4101 | return count > 0; |
| 4102 | } |
| 4103 | |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 4104 | void CameraService::CameraState::setClientPackage(const String8& clientPackage) { |
| 4105 | Mutex::Autolock lock(mStatusLock); |
| 4106 | mClientPackage = clientPackage; |
| 4107 | } |
| 4108 | |
| 4109 | String8 CameraService::CameraState::getClientPackage() const { |
| 4110 | Mutex::Autolock lock(mStatusLock); |
| 4111 | return mClientPackage; |
| 4112 | } |
| 4113 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4114 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4115 | // ClientEventListener |
| 4116 | // ---------------------------------------------------------------------------- |
| 4117 | |
| 4118 | void CameraService::ClientEventListener::onClientAdded( |
| 4119 | const resource_policy::ClientDescriptor<String8, |
| 4120 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 4121 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4122 | if (basicClient.get() != nullptr) { |
| 4123 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 4124 | notifier.noteStartCamera(descriptor.getKey(), |
| 4125 | static_cast<int>(basicClient->getClientUid())); |
| 4126 | } |
| 4127 | } |
| 4128 | |
| 4129 | void CameraService::ClientEventListener::onClientRemoved( |
| 4130 | const resource_policy::ClientDescriptor<String8, |
| 4131 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 4132 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4133 | if (basicClient.get() != nullptr) { |
| 4134 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 4135 | notifier.noteStopCamera(descriptor.getKey(), |
| 4136 | static_cast<int>(basicClient->getClientUid())); |
| 4137 | } |
| 4138 | } |
| 4139 | |
| 4140 | |
| 4141 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4142 | // CameraClientManager |
| 4143 | // ---------------------------------------------------------------------------- |
| 4144 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4145 | CameraService::CameraClientManager::CameraClientManager() { |
| 4146 | setListener(std::make_shared<ClientEventListener>()); |
| 4147 | } |
| 4148 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4149 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 4150 | |
| 4151 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
| 4152 | const String8& id) const { |
| 4153 | auto descriptor = get(id); |
| 4154 | if (descriptor == nullptr) { |
| 4155 | return sp<BasicClient>{nullptr}; |
| 4156 | } |
| 4157 | return descriptor->getValue(); |
| 4158 | } |
| 4159 | |
| 4160 | String8 CameraService::CameraClientManager::toString() const { |
| 4161 | auto all = getAll(); |
| 4162 | String8 ret("["); |
| 4163 | bool hasAny = false; |
| 4164 | for (auto& i : all) { |
| 4165 | hasAny = true; |
| 4166 | String8 key = i->getKey(); |
| 4167 | int32_t cost = i->getCost(); |
| 4168 | int32_t pid = i->getOwnerId(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4169 | int32_t score = i->getPriority().getScore(); |
| 4170 | int32_t state = i->getPriority().getState(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4171 | auto conflicting = i->getConflicting(); |
| 4172 | auto clientSp = i->getValue(); |
| 4173 | String8 packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 4174 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4175 | if (clientSp.get() != nullptr) { |
| 4176 | packageName = String8{clientSp->getPackageName()}; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4177 | uid_t clientUid = clientSp->getClientUid(); |
| 4178 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4179 | } |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4180 | ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" |
| 4181 | PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4182 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4183 | if (clientSp.get() != nullptr) { |
| 4184 | ret.appendFormat("User Id: %d, ", clientUserId); |
| 4185 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4186 | if (packageName.size() != 0) { |
| 4187 | ret.appendFormat("Client Package Name: %s", packageName.string()); |
| 4188 | } |
| 4189 | |
| 4190 | ret.append(", Conflicting Client Devices: {"); |
| 4191 | for (auto& j : conflicting) { |
| 4192 | ret.appendFormat("%s, ", j.string()); |
| 4193 | } |
| 4194 | ret.append("})"); |
| 4195 | } |
| 4196 | if (hasAny) ret.append("\n"); |
| 4197 | ret.append("]\n"); |
| 4198 | return ret; |
| 4199 | } |
| 4200 | |
| 4201 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
| 4202 | const String8& key, const sp<BasicClient>& value, int32_t cost, |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4203 | const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4204 | int32_t state, int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4205 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4206 | int32_t score_adj = systemNativeClient ? kSystemNativeClientScore : score; |
| 4207 | int32_t state_adj = systemNativeClient ? kSystemNativeClientState: state; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 4208 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4209 | return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>( |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4210 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, |
| 4211 | systemNativeClient, oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 4215 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4216 | int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4217 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4218 | partial->getConflicting(), partial->getPriority().getScore(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4219 | partial->getOwnerId(), partial->getPriority().getState(), oomScoreOffset, |
| 4220 | systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | // ---------------------------------------------------------------------------- |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4224 | // InjectionStatusListener |
| 4225 | // ---------------------------------------------------------------------------- |
| 4226 | |
| 4227 | void CameraService::InjectionStatusListener::addListener( |
| 4228 | const sp<ICameraInjectionCallback>& callback) { |
| 4229 | Mutex::Autolock lock(mListenerLock); |
| 4230 | if (mCameraInjectionCallback) return; |
| 4231 | status_t res = IInterface::asBinder(callback)->linkToDeath(this); |
| 4232 | if (res == OK) { |
| 4233 | mCameraInjectionCallback = callback; |
| 4234 | } |
| 4235 | } |
| 4236 | |
| 4237 | void CameraService::InjectionStatusListener::removeListener() { |
| 4238 | Mutex::Autolock lock(mListenerLock); |
| 4239 | if (mCameraInjectionCallback == nullptr) { |
| 4240 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 4241 | return; |
| 4242 | } |
| 4243 | IInterface::asBinder(mCameraInjectionCallback)->unlinkToDeath(this); |
| 4244 | mCameraInjectionCallback = nullptr; |
| 4245 | } |
| 4246 | |
| 4247 | void CameraService::InjectionStatusListener::notifyInjectionError( |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4248 | String8 injectedCamId, status_t err) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4249 | if (mCameraInjectionCallback == nullptr) { |
| 4250 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 4251 | return; |
| 4252 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4253 | |
| 4254 | switch (err) { |
| 4255 | case -ENODEV: |
| 4256 | mCameraInjectionCallback->onInjectionError( |
| 4257 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4258 | ALOGE("No camera device with ID \"%s\" currently available!", |
| 4259 | injectedCamId.string()); |
| 4260 | break; |
| 4261 | case -EBUSY: |
| 4262 | mCameraInjectionCallback->onInjectionError( |
| 4263 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4264 | ALOGE("Higher-priority client using camera, ID \"%s\" currently unavailable!", |
| 4265 | injectedCamId.string()); |
| 4266 | break; |
| 4267 | case DEAD_OBJECT: |
| 4268 | mCameraInjectionCallback->onInjectionError( |
| 4269 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4270 | ALOGE("Camera ID \"%s\" object is dead!", |
| 4271 | injectedCamId.string()); |
| 4272 | break; |
| 4273 | case INVALID_OPERATION: |
| 4274 | mCameraInjectionCallback->onInjectionError( |
| 4275 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4276 | ALOGE("Camera ID \"%s\" encountered an operating or internal error!", |
| 4277 | injectedCamId.string()); |
| 4278 | break; |
| 4279 | case UNKNOWN_TRANSACTION: |
| 4280 | mCameraInjectionCallback->onInjectionError( |
| 4281 | ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED); |
| 4282 | ALOGE("Camera ID \"%s\" method doesn't support!", |
| 4283 | injectedCamId.string()); |
| 4284 | break; |
| 4285 | default: |
| 4286 | mCameraInjectionCallback->onInjectionError( |
| 4287 | ICameraInjectionCallback::ERROR_INJECTION_INVALID_ERROR); |
| 4288 | ALOGE("Unexpected error %s (%d) opening camera \"%s\"!", |
| 4289 | strerror(-err), err, injectedCamId.string()); |
| 4290 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4291 | } |
| 4292 | |
| 4293 | void CameraService::InjectionStatusListener::binderDied( |
| 4294 | const wp<IBinder>& /*who*/) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4295 | ALOGV("InjectionStatusListener: ICameraInjectionCallback has died"); |
| 4296 | auto parent = mParent.promote(); |
| 4297 | if (parent != nullptr) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4298 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 4299 | if (clientDescriptor != nullptr) { |
| 4300 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 4301 | baseClientPtr->stopInjection(); |
| 4302 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 4303 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4304 | } |
| 4305 | } |
| 4306 | |
| 4307 | // ---------------------------------------------------------------------------- |
| 4308 | // CameraInjectionSession |
| 4309 | // ---------------------------------------------------------------------------- |
| 4310 | |
| 4311 | binder::Status CameraService::CameraInjectionSession::stopInjection() { |
| 4312 | Mutex::Autolock lock(mInjectionSessionLock); |
| 4313 | auto parent = mParent.promote(); |
| 4314 | if (parent == nullptr) { |
| 4315 | ALOGE("CameraInjectionSession: Parent is gone"); |
| 4316 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SERVICE, |
| 4317 | "Camera service encountered error"); |
| 4318 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4319 | |
| 4320 | status_t res = NO_ERROR; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4321 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 4322 | if (clientDescriptor != nullptr) { |
| 4323 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 4324 | res = baseClientPtr->stopInjection(); |
| 4325 | if (res != OK) { |
| 4326 | ALOGE("CameraInjectionSession: Failed to stop the injection camera!" |
| 4327 | " ret != NO_ERROR: %d", res); |
| 4328 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SESSION, |
| 4329 | "Camera session encountered error"); |
| 4330 | } |
| 4331 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 4332 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4333 | return binder::Status::ok(); |
| 4334 | } |
| 4335 | |
| 4336 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4337 | |
| 4338 | static const int kDumpLockRetries = 50; |
| 4339 | static const int kDumpLockSleep = 60000; |
| 4340 | |
| 4341 | static bool tryLock(Mutex& mutex) |
| 4342 | { |
| 4343 | bool locked = false; |
| 4344 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 4345 | if (mutex.tryLock() == NO_ERROR) { |
| 4346 | locked = true; |
| 4347 | break; |
| 4348 | } |
| 4349 | usleep(kDumpLockSleep); |
| 4350 | } |
| 4351 | return locked; |
| 4352 | } |
| 4353 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4354 | void CameraService::cacheDump() { |
| 4355 | if (mMemFd != -1) { |
| 4356 | const Vector<String16> args; |
| 4357 | ATRACE_CALL(); |
| 4358 | // Acquiring service lock here will avoid the deadlock since |
| 4359 | // cacheDump will not be called during the second disconnect. |
| 4360 | Mutex::Autolock lock(mServiceLock); |
| 4361 | |
| 4362 | Mutex::Autolock l(mCameraStatesLock); |
| 4363 | // Start collecting the info for open sessions and store it in temp file. |
| 4364 | for (const auto& state : mCameraStates) { |
| 4365 | String8 cameraId = state.first; |
| 4366 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 4367 | if (clientDescriptor != nullptr) { |
| 4368 | dprintf(mMemFd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
| 4369 | // Log the current open session info before device is disconnected. |
| 4370 | dumpOpenSessionClientLogs(mMemFd, args, cameraId); |
| 4371 | } |
| 4372 | } |
| 4373 | } |
| 4374 | } |
| 4375 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4376 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4377 | ATRACE_CALL(); |
| 4378 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4379 | if (checkCallingPermission(sDumpPermission) == false) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4380 | 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] | 4381 | CameraThreadState::getCallingPid(), |
| 4382 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4383 | return NO_ERROR; |
| 4384 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4385 | bool locked = tryLock(mServiceLock); |
| 4386 | // failed to lock - CameraService is probably deadlocked |
| 4387 | if (!locked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4388 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4389 | } |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4390 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4391 | if (!mInitialized) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4392 | dprintf(fd, "!! No camera HAL available !!\n"); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 4393 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4394 | // Dump event log for error information |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4395 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4396 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4397 | if (locked) mServiceLock.unlock(); |
| 4398 | return NO_ERROR; |
| 4399 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4400 | dprintf(fd, "\n== Service global info: ==\n\n"); |
| 4401 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4402 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 4403 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", |
| 4404 | mNormalDeviceIdsWithoutSystemCamera.size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4405 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { |
| 4406 | dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); |
| 4407 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4408 | String8 activeClientString = mActiveClientManager.toString(); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4409 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string()); |
| 4410 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4411 | |
| 4412 | dumpEventLog(fd); |
| 4413 | |
| 4414 | bool stateLocked = tryLock(mCameraStatesLock); |
| 4415 | if (!stateLocked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4416 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4417 | } |
| 4418 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 4419 | int argSize = args.size(); |
| 4420 | for (int i = 0; i < argSize; i++) { |
| 4421 | if (args[i] == TagMonitor::kMonitorOption) { |
| 4422 | if (i + 1 < argSize) { |
| 4423 | mMonitorTags = String8(args[i + 1]); |
| 4424 | } |
| 4425 | break; |
| 4426 | } |
| 4427 | } |
| 4428 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4429 | for (auto& state : mCameraStates) { |
| 4430 | String8 cameraId = state.first; |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4431 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4432 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4433 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4434 | CameraParameters p = state.second->getShimParams(); |
| 4435 | if (!p.isEmpty()) { |
| 4436 | dprintf(fd, " Camera1 API shim is using parameters:\n "); |
| 4437 | p.dump(fd, args); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4441 | if (clientDescriptor != nullptr) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4442 | // log the current open session info |
| 4443 | dumpOpenSessionClientLogs(fd, args, cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4444 | } else { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4445 | dumpClosedSessionClientLogs(fd, cameraId); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4446 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4447 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4448 | } |
| 4449 | |
| 4450 | if (stateLocked) mCameraStatesLock.unlock(); |
| 4451 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4452 | if (locked) mServiceLock.unlock(); |
| 4453 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 4454 | mCameraProviderManager->dump(fd, args); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4455 | |
| 4456 | dprintf(fd, "\n== Vendor tags: ==\n\n"); |
| 4457 | |
| 4458 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 4459 | if (desc == NULL) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 4460 | sp<VendorTagDescriptorCache> cache = |
| 4461 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 4462 | if (cache == NULL) { |
| 4463 | dprintf(fd, "No vendor tags.\n"); |
| 4464 | } else { |
| 4465 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 4466 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4467 | } else { |
| 4468 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 4469 | } |
| 4470 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4471 | // Dump camera traces if there were any |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4472 | dprintf(fd, "\n"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4473 | camera3::CameraTraces::dump(fd); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4474 | |
| 4475 | // Process dump arguments, if any |
| 4476 | int n = args.size(); |
| 4477 | String16 verboseOption("-v"); |
| 4478 | String16 unreachableOption("--unreachable"); |
| 4479 | for (int i = 0; i < n; i++) { |
| 4480 | if (args[i] == verboseOption) { |
| 4481 | // change logging level |
| 4482 | if (i + 1 >= n) continue; |
| 4483 | String8 levelStr(args[i+1]); |
| 4484 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4485 | dprintf(fd, "\nSetting log level to %d.\n", level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4486 | setLogLevel(level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4487 | } else if (args[i] == unreachableOption) { |
| 4488 | // Dump memory analysis |
| 4489 | // TODO - should limit be an argument parameter? |
| 4490 | UnreachableMemoryInfo info; |
| 4491 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); |
| 4492 | if (!success) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4493 | dprintf(fd, "\n== Unable to dump unreachable memory. " |
| 4494 | "Try disabling SELinux enforcement. ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4495 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4496 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4497 | std::string s = info.ToString(/*log_contents*/ true); |
| 4498 | write(fd, s.c_str(), s.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4499 | } |
| 4500 | } |
| 4501 | } |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4502 | |
| 4503 | bool serviceLocked = tryLock(mServiceLock); |
| 4504 | |
| 4505 | // Dump info from previous open sessions. |
| 4506 | // Reposition the offset to beginning of the file before reading |
| 4507 | |
| 4508 | if ((mMemFd >= 0) && (lseek(mMemFd, 0, SEEK_SET) != -1)) { |
| 4509 | dprintf(fd, "\n**********Dumpsys from previous open session**********\n"); |
| 4510 | ssize_t size_read; |
| 4511 | char buf[4096]; |
| 4512 | while ((size_read = read(mMemFd, buf, (sizeof(buf) - 1))) > 0) { |
| 4513 | // Read data from file to a small buffer and write it to fd. |
| 4514 | write(fd, buf, size_read); |
| 4515 | if (size_read == -1) { |
| 4516 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 4517 | break; |
| 4518 | } |
| 4519 | } |
| 4520 | dprintf(fd, "\n**********End of Dumpsys from previous open session**********\n"); |
| 4521 | } else { |
| 4522 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 4523 | } |
| 4524 | |
| 4525 | if (serviceLocked) mServiceLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4526 | return NO_ERROR; |
| 4527 | } |
| 4528 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4529 | void CameraService::dumpOpenSessionClientLogs(int fd, |
| 4530 | const Vector<String16>& args, const String8& cameraId) { |
| 4531 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Rucha Katakwar | 71a0e05 | 2022-04-07 15:44:18 -0700 | [diff] [blame] | 4532 | dprintf(fd, " %s : Device %s is open. Client instance dump:\n", |
| 4533 | getFormattedCurrentTime().string(), |
| 4534 | cameraId.string()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4535 | dprintf(fd, " Client priority score: %d state: %d\n", |
| 4536 | clientDescriptor->getPriority().getScore(), |
| 4537 | clientDescriptor->getPriority().getState()); |
| 4538 | dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 4539 | |
| 4540 | auto client = clientDescriptor->getValue(); |
| 4541 | dprintf(fd, " Client package: %s\n", |
| 4542 | String8(client->getPackageName()).string()); |
| 4543 | |
| 4544 | client->dumpClient(fd, args); |
| 4545 | } |
| 4546 | |
| 4547 | void CameraService::dumpClosedSessionClientLogs(int fd, const String8& cameraId) { |
| 4548 | dprintf(fd, " Device %s is closed, no client instance\n", |
| 4549 | cameraId.string()); |
| 4550 | } |
| 4551 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4552 | void CameraService::dumpEventLog(int fd) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4553 | 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] | 4554 | |
| 4555 | Mutex::Autolock l(mLogLock); |
| 4556 | for (const auto& msg : mEventLog) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4557 | dprintf(fd, " %s\n", msg.string()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4561 | dprintf(fd, " ...\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4562 | } else if (mEventLog.size() == 0) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4563 | dprintf(fd, " [no events yet]\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4564 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4565 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4566 | } |
| 4567 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4568 | void CameraService::cacheClientTagDumpIfNeeded(const char *cameraId, BasicClient* client) { |
| 4569 | Mutex::Autolock lock(mLogLock); |
| 4570 | if (!isClientWatchedLocked(client)) { return; } |
| 4571 | |
| 4572 | std::vector<std::string> dumpVector; |
| 4573 | client->dumpWatchedEventsToVector(dumpVector); |
| 4574 | |
| 4575 | if (dumpVector.empty()) { return; } |
| 4576 | |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 4577 | std::string dumpString; |
| 4578 | |
| 4579 | String8 currentTime = getFormattedCurrentTime(); |
| 4580 | dumpString += "Cached @ "; |
| 4581 | dumpString += currentTime.string(); |
| 4582 | dumpString += "\n"; // First line is the timestamp of when client is cached. |
| 4583 | |
| 4584 | |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4585 | const String16 &packageName = client->getPackageName(); |
| 4586 | |
| 4587 | String8 packageName8 = String8(packageName); |
| 4588 | const char *printablePackageName = packageName8.lockBuffer(packageName.size()); |
| 4589 | |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 4590 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4591 | size_t i = dumpVector.size(); |
| 4592 | |
| 4593 | // Store the string in reverse order (latest last) |
| 4594 | while (i > 0) { |
| 4595 | i--; |
| 4596 | dumpString += cameraId; |
| 4597 | dumpString += ":"; |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4598 | dumpString += printablePackageName; |
| 4599 | dumpString += " "; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4600 | dumpString += dumpVector[i]; // implicitly ends with '\n' |
| 4601 | } |
| 4602 | |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4603 | packageName8.unlockBuffer(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4604 | mWatchedClientsDumpCache[packageName] = dumpString; |
| 4605 | } |
| 4606 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4607 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4608 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4609 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 4610 | if (mTorchClientMap[i] == who) { |
| 4611 | // 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] | 4612 | String8 cameraId = mTorchClientMap.keyAt(i); |
| 4613 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 4614 | if (res) { |
| 4615 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 4616 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 4617 | return; |
| 4618 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4619 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4620 | break; |
| 4621 | } |
| 4622 | } |
| 4623 | } |
| 4624 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4625 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 4626 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 4627 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4628 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 4629 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 4630 | */ |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 4631 | // PID here is approximate and can be wrong. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 4632 | logClientDied(CameraThreadState::getCallingPid(), String8("Binder died unexpectedly")); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4633 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4634 | // check torch client |
| 4635 | handleTorchClientBinderDied(who); |
| 4636 | |
| 4637 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4638 | if(!evictClientIdByRemote(who)) { |
| 4639 | 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] | 4640 | return; |
| 4641 | } |
| 4642 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4643 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 4644 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 4645 | } |
| 4646 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4647 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4648 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 4649 | } |
| 4650 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4651 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId, |
| 4652 | std::initializer_list<StatusInternal> rejectSourceStates) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4653 | // Do not lock mServiceLock here or can get into a deadlock from |
| 4654 | // connect() -> disconnect -> updateStatus |
| 4655 | |
| 4656 | auto state = getCameraState(cameraId); |
| 4657 | |
| 4658 | if (state == nullptr) { |
| 4659 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 4660 | cameraId.string()); |
| 4661 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4662 | } |
| 4663 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4664 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 4665 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 4666 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 4667 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 4668 | return; |
| 4669 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4670 | |
| 4671 | // Collect the logical cameras without holding mStatusLock in updateStatus |
| 4672 | // as that can lead to a deadlock(b/162192331). |
| 4673 | auto logicalCameraIds = getLogicalCameras(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4674 | // 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] | 4675 | // of the listeners with both the mStatusLock and mStatusListenerLock held |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 4676 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4677 | &logicalCameraIds] |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4678 | (const String8& cameraId, StatusInternal status) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4679 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4680 | if (status != StatusInternal::ENUMERATING) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4681 | // Update torch status if it has a flash unit. |
| 4682 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4683 | TorchModeStatus torchStatus; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4684 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 4685 | NAME_NOT_FOUND) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4686 | TorchModeStatus newTorchStatus = |
| 4687 | status == StatusInternal::PRESENT ? |
| 4688 | TorchModeStatus::AVAILABLE_OFF : |
| 4689 | TorchModeStatus::NOT_AVAILABLE; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4690 | if (torchStatus != newTorchStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 4691 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4692 | } |
| 4693 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | Mutex::Autolock lock(mStatusListenerLock); |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4697 | notifyPhysicalCameraStatusLocked(mapToInterface(status), String16(cameraId), |
| 4698 | logicalCameraIds, deviceKind); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4699 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4700 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 4701 | bool isVendorListener = listener->isVendorListener(); |
| 4702 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, |
| 4703 | listener->getListenerPid(), listener->getListenerUid()) || |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 4704 | isVendorListener) { |
| 4705 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4706 | cameraId.c_str()); |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 4707 | continue; |
| 4708 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4709 | auto ret = listener->getListener()->onStatusChanged(mapToInterface(status), |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4710 | String16(cameraId)); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4711 | listener->handleBinderStatus(ret, |
| 4712 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", |
| 4713 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 4714 | ret.exceptionCode()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4715 | } |
| 4716 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4717 | } |
| 4718 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4719 | void CameraService::updateOpenCloseStatus(const String8& cameraId, bool open, |
| 4720 | const String16& clientPackageName) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 4721 | auto state = getCameraState(cameraId); |
| 4722 | if (state == nullptr) { |
| 4723 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 4724 | cameraId.string()); |
| 4725 | return; |
| 4726 | } |
| 4727 | if (open) { |
| 4728 | state->setClientPackage(String8(clientPackageName)); |
| 4729 | } else { |
| 4730 | state->setClientPackage(String8::empty()); |
| 4731 | } |
| 4732 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4733 | Mutex::Autolock lock(mStatusListenerLock); |
| 4734 | |
| 4735 | for (const auto& it : mListenerList) { |
| 4736 | if (!it->isOpenCloseCallbackAllowed()) { |
| 4737 | continue; |
| 4738 | } |
| 4739 | |
| 4740 | binder::Status ret; |
| 4741 | String16 cameraId64(cameraId); |
| 4742 | if (open) { |
| 4743 | ret = it->getListener()->onCameraOpened(cameraId64, clientPackageName); |
| 4744 | } else { |
| 4745 | ret = it->getListener()->onCameraClosed(cameraId64); |
| 4746 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4747 | |
| 4748 | it->handleBinderStatus(ret, |
| 4749 | "%s: Failed to trigger onCameraOpened/onCameraClosed callback for %d:%d: %d", |
| 4750 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4751 | } |
| 4752 | } |
| 4753 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4754 | template<class Func> |
| 4755 | void CameraService::CameraState::updateStatus(StatusInternal status, |
| 4756 | const String8& cameraId, |
| 4757 | std::initializer_list<StatusInternal> rejectSourceStates, |
| 4758 | Func onStatusUpdatedLocked) { |
| 4759 | Mutex::Autolock lock(mStatusLock); |
| 4760 | StatusInternal oldStatus = mStatus; |
| 4761 | mStatus = status; |
| 4762 | |
| 4763 | if (oldStatus == status) { |
| 4764 | return; |
| 4765 | } |
| 4766 | |
| 4767 | ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__, |
| 4768 | cameraId.string(), oldStatus, status); |
| 4769 | |
| 4770 | if (oldStatus == StatusInternal::NOT_PRESENT && |
| 4771 | (status != StatusInternal::PRESENT && |
| 4772 | status != StatusInternal::ENUMERATING)) { |
| 4773 | |
| 4774 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 4775 | __FUNCTION__); |
| 4776 | mStatus = oldStatus; |
| 4777 | return; |
| 4778 | } |
| 4779 | |
| 4780 | /** |
| 4781 | * Sometimes we want to conditionally do a transition. |
| 4782 | * For example if a client disconnects, we want to go to PRESENT |
| 4783 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 4784 | */ |
| 4785 | for (auto& rejectStatus : rejectSourceStates) { |
| 4786 | if (oldStatus == rejectStatus) { |
| 4787 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
| 4788 | "state was was in one of the bad states.", __FUNCTION__, cameraId.string()); |
| 4789 | mStatus = oldStatus; |
| 4790 | return; |
| 4791 | } |
| 4792 | } |
| 4793 | |
| 4794 | onStatusUpdatedLocked(cameraId, status); |
| 4795 | } |
| 4796 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4797 | status_t CameraService::getTorchStatusLocked( |
| 4798 | const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4799 | TorchModeStatus *status) const { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4800 | if (!status) { |
| 4801 | return BAD_VALUE; |
| 4802 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4803 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 4804 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4805 | // invalid camera ID or the camera doesn't have a flash unit |
| 4806 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4807 | } |
| 4808 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4809 | *status = mTorchStatusMap.valueAt(index); |
| 4810 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4811 | } |
| 4812 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4813 | status_t CameraService::setTorchStatusLocked(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4814 | TorchModeStatus status) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4815 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 4816 | if (index == NAME_NOT_FOUND) { |
| 4817 | return BAD_VALUE; |
| 4818 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4819 | mTorchStatusMap.editValueAt(index) = status; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4820 | |
| 4821 | return OK; |
| 4822 | } |
| 4823 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4824 | std::list<String16> CameraService::getLogicalCameras( |
| 4825 | const String8& physicalCameraId) { |
| 4826 | std::list<String16> retList; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4827 | Mutex::Autolock lock(mCameraStatesLock); |
| 4828 | for (const auto& state : mCameraStates) { |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4829 | if (state.second->containsPhysicalCamera(physicalCameraId.c_str())) { |
| 4830 | retList.emplace_back(String16(state.first)); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4831 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4832 | } |
| 4833 | return retList; |
| 4834 | } |
| 4835 | |
| 4836 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, |
| 4837 | const String16& physicalCameraId, const std::list<String16>& logicalCameraIds, |
| 4838 | SystemCameraKind deviceKind) { |
| 4839 | // mStatusListenerLock is expected to be locked |
| 4840 | for (const auto& logicalCameraId : logicalCameraIds) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4841 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4842 | // Note: we check only the deviceKind of the physical camera id |
| 4843 | // since, logical camera ids and their physical camera ids are |
| 4844 | // guaranteed to have the same system camera kind. |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 4845 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 4846 | listener->getListenerPid(), listener->getListenerUid())) { |
| 4847 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4848 | String8(physicalCameraId).c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 4849 | continue; |
| 4850 | } |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4851 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged(status, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4852 | logicalCameraId, physicalCameraId); |
Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4853 | listener->handleBinderStatus(ret, |
| 4854 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", |
| 4855 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), |
| 4856 | ret.exceptionCode()); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4857 | } |
| 4858 | } |
| 4859 | } |
| 4860 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4861 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4862 | void CameraService::blockClientsForUid(uid_t uid) { |
| 4863 | const auto clients = mActiveClientManager.getAll(); |
| 4864 | for (auto& current : clients) { |
| 4865 | if (current != nullptr) { |
| 4866 | const auto basicClient = current->getValue(); |
| 4867 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { |
| 4868 | basicClient->block(); |
| 4869 | } |
| 4870 | } |
| 4871 | } |
| 4872 | } |
| 4873 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4874 | void CameraService::blockAllClients() { |
| 4875 | const auto clients = mActiveClientManager.getAll(); |
| 4876 | for (auto& current : clients) { |
| 4877 | if (current != nullptr) { |
| 4878 | const auto basicClient = current->getValue(); |
| 4879 | if (basicClient.get() != nullptr) { |
| 4880 | basicClient->block(); |
| 4881 | } |
| 4882 | } |
| 4883 | } |
| 4884 | } |
| 4885 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4886 | // NOTE: This is a remote API - make sure all args are validated |
| 4887 | status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) { |
| 4888 | if (!checkCallingPermission(sManageCameraPermission, nullptr, nullptr)) { |
| 4889 | return PERMISSION_DENIED; |
| 4890 | } |
| 4891 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 4892 | return BAD_VALUE; |
| 4893 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4894 | if (args.size() >= 3 && args[0] == String16("set-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4895 | return handleSetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4896 | } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4897 | return handleResetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4898 | } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4899 | return handleGetUidState(args, out, err); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4900 | } else if (args.size() >= 2 && args[0] == String16("set-rotate-and-crop")) { |
| 4901 | return handleSetRotateAndCrop(args); |
| 4902 | } else if (args.size() >= 1 && args[0] == String16("get-rotate-and-crop")) { |
| 4903 | return handleGetRotateAndCrop(out); |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame^] | 4904 | } else if (args.size() >= 2 && args[0] == String16("set-autoframing")) { |
| 4905 | return handleSetAutoframing(args); |
| 4906 | } else if (args.size() >= 1 && args[0] == String16("get-autoframing")) { |
| 4907 | return handleGetAutoframing(out); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4908 | } else if (args.size() >= 2 && args[0] == String16("set-image-dump-mask")) { |
| 4909 | return handleSetImageDumpMask(args); |
| 4910 | } else if (args.size() >= 1 && args[0] == String16("get-image-dump-mask")) { |
| 4911 | return handleGetImageDumpMask(out); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 4912 | } else if (args.size() >= 2 && args[0] == String16("set-camera-mute")) { |
| 4913 | return handleSetCameraMute(args); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4914 | } else if (args.size() >= 2 && args[0] == String16("watch")) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 4915 | return handleWatchCommand(args, in, out); |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 4916 | } else if (args.size() >= 2 && args[0] == String16("set-watchdog")) { |
| 4917 | return handleSetCameraServiceWatchdog(args); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4918 | } else if (args.size() == 1 && args[0] == String16("help")) { |
| 4919 | printHelp(out); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4920 | return OK; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4921 | } |
| 4922 | printHelp(err); |
| 4923 | return BAD_VALUE; |
| 4924 | } |
| 4925 | |
| 4926 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4927 | String16 packageName = args[1]; |
| 4928 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4929 | bool active = false; |
| 4930 | if (args[2] == String16("active")) { |
| 4931 | active = true; |
| 4932 | } else if ((args[2] != String16("idle"))) { |
| 4933 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string()); |
| 4934 | return BAD_VALUE; |
| 4935 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4936 | |
| 4937 | int userId = 0; |
| 4938 | if (args.size() >= 5 && args[3] == String16("--user")) { |
| 4939 | userId = atoi(String8(args[4])); |
| 4940 | } |
| 4941 | |
| 4942 | uid_t uid; |
| 4943 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
| 4944 | return BAD_VALUE; |
| 4945 | } |
| 4946 | |
| 4947 | mUidPolicy->addOverrideUid(uid, packageName, active); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4948 | return NO_ERROR; |
| 4949 | } |
| 4950 | |
| 4951 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4952 | String16 packageName = args[1]; |
| 4953 | |
| 4954 | int userId = 0; |
| 4955 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 4956 | userId = atoi(String8(args[3])); |
| 4957 | } |
| 4958 | |
| 4959 | uid_t uid; |
| 4960 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4961 | return BAD_VALUE; |
| 4962 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4963 | |
| 4964 | mUidPolicy->removeOverrideUid(uid, packageName); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4965 | return NO_ERROR; |
| 4966 | } |
| 4967 | |
| 4968 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4969 | String16 packageName = args[1]; |
| 4970 | |
| 4971 | int userId = 0; |
| 4972 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 4973 | userId = atoi(String8(args[3])); |
| 4974 | } |
| 4975 | |
| 4976 | uid_t uid; |
| 4977 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4978 | return BAD_VALUE; |
| 4979 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4980 | |
| 4981 | if (mUidPolicy->isUidActive(uid, packageName)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4982 | return dprintf(out, "active\n"); |
| 4983 | } else { |
| 4984 | return dprintf(out, "idle\n"); |
| 4985 | } |
| 4986 | } |
| 4987 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4988 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { |
| 4989 | int rotateValue = atoi(String8(args[1])); |
| 4990 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || |
| 4991 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; |
| 4992 | Mutex::Autolock lock(mServiceLock); |
| 4993 | |
| 4994 | mOverrideRotateAndCropMode = rotateValue; |
| 4995 | |
| 4996 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; |
| 4997 | |
| 4998 | const auto clients = mActiveClientManager.getAll(); |
| 4999 | for (auto& current : clients) { |
| 5000 | if (current != nullptr) { |
| 5001 | const auto basicClient = current->getValue(); |
| 5002 | if (basicClient.get() != nullptr) { |
| 5003 | basicClient->setRotateAndCropOverride(rotateValue); |
| 5004 | } |
| 5005 | } |
| 5006 | } |
| 5007 | |
| 5008 | return OK; |
| 5009 | } |
| 5010 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame^] | 5011 | status_t CameraService::handleSetAutoframing(const Vector<String16>& args) { |
| 5012 | char* end; |
| 5013 | int autoframingValue = (int) strtol(String8(args[1]), &end, /*base=*/10); |
| 5014 | if ((*end != '\0') || |
| 5015 | (autoframingValue != ANDROID_CONTROL_AUTOFRAMING_OFF && |
| 5016 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_ON && |
| 5017 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_AUTO)) { |
| 5018 | return BAD_VALUE; |
| 5019 | } |
| 5020 | |
| 5021 | Mutex::Autolock lock(mServiceLock); |
| 5022 | mOverrideAutoframingMode = autoframingValue; |
| 5023 | |
| 5024 | if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) return OK; |
| 5025 | |
| 5026 | const auto clients = mActiveClientManager.getAll(); |
| 5027 | for (auto& current : clients) { |
| 5028 | if (current != nullptr) { |
| 5029 | const auto basicClient = current->getValue(); |
| 5030 | if (basicClient.get() != nullptr) { |
| 5031 | basicClient->setAutoframingOverride(autoframingValue); |
| 5032 | } |
| 5033 | } |
| 5034 | } |
| 5035 | |
| 5036 | return OK; |
| 5037 | } |
| 5038 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 5039 | status_t CameraService::handleSetCameraServiceWatchdog(const Vector<String16>& args) { |
| 5040 | int enableWatchdog = atoi(String8(args[1])); |
| 5041 | |
| 5042 | if (enableWatchdog < 0 || enableWatchdog > 1) return BAD_VALUE; |
| 5043 | |
| 5044 | Mutex::Autolock lock(mServiceLock); |
| 5045 | |
| 5046 | mCameraServiceWatchdogEnabled = enableWatchdog; |
| 5047 | |
| 5048 | const auto clients = mActiveClientManager.getAll(); |
| 5049 | for (auto& current : clients) { |
| 5050 | if (current != nullptr) { |
| 5051 | const auto basicClient = current->getValue(); |
| 5052 | if (basicClient.get() != nullptr) { |
| 5053 | basicClient->setCameraServiceWatchdog(enableWatchdog); |
| 5054 | } |
| 5055 | } |
| 5056 | } |
| 5057 | |
| 5058 | return OK; |
| 5059 | } |
| 5060 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5061 | status_t CameraService::handleGetRotateAndCrop(int out) { |
| 5062 | Mutex::Autolock lock(mServiceLock); |
| 5063 | |
| 5064 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); |
| 5065 | } |
| 5066 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame^] | 5067 | status_t CameraService::handleGetAutoframing(int out) { |
| 5068 | Mutex::Autolock lock(mServiceLock); |
| 5069 | |
| 5070 | return dprintf(out, "autoframing override: %d\n", mOverrideAutoframingMode); |
| 5071 | } |
| 5072 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5073 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { |
| 5074 | char *endPtr; |
| 5075 | errno = 0; |
| 5076 | String8 maskString8 = String8(args[1]); |
| 5077 | long maskValue = strtol(maskString8.c_str(), &endPtr, 10); |
| 5078 | |
| 5079 | if (errno != 0) return BAD_VALUE; |
| 5080 | if (endPtr != maskString8.c_str() + maskString8.size()) return BAD_VALUE; |
| 5081 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; |
| 5082 | |
| 5083 | Mutex::Autolock lock(mServiceLock); |
| 5084 | |
| 5085 | mImageDumpMask = maskValue; |
| 5086 | |
| 5087 | return OK; |
| 5088 | } |
| 5089 | |
| 5090 | status_t CameraService::handleGetImageDumpMask(int out) { |
| 5091 | Mutex::Autolock lock(mServiceLock); |
| 5092 | |
| 5093 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); |
| 5094 | } |
| 5095 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 5096 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { |
| 5097 | int muteValue = strtol(String8(args[1]), nullptr, 10); |
| 5098 | if (errno != 0) return BAD_VALUE; |
| 5099 | |
| 5100 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; |
| 5101 | Mutex::Autolock lock(mServiceLock); |
| 5102 | |
| 5103 | mOverrideCameraMuteMode = (muteValue == 1); |
| 5104 | |
| 5105 | const auto clients = mActiveClientManager.getAll(); |
| 5106 | for (auto& current : clients) { |
| 5107 | if (current != nullptr) { |
| 5108 | const auto basicClient = current->getValue(); |
| 5109 | if (basicClient.get() != nullptr) { |
| 5110 | if (basicClient->supportsCameraMute()) { |
| 5111 | basicClient->setCameraMute(mOverrideCameraMuteMode); |
| 5112 | } |
| 5113 | } |
| 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | return OK; |
| 5118 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5119 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5120 | status_t CameraService::handleWatchCommand(const Vector<String16>& args, int inFd, int outFd) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5121 | if (args.size() >= 3 && args[1] == String16("start")) { |
| 5122 | return startWatchingTags(args, outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5123 | } else if (args.size() == 2 && args[1] == String16("stop")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5124 | return stopWatchingTags(outFd); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5125 | } else if (args.size() == 2 && args[1] == String16("dump")) { |
| 5126 | return printWatchedTags(outFd); |
| 5127 | } else if (args.size() >= 2 && args[1] == String16("live")) { |
| 5128 | return printWatchedTagsUntilInterrupt(args, inFd, outFd); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5129 | } else if (args.size() == 2 && args[1] == String16("clear")) { |
| 5130 | return clearCachedMonitoredTagDumps(outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5131 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5132 | dprintf(outFd, "Camera service watch commands:\n" |
| 5133 | " start -m <comma_separated_tag_list> [-c <comma_separated_client_list>]\n" |
| 5134 | " starts watching the provided tags for clients with provided package\n" |
| 5135 | " recognizes tag shorthands like '3a'\n" |
| 5136 | " 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] | 5137 | " dump dumps the monitoring information and exits\n" |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5138 | " stop stops watching all tags\n" |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5139 | " live [-n <refresh_interval_ms>]\n" |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5140 | " prints the monitored information in real time\n" |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5141 | " Hit return to exit\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5142 | " clear clears all buffers storing information for watch command"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5143 | return BAD_VALUE; |
| 5144 | } |
| 5145 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5146 | status_t CameraService::startWatchingTags(const Vector<String16> &args, int outFd) { |
| 5147 | Mutex::Autolock lock(mLogLock); |
| 5148 | size_t tagsIdx; // index of '-m' |
| 5149 | String16 tags(""); |
| 5150 | for (tagsIdx = 2; tagsIdx < args.size() && args[tagsIdx] != String16("-m"); tagsIdx++); |
| 5151 | if (tagsIdx < args.size() - 1) { |
| 5152 | tags = args[tagsIdx + 1]; |
| 5153 | } else { |
| 5154 | dprintf(outFd, "No tags provided.\n"); |
| 5155 | return BAD_VALUE; |
| 5156 | } |
| 5157 | |
| 5158 | size_t clientsIdx; // index of '-c' |
| 5159 | String16 clients = kWatchAllClientsFlag; // watch all clients if no clients are provided |
| 5160 | for (clientsIdx = 2; clientsIdx < args.size() && args[clientsIdx] != String16("-c"); |
| 5161 | clientsIdx++); |
| 5162 | if (clientsIdx < args.size() - 1) { |
| 5163 | clients = args[clientsIdx + 1]; |
| 5164 | } |
| 5165 | parseClientsToWatchLocked(String8(clients)); |
| 5166 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5167 | // track tags to initialize future clients with the monitoring information |
| 5168 | mMonitorTags = String8(tags); |
| 5169 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5170 | bool serviceLock = tryLock(mServiceLock); |
| 5171 | int numWatchedClients = 0; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5172 | auto cameraClients = mActiveClientManager.getAll(); |
| 5173 | for (const auto &clientDescriptor: cameraClients) { |
| 5174 | if (clientDescriptor == nullptr) { continue; } |
| 5175 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5176 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5177 | |
| 5178 | if (isClientWatchedLocked(client.get())) { |
| 5179 | client->startWatchingTags(mMonitorTags, outFd); |
| 5180 | numWatchedClients++; |
| 5181 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5182 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5183 | dprintf(outFd, "Started watching %d active clients\n", numWatchedClients); |
| 5184 | |
| 5185 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5186 | return OK; |
| 5187 | } |
| 5188 | |
| 5189 | status_t CameraService::stopWatchingTags(int outFd) { |
| 5190 | // clear mMonitorTags to prevent new clients from monitoring tags at initialization |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5191 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5192 | mMonitorTags = String8::empty(); |
| 5193 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5194 | mWatchedClientPackages.clear(); |
| 5195 | mWatchedClientsDumpCache.clear(); |
| 5196 | |
| 5197 | bool serviceLock = tryLock(mServiceLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5198 | auto cameraClients = mActiveClientManager.getAll(); |
| 5199 | for (const auto &clientDescriptor : cameraClients) { |
| 5200 | if (clientDescriptor == nullptr) { continue; } |
| 5201 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5202 | if (client.get() == nullptr) { continue; } |
| 5203 | client->stopWatchingTags(outFd); |
| 5204 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5205 | dprintf(outFd, "Stopped watching all clients.\n"); |
| 5206 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5207 | return OK; |
| 5208 | } |
| 5209 | |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5210 | status_t CameraService::clearCachedMonitoredTagDumps(int outFd) { |
| 5211 | Mutex::Autolock lock(mLogLock); |
| 5212 | size_t clearedSize = mWatchedClientsDumpCache.size(); |
| 5213 | mWatchedClientsDumpCache.clear(); |
| 5214 | dprintf(outFd, "Cleared tag information of %zu cached clients.\n", clearedSize); |
| 5215 | return OK; |
| 5216 | } |
| 5217 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5218 | status_t CameraService::printWatchedTags(int outFd) { |
| 5219 | Mutex::Autolock logLock(mLogLock); |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 5220 | std::set<String16> connectedMonitoredClients; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5221 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5222 | bool printedSomething = false; // tracks if any monitoring information was printed |
| 5223 | // (from either cached or active clients) |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5224 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5225 | bool serviceLock = tryLock(mServiceLock); |
| 5226 | // get all watched clients that are currently connected |
| 5227 | for (const auto &clientDescriptor: mActiveClientManager.getAll()) { |
| 5228 | if (clientDescriptor == nullptr) { continue; } |
| 5229 | |
| 5230 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5231 | if (client.get() == nullptr) { continue; } |
| 5232 | if (!isClientWatchedLocked(client.get())) { continue; } |
| 5233 | |
| 5234 | std::vector<std::string> dumpVector; |
| 5235 | client->dumpWatchedEventsToVector(dumpVector); |
| 5236 | |
| 5237 | size_t printIdx = dumpVector.size(); |
| 5238 | if (printIdx == 0) { |
| 5239 | continue; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5240 | } |
| 5241 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5242 | // Print tag dumps for active client |
| 5243 | const String8 &cameraId = clientDescriptor->getKey(); |
| 5244 | String8 packageName8 = String8(client->getPackageName()); |
| 5245 | const char *printablePackageName = packageName8.lockBuffer(packageName8.size()); |
| 5246 | dprintf(outFd, "Client: %s (active)\n", printablePackageName); |
| 5247 | while(printIdx > 0) { |
| 5248 | printIdx--; |
| 5249 | dprintf(outFd, "%s:%s %s", cameraId.string(), printablePackageName, |
| 5250 | dumpVector[printIdx].c_str()); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5251 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5252 | dprintf(outFd, "\n"); |
| 5253 | packageName8.unlockBuffer(); |
| 5254 | printedSomething = true; |
| 5255 | |
| 5256 | connectedMonitoredClients.emplace(client->getPackageName()); |
| 5257 | } |
| 5258 | if (serviceLock) { mServiceLock.unlock(); } |
| 5259 | |
| 5260 | // Print entries in mWatchedClientsDumpCache for clients that are not connected |
| 5261 | for (const auto &kv: mWatchedClientsDumpCache) { |
| 5262 | const String16 &package = kv.first; |
| 5263 | if (connectedMonitoredClients.find(package) != connectedMonitoredClients.end()) { |
| 5264 | continue; |
| 5265 | } |
| 5266 | |
| 5267 | dprintf(outFd, "Client: %s (cached)\n", String8(package).string()); |
| 5268 | dprintf(outFd, "%s\n", kv.second.c_str()); |
| 5269 | printedSomething = true; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5270 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5271 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5272 | if (!printedSomething) { |
| 5273 | dprintf(outFd, "No monitoring information to print.\n"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5274 | } |
| 5275 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5276 | return OK; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5277 | } |
| 5278 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5279 | // Print all events in vector `events' that came after lastPrintedEvent |
| 5280 | void printNewWatchedEvents(int outFd, |
| 5281 | const char *cameraId, |
| 5282 | const String16 &packageName, |
| 5283 | const std::vector<std::string> &events, |
| 5284 | const std::string &lastPrintedEvent) { |
| 5285 | if (events.empty()) { return; } |
| 5286 | |
| 5287 | // index of lastPrintedEvent in events. |
| 5288 | // lastPrintedIdx = events.size() if lastPrintedEvent is not in events |
| 5289 | size_t lastPrintedIdx; |
| 5290 | for (lastPrintedIdx = 0; |
| 5291 | lastPrintedIdx < events.size() && lastPrintedEvent != events[lastPrintedIdx]; |
| 5292 | lastPrintedIdx++); |
| 5293 | |
| 5294 | if (lastPrintedIdx == 0) { return; } // early exit if no new event in `events` |
| 5295 | |
| 5296 | String8 packageName8(packageName); |
| 5297 | const char *printablePackageName = packageName8.lockBuffer(packageName8.size()); |
| 5298 | |
| 5299 | // print events in chronological order (latest event last) |
| 5300 | size_t idxToPrint = lastPrintedIdx; |
| 5301 | do { |
| 5302 | idxToPrint--; |
| 5303 | dprintf(outFd, "%s:%s %s", cameraId, printablePackageName, events[idxToPrint].c_str()); |
| 5304 | } while (idxToPrint != 0); |
| 5305 | |
| 5306 | packageName8.unlockBuffer(); |
| 5307 | } |
| 5308 | |
| 5309 | // Returns true if adb shell cmd watch should be interrupted based on data in inFd. The watch |
| 5310 | // command should be interrupted if the user presses the return key, or if user loses any way to |
| 5311 | // signal interrupt. |
| 5312 | // If timeoutMs == 0, this function will always return false |
| 5313 | bool shouldInterruptWatchCommand(int inFd, int outFd, long timeoutMs) { |
| 5314 | struct timeval startTime; |
| 5315 | int startTimeError = gettimeofday(&startTime, nullptr); |
| 5316 | if (startTimeError) { |
| 5317 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 5318 | return true; |
| 5319 | } |
| 5320 | |
| 5321 | const nfds_t numFds = 1; |
| 5322 | struct pollfd pollFd = { .fd = inFd, .events = POLLIN, .revents = 0 }; |
| 5323 | |
| 5324 | struct timeval currTime; |
| 5325 | char buffer[2]; |
| 5326 | while(true) { |
| 5327 | int currTimeError = gettimeofday(&currTime, nullptr); |
| 5328 | if (currTimeError) { |
| 5329 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 5330 | return true; |
| 5331 | } |
| 5332 | |
| 5333 | long elapsedTimeMs = ((currTime.tv_sec - startTime.tv_sec) * 1000L) |
| 5334 | + ((currTime.tv_usec - startTime.tv_usec) / 1000L); |
| 5335 | int remainingTimeMs = (int) (timeoutMs - elapsedTimeMs); |
| 5336 | |
| 5337 | if (remainingTimeMs <= 0) { |
| 5338 | // No user interrupt within timeoutMs, don't interrupt watch command |
| 5339 | return false; |
| 5340 | } |
| 5341 | |
| 5342 | int numFdsUpdated = poll(&pollFd, numFds, remainingTimeMs); |
| 5343 | if (numFdsUpdated < 0) { |
| 5344 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 5345 | return true; |
| 5346 | } |
| 5347 | |
| 5348 | if (numFdsUpdated == 0) { |
| 5349 | // No user input within timeoutMs, don't interrupt watch command |
| 5350 | return false; |
| 5351 | } |
| 5352 | |
| 5353 | if (!(pollFd.revents & POLLIN)) { |
| 5354 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 5355 | return true; |
| 5356 | } |
| 5357 | |
| 5358 | ssize_t sizeRead = read(inFd, buffer, sizeof(buffer) - 1); |
| 5359 | if (sizeRead < 0) { |
| 5360 | dprintf(outFd, "Error reading user input. Exiting.\n"); |
| 5361 | return true; |
| 5362 | } |
| 5363 | |
| 5364 | if (sizeRead == 0) { |
| 5365 | // Reached end of input fd (can happen if input is piped) |
| 5366 | // User has no way to signal an interrupt, so interrupt here |
| 5367 | return true; |
| 5368 | } |
| 5369 | |
| 5370 | if (buffer[0] == '\n') { |
| 5371 | // User pressed return, interrupt watch command. |
| 5372 | return true; |
| 5373 | } |
| 5374 | } |
| 5375 | } |
| 5376 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5377 | status_t CameraService::printWatchedTagsUntilInterrupt(const Vector<String16> &args, |
| 5378 | int inFd, int outFd) { |
| 5379 | // Figure out refresh interval, if present in args |
| 5380 | long refreshTimeoutMs = 1000L; // refresh every 1s by default |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5381 | if (args.size() > 2) { |
| 5382 | size_t intervalIdx; // index of '-n' |
| 5383 | for (intervalIdx = 2; intervalIdx < args.size() && String16("-n") != args[intervalIdx]; |
| 5384 | intervalIdx++); |
| 5385 | |
| 5386 | size_t intervalValIdx = intervalIdx + 1; |
| 5387 | if (intervalValIdx < args.size()) { |
| 5388 | refreshTimeoutMs = strtol(String8(args[intervalValIdx].string()), nullptr, 10); |
| 5389 | if (errno) { return BAD_VALUE; } |
| 5390 | } |
| 5391 | } |
| 5392 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5393 | // Set min timeout of 10ms. This prevents edge cases in polling when timeout of 0 is passed. |
| 5394 | refreshTimeoutMs = refreshTimeoutMs < 10 ? 10 : refreshTimeoutMs; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5395 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5396 | dprintf(outFd, "Press return to exit...\n\n"); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5397 | std::map<String16, std::string> packageNameToLastEvent; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5398 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5399 | while (true) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5400 | bool serviceLock = tryLock(mServiceLock); |
| 5401 | auto cameraClients = mActiveClientManager.getAll(); |
| 5402 | if (serviceLock) { mServiceLock.unlock(); } |
| 5403 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5404 | for (const auto& clientDescriptor : cameraClients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5405 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5406 | if (clientDescriptor == nullptr) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5407 | |
| 5408 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5409 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5410 | if (!isClientWatchedLocked(client.get())) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5411 | |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5412 | const String16 &packageName = client->getPackageName(); |
| 5413 | // This also initializes the map entries with an empty string |
| 5414 | const std::string& lastPrintedEvent = packageNameToLastEvent[packageName]; |
| 5415 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5416 | std::vector<std::string> latestEvents; |
| 5417 | client->dumpWatchedEventsToVector(latestEvents); |
| 5418 | |
| 5419 | if (!latestEvents.empty()) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5420 | String8 cameraId = clientDescriptor->getKey(); |
| 5421 | const char *printableCameraId = cameraId.lockBuffer(cameraId.size()); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5422 | printNewWatchedEvents(outFd, |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5423 | printableCameraId, |
| 5424 | packageName, |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5425 | latestEvents, |
| 5426 | lastPrintedEvent); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5427 | packageNameToLastEvent[packageName] = latestEvents[0]; |
| 5428 | cameraId.unlockBuffer(); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5429 | } |
| 5430 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5431 | if (shouldInterruptWatchCommand(inFd, outFd, refreshTimeoutMs)) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5432 | break; |
| 5433 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5434 | } |
| 5435 | return OK; |
| 5436 | } |
| 5437 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5438 | void CameraService::parseClientsToWatchLocked(String8 clients) { |
| 5439 | mWatchedClientPackages.clear(); |
| 5440 | |
| 5441 | const char *allSentinel = String8(kWatchAllClientsFlag).string(); |
| 5442 | |
| 5443 | char *tokenized = clients.lockBuffer(clients.size()); |
| 5444 | char *savePtr; |
| 5445 | char *nextClient = strtok_r(tokenized, ",", &savePtr); |
| 5446 | |
| 5447 | while (nextClient != nullptr) { |
| 5448 | if (strcmp(nextClient, allSentinel) == 0) { |
| 5449 | // Don't need to track any other package if 'all' is present |
| 5450 | mWatchedClientPackages.clear(); |
| 5451 | mWatchedClientPackages.emplace(kWatchAllClientsFlag); |
| 5452 | break; |
| 5453 | } |
| 5454 | |
| 5455 | // track package names |
| 5456 | mWatchedClientPackages.emplace(nextClient); |
| 5457 | nextClient = strtok_r(nullptr, ",", &savePtr); |
| 5458 | } |
| 5459 | clients.unlockBuffer(); |
| 5460 | } |
| 5461 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5462 | status_t CameraService::printHelp(int out) { |
| 5463 | return dprintf(out, "Camera service commands:\n" |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5464 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 5465 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 5466 | " 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] | 5467 | " set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" |
| 5468 | " Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" |
| 5469 | " get-rotate-and-crop returns the current override rotate-and-crop value\n" |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame^] | 5470 | " set-autoframing <VALUE> overrides the autoframing value for AUTO\n" |
| 5471 | " Valid values 0=false, 1=true, 2=auto\n" |
| 5472 | " get-autoframing returns the current override autoframing value\n" |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5473 | " set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" |
| 5474 | " Valid values 0=OFF, 1=ON for JPEG\n" |
| 5475 | " 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] | 5476 | " set-camera-mute <0/1> enable or disable camera muting\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5477 | " 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] | 5478 | " help print this message\n"); |
| 5479 | } |
| 5480 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5481 | bool CameraService::isClientWatched(const BasicClient *client) { |
| 5482 | Mutex::Autolock lock(mLogLock); |
| 5483 | return isClientWatchedLocked(client); |
| 5484 | } |
| 5485 | |
| 5486 | bool CameraService::isClientWatchedLocked(const BasicClient *client) { |
| 5487 | return mWatchedClientPackages.find(kWatchAllClientsFlag) != mWatchedClientPackages.end() || |
| 5488 | mWatchedClientPackages.find(client->getPackageName()) != mWatchedClientPackages.end(); |
| 5489 | } |
| 5490 | |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 5491 | int32_t CameraService::updateAudioRestriction() { |
| 5492 | Mutex::Autolock lock(mServiceLock); |
| 5493 | return updateAudioRestrictionLocked(); |
| 5494 | } |
| 5495 | |
| 5496 | int32_t CameraService::updateAudioRestrictionLocked() { |
| 5497 | int32_t mode = 0; |
| 5498 | // iterate through all active client |
| 5499 | for (const auto& i : mActiveClientManager.getAll()) { |
| 5500 | const auto clientSp = i->getValue(); |
| 5501 | mode |= clientSp->getAudioRestriction(); |
| 5502 | } |
| 5503 | |
| 5504 | bool modeChanged = (mAudioRestriction != mode); |
| 5505 | mAudioRestriction = mode; |
| 5506 | if (modeChanged) { |
| 5507 | mAppOps.setCameraAudioRestriction(mode); |
| 5508 | } |
| 5509 | return mode; |
| 5510 | } |
| 5511 | |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 5512 | status_t CameraService::checkIfInjectionCameraIsPresent(const String8& externalCamId, |
| 5513 | sp<BasicClient> clientSp) { |
| 5514 | std::unique_ptr<AutoConditionLock> lock = |
| 5515 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 5516 | status_t res = NO_ERROR; |
| 5517 | if ((res = checkIfDeviceIsUsable(externalCamId)) != NO_ERROR) { |
| 5518 | ALOGW("Device %s is not usable!", externalCamId.string()); |
| 5519 | mInjectionStatusListener->notifyInjectionError( |
| 5520 | externalCamId, UNKNOWN_TRANSACTION); |
| 5521 | clientSp->notifyError( |
| 5522 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 5523 | CaptureResultExtras()); |
| 5524 | |
| 5525 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 5526 | // other clients from connecting in mServiceLockWrapper if held |
| 5527 | mServiceLock.unlock(); |
| 5528 | |
| 5529 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
| 5530 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 5531 | clientSp->disconnect(); |
| 5532 | CameraThreadState::restoreCallingIdentity(token); |
| 5533 | |
| 5534 | // Reacquire mServiceLock |
| 5535 | mServiceLock.lock(); |
| 5536 | } |
| 5537 | |
| 5538 | return res; |
| 5539 | } |
| 5540 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5541 | void CameraService::clearInjectionParameters() { |
| 5542 | { |
| 5543 | Mutex::Autolock lock(mInjectionParametersLock); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 5544 | mInjectionInitPending = false; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5545 | mInjectionInternalCamId = ""; |
| 5546 | } |
| 5547 | mInjectionExternalCamId = ""; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5548 | mInjectionStatusListener->removeListener(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5549 | } |
| 5550 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5551 | }; // namespace android |