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 | |
Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 145 | CameraService::CameraService() : |
| 146 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 147 | mNumberOfCameras(0), |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 148 | mNumberOfCamerasWithoutSystemCamera(0), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 149 | mSoundRef(0), mInitialized(false), |
| 150 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 151 | ALOGI("CameraService started (pid=%d)", getpid()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 152 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 153 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 154 | if (mMemFd == -1) { |
| 155 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 156 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 159 | // The word 'System' here does not refer to clients only on the system |
| 160 | // partition. They just need to have a android system uid. |
| 161 | static bool doesClientHaveSystemUid() { |
| 162 | return (CameraThreadState::getCallingUid() < AID_APP_START); |
| 163 | } |
| 164 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 165 | void CameraService::onFirstRef() |
| 166 | { |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 167 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 168 | ALOGI("CameraService process starting"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 169 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 170 | BnCameraService::onFirstRef(); |
| 171 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 172 | // Update battery life tracking if service is restarting |
| 173 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 174 | notifier.noteResetCamera(); |
| 175 | notifier.noteResetFlashlight(); |
| 176 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 177 | status_t res = INVALID_OPERATION; |
Eino-Ville Talvala | 9cbbc83 | 2017-01-23 15:39:53 -0800 | [diff] [blame] | 178 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 179 | res = enumerateProviders(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 180 | if (res == OK) { |
| 181 | mInitialized = true; |
| 182 | } |
| 183 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 184 | mUidPolicy = new UidPolicy(this); |
| 185 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 186 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this); |
| 187 | mSensorPrivacyPolicy->registerSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 188 | mInjectionStatusListener = new InjectionStatusListener(this); |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 189 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 190 | sp<HidlCameraService> hcs = HidlCameraService::getInstance(this); |
| 191 | if (hcs->registerAsService() != android::OK) { |
| 192 | ALOGE("%s: Failed to register default android.frameworks.cameraservice.service@1.0", |
| 193 | __FUNCTION__); |
| 194 | } |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 195 | |
| 196 | // This needs to be last call in this function, so that it's as close to |
| 197 | // ServiceManager::addService() as possible. |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 198 | CameraServiceProxyWrapper::pingCameraServiceProxy(); |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 199 | ALOGI("CameraService pinged cameraservice proxy"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 202 | status_t CameraService::enumerateProviders() { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 203 | status_t res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 204 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 205 | std::vector<std::string> deviceIds; |
| 206 | { |
| 207 | Mutex::Autolock l(mServiceLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 208 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 209 | if (nullptr == mCameraProviderManager.get()) { |
| 210 | mCameraProviderManager = new CameraProviderManager(); |
| 211 | res = mCameraProviderManager->initialize(this); |
| 212 | if (res != OK) { |
| 213 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", |
| 214 | __FUNCTION__, strerror(-res), res); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 215 | logServiceError(String8::format("Unable to initialize camera provider manager"), |
| 216 | ERROR_DISCONNECTED); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 217 | return res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 221 | |
| 222 | // Setup vendor tags before we call get_camera_info the first time |
| 223 | // because HAL might need to setup static vendor keys in get_camera_info |
| 224 | // TODO: maybe put this into CameraProviderManager::initialize()? |
| 225 | mCameraProviderManager->setUpVendorTags(); |
| 226 | |
| 227 | if (nullptr == mFlashlight.get()) { |
| 228 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 231 | res = mFlashlight->findFlashUnits(); |
| 232 | if (res != OK) { |
| 233 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); |
| 234 | } |
| 235 | |
| 236 | deviceIds = mCameraProviderManager->getCameraDeviceIds(); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | for (auto& cameraId : deviceIds) { |
| 241 | String8 id8 = String8(cameraId.c_str()); |
Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 242 | if (getCameraState(id8) == nullptr) { |
| 243 | onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT); |
| 244 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 247 | // Derive primary rear/front cameras, and filter their charactierstics. |
| 248 | // 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] | 249 | if (SessionConfigurationUtils::IS_PERF_CLASS) { |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 250 | // Assume internal cameras are advertised from the same |
| 251 | // provider. If multiple providers are registered at different time, |
| 252 | // and each provider contains multiple internal color cameras, the current |
| 253 | // logic may filter the characteristics of more than one front/rear color |
| 254 | // cameras. |
| 255 | Mutex::Autolock l(mServiceLock); |
| 256 | filterSPerfClassCharacteristicsLocked(); |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 257 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 258 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 259 | return OK; |
| 260 | } |
| 261 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 262 | void CameraService::broadcastTorchModeStatus(const String8& cameraId, TorchModeStatus status, |
| 263 | SystemCameraKind systemCameraKind) { |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 264 | Mutex::Autolock lock(mStatusListenerLock); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 265 | for (auto& i : mListenerList) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 266 | if (shouldSkipStatusUpdates(systemCameraKind, i->isVendorListener(), i->getListenerPid(), |
| 267 | i->getListenerUid())) { |
| 268 | ALOGV("Skipping torch callback for system-only camera device %s", |
| 269 | cameraId.c_str()); |
| 270 | continue; |
| 271 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 272 | i->getListener()->onTorchStatusChanged(mapToInterface(status), String16{cameraId}); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 276 | CameraService::~CameraService() { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 277 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 278 | mUidPolicy->unregisterSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 279 | mSensorPrivacyPolicy->unregisterSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 280 | mInjectionStatusListener->removeListener(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 283 | void CameraService::onNewProviderRegistered() { |
| 284 | enumerateProviders(); |
| 285 | } |
| 286 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 287 | void CameraService::filterAPI1SystemCameraLocked( |
| 288 | const std::vector<std::string> &normalDeviceIds) { |
| 289 | mNormalDeviceIdsWithoutSystemCamera.clear(); |
| 290 | for (auto &deviceId : normalDeviceIds) { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 291 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 292 | if (getSystemCameraKind(String8(deviceId.c_str()), &deviceKind) != OK) { |
| 293 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, deviceId.c_str()); |
| 294 | continue; |
| 295 | } |
| 296 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 297 | // All system camera ids will necessarily come after public camera |
| 298 | // device ids as per the HAL interface contract. |
| 299 | break; |
| 300 | } |
| 301 | mNormalDeviceIdsWithoutSystemCamera.push_back(deviceId); |
| 302 | } |
| 303 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, |
| 304 | mNormalDeviceIdsWithoutSystemCamera.size()); |
| 305 | } |
| 306 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 307 | status_t CameraService::getSystemCameraKind(const String8& cameraId, SystemCameraKind *kind) const { |
| 308 | auto state = getCameraState(cameraId); |
| 309 | if (state != nullptr) { |
| 310 | *kind = state->getSystemCameraKind(); |
| 311 | return OK; |
| 312 | } |
| 313 | // Hidden physical camera ids won't have CameraState |
| 314 | return mCameraProviderManager->getSystemCameraKind(cameraId.c_str(), kind); |
| 315 | } |
| 316 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 317 | void CameraService::updateCameraNumAndIds() { |
| 318 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 319 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); |
| 320 | // Excludes hidden secure cameras |
| 321 | mNumberOfCameras = |
| 322 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; |
| 323 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 324 | mNormalDeviceIds = |
| 325 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 326 | filterAPI1SystemCameraLocked(mNormalDeviceIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 329 | void CameraService::filterSPerfClassCharacteristicsLocked() { |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 330 | // To claim to be S Performance primary cameras, the cameras must be |
| 331 | // backward compatible. So performance class primary camera Ids must be API1 |
| 332 | // compatible. |
| 333 | bool firstRearCameraSeen = false, firstFrontCameraSeen = false; |
| 334 | for (const auto& cameraId : mNormalDeviceIdsWithoutSystemCamera) { |
| 335 | int facing = -1; |
| 336 | int orientation = 0; |
| 337 | String8 cameraId8(cameraId.c_str()); |
| 338 | getDeviceVersion(cameraId8, /*out*/&facing, /*out*/&orientation); |
| 339 | if (facing == -1) { |
| 340 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.c_str()); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | if ((facing == hardware::CAMERA_FACING_BACK && !firstRearCameraSeen) || |
| 345 | (facing == hardware::CAMERA_FACING_FRONT && !firstFrontCameraSeen)) { |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 346 | status_t res = mCameraProviderManager->filterSmallJpegSizes(cameraId); |
| 347 | if (res == OK) { |
| 348 | mPerfClassPrimaryCameraIds.insert(cameraId); |
| 349 | } else { |
| 350 | ALOGE("%s: Failed to filter small JPEG sizes for performance class primary " |
| 351 | "camera %s: %s(%d)", __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
| 352 | break; |
| 353 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 354 | |
| 355 | if (facing == hardware::CAMERA_FACING_BACK) { |
| 356 | firstRearCameraSeen = true; |
| 357 | } |
| 358 | if (facing == hardware::CAMERA_FACING_FRONT) { |
| 359 | firstFrontCameraSeen = true; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | if (firstRearCameraSeen && firstFrontCameraSeen) { |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 369 | void CameraService::addStates(const String8 id) { |
| 370 | std::string cameraId(id.c_str()); |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 371 | CameraResourceCost cost; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 372 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); |
| 373 | if (res != OK) { |
| 374 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); |
| 375 | return; |
| 376 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 377 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 378 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); |
| 379 | if (res != OK) { |
| 380 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); |
| 381 | return; |
| 382 | } |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 383 | std::vector<std::string> physicalCameraIds; |
| 384 | mCameraProviderManager->isLogicalCamera(cameraId, &physicalCameraIds); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 385 | std::set<String8> conflicting; |
| 386 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { |
| 387 | conflicting.emplace(String8(cost.conflictingDevices[i].c_str())); |
| 388 | } |
| 389 | |
| 390 | { |
| 391 | Mutex::Autolock lock(mCameraStatesLock); |
| 392 | mCameraStates.emplace(id, std::make_shared<CameraState>(id, cost.resourceCost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 393 | conflicting, deviceKind, physicalCameraIds)); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 397 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 398 | mTorchStatusMap.add(id, TorchModeStatus::AVAILABLE_OFF); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 399 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 400 | broadcastTorchModeStatus(id, TorchModeStatus::AVAILABLE_OFF, deviceKind); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 401 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 402 | |
| 403 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 404 | logDeviceAdded(id, "Device added"); |
| 405 | } |
| 406 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 407 | void CameraService::removeStates(const String8 id) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 408 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 409 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 410 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 411 | mTorchStatusMap.removeItem(id); |
| 412 | } |
| 413 | |
| 414 | { |
| 415 | Mutex::Autolock lock(mCameraStatesLock); |
| 416 | mCameraStates.erase(id); |
| 417 | } |
| 418 | } |
| 419 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 420 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 421 | CameraDeviceStatus newHalStatus) { |
| 422 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, |
| 423 | id.string(), newHalStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 424 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 425 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 426 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 427 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 428 | |
| 429 | if (state == nullptr) { |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 430 | if (newStatus == StatusInternal::PRESENT) { |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 431 | ALOGI("%s: Unknown camera ID %s, a new camera is added", |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 432 | __FUNCTION__, id.string()); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 433 | |
| 434 | // First add as absent to make sure clients are notified below |
| 435 | addStates(id); |
| 436 | |
| 437 | updateStatus(newStatus, id); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 438 | } else { |
| 439 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string()); |
| 440 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 441 | return; |
| 442 | } |
| 443 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 444 | StatusInternal oldStatus = state->getStatus(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 445 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 446 | if (oldStatus == newStatus) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 447 | 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] | 448 | return; |
| 449 | } |
| 450 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 451 | if (newStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 452 | logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 453 | newStatus)); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 454 | |
| 455 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 456 | // to this device until the status changes |
| 457 | updateStatus(StatusInternal::NOT_PRESENT, id); |
| 458 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 459 | sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 460 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 461 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 462 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 463 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 464 | // Remove cached shim parameters |
| 465 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 466 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 467 | // Remove online as well as offline client from the list of active clients, |
| 468 | // if they are present |
| 469 | clientToDisconnectOnline = removeClientLocked(id); |
| 470 | clientToDisconnectOffline = removeClientLocked(kOfflineDevice + id); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 471 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 472 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 473 | disconnectClient(id, clientToDisconnectOnline); |
| 474 | disconnectClient(kOfflineDevice + id, clientToDisconnectOffline); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 475 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 476 | removeStates(id); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 477 | } else { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 478 | if (oldStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 479 | logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 480 | newStatus)); |
| 481 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 482 | updateStatus(newStatus, id); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 483 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 484 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 485 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 486 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 487 | const String8& physicalId, |
| 488 | CameraDeviceStatus newHalStatus) { |
| 489 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", |
| 490 | __FUNCTION__, id.string(), physicalId.string(), newHalStatus); |
| 491 | |
| 492 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 493 | |
| 494 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 495 | |
| 496 | if (state == nullptr) { |
| 497 | ALOGE("%s: Physical camera id %s status change on a non-present ID %s", |
| 498 | __FUNCTION__, id.string(), physicalId.string()); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | StatusInternal logicalCameraStatus = state->getStatus(); |
| 503 | if (logicalCameraStatus != StatusInternal::PRESENT && |
| 504 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 505 | ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d", |
| 506 | __FUNCTION__, physicalId.string(), newHalStatus, logicalCameraStatus); |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | bool updated = false; |
| 511 | if (newStatus == StatusInternal::PRESENT) { |
| 512 | updated = state->removeUnavailablePhysicalId(physicalId); |
| 513 | } else { |
| 514 | updated = state->addUnavailablePhysicalId(physicalId); |
| 515 | } |
| 516 | |
| 517 | if (updated) { |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 518 | String8 idCombo = id + " : " + physicalId; |
| 519 | if (newStatus == StatusInternal::PRESENT) { |
| 520 | logDeviceAdded(idCombo, |
| 521 | String8::format("Device status changed to %d", newStatus)); |
| 522 | } else { |
| 523 | logDeviceRemoved(idCombo, |
| 524 | String8::format("Device status changed to %d", newStatus)); |
| 525 | } |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 526 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 527 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 528 | if (getSystemCameraKind(id, &deviceKind) != OK) { |
| 529 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.string()); |
| 530 | return; |
| 531 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 532 | String16 id16(id), physicalId16(physicalId); |
| 533 | Mutex::Autolock lock(mStatusListenerLock); |
| 534 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 535 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 536 | listener->getListenerPid(), listener->getListenerUid())) { |
| 537 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 538 | id.c_str()); |
| 539 | continue; |
| 540 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 541 | listener->getListener()->onPhysicalCameraStatusChanged(mapToInterface(newStatus), |
| 542 | id16, physicalId16); |
| 543 | } |
| 544 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 547 | void CameraService::disconnectClient(const String8& id, sp<BasicClient> clientToDisconnect) { |
| 548 | if (clientToDisconnect.get() != nullptr) { |
| 549 | ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL", |
| 550 | __FUNCTION__, id.string()); |
| 551 | // Notify the client of disconnection |
| 552 | clientToDisconnect->notifyError( |
| 553 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 554 | CaptureResultExtras{}); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 555 | clientToDisconnect->disconnect(); |
| 556 | } |
| 557 | } |
| 558 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 559 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 560 | TorchModeStatus newStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 561 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 562 | status_t res = getSystemCameraKind(cameraId, &systemCameraKind); |
| 563 | if (res != OK) { |
| 564 | ALOGE("%s: Could not get system camera kind for camera id %s", __FUNCTION__, |
| 565 | cameraId.string()); |
| 566 | return; |
| 567 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 568 | Mutex::Autolock al(mTorchStatusMutex); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 569 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 570 | } |
| 571 | |
Jayant Chowdhary | 46ef0f5 | 2021-10-05 14:36:13 -0700 | [diff] [blame] | 572 | |
| 573 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
| 574 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
| 575 | Mutex::Autolock al(mTorchStatusMutex); |
| 576 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
| 577 | } |
| 578 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 579 | void CameraService::broadcastTorchStrengthLevel(const String8& cameraId, |
| 580 | int32_t newStrengthLevel) { |
| 581 | Mutex::Autolock lock(mStatusListenerLock); |
| 582 | for (auto& i : mListenerList) { |
| 583 | i->getListener()->onTorchStrengthLevelChanged(String16{cameraId}, |
| 584 | newStrengthLevel); |
| 585 | } |
| 586 | } |
| 587 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 588 | void CameraService::onTorchStatusChangedLocked(const String8& cameraId, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 589 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 590 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
| 591 | __FUNCTION__, cameraId.string(), newStatus); |
| 592 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 593 | TorchModeStatus status; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 594 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 595 | if (res) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 596 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", |
| 597 | __FUNCTION__, cameraId.string(), strerror(-res), res); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 598 | return; |
| 599 | } |
| 600 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 601 | return; |
| 602 | } |
| 603 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 604 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 605 | if (res) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 606 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, |
| 607 | (uint32_t)newStatus, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 608 | return; |
| 609 | } |
| 610 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 611 | { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 612 | // Update battery life logging for flashlight |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 613 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 614 | auto iter = mTorchUidMap.find(cameraId); |
| 615 | if (iter != mTorchUidMap.end()) { |
| 616 | int oldUid = iter->second.second; |
| 617 | int newUid = iter->second.first; |
| 618 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 619 | if (oldUid != newUid) { |
| 620 | // 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] | 621 | if (status == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 622 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 623 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 624 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 625 | notifier.noteFlashlightOn(cameraId, newUid); |
| 626 | } |
| 627 | iter->second.second = newUid; |
| 628 | } else { |
| 629 | // If the UID has not changed, log the status |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 630 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 631 | notifier.noteFlashlightOn(cameraId, oldUid); |
| 632 | } else { |
| 633 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 638 | broadcastTorchModeStatus(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 641 | static bool hasPermissionsForSystemCamera(int callingPid, int callingUid, |
| 642 | bool logPermissionFailure = false) { |
| 643 | return checkPermission(sSystemCameraPermission, callingPid, callingUid, |
| 644 | logPermissionFailure) && |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 645 | checkPermission(sCameraPermission, callingPid, callingUid); |
| 646 | } |
| 647 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 648 | Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 649 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 650 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 651 | bool hasSystemCameraPermissions = |
| 652 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), |
| 653 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 654 | switch (type) { |
| 655 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 656 | if (hasSystemCameraPermissions) { |
| 657 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); |
| 658 | } else { |
| 659 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); |
| 660 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 661 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 662 | case CAMERA_TYPE_ALL: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 663 | if (hasSystemCameraPermissions) { |
| 664 | *numCameras = mNumberOfCameras; |
| 665 | } else { |
| 666 | *numCameras = mNumberOfCamerasWithoutSystemCamera; |
| 667 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 668 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 669 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 670 | ALOGW("%s: Unknown camera type %d", |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 671 | __FUNCTION__, type); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 672 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 673 | "Unknown camera type %d", type); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 674 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 675 | return Status::ok(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 678 | Status CameraService::getCameraInfo(int cameraId, |
| 679 | CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 680 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 681 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 682 | std::string cameraIdStr = cameraIdIntToStrLocked(cameraId); |
| 683 | if (shouldRejectSystemCameraConnection(String8(cameraIdStr.c_str()))) { |
| 684 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 685 | "characteristics for system only device %s: ", cameraIdStr.c_str()); |
| 686 | } |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 687 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 688 | if (!mInitialized) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 689 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 690 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 691 | "Camera subsystem is not available"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 692 | } |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 693 | bool hasSystemCameraPermissions = |
| 694 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), |
| 695 | CameraThreadState::getCallingUid()); |
| 696 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; |
| 697 | if (hasSystemCameraPermissions) { |
| 698 | cameraIdBound = mNumberOfCameras; |
| 699 | } |
| 700 | if (cameraId < 0 || cameraId >= cameraIdBound) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 701 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 702 | "CameraId is not valid"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 705 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 706 | status_t err = mCameraProviderManager->getCameraInfo( |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 707 | cameraIdStr.c_str(), cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 708 | if (err != OK) { |
| 709 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 710 | "Error retrieving camera info from device %d: %s (%d)", cameraId, |
| 711 | strerror(-err), err); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 712 | logServiceError(String8::format("Error retrieving camera info from device %d",cameraId), |
| 713 | ERROR_INVALID_OPERATION); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 714 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 715 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 716 | return ret; |
| 717 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 718 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 719 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 720 | const std::vector<std::string> *deviceIds = &mNormalDeviceIdsWithoutSystemCamera; |
| 721 | auto callingPid = CameraThreadState::getCallingPid(); |
| 722 | auto callingUid = CameraThreadState::getCallingUid(); |
Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 723 | if (checkPermission(sSystemCameraPermission, callingPid, callingUid, |
| 724 | /*logPermissionFailure*/false) || getpid() == callingPid) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 725 | deviceIds = &mNormalDeviceIds; |
| 726 | } |
| 727 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(deviceIds->size())) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 728 | ALOGE("%s: input id %d invalid: valid range (0, %zu)", |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 729 | __FUNCTION__, cameraIdInt, deviceIds->size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 730 | return std::string{}; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 731 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 732 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 733 | return (*deviceIds)[cameraIdInt]; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | String8 CameraService::cameraIdIntToStr(int cameraIdInt) { |
| 737 | Mutex::Autolock lock(mServiceLock); |
| 738 | return String8(cameraIdIntToStrLocked(cameraIdInt).c_str()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | Status CameraService::getCameraCharacteristics(const String16& cameraId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 742 | int targetSdkVersion, CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 743 | ATRACE_CALL(); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 744 | if (!cameraInfo) { |
| 745 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 746 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 749 | if (!mInitialized) { |
| 750 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 751 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 752 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 753 | "Camera subsystem is not available");; |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 756 | if (shouldRejectSystemCameraConnection(String8(cameraId))) { |
| 757 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 758 | "characteristics for system only device %s: ", String8(cameraId).string()); |
| 759 | } |
| 760 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 761 | Status ret{}; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 762 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 763 | |
| 764 | std::string cameraIdStr = String8(cameraId).string(); |
| 765 | bool overrideForPerfClass = |
| 766 | SessionConfigurationUtils::targetPerfClassPrimaryCamera(mPerfClassPrimaryCameraIds, |
| 767 | cameraIdStr, targetSdkVersion); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 768 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 769 | cameraIdStr, overrideForPerfClass, cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 770 | if (res != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 771 | if (res == NAME_NOT_FOUND) { |
| 772 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " |
| 773 | "characteristics for unknown device %s: %s (%d)", String8(cameraId).string(), |
| 774 | strerror(-res), res); |
| 775 | } else { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 776 | logServiceError(String8::format("Unable to retrieve camera characteristics for " |
| 777 | "device %s.", String8(cameraId).string()),ERROR_INVALID_OPERATION); |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 778 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " |
| 779 | "characteristics for device %s: %s (%d)", String8(cameraId).string(), |
| 780 | strerror(-res), res); |
| 781 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 782 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 783 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 784 | if (getSystemCameraKind(String8(cameraId), &deviceKind) != OK) { |
| 785 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, String8(cameraId).string()); |
| 786 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera kind " |
| 787 | "for device %s", String8(cameraId).string()); |
| 788 | } |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 789 | int callingPid = CameraThreadState::getCallingPid(); |
| 790 | int callingUid = CameraThreadState::getCallingUid(); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 791 | std::vector<int32_t> tagsRemoved; |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 792 | // If it's not calling from cameraserver, check the permission only if |
| 793 | // android.permission.CAMERA is required. If android.permission.SYSTEM_CAMERA was needed, |
| 794 | // it would've already been checked in shouldRejectSystemCameraConnection. |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 795 | if ((callingPid != getpid()) && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 796 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 797 | !checkPermission(sCameraPermission, callingPid, callingUid)) { |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 798 | res = cameraInfo->removePermissionEntries( |
| 799 | mCameraProviderManager->getProviderTagIdLocked(String8(cameraId).string()), |
| 800 | &tagsRemoved); |
| 801 | if (res != OK) { |
| 802 | cameraInfo->clear(); |
| 803 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to remove camera" |
| 804 | " characteristics needing camera permission for device %s: %s (%d)", |
| 805 | String8(cameraId).string(), strerror(-res), res); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | if (!tagsRemoved.empty()) { |
| 810 | res = cameraInfo->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, |
| 811 | tagsRemoved.data(), tagsRemoved.size()); |
| 812 | if (res != OK) { |
| 813 | cameraInfo->clear(); |
| 814 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to insert camera " |
| 815 | "keys needing permission for device %s: %s (%d)", String8(cameraId).string(), |
| 816 | strerror(-res), res); |
| 817 | } |
| 818 | } |
| 819 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 820 | return ret; |
| 821 | } |
| 822 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 823 | Status CameraService::getTorchStrengthLevel(const String16& cameraId, |
| 824 | int32_t* torchStrength) { |
| 825 | ATRACE_CALL(); |
| 826 | Mutex::Autolock l(mServiceLock); |
| 827 | if (!mInitialized) { |
| 828 | ALOGE("%s: Camera HAL couldn't be initialized.", __FUNCTION__); |
| 829 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera HAL couldn't be initialized."); |
| 830 | } |
| 831 | |
| 832 | if(torchStrength == NULL) { |
| 833 | ALOGE("%s: strength level must not be null.", __FUNCTION__); |
| 834 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Strength level should not be null."); |
| 835 | } |
| 836 | |
| 837 | status_t res = mCameraProviderManager->getTorchStrengthLevel(String8(cameraId).string(), |
| 838 | torchStrength); |
| 839 | if (res != OK) { |
| 840 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve torch " |
| 841 | "strength level for device %s: %s (%d)", String8(cameraId).string(), |
| 842 | strerror(-res), res); |
| 843 | } |
| 844 | ALOGI("%s: Torch strength level is: %d", __FUNCTION__, *torchStrength); |
| 845 | return Status::ok(); |
| 846 | } |
| 847 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 848 | String8 CameraService::getFormattedCurrentTime() { |
| 849 | time_t now = time(nullptr); |
| 850 | char formattedTime[64]; |
| 851 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
| 852 | return String8(formattedTime); |
| 853 | } |
| 854 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 855 | Status CameraService::getCameraVendorTagDescriptor( |
| 856 | /*out*/ |
| 857 | hardware::camera2::params::VendorTagDescriptor* desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 858 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 859 | if (!mInitialized) { |
| 860 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 861 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 862 | } |
Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 863 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 864 | if (globalDescriptor != nullptr) { |
| 865 | *desc = *(globalDescriptor.get()); |
| 866 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 867 | return Status::ok(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 868 | } |
| 869 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 870 | Status CameraService::getCameraVendorTagCache( |
| 871 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { |
| 872 | ATRACE_CALL(); |
| 873 | if (!mInitialized) { |
| 874 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 875 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 876 | "Camera subsystem not available"); |
| 877 | } |
| 878 | sp<VendorTagDescriptorCache> globalCache = |
| 879 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 880 | if (globalCache != nullptr) { |
| 881 | *cache = *(globalCache.get()); |
| 882 | } |
| 883 | return Status::ok(); |
| 884 | } |
| 885 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 886 | void CameraService::clearCachedVariables() { |
| 887 | BasicClient::BasicClient::sCameraService = nullptr; |
| 888 | } |
| 889 | |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 890 | int CameraService::getDeviceVersion(const String8& cameraId, int* facing, int* orientation) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 891 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 892 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 893 | int deviceVersion = 0; |
| 894 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 895 | status_t res; |
| 896 | hardware::hidl_version maxVersion{0,0}; |
| 897 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(), |
| 898 | &maxVersion); |
| 899 | if (res != OK) return -1; |
| 900 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 901 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 902 | hardware::CameraInfo info; |
| 903 | if (facing) { |
| 904 | res = mCameraProviderManager->getCameraInfo(cameraId.string(), &info); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 905 | if (res != OK) return -1; |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 906 | *facing = info.facing; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 907 | if (orientation) { |
| 908 | *orientation = info.orientation; |
| 909 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 910 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 911 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 912 | return deviceVersion; |
| 913 | } |
| 914 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 915 | Status CameraService::filterGetInfoErrorCode(status_t err) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 916 | switch(err) { |
| 917 | case NO_ERROR: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 918 | return Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 919 | case BAD_VALUE: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 920 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 921 | "CameraId is not valid for HAL module"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 922 | case NO_INIT: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 923 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 924 | "Camera device not available"); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 925 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 926 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 927 | "Camera HAL encountered error %d: %s", |
| 928 | err, strerror(-err)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 929 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 930 | } |
| 931 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 932 | Status CameraService::makeClient(const sp<CameraService>& cameraService, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 933 | const sp<IInterface>& cameraCb, const String16& packageName, bool systemNativeClient, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 934 | const std::optional<String16>& featureId, const String8& cameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 935 | int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 936 | int servicePid, int deviceVersion, apiLevel effectiveApiLevel, bool overrideForPerfClass, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 937 | /*out*/sp<BasicClient>* client) { |
| 938 | |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 939 | // Create CameraClient based on device version reported by the HAL. |
| 940 | switch(deviceVersion) { |
| 941 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 942 | ALOGE("Camera using old HAL version: %d", deviceVersion); |
| 943 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, |
| 944 | "Camera device \"%s\" HAL version %d no longer supported", |
| 945 | cameraId.string(), deviceVersion); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 946 | break; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 947 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 948 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 949 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 950 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 951 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 952 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 953 | case CAMERA_DEVICE_API_VERSION_3_6: |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 954 | case CAMERA_DEVICE_API_VERSION_3_7: |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 955 | case CAMERA_DEVICE_API_VERSION_3_8: |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 956 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 957 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 958 | *client = new Camera2Client(cameraService, tmp, packageName, featureId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 959 | cameraId, api1CameraId, facing, sensorOrientation, clientPid, clientUid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 960 | servicePid, overrideForPerfClass); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 961 | } else { // Camera2 API route |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 962 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = |
| 963 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 964 | *client = new CameraDeviceClient(cameraService, tmp, packageName, |
| 965 | systemNativeClient, featureId, cameraId, facing, sensorOrientation, |
| 966 | clientPid, clientUid, servicePid, overrideForPerfClass); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 967 | } |
| 968 | break; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 969 | default: |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 970 | // Should not be reachable |
| 971 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 972 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 973 | "Camera device \"%s\" has unknown HAL version %d", |
| 974 | cameraId.string(), deviceVersion); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 975 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 976 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 977 | } |
| 978 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 979 | String8 CameraService::toString(std::set<userid_t> intSet) { |
| 980 | String8 s(""); |
| 981 | bool first = true; |
| 982 | for (userid_t i : intSet) { |
| 983 | if (first) { |
| 984 | s.appendFormat("%d", i); |
| 985 | first = false; |
| 986 | } else { |
| 987 | s.appendFormat(", %d", i); |
| 988 | } |
| 989 | } |
| 990 | return s; |
| 991 | } |
| 992 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 993 | int32_t CameraService::mapToInterface(TorchModeStatus status) { |
| 994 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 995 | switch (status) { |
| 996 | case TorchModeStatus::NOT_AVAILABLE: |
| 997 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 998 | break; |
| 999 | case TorchModeStatus::AVAILABLE_OFF: |
| 1000 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 1001 | break; |
| 1002 | case TorchModeStatus::AVAILABLE_ON: |
| 1003 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
| 1004 | break; |
| 1005 | default: |
| 1006 | ALOGW("Unknown new flash status: %d", status); |
| 1007 | } |
| 1008 | return serviceStatus; |
| 1009 | } |
| 1010 | |
| 1011 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { |
| 1012 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; |
| 1013 | switch (status) { |
| 1014 | case CameraDeviceStatus::NOT_PRESENT: |
| 1015 | serviceStatus = StatusInternal::NOT_PRESENT; |
| 1016 | break; |
| 1017 | case CameraDeviceStatus::PRESENT: |
| 1018 | serviceStatus = StatusInternal::PRESENT; |
| 1019 | break; |
| 1020 | case CameraDeviceStatus::ENUMERATING: |
| 1021 | serviceStatus = StatusInternal::ENUMERATING; |
| 1022 | break; |
| 1023 | default: |
| 1024 | ALOGW("Unknown new HAL device status: %d", status); |
| 1025 | } |
| 1026 | return serviceStatus; |
| 1027 | } |
| 1028 | |
| 1029 | int32_t CameraService::mapToInterface(StatusInternal status) { |
| 1030 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1031 | switch (status) { |
| 1032 | case StatusInternal::NOT_PRESENT: |
| 1033 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 1034 | break; |
| 1035 | case StatusInternal::PRESENT: |
| 1036 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; |
| 1037 | break; |
| 1038 | case StatusInternal::ENUMERATING: |
| 1039 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; |
| 1040 | break; |
| 1041 | case StatusInternal::NOT_AVAILABLE: |
| 1042 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; |
| 1043 | break; |
| 1044 | case StatusInternal::UNKNOWN: |
| 1045 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; |
| 1046 | break; |
| 1047 | default: |
| 1048 | ALOGW("Unknown new internal device status: %d", status); |
| 1049 | } |
| 1050 | return serviceStatus; |
| 1051 | } |
| 1052 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1053 | Status CameraService::initializeShimMetadata(int cameraId) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1054 | int uid = CameraThreadState::getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1055 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1056 | String16 internalPackageName("cameraserver"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1057 | String8 id = String8::format("%d", cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1058 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1059 | sp<Client> tmp = nullptr; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1060 | if (!(ret = connectHelper<ICameraClient,Client>( |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1061 | sp<ICameraClient>{nullptr}, id, cameraId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1062 | internalPackageName, /*systemNativeClient*/ false, {}, uid, USE_CALLING_PID, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1063 | API_1, /*shimUpdateOnly*/ true, /*oomScoreOffset*/ 0, |
| 1064 | /*targetSdkVersion*/ __ANDROID_API_FUTURE__, /*out*/ tmp) |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1065 | ).isOk()) { |
| 1066 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string()); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1067 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1068 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1071 | Status CameraService::getLegacyParametersLazy(int cameraId, |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1072 | /*out*/ |
| 1073 | CameraParameters* parameters) { |
| 1074 | |
| 1075 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 1076 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1077 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1078 | |
| 1079 | if (parameters == NULL) { |
| 1080 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1081 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1084 | String8 id = String8::format("%d", cameraId); |
| 1085 | |
| 1086 | // Check if we already have parameters |
| 1087 | { |
| 1088 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1089 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1090 | auto cameraState = getCameraState(id); |
| 1091 | if (cameraState == nullptr) { |
| 1092 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1093 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1094 | "Invalid camera ID: %s", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1095 | } |
| 1096 | CameraParameters p = cameraState->getShimParams(); |
| 1097 | if (!p.isEmpty()) { |
| 1098 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1099 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1103 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1104 | ret = initializeShimMetadata(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1105 | CameraThreadState::restoreCallingIdentity(token); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1106 | if (!ret.isOk()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1107 | // Error already logged by callee |
| 1108 | return ret; |
| 1109 | } |
| 1110 | |
| 1111 | // Check for parameters again |
| 1112 | { |
| 1113 | // Scope for service lock |
| 1114 | Mutex::Autolock lock(mServiceLock); |
| 1115 | auto cameraState = getCameraState(id); |
| 1116 | if (cameraState == nullptr) { |
| 1117 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1118 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1119 | "Invalid camera ID: %s", id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1120 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1121 | CameraParameters p = cameraState->getShimParams(); |
| 1122 | if (!p.isEmpty()) { |
| 1123 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1124 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1128 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 1129 | __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1130 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1133 | // Can camera service trust the caller based on the calling UID? |
| 1134 | static bool isTrustedCallingUid(uid_t uid) { |
| 1135 | switch (uid) { |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1136 | case AID_MEDIA: // mediaserver |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1137 | case AID_CAMERASERVER: // cameraserver |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1138 | case AID_RADIO: // telephony |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1139 | return true; |
| 1140 | default: |
| 1141 | return false; |
| 1142 | } |
| 1143 | } |
| 1144 | |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 1145 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { |
| 1146 | PermissionController pc; |
| 1147 | uid = pc.getPackageUid(packageName, 0); |
| 1148 | if (uid <= 0) { |
| 1149 | ALOGE("Unknown package: '%s'", String8(packageName).string()); |
| 1150 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).string()); |
| 1151 | return BAD_VALUE; |
| 1152 | } |
| 1153 | |
| 1154 | if (userId < 0) { |
| 1155 | ALOGE("Invalid user: %d", userId); |
| 1156 | dprintf(err, "Invalid user: %d\n", userId); |
| 1157 | return BAD_VALUE; |
| 1158 | } |
| 1159 | |
| 1160 | uid = multiuser_get_uid(userId, uid); |
| 1161 | return NO_ERROR; |
| 1162 | } |
| 1163 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1164 | Status CameraService::validateConnectLocked(const String8& cameraId, |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1165 | const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, |
| 1166 | /*out*/int& originalClientPid) const { |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1167 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1168 | #ifdef __BRILLO__ |
| 1169 | UNUSED(clientName8); |
| 1170 | UNUSED(clientUid); |
| 1171 | UNUSED(clientPid); |
| 1172 | UNUSED(originalClientPid); |
| 1173 | #else |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1174 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, |
| 1175 | originalClientPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1176 | if (!allowed.isOk()) { |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1177 | return allowed; |
| 1178 | } |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1179 | #endif // __BRILLO__ |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1180 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1181 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1182 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1183 | if (!mInitialized) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1184 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 1185 | callingPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1186 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1187 | "No camera HAL module available to open camera device \"%s\"", cameraId.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1190 | if (getCameraState(cameraId) == nullptr) { |
| 1191 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1192 | cameraId.string()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1193 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1194 | "No camera device with ID \"%s\" available", cameraId.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1197 | status_t err = checkIfDeviceIsUsable(cameraId); |
| 1198 | if (err != NO_ERROR) { |
| 1199 | switch(err) { |
| 1200 | case -ENODEV: |
| 1201 | case -EBUSY: |
| 1202 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1203 | "No camera device with ID \"%s\" currently available", cameraId.string()); |
| 1204 | default: |
| 1205 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1206 | "Unknown error connecting to ID \"%s\"", cameraId.string()); |
| 1207 | } |
| 1208 | } |
| 1209 | return Status::ok(); |
Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1210 | } |
| 1211 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1212 | Status CameraService::validateClientPermissionsLocked(const String8& cameraId, |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1213 | const String8& clientName8, int& clientUid, int& clientPid, |
| 1214 | /*out*/int& originalClientPid) const { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1215 | int callingPid = CameraThreadState::getCallingPid(); |
| 1216 | int callingUid = CameraThreadState::getCallingUid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1217 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1218 | // Check if we can trust clientUid |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1219 | if (clientUid == USE_CALLING_UID) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1220 | clientUid = callingUid; |
| 1221 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1222 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1223 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1224 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1225 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1226 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1227 | callingPid, callingUid, cameraId.string(), |
| 1228 | clientName8.string(), clientUid, clientPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1231 | // Check if we can trust clientPid |
| 1232 | if (clientPid == USE_CALLING_PID) { |
| 1233 | clientPid = callingPid; |
| 1234 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1235 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1236 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1237 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1238 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1239 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1240 | callingPid, callingUid, cameraId.string(), |
| 1241 | clientName8.string(), clientUid, clientPid); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1244 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1245 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", |
| 1246 | cameraId.c_str()); |
| 1247 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, "No camera device with ID \"%s\" is" |
| 1248 | "available", cameraId.string()); |
| 1249 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1250 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1251 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 1252 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 1253 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "No camera device with ID \"%s\"" |
| 1254 | "found while trying to query device kind", cameraId.string()); |
| 1255 | |
| 1256 | } |
| 1257 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1258 | // If it's not calling from cameraserver, check the permission if the |
| 1259 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for |
| 1260 | // android.permission.SYSTEM_CAMERA for system only camera devices). |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1261 | if (callingPid != getpid() && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1262 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1263 | !checkPermission(sCameraPermission, clientPid, clientUid)) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1264 | 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] | 1265 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1266 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission", |
| 1267 | clientName8.string(), clientUid, clientPid, cameraId.string()); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1270 | // 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] | 1271 | if (!mUidPolicy->isUidActive(callingUid, String16(clientName8))) { |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1272 | int32_t procState = mUidPolicy->getProcState(callingUid); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1273 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", |
| 1274 | clientPid, clientUid); |
| 1275 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1276 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" |
| 1277 | "calling UID %d proc state %" PRId32 ")", |
| 1278 | clientName8.string(), clientUid, clientPid, cameraId.string(), |
| 1279 | callingUid, procState); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1282 | // If sensor privacy is enabled then prevent access to the camera |
| 1283 | if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
| 1284 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); |
| 1285 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1286 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" when sensor privacy " |
| 1287 | "is enabled", clientName8.string(), clientUid, clientPid, cameraId.string()); |
| 1288 | } |
| 1289 | |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1290 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's |
| 1291 | // connected to camera service directly. |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1292 | originalClientPid = clientPid; |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1293 | clientPid = callingPid; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1294 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1295 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1296 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1297 | // For non-system clients : Only allow clients who are being used by the current foreground |
| 1298 | // device user, unless calling from our own process. |
| 1299 | if (!doesClientHaveSystemUid() && callingPid != getpid() && |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1300 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1301 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 1302 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
| 1303 | toString(mAllowedUsers).string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1304 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1305 | "Callers from device user %d are not currently allowed to connect to camera \"%s\"", |
| 1306 | clientUserId, cameraId.string()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1309 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const { |
| 1313 | auto cameraState = getCameraState(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1314 | int callingPid = CameraThreadState::getCallingPid(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1315 | if (cameraState == nullptr) { |
| 1316 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1317 | cameraId.string()); |
| 1318 | return -ENODEV; |
| 1319 | } |
| 1320 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1321 | StatusInternal currentStatus = cameraState->getStatus(); |
| 1322 | if (currentStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1323 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
| 1324 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1325 | return -ENODEV; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1326 | } else if (currentStatus == StatusInternal::ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1327 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
| 1328 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1329 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1330 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1331 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1332 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1335 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1336 | const CameraService::DescriptorPtr& desc, int oomScoreOffset, bool systemNativeClient) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1337 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1338 | // Make a descriptor for the incoming client |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1339 | auto clientDescriptor = |
| 1340 | CameraService::CameraClientManager::makeClientDescriptor(client, desc, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1341 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1342 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 1343 | |
| 1344 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
| 1345 | String8(client->getPackageName())); |
| 1346 | |
| 1347 | if (evicted.size() > 0) { |
| 1348 | // This should never happen - clients should already have been removed in disconnect |
| 1349 | for (auto& i : evicted) { |
| 1350 | ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect", |
| 1351 | __FUNCTION__, i->getKey().string()); |
| 1352 | } |
| 1353 | |
| 1354 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 1355 | __FUNCTION__); |
| 1356 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1357 | |
| 1358 | // And register a death notification for the client callback. Do |
| 1359 | // this last to avoid Binder policy where a nested Binder |
| 1360 | // transaction might be pre-empted to service the client death |
| 1361 | // notification if the client process dies before linkToDeath is |
| 1362 | // invoked. |
| 1363 | sp<IBinder> remoteCallback = client->getRemote(); |
| 1364 | if (remoteCallback != nullptr) { |
| 1365 | remoteCallback->linkToDeath(this); |
| 1366 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid, |
| 1370 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1371 | int oomScoreOffset, bool systemNativeClient, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1372 | /*out*/ |
| 1373 | sp<BasicClient>* client, |
| 1374 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1375 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1376 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1377 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1378 | DescriptorPtr clientDescriptor; |
| 1379 | { |
| 1380 | if (effectiveApiLevel == API_1) { |
| 1381 | // If we are using API1, any existing client for this camera ID with the same remote |
| 1382 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 1383 | |
| 1384 | auto current = mActiveClientManager.get(cameraId); |
| 1385 | if (current != nullptr) { |
| 1386 | auto clientSp = current->getValue(); |
| 1387 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1388 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
Shuzhen Wang | b2d43f6 | 2021-08-25 14:01:11 -0700 | [diff] [blame] | 1389 | ALOGW("CameraService connect called with a different" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1390 | " API level, evicting prior client..."); |
| 1391 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1392 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1393 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1394 | *client = clientSp; |
| 1395 | return NO_ERROR; |
| 1396 | } |
| 1397 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1398 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1399 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 1400 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1401 | // Get current active client PIDs |
| 1402 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 1403 | ownerPids.push_back(clientPid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1404 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1405 | std::vector<int> priorityScores(ownerPids.size()); |
| 1406 | std::vector<int> states(ownerPids.size()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1407 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1408 | // Get priority scores of all active PIDs |
| 1409 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids( |
| 1410 | ownerPids.size(), &ownerPids[0], /*out*/&states[0], |
| 1411 | /*out*/&priorityScores[0]); |
| 1412 | if (err != OK) { |
| 1413 | ALOGE("%s: Priority score query failed: %d", |
| 1414 | __FUNCTION__, err); |
| 1415 | return err; |
| 1416 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1417 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1418 | // Update all active clients' priorities |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1419 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1420 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1421 | pidToPriorityMap.emplace(ownerPids[i], |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 1422 | resource_policy::ClientPriority(priorityScores[i], states[i], |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1423 | /* isVendorClient won't get copied over*/ false, |
| 1424 | /* oomScoreOffset won't get copied over*/ 0)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1425 | } |
| 1426 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1427 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1428 | // Get state for the given cameraId |
| 1429 | auto state = getCameraState(cameraId); |
| 1430 | if (state == nullptr) { |
| 1431 | ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", |
| 1432 | clientPid, cameraId.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1433 | // Should never get here because validateConnectLocked should have errored out |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1434 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1435 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1436 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1437 | int32_t actualScore = priorityScores[priorityScores.size() - 1]; |
| 1438 | int32_t actualState = states[states.size() - 1]; |
| 1439 | |
| 1440 | // Make descriptor for incoming client. We store the oomScoreOffset |
| 1441 | // since we might need it later on new handleEvictionsLocked and |
| 1442 | // ProcessInfoService would not take that into account. |
Shuzhen Wang | 2db86ff | 2018-04-25 01:11:17 +0000 | [diff] [blame] | 1443 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1444 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1445 | state->getConflicting(), actualScore, clientPid, actualState, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1446 | oomScoreOffset, systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1447 | |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1448 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); |
| 1449 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1450 | // Find clients that would be evicted |
| 1451 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 1452 | |
| 1453 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 1454 | // background, so we cannot do evictions |
| 1455 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 1456 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 1457 | " priority).", clientPid); |
| 1458 | |
| 1459 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1460 | String8 curTime = getFormattedCurrentTime(); |
| 1461 | auto incompatibleClients = |
| 1462 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 1463 | |
| 1464 | 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] | 1465 | "(PID %d, score %d state %d) due to eviction policy", curTime.string(), |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1466 | cameraId.string(), packageName.string(), clientPid, |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1467 | clientPriority.getScore(), clientPriority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1468 | |
| 1469 | for (auto& i : incompatibleClients) { |
| 1470 | msg.appendFormat("\n - Blocked by existing device %s client for package %s" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1471 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", |
| 1472 | i->getKey().string(), |
| 1473 | String8{i->getValue()->getPackageName()}.string(), |
| 1474 | i->getOwnerId(), i->getPriority().getScore(), |
| 1475 | i->getPriority().getState()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1476 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1477 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(), |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1478 | String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1479 | i->getPriority().getScore(), i->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1483 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1484 | mEventLog.add(msg); |
| 1485 | |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1486 | auto current = mActiveClientManager.get(cameraId); |
| 1487 | if (current != nullptr) { |
| 1488 | return -EBUSY; // CAMERA_IN_USE |
| 1489 | } else { |
| 1490 | return -EUSERS; // MAX_CAMERAS_IN_USE |
| 1491 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | for (auto& i : evicted) { |
| 1495 | sp<BasicClient> clientSp = i->getValue(); |
| 1496 | if (clientSp.get() == nullptr) { |
| 1497 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 1498 | |
| 1499 | // TODO: Remove this |
| 1500 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 1501 | __FUNCTION__); |
| 1502 | mActiveClientManager.remove(i); |
| 1503 | continue; |
| 1504 | } |
| 1505 | |
| 1506 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
| 1507 | i->getKey().string()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1508 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1509 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1510 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1511 | logEvent(String8::format("EVICT device %s client held by package %s (PID" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1512 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" |
| 1513 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1514 | i->getKey().string(), String8{clientSp->getPackageName()}.string(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1515 | i->getOwnerId(), i->getPriority().getScore(), |
| 1516 | i->getPriority().getState(), cameraId.string(), |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1517 | packageName.string(), clientPid, clientPriority.getScore(), |
| 1518 | clientPriority.getState())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1519 | |
| 1520 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1521 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1522 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1523 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1526 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 1527 | // other clients from connecting in mServiceLockWrapper if held |
| 1528 | mServiceLock.unlock(); |
| 1529 | |
| 1530 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1531 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1532 | |
| 1533 | // Destroy evicted clients |
| 1534 | for (auto& i : evictedClients) { |
| 1535 | // 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] | 1536 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1537 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1538 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1539 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1540 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1541 | for (const auto& i : evictedClients) { |
| 1542 | ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")", |
| 1543 | __FUNCTION__, i->getKey().string(), i->getOwnerId()); |
| 1544 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 1545 | if (ret == TIMED_OUT) { |
| 1546 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
| 1547 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), |
| 1548 | mActiveClientManager.toString().string()); |
| 1549 | return -EBUSY; |
| 1550 | } |
| 1551 | if (ret != NO_ERROR) { |
| 1552 | ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), " |
| 1553 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret), |
| 1554 | ret, mActiveClientManager.toString().string()); |
| 1555 | return ret; |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | evictedClients.clear(); |
| 1560 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1561 | // Once clients have been disconnected, relock |
| 1562 | mServiceLock.lock(); |
| 1563 | |
| 1564 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 1565 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 1566 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1567 | } |
| 1568 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1569 | *partial = clientDescriptor; |
| 1570 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1573 | Status CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1574 | const sp<ICameraClient>& cameraClient, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1575 | int api1CameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1576 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1577 | int clientUid, |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1578 | int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1579 | int targetSdkVersion, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1580 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1581 | sp<ICamera>* device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1582 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1583 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1584 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1585 | |
| 1586 | String8 id = cameraIdIntToStr(api1CameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1587 | sp<Client> client = nullptr; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1588 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1589 | clientPackageName,/*systemNativeClient*/ false, {}, clientUid, clientPid, API_1, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1590 | /*shimUpdateOnly*/ false, /*oomScoreOffset*/ 0, targetSdkVersion, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1591 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1592 | if(!ret.isOk()) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1593 | logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageName), |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1594 | ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1595 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1598 | *device = client; |
| 1599 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1602 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, |
| 1603 | bool isVendorListener, int clientPid, int clientUid) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1604 | // If the client is not a vendor client, don't add listener if |
| 1605 | // a) the camera is a publicly hidden secure camera OR |
| 1606 | // b) the camera is a system only camera and the client doesn't |
| 1607 | // have android.permission.SYSTEM_CAMERA permissions. |
| 1608 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || |
| 1609 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
| 1610 | !hasPermissionsForSystemCamera(clientPid, clientUid)))) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 1611 | return true; |
| 1612 | } |
| 1613 | return false; |
| 1614 | } |
| 1615 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1616 | bool CameraService::shouldRejectSystemCameraConnection(const String8& cameraId) const { |
| 1617 | // Rules for rejection: |
| 1618 | // 1) If cameraserver tries to access this camera device, accept the |
| 1619 | // connection. |
| 1620 | // 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] | 1621 | // non system component is trying to access it. |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1622 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY |
| 1623 | // and the serving thread is a non hwbinder thread, the client must have |
| 1624 | // android.permission.SYSTEM_CAMERA permissions to connect. |
| 1625 | |
| 1626 | int cPid = CameraThreadState::getCallingPid(); |
| 1627 | int cUid = CameraThreadState::getCallingUid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1628 | bool systemClient = doesClientHaveSystemUid(); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1629 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 1630 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1631 | // This isn't a known camera ID, so it's not a system camera |
| 1632 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); |
| 1633 | return false; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1634 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1635 | |
| 1636 | // (1) Cameraserver trying to connect, accept. |
| 1637 | if (CameraThreadState::getCallingPid() == getpid()) { |
| 1638 | return false; |
| 1639 | } |
| 1640 | // (2) |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1641 | if (!systemClient && systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1642 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); |
| 1643 | return true; |
| 1644 | } |
| 1645 | // (3) Here we only check for permissions if it is a system only camera device. This is since |
| 1646 | // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some |
| 1647 | // characteristics) even if clients don't have android.permission.CAMERA. We do not want the |
| 1648 | // same behavior for system camera devices. |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1649 | if (!systemClient && systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 1650 | !hasPermissionsForSystemCamera(cPid, cUid, /*logPermissionFailure*/true)) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1651 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", |
| 1652 | cameraId.c_str()); |
| 1653 | return true; |
| 1654 | } |
| 1655 | |
| 1656 | return false; |
| 1657 | } |
| 1658 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1659 | Status CameraService::connectDevice( |
| 1660 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1661 | const String16& cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1662 | const String16& clientPackageName, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1663 | const std::optional<String16>& clientFeatureId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1664 | int clientUid, int oomScoreOffset, int targetSdkVersion, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1665 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1666 | sp<hardware::camera2::ICameraDeviceUser>* device) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1667 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1668 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1669 | Status ret = Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1670 | String8 id = String8(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1671 | sp<CameraDeviceClient> client = nullptr; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1672 | String16 clientPackageNameAdj = clientPackageName; |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1673 | int callingPid = CameraThreadState::getCallingPid(); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1674 | bool systemNativeClient = false; |
| 1675 | if (doesClientHaveSystemUid() && (clientPackageNameAdj.size() == 0)) { |
| 1676 | std::string systemClient = |
| 1677 | StringPrintf("client.pid<%d>", CameraThreadState::getCallingPid()); |
| 1678 | clientPackageNameAdj = String16(systemClient.c_str()); |
| 1679 | systemNativeClient = true; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1680 | } |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1681 | |
| 1682 | if (oomScoreOffset < 0) { |
| 1683 | String8 msg = |
| 1684 | String8::format("Cannot increase the priority of a client %s pid %d for " |
| 1685 | "camera id %s", String8(clientPackageNameAdj).string(), callingPid, |
| 1686 | id.string()); |
| 1687 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1688 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 1689 | } |
| 1690 | |
| 1691 | // enforce system camera permissions |
| 1692 | if (oomScoreOffset > 0 && |
| 1693 | !hasPermissionsForSystemCamera(callingPid, CameraThreadState::getCallingUid())) { |
| 1694 | String8 msg = |
| 1695 | String8::format("Cannot change the priority of a client %s pid %d for " |
| 1696 | "camera id %s without SYSTEM_CAMERA permissions", |
| 1697 | String8(clientPackageNameAdj).string(), callingPid, id.string()); |
| 1698 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1699 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, msg.string()); |
| 1700 | } |
| 1701 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1702 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1703 | /*api1CameraId*/-1, clientPackageNameAdj, systemNativeClient,clientFeatureId, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1704 | clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, oomScoreOffset, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1705 | targetSdkVersion, /*out*/client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1706 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1707 | if(!ret.isOk()) { |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1708 | logRejected(id, callingPid, String8(clientPackageNameAdj), ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1709 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1710 | } |
| 1711 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1712 | *device = client; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 1713 | Mutex::Autolock lock(mServiceLock); |
| 1714 | |
| 1715 | // Clear the previous cached logs and reposition the |
| 1716 | // file offset to beginning of the file to log new data. |
| 1717 | // If either truncate or lseek fails, close the previous file and create a new one. |
| 1718 | if ((ftruncate(mMemFd, 0) == -1) || (lseek(mMemFd, 0, SEEK_SET) == -1)) { |
| 1719 | ALOGE("%s: Error while truncating the file: %s", __FUNCTION__, sFileName); |
| 1720 | // Close the previous memfd. |
| 1721 | close(mMemFd); |
| 1722 | // If failure to wipe the data, then create a new file and |
| 1723 | // assign the new value to mMemFd. |
| 1724 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 1725 | if (mMemFd == -1) { |
| 1726 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 1727 | } |
| 1728 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1729 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1730 | } |
| 1731 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1732 | template<class CALLBACK, class CLIENT> |
| 1733 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1734 | int api1CameraId, const String16& clientPackageName, bool systemNativeClient, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1735 | const std::optional<String16>& clientFeatureId, int clientUid, int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1736 | apiLevel effectiveApiLevel, bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1737 | /*out*/sp<CLIENT>& device) { |
| 1738 | binder::Status ret = binder::Status::ok(); |
| 1739 | |
| 1740 | String8 clientName8(clientPackageName); |
| 1741 | |
| 1742 | int originalClientPid = 0; |
| 1743 | |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1744 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1745 | "Camera API version %d", clientPid, clientName8.string(), cameraId.string(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1746 | static_cast<int>(effectiveApiLevel)); |
| 1747 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1748 | nsecs_t openTimeNs = systemTime(); |
| 1749 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1750 | sp<CLIENT> client = nullptr; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1751 | int facing = -1; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1752 | int orientation = 0; |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1753 | bool isNonSystemNdk = (clientPackageName.size() == 0); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1754 | { |
| 1755 | // Acquire mServiceLock and prevent other clients from connecting |
| 1756 | std::unique_ptr<AutoConditionLock> lock = |
| 1757 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 1758 | |
| 1759 | if (lock == nullptr) { |
| 1760 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." |
| 1761 | , clientPid); |
| 1762 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1763 | "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting", |
| 1764 | cameraId.string(), clientName8.string(), clientPid); |
| 1765 | } |
| 1766 | |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 1767 | // Enforce client permissions and do basic validity checks |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1768 | if(!(ret = validateConnectLocked(cameraId, clientName8, |
| 1769 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { |
| 1770 | return ret; |
| 1771 | } |
| 1772 | |
| 1773 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 1774 | // we were doing a shim update, return immediately |
| 1775 | if (shimUpdateOnly) { |
| 1776 | auto cameraState = getCameraState(cameraId); |
| 1777 | if (cameraState != nullptr) { |
| 1778 | if (!cameraState->getShimParams().isEmpty()) return ret; |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | status_t err; |
| 1783 | |
| 1784 | sp<BasicClient> clientTmp = nullptr; |
| 1785 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial; |
| 1786 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1787 | IInterface::asBinder(cameraCb), clientName8, oomScoreOffset, systemNativeClient, |
| 1788 | /*out*/&clientTmp, /*out*/&partial)) != NO_ERROR) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1789 | switch (err) { |
| 1790 | case -ENODEV: |
| 1791 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1792 | "No camera device with ID \"%s\" currently available", |
| 1793 | cameraId.string()); |
| 1794 | case -EBUSY: |
| 1795 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 1796 | "Higher-priority client using camera, ID \"%s\" currently unavailable", |
| 1797 | cameraId.string()); |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1798 | case -EUSERS: |
| 1799 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1800 | "Too many cameras already open, cannot open camera \"%s\"", |
| 1801 | cameraId.string()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1802 | default: |
| 1803 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1804 | "Unexpected error %s (%d) opening camera \"%s\"", |
| 1805 | strerror(-err), err, cameraId.string()); |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | if (clientTmp.get() != nullptr) { |
| 1810 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 1811 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 1812 | return ret; |
| 1813 | } |
| 1814 | |
| 1815 | // give flashlight a chance to close devices if necessary. |
| 1816 | mFlashlight->prepareDeviceOpen(cameraId); |
| 1817 | |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1818 | int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing, /*out*/&orientation); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 1819 | if (facing == -1) { |
| 1820 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string()); |
| 1821 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1822 | "Unable to get camera device \"%s\" facing", cameraId.string()); |
| 1823 | } |
| 1824 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1825 | sp<BasicClient> tmp = nullptr; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1826 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
| 1827 | mPerfClassPrimaryCameraIds, cameraId.string(), targetSdkVersion); |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1828 | if(!(ret = makeClient(this, cameraCb, clientPackageName, systemNativeClient, |
| 1829 | clientFeatureId, cameraId, api1CameraId, facing, orientation, |
Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 1830 | clientPid, clientUid, getpid(), |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1831 | deviceVersion, effectiveApiLevel, overrideForPerfClass, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1832 | /*out*/&tmp)).isOk()) { |
| 1833 | return ret; |
| 1834 | } |
| 1835 | client = static_cast<CLIENT*>(tmp.get()); |
| 1836 | |
| 1837 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 1838 | __FUNCTION__); |
| 1839 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 1840 | String8 monitorTags = isClientWatched(client.get()) ? mMonitorTags : String8(""); |
| 1841 | err = client->initialize(mCameraProviderManager, monitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1842 | if (err != OK) { |
| 1843 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1844 | // Errors could be from the HAL module open call or from AppOpsManager |
| 1845 | switch(err) { |
| 1846 | case BAD_VALUE: |
| 1847 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1848 | "Illegal argument to HAL module for camera \"%s\"", cameraId.string()); |
| 1849 | case -EBUSY: |
| 1850 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 1851 | "Camera \"%s\" is already open", cameraId.string()); |
| 1852 | case -EUSERS: |
| 1853 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1854 | "Too many cameras already open, cannot open camera \"%s\"", |
| 1855 | cameraId.string()); |
| 1856 | case PERMISSION_DENIED: |
| 1857 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1858 | "No permission to open camera \"%s\"", cameraId.string()); |
| 1859 | case -EACCES: |
| 1860 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1861 | "Camera \"%s\" disabled by policy", cameraId.string()); |
| 1862 | case -ENODEV: |
| 1863 | default: |
| 1864 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1865 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(), |
| 1866 | strerror(-err), err); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | // Update shim paremeters for legacy clients |
| 1871 | if (effectiveApiLevel == API_1) { |
| 1872 | // Assume we have always received a Client subclass for API1 |
| 1873 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 1874 | String8 rawParams = shimClient->getParameters(); |
| 1875 | CameraParameters params(rawParams); |
| 1876 | |
| 1877 | auto cameraState = getCameraState(cameraId); |
| 1878 | if (cameraState != nullptr) { |
| 1879 | cameraState->setShimParams(params); |
| 1880 | } else { |
| 1881 | ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.", |
| 1882 | __FUNCTION__, cameraId.string()); |
| 1883 | } |
| 1884 | } |
| 1885 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1886 | // Set rotate-and-crop override behavior |
| 1887 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { |
| 1888 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame^] | 1889 | } else { |
Emilian Peev | 065b2c1 | 2021-11-23 13:12:57 -0800 | [diff] [blame] | 1890 | client->setRotateAndCropOverride( |
| 1891 | CameraServiceProxyWrapper::getRotateAndCropOverride( |
| 1892 | clientPackageName, facing, multiuser_get_user_id(clientUid))); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1893 | } |
| 1894 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 1895 | // Set camera muting behavior |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 1896 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 1897 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 1898 | if (client->supportsCameraMute()) { |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 1899 | client->setCameraMute( |
| 1900 | mOverrideCameraMuteMode || isCameraPrivacyEnabled); |
| 1901 | } else if (isCameraPrivacyEnabled) { |
| 1902 | // no camera mute supported, but privacy is on! => disconnect |
| 1903 | ALOGI("Camera mute not supported for package: %s, camera id: %s", |
| 1904 | String8(client->getPackageName()).string(), cameraId.string()); |
| 1905 | // Do not hold mServiceLock while disconnecting clients, but |
| 1906 | // retain the condition blocking other clients from connecting |
| 1907 | // in mServiceLockWrapper if held. |
| 1908 | mServiceLock.unlock(); |
| 1909 | // Clear caller identity temporarily so client disconnect PID |
| 1910 | // checks work correctly |
| 1911 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 1912 | // Note AppOp to trigger the "Unblock" dialog |
| 1913 | client->noteAppOp(); |
| 1914 | client->disconnect(); |
| 1915 | CameraThreadState::restoreCallingIdentity(token); |
| 1916 | // Reacquire mServiceLock |
| 1917 | mServiceLock.lock(); |
| 1918 | |
| 1919 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1920 | "Camera \"%s\" disabled due to camera mute", cameraId.string()); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 1921 | } |
| 1922 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1923 | if (shimUpdateOnly) { |
| 1924 | // If only updating legacy shim parameters, immediately disconnect client |
| 1925 | mServiceLock.unlock(); |
| 1926 | client->disconnect(); |
| 1927 | mServiceLock.lock(); |
| 1928 | } else { |
| 1929 | // Otherwise, add client to active clients list |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1930 | finishConnectLocked(client, partial, oomScoreOffset, systemNativeClient); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1931 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 1932 | |
| 1933 | client->setImageDumpMask(mImageDumpMask); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1934 | } // lock is destroyed, allow further connect calls |
| 1935 | |
| 1936 | // Important: release the mutex here so the client can call back into the service from its |
| 1937 | // destructor (can be at the end of the call) |
| 1938 | device = client; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1939 | |
| 1940 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); |
| 1941 | CameraServiceProxyWrapper::logOpen(cameraId, facing, clientPackageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1942 | effectiveApiLevel, isNonSystemNdk, openLatencyMs); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1943 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 1944 | { |
| 1945 | Mutex::Autolock lock(mInjectionParametersLock); |
| 1946 | if (cameraId == mInjectionInternalCamId && mInjectionInitPending) { |
| 1947 | mInjectionInitPending = false; |
| 1948 | status_t res = NO_ERROR; |
| 1949 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 1950 | if (clientDescriptor != nullptr) { |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 1951 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1952 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 1953 | if(res != OK) { |
| 1954 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1955 | "No camera device with ID \"%s\" currently available", |
| 1956 | mInjectionExternalCamId.string()); |
| 1957 | } |
| 1958 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 1959 | if (res != OK) { |
| 1960 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 1961 | } |
| 1962 | } else { |
| 1963 | ALOGE("%s: Internal camera ID = %s 's client does not exist!", |
| 1964 | __FUNCTION__, mInjectionInternalCamId.string()); |
| 1965 | res = NO_INIT; |
| 1966 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 1967 | } |
| 1968 | } |
| 1969 | } |
| 1970 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1971 | return ret; |
| 1972 | } |
| 1973 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 1974 | status_t CameraService::addOfflineClient(String8 cameraId, sp<BasicClient> offlineClient) { |
| 1975 | if (offlineClient.get() == nullptr) { |
| 1976 | return BAD_VALUE; |
| 1977 | } |
| 1978 | |
| 1979 | { |
| 1980 | // Acquire mServiceLock and prevent other clients from connecting |
| 1981 | std::unique_ptr<AutoConditionLock> lock = |
| 1982 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 1983 | |
| 1984 | if (lock == nullptr) { |
| 1985 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." |
| 1986 | , __FUNCTION__, offlineClient->getClientPid()); |
| 1987 | return TIMED_OUT; |
| 1988 | } |
| 1989 | |
| 1990 | auto onlineClientDesc = mActiveClientManager.get(cameraId); |
| 1991 | if (onlineClientDesc.get() == nullptr) { |
| 1992 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, |
| 1993 | cameraId.c_str()); |
| 1994 | return BAD_VALUE; |
| 1995 | } |
| 1996 | |
| 1997 | // Offline clients do not evict or conflict with other online devices. Resource sharing |
| 1998 | // conflicts are handled by the camera provider which will either succeed or fail before |
| 1999 | // reaching this method. |
| 2000 | const auto& onlinePriority = onlineClientDesc->getPriority(); |
| 2001 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( |
| 2002 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, |
| 2003 | /*conflictingKeys*/ std::set<String8>(), onlinePriority.getScore(), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2004 | onlineClientDesc->getOwnerId(), onlinePriority.getState(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2005 | // native clients don't have offline processing support. |
| 2006 | /*ommScoreOffset*/ 0, /*systemNativeClient*/false); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2007 | |
| 2008 | // Allow only one offline device per camera |
| 2009 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); |
| 2010 | if (!incompatibleClients.empty()) { |
| 2011 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); |
| 2012 | return BAD_VALUE; |
| 2013 | } |
| 2014 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2015 | String8 monitorTags = isClientWatched(offlineClient.get()) ? mMonitorTags : String8(""); |
| 2016 | auto err = offlineClient->initialize(mCameraProviderManager, monitorTags); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2017 | if (err != OK) { |
| 2018 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); |
| 2019 | return err; |
| 2020 | } |
| 2021 | |
| 2022 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); |
| 2023 | if (evicted.size() > 0) { |
| 2024 | for (auto& i : evicted) { |
| 2025 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", |
| 2026 | __FUNCTION__, i->getKey().string()); |
| 2027 | } |
| 2028 | |
| 2029 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " |
| 2030 | "properly", __FUNCTION__); |
| 2031 | |
| 2032 | return BAD_VALUE; |
| 2033 | } |
| 2034 | |
| 2035 | logConnectedOffline(offlineClientDesc->getKey(), |
| 2036 | static_cast<int>(offlineClientDesc->getOwnerId()), |
| 2037 | String8(offlineClient->getPackageName())); |
| 2038 | |
| 2039 | sp<IBinder> remoteCallback = offlineClient->getRemote(); |
| 2040 | if (remoteCallback != nullptr) { |
| 2041 | remoteCallback->linkToDeath(this); |
| 2042 | } |
| 2043 | } // lock is destroyed, allow further connect calls |
| 2044 | |
| 2045 | return OK; |
| 2046 | } |
| 2047 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2048 | Status CameraService::turnOnTorchWithStrengthLevel(const String16& cameraId, int32_t torchStrength, |
| 2049 | const sp<IBinder>& clientBinder) { |
| 2050 | Mutex::Autolock lock(mServiceLock); |
| 2051 | |
| 2052 | ATRACE_CALL(); |
| 2053 | if (clientBinder == nullptr) { |
| 2054 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
| 2055 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2056 | "Torch client binder in null."); |
| 2057 | } |
| 2058 | |
| 2059 | String8 id = String8(cameraId.string()); |
| 2060 | int uid = CameraThreadState::getCallingUid(); |
| 2061 | |
| 2062 | if (shouldRejectSystemCameraConnection(id)) { |
| 2063 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to change the strength level" |
| 2064 | "for system only device %s: ", id.string()); |
| 2065 | } |
| 2066 | |
| 2067 | // verify id is valid |
| 2068 | auto state = getCameraState(id); |
| 2069 | if (state == nullptr) { |
| 2070 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
| 2071 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2072 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
| 2073 | } |
| 2074 | |
| 2075 | StatusInternal cameraStatus = state->getStatus(); |
| 2076 | if (cameraStatus != StatusInternal::NOT_AVAILABLE && |
| 2077 | cameraStatus != StatusInternal::PRESENT) { |
| 2078 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), |
| 2079 | (int)cameraStatus); |
| 2080 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2081 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
| 2082 | } |
| 2083 | |
| 2084 | { |
| 2085 | Mutex::Autolock al(mTorchStatusMutex); |
| 2086 | TorchModeStatus status; |
| 2087 | status_t err = getTorchStatusLocked(id, &status); |
| 2088 | if (err != OK) { |
| 2089 | if (err == NAME_NOT_FOUND) { |
| 2090 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2091 | "Camera \"%s\" does not have a flash unit", id.string()); |
| 2092 | } |
| 2093 | ALOGE("%s: getting current torch status failed for camera %s", |
| 2094 | __FUNCTION__, id.string()); |
| 2095 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2096 | "Error changing torch strength level for camera \"%s\": %s (%d)", |
| 2097 | id.string(), strerror(-err), err); |
| 2098 | } |
| 2099 | |
| 2100 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
| 2101 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
| 2102 | ALOGE("%s: torch mode of camera %s is not available because " |
| 2103 | "camera is in use.", __FUNCTION__, id.string()); |
| 2104 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2105 | "Torch for camera \"%s\" is not available due to an existing camera user", |
| 2106 | id.string()); |
| 2107 | } else { |
| 2108 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 2109 | "insufficient resources", __FUNCTION__, id.string()); |
| 2110 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2111 | "Torch for camera \"%s\" is not available due to insufficient resources", |
| 2112 | id.string()); |
| 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | { |
| 2118 | Mutex::Autolock al(mTorchUidMapMutex); |
| 2119 | updateTorchUidMapLocked(cameraId, uid); |
| 2120 | } |
| 2121 | // Check if the current torch strength level is same as the new one. |
| 2122 | bool shouldSkipTorchStrengthUpdates = mCameraProviderManager->shouldSkipTorchStrengthUpdate( |
| 2123 | id.string(), torchStrength); |
| 2124 | |
| 2125 | status_t err = mFlashlight->turnOnTorchWithStrengthLevel(id, torchStrength); |
| 2126 | |
| 2127 | if (err != OK) { |
| 2128 | int32_t errorCode; |
| 2129 | String8 msg; |
| 2130 | switch (err) { |
| 2131 | case -ENOSYS: |
| 2132 | msg = String8::format("Camera \"%s\" has no flashlight.", |
| 2133 | id.string()); |
| 2134 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2135 | break; |
| 2136 | case -EBUSY: |
| 2137 | msg = String8::format("Camera \"%s\" is in use", |
| 2138 | id.string()); |
| 2139 | errorCode = ERROR_CAMERA_IN_USE; |
| 2140 | break; |
Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 2141 | case -EINVAL: |
| 2142 | msg = String8::format("Torch strength level %d is not within the " |
| 2143 | "valid range.", torchStrength); |
| 2144 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2145 | break; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2146 | default: |
| 2147 | msg = String8::format("Changing torch strength level failed."); |
| 2148 | errorCode = ERROR_INVALID_OPERATION; |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2149 | } |
| 2150 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2151 | return STATUS_ERROR(errorCode, msg.string()); |
| 2152 | } |
| 2153 | |
| 2154 | { |
| 2155 | // update the link to client's death |
| 2156 | // Store the last client that turns on each camera's torch mode. |
| 2157 | Mutex::Autolock al(mTorchClientMapMutex); |
| 2158 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 2159 | if (index == NAME_NOT_FOUND) { |
| 2160 | mTorchClientMap.add(id, clientBinder); |
| 2161 | } else { |
| 2162 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
| 2163 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2164 | } |
| 2165 | clientBinder->linkToDeath(this); |
| 2166 | } |
| 2167 | |
| 2168 | int clientPid = CameraThreadState::getCallingPid(); |
| 2169 | const char *id_cstr = id.c_str(); |
| 2170 | ALOGI("%s: Torch strength for camera id %s changed to %d for client PID %d", |
| 2171 | __FUNCTION__, id_cstr, torchStrength, clientPid); |
| 2172 | if (!shouldSkipTorchStrengthUpdates) { |
| 2173 | broadcastTorchStrengthLevel(id, torchStrength); |
| 2174 | } |
| 2175 | return Status::ok(); |
| 2176 | } |
| 2177 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2178 | Status CameraService::setTorchMode(const String16& cameraId, bool enabled, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2179 | const sp<IBinder>& clientBinder) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2180 | Mutex::Autolock lock(mServiceLock); |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2181 | |
| 2182 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2183 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2184 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2185 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 2186 | "Torch client Binder is null"); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2187 | } |
| 2188 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2189 | String8 id = String8(cameraId.string()); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2190 | int uid = CameraThreadState::getCallingUid(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2191 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2192 | if (shouldRejectSystemCameraConnection(id)) { |
| 2193 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" |
| 2194 | " for system only device %s: ", id.string()); |
| 2195 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2196 | // verify id is valid. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2197 | auto state = getCameraState(id); |
| 2198 | if (state == nullptr) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 2199 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2200 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2201 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2202 | } |
| 2203 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2204 | StatusInternal cameraStatus = state->getStatus(); |
| 2205 | if (cameraStatus != StatusInternal::PRESENT && |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2206 | cameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 2207 | 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] | 2208 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2209 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | { |
| 2213 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2214 | TorchModeStatus status; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2215 | status_t err = getTorchStatusLocked(id, &status); |
| 2216 | if (err != OK) { |
| 2217 | if (err == NAME_NOT_FOUND) { |
| 2218 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2219 | "Camera \"%s\" does not have a flash unit", id.string()); |
| 2220 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2221 | ALOGE("%s: getting current torch status failed for camera %s", |
| 2222 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2223 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2224 | "Error updating torch status for camera \"%s\": %s (%d)", id.string(), |
| 2225 | strerror(-err), err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2226 | } |
| 2227 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2228 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2229 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2230 | ALOGE("%s: torch mode of camera %s is not available because " |
| 2231 | "camera is in use", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2232 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2233 | "Torch for camera \"%s\" is not available due to an existing camera user", |
| 2234 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2235 | } else { |
| 2236 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 2237 | "insufficient resources", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2238 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2239 | "Torch for camera \"%s\" is not available due to insufficient resources", |
| 2240 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2241 | } |
| 2242 | } |
| 2243 | } |
| 2244 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2245 | { |
| 2246 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 2247 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 2248 | Mutex::Autolock al(mTorchUidMapMutex); |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2249 | updateTorchUidMapLocked(cameraId, uid); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2250 | } |
| 2251 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2252 | status_t err = mFlashlight->setTorchMode(id, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2253 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2254 | if (err != OK) { |
| 2255 | int32_t errorCode; |
| 2256 | String8 msg; |
| 2257 | switch (err) { |
| 2258 | case -ENOSYS: |
| 2259 | msg = String8::format("Camera \"%s\" has no flashlight", |
| 2260 | id.string()); |
| 2261 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2262 | break; |
Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 2263 | case -EBUSY: |
| 2264 | msg = String8::format("Camera \"%s\" is in use", |
| 2265 | id.string()); |
| 2266 | errorCode = ERROR_CAMERA_IN_USE; |
| 2267 | break; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2268 | default: |
| 2269 | msg = String8::format( |
| 2270 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", |
| 2271 | id.string(), enabled, strerror(-err), err); |
| 2272 | errorCode = ERROR_INVALID_OPERATION; |
| 2273 | } |
| 2274 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2275 | logServiceError(msg,errorCode); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2276 | return STATUS_ERROR(errorCode, msg.string()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2277 | } |
| 2278 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2279 | { |
| 2280 | // update the link to client's death |
| 2281 | Mutex::Autolock al(mTorchClientMapMutex); |
| 2282 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 2283 | if (enabled) { |
| 2284 | if (index == NAME_NOT_FOUND) { |
| 2285 | mTorchClientMap.add(id, clientBinder); |
| 2286 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2287 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2288 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2289 | } |
| 2290 | clientBinder->linkToDeath(this); |
| 2291 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2292 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2293 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2294 | } |
| 2295 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 2296 | int clientPid = CameraThreadState::getCallingPid(); |
| 2297 | const char *id_cstr = id.c_str(); |
| 2298 | const char *torchState = enabled ? "on" : "off"; |
| 2299 | ALOGI("Torch for camera id %s turned %s for client PID %d", id_cstr, torchState, clientPid); |
| 2300 | logTorchEvent(id_cstr, torchState , clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2301 | return Status::ok(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2302 | } |
| 2303 | |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2304 | void CameraService::updateTorchUidMapLocked(const String16& cameraId, int uid) { |
| 2305 | String8 id = String8(cameraId.string()); |
| 2306 | if (mTorchUidMap.find(id) == mTorchUidMap.end()) { |
| 2307 | mTorchUidMap[id].first = uid; |
| 2308 | mTorchUidMap[id].second = uid; |
| 2309 | } else { |
| 2310 | // Set the pending UID |
| 2311 | mTorchUidMap[id].first = uid; |
| 2312 | } |
| 2313 | } |
| 2314 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2315 | Status CameraService::notifySystemEvent(int32_t eventId, |
| 2316 | const std::vector<int32_t>& args) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2317 | const int pid = CameraThreadState::getCallingPid(); |
| 2318 | const int selfPid = getpid(); |
| 2319 | |
| 2320 | // Permission checks |
| 2321 | if (pid != selfPid) { |
| 2322 | // Ensure we're being called by system_server, or similar process with |
| 2323 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2324 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2325 | const int uid = CameraThreadState::getCallingUid(); |
| 2326 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 2327 | " events from pid=%d, uid=%d", pid, uid); |
| 2328 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 2329 | "No permission to send updates to camera service about system events" |
| 2330 | " from pid=%d, uid=%d", pid, uid); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2331 | } |
| 2332 | } |
| 2333 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2334 | ATRACE_CALL(); |
| 2335 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2336 | switch(eventId) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2337 | case ICameraService::EVENT_USER_SWITCHED: { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2338 | // 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] | 2339 | // from a system server crash |
| 2340 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2341 | mSensorPrivacyPolicy->registerSelf(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2342 | doUserSwitch(/*newUserIds*/ args); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2343 | break; |
| 2344 | } |
Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 2345 | case ICameraService::EVENT_USB_DEVICE_ATTACHED: |
| 2346 | case ICameraService::EVENT_USB_DEVICE_DETACHED: { |
| 2347 | // Notify CameraProviderManager for lazy HALs |
| 2348 | mCameraProviderManager->notifyUsbDeviceEvent(eventId, |
| 2349 | std::to_string(args[0])); |
| 2350 | break; |
| 2351 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2352 | case ICameraService::EVENT_NONE: |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2353 | default: { |
| 2354 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 2355 | eventId); |
| 2356 | break; |
| 2357 | } |
| 2358 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2359 | return Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2360 | } |
| 2361 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2362 | void CameraService::notifyMonitoredUids() { |
| 2363 | Mutex::Autolock lock(mStatusListenerLock); |
| 2364 | |
| 2365 | for (const auto& it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2366 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2367 | if (!ret.isOk()) { |
| 2368 | ALOGE("%s: Failed to trigger permission callback: %d", __FUNCTION__, |
| 2369 | ret.exceptionCode()); |
| 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2374 | Status CameraService::notifyDeviceStateChange(int64_t newState) { |
| 2375 | const int pid = CameraThreadState::getCallingPid(); |
| 2376 | const int selfPid = getpid(); |
| 2377 | |
| 2378 | // Permission checks |
| 2379 | if (pid != selfPid) { |
| 2380 | // Ensure we're being called by system_server, or similar process with |
| 2381 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2382 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2383 | const int uid = CameraThreadState::getCallingUid(); |
| 2384 | ALOGE("Permission Denial: cannot send updates to camera service about device" |
| 2385 | " state changes from pid=%d, uid=%d", pid, uid); |
| 2386 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2387 | "No permission to send updates to camera service about device state" |
| 2388 | " changes from pid=%d, uid=%d", pid, uid); |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | ATRACE_CALL(); |
| 2393 | |
Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 2394 | mCameraProviderManager->notifyDeviceStateChange(newState); |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2395 | |
| 2396 | return Status::ok(); |
| 2397 | } |
| 2398 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2399 | Status CameraService::notifyDisplayConfigurationChange() { |
| 2400 | ATRACE_CALL(); |
| 2401 | const int callingPid = CameraThreadState::getCallingPid(); |
| 2402 | const int selfPid = getpid(); |
| 2403 | |
| 2404 | // Permission checks |
| 2405 | if (callingPid != selfPid) { |
| 2406 | // Ensure we're being called by system_server, or similar process with |
| 2407 | // permissions to notify the camera service about system events |
| 2408 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
| 2409 | const int uid = CameraThreadState::getCallingUid(); |
| 2410 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" |
| 2411 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2412 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2413 | "No permission to send updates to camera service about orientation" |
| 2414 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2415 | } |
| 2416 | } |
| 2417 | |
| 2418 | Mutex::Autolock lock(mServiceLock); |
| 2419 | |
| 2420 | // Don't do anything if rotate-and-crop override via cmd is active |
| 2421 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); |
| 2422 | |
| 2423 | const auto clients = mActiveClientManager.getAll(); |
| 2424 | for (auto& current : clients) { |
| 2425 | if (current != nullptr) { |
| 2426 | const auto basicClient = current->getValue(); |
Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame^] | 2427 | if (basicClient.get() != nullptr) { |
Emilian Peev | 065b2c1 | 2021-11-23 13:12:57 -0800 | [diff] [blame] | 2428 | basicClient->setRotateAndCropOverride( |
| 2429 | CameraServiceProxyWrapper::getRotateAndCropOverride( |
| 2430 | basicClient->getPackageName(), |
| 2431 | basicClient->getCameraFacing(), |
| 2432 | multiuser_get_user_id(basicClient->getClientUid()))); |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2433 | } |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | return Status::ok(); |
| 2438 | } |
| 2439 | |
| 2440 | Status CameraService::getConcurrentCameraIds( |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2441 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { |
| 2442 | ATRACE_CALL(); |
| 2443 | if (!concurrentCameraIds) { |
| 2444 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); |
| 2445 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); |
| 2446 | } |
| 2447 | |
| 2448 | if (!mInitialized) { |
| 2449 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2450 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2451 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2452 | "Camera subsystem is not available"); |
| 2453 | } |
| 2454 | // First call into the provider and get the set of concurrent camera |
| 2455 | // combinations |
| 2456 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = |
Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 2457 | mCameraProviderManager->getConcurrentCameraIds(); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2458 | for (auto &combination : concurrentCameraCombinations) { |
| 2459 | std::vector<std::string> validCombination; |
| 2460 | for (auto &cameraId : combination) { |
| 2461 | // if the camera state is not present, skip |
| 2462 | String8 cameraIdStr(cameraId.c_str()); |
| 2463 | auto state = getCameraState(cameraIdStr); |
| 2464 | if (state == nullptr) { |
| 2465 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); |
| 2466 | continue; |
| 2467 | } |
| 2468 | StatusInternal status = state->getStatus(); |
| 2469 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { |
| 2470 | continue; |
| 2471 | } |
| 2472 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { |
| 2473 | continue; |
| 2474 | } |
| 2475 | validCombination.push_back(cameraId); |
| 2476 | } |
| 2477 | if (validCombination.size() != 0) { |
| 2478 | concurrentCameraIds->push_back(std::move(validCombination)); |
| 2479 | } |
| 2480 | } |
| 2481 | return Status::ok(); |
| 2482 | } |
| 2483 | |
| 2484 | Status CameraService::isConcurrentSessionConfigurationSupported( |
| 2485 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2486 | int targetSdkVersion, /*out*/bool* isSupported) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2487 | if (!isSupported) { |
| 2488 | ALOGE("%s: isSupported is NULL", __FUNCTION__); |
| 2489 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); |
| 2490 | } |
| 2491 | |
| 2492 | if (!mInitialized) { |
| 2493 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 2494 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2495 | "Camera subsystem is not available"); |
| 2496 | } |
| 2497 | |
| 2498 | // Check for camera permissions |
| 2499 | int callingPid = CameraThreadState::getCallingPid(); |
| 2500 | int callingUid = CameraThreadState::getCallingUid(); |
| 2501 | if ((callingPid != getpid()) && !checkPermission(sCameraPermission, callingPid, callingUid)) { |
| 2502 | ALOGE("%s: pid %d doesn't have camera permissions", __FUNCTION__, callingPid); |
| 2503 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 2504 | "android.permission.CAMERA needed to call" |
| 2505 | "isConcurrentSessionConfigurationSupported"); |
| 2506 | } |
| 2507 | |
| 2508 | status_t res = |
| 2509 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2510 | cameraIdsAndSessionConfigurations, mPerfClassPrimaryCameraIds, |
| 2511 | targetSdkVersion, isSupported); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2512 | if (res != OK) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2513 | logServiceError(String8::format("Unable to query session configuration support"), |
| 2514 | ERROR_INVALID_OPERATION); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2515 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " |
| 2516 | "support %s (%d)", strerror(-res), res); |
| 2517 | } |
| 2518 | return Status::ok(); |
| 2519 | } |
| 2520 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2521 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, |
| 2522 | /*out*/ |
| 2523 | std::vector<hardware::CameraStatus> *cameraStatuses) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2524 | return addListenerHelper(listener, cameraStatuses); |
| 2525 | } |
| 2526 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2527 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, |
| 2528 | std::vector<hardware::CameraStatus>* cameraStatuses) { |
| 2529 | return addListenerHelper(listener, cameraStatuses, false, true); |
| 2530 | } |
| 2531 | |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2532 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, |
| 2533 | /*out*/ |
| 2534 | std::vector<hardware::CameraStatus> *cameraStatuses, |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2535 | bool isVendorListener, bool isProcessLocalTest) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2536 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2537 | ATRACE_CALL(); |
| 2538 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2539 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2540 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 2541 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2542 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2543 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2544 | } |
| 2545 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2546 | auto clientUid = CameraThreadState::getCallingUid(); |
| 2547 | auto clientPid = CameraThreadState::getCallingPid(); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2548 | bool openCloseCallbackAllowed = checkPermission(sCameraOpenCloseListenerPermission, |
Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 2549 | clientPid, clientUid, /*logPermissionFailure*/false); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2550 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2551 | Mutex::Autolock lock(mServiceLock); |
| 2552 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2553 | { |
| 2554 | Mutex::Autolock lock(mStatusListenerLock); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2555 | for (const auto &it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2556 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2557 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 2558 | __FUNCTION__, listener.get()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2559 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2560 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2561 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2562 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2563 | sp<ServiceListener> serviceListener = |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2564 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, |
| 2565 | openCloseCallbackAllowed); |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2566 | auto ret = serviceListener->initialize(isProcessLocalTest); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2567 | if (ret != NO_ERROR) { |
| 2568 | String8 msg = String8::format("Failed to initialize service listener: %s (%d)", |
| 2569 | strerror(-ret), ret); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2570 | logServiceError(msg,ERROR_ILLEGAL_ARGUMENT); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2571 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2572 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 2573 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2574 | // The listener still needs to be added to the list of listeners, regardless of what |
| 2575 | // permissions the listener process has / whether it is a vendor listener. Since it might be |
| 2576 | // eligible to listen to other camera ids. |
| 2577 | mListenerList.emplace_back(serviceListener); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2578 | mUidPolicy->registerMonitorUid(clientUid); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2579 | } |
| 2580 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2581 | /* Collect current devices and status */ |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2582 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2583 | Mutex::Autolock lock(mCameraStatesLock); |
| 2584 | for (auto& i : mCameraStates) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 2585 | cameraStatuses->emplace_back(i.first, |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 2586 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds(), |
| 2587 | openCloseCallbackAllowed ? i.second->getClientPackage() : String8::empty()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2588 | } |
| 2589 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2590 | // Remove the camera statuses that should be hidden from the client, we do |
| 2591 | // this after collecting the states in order to avoid holding |
| 2592 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at |
| 2593 | // the same time. |
| 2594 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), |
| 2595 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { |
| 2596 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 2597 | if (getSystemCameraKind(s.cameraId, &deviceKind) != OK) { |
| 2598 | ALOGE("%s: Invalid camera id %s, skipping status update", |
| 2599 | __FUNCTION__, s.cameraId.c_str()); |
| 2600 | return true; |
| 2601 | } |
| 2602 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, |
| 2603 | clientUid);}), cameraStatuses->end()); |
| 2604 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2605 | //cameraStatuses will have non-eligible camera ids removed. |
| 2606 | std::set<String16> idsChosenForCallback; |
| 2607 | for (const auto &s : *cameraStatuses) { |
| 2608 | idsChosenForCallback.insert(String16(s.cameraId)); |
| 2609 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2610 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2611 | /* |
| 2612 | * Immediately signal current torch status to this listener only |
| 2613 | * This may be a subset of all the devices, so don't include it in the response directly |
| 2614 | */ |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2615 | { |
| 2616 | Mutex::Autolock al(mTorchStatusMutex); |
| 2617 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2618 | String16 id = String16(mTorchStatusMap.keyAt(i).string()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2619 | // The camera id is visible to the client. Fine to send torch |
| 2620 | // callback. |
| 2621 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { |
| 2622 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id); |
| 2623 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2624 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2625 | } |
| 2626 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2627 | return Status::ok(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2628 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2629 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2630 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2631 | ATRACE_CALL(); |
| 2632 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2633 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 2634 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2635 | if (listener == 0) { |
| 2636 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2637 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2638 | } |
| 2639 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2640 | Mutex::Autolock lock(mServiceLock); |
| 2641 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2642 | { |
| 2643 | Mutex::Autolock lock(mStatusListenerLock); |
| 2644 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2645 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { |
| 2646 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid()); |
| 2647 | IInterface::asBinder(listener)->unlinkToDeath(*it); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2648 | mListenerList.erase(it); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2649 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2650 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2651 | } |
| 2652 | } |
| 2653 | |
| 2654 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 2655 | __FUNCTION__, listener.get()); |
| 2656 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2657 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2658 | } |
| 2659 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2660 | Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2661 | |
| 2662 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2663 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 2664 | |
| 2665 | if (parameters == NULL) { |
| 2666 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2667 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2668 | } |
| 2669 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2670 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2671 | |
| 2672 | CameraParameters shimParams; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2673 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2674 | // Error logged by caller |
| 2675 | return ret; |
| 2676 | } |
| 2677 | |
| 2678 | String8 shimParamsString8 = shimParams.flatten(); |
| 2679 | String16 shimParamsString16 = String16(shimParamsString8); |
| 2680 | |
| 2681 | *parameters = shimParamsString16; |
| 2682 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2683 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2684 | } |
| 2685 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2686 | Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion, |
| 2687 | /*out*/ bool *isSupported) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2688 | ATRACE_CALL(); |
| 2689 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2690 | const String8 id = String8(cameraId); |
| 2691 | |
| 2692 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2693 | |
| 2694 | switch (apiVersion) { |
| 2695 | case API_VERSION_1: |
| 2696 | case API_VERSION_2: |
| 2697 | break; |
| 2698 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2699 | String8 msg = String8::format("Unknown API version %d", apiVersion); |
| 2700 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2701 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2702 | } |
| 2703 | |
Emilian Peev | 28ad2ea | 2017-02-07 16:14:32 +0000 | [diff] [blame] | 2704 | int deviceVersion = getDeviceVersion(id); |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2705 | switch (deviceVersion) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2706 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 2707 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 2708 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 2709 | if (apiVersion == API_VERSION_2) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2710 | ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim", |
| 2711 | __FUNCTION__, id.string(), deviceVersion); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2712 | *isSupported = false; |
| 2713 | } else { // if (apiVersion == API_VERSION_1) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2714 | ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported", |
| 2715 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2716 | *isSupported = true; |
| 2717 | } |
| 2718 | break; |
| 2719 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 2720 | case CAMERA_DEVICE_API_VERSION_3_3: |
Zhijun He | 4afbdec | 2016-05-04 15:42:51 -0700 | [diff] [blame] | 2721 | case CAMERA_DEVICE_API_VERSION_3_4: |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2722 | case CAMERA_DEVICE_API_VERSION_3_5: |
Yin-Chia Yeh | 56d98ba | 2020-01-16 16:57:14 -0800 | [diff] [blame] | 2723 | case CAMERA_DEVICE_API_VERSION_3_6: |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 2724 | case CAMERA_DEVICE_API_VERSION_3_7: |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 2725 | case CAMERA_DEVICE_API_VERSION_3_8: |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2726 | ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly", |
| 2727 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2728 | *isSupported = true; |
| 2729 | break; |
| 2730 | case -1: { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2731 | String8 msg = String8::format("Unknown camera ID %s", id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2732 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2733 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2734 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2735 | default: { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2736 | String8 msg = String8::format("Unknown device version %x for device %s", |
| 2737 | deviceVersion, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2738 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2739 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string()); |
| 2740 | } |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2741 | } |
| 2742 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2743 | return Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2744 | } |
| 2745 | |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2746 | Status CameraService::isHiddenPhysicalCamera(const String16& cameraId, |
| 2747 | /*out*/ bool *isSupported) { |
| 2748 | ATRACE_CALL(); |
| 2749 | |
| 2750 | const String8 id = String8(cameraId); |
| 2751 | |
| 2752 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
| 2753 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(id.string()); |
| 2754 | |
| 2755 | return Status::ok(); |
| 2756 | } |
| 2757 | |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2758 | Status CameraService::injectCamera( |
| 2759 | const String16& packageName, const String16& internalCamId, |
| 2760 | const String16& externalCamId, |
| 2761 | const sp<ICameraInjectionCallback>& callback, |
| 2762 | /*out*/ |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2763 | sp<ICameraInjectionSession>* cameraInjectionSession) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2764 | ATRACE_CALL(); |
| 2765 | |
| 2766 | if (!checkCallingPermission(sCameraInjectExternalCameraPermission)) { |
| 2767 | const int pid = CameraThreadState::getCallingPid(); |
| 2768 | const int uid = CameraThreadState::getCallingUid(); |
| 2769 | ALOGE("Permission Denial: can't inject camera pid=%d, uid=%d", pid, uid); |
| 2770 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 2771 | "Permission Denial: no permission to inject camera"); |
| 2772 | } |
| 2773 | |
| 2774 | ALOGV( |
| 2775 | "%s: Package name = %s, Internal camera ID = %s, External camera ID = " |
| 2776 | "%s", |
| 2777 | __FUNCTION__, String8(packageName).string(), |
| 2778 | String8(internalCamId).string(), String8(externalCamId).string()); |
| 2779 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2780 | { |
| 2781 | Mutex::Autolock lock(mInjectionParametersLock); |
| 2782 | mInjectionInternalCamId = String8(internalCamId); |
| 2783 | mInjectionExternalCamId = String8(externalCamId); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2784 | mInjectionStatusListener->addListener(callback); |
| 2785 | *cameraInjectionSession = new CameraInjectionSession(this); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2786 | status_t res = NO_ERROR; |
| 2787 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); |
| 2788 | // If the client already exists, we can directly connect to the camera device through the |
| 2789 | // client's injectCamera(), otherwise we need to wait until the client is established |
| 2790 | // (execute connectHelper()) before injecting the camera to the camera device. |
| 2791 | if (clientDescriptor != nullptr) { |
| 2792 | mInjectionInitPending = false; |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2793 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 2794 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); |
| 2795 | if(res != OK) { |
| 2796 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 2797 | "No camera device with ID \"%s\" currently available", |
| 2798 | mInjectionExternalCamId.string()); |
| 2799 | } |
| 2800 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2801 | if(res != OK) { |
| 2802 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); |
| 2803 | } |
| 2804 | } else { |
| 2805 | mInjectionInitPending = true; |
| 2806 | } |
| 2807 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2808 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2809 | return binder::Status::ok(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2810 | } |
| 2811 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2812 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2813 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2814 | for (auto& i : mActiveClientManager.getAll()) { |
| 2815 | auto clientSp = i->getValue(); |
| 2816 | if (clientSp.get() == client) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2817 | cacheClientTagDumpIfNeeded(client->mCameraIdStr, clientSp.get()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2818 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2819 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2820 | } |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2821 | updateAudioRestrictionLocked(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2822 | } |
| 2823 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2824 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2825 | bool ret = false; |
| 2826 | { |
| 2827 | // Acquire mServiceLock and prevent other clients from connecting |
| 2828 | std::unique_ptr<AutoConditionLock> lock = |
| 2829 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2830 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2831 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2832 | std::vector<sp<BasicClient>> evicted; |
| 2833 | for (auto& i : mActiveClientManager.getAll()) { |
| 2834 | auto clientSp = i->getValue(); |
| 2835 | if (clientSp.get() == nullptr) { |
| 2836 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 2837 | mActiveClientManager.remove(i); |
| 2838 | continue; |
| 2839 | } |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 2840 | if (remote == clientSp->getRemote()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2841 | mActiveClientManager.remove(i); |
| 2842 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2843 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2844 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2845 | clientSp->notifyError( |
| 2846 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2847 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2851 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 2852 | // other clients from connecting in mServiceLockWrapper if held |
| 2853 | mServiceLock.unlock(); |
| 2854 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2855 | // Do not clear caller identity, remote caller should be client proccess |
| 2856 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2857 | for (auto& i : evicted) { |
| 2858 | if (i.get() != nullptr) { |
| 2859 | i->disconnect(); |
| 2860 | ret = true; |
| 2861 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2862 | } |
| 2863 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2864 | // Reacquire mServiceLock |
| 2865 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2866 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2867 | } // lock is destroyed, allow further connect calls |
| 2868 | |
| 2869 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2870 | } |
| 2871 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2872 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
| 2873 | const String8& cameraId) const { |
| 2874 | std::shared_ptr<CameraState> state; |
| 2875 | { |
| 2876 | Mutex::Autolock lock(mCameraStatesLock); |
| 2877 | auto iter = mCameraStates.find(cameraId); |
| 2878 | if (iter != mCameraStates.end()) { |
| 2879 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2880 | } |
| 2881 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2882 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2883 | } |
| 2884 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2885 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) { |
| 2886 | // Remove from active clients list |
| 2887 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 2888 | if (clientDescriptorPtr == nullptr) { |
| 2889 | ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__, |
| 2890 | cameraId.string()); |
| 2891 | return sp<BasicClient>{nullptr}; |
| 2892 | } |
| 2893 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2894 | sp<BasicClient> client = clientDescriptorPtr->getValue(); |
| 2895 | if (client.get() != nullptr) { |
| 2896 | cacheClientTagDumpIfNeeded(clientDescriptorPtr->getKey(), client.get()); |
| 2897 | } |
| 2898 | return client; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 2899 | } |
| 2900 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2901 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2902 | // Acquire mServiceLock and prevent other clients from connecting |
| 2903 | std::unique_ptr<AutoConditionLock> lock = |
| 2904 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 2905 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2906 | std::set<userid_t> newAllowedUsers; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2907 | for (size_t i = 0; i < newUserIds.size(); i++) { |
| 2908 | if (newUserIds[i] < 0) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2909 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2910 | __FUNCTION__, newUserIds[i]); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2911 | return; |
| 2912 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2913 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2914 | } |
| 2915 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2916 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2917 | if (newAllowedUsers == mAllowedUsers) { |
| 2918 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 2919 | return; |
| 2920 | } |
| 2921 | |
| 2922 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 2923 | |
| 2924 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2925 | |
| 2926 | // Current user has switched, evict all current clients. |
| 2927 | std::vector<sp<BasicClient>> evicted; |
| 2928 | for (auto& i : mActiveClientManager.getAll()) { |
| 2929 | auto clientSp = i->getValue(); |
| 2930 | |
| 2931 | if (clientSp.get() == nullptr) { |
| 2932 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 2933 | continue; |
| 2934 | } |
| 2935 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2936 | // Don't evict clients that are still allowed. |
| 2937 | uid_t clientUid = clientSp->getClientUid(); |
| 2938 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 2939 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 2940 | continue; |
| 2941 | } |
| 2942 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2943 | evicted.push_back(clientSp); |
| 2944 | |
| 2945 | String8 curTime = getFormattedCurrentTime(); |
| 2946 | |
| 2947 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
| 2948 | i->getKey().string()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2949 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2950 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2951 | logEvent(String8::format("EVICT device %s client held by package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2952 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due" |
| 2953 | " to user switch.", i->getKey().string(), |
| 2954 | String8{clientSp->getPackageName()}.string(), |
| 2955 | i->getOwnerId(), i->getPriority().getScore(), |
| 2956 | i->getPriority().getState())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2957 | |
| 2958 | } |
| 2959 | |
| 2960 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 2961 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 2962 | mServiceLock.unlock(); |
| 2963 | |
| 2964 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2965 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2966 | |
| 2967 | for (auto& i : evicted) { |
| 2968 | i->disconnect(); |
| 2969 | } |
| 2970 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2971 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2972 | |
| 2973 | // Reacquire mServiceLock |
| 2974 | mServiceLock.lock(); |
| 2975 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2976 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2977 | void CameraService::logEvent(const char* event) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2978 | String8 curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2979 | Mutex::Autolock l(mLogLock); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2980 | String8 msg = String8::format("%s : %s", curTime.string(), event); |
| 2981 | // For service error events, print the msg only once. |
| 2982 | if(!msg.contains("SERVICE ERROR")) { |
| 2983 | mEventLog.add(msg); |
| 2984 | } else if(sServiceErrorEventSet.find(msg) == sServiceErrorEventSet.end()) { |
| 2985 | // Error event not added to the dumpsys log before |
| 2986 | mEventLog.add(msg); |
| 2987 | sServiceErrorEventSet.insert(msg); |
| 2988 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2989 | } |
| 2990 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2991 | void CameraService::logDisconnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2992 | const char* clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2993 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2994 | 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] | 2995 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2996 | } |
| 2997 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2998 | void CameraService::logDisconnectedOffline(const char* cameraId, int clientPid, |
| 2999 | const char* clientPackage) { |
| 3000 | // Log the clients evicted |
| 3001 | logEvent(String8::format("DISCONNECT offline device %s client for package %s (PID %d)", |
| 3002 | cameraId, clientPackage, clientPid)); |
| 3003 | } |
| 3004 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3005 | void CameraService::logConnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3006 | const char* clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3007 | // Log the clients evicted |
| 3008 | 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] | 3009 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3010 | } |
| 3011 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3012 | void CameraService::logConnectedOffline(const char* cameraId, int clientPid, |
| 3013 | const char* clientPackage) { |
| 3014 | // Log the clients evicted |
| 3015 | logEvent(String8::format("CONNECT offline device %s client for package %s (PID %d)", cameraId, |
| 3016 | clientPackage, clientPid)); |
| 3017 | } |
| 3018 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3019 | void CameraService::logRejected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3020 | const char* clientPackage, const char* reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3021 | // Log the client rejected |
| 3022 | 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] | 3023 | cameraId, clientPackage, clientPid, reason)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3026 | void CameraService::logTorchEvent(const char* cameraId, const char *torchState, int clientPid) { |
| 3027 | // Log torch event |
| 3028 | logEvent(String8::format("Torch for camera id %s turned %s for client PID %d", cameraId, |
| 3029 | torchState, clientPid)); |
| 3030 | } |
| 3031 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3032 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 3033 | const std::set<userid_t>& newUserIds) { |
| 3034 | String8 newUsers = toString(newUserIds); |
| 3035 | String8 oldUsers = toString(oldUserIds); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3036 | if (oldUsers.size() == 0) { |
| 3037 | oldUsers = "<None>"; |
| 3038 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3039 | // Log the new and old users |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3040 | 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] | 3041 | oldUsers.string(), newUsers.string())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) { |
| 3045 | // Log the device removal |
| 3046 | logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason)); |
| 3047 | } |
| 3048 | |
| 3049 | void CameraService::logDeviceAdded(const char* cameraId, const char* reason) { |
| 3050 | // Log the device removal |
| 3051 | logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason)); |
| 3052 | } |
| 3053 | |
| 3054 | void CameraService::logClientDied(int clientPid, const char* reason) { |
| 3055 | // Log the device removal |
| 3056 | 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] | 3057 | } |
| 3058 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3059 | void CameraService::logServiceError(const char* msg, int errorCode) { |
| 3060 | String8 curTime = getFormattedCurrentTime(); |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 3061 | 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] | 3062 | } |
| 3063 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3064 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 3065 | uint32_t flags) { |
| 3066 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3067 | // Permission checks |
| 3068 | switch (code) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3069 | case SHELL_COMMAND_TRANSACTION: { |
| 3070 | int in = data.readFileDescriptor(); |
| 3071 | int out = data.readFileDescriptor(); |
| 3072 | int err = data.readFileDescriptor(); |
| 3073 | int argc = data.readInt32(); |
| 3074 | Vector<String16> args; |
| 3075 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 3076 | args.add(data.readString16()); |
| 3077 | } |
| 3078 | sp<IBinder> unusedCallback; |
| 3079 | sp<IResultReceiver> resultReceiver; |
| 3080 | status_t status; |
| 3081 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 3082 | return status; |
| 3083 | } |
| 3084 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 3085 | return status; |
| 3086 | } |
| 3087 | status = shellCommand(in, out, err, args); |
| 3088 | if (resultReceiver != nullptr) { |
| 3089 | resultReceiver->send(status); |
| 3090 | } |
| 3091 | return NO_ERROR; |
| 3092 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3093 | } |
| 3094 | |
| 3095 | return BnCameraService::onTransact(code, data, reply, flags); |
| 3096 | } |
| 3097 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3098 | // We share the media players for shutter and recording sound for all clients. |
| 3099 | // A reference count is kept to determine when we will actually release the |
| 3100 | // media players. |
| 3101 | |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3102 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { |
| 3103 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 3104 | status_t error; |
| 3105 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 3106 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3107 | error = mp->prepare(); |
| 3108 | } |
| 3109 | if (error != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3110 | ALOGE("Failed to load CameraService sounds: %s", file); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3111 | mp->disconnect(); |
| 3112 | mp.clear(); |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3113 | return nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3114 | } |
| 3115 | return mp; |
| 3116 | } |
| 3117 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3118 | void CameraService::increaseSoundRef() { |
| 3119 | Mutex::Autolock lock(mSoundLock); |
| 3120 | mSoundRef++; |
| 3121 | } |
| 3122 | |
| 3123 | void CameraService::loadSoundLocked(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3124 | ATRACE_CALL(); |
| 3125 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3126 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); |
| 3127 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { |
| 3128 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); |
| 3129 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { |
| 3130 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 3131 | } |
| 3132 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) { |
| 3133 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); |
| 3134 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { |
| 3135 | mSoundPlayer[SOUND_RECORDING_START] = |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3136 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3137 | } |
| 3138 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { |
| 3139 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); |
| 3140 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { |
| 3141 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
| 3142 | } |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3143 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3144 | } |
| 3145 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3146 | void CameraService::decreaseSoundRef() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3147 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3148 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3149 | if (--mSoundRef) return; |
| 3150 | |
| 3151 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 3152 | if (mSoundPlayer[i] != 0) { |
| 3153 | mSoundPlayer[i]->disconnect(); |
| 3154 | mSoundPlayer[i].clear(); |
| 3155 | } |
| 3156 | } |
| 3157 | } |
| 3158 | |
| 3159 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3160 | ATRACE_CALL(); |
| 3161 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3162 | LOG1("playSound(%d)", kind); |
Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame] | 3163 | if (kind < 0 || kind >= NUM_SOUNDS) { |
| 3164 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); |
| 3165 | return; |
| 3166 | } |
| 3167 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3168 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3169 | loadSoundLocked(kind); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3170 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 3171 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 3172 | player->seekTo(0); |
| 3173 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | // ---------------------------------------------------------------------------- |
| 3178 | |
| 3179 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 3180 | const sp<ICameraClient>& cameraClient, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3181 | const String16& clientPackageName, bool systemNativeClient, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 3182 | const std::optional<String16>& clientFeatureId, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3183 | const String8& cameraIdStr, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3184 | int api1CameraId, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3185 | int clientPid, uid_t clientUid, |
| 3186 | int servicePid) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3187 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 3188 | IInterface::asBinder(cameraClient), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3189 | clientPackageName, systemNativeClient, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3190 | cameraIdStr, cameraFacing, sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3191 | clientPid, clientUid, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3192 | servicePid), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3193 | mCameraId(api1CameraId) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3194 | { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3195 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3196 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3197 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 3198 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3199 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3200 | cameraService->increaseSoundRef(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3201 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3202 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3203 | } |
| 3204 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3205 | // tear down the client |
| 3206 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3207 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3208 | mDestructionStarted = true; |
| 3209 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3210 | sCameraService->decreaseSoundRef(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3211 | // unconditionally disconnect. function is idempotent |
| 3212 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3213 | } |
| 3214 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3215 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; |
| 3216 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3217 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3218 | const sp<IBinder>& remoteCallback, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3219 | const String16& clientPackageName, bool nativeClient, |
| 3220 | const std::optional<String16>& clientFeatureId, const String8& cameraIdStr, |
| 3221 | int cameraFacing, int sensorOrientation, int clientPid, uid_t clientUid, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3222 | int servicePid): |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3223 | mDestructionStarted(false), |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3224 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), mOrientation(sensorOrientation), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3225 | mClientPackageName(clientPackageName), mSystemNativeClient(nativeClient), |
| 3226 | mClientFeatureId(clientFeatureId), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 3227 | mClientPid(clientPid), mClientUid(clientUid), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3228 | mServicePid(servicePid), |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3229 | mDisconnected(false), mUidIsTrusted(false), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3230 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3231 | mRemoteBinder(remoteCallback), |
| 3232 | mOpsActive(false), |
| 3233 | mOpsStreaming(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3234 | { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3235 | if (sCameraService == nullptr) { |
| 3236 | sCameraService = cameraService; |
| 3237 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 3238 | |
| 3239 | // In some cases the calling code has no access to the package it runs under. |
| 3240 | // For example, NDK camera API. |
| 3241 | // In this case we will get the packages for the calling UID and pick the first one |
| 3242 | // for attributing the app op. This will work correctly for runtime permissions |
| 3243 | // as for legacy apps we will toggle the app op for all packages in the UID. |
| 3244 | // The caveat is that the operation may be attributed to the wrong package and |
| 3245 | // stats based on app ops may be slightly off. |
| 3246 | if (mClientPackageName.size() <= 0) { |
| 3247 | sp<IServiceManager> sm = defaultServiceManager(); |
| 3248 | sp<IBinder> binder = sm->getService(String16(kPermissionServiceName)); |
| 3249 | if (binder == 0) { |
| 3250 | ALOGE("Cannot get permission service"); |
| 3251 | // Leave mClientPackageName unchanged (empty) and the further interaction |
| 3252 | // with camera will fail in BasicClient::startCameraOps |
| 3253 | return; |
| 3254 | } |
| 3255 | |
| 3256 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 3257 | Vector<String16> packages; |
| 3258 | |
| 3259 | permCtrl->getPackagesForUid(mClientUid, packages); |
| 3260 | |
| 3261 | if (packages.isEmpty()) { |
| 3262 | ALOGE("No packages for calling UID"); |
| 3263 | // Leave mClientPackageName unchanged (empty) and the further interaction |
| 3264 | // with camera will fail in BasicClient::startCameraOps |
| 3265 | return; |
| 3266 | } |
| 3267 | mClientPackageName = packages[0]; |
| 3268 | } |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3269 | |
| 3270 | // There are 2 scenarios in which a client won't have AppOps operations |
| 3271 | // (both scenarios : native clients) |
| 3272 | // 1) It's an system native client*, the package name will be empty |
| 3273 | // and it will return from this function in the previous if condition |
| 3274 | // (This is the same as the previously existing behavior). |
| 3275 | // 2) It is a system native client, but its package name has been |
| 3276 | // modified for debugging, however it still must not use AppOps since |
| 3277 | // the package name is not a real one. |
| 3278 | // |
| 3279 | // * system native client - native client with UID < AID_APP_START. It |
| 3280 | // doesn't exclude clients not on the system partition. |
| 3281 | if (!mSystemNativeClient) { |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3282 | mAppOpsManager = std::make_unique<AppOpsManager>(); |
| 3283 | } |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3284 | |
| 3285 | mUidIsTrusted = isTrustedCallingUid(mClientUid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3289 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3290 | mDestructionStarted = true; |
| 3291 | } |
| 3292 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3293 | binder::Status CameraService::BasicClient::disconnect() { |
| 3294 | binder::Status res = Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3295 | if (mDisconnected) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3296 | return res; |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3297 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 3298 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3299 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3300 | sCameraService->removeByClient(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3301 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, String8(mClientPackageName)); |
Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 3302 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, |
| 3303 | mCameraIdStr.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3304 | |
| 3305 | sp<IBinder> remote = getRemote(); |
| 3306 | if (remote != nullptr) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3307 | remote->unlinkToDeath(sCameraService); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3308 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3309 | |
| 3310 | finishCameraOps(); |
Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 3311 | // Notify flashlight that a camera device is closed. |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3312 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); |
| 3313 | ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(), |
| 3314 | mClientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3315 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3316 | // client shouldn't be able to call into us anymore |
| 3317 | mClientPid = 0; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3318 | |
| 3319 | return res; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3320 | } |
| 3321 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3322 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 3323 | // No dumping of clients directly over Binder, |
| 3324 | // must go through CameraService::dump |
| 3325 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3326 | CameraThreadState::getCallingUid(), NULL, 0); |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3327 | return OK; |
| 3328 | } |
| 3329 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 3330 | status_t CameraService::BasicClient::startWatchingTags(const String8&, int) { |
| 3331 | // Can't watch tags directly, must go through CameraService::startWatchingTags |
| 3332 | return OK; |
| 3333 | } |
| 3334 | |
| 3335 | status_t CameraService::BasicClient::stopWatchingTags(int) { |
| 3336 | // Can't watch tags directly, must go through CameraService::stopWatchingTags |
| 3337 | return OK; |
| 3338 | } |
| 3339 | |
| 3340 | status_t CameraService::BasicClient::dumpWatchedEventsToVector(std::vector<std::string> &) { |
| 3341 | // Can't watch tags directly, must go through CameraService::dumpWatchedEventsToVector |
| 3342 | return OK; |
| 3343 | } |
| 3344 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3345 | String16 CameraService::BasicClient::getPackageName() const { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3346 | return mClientPackageName; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3347 | } |
| 3348 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3349 | int CameraService::BasicClient::getCameraFacing() const { |
| 3350 | return mCameraFacing; |
| 3351 | } |
| 3352 | |
| 3353 | int CameraService::BasicClient::getCameraOrientation() const { |
| 3354 | return mOrientation; |
| 3355 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3356 | |
| 3357 | int CameraService::BasicClient::getClientPid() const { |
| 3358 | return mClientPid; |
| 3359 | } |
| 3360 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3361 | uid_t CameraService::BasicClient::getClientUid() const { |
| 3362 | return mClientUid; |
| 3363 | } |
| 3364 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3365 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 3366 | // Defaults to API2. |
| 3367 | return level == API_2; |
| 3368 | } |
| 3369 | |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3370 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3371 | { |
| 3372 | Mutex::Autolock l(mAudioRestrictionLock); |
| 3373 | mAudioRestriction = mode; |
| 3374 | } |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3375 | sCameraService->updateAudioRestriction(); |
| 3376 | return OK; |
| 3377 | } |
| 3378 | |
| 3379 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3380 | return sCameraService->updateAudioRestriction(); |
| 3381 | } |
| 3382 | |
| 3383 | int32_t CameraService::BasicClient::getAudioRestriction() const { |
| 3384 | Mutex::Autolock l(mAudioRestrictionLock); |
| 3385 | return mAudioRestriction; |
| 3386 | } |
| 3387 | |
| 3388 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { |
| 3389 | switch (mode) { |
| 3390 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: |
| 3391 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: |
| 3392 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: |
| 3393 | return true; |
| 3394 | default: |
| 3395 | return false; |
| 3396 | } |
| 3397 | } |
| 3398 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3399 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { |
| 3400 | if (mode == AppOpsManager::MODE_ERRORED) { |
| 3401 | ALOGI("Camera %s: Access for \"%s\" has been revoked", |
| 3402 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 3403 | return PERMISSION_DENIED; |
| 3404 | } else if (!mUidIsTrusted && mode == AppOpsManager::MODE_IGNORED) { |
| 3405 | // If the calling Uid is trusted (a native service), the AppOpsManager could |
| 3406 | // return MODE_IGNORED. Do not treat such case as error. |
| 3407 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, |
| 3408 | mClientPackageName); |
| 3409 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3410 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3411 | if (!isUidActive || !isCameraPrivacyEnabled) { |
| 3412 | ALOGI("Camera %s: Access for \"%s\" has been restricted", |
| 3413 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 3414 | // Return the same error as for device policy manager rejection |
| 3415 | return -EACCES; |
| 3416 | } |
| 3417 | } |
| 3418 | return OK; |
| 3419 | } |
| 3420 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3421 | status_t CameraService::BasicClient::startCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3422 | ATRACE_CALL(); |
| 3423 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3424 | { |
| 3425 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3426 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3427 | } |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3428 | if (mAppOpsManager != nullptr) { |
| 3429 | // Notify app ops that the camera is not available |
| 3430 | mOpsCallback = new OpsCallback(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3431 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 3432 | mClientPackageName, mOpsCallback); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3433 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3434 | // Just check for camera acccess here on open - delay startOp until |
| 3435 | // camera frames start streaming in startCameraStreamingOps |
| 3436 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3437 | mClientPackageName); |
| 3438 | status_t res = handleAppOpMode(mode); |
| 3439 | if (res != OK) { |
| 3440 | return res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3441 | } |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 3442 | } |
| 3443 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3444 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3445 | |
| 3446 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3447 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3448 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3449 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid); |
| 3450 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3451 | // Notify listeners of camera open/close status |
| 3452 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); |
| 3453 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3454 | return OK; |
| 3455 | } |
| 3456 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3457 | status_t CameraService::BasicClient::startCameraStreamingOps() { |
| 3458 | ATRACE_CALL(); |
| 3459 | |
| 3460 | if (!mOpsActive) { |
| 3461 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3462 | return INVALID_OPERATION; |
| 3463 | } |
| 3464 | if (mOpsStreaming) { |
| 3465 | ALOGV("%s: Streaming already active!", __FUNCTION__); |
| 3466 | return OK; |
| 3467 | } |
| 3468 | |
| 3469 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", |
| 3470 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3471 | |
| 3472 | if (mAppOpsManager != nullptr) { |
| 3473 | int32_t mode = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, |
| 3474 | mClientPackageName, /*startIfModeDefault*/ false, mClientFeatureId, |
| 3475 | String16("start camera ") + String16(mCameraIdStr)); |
| 3476 | status_t res = handleAppOpMode(mode); |
| 3477 | if (res != OK) { |
| 3478 | return res; |
| 3479 | } |
| 3480 | } |
| 3481 | |
| 3482 | mOpsStreaming = true; |
| 3483 | |
| 3484 | return OK; |
| 3485 | } |
| 3486 | |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 3487 | status_t CameraService::BasicClient::noteAppOp() { |
| 3488 | ATRACE_CALL(); |
| 3489 | |
| 3490 | ALOGV("%s: Start camera noteAppOp, package name = %s, client UID = %d", |
| 3491 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3492 | |
| 3493 | // noteAppOp is only used for when camera mute is not supported, in order |
| 3494 | // to trigger the sensor privacy "Unblock" dialog |
| 3495 | if (mAppOpsManager != nullptr) { |
| 3496 | int32_t mode = mAppOpsManager->noteOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3497 | mClientPackageName, mClientFeatureId, |
| 3498 | String16("start camera ") + String16(mCameraIdStr)); |
| 3499 | status_t res = handleAppOpMode(mode); |
| 3500 | if (res != OK) { |
| 3501 | return res; |
| 3502 | } |
| 3503 | } |
| 3504 | |
| 3505 | return OK; |
| 3506 | } |
| 3507 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3508 | status_t CameraService::BasicClient::finishCameraStreamingOps() { |
| 3509 | ATRACE_CALL(); |
| 3510 | |
| 3511 | if (!mOpsActive) { |
| 3512 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3513 | return INVALID_OPERATION; |
| 3514 | } |
| 3515 | if (!mOpsStreaming) { |
| 3516 | ALOGV("%s: Streaming not active!", __FUNCTION__); |
| 3517 | return OK; |
| 3518 | } |
| 3519 | |
| 3520 | if (mAppOpsManager != nullptr) { |
| 3521 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3522 | mClientPackageName, mClientFeatureId); |
| 3523 | mOpsStreaming = false; |
| 3524 | } |
| 3525 | |
| 3526 | return OK; |
| 3527 | } |
| 3528 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3529 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3530 | ATRACE_CALL(); |
| 3531 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3532 | if (mOpsStreaming) { |
| 3533 | // Make sure we've notified everyone about camera stopping |
| 3534 | finishCameraStreamingOps(); |
| 3535 | } |
| 3536 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3537 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3538 | if (mOpsActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3539 | mOpsActive = false; |
| 3540 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3541 | // This function is called when a client disconnects. This should |
| 3542 | // release the camera, but actually only if it was in a proper |
| 3543 | // functional state, i.e. with status NOT_AVAILABLE |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3544 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3545 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3546 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3547 | // 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] | 3548 | sCameraService->updateStatus(StatusInternal::PRESENT, |
| 3549 | mCameraIdStr, rejected); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3550 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3551 | // Always stop watching, even if no camera op is active |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3552 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 3553 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3554 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3555 | mOpsCallback.clear(); |
| 3556 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3557 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid); |
| 3558 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3559 | // Notify listeners of camera open/close status |
| 3560 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); |
| 3561 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3562 | return OK; |
| 3563 | } |
| 3564 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3565 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3566 | ATRACE_CALL(); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3567 | if (mAppOpsManager == nullptr) { |
| 3568 | return; |
| 3569 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3570 | // TODO : add offline camera session case |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3571 | if (op != AppOpsManager::OP_CAMERA) { |
| 3572 | ALOGW("Unexpected app ops notification received: %d", op); |
| 3573 | return; |
| 3574 | } |
| 3575 | |
| 3576 | int32_t res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3577 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3578 | mClientUid, mClientPackageName); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3579 | ALOGV("checkOp returns: %d, %s ", res, |
| 3580 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 3581 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 3582 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 3583 | "UNKNOWN"); |
| 3584 | |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3585 | if (res == AppOpsManager::MODE_ERRORED) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3586 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(), |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3587 | String8(mClientPackageName).string()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3588 | block(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3589 | } else if (res == AppOpsManager::MODE_IGNORED) { |
| 3590 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3591 | bool isCameraPrivacyEnabled = |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3592 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3593 | ALOGI("Camera %s: Access for \"%s\" has been restricted, isUidTrusted %d, isUidActive %d", |
| 3594 | mCameraIdStr.string(), String8(mClientPackageName).string(), |
| 3595 | mUidIsTrusted, isUidActive); |
| 3596 | // If the calling Uid is trusted (a native service), or the client Uid is active (WAR for |
| 3597 | // b/175320666), the AppOpsManager could return MODE_IGNORED. Do not treat such cases as |
| 3598 | // error. |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 3599 | if (!mUidIsTrusted) { |
| 3600 | if (isUidActive && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 3601 | setCameraMute(true); |
| 3602 | } else if (!isUidActive |
| 3603 | || (isCameraPrivacyEnabled && !supportsCameraMute())) { |
| 3604 | block(); |
| 3605 | } |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3606 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3607 | } else if (res == AppOpsManager::MODE_ALLOWED) { |
| 3608 | setCameraMute(sCameraService->mOverrideCameraMuteMode); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3609 | } |
| 3610 | } |
| 3611 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3612 | void CameraService::BasicClient::block() { |
| 3613 | ATRACE_CALL(); |
| 3614 | |
| 3615 | // Reset the client PID to allow server-initiated disconnect, |
| 3616 | // and to prevent further calls by client. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3617 | mClientPid = CameraThreadState::getCallingPid(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3618 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 3619 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); |
| 3620 | disconnect(); |
| 3621 | } |
| 3622 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3623 | // ---------------------------------------------------------------------------- |
| 3624 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3625 | void CameraService::Client::notifyError(int32_t errorCode, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3626 | const CaptureResultExtras& resultExtras) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 3627 | (void) resultExtras; |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3628 | if (mRemoteCallback != NULL) { |
Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 3629 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; |
| 3630 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { |
| 3631 | api1ErrorCode = CAMERA_ERROR_DISABLED; |
| 3632 | } |
| 3633 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3634 | } else { |
| 3635 | ALOGE("mRemoteCallback is NULL!!"); |
| 3636 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3637 | } |
| 3638 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3639 | // NOTE: function is idempotent |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3640 | binder::Status CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3641 | ALOGV("Client::disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3642 | return BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 3643 | } |
| 3644 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3645 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 3646 | return level == API_1; |
| 3647 | } |
| 3648 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3649 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 3650 | mClient(client) { |
| 3651 | } |
| 3652 | |
| 3653 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 3654 | const String16& packageName) { |
| 3655 | sp<BasicClient> client = mClient.promote(); |
| 3656 | if (client != NULL) { |
| 3657 | client->opChanged(op, packageName); |
| 3658 | } |
| 3659 | } |
| 3660 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3661 | // ---------------------------------------------------------------------------- |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3662 | // UidPolicy |
| 3663 | // ---------------------------------------------------------------------------- |
| 3664 | |
| 3665 | void CameraService::UidPolicy::registerSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3666 | Mutex::Autolock _l(mUidLock); |
| 3667 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3668 | if (mRegistered) return; |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3669 | status_t res = mAm.linkToDeath(this); |
| 3670 | mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3671 | | ActivityManager::UID_OBSERVER_IDLE |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3672 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE |
| 3673 | | ActivityManager::UID_OBSERVER_PROC_OOM_ADJ, |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3674 | ActivityManager::PROCESS_STATE_UNKNOWN, |
| 3675 | String16("cameraserver")); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3676 | if (res == OK) { |
| 3677 | mRegistered = true; |
| 3678 | ALOGV("UidPolicy: Registered with ActivityManager"); |
| 3679 | } |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | void CameraService::UidPolicy::unregisterSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3683 | Mutex::Autolock _l(mUidLock); |
| 3684 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3685 | mAm.unregisterUidObserver(this); |
| 3686 | mAm.unlinkToDeath(this); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3687 | mRegistered = false; |
| 3688 | mActiveUids.clear(); |
| 3689 | ALOGV("UidPolicy: Unregistered with ActivityManager"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { |
| 3693 | onUidIdle(uid, disabled); |
| 3694 | } |
| 3695 | |
| 3696 | void CameraService::UidPolicy::onUidActive(uid_t uid) { |
| 3697 | Mutex::Autolock _l(mUidLock); |
| 3698 | mActiveUids.insert(uid); |
| 3699 | } |
| 3700 | |
| 3701 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { |
| 3702 | bool deleted = false; |
| 3703 | { |
| 3704 | Mutex::Autolock _l(mUidLock); |
| 3705 | if (mActiveUids.erase(uid) > 0) { |
| 3706 | deleted = true; |
| 3707 | } |
| 3708 | } |
| 3709 | if (deleted) { |
| 3710 | sp<CameraService> service = mService.promote(); |
| 3711 | if (service != nullptr) { |
| 3712 | service->blockClientsForUid(uid); |
| 3713 | } |
| 3714 | } |
| 3715 | } |
| 3716 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3717 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 3718 | int64_t procStateSeq __unused, int32_t capability __unused) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3719 | bool procStateChange = false; |
| 3720 | { |
| 3721 | Mutex::Autolock _l(mUidLock); |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3722 | if (mMonitoredUids.find(uid) != mMonitoredUids.end() && |
| 3723 | mMonitoredUids[uid].procState != procState) { |
| 3724 | mMonitoredUids[uid].procState = procState; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3725 | procStateChange = true; |
| 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | if (procStateChange) { |
| 3730 | sp<CameraService> service = mService.promote(); |
| 3731 | if (service != nullptr) { |
| 3732 | service->notifyMonitoredUids(); |
| 3733 | } |
| 3734 | } |
| 3735 | } |
| 3736 | |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3737 | void CameraService::UidPolicy::onUidProcAdjChanged(uid_t uid) { |
| 3738 | bool procAdjChange = false; |
| 3739 | { |
| 3740 | Mutex::Autolock _l(mUidLock); |
| 3741 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
| 3742 | procAdjChange = true; |
| 3743 | } |
| 3744 | } |
| 3745 | |
| 3746 | if (procAdjChange) { |
| 3747 | sp<CameraService> service = mService.promote(); |
| 3748 | if (service != nullptr) { |
| 3749 | service->notifyMonitoredUids(); |
| 3750 | } |
| 3751 | } |
| 3752 | } |
| 3753 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3754 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid) { |
| 3755 | Mutex::Autolock _l(mUidLock); |
| 3756 | auto it = mMonitoredUids.find(uid); |
| 3757 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3758 | it->second.refCount++; |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3759 | } else { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3760 | MonitoredUid monitoredUid; |
| 3761 | monitoredUid.procState = ActivityManager::PROCESS_STATE_NONEXISTENT; |
| 3762 | monitoredUid.refCount = 1; |
| 3763 | mMonitoredUids.emplace(std::pair<uid_t, MonitoredUid>(uid, monitoredUid)); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3764 | } |
| 3765 | } |
| 3766 | |
| 3767 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid) { |
| 3768 | Mutex::Autolock _l(mUidLock); |
| 3769 | auto it = mMonitoredUids.find(uid); |
| 3770 | if (it != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3771 | it->second.refCount--; |
| 3772 | if (it->second.refCount == 0) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3773 | mMonitoredUids.erase(it); |
| 3774 | } |
| 3775 | } else { |
| 3776 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 3777 | } |
| 3778 | } |
| 3779 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3780 | bool CameraService::UidPolicy::isUidActive(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3781 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3782 | return isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3783 | } |
| 3784 | |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3785 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; |
| 3786 | static const int64_t kPollUidActiveTimeoutMillis = 50; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3787 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3788 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3789 | // Non-app UIDs are considered always active |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3790 | // If activity manager is unreachable, assume everything is active |
| 3791 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3792 | return true; |
| 3793 | } |
| 3794 | auto it = mOverrideUids.find(uid); |
| 3795 | if (it != mOverrideUids.end()) { |
| 3796 | return it->second; |
| 3797 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3798 | bool active = mActiveUids.find(uid) != mActiveUids.end(); |
| 3799 | if (!active) { |
| 3800 | // We want active UIDs to always access camera with their first attempt since |
| 3801 | // there is no guarantee the app is robustly written and would retry getting |
| 3802 | // the camera on failure. The inverse case is not a problem as we would take |
| 3803 | // camera away soon once we get the callback that the uid is no longer active. |
| 3804 | ActivityManager am; |
| 3805 | // Okay to access with a lock held as UID changes are dispatched without |
| 3806 | // a lock and we are a higher level component. |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3807 | int64_t startTimeMillis = 0; |
| 3808 | do { |
| 3809 | // TODO: Fix this b/109950150! |
| 3810 | // Okay this is a hack. There is a race between the UID turning active and |
| 3811 | // activity being resumed. The proper fix is very risky, so we temporary add |
| 3812 | // some polling which should happen pretty rarely anyway as the race is hard |
| 3813 | // to hit. |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3814 | active = mActiveUids.find(uid) != mActiveUids.end(); |
Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 3815 | if (!active) active = am.isUidActive(uid, callingPackage); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3816 | if (active) { |
| 3817 | break; |
| 3818 | } |
| 3819 | if (startTimeMillis <= 0) { |
| 3820 | startTimeMillis = uptimeMillis(); |
| 3821 | } |
| 3822 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3823 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3824 | if (remainingTimeMillis <= 0) { |
| 3825 | break; |
| 3826 | } |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3827 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); |
| 3828 | |
| 3829 | mUidLock.unlock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3830 | usleep(remainingTimeMillis * 1000); |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3831 | mUidLock.lock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3832 | } while (true); |
| 3833 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3834 | if (active) { |
| 3835 | // Now that we found out the UID is actually active, cache that |
| 3836 | mActiveUids.insert(uid); |
| 3837 | } |
| 3838 | } |
| 3839 | return active; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3840 | } |
| 3841 | |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 3842 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { |
| 3843 | Mutex::Autolock _l(mUidLock); |
| 3844 | return getProcStateLocked(uid); |
| 3845 | } |
| 3846 | |
| 3847 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { |
| 3848 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 3849 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3850 | procState = mMonitoredUids[uid].procState; |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 3851 | } |
| 3852 | return procState; |
| 3853 | } |
| 3854 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3855 | void CameraService::UidPolicy::UidPolicy::addOverrideUid(uid_t uid, |
| 3856 | String16 callingPackage, bool active) { |
| 3857 | updateOverrideUid(uid, callingPackage, active, true); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3858 | } |
| 3859 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3860 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, String16 callingPackage) { |
| 3861 | updateOverrideUid(uid, callingPackage, false, false); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3862 | } |
| 3863 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3864 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 3865 | Mutex::Autolock _l(mUidLock); |
| 3866 | ALOGV("UidPolicy: ActivityManager has died"); |
| 3867 | mRegistered = false; |
| 3868 | mActiveUids.clear(); |
| 3869 | } |
| 3870 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3871 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, String16 callingPackage, |
| 3872 | bool active, bool insert) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3873 | bool wasActive = false; |
| 3874 | bool isActive = false; |
| 3875 | { |
| 3876 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3877 | wasActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3878 | mOverrideUids.erase(uid); |
| 3879 | if (insert) { |
| 3880 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 3881 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3882 | isActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3883 | } |
| 3884 | if (wasActive != isActive && !isActive) { |
| 3885 | sp<CameraService> service = mService.promote(); |
| 3886 | if (service != nullptr) { |
| 3887 | service->blockClientsForUid(uid); |
| 3888 | } |
| 3889 | } |
| 3890 | } |
| 3891 | |
| 3892 | // ---------------------------------------------------------------------------- |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3893 | // SensorPrivacyPolicy |
| 3894 | // ---------------------------------------------------------------------------- |
| 3895 | void CameraService::SensorPrivacyPolicy::registerSelf() { |
| 3896 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3897 | if (mRegistered) { |
| 3898 | return; |
| 3899 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3900 | hasCameraPrivacyFeature(); // Called so the result is cached |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 3901 | mSpm.addSensorPrivacyListener(this); |
| 3902 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); |
| 3903 | status_t res = mSpm.linkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3904 | if (res == OK) { |
| 3905 | mRegistered = true; |
| 3906 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); |
| 3907 | } |
| 3908 | } |
| 3909 | |
| 3910 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { |
| 3911 | Mutex::Autolock _l(mSensorPrivacyLock); |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 3912 | mSpm.removeSensorPrivacyListener(this); |
| 3913 | mSpm.unlinkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3914 | mRegistered = false; |
| 3915 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); |
| 3916 | } |
| 3917 | |
| 3918 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
| 3919 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3920 | return mSensorPrivacyEnabled; |
| 3921 | } |
| 3922 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3923 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled() { |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3924 | if (!hasCameraPrivacyFeature()) { |
| 3925 | return false; |
| 3926 | } |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3927 | return mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3928 | } |
| 3929 | |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3930 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged( |
| 3931 | int toggleType __unused, int sensor __unused, bool enabled) { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3932 | { |
| 3933 | Mutex::Autolock _l(mSensorPrivacyLock); |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3934 | mSensorPrivacyEnabled = mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3935 | } |
| 3936 | // 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] | 3937 | if (enabled) { |
| 3938 | sp<CameraService> service = mService.promote(); |
| 3939 | if (service != nullptr) { |
| 3940 | service->blockAllClients(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3941 | } |
| 3942 | } |
| 3943 | return binder::Status::ok(); |
| 3944 | } |
| 3945 | |
| 3946 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 3947 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3948 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); |
| 3949 | mRegistered = false; |
| 3950 | } |
| 3951 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3952 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { |
Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3953 | bool supportsSoftwareToggle = mSpm.supportsSensorToggle( |
| 3954 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 3955 | bool supportsHardwareToggle = mSpm.supportsSensorToggle( |
| 3956 | SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); |
| 3957 | return supportsSoftwareToggle || supportsHardwareToggle; |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3958 | } |
| 3959 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3960 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3961 | // CameraState |
| 3962 | // ---------------------------------------------------------------------------- |
| 3963 | |
| 3964 | CameraService::CameraState::CameraState(const String8& id, int cost, |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 3965 | const std::set<String8>& conflicting, SystemCameraKind systemCameraKind, |
| 3966 | const std::vector<std::string>& physicalCameras) : mId(id), |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3967 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 3968 | mSystemCameraKind(systemCameraKind), mPhysicalCameras(physicalCameras) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3969 | |
| 3970 | CameraService::CameraState::~CameraState() {} |
| 3971 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3972 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3973 | Mutex::Autolock lock(mStatusLock); |
| 3974 | return mStatus; |
| 3975 | } |
| 3976 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3977 | std::vector<String8> CameraService::CameraState::getUnavailablePhysicalIds() const { |
| 3978 | Mutex::Autolock lock(mStatusLock); |
| 3979 | std::vector<String8> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); |
| 3980 | return res; |
| 3981 | } |
| 3982 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3983 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 3984 | return mShimParams; |
| 3985 | } |
| 3986 | |
| 3987 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 3988 | mShimParams = params; |
| 3989 | } |
| 3990 | |
| 3991 | int CameraService::CameraState::getCost() const { |
| 3992 | return mCost; |
| 3993 | } |
| 3994 | |
| 3995 | std::set<String8> CameraService::CameraState::getConflicting() const { |
| 3996 | return mConflicting; |
| 3997 | } |
| 3998 | |
| 3999 | String8 CameraService::CameraState::getId() const { |
| 4000 | return mId; |
| 4001 | } |
| 4002 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4003 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { |
| 4004 | return mSystemCameraKind; |
| 4005 | } |
| 4006 | |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4007 | bool CameraService::CameraState::containsPhysicalCamera(const std::string& physicalCameraId) const { |
| 4008 | return std::find(mPhysicalCameras.begin(), mPhysicalCameras.end(), physicalCameraId) |
| 4009 | != mPhysicalCameras.end(); |
| 4010 | } |
| 4011 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4012 | bool CameraService::CameraState::addUnavailablePhysicalId(const String8& physicalId) { |
| 4013 | Mutex::Autolock lock(mStatusLock); |
| 4014 | auto result = mUnavailablePhysicalIds.insert(physicalId); |
| 4015 | return result.second; |
| 4016 | } |
| 4017 | |
| 4018 | bool CameraService::CameraState::removeUnavailablePhysicalId(const String8& physicalId) { |
| 4019 | Mutex::Autolock lock(mStatusLock); |
| 4020 | auto count = mUnavailablePhysicalIds.erase(physicalId); |
| 4021 | return count > 0; |
| 4022 | } |
| 4023 | |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 4024 | void CameraService::CameraState::setClientPackage(const String8& clientPackage) { |
| 4025 | Mutex::Autolock lock(mStatusLock); |
| 4026 | mClientPackage = clientPackage; |
| 4027 | } |
| 4028 | |
| 4029 | String8 CameraService::CameraState::getClientPackage() const { |
| 4030 | Mutex::Autolock lock(mStatusLock); |
| 4031 | return mClientPackage; |
| 4032 | } |
| 4033 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4034 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4035 | // ClientEventListener |
| 4036 | // ---------------------------------------------------------------------------- |
| 4037 | |
| 4038 | void CameraService::ClientEventListener::onClientAdded( |
| 4039 | const resource_policy::ClientDescriptor<String8, |
| 4040 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 4041 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4042 | if (basicClient.get() != nullptr) { |
| 4043 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 4044 | notifier.noteStartCamera(descriptor.getKey(), |
| 4045 | static_cast<int>(basicClient->getClientUid())); |
| 4046 | } |
| 4047 | } |
| 4048 | |
| 4049 | void CameraService::ClientEventListener::onClientRemoved( |
| 4050 | const resource_policy::ClientDescriptor<String8, |
| 4051 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 4052 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4053 | if (basicClient.get() != nullptr) { |
| 4054 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 4055 | notifier.noteStopCamera(descriptor.getKey(), |
| 4056 | static_cast<int>(basicClient->getClientUid())); |
| 4057 | } |
| 4058 | } |
| 4059 | |
| 4060 | |
| 4061 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4062 | // CameraClientManager |
| 4063 | // ---------------------------------------------------------------------------- |
| 4064 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4065 | CameraService::CameraClientManager::CameraClientManager() { |
| 4066 | setListener(std::make_shared<ClientEventListener>()); |
| 4067 | } |
| 4068 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4069 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 4070 | |
| 4071 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
| 4072 | const String8& id) const { |
| 4073 | auto descriptor = get(id); |
| 4074 | if (descriptor == nullptr) { |
| 4075 | return sp<BasicClient>{nullptr}; |
| 4076 | } |
| 4077 | return descriptor->getValue(); |
| 4078 | } |
| 4079 | |
| 4080 | String8 CameraService::CameraClientManager::toString() const { |
| 4081 | auto all = getAll(); |
| 4082 | String8 ret("["); |
| 4083 | bool hasAny = false; |
| 4084 | for (auto& i : all) { |
| 4085 | hasAny = true; |
| 4086 | String8 key = i->getKey(); |
| 4087 | int32_t cost = i->getCost(); |
| 4088 | int32_t pid = i->getOwnerId(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4089 | int32_t score = i->getPriority().getScore(); |
| 4090 | int32_t state = i->getPriority().getState(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4091 | auto conflicting = i->getConflicting(); |
| 4092 | auto clientSp = i->getValue(); |
| 4093 | String8 packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 4094 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4095 | if (clientSp.get() != nullptr) { |
| 4096 | packageName = String8{clientSp->getPackageName()}; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4097 | uid_t clientUid = clientSp->getClientUid(); |
| 4098 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4099 | } |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4100 | ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" |
| 4101 | PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4102 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4103 | if (clientSp.get() != nullptr) { |
| 4104 | ret.appendFormat("User Id: %d, ", clientUserId); |
| 4105 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4106 | if (packageName.size() != 0) { |
| 4107 | ret.appendFormat("Client Package Name: %s", packageName.string()); |
| 4108 | } |
| 4109 | |
| 4110 | ret.append(", Conflicting Client Devices: {"); |
| 4111 | for (auto& j : conflicting) { |
| 4112 | ret.appendFormat("%s, ", j.string()); |
| 4113 | } |
| 4114 | ret.append("})"); |
| 4115 | } |
| 4116 | if (hasAny) ret.append("\n"); |
| 4117 | ret.append("]\n"); |
| 4118 | return ret; |
| 4119 | } |
| 4120 | |
| 4121 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
| 4122 | const String8& key, const sp<BasicClient>& value, int32_t cost, |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4123 | const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4124 | int32_t state, int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4125 | |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4126 | int32_t score_adj = systemNativeClient ? kSystemNativeClientScore : score; |
| 4127 | int32_t state_adj = systemNativeClient ? kSystemNativeClientState: state; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 4128 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4129 | return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>( |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4130 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, |
| 4131 | systemNativeClient, oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 4135 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4136 | int32_t oomScoreOffset, bool systemNativeClient) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4137 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4138 | partial->getConflicting(), partial->getPriority().getScore(), |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4139 | partial->getOwnerId(), partial->getPriority().getState(), oomScoreOffset, |
| 4140 | systemNativeClient); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | // ---------------------------------------------------------------------------- |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4144 | // InjectionStatusListener |
| 4145 | // ---------------------------------------------------------------------------- |
| 4146 | |
| 4147 | void CameraService::InjectionStatusListener::addListener( |
| 4148 | const sp<ICameraInjectionCallback>& callback) { |
| 4149 | Mutex::Autolock lock(mListenerLock); |
| 4150 | if (mCameraInjectionCallback) return; |
| 4151 | status_t res = IInterface::asBinder(callback)->linkToDeath(this); |
| 4152 | if (res == OK) { |
| 4153 | mCameraInjectionCallback = callback; |
| 4154 | } |
| 4155 | } |
| 4156 | |
| 4157 | void CameraService::InjectionStatusListener::removeListener() { |
| 4158 | Mutex::Autolock lock(mListenerLock); |
| 4159 | if (mCameraInjectionCallback == nullptr) { |
| 4160 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 4161 | return; |
| 4162 | } |
| 4163 | IInterface::asBinder(mCameraInjectionCallback)->unlinkToDeath(this); |
| 4164 | mCameraInjectionCallback = nullptr; |
| 4165 | } |
| 4166 | |
| 4167 | void CameraService::InjectionStatusListener::notifyInjectionError( |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4168 | String8 injectedCamId, status_t err) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4169 | if (mCameraInjectionCallback == nullptr) { |
| 4170 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 4171 | return; |
| 4172 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4173 | |
| 4174 | switch (err) { |
| 4175 | case -ENODEV: |
| 4176 | mCameraInjectionCallback->onInjectionError( |
| 4177 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4178 | ALOGE("No camera device with ID \"%s\" currently available!", |
| 4179 | injectedCamId.string()); |
| 4180 | break; |
| 4181 | case -EBUSY: |
| 4182 | mCameraInjectionCallback->onInjectionError( |
| 4183 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4184 | ALOGE("Higher-priority client using camera, ID \"%s\" currently unavailable!", |
| 4185 | injectedCamId.string()); |
| 4186 | break; |
| 4187 | case DEAD_OBJECT: |
| 4188 | mCameraInjectionCallback->onInjectionError( |
| 4189 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4190 | ALOGE("Camera ID \"%s\" object is dead!", |
| 4191 | injectedCamId.string()); |
| 4192 | break; |
| 4193 | case INVALID_OPERATION: |
| 4194 | mCameraInjectionCallback->onInjectionError( |
| 4195 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); |
| 4196 | ALOGE("Camera ID \"%s\" encountered an operating or internal error!", |
| 4197 | injectedCamId.string()); |
| 4198 | break; |
| 4199 | case UNKNOWN_TRANSACTION: |
| 4200 | mCameraInjectionCallback->onInjectionError( |
| 4201 | ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED); |
| 4202 | ALOGE("Camera ID \"%s\" method doesn't support!", |
| 4203 | injectedCamId.string()); |
| 4204 | break; |
| 4205 | default: |
| 4206 | mCameraInjectionCallback->onInjectionError( |
| 4207 | ICameraInjectionCallback::ERROR_INJECTION_INVALID_ERROR); |
| 4208 | ALOGE("Unexpected error %s (%d) opening camera \"%s\"!", |
| 4209 | strerror(-err), err, injectedCamId.string()); |
| 4210 | } |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | void CameraService::InjectionStatusListener::binderDied( |
| 4214 | const wp<IBinder>& /*who*/) { |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4215 | ALOGV("InjectionStatusListener: ICameraInjectionCallback has died"); |
| 4216 | auto parent = mParent.promote(); |
| 4217 | if (parent != nullptr) { |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4218 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 4219 | if (clientDescriptor != nullptr) { |
| 4220 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 4221 | baseClientPtr->stopInjection(); |
| 4222 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 4223 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4224 | } |
| 4225 | } |
| 4226 | |
| 4227 | // ---------------------------------------------------------------------------- |
| 4228 | // CameraInjectionSession |
| 4229 | // ---------------------------------------------------------------------------- |
| 4230 | |
| 4231 | binder::Status CameraService::CameraInjectionSession::stopInjection() { |
| 4232 | Mutex::Autolock lock(mInjectionSessionLock); |
| 4233 | auto parent = mParent.promote(); |
| 4234 | if (parent == nullptr) { |
| 4235 | ALOGE("CameraInjectionSession: Parent is gone"); |
| 4236 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SERVICE, |
| 4237 | "Camera service encountered error"); |
| 4238 | } |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4239 | |
| 4240 | status_t res = NO_ERROR; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4241 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); |
| 4242 | if (clientDescriptor != nullptr) { |
| 4243 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); |
| 4244 | res = baseClientPtr->stopInjection(); |
| 4245 | if (res != OK) { |
| 4246 | ALOGE("CameraInjectionSession: Failed to stop the injection camera!" |
| 4247 | " ret != NO_ERROR: %d", res); |
| 4248 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SESSION, |
| 4249 | "Camera session encountered error"); |
| 4250 | } |
| 4251 | } |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 4252 | parent->clearInjectionParameters(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4253 | return binder::Status::ok(); |
| 4254 | } |
| 4255 | |
| 4256 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4257 | |
| 4258 | static const int kDumpLockRetries = 50; |
| 4259 | static const int kDumpLockSleep = 60000; |
| 4260 | |
| 4261 | static bool tryLock(Mutex& mutex) |
| 4262 | { |
| 4263 | bool locked = false; |
| 4264 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 4265 | if (mutex.tryLock() == NO_ERROR) { |
| 4266 | locked = true; |
| 4267 | break; |
| 4268 | } |
| 4269 | usleep(kDumpLockSleep); |
| 4270 | } |
| 4271 | return locked; |
| 4272 | } |
| 4273 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4274 | void CameraService::cacheDump() { |
| 4275 | if (mMemFd != -1) { |
| 4276 | const Vector<String16> args; |
| 4277 | ATRACE_CALL(); |
| 4278 | // Acquiring service lock here will avoid the deadlock since |
| 4279 | // cacheDump will not be called during the second disconnect. |
| 4280 | Mutex::Autolock lock(mServiceLock); |
| 4281 | |
| 4282 | Mutex::Autolock l(mCameraStatesLock); |
| 4283 | // Start collecting the info for open sessions and store it in temp file. |
| 4284 | for (const auto& state : mCameraStates) { |
| 4285 | String8 cameraId = state.first; |
| 4286 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 4287 | if (clientDescriptor != nullptr) { |
| 4288 | dprintf(mMemFd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
| 4289 | // Log the current open session info before device is disconnected. |
| 4290 | dumpOpenSessionClientLogs(mMemFd, args, cameraId); |
| 4291 | } |
| 4292 | } |
| 4293 | } |
| 4294 | } |
| 4295 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4296 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4297 | ATRACE_CALL(); |
| 4298 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4299 | if (checkCallingPermission(sDumpPermission) == false) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4300 | 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] | 4301 | CameraThreadState::getCallingPid(), |
| 4302 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4303 | return NO_ERROR; |
| 4304 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4305 | bool locked = tryLock(mServiceLock); |
| 4306 | // failed to lock - CameraService is probably deadlocked |
| 4307 | if (!locked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4308 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4309 | } |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4310 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4311 | if (!mInitialized) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4312 | dprintf(fd, "!! No camera HAL available !!\n"); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 4313 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4314 | // Dump event log for error information |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4315 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4316 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4317 | if (locked) mServiceLock.unlock(); |
| 4318 | return NO_ERROR; |
| 4319 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4320 | dprintf(fd, "\n== Service global info: ==\n\n"); |
| 4321 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4322 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 4323 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", |
| 4324 | mNormalDeviceIdsWithoutSystemCamera.size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4325 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { |
| 4326 | dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); |
| 4327 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4328 | String8 activeClientString = mActiveClientManager.toString(); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4329 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string()); |
| 4330 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4331 | |
| 4332 | dumpEventLog(fd); |
| 4333 | |
| 4334 | bool stateLocked = tryLock(mCameraStatesLock); |
| 4335 | if (!stateLocked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4336 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4337 | } |
| 4338 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 4339 | int argSize = args.size(); |
| 4340 | for (int i = 0; i < argSize; i++) { |
| 4341 | if (args[i] == TagMonitor::kMonitorOption) { |
| 4342 | if (i + 1 < argSize) { |
| 4343 | mMonitorTags = String8(args[i + 1]); |
| 4344 | } |
| 4345 | break; |
| 4346 | } |
| 4347 | } |
| 4348 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4349 | for (auto& state : mCameraStates) { |
| 4350 | String8 cameraId = state.first; |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4351 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4352 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4353 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4354 | CameraParameters p = state.second->getShimParams(); |
| 4355 | if (!p.isEmpty()) { |
| 4356 | dprintf(fd, " Camera1 API shim is using parameters:\n "); |
| 4357 | p.dump(fd, args); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4358 | } |
| 4359 | |
| 4360 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4361 | if (clientDescriptor != nullptr) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4362 | // log the current open session info |
| 4363 | dumpOpenSessionClientLogs(fd, args, cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4364 | } else { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4365 | dumpClosedSessionClientLogs(fd, cameraId); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4366 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4367 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | if (stateLocked) mCameraStatesLock.unlock(); |
| 4371 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4372 | if (locked) mServiceLock.unlock(); |
| 4373 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 4374 | mCameraProviderManager->dump(fd, args); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4375 | |
| 4376 | dprintf(fd, "\n== Vendor tags: ==\n\n"); |
| 4377 | |
| 4378 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 4379 | if (desc == NULL) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 4380 | sp<VendorTagDescriptorCache> cache = |
| 4381 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 4382 | if (cache == NULL) { |
| 4383 | dprintf(fd, "No vendor tags.\n"); |
| 4384 | } else { |
| 4385 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 4386 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4387 | } else { |
| 4388 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 4389 | } |
| 4390 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4391 | // Dump camera traces if there were any |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4392 | dprintf(fd, "\n"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4393 | camera3::CameraTraces::dump(fd); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4394 | |
| 4395 | // Process dump arguments, if any |
| 4396 | int n = args.size(); |
| 4397 | String16 verboseOption("-v"); |
| 4398 | String16 unreachableOption("--unreachable"); |
| 4399 | for (int i = 0; i < n; i++) { |
| 4400 | if (args[i] == verboseOption) { |
| 4401 | // change logging level |
| 4402 | if (i + 1 >= n) continue; |
| 4403 | String8 levelStr(args[i+1]); |
| 4404 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4405 | dprintf(fd, "\nSetting log level to %d.\n", level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4406 | setLogLevel(level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4407 | } else if (args[i] == unreachableOption) { |
| 4408 | // Dump memory analysis |
| 4409 | // TODO - should limit be an argument parameter? |
| 4410 | UnreachableMemoryInfo info; |
| 4411 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); |
| 4412 | if (!success) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4413 | dprintf(fd, "\n== Unable to dump unreachable memory. " |
| 4414 | "Try disabling SELinux enforcement. ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4415 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4416 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4417 | std::string s = info.ToString(/*log_contents*/ true); |
| 4418 | write(fd, s.c_str(), s.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4419 | } |
| 4420 | } |
| 4421 | } |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4422 | |
| 4423 | bool serviceLocked = tryLock(mServiceLock); |
| 4424 | |
| 4425 | // Dump info from previous open sessions. |
| 4426 | // Reposition the offset to beginning of the file before reading |
| 4427 | |
| 4428 | if ((mMemFd >= 0) && (lseek(mMemFd, 0, SEEK_SET) != -1)) { |
| 4429 | dprintf(fd, "\n**********Dumpsys from previous open session**********\n"); |
| 4430 | ssize_t size_read; |
| 4431 | char buf[4096]; |
| 4432 | while ((size_read = read(mMemFd, buf, (sizeof(buf) - 1))) > 0) { |
| 4433 | // Read data from file to a small buffer and write it to fd. |
| 4434 | write(fd, buf, size_read); |
| 4435 | if (size_read == -1) { |
| 4436 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 4437 | break; |
| 4438 | } |
| 4439 | } |
| 4440 | dprintf(fd, "\n**********End of Dumpsys from previous open session**********\n"); |
| 4441 | } else { |
| 4442 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 4443 | } |
| 4444 | |
| 4445 | if (serviceLocked) mServiceLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4446 | return NO_ERROR; |
| 4447 | } |
| 4448 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4449 | void CameraService::dumpOpenSessionClientLogs(int fd, |
| 4450 | const Vector<String16>& args, const String8& cameraId) { |
| 4451 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Rucha Katakwar | 71a0e05 | 2022-04-07 15:44:18 -0700 | [diff] [blame] | 4452 | dprintf(fd, " %s : Device %s is open. Client instance dump:\n", |
| 4453 | getFormattedCurrentTime().string(), |
| 4454 | cameraId.string()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4455 | dprintf(fd, " Client priority score: %d state: %d\n", |
| 4456 | clientDescriptor->getPriority().getScore(), |
| 4457 | clientDescriptor->getPriority().getState()); |
| 4458 | dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 4459 | |
| 4460 | auto client = clientDescriptor->getValue(); |
| 4461 | dprintf(fd, " Client package: %s\n", |
| 4462 | String8(client->getPackageName()).string()); |
| 4463 | |
| 4464 | client->dumpClient(fd, args); |
| 4465 | } |
| 4466 | |
| 4467 | void CameraService::dumpClosedSessionClientLogs(int fd, const String8& cameraId) { |
| 4468 | dprintf(fd, " Device %s is closed, no client instance\n", |
| 4469 | cameraId.string()); |
| 4470 | } |
| 4471 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4472 | void CameraService::dumpEventLog(int fd) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4473 | 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] | 4474 | |
| 4475 | Mutex::Autolock l(mLogLock); |
| 4476 | for (const auto& msg : mEventLog) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4477 | dprintf(fd, " %s\n", msg.string()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4481 | dprintf(fd, " ...\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4482 | } else if (mEventLog.size() == 0) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4483 | dprintf(fd, " [no events yet]\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4484 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4485 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4486 | } |
| 4487 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4488 | void CameraService::cacheClientTagDumpIfNeeded(const char *cameraId, BasicClient* client) { |
| 4489 | Mutex::Autolock lock(mLogLock); |
| 4490 | if (!isClientWatchedLocked(client)) { return; } |
| 4491 | |
| 4492 | std::vector<std::string> dumpVector; |
| 4493 | client->dumpWatchedEventsToVector(dumpVector); |
| 4494 | |
| 4495 | if (dumpVector.empty()) { return; } |
| 4496 | |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 4497 | std::string dumpString; |
| 4498 | |
| 4499 | String8 currentTime = getFormattedCurrentTime(); |
| 4500 | dumpString += "Cached @ "; |
| 4501 | dumpString += currentTime.string(); |
| 4502 | dumpString += "\n"; // First line is the timestamp of when client is cached. |
| 4503 | |
| 4504 | |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4505 | const String16 &packageName = client->getPackageName(); |
| 4506 | |
| 4507 | String8 packageName8 = String8(packageName); |
| 4508 | const char *printablePackageName = packageName8.lockBuffer(packageName.size()); |
| 4509 | |
Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 4510 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4511 | size_t i = dumpVector.size(); |
| 4512 | |
| 4513 | // Store the string in reverse order (latest last) |
| 4514 | while (i > 0) { |
| 4515 | i--; |
| 4516 | dumpString += cameraId; |
| 4517 | dumpString += ":"; |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4518 | dumpString += printablePackageName; |
| 4519 | dumpString += " "; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4520 | dumpString += dumpVector[i]; // implicitly ends with '\n' |
| 4521 | } |
| 4522 | |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4523 | packageName8.unlockBuffer(); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4524 | mWatchedClientsDumpCache[packageName] = dumpString; |
| 4525 | } |
| 4526 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4527 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4528 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4529 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 4530 | if (mTorchClientMap[i] == who) { |
| 4531 | // 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] | 4532 | String8 cameraId = mTorchClientMap.keyAt(i); |
| 4533 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 4534 | if (res) { |
| 4535 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 4536 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 4537 | return; |
| 4538 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4539 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4540 | break; |
| 4541 | } |
| 4542 | } |
| 4543 | } |
| 4544 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4545 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 4546 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 4547 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4548 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 4549 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 4550 | */ |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 4551 | // PID here is approximate and can be wrong. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 4552 | logClientDied(CameraThreadState::getCallingPid(), String8("Binder died unexpectedly")); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4553 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4554 | // check torch client |
| 4555 | handleTorchClientBinderDied(who); |
| 4556 | |
| 4557 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4558 | if(!evictClientIdByRemote(who)) { |
| 4559 | 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] | 4560 | return; |
| 4561 | } |
| 4562 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4563 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 4564 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 4565 | } |
| 4566 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4567 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4568 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 4569 | } |
| 4570 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4571 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId, |
| 4572 | std::initializer_list<StatusInternal> rejectSourceStates) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4573 | // Do not lock mServiceLock here or can get into a deadlock from |
| 4574 | // connect() -> disconnect -> updateStatus |
| 4575 | |
| 4576 | auto state = getCameraState(cameraId); |
| 4577 | |
| 4578 | if (state == nullptr) { |
| 4579 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 4580 | cameraId.string()); |
| 4581 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4582 | } |
| 4583 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4584 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 4585 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 4586 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 4587 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 4588 | return; |
| 4589 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4590 | |
| 4591 | // Collect the logical cameras without holding mStatusLock in updateStatus |
| 4592 | // as that can lead to a deadlock(b/162192331). |
| 4593 | auto logicalCameraIds = getLogicalCameras(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4594 | // 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] | 4595 | // of the listeners with both the mStatusLock and mStatusListenerLock held |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 4596 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4597 | &logicalCameraIds] |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4598 | (const String8& cameraId, StatusInternal status) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4599 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4600 | if (status != StatusInternal::ENUMERATING) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4601 | // Update torch status if it has a flash unit. |
| 4602 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4603 | TorchModeStatus torchStatus; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4604 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 4605 | NAME_NOT_FOUND) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4606 | TorchModeStatus newTorchStatus = |
| 4607 | status == StatusInternal::PRESENT ? |
| 4608 | TorchModeStatus::AVAILABLE_OFF : |
| 4609 | TorchModeStatus::NOT_AVAILABLE; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4610 | if (torchStatus != newTorchStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 4611 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4612 | } |
| 4613 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4614 | } |
| 4615 | |
| 4616 | Mutex::Autolock lock(mStatusListenerLock); |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4617 | notifyPhysicalCameraStatusLocked(mapToInterface(status), String16(cameraId), |
| 4618 | logicalCameraIds, deviceKind); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4619 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4620 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 4621 | bool isVendorListener = listener->isVendorListener(); |
| 4622 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, |
| 4623 | listener->getListenerPid(), listener->getListenerUid()) || |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 4624 | isVendorListener) { |
| 4625 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4626 | cameraId.c_str()); |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 4627 | continue; |
| 4628 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4629 | listener->getListener()->onStatusChanged(mapToInterface(status), |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4630 | String16(cameraId)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4631 | } |
| 4632 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4633 | } |
| 4634 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4635 | void CameraService::updateOpenCloseStatus(const String8& cameraId, bool open, |
| 4636 | const String16& clientPackageName) { |
Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 4637 | auto state = getCameraState(cameraId); |
| 4638 | if (state == nullptr) { |
| 4639 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 4640 | cameraId.string()); |
| 4641 | return; |
| 4642 | } |
| 4643 | if (open) { |
| 4644 | state->setClientPackage(String8(clientPackageName)); |
| 4645 | } else { |
| 4646 | state->setClientPackage(String8::empty()); |
| 4647 | } |
| 4648 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4649 | Mutex::Autolock lock(mStatusListenerLock); |
| 4650 | |
| 4651 | for (const auto& it : mListenerList) { |
| 4652 | if (!it->isOpenCloseCallbackAllowed()) { |
| 4653 | continue; |
| 4654 | } |
| 4655 | |
| 4656 | binder::Status ret; |
| 4657 | String16 cameraId64(cameraId); |
| 4658 | if (open) { |
| 4659 | ret = it->getListener()->onCameraOpened(cameraId64, clientPackageName); |
| 4660 | } else { |
| 4661 | ret = it->getListener()->onCameraClosed(cameraId64); |
| 4662 | } |
| 4663 | if (!ret.isOk()) { |
| 4664 | ALOGE("%s: Failed to trigger onCameraOpened/onCameraClosed callback: %d", __FUNCTION__, |
| 4665 | ret.exceptionCode()); |
| 4666 | } |
| 4667 | } |
| 4668 | } |
| 4669 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4670 | template<class Func> |
| 4671 | void CameraService::CameraState::updateStatus(StatusInternal status, |
| 4672 | const String8& cameraId, |
| 4673 | std::initializer_list<StatusInternal> rejectSourceStates, |
| 4674 | Func onStatusUpdatedLocked) { |
| 4675 | Mutex::Autolock lock(mStatusLock); |
| 4676 | StatusInternal oldStatus = mStatus; |
| 4677 | mStatus = status; |
| 4678 | |
| 4679 | if (oldStatus == status) { |
| 4680 | return; |
| 4681 | } |
| 4682 | |
| 4683 | ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__, |
| 4684 | cameraId.string(), oldStatus, status); |
| 4685 | |
| 4686 | if (oldStatus == StatusInternal::NOT_PRESENT && |
| 4687 | (status != StatusInternal::PRESENT && |
| 4688 | status != StatusInternal::ENUMERATING)) { |
| 4689 | |
| 4690 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 4691 | __FUNCTION__); |
| 4692 | mStatus = oldStatus; |
| 4693 | return; |
| 4694 | } |
| 4695 | |
| 4696 | /** |
| 4697 | * Sometimes we want to conditionally do a transition. |
| 4698 | * For example if a client disconnects, we want to go to PRESENT |
| 4699 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 4700 | */ |
| 4701 | for (auto& rejectStatus : rejectSourceStates) { |
| 4702 | if (oldStatus == rejectStatus) { |
| 4703 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
| 4704 | "state was was in one of the bad states.", __FUNCTION__, cameraId.string()); |
| 4705 | mStatus = oldStatus; |
| 4706 | return; |
| 4707 | } |
| 4708 | } |
| 4709 | |
| 4710 | onStatusUpdatedLocked(cameraId, status); |
| 4711 | } |
| 4712 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4713 | status_t CameraService::getTorchStatusLocked( |
| 4714 | const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4715 | TorchModeStatus *status) const { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4716 | if (!status) { |
| 4717 | return BAD_VALUE; |
| 4718 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4719 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 4720 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4721 | // invalid camera ID or the camera doesn't have a flash unit |
| 4722 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4723 | } |
| 4724 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4725 | *status = mTorchStatusMap.valueAt(index); |
| 4726 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4727 | } |
| 4728 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4729 | status_t CameraService::setTorchStatusLocked(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4730 | TorchModeStatus status) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4731 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 4732 | if (index == NAME_NOT_FOUND) { |
| 4733 | return BAD_VALUE; |
| 4734 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4735 | mTorchStatusMap.editValueAt(index) = status; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4736 | |
| 4737 | return OK; |
| 4738 | } |
| 4739 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4740 | std::list<String16> CameraService::getLogicalCameras( |
| 4741 | const String8& physicalCameraId) { |
| 4742 | std::list<String16> retList; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4743 | Mutex::Autolock lock(mCameraStatesLock); |
| 4744 | for (const auto& state : mCameraStates) { |
Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4745 | if (state.second->containsPhysicalCamera(physicalCameraId.c_str())) { |
| 4746 | retList.emplace_back(String16(state.first)); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4747 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4748 | } |
| 4749 | return retList; |
| 4750 | } |
| 4751 | |
| 4752 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, |
| 4753 | const String16& physicalCameraId, const std::list<String16>& logicalCameraIds, |
| 4754 | SystemCameraKind deviceKind) { |
| 4755 | // mStatusListenerLock is expected to be locked |
| 4756 | for (const auto& logicalCameraId : logicalCameraIds) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4757 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4758 | // Note: we check only the deviceKind of the physical camera id |
| 4759 | // since, logical camera ids and their physical camera ids are |
| 4760 | // guaranteed to have the same system camera kind. |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 4761 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 4762 | listener->getListenerPid(), listener->getListenerUid())) { |
| 4763 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4764 | String8(physicalCameraId).c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 4765 | continue; |
| 4766 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4767 | listener->getListener()->onPhysicalCameraStatusChanged(status, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4768 | logicalCameraId, physicalCameraId); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4769 | } |
| 4770 | } |
| 4771 | } |
| 4772 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4773 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4774 | void CameraService::blockClientsForUid(uid_t uid) { |
| 4775 | const auto clients = mActiveClientManager.getAll(); |
| 4776 | for (auto& current : clients) { |
| 4777 | if (current != nullptr) { |
| 4778 | const auto basicClient = current->getValue(); |
| 4779 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { |
| 4780 | basicClient->block(); |
| 4781 | } |
| 4782 | } |
| 4783 | } |
| 4784 | } |
| 4785 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4786 | void CameraService::blockAllClients() { |
| 4787 | const auto clients = mActiveClientManager.getAll(); |
| 4788 | for (auto& current : clients) { |
| 4789 | if (current != nullptr) { |
| 4790 | const auto basicClient = current->getValue(); |
| 4791 | if (basicClient.get() != nullptr) { |
| 4792 | basicClient->block(); |
| 4793 | } |
| 4794 | } |
| 4795 | } |
| 4796 | } |
| 4797 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4798 | // NOTE: This is a remote API - make sure all args are validated |
| 4799 | status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) { |
| 4800 | if (!checkCallingPermission(sManageCameraPermission, nullptr, nullptr)) { |
| 4801 | return PERMISSION_DENIED; |
| 4802 | } |
| 4803 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 4804 | return BAD_VALUE; |
| 4805 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4806 | if (args.size() >= 3 && args[0] == String16("set-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4807 | return handleSetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4808 | } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4809 | return handleResetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4810 | } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4811 | return handleGetUidState(args, out, err); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4812 | } else if (args.size() >= 2 && args[0] == String16("set-rotate-and-crop")) { |
| 4813 | return handleSetRotateAndCrop(args); |
| 4814 | } else if (args.size() >= 1 && args[0] == String16("get-rotate-and-crop")) { |
| 4815 | return handleGetRotateAndCrop(out); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4816 | } else if (args.size() >= 2 && args[0] == String16("set-image-dump-mask")) { |
| 4817 | return handleSetImageDumpMask(args); |
| 4818 | } else if (args.size() >= 1 && args[0] == String16("get-image-dump-mask")) { |
| 4819 | return handleGetImageDumpMask(out); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 4820 | } else if (args.size() >= 2 && args[0] == String16("set-camera-mute")) { |
| 4821 | return handleSetCameraMute(args); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4822 | } else if (args.size() >= 2 && args[0] == String16("watch")) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 4823 | return handleWatchCommand(args, in, out); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4824 | } else if (args.size() == 1 && args[0] == String16("help")) { |
| 4825 | printHelp(out); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4826 | return OK; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4827 | } |
| 4828 | printHelp(err); |
| 4829 | return BAD_VALUE; |
| 4830 | } |
| 4831 | |
| 4832 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4833 | String16 packageName = args[1]; |
| 4834 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4835 | bool active = false; |
| 4836 | if (args[2] == String16("active")) { |
| 4837 | active = true; |
| 4838 | } else if ((args[2] != String16("idle"))) { |
| 4839 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string()); |
| 4840 | return BAD_VALUE; |
| 4841 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4842 | |
| 4843 | int userId = 0; |
| 4844 | if (args.size() >= 5 && args[3] == String16("--user")) { |
| 4845 | userId = atoi(String8(args[4])); |
| 4846 | } |
| 4847 | |
| 4848 | uid_t uid; |
| 4849 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
| 4850 | return BAD_VALUE; |
| 4851 | } |
| 4852 | |
| 4853 | mUidPolicy->addOverrideUid(uid, packageName, active); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4854 | return NO_ERROR; |
| 4855 | } |
| 4856 | |
| 4857 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4858 | String16 packageName = args[1]; |
| 4859 | |
| 4860 | int userId = 0; |
| 4861 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 4862 | userId = atoi(String8(args[3])); |
| 4863 | } |
| 4864 | |
| 4865 | uid_t uid; |
| 4866 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4867 | return BAD_VALUE; |
| 4868 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4869 | |
| 4870 | mUidPolicy->removeOverrideUid(uid, packageName); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4871 | return NO_ERROR; |
| 4872 | } |
| 4873 | |
| 4874 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4875 | String16 packageName = args[1]; |
| 4876 | |
| 4877 | int userId = 0; |
| 4878 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 4879 | userId = atoi(String8(args[3])); |
| 4880 | } |
| 4881 | |
| 4882 | uid_t uid; |
| 4883 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4884 | return BAD_VALUE; |
| 4885 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4886 | |
| 4887 | if (mUidPolicy->isUidActive(uid, packageName)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4888 | return dprintf(out, "active\n"); |
| 4889 | } else { |
| 4890 | return dprintf(out, "idle\n"); |
| 4891 | } |
| 4892 | } |
| 4893 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4894 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { |
| 4895 | int rotateValue = atoi(String8(args[1])); |
| 4896 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || |
| 4897 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; |
| 4898 | Mutex::Autolock lock(mServiceLock); |
| 4899 | |
| 4900 | mOverrideRotateAndCropMode = rotateValue; |
| 4901 | |
| 4902 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; |
| 4903 | |
| 4904 | const auto clients = mActiveClientManager.getAll(); |
| 4905 | for (auto& current : clients) { |
| 4906 | if (current != nullptr) { |
| 4907 | const auto basicClient = current->getValue(); |
| 4908 | if (basicClient.get() != nullptr) { |
| 4909 | basicClient->setRotateAndCropOverride(rotateValue); |
| 4910 | } |
| 4911 | } |
| 4912 | } |
| 4913 | |
| 4914 | return OK; |
| 4915 | } |
| 4916 | |
| 4917 | status_t CameraService::handleGetRotateAndCrop(int out) { |
| 4918 | Mutex::Autolock lock(mServiceLock); |
| 4919 | |
| 4920 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); |
| 4921 | } |
| 4922 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4923 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { |
| 4924 | char *endPtr; |
| 4925 | errno = 0; |
| 4926 | String8 maskString8 = String8(args[1]); |
| 4927 | long maskValue = strtol(maskString8.c_str(), &endPtr, 10); |
| 4928 | |
| 4929 | if (errno != 0) return BAD_VALUE; |
| 4930 | if (endPtr != maskString8.c_str() + maskString8.size()) return BAD_VALUE; |
| 4931 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; |
| 4932 | |
| 4933 | Mutex::Autolock lock(mServiceLock); |
| 4934 | |
| 4935 | mImageDumpMask = maskValue; |
| 4936 | |
| 4937 | return OK; |
| 4938 | } |
| 4939 | |
| 4940 | status_t CameraService::handleGetImageDumpMask(int out) { |
| 4941 | Mutex::Autolock lock(mServiceLock); |
| 4942 | |
| 4943 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); |
| 4944 | } |
| 4945 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 4946 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { |
| 4947 | int muteValue = strtol(String8(args[1]), nullptr, 10); |
| 4948 | if (errno != 0) return BAD_VALUE; |
| 4949 | |
| 4950 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; |
| 4951 | Mutex::Autolock lock(mServiceLock); |
| 4952 | |
| 4953 | mOverrideCameraMuteMode = (muteValue == 1); |
| 4954 | |
| 4955 | const auto clients = mActiveClientManager.getAll(); |
| 4956 | for (auto& current : clients) { |
| 4957 | if (current != nullptr) { |
| 4958 | const auto basicClient = current->getValue(); |
| 4959 | if (basicClient.get() != nullptr) { |
| 4960 | if (basicClient->supportsCameraMute()) { |
| 4961 | basicClient->setCameraMute(mOverrideCameraMuteMode); |
| 4962 | } |
| 4963 | } |
| 4964 | } |
| 4965 | } |
| 4966 | |
| 4967 | return OK; |
| 4968 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4969 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 4970 | status_t CameraService::handleWatchCommand(const Vector<String16>& args, int inFd, int outFd) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4971 | if (args.size() >= 3 && args[1] == String16("start")) { |
| 4972 | return startWatchingTags(args, outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4973 | } else if (args.size() == 2 && args[1] == String16("stop")) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4974 | return stopWatchingTags(outFd); |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 4975 | } else if (args.size() == 2 && args[1] == String16("dump")) { |
| 4976 | return printWatchedTags(outFd); |
| 4977 | } else if (args.size() >= 2 && args[1] == String16("live")) { |
| 4978 | return printWatchedTagsUntilInterrupt(args, inFd, outFd); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 4979 | } else if (args.size() == 2 && args[1] == String16("clear")) { |
| 4980 | return clearCachedMonitoredTagDumps(outFd); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4981 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4982 | dprintf(outFd, "Camera service watch commands:\n" |
| 4983 | " start -m <comma_separated_tag_list> [-c <comma_separated_client_list>]\n" |
| 4984 | " starts watching the provided tags for clients with provided package\n" |
| 4985 | " recognizes tag shorthands like '3a'\n" |
| 4986 | " 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] | 4987 | " dump dumps the monitoring information and exits\n" |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4988 | " stop stops watching all tags\n" |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 4989 | " live [-n <refresh_interval_ms>]\n" |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4990 | " prints the monitored information in real time\n" |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 4991 | " Hit return to exit\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 4992 | " clear clears all buffers storing information for watch command"); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4993 | return BAD_VALUE; |
| 4994 | } |
| 4995 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4996 | status_t CameraService::startWatchingTags(const Vector<String16> &args, int outFd) { |
| 4997 | Mutex::Autolock lock(mLogLock); |
| 4998 | size_t tagsIdx; // index of '-m' |
| 4999 | String16 tags(""); |
| 5000 | for (tagsIdx = 2; tagsIdx < args.size() && args[tagsIdx] != String16("-m"); tagsIdx++); |
| 5001 | if (tagsIdx < args.size() - 1) { |
| 5002 | tags = args[tagsIdx + 1]; |
| 5003 | } else { |
| 5004 | dprintf(outFd, "No tags provided.\n"); |
| 5005 | return BAD_VALUE; |
| 5006 | } |
| 5007 | |
| 5008 | size_t clientsIdx; // index of '-c' |
| 5009 | String16 clients = kWatchAllClientsFlag; // watch all clients if no clients are provided |
| 5010 | for (clientsIdx = 2; clientsIdx < args.size() && args[clientsIdx] != String16("-c"); |
| 5011 | clientsIdx++); |
| 5012 | if (clientsIdx < args.size() - 1) { |
| 5013 | clients = args[clientsIdx + 1]; |
| 5014 | } |
| 5015 | parseClientsToWatchLocked(String8(clients)); |
| 5016 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5017 | // track tags to initialize future clients with the monitoring information |
| 5018 | mMonitorTags = String8(tags); |
| 5019 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5020 | bool serviceLock = tryLock(mServiceLock); |
| 5021 | int numWatchedClients = 0; |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5022 | auto cameraClients = mActiveClientManager.getAll(); |
| 5023 | for (const auto &clientDescriptor: cameraClients) { |
| 5024 | if (clientDescriptor == nullptr) { continue; } |
| 5025 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5026 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5027 | |
| 5028 | if (isClientWatchedLocked(client.get())) { |
| 5029 | client->startWatchingTags(mMonitorTags, outFd); |
| 5030 | numWatchedClients++; |
| 5031 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5032 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5033 | dprintf(outFd, "Started watching %d active clients\n", numWatchedClients); |
| 5034 | |
| 5035 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5036 | return OK; |
| 5037 | } |
| 5038 | |
| 5039 | status_t CameraService::stopWatchingTags(int outFd) { |
| 5040 | // clear mMonitorTags to prevent new clients from monitoring tags at initialization |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5041 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5042 | mMonitorTags = String8::empty(); |
| 5043 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5044 | mWatchedClientPackages.clear(); |
| 5045 | mWatchedClientsDumpCache.clear(); |
| 5046 | |
| 5047 | bool serviceLock = tryLock(mServiceLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5048 | auto cameraClients = mActiveClientManager.getAll(); |
| 5049 | for (const auto &clientDescriptor : cameraClients) { |
| 5050 | if (clientDescriptor == nullptr) { continue; } |
| 5051 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5052 | if (client.get() == nullptr) { continue; } |
| 5053 | client->stopWatchingTags(outFd); |
| 5054 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5055 | dprintf(outFd, "Stopped watching all clients.\n"); |
| 5056 | if (serviceLock) { mServiceLock.unlock(); } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5057 | return OK; |
| 5058 | } |
| 5059 | |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5060 | status_t CameraService::clearCachedMonitoredTagDumps(int outFd) { |
| 5061 | Mutex::Autolock lock(mLogLock); |
| 5062 | size_t clearedSize = mWatchedClientsDumpCache.size(); |
| 5063 | mWatchedClientsDumpCache.clear(); |
| 5064 | dprintf(outFd, "Cleared tag information of %zu cached clients.\n", clearedSize); |
| 5065 | return OK; |
| 5066 | } |
| 5067 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5068 | status_t CameraService::printWatchedTags(int outFd) { |
| 5069 | Mutex::Autolock logLock(mLogLock); |
Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 5070 | std::set<String16> connectedMonitoredClients; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5071 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5072 | bool printedSomething = false; // tracks if any monitoring information was printed |
| 5073 | // (from either cached or active clients) |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5074 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5075 | bool serviceLock = tryLock(mServiceLock); |
| 5076 | // get all watched clients that are currently connected |
| 5077 | for (const auto &clientDescriptor: mActiveClientManager.getAll()) { |
| 5078 | if (clientDescriptor == nullptr) { continue; } |
| 5079 | |
| 5080 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5081 | if (client.get() == nullptr) { continue; } |
| 5082 | if (!isClientWatchedLocked(client.get())) { continue; } |
| 5083 | |
| 5084 | std::vector<std::string> dumpVector; |
| 5085 | client->dumpWatchedEventsToVector(dumpVector); |
| 5086 | |
| 5087 | size_t printIdx = dumpVector.size(); |
| 5088 | if (printIdx == 0) { |
| 5089 | continue; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5090 | } |
| 5091 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5092 | // Print tag dumps for active client |
| 5093 | const String8 &cameraId = clientDescriptor->getKey(); |
| 5094 | String8 packageName8 = String8(client->getPackageName()); |
| 5095 | const char *printablePackageName = packageName8.lockBuffer(packageName8.size()); |
| 5096 | dprintf(outFd, "Client: %s (active)\n", printablePackageName); |
| 5097 | while(printIdx > 0) { |
| 5098 | printIdx--; |
| 5099 | dprintf(outFd, "%s:%s %s", cameraId.string(), printablePackageName, |
| 5100 | dumpVector[printIdx].c_str()); |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5101 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5102 | dprintf(outFd, "\n"); |
| 5103 | packageName8.unlockBuffer(); |
| 5104 | printedSomething = true; |
| 5105 | |
| 5106 | connectedMonitoredClients.emplace(client->getPackageName()); |
| 5107 | } |
| 5108 | if (serviceLock) { mServiceLock.unlock(); } |
| 5109 | |
| 5110 | // Print entries in mWatchedClientsDumpCache for clients that are not connected |
| 5111 | for (const auto &kv: mWatchedClientsDumpCache) { |
| 5112 | const String16 &package = kv.first; |
| 5113 | if (connectedMonitoredClients.find(package) != connectedMonitoredClients.end()) { |
| 5114 | continue; |
| 5115 | } |
| 5116 | |
| 5117 | dprintf(outFd, "Client: %s (cached)\n", String8(package).string()); |
| 5118 | dprintf(outFd, "%s\n", kv.second.c_str()); |
| 5119 | printedSomething = true; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5120 | } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5121 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5122 | if (!printedSomething) { |
| 5123 | dprintf(outFd, "No monitoring information to print.\n"); |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5124 | } |
| 5125 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5126 | return OK; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5127 | } |
| 5128 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5129 | // Print all events in vector `events' that came after lastPrintedEvent |
| 5130 | void printNewWatchedEvents(int outFd, |
| 5131 | const char *cameraId, |
| 5132 | const String16 &packageName, |
| 5133 | const std::vector<std::string> &events, |
| 5134 | const std::string &lastPrintedEvent) { |
| 5135 | if (events.empty()) { return; } |
| 5136 | |
| 5137 | // index of lastPrintedEvent in events. |
| 5138 | // lastPrintedIdx = events.size() if lastPrintedEvent is not in events |
| 5139 | size_t lastPrintedIdx; |
| 5140 | for (lastPrintedIdx = 0; |
| 5141 | lastPrintedIdx < events.size() && lastPrintedEvent != events[lastPrintedIdx]; |
| 5142 | lastPrintedIdx++); |
| 5143 | |
| 5144 | if (lastPrintedIdx == 0) { return; } // early exit if no new event in `events` |
| 5145 | |
| 5146 | String8 packageName8(packageName); |
| 5147 | const char *printablePackageName = packageName8.lockBuffer(packageName8.size()); |
| 5148 | |
| 5149 | // print events in chronological order (latest event last) |
| 5150 | size_t idxToPrint = lastPrintedIdx; |
| 5151 | do { |
| 5152 | idxToPrint--; |
| 5153 | dprintf(outFd, "%s:%s %s", cameraId, printablePackageName, events[idxToPrint].c_str()); |
| 5154 | } while (idxToPrint != 0); |
| 5155 | |
| 5156 | packageName8.unlockBuffer(); |
| 5157 | } |
| 5158 | |
| 5159 | // Returns true if adb shell cmd watch should be interrupted based on data in inFd. The watch |
| 5160 | // command should be interrupted if the user presses the return key, or if user loses any way to |
| 5161 | // signal interrupt. |
| 5162 | // If timeoutMs == 0, this function will always return false |
| 5163 | bool shouldInterruptWatchCommand(int inFd, int outFd, long timeoutMs) { |
| 5164 | struct timeval startTime; |
| 5165 | int startTimeError = gettimeofday(&startTime, nullptr); |
| 5166 | if (startTimeError) { |
| 5167 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 5168 | return true; |
| 5169 | } |
| 5170 | |
| 5171 | const nfds_t numFds = 1; |
| 5172 | struct pollfd pollFd = { .fd = inFd, .events = POLLIN, .revents = 0 }; |
| 5173 | |
| 5174 | struct timeval currTime; |
| 5175 | char buffer[2]; |
| 5176 | while(true) { |
| 5177 | int currTimeError = gettimeofday(&currTime, nullptr); |
| 5178 | if (currTimeError) { |
| 5179 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); |
| 5180 | return true; |
| 5181 | } |
| 5182 | |
| 5183 | long elapsedTimeMs = ((currTime.tv_sec - startTime.tv_sec) * 1000L) |
| 5184 | + ((currTime.tv_usec - startTime.tv_usec) / 1000L); |
| 5185 | int remainingTimeMs = (int) (timeoutMs - elapsedTimeMs); |
| 5186 | |
| 5187 | if (remainingTimeMs <= 0) { |
| 5188 | // No user interrupt within timeoutMs, don't interrupt watch command |
| 5189 | return false; |
| 5190 | } |
| 5191 | |
| 5192 | int numFdsUpdated = poll(&pollFd, numFds, remainingTimeMs); |
| 5193 | if (numFdsUpdated < 0) { |
| 5194 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 5195 | return true; |
| 5196 | } |
| 5197 | |
| 5198 | if (numFdsUpdated == 0) { |
| 5199 | // No user input within timeoutMs, don't interrupt watch command |
| 5200 | return false; |
| 5201 | } |
| 5202 | |
| 5203 | if (!(pollFd.revents & POLLIN)) { |
| 5204 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); |
| 5205 | return true; |
| 5206 | } |
| 5207 | |
| 5208 | ssize_t sizeRead = read(inFd, buffer, sizeof(buffer) - 1); |
| 5209 | if (sizeRead < 0) { |
| 5210 | dprintf(outFd, "Error reading user input. Exiting.\n"); |
| 5211 | return true; |
| 5212 | } |
| 5213 | |
| 5214 | if (sizeRead == 0) { |
| 5215 | // Reached end of input fd (can happen if input is piped) |
| 5216 | // User has no way to signal an interrupt, so interrupt here |
| 5217 | return true; |
| 5218 | } |
| 5219 | |
| 5220 | if (buffer[0] == '\n') { |
| 5221 | // User pressed return, interrupt watch command. |
| 5222 | return true; |
| 5223 | } |
| 5224 | } |
| 5225 | } |
| 5226 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5227 | status_t CameraService::printWatchedTagsUntilInterrupt(const Vector<String16> &args, |
| 5228 | int inFd, int outFd) { |
| 5229 | // Figure out refresh interval, if present in args |
| 5230 | long refreshTimeoutMs = 1000L; // refresh every 1s by default |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5231 | if (args.size() > 2) { |
| 5232 | size_t intervalIdx; // index of '-n' |
| 5233 | for (intervalIdx = 2; intervalIdx < args.size() && String16("-n") != args[intervalIdx]; |
| 5234 | intervalIdx++); |
| 5235 | |
| 5236 | size_t intervalValIdx = intervalIdx + 1; |
| 5237 | if (intervalValIdx < args.size()) { |
| 5238 | refreshTimeoutMs = strtol(String8(args[intervalValIdx].string()), nullptr, 10); |
| 5239 | if (errno) { return BAD_VALUE; } |
| 5240 | } |
| 5241 | } |
| 5242 | |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5243 | // Set min timeout of 10ms. This prevents edge cases in polling when timeout of 0 is passed. |
| 5244 | refreshTimeoutMs = refreshTimeoutMs < 10 ? 10 : refreshTimeoutMs; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5245 | |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5246 | dprintf(outFd, "Press return to exit...\n\n"); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5247 | std::map<String16, std::string> packageNameToLastEvent; |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5248 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5249 | while (true) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5250 | bool serviceLock = tryLock(mServiceLock); |
| 5251 | auto cameraClients = mActiveClientManager.getAll(); |
| 5252 | if (serviceLock) { mServiceLock.unlock(); } |
| 5253 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5254 | for (const auto& clientDescriptor : cameraClients) { |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5255 | Mutex::Autolock lock(mLogLock); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5256 | if (clientDescriptor == nullptr) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5257 | |
| 5258 | sp<BasicClient> client = clientDescriptor->getValue(); |
| 5259 | if (client.get() == nullptr) { continue; } |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5260 | if (!isClientWatchedLocked(client.get())) { continue; } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5261 | |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5262 | const String16 &packageName = client->getPackageName(); |
| 5263 | // This also initializes the map entries with an empty string |
| 5264 | const std::string& lastPrintedEvent = packageNameToLastEvent[packageName]; |
| 5265 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5266 | std::vector<std::string> latestEvents; |
| 5267 | client->dumpWatchedEventsToVector(latestEvents); |
| 5268 | |
| 5269 | if (!latestEvents.empty()) { |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5270 | String8 cameraId = clientDescriptor->getKey(); |
| 5271 | const char *printableCameraId = cameraId.lockBuffer(cameraId.size()); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5272 | printNewWatchedEvents(outFd, |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5273 | printableCameraId, |
| 5274 | packageName, |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5275 | latestEvents, |
| 5276 | lastPrintedEvent); |
Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5277 | packageNameToLastEvent[packageName] = latestEvents[0]; |
| 5278 | cameraId.unlockBuffer(); |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5279 | } |
| 5280 | } |
Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5281 | if (shouldInterruptWatchCommand(inFd, outFd, refreshTimeoutMs)) { |
Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5282 | break; |
| 5283 | } |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5284 | } |
| 5285 | return OK; |
| 5286 | } |
| 5287 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5288 | void CameraService::parseClientsToWatchLocked(String8 clients) { |
| 5289 | mWatchedClientPackages.clear(); |
| 5290 | |
| 5291 | const char *allSentinel = String8(kWatchAllClientsFlag).string(); |
| 5292 | |
| 5293 | char *tokenized = clients.lockBuffer(clients.size()); |
| 5294 | char *savePtr; |
| 5295 | char *nextClient = strtok_r(tokenized, ",", &savePtr); |
| 5296 | |
| 5297 | while (nextClient != nullptr) { |
| 5298 | if (strcmp(nextClient, allSentinel) == 0) { |
| 5299 | // Don't need to track any other package if 'all' is present |
| 5300 | mWatchedClientPackages.clear(); |
| 5301 | mWatchedClientPackages.emplace(kWatchAllClientsFlag); |
| 5302 | break; |
| 5303 | } |
| 5304 | |
| 5305 | // track package names |
| 5306 | mWatchedClientPackages.emplace(nextClient); |
| 5307 | nextClient = strtok_r(nullptr, ",", &savePtr); |
| 5308 | } |
| 5309 | clients.unlockBuffer(); |
| 5310 | } |
| 5311 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5312 | status_t CameraService::printHelp(int out) { |
| 5313 | return dprintf(out, "Camera service commands:\n" |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5314 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 5315 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 5316 | " 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] | 5317 | " set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" |
| 5318 | " Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" |
| 5319 | " get-rotate-and-crop returns the current override rotate-and-crop value\n" |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5320 | " set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" |
| 5321 | " Valid values 0=OFF, 1=ON for JPEG\n" |
| 5322 | " 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] | 5323 | " set-camera-mute <0/1> enable or disable camera muting\n" |
Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5324 | " 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] | 5325 | " help print this message\n"); |
| 5326 | } |
| 5327 | |
Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5328 | bool CameraService::isClientWatched(const BasicClient *client) { |
| 5329 | Mutex::Autolock lock(mLogLock); |
| 5330 | return isClientWatchedLocked(client); |
| 5331 | } |
| 5332 | |
| 5333 | bool CameraService::isClientWatchedLocked(const BasicClient *client) { |
| 5334 | return mWatchedClientPackages.find(kWatchAllClientsFlag) != mWatchedClientPackages.end() || |
| 5335 | mWatchedClientPackages.find(client->getPackageName()) != mWatchedClientPackages.end(); |
| 5336 | } |
| 5337 | |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 5338 | int32_t CameraService::updateAudioRestriction() { |
| 5339 | Mutex::Autolock lock(mServiceLock); |
| 5340 | return updateAudioRestrictionLocked(); |
| 5341 | } |
| 5342 | |
| 5343 | int32_t CameraService::updateAudioRestrictionLocked() { |
| 5344 | int32_t mode = 0; |
| 5345 | // iterate through all active client |
| 5346 | for (const auto& i : mActiveClientManager.getAll()) { |
| 5347 | const auto clientSp = i->getValue(); |
| 5348 | mode |= clientSp->getAudioRestriction(); |
| 5349 | } |
| 5350 | |
| 5351 | bool modeChanged = (mAudioRestriction != mode); |
| 5352 | mAudioRestriction = mode; |
| 5353 | if (modeChanged) { |
| 5354 | mAppOps.setCameraAudioRestriction(mode); |
| 5355 | } |
| 5356 | return mode; |
| 5357 | } |
| 5358 | |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 5359 | status_t CameraService::checkIfInjectionCameraIsPresent(const String8& externalCamId, |
| 5360 | sp<BasicClient> clientSp) { |
| 5361 | std::unique_ptr<AutoConditionLock> lock = |
| 5362 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 5363 | status_t res = NO_ERROR; |
| 5364 | if ((res = checkIfDeviceIsUsable(externalCamId)) != NO_ERROR) { |
| 5365 | ALOGW("Device %s is not usable!", externalCamId.string()); |
| 5366 | mInjectionStatusListener->notifyInjectionError( |
| 5367 | externalCamId, UNKNOWN_TRANSACTION); |
| 5368 | clientSp->notifyError( |
| 5369 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 5370 | CaptureResultExtras()); |
| 5371 | |
| 5372 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 5373 | // other clients from connecting in mServiceLockWrapper if held |
| 5374 | mServiceLock.unlock(); |
| 5375 | |
| 5376 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
| 5377 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 5378 | clientSp->disconnect(); |
| 5379 | CameraThreadState::restoreCallingIdentity(token); |
| 5380 | |
| 5381 | // Reacquire mServiceLock |
| 5382 | mServiceLock.lock(); |
| 5383 | } |
| 5384 | |
| 5385 | return res; |
| 5386 | } |
| 5387 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5388 | void CameraService::clearInjectionParameters() { |
| 5389 | { |
| 5390 | Mutex::Autolock lock(mInjectionParametersLock); |
Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 5391 | mInjectionInitPending = false; |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5392 | mInjectionInternalCamId = ""; |
| 5393 | } |
| 5394 | mInjectionExternalCamId = ""; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5395 | mInjectionStatusListener->removeListener(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5396 | } |
| 5397 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5398 | }; // namespace android |