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> |
| 31 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 32 | #include <android/hardware/ICamera.h> |
| 33 | #include <android/hardware/ICameraClient.h> |
| 34 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 35 | #include <android-base/macros.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 36 | #include <android-base/parseint.h> |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 38 | #include <binder/ActivityManager.h> |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 39 | #include <binder/AppOpsManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 41 | #include <binder/MemoryBase.h> |
| 42 | #include <binder/MemoryHeapBase.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 43 | #include <binder/PermissionController.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 44 | #include <binder/IResultReceiver.h> |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 45 | #include <binderthreadstate/CallerUtils.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 46 | #include <cutils/atomic.h> |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 47 | #include <cutils/properties.h> |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 48 | #include <cutils/misc.h> |
Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 49 | #include <gui/Surface.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 50 | #include <hardware/hardware.h> |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 51 | #include "hidl/HidlCameraService.h" |
| 52 | #include <hidl/HidlTransportSupport.h> |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 53 | #include <hwbinder/IPCThreadState.h> |
Eino-Ville Talvala | d89821e | 2016-04-20 11:23:50 -0700 | [diff] [blame] | 54 | #include <memunreachable/memunreachable.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 55 | #include <media/AudioSystem.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 56 | #include <media/IMediaHTTPService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 57 | #include <media/mediaplayer.h> |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 58 | #include <mediautils/BatteryNotifier.h> |
Steven Moreland | 886d732 | 2021-04-02 04:19:45 +0000 | [diff] [blame] | 59 | #include <processinfo/ProcessInfoService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 60 | #include <utils/Errors.h> |
| 61 | #include <utils/Log.h> |
| 62 | #include <utils/String16.h> |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 63 | #include <utils/SystemClock.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 64 | #include <utils/Trace.h> |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 65 | #include <utils/CallStack.h> |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 66 | #include <private/android_filesystem_config.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 67 | #include <system/camera_vendor_tags.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 68 | #include <system/camera_metadata.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 69 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 70 | #include <system/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 71 | |
| 72 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 73 | #include "api1/Camera2Client.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 74 | #include "api2/CameraDeviceClient.h" |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 75 | #include "utils/CameraTraces.h" |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 76 | #include "utils/TagMonitor.h" |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 77 | #include "utils/CameraThreadState.h" |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 78 | #include "utils/CameraServiceProxyWrapper.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 79 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 80 | namespace { |
| 81 | const char* kPermissionServiceName = "permission"; |
| 82 | }; // namespace anonymous |
| 83 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 84 | namespace android { |
| 85 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 86 | using base::StringPrintf; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 87 | using binder::Status; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 88 | using camera3::SessionConfigurationUtils; |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 89 | using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 90 | using hardware::ICamera; |
| 91 | using hardware::ICameraClient; |
| 92 | using hardware::ICameraServiceListener; |
| 93 | using hardware::camera::common::V1_0::CameraDeviceStatus; |
| 94 | using hardware::camera::common::V1_0::TorchModeStatus; |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 95 | using hardware::camera2::ICameraInjectionCallback; |
| 96 | using hardware::camera2::ICameraInjectionSession; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 97 | using hardware::camera2::utils::CameraIdAndSessionConfiguration; |
| 98 | using hardware::camera2::utils::ConcurrentCameraIdCombination; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 99 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 100 | // ---------------------------------------------------------------------------- |
| 101 | // Logging support -- this is for debugging only |
| 102 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 103 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 104 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 105 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 106 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 107 | |
| 108 | static void setLogLevel(int level) { |
| 109 | android_atomic_write(level, &gLogLevel); |
| 110 | } |
| 111 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 112 | // Convenience methods for constructing binder::Status objects for error returns |
| 113 | |
| 114 | #define STATUS_ERROR(errorCode, errorString) \ |
| 115 | binder::Status::fromServiceSpecificError(errorCode, \ |
| 116 | String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString)) |
| 117 | |
| 118 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ |
| 119 | binder::Status::fromServiceSpecificError(errorCode, \ |
| 120 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \ |
| 121 | __VA_ARGS__)) |
| 122 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 123 | // ---------------------------------------------------------------------------- |
| 124 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 125 | static const String16 sDumpPermission("android.permission.DUMP"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 126 | static const String16 sManageCameraPermission("android.permission.MANAGE_CAMERA"); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 127 | static const String16 sCameraPermission("android.permission.CAMERA"); |
| 128 | static const String16 sSystemCameraPermission("android.permission.SYSTEM_CAMERA"); |
| 129 | static const String16 |
| 130 | sCameraSendSystemEventsPermission("android.permission.CAMERA_SEND_SYSTEM_EVENTS"); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 131 | static const String16 sCameraOpenCloseListenerPermission( |
| 132 | "android.permission.CAMERA_OPEN_CLOSE_LISTENER"); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 133 | static const String16 |
| 134 | sCameraInjectExternalCameraPermission("android.permission.CAMERA_INJECT_EXTERNAL_CAMERA"); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 135 | const char *sFileName = "lastOpenSessionDumpFile"; |
Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame] | 136 | static constexpr int32_t kVendorClientScore = resource_policy::PERCEPTIBLE_APP_ADJ; |
| 137 | static constexpr int32_t kVendorClientState = ActivityManager::PROCESS_STATE_PERSISTENT_UI; |
| 138 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 139 | const String8 CameraService::kOfflineDevice("offline-"); |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 140 | |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 141 | // Set to keep track of logged service error events. |
| 142 | static std::set<String8> sServiceErrorEventSet; |
| 143 | |
Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 144 | CameraService::CameraService() : |
| 145 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 146 | mNumberOfCameras(0), |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 147 | mNumberOfCamerasWithoutSystemCamera(0), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 148 | mSoundRef(0), mInitialized(false), |
| 149 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 150 | ALOGI("CameraService started (pid=%d)", getpid()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 151 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 152 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 153 | if (mMemFd == -1) { |
| 154 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 155 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 158 | void CameraService::onFirstRef() |
| 159 | { |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 160 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 161 | ALOGI("CameraService process starting"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 162 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 163 | BnCameraService::onFirstRef(); |
| 164 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 165 | // Update battery life tracking if service is restarting |
| 166 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 167 | notifier.noteResetCamera(); |
| 168 | notifier.noteResetFlashlight(); |
| 169 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 170 | status_t res = INVALID_OPERATION; |
Eino-Ville Talvala | 9cbbc83 | 2017-01-23 15:39:53 -0800 | [diff] [blame] | 171 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 172 | res = enumerateProviders(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 173 | if (res == OK) { |
| 174 | mInitialized = true; |
| 175 | } |
| 176 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 177 | mUidPolicy = new UidPolicy(this); |
| 178 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 179 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this); |
| 180 | mSensorPrivacyPolicy->registerSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 181 | mInjectionStatusListener = new InjectionStatusListener(this); |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 182 | mAppOps.setCameraAudioRestriction(mAudioRestriction); |
Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 183 | sp<HidlCameraService> hcs = HidlCameraService::getInstance(this); |
| 184 | if (hcs->registerAsService() != android::OK) { |
| 185 | ALOGE("%s: Failed to register default android.frameworks.cameraservice.service@1.0", |
| 186 | __FUNCTION__); |
| 187 | } |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 188 | |
| 189 | // This needs to be last call in this function, so that it's as close to |
| 190 | // ServiceManager::addService() as possible. |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 191 | CameraServiceProxyWrapper::pingCameraServiceProxy(); |
Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 192 | ALOGI("CameraService pinged cameraservice proxy"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 195 | status_t CameraService::enumerateProviders() { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 196 | status_t res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 197 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 198 | std::vector<std::string> deviceIds; |
| 199 | { |
| 200 | Mutex::Autolock l(mServiceLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 201 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 202 | if (nullptr == mCameraProviderManager.get()) { |
| 203 | mCameraProviderManager = new CameraProviderManager(); |
| 204 | res = mCameraProviderManager->initialize(this); |
| 205 | if (res != OK) { |
| 206 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", |
| 207 | __FUNCTION__, strerror(-res), res); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 208 | logServiceError(String8::format("Unable to initialize camera provider manager"), |
| 209 | ERROR_DISCONNECTED); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 210 | return res; |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 214 | |
| 215 | // Setup vendor tags before we call get_camera_info the first time |
| 216 | // because HAL might need to setup static vendor keys in get_camera_info |
| 217 | // TODO: maybe put this into CameraProviderManager::initialize()? |
| 218 | mCameraProviderManager->setUpVendorTags(); |
| 219 | |
| 220 | if (nullptr == mFlashlight.get()) { |
| 221 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 224 | res = mFlashlight->findFlashUnits(); |
| 225 | if (res != OK) { |
| 226 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); |
| 227 | } |
| 228 | |
| 229 | deviceIds = mCameraProviderManager->getCameraDeviceIds(); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | for (auto& cameraId : deviceIds) { |
| 234 | String8 id8 = String8(cameraId.c_str()); |
Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 235 | if (getCameraState(id8) == nullptr) { |
| 236 | onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT); |
| 237 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 240 | //Derive primary rear/front cameras, and filter their charactierstics. |
| 241 | //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] | 242 | if (SessionConfigurationUtils::IS_PERF_CLASS) { |
| 243 | filterSPerfClassCharacteristics(); |
| 244 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 245 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 246 | return OK; |
| 247 | } |
| 248 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 249 | void CameraService::broadcastTorchModeStatus(const String8& cameraId, TorchModeStatus status, |
| 250 | SystemCameraKind systemCameraKind) { |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 251 | Mutex::Autolock lock(mStatusListenerLock); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 252 | for (auto& i : mListenerList) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 253 | if (shouldSkipStatusUpdates(systemCameraKind, i->isVendorListener(), i->getListenerPid(), |
| 254 | i->getListenerUid())) { |
| 255 | ALOGV("Skipping torch callback for system-only camera device %s", |
| 256 | cameraId.c_str()); |
| 257 | continue; |
| 258 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 259 | i->getListener()->onTorchStatusChanged(mapToInterface(status), String16{cameraId}); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 263 | CameraService::~CameraService() { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 264 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 265 | mUidPolicy->unregisterSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 266 | mSensorPrivacyPolicy->unregisterSelf(); |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 267 | mInjectionStatusListener->removeListener(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 270 | void CameraService::onNewProviderRegistered() { |
| 271 | enumerateProviders(); |
| 272 | } |
| 273 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 274 | void CameraService::filterAPI1SystemCameraLocked( |
| 275 | const std::vector<std::string> &normalDeviceIds) { |
| 276 | mNormalDeviceIdsWithoutSystemCamera.clear(); |
| 277 | for (auto &deviceId : normalDeviceIds) { |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 278 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 279 | if (getSystemCameraKind(String8(deviceId.c_str()), &deviceKind) != OK) { |
| 280 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, deviceId.c_str()); |
| 281 | continue; |
| 282 | } |
| 283 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 284 | // All system camera ids will necessarily come after public camera |
| 285 | // device ids as per the HAL interface contract. |
| 286 | break; |
| 287 | } |
| 288 | mNormalDeviceIdsWithoutSystemCamera.push_back(deviceId); |
| 289 | } |
| 290 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, |
| 291 | mNormalDeviceIdsWithoutSystemCamera.size()); |
| 292 | } |
| 293 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 294 | status_t CameraService::getSystemCameraKind(const String8& cameraId, SystemCameraKind *kind) const { |
| 295 | auto state = getCameraState(cameraId); |
| 296 | if (state != nullptr) { |
| 297 | *kind = state->getSystemCameraKind(); |
| 298 | return OK; |
| 299 | } |
| 300 | // Hidden physical camera ids won't have CameraState |
| 301 | return mCameraProviderManager->getSystemCameraKind(cameraId.c_str(), kind); |
| 302 | } |
| 303 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 304 | void CameraService::updateCameraNumAndIds() { |
| 305 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 306 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); |
| 307 | // Excludes hidden secure cameras |
| 308 | mNumberOfCameras = |
| 309 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; |
| 310 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 311 | mNormalDeviceIds = |
| 312 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 313 | filterAPI1SystemCameraLocked(mNormalDeviceIds); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 316 | void CameraService::filterSPerfClassCharacteristics() { |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 317 | // To claim to be S Performance primary cameras, the cameras must be |
| 318 | // backward compatible. So performance class primary camera Ids must be API1 |
| 319 | // compatible. |
| 320 | bool firstRearCameraSeen = false, firstFrontCameraSeen = false; |
| 321 | for (const auto& cameraId : mNormalDeviceIdsWithoutSystemCamera) { |
| 322 | int facing = -1; |
| 323 | int orientation = 0; |
| 324 | String8 cameraId8(cameraId.c_str()); |
| 325 | getDeviceVersion(cameraId8, /*out*/&facing, /*out*/&orientation); |
| 326 | if (facing == -1) { |
| 327 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.c_str()); |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | if ((facing == hardware::CAMERA_FACING_BACK && !firstRearCameraSeen) || |
| 332 | (facing == hardware::CAMERA_FACING_FRONT && !firstFrontCameraSeen)) { |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 333 | status_t res = mCameraProviderManager->filterSmallJpegSizes(cameraId); |
| 334 | if (res == OK) { |
| 335 | mPerfClassPrimaryCameraIds.insert(cameraId); |
| 336 | } else { |
| 337 | ALOGE("%s: Failed to filter small JPEG sizes for performance class primary " |
| 338 | "camera %s: %s(%d)", __FUNCTION__, cameraId.c_str(), strerror(-res), res); |
| 339 | break; |
| 340 | } |
Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 341 | |
| 342 | if (facing == hardware::CAMERA_FACING_BACK) { |
| 343 | firstRearCameraSeen = true; |
| 344 | } |
| 345 | if (facing == hardware::CAMERA_FACING_FRONT) { |
| 346 | firstFrontCameraSeen = true; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (firstRearCameraSeen && firstFrontCameraSeen) { |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 356 | void CameraService::addStates(const String8 id) { |
| 357 | std::string cameraId(id.c_str()); |
| 358 | hardware::camera::common::V1_0::CameraResourceCost cost; |
| 359 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 360 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 361 | if (res != OK) { |
| 362 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); |
| 363 | return; |
| 364 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 365 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); |
| 366 | if (res != OK) { |
| 367 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); |
| 368 | return; |
| 369 | } |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 370 | std::set<String8> conflicting; |
| 371 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { |
| 372 | conflicting.emplace(String8(cost.conflictingDevices[i].c_str())); |
| 373 | } |
| 374 | |
| 375 | { |
| 376 | Mutex::Autolock lock(mCameraStatesLock); |
| 377 | mCameraStates.emplace(id, std::make_shared<CameraState>(id, cost.resourceCost, |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 378 | conflicting, deviceKind)); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 382 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 383 | mTorchStatusMap.add(id, TorchModeStatus::AVAILABLE_OFF); |
Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 384 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 385 | broadcastTorchModeStatus(id, TorchModeStatus::AVAILABLE_OFF, deviceKind); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 386 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 387 | |
| 388 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 389 | logDeviceAdded(id, "Device added"); |
| 390 | } |
| 391 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 392 | void CameraService::removeStates(const String8 id) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 393 | updateCameraNumAndIds(); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 394 | if (mFlashlight->hasFlashUnit(id)) { |
Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 395 | Mutex::Autolock al(mTorchStatusMutex); |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 396 | mTorchStatusMap.removeItem(id); |
| 397 | } |
| 398 | |
| 399 | { |
| 400 | Mutex::Autolock lock(mCameraStatesLock); |
| 401 | mCameraStates.erase(id); |
| 402 | } |
| 403 | } |
| 404 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 405 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 406 | CameraDeviceStatus newHalStatus) { |
| 407 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, |
| 408 | id.string(), newHalStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 409 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 410 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 411 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 412 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 413 | |
| 414 | if (state == nullptr) { |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 415 | if (newStatus == StatusInternal::PRESENT) { |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 416 | ALOGI("%s: Unknown camera ID %s, a new camera is added", |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 417 | __FUNCTION__, id.string()); |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 418 | |
| 419 | // First add as absent to make sure clients are notified below |
| 420 | addStates(id); |
| 421 | |
| 422 | updateStatus(newStatus, id); |
Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 423 | } else { |
| 424 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string()); |
| 425 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 426 | return; |
| 427 | } |
| 428 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 429 | StatusInternal oldStatus = state->getStatus(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 430 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 431 | if (oldStatus == newStatus) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 432 | 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] | 433 | return; |
| 434 | } |
| 435 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 436 | if (newStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 437 | logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 438 | newStatus)); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 439 | |
| 440 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 441 | // to this device until the status changes |
| 442 | updateStatus(StatusInternal::NOT_PRESENT, id); |
| 443 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 444 | sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 445 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 446 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 447 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 448 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 449 | // Remove cached shim parameters |
| 450 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 451 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 452 | // Remove online as well as offline client from the list of active clients, |
| 453 | // if they are present |
| 454 | clientToDisconnectOnline = removeClientLocked(id); |
| 455 | clientToDisconnectOffline = removeClientLocked(kOfflineDevice + id); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 456 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 457 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 458 | disconnectClient(id, clientToDisconnectOnline); |
| 459 | disconnectClient(kOfflineDevice + id, clientToDisconnectOffline); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 460 | |
Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 461 | removeStates(id); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 462 | } else { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 463 | if (oldStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 464 | logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus, |
| 465 | newStatus)); |
| 466 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 467 | updateStatus(newStatus, id); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 468 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 469 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 470 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 471 | void CameraService::onDeviceStatusChanged(const String8& id, |
| 472 | const String8& physicalId, |
| 473 | CameraDeviceStatus newHalStatus) { |
| 474 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", |
| 475 | __FUNCTION__, id.string(), physicalId.string(), newHalStatus); |
| 476 | |
| 477 | StatusInternal newStatus = mapToInternal(newHalStatus); |
| 478 | |
| 479 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 480 | |
| 481 | if (state == nullptr) { |
| 482 | ALOGE("%s: Physical camera id %s status change on a non-present ID %s", |
| 483 | __FUNCTION__, id.string(), physicalId.string()); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | StatusInternal logicalCameraStatus = state->getStatus(); |
| 488 | if (logicalCameraStatus != StatusInternal::PRESENT && |
| 489 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 490 | ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d", |
| 491 | __FUNCTION__, physicalId.string(), newHalStatus, logicalCameraStatus); |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | bool updated = false; |
| 496 | if (newStatus == StatusInternal::PRESENT) { |
| 497 | updated = state->removeUnavailablePhysicalId(physicalId); |
| 498 | } else { |
| 499 | updated = state->addUnavailablePhysicalId(physicalId); |
| 500 | } |
| 501 | |
| 502 | if (updated) { |
Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 503 | String8 idCombo = id + " : " + physicalId; |
| 504 | if (newStatus == StatusInternal::PRESENT) { |
| 505 | logDeviceAdded(idCombo, |
| 506 | String8::format("Device status changed to %d", newStatus)); |
| 507 | } else { |
| 508 | logDeviceRemoved(idCombo, |
| 509 | String8::format("Device status changed to %d", newStatus)); |
| 510 | } |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 511 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 512 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 513 | if (getSystemCameraKind(id, &deviceKind) != OK) { |
| 514 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.string()); |
| 515 | return; |
| 516 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 517 | String16 id16(id), physicalId16(physicalId); |
| 518 | Mutex::Autolock lock(mStatusListenerLock); |
| 519 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 520 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 521 | listener->getListenerPid(), listener->getListenerUid())) { |
| 522 | ALOGV("Skipping discovery callback for system-only camera device %s", |
| 523 | id.c_str()); |
| 524 | continue; |
| 525 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 526 | listener->getListener()->onPhysicalCameraStatusChanged(mapToInterface(newStatus), |
| 527 | id16, physicalId16); |
| 528 | } |
| 529 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 532 | void CameraService::disconnectClient(const String8& id, sp<BasicClient> clientToDisconnect) { |
| 533 | if (clientToDisconnect.get() != nullptr) { |
| 534 | ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL", |
| 535 | __FUNCTION__, id.string()); |
| 536 | // Notify the client of disconnection |
| 537 | clientToDisconnect->notifyError( |
| 538 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 539 | CaptureResultExtras{}); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 540 | clientToDisconnect->disconnect(); |
| 541 | } |
| 542 | } |
| 543 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 544 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 545 | TorchModeStatus newStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 546 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 547 | status_t res = getSystemCameraKind(cameraId, &systemCameraKind); |
| 548 | if (res != OK) { |
| 549 | ALOGE("%s: Could not get system camera kind for camera id %s", __FUNCTION__, |
| 550 | cameraId.string()); |
| 551 | return; |
| 552 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 553 | Mutex::Autolock al(mTorchStatusMutex); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 554 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 557 | void CameraService::onTorchStatusChangedLocked(const String8& cameraId, |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 558 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 559 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
| 560 | __FUNCTION__, cameraId.string(), newStatus); |
| 561 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 562 | TorchModeStatus status; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 563 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 564 | if (res) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 565 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", |
| 566 | __FUNCTION__, cameraId.string(), strerror(-res), res); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 567 | return; |
| 568 | } |
| 569 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 570 | return; |
| 571 | } |
| 572 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 573 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 574 | if (res) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 575 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, |
| 576 | (uint32_t)newStatus, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 577 | return; |
| 578 | } |
| 579 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 580 | { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 581 | // Update battery life logging for flashlight |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 582 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 583 | auto iter = mTorchUidMap.find(cameraId); |
| 584 | if (iter != mTorchUidMap.end()) { |
| 585 | int oldUid = iter->second.second; |
| 586 | int newUid = iter->second.first; |
| 587 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 588 | if (oldUid != newUid) { |
| 589 | // 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] | 590 | if (status == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 591 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 592 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 593 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 594 | notifier.noteFlashlightOn(cameraId, newUid); |
| 595 | } |
| 596 | iter->second.second = newUid; |
| 597 | } else { |
| 598 | // If the UID has not changed, log the status |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 599 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 600 | notifier.noteFlashlightOn(cameraId, oldUid); |
| 601 | } else { |
| 602 | notifier.noteFlashlightOff(cameraId, oldUid); |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | } |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 607 | broadcastTorchModeStatus(cameraId, newStatus, systemCameraKind); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 610 | static bool hasPermissionsForSystemCamera(int callingPid, int callingUid) { |
| 611 | return checkPermission(sSystemCameraPermission, callingPid, callingUid) && |
| 612 | checkPermission(sCameraPermission, callingPid, callingUid); |
| 613 | } |
| 614 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 615 | Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 616 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 617 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 618 | bool hasSystemCameraPermissions = |
| 619 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), |
| 620 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 621 | switch (type) { |
| 622 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 623 | if (hasSystemCameraPermissions) { |
| 624 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); |
| 625 | } else { |
| 626 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); |
| 627 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 628 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 629 | case CAMERA_TYPE_ALL: |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 630 | if (hasSystemCameraPermissions) { |
| 631 | *numCameras = mNumberOfCameras; |
| 632 | } else { |
| 633 | *numCameras = mNumberOfCamerasWithoutSystemCamera; |
| 634 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 635 | break; |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 636 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 637 | ALOGW("%s: Unknown camera type %d", |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 638 | __FUNCTION__, type); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 639 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 640 | "Unknown camera type %d", type); |
Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 641 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 642 | return Status::ok(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 645 | Status CameraService::getCameraInfo(int cameraId, |
| 646 | CameraInfo* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 647 | ATRACE_CALL(); |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 648 | Mutex::Autolock l(mServiceLock); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 649 | std::string cameraIdStr = cameraIdIntToStrLocked(cameraId); |
| 650 | if (shouldRejectSystemCameraConnection(String8(cameraIdStr.c_str()))) { |
| 651 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 652 | "characteristics for system only device %s: ", cameraIdStr.c_str()); |
| 653 | } |
Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 654 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 655 | if (!mInitialized) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 656 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 657 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 658 | "Camera subsystem is not available"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 659 | } |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 660 | bool hasSystemCameraPermissions = |
| 661 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), |
| 662 | CameraThreadState::getCallingUid()); |
| 663 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; |
| 664 | if (hasSystemCameraPermissions) { |
| 665 | cameraIdBound = mNumberOfCameras; |
| 666 | } |
| 667 | if (cameraId < 0 || cameraId >= cameraIdBound) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 668 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 669 | "CameraId is not valid"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 672 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 673 | status_t err = mCameraProviderManager->getCameraInfo( |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 674 | cameraIdStr.c_str(), cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 675 | if (err != OK) { |
| 676 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 677 | "Error retrieving camera info from device %d: %s (%d)", cameraId, |
| 678 | strerror(-err), err); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 679 | logServiceError(String8::format("Error retrieving camera info from device %d",cameraId), |
| 680 | ERROR_INVALID_OPERATION); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 681 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 682 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 683 | return ret; |
| 684 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 685 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 686 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt) { |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 687 | const std::vector<std::string> *deviceIds = &mNormalDeviceIdsWithoutSystemCamera; |
| 688 | auto callingPid = CameraThreadState::getCallingPid(); |
| 689 | auto callingUid = CameraThreadState::getCallingUid(); |
| 690 | if (checkPermission(sSystemCameraPermission, callingPid, callingUid) || |
| 691 | getpid() == callingPid) { |
| 692 | deviceIds = &mNormalDeviceIds; |
| 693 | } |
| 694 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(deviceIds->size())) { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 695 | ALOGE("%s: input id %d invalid: valid range (0, %zu)", |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 696 | __FUNCTION__, cameraIdInt, deviceIds->size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 697 | return std::string{}; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 698 | } |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 699 | |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 700 | return (*deviceIds)[cameraIdInt]; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | String8 CameraService::cameraIdIntToStr(int cameraIdInt) { |
| 704 | Mutex::Autolock lock(mServiceLock); |
| 705 | return String8(cameraIdIntToStrLocked(cameraIdInt).c_str()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | Status CameraService::getCameraCharacteristics(const String16& cameraId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 709 | int targetSdkVersion, CameraMetadata* cameraInfo) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 710 | ATRACE_CALL(); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 711 | if (!cameraInfo) { |
| 712 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 713 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 716 | if (!mInitialized) { |
| 717 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 718 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 719 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 720 | "Camera subsystem is not available");; |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 723 | if (shouldRejectSystemCameraConnection(String8(cameraId))) { |
| 724 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" |
| 725 | "characteristics for system only device %s: ", String8(cameraId).string()); |
| 726 | } |
| 727 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 728 | Status ret{}; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 729 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 730 | |
| 731 | std::string cameraIdStr = String8(cameraId).string(); |
| 732 | bool overrideForPerfClass = |
| 733 | SessionConfigurationUtils::targetPerfClassPrimaryCamera(mPerfClassPrimaryCameraIds, |
| 734 | cameraIdStr, targetSdkVersion); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 735 | status_t res = mCameraProviderManager->getCameraCharacteristics( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 736 | cameraIdStr, overrideForPerfClass, cameraInfo); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 737 | if (res != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 738 | if (res == NAME_NOT_FOUND) { |
| 739 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " |
| 740 | "characteristics for unknown device %s: %s (%d)", String8(cameraId).string(), |
| 741 | strerror(-res), res); |
| 742 | } else { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 743 | logServiceError(String8::format("Unable to retrieve camera characteristics for " |
| 744 | "device %s.", String8(cameraId).string()),ERROR_INVALID_OPERATION); |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 745 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " |
| 746 | "characteristics for device %s: %s (%d)", String8(cameraId).string(), |
| 747 | strerror(-res), res); |
| 748 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 749 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 750 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 751 | if (getSystemCameraKind(String8(cameraId), &deviceKind) != OK) { |
| 752 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, String8(cameraId).string()); |
| 753 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera kind " |
| 754 | "for device %s", String8(cameraId).string()); |
| 755 | } |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 756 | int callingPid = CameraThreadState::getCallingPid(); |
| 757 | int callingUid = CameraThreadState::getCallingUid(); |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 758 | std::vector<int32_t> tagsRemoved; |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 759 | // If it's not calling from cameraserver, check the permission only if |
| 760 | // android.permission.CAMERA is required. If android.permission.SYSTEM_CAMERA was needed, |
| 761 | // it would've already been checked in shouldRejectSystemCameraConnection. |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 762 | if ((callingPid != getpid()) && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 763 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 764 | !checkPermission(sCameraPermission, callingPid, callingUid)) { |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 765 | res = cameraInfo->removePermissionEntries( |
| 766 | mCameraProviderManager->getProviderTagIdLocked(String8(cameraId).string()), |
| 767 | &tagsRemoved); |
| 768 | if (res != OK) { |
| 769 | cameraInfo->clear(); |
| 770 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to remove camera" |
| 771 | " characteristics needing camera permission for device %s: %s (%d)", |
| 772 | String8(cameraId).string(), strerror(-res), res); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | if (!tagsRemoved.empty()) { |
| 777 | res = cameraInfo->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, |
| 778 | tagsRemoved.data(), tagsRemoved.size()); |
| 779 | if (res != OK) { |
| 780 | cameraInfo->clear(); |
| 781 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to insert camera " |
| 782 | "keys needing permission for device %s: %s (%d)", String8(cameraId).string(), |
| 783 | strerror(-res), res); |
| 784 | } |
| 785 | } |
| 786 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 787 | return ret; |
| 788 | } |
| 789 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 790 | String8 CameraService::getFormattedCurrentTime() { |
| 791 | time_t now = time(nullptr); |
| 792 | char formattedTime[64]; |
| 793 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
| 794 | return String8(formattedTime); |
| 795 | } |
| 796 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 797 | Status CameraService::getCameraVendorTagDescriptor( |
| 798 | /*out*/ |
| 799 | hardware::camera2::params::VendorTagDescriptor* desc) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 800 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 801 | if (!mInitialized) { |
| 802 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 803 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 804 | } |
Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 805 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 806 | if (globalDescriptor != nullptr) { |
| 807 | *desc = *(globalDescriptor.get()); |
| 808 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 809 | return Status::ok(); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 812 | Status CameraService::getCameraVendorTagCache( |
| 813 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { |
| 814 | ATRACE_CALL(); |
| 815 | if (!mInitialized) { |
| 816 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 817 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 818 | "Camera subsystem not available"); |
| 819 | } |
| 820 | sp<VendorTagDescriptorCache> globalCache = |
| 821 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 822 | if (globalCache != nullptr) { |
| 823 | *cache = *(globalCache.get()); |
| 824 | } |
| 825 | return Status::ok(); |
| 826 | } |
| 827 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 828 | void CameraService::clearCachedVariables() { |
| 829 | BasicClient::BasicClient::sCameraService = nullptr; |
| 830 | } |
| 831 | |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 832 | int CameraService::getDeviceVersion(const String8& cameraId, int* facing, int* orientation) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 833 | ATRACE_CALL(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 834 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 835 | int deviceVersion = 0; |
| 836 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 837 | status_t res; |
| 838 | hardware::hidl_version maxVersion{0,0}; |
| 839 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(), |
| 840 | &maxVersion); |
| 841 | if (res != OK) return -1; |
| 842 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 843 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 844 | hardware::CameraInfo info; |
| 845 | if (facing) { |
| 846 | res = mCameraProviderManager->getCameraInfo(cameraId.string(), &info); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 847 | if (res != OK) return -1; |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 848 | *facing = info.facing; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 849 | if (orientation) { |
| 850 | *orientation = info.orientation; |
| 851 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 852 | } |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 853 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 854 | return deviceVersion; |
| 855 | } |
| 856 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 857 | Status CameraService::filterGetInfoErrorCode(status_t err) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 858 | switch(err) { |
| 859 | case NO_ERROR: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 860 | return Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 861 | case BAD_VALUE: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 862 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 863 | "CameraId is not valid for HAL module"); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 864 | case NO_INIT: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 865 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 866 | "Camera device not available"); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 867 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 868 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 869 | "Camera HAL encountered error %d: %s", |
| 870 | err, strerror(-err)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 871 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 874 | Status CameraService::makeClient(const sp<CameraService>& cameraService, |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 875 | const sp<IInterface>& cameraCb, const String16& packageName, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 876 | const std::optional<String16>& featureId, const String8& cameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 877 | int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 878 | int servicePid, int deviceVersion, apiLevel effectiveApiLevel, bool overrideForPerfClass, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 879 | /*out*/sp<BasicClient>* client) { |
| 880 | |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 881 | // Create CameraClient based on device version reported by the HAL. |
| 882 | switch(deviceVersion) { |
| 883 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 884 | ALOGE("Camera using old HAL version: %d", deviceVersion); |
| 885 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, |
| 886 | "Camera device \"%s\" HAL version %d no longer supported", |
| 887 | cameraId.string(), deviceVersion); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 888 | break; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 889 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 890 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 891 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 892 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 893 | case CAMERA_DEVICE_API_VERSION_3_4: |
| 894 | case CAMERA_DEVICE_API_VERSION_3_5: |
| 895 | case CAMERA_DEVICE_API_VERSION_3_6: |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 896 | case CAMERA_DEVICE_API_VERSION_3_7: |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 897 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 898 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 899 | *client = new Camera2Client(cameraService, tmp, packageName, featureId, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 900 | cameraId, api1CameraId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 901 | facing, sensorOrientation, clientPid, clientUid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 902 | servicePid, overrideForPerfClass); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 903 | } else { // Camera2 API route |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 904 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = |
| 905 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 906 | *client = new CameraDeviceClient(cameraService, tmp, packageName, featureId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 907 | cameraId, facing, sensorOrientation, clientPid, clientUid, servicePid, |
| 908 | overrideForPerfClass); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 909 | } |
| 910 | break; |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 911 | default: |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 912 | // Should not be reachable |
| 913 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 914 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 915 | "Camera device \"%s\" has unknown HAL version %d", |
| 916 | cameraId.string(), deviceVersion); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 917 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 918 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 919 | } |
| 920 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 921 | String8 CameraService::toString(std::set<userid_t> intSet) { |
| 922 | String8 s(""); |
| 923 | bool first = true; |
| 924 | for (userid_t i : intSet) { |
| 925 | if (first) { |
| 926 | s.appendFormat("%d", i); |
| 927 | first = false; |
| 928 | } else { |
| 929 | s.appendFormat(", %d", i); |
| 930 | } |
| 931 | } |
| 932 | return s; |
| 933 | } |
| 934 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 935 | int32_t CameraService::mapToInterface(TorchModeStatus status) { |
| 936 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 937 | switch (status) { |
| 938 | case TorchModeStatus::NOT_AVAILABLE: |
| 939 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 940 | break; |
| 941 | case TorchModeStatus::AVAILABLE_OFF: |
| 942 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 943 | break; |
| 944 | case TorchModeStatus::AVAILABLE_ON: |
| 945 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
| 946 | break; |
| 947 | default: |
| 948 | ALOGW("Unknown new flash status: %d", status); |
| 949 | } |
| 950 | return serviceStatus; |
| 951 | } |
| 952 | |
| 953 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { |
| 954 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; |
| 955 | switch (status) { |
| 956 | case CameraDeviceStatus::NOT_PRESENT: |
| 957 | serviceStatus = StatusInternal::NOT_PRESENT; |
| 958 | break; |
| 959 | case CameraDeviceStatus::PRESENT: |
| 960 | serviceStatus = StatusInternal::PRESENT; |
| 961 | break; |
| 962 | case CameraDeviceStatus::ENUMERATING: |
| 963 | serviceStatus = StatusInternal::ENUMERATING; |
| 964 | break; |
| 965 | default: |
| 966 | ALOGW("Unknown new HAL device status: %d", status); |
| 967 | } |
| 968 | return serviceStatus; |
| 969 | } |
| 970 | |
| 971 | int32_t CameraService::mapToInterface(StatusInternal status) { |
| 972 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 973 | switch (status) { |
| 974 | case StatusInternal::NOT_PRESENT: |
| 975 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; |
| 976 | break; |
| 977 | case StatusInternal::PRESENT: |
| 978 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; |
| 979 | break; |
| 980 | case StatusInternal::ENUMERATING: |
| 981 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; |
| 982 | break; |
| 983 | case StatusInternal::NOT_AVAILABLE: |
| 984 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; |
| 985 | break; |
| 986 | case StatusInternal::UNKNOWN: |
| 987 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; |
| 988 | break; |
| 989 | default: |
| 990 | ALOGW("Unknown new internal device status: %d", status); |
| 991 | } |
| 992 | return serviceStatus; |
| 993 | } |
| 994 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 995 | Status CameraService::initializeShimMetadata(int cameraId) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 996 | int uid = CameraThreadState::getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 997 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 998 | String16 internalPackageName("cameraserver"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 999 | String8 id = String8::format("%d", cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1000 | Status ret = Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1001 | sp<Client> tmp = nullptr; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1002 | if (!(ret = connectHelper<ICameraClient,Client>( |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1003 | sp<ICameraClient>{nullptr}, id, cameraId, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1004 | internalPackageName, {}, uid, USE_CALLING_PID, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1005 | API_1, /*shimUpdateOnly*/ true, /*oomScoreOffset*/ 0, |
| 1006 | /*targetSdkVersion*/ __ANDROID_API_FUTURE__, /*out*/ tmp) |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1007 | ).isOk()) { |
| 1008 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string()); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1009 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1010 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1013 | Status CameraService::getLegacyParametersLazy(int cameraId, |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1014 | /*out*/ |
| 1015 | CameraParameters* parameters) { |
| 1016 | |
| 1017 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 1018 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1019 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1020 | |
| 1021 | if (parameters == NULL) { |
| 1022 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1023 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1026 | String8 id = String8::format("%d", cameraId); |
| 1027 | |
| 1028 | // Check if we already have parameters |
| 1029 | { |
| 1030 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1031 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1032 | auto cameraState = getCameraState(id); |
| 1033 | if (cameraState == nullptr) { |
| 1034 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1035 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1036 | "Invalid camera ID: %s", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1037 | } |
| 1038 | CameraParameters p = cameraState->getShimParams(); |
| 1039 | if (!p.isEmpty()) { |
| 1040 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1041 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1045 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1046 | ret = initializeShimMetadata(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1047 | CameraThreadState::restoreCallingIdentity(token); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1048 | if (!ret.isOk()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1049 | // Error already logged by callee |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
| 1053 | // Check for parameters again |
| 1054 | { |
| 1055 | // Scope for service lock |
| 1056 | Mutex::Autolock lock(mServiceLock); |
| 1057 | auto cameraState = getCameraState(id); |
| 1058 | if (cameraState == nullptr) { |
| 1059 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1060 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1061 | "Invalid camera ID: %s", id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1062 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1063 | CameraParameters p = cameraState->getShimParams(); |
| 1064 | if (!p.isEmpty()) { |
| 1065 | *parameters = p; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1066 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1070 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 1071 | __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1072 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1075 | // Can camera service trust the caller based on the calling UID? |
| 1076 | static bool isTrustedCallingUid(uid_t uid) { |
| 1077 | switch (uid) { |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1078 | case AID_MEDIA: // mediaserver |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1079 | case AID_CAMERASERVER: // cameraserver |
Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1080 | case AID_RADIO: // telephony |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1081 | return true; |
| 1082 | default: |
| 1083 | return false; |
| 1084 | } |
| 1085 | } |
| 1086 | |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 1087 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { |
| 1088 | PermissionController pc; |
| 1089 | uid = pc.getPackageUid(packageName, 0); |
| 1090 | if (uid <= 0) { |
| 1091 | ALOGE("Unknown package: '%s'", String8(packageName).string()); |
| 1092 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).string()); |
| 1093 | return BAD_VALUE; |
| 1094 | } |
| 1095 | |
| 1096 | if (userId < 0) { |
| 1097 | ALOGE("Invalid user: %d", userId); |
| 1098 | dprintf(err, "Invalid user: %d\n", userId); |
| 1099 | return BAD_VALUE; |
| 1100 | } |
| 1101 | |
| 1102 | uid = multiuser_get_uid(userId, uid); |
| 1103 | return NO_ERROR; |
| 1104 | } |
| 1105 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1106 | Status CameraService::validateConnectLocked(const String8& cameraId, |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1107 | const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, |
| 1108 | /*out*/int& originalClientPid) const { |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1109 | |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1110 | #ifdef __BRILLO__ |
| 1111 | UNUSED(clientName8); |
| 1112 | UNUSED(clientUid); |
| 1113 | UNUSED(clientPid); |
| 1114 | UNUSED(originalClientPid); |
| 1115 | #else |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1116 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, |
| 1117 | originalClientPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1118 | if (!allowed.isOk()) { |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1119 | return allowed; |
| 1120 | } |
Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1121 | #endif // __BRILLO__ |
Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1122 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1123 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1124 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1125 | if (!mInitialized) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1126 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 1127 | callingPid); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1128 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1129 | "No camera HAL module available to open camera device \"%s\"", cameraId.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1132 | if (getCameraState(cameraId) == nullptr) { |
| 1133 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1134 | cameraId.string()); |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1135 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1136 | "No camera device with ID \"%s\" available", cameraId.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1139 | status_t err = checkIfDeviceIsUsable(cameraId); |
| 1140 | if (err != NO_ERROR) { |
| 1141 | switch(err) { |
| 1142 | case -ENODEV: |
| 1143 | case -EBUSY: |
| 1144 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1145 | "No camera device with ID \"%s\" currently available", cameraId.string()); |
| 1146 | default: |
| 1147 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1148 | "Unknown error connecting to ID \"%s\"", cameraId.string()); |
| 1149 | } |
| 1150 | } |
| 1151 | return Status::ok(); |
Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1154 | Status CameraService::validateClientPermissionsLocked(const String8& cameraId, |
Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1155 | const String8& clientName8, int& clientUid, int& clientPid, |
| 1156 | /*out*/int& originalClientPid) const { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1157 | int callingPid = CameraThreadState::getCallingPid(); |
| 1158 | int callingUid = CameraThreadState::getCallingUid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1159 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1160 | // Check if we can trust clientUid |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1161 | if (clientUid == USE_CALLING_UID) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1162 | clientUid = callingUid; |
| 1163 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1164 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1165 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1166 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1167 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1168 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1169 | callingPid, callingUid, cameraId.string(), |
| 1170 | clientName8.string(), clientUid, clientPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1173 | // Check if we can trust clientPid |
| 1174 | if (clientPid == USE_CALLING_PID) { |
| 1175 | clientPid = callingPid; |
| 1176 | } else if (!isTrustedCallingUid(callingUid)) { |
| 1177 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " |
| 1178 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1179 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1180 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 1181 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 1182 | callingPid, callingUid, cameraId.string(), |
| 1183 | clientName8.string(), clientUid, clientPid); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1184 | } |
| 1185 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1186 | if (shouldRejectSystemCameraConnection(cameraId)) { |
| 1187 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", |
| 1188 | cameraId.c_str()); |
| 1189 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, "No camera device with ID \"%s\" is" |
| 1190 | "available", cameraId.string()); |
| 1191 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1192 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 1193 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 1194 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 1195 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "No camera device with ID \"%s\"" |
| 1196 | "found while trying to query device kind", cameraId.string()); |
| 1197 | |
| 1198 | } |
| 1199 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1200 | // If it's not calling from cameraserver, check the permission if the |
| 1201 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for |
| 1202 | // android.permission.SYSTEM_CAMERA for system only camera devices). |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1203 | if (callingPid != getpid() && |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1204 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1205 | !checkPermission(sCameraPermission, clientPid, clientUid)) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1206 | 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] | 1207 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1208 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission", |
| 1209 | clientName8.string(), clientUid, clientPid, cameraId.string()); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1210 | } |
| 1211 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1212 | // 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] | 1213 | if (!mUidPolicy->isUidActive(callingUid, String16(clientName8))) { |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1214 | int32_t procState = mUidPolicy->getProcState(callingUid); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1215 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", |
| 1216 | clientPid, clientUid); |
| 1217 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1218 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" |
| 1219 | "calling UID %d proc state %" PRId32 ")", |
| 1220 | clientName8.string(), clientUid, clientPid, cameraId.string(), |
| 1221 | callingUid, procState); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1222 | } |
| 1223 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1224 | // If sensor privacy is enabled then prevent access to the camera |
| 1225 | if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
| 1226 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); |
| 1227 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1228 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" when sensor privacy " |
| 1229 | "is enabled", clientName8.string(), clientUid, clientPid, cameraId.string()); |
| 1230 | } |
| 1231 | |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1232 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's |
| 1233 | // connected to camera service directly. |
Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1234 | originalClientPid = clientPid; |
Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1235 | clientPid = callingPid; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1236 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1237 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1238 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1239 | // Only allow clients who are being used by the current foreground device user, unless calling |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1240 | // from our own process OR the caller is using the cameraserver's HIDL interface. |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 1241 | if (getCurrentServingCall() != BinderCallType::HWBINDER && callingPid != getpid() && |
Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1242 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1243 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " |
| 1244 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, |
| 1245 | toString(mAllowedUsers).string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1246 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1247 | "Callers from device user %d are not currently allowed to connect to camera \"%s\"", |
| 1248 | clientUserId, cameraId.string()); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1251 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const { |
| 1255 | auto cameraState = getCameraState(cameraId); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1256 | int callingPid = CameraThreadState::getCallingPid(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1257 | if (cameraState == nullptr) { |
| 1258 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 1259 | cameraId.string()); |
| 1260 | return -ENODEV; |
| 1261 | } |
| 1262 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1263 | StatusInternal currentStatus = cameraState->getStatus(); |
| 1264 | if (currentStatus == StatusInternal::NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1265 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
| 1266 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1267 | return -ENODEV; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1268 | } else if (currentStatus == StatusInternal::ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1269 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
| 1270 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1271 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1272 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1273 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1274 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1277 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1278 | const CameraService::DescriptorPtr& desc, int oomScoreOffset) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1279 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1280 | // Make a descriptor for the incoming client |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1281 | auto clientDescriptor = |
| 1282 | CameraService::CameraClientManager::makeClientDescriptor(client, desc, |
| 1283 | oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1284 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 1285 | |
| 1286 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
| 1287 | String8(client->getPackageName())); |
| 1288 | |
| 1289 | if (evicted.size() > 0) { |
| 1290 | // This should never happen - clients should already have been removed in disconnect |
| 1291 | for (auto& i : evicted) { |
| 1292 | ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect", |
| 1293 | __FUNCTION__, i->getKey().string()); |
| 1294 | } |
| 1295 | |
| 1296 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 1297 | __FUNCTION__); |
| 1298 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1299 | |
| 1300 | // And register a death notification for the client callback. Do |
| 1301 | // this last to avoid Binder policy where a nested Binder |
| 1302 | // transaction might be pre-empted to service the client death |
| 1303 | // notification if the client process dies before linkToDeath is |
| 1304 | // invoked. |
| 1305 | sp<IBinder> remoteCallback = client->getRemote(); |
| 1306 | if (remoteCallback != nullptr) { |
| 1307 | remoteCallback->linkToDeath(this); |
| 1308 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid, |
| 1312 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1313 | int oomScoreOffset, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1314 | /*out*/ |
| 1315 | sp<BasicClient>* client, |
| 1316 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1317 | ATRACE_CALL(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1318 | status_t ret = NO_ERROR; |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1319 | std::vector<DescriptorPtr> evictedClients; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1320 | DescriptorPtr clientDescriptor; |
| 1321 | { |
| 1322 | if (effectiveApiLevel == API_1) { |
| 1323 | // If we are using API1, any existing client for this camera ID with the same remote |
| 1324 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 1325 | |
| 1326 | auto current = mActiveClientManager.get(cameraId); |
| 1327 | if (current != nullptr) { |
| 1328 | auto clientSp = current->getValue(); |
| 1329 | if (clientSp.get() != nullptr) { // should never be needed |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1330 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { |
| 1331 | ALOGW("CameraService connect called from same client, but with a different" |
| 1332 | " API level, evicting prior client..."); |
| 1333 | } else if (clientSp->getRemote() == remoteCallback) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1334 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1335 | " app binder, returning the same client)", clientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1336 | *client = clientSp; |
| 1337 | return NO_ERROR; |
| 1338 | } |
| 1339 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1340 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1341 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 1342 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1343 | // Get current active client PIDs |
| 1344 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 1345 | ownerPids.push_back(clientPid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1346 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1347 | std::vector<int> priorityScores(ownerPids.size()); |
| 1348 | std::vector<int> states(ownerPids.size()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1349 | |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1350 | // Get priority scores of all active PIDs |
| 1351 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids( |
| 1352 | ownerPids.size(), &ownerPids[0], /*out*/&states[0], |
| 1353 | /*out*/&priorityScores[0]); |
| 1354 | if (err != OK) { |
| 1355 | ALOGE("%s: Priority score query failed: %d", |
| 1356 | __FUNCTION__, err); |
| 1357 | return err; |
| 1358 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1359 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1360 | // Update all active clients' priorities |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1361 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1362 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1363 | pidToPriorityMap.emplace(ownerPids[i], |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 1364 | resource_policy::ClientPriority(priorityScores[i], states[i], |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1365 | /* isVendorClient won't get copied over*/ false, |
| 1366 | /* oomScoreOffset won't get copied over*/ 0)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1367 | } |
| 1368 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1369 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1370 | // Get state for the given cameraId |
| 1371 | auto state = getCameraState(cameraId); |
| 1372 | if (state == nullptr) { |
| 1373 | ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", |
| 1374 | clientPid, cameraId.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1375 | // Should never get here because validateConnectLocked should have errored out |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1376 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1377 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1378 | |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1379 | int32_t actualScore = priorityScores[priorityScores.size() - 1]; |
| 1380 | int32_t actualState = states[states.size() - 1]; |
| 1381 | |
| 1382 | // Make descriptor for incoming client. We store the oomScoreOffset |
| 1383 | // since we might need it later on new handleEvictionsLocked and |
| 1384 | // ProcessInfoService would not take that into account. |
Shuzhen Wang | 2db86ff | 2018-04-25 01:11:17 +0000 | [diff] [blame] | 1385 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1386 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1387 | state->getConflicting(), actualScore, clientPid, actualState, |
| 1388 | oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1389 | |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1390 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); |
| 1391 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1392 | // Find clients that would be evicted |
| 1393 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 1394 | |
| 1395 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 1396 | // background, so we cannot do evictions |
| 1397 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 1398 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 1399 | " priority).", clientPid); |
| 1400 | |
| 1401 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 1402 | String8 curTime = getFormattedCurrentTime(); |
| 1403 | auto incompatibleClients = |
| 1404 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 1405 | |
| 1406 | 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] | 1407 | "(PID %d, score %d state %d) due to eviction policy", curTime.string(), |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1408 | cameraId.string(), packageName.string(), clientPid, |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1409 | clientPriority.getScore(), clientPriority.getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1410 | |
| 1411 | for (auto& i : incompatibleClients) { |
| 1412 | msg.appendFormat("\n - Blocked by existing device %s client for package %s" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1413 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", |
| 1414 | i->getKey().string(), |
| 1415 | String8{i->getValue()->getPackageName()}.string(), |
| 1416 | i->getOwnerId(), i->getPriority().getScore(), |
| 1417 | i->getPriority().getState()); |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1418 | ALOGE(" Conflicts with: Device %s, client package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1419 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(), |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1420 | String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1421 | i->getPriority().getScore(), i->getPriority().getState()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | // Log the client's attempt |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1425 | Mutex::Autolock l(mLogLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1426 | mEventLog.add(msg); |
| 1427 | |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1428 | auto current = mActiveClientManager.get(cameraId); |
| 1429 | if (current != nullptr) { |
| 1430 | return -EBUSY; // CAMERA_IN_USE |
| 1431 | } else { |
| 1432 | return -EUSERS; // MAX_CAMERAS_IN_USE |
| 1433 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | for (auto& i : evicted) { |
| 1437 | sp<BasicClient> clientSp = i->getValue(); |
| 1438 | if (clientSp.get() == nullptr) { |
| 1439 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 1440 | |
| 1441 | // TODO: Remove this |
| 1442 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 1443 | __FUNCTION__); |
| 1444 | mActiveClientManager.remove(i); |
| 1445 | continue; |
| 1446 | } |
| 1447 | |
| 1448 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
| 1449 | i->getKey().string()); |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1450 | evictedClients.push_back(i); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1451 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1452 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1453 | logEvent(String8::format("EVICT device %s client held by package %s (PID" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1454 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" |
| 1455 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1456 | i->getKey().string(), String8{clientSp->getPackageName()}.string(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1457 | i->getOwnerId(), i->getPriority().getScore(), |
| 1458 | i->getPriority().getState(), cameraId.string(), |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1459 | packageName.string(), clientPid, clientPriority.getScore(), |
| 1460 | clientPriority.getState())); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1461 | |
| 1462 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1463 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1464 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1465 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1468 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 1469 | // other clients from connecting in mServiceLockWrapper if held |
| 1470 | mServiceLock.unlock(); |
| 1471 | |
| 1472 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1473 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1474 | |
| 1475 | // Destroy evicted clients |
| 1476 | for (auto& i : evictedClients) { |
| 1477 | // 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] | 1478 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1479 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1480 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1481 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1482 | |
Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1483 | for (const auto& i : evictedClients) { |
| 1484 | ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")", |
| 1485 | __FUNCTION__, i->getKey().string(), i->getOwnerId()); |
| 1486 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); |
| 1487 | if (ret == TIMED_OUT) { |
| 1488 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " |
| 1489 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), |
| 1490 | mActiveClientManager.toString().string()); |
| 1491 | return -EBUSY; |
| 1492 | } |
| 1493 | if (ret != NO_ERROR) { |
| 1494 | ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), " |
| 1495 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret), |
| 1496 | ret, mActiveClientManager.toString().string()); |
| 1497 | return ret; |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | evictedClients.clear(); |
| 1502 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1503 | // Once clients have been disconnected, relock |
| 1504 | mServiceLock.lock(); |
| 1505 | |
| 1506 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 1507 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 1508 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1511 | *partial = clientDescriptor; |
| 1512 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1515 | Status CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1516 | const sp<ICameraClient>& cameraClient, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1517 | int api1CameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1518 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1519 | int clientUid, |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1520 | int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1521 | int targetSdkVersion, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1522 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1523 | sp<ICamera>* device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1524 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1525 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1526 | Status ret = Status::ok(); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1527 | |
| 1528 | String8 id = cameraIdIntToStr(api1CameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1529 | sp<Client> client = nullptr; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1530 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1531 | clientPackageName, {}, clientUid, clientPid, API_1, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1532 | /*shimUpdateOnly*/ false, /*oomScoreOffset*/ 0, targetSdkVersion, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1533 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1534 | if(!ret.isOk()) { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1535 | logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageName), |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1536 | ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1537 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1540 | *device = client; |
| 1541 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1544 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, |
| 1545 | bool isVendorListener, int clientPid, int clientUid) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1546 | // If the client is not a vendor client, don't add listener if |
| 1547 | // a) the camera is a publicly hidden secure camera OR |
| 1548 | // b) the camera is a system only camera and the client doesn't |
| 1549 | // have android.permission.SYSTEM_CAMERA permissions. |
| 1550 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || |
| 1551 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
| 1552 | !hasPermissionsForSystemCamera(clientPid, clientUid)))) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 1553 | return true; |
| 1554 | } |
| 1555 | return false; |
| 1556 | } |
| 1557 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1558 | bool CameraService::shouldRejectSystemCameraConnection(const String8& cameraId) const { |
| 1559 | // Rules for rejection: |
| 1560 | // 1) If cameraserver tries to access this camera device, accept the |
| 1561 | // connection. |
| 1562 | // 2) The camera device is a publicly hidden secure camera device AND some |
| 1563 | // component is trying to access it on a non-hwbinder thread (generally a non HAL client), |
| 1564 | // reject it. |
| 1565 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY |
| 1566 | // and the serving thread is a non hwbinder thread, the client must have |
| 1567 | // android.permission.SYSTEM_CAMERA permissions to connect. |
| 1568 | |
| 1569 | int cPid = CameraThreadState::getCallingPid(); |
| 1570 | int cUid = CameraThreadState::getCallingUid(); |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1571 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; |
| 1572 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1573 | // This isn't a known camera ID, so it's not a system camera |
| 1574 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); |
| 1575 | return false; |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1576 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1577 | |
| 1578 | // (1) Cameraserver trying to connect, accept. |
| 1579 | if (CameraThreadState::getCallingPid() == getpid()) { |
| 1580 | return false; |
| 1581 | } |
| 1582 | // (2) |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 1583 | if (getCurrentServingCall() != BinderCallType::HWBINDER && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1584 | systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { |
| 1585 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); |
| 1586 | return true; |
| 1587 | } |
| 1588 | // (3) Here we only check for permissions if it is a system only camera device. This is since |
| 1589 | // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some |
| 1590 | // characteristics) even if clients don't have android.permission.CAMERA. We do not want the |
| 1591 | // same behavior for system camera devices. |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 1592 | if (getCurrentServingCall() != BinderCallType::HWBINDER && |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1593 | systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && |
| 1594 | !hasPermissionsForSystemCamera(cPid, cUid)) { |
| 1595 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", |
| 1596 | cameraId.c_str()); |
| 1597 | return true; |
| 1598 | } |
| 1599 | |
| 1600 | return false; |
| 1601 | } |
| 1602 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1603 | Status CameraService::connectDevice( |
| 1604 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1605 | const String16& cameraId, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1606 | const String16& clientPackageName, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1607 | const std::optional<String16>& clientFeatureId, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1608 | int clientUid, int oomScoreOffset, int targetSdkVersion, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1609 | /*out*/ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1610 | sp<hardware::camera2::ICameraDeviceUser>* device) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1611 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1612 | ATRACE_CALL(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1613 | Status ret = Status::ok(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1614 | String8 id = String8(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1615 | sp<CameraDeviceClient> client = nullptr; |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1616 | String16 clientPackageNameAdj = clientPackageName; |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1617 | int callingPid = CameraThreadState::getCallingPid(); |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 1618 | |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 1619 | if (getCurrentServingCall() == BinderCallType::HWBINDER) { |
Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1620 | std::string vendorClient = |
| 1621 | StringPrintf("vendor.client.pid<%d>", CameraThreadState::getCallingPid()); |
| 1622 | clientPackageNameAdj = String16(vendorClient.c_str()); |
| 1623 | } |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1624 | |
| 1625 | if (oomScoreOffset < 0) { |
| 1626 | String8 msg = |
| 1627 | String8::format("Cannot increase the priority of a client %s pid %d for " |
| 1628 | "camera id %s", String8(clientPackageNameAdj).string(), callingPid, |
| 1629 | id.string()); |
| 1630 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1631 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 1632 | } |
| 1633 | |
| 1634 | // enforce system camera permissions |
| 1635 | if (oomScoreOffset > 0 && |
| 1636 | !hasPermissionsForSystemCamera(callingPid, CameraThreadState::getCallingUid())) { |
| 1637 | String8 msg = |
| 1638 | String8::format("Cannot change the priority of a client %s pid %d for " |
| 1639 | "camera id %s without SYSTEM_CAMERA permissions", |
| 1640 | String8(clientPackageNameAdj).string(), callingPid, id.string()); |
| 1641 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 1642 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, msg.string()); |
| 1643 | } |
| 1644 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1645 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1646 | /*api1CameraId*/-1, clientPackageNameAdj, clientFeatureId, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1647 | clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, oomScoreOffset, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1648 | targetSdkVersion, /*out*/client); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1649 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1650 | if(!ret.isOk()) { |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1651 | logRejected(id, callingPid, String8(clientPackageNameAdj), ret.toString8()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1652 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1655 | *device = client; |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 1656 | Mutex::Autolock lock(mServiceLock); |
| 1657 | |
| 1658 | // Clear the previous cached logs and reposition the |
| 1659 | // file offset to beginning of the file to log new data. |
| 1660 | // If either truncate or lseek fails, close the previous file and create a new one. |
| 1661 | if ((ftruncate(mMemFd, 0) == -1) || (lseek(mMemFd, 0, SEEK_SET) == -1)) { |
| 1662 | ALOGE("%s: Error while truncating the file: %s", __FUNCTION__, sFileName); |
| 1663 | // Close the previous memfd. |
| 1664 | close(mMemFd); |
| 1665 | // If failure to wipe the data, then create a new file and |
| 1666 | // assign the new value to mMemFd. |
| 1667 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); |
| 1668 | if (mMemFd == -1) { |
| 1669 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); |
| 1670 | } |
| 1671 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1672 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1673 | } |
| 1674 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1675 | template<class CALLBACK, class CLIENT> |
| 1676 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1677 | int api1CameraId, const String16& clientPackageName, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1678 | const std::optional<String16>& clientFeatureId, int clientUid, int clientPid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1679 | apiLevel effectiveApiLevel, bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1680 | /*out*/sp<CLIENT>& device) { |
| 1681 | binder::Status ret = binder::Status::ok(); |
| 1682 | |
| 1683 | String8 clientName8(clientPackageName); |
| 1684 | |
| 1685 | int originalClientPid = 0; |
| 1686 | |
Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1687 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1688 | "Camera API version %d", clientPid, clientName8.string(), cameraId.string(), |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1689 | static_cast<int>(effectiveApiLevel)); |
| 1690 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1691 | nsecs_t openTimeNs = systemTime(); |
| 1692 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1693 | sp<CLIENT> client = nullptr; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1694 | int facing = -1; |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1695 | int orientation = 0; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1696 | bool isNdk = (clientPackageName.size() == 0); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1697 | { |
| 1698 | // Acquire mServiceLock and prevent other clients from connecting |
| 1699 | std::unique_ptr<AutoConditionLock> lock = |
| 1700 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 1701 | |
| 1702 | if (lock == nullptr) { |
| 1703 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." |
| 1704 | , clientPid); |
| 1705 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1706 | "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting", |
| 1707 | cameraId.string(), clientName8.string(), clientPid); |
| 1708 | } |
| 1709 | |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 1710 | // Enforce client permissions and do basic validity checks |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1711 | if(!(ret = validateConnectLocked(cameraId, clientName8, |
| 1712 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { |
| 1713 | return ret; |
| 1714 | } |
| 1715 | |
| 1716 | // Check the shim parameters after acquiring lock, if they have already been updated and |
| 1717 | // we were doing a shim update, return immediately |
| 1718 | if (shimUpdateOnly) { |
| 1719 | auto cameraState = getCameraState(cameraId); |
| 1720 | if (cameraState != nullptr) { |
| 1721 | if (!cameraState->getShimParams().isEmpty()) return ret; |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | status_t err; |
| 1726 | |
| 1727 | sp<BasicClient> clientTmp = nullptr; |
| 1728 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial; |
| 1729 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1730 | IInterface::asBinder(cameraCb), clientName8, oomScoreOffset, /*out*/&clientTmp, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1731 | /*out*/&partial)) != NO_ERROR) { |
| 1732 | switch (err) { |
| 1733 | case -ENODEV: |
| 1734 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, |
| 1735 | "No camera device with ID \"%s\" currently available", |
| 1736 | cameraId.string()); |
| 1737 | case -EBUSY: |
| 1738 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 1739 | "Higher-priority client using camera, ID \"%s\" currently unavailable", |
| 1740 | cameraId.string()); |
Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1741 | case -EUSERS: |
| 1742 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1743 | "Too many cameras already open, cannot open camera \"%s\"", |
| 1744 | cameraId.string()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1745 | default: |
| 1746 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1747 | "Unexpected error %s (%d) opening camera \"%s\"", |
| 1748 | strerror(-err), err, cameraId.string()); |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | if (clientTmp.get() != nullptr) { |
| 1753 | // Handle special case for API1 MediaRecorder where the existing client is returned |
| 1754 | device = static_cast<CLIENT*>(clientTmp.get()); |
| 1755 | return ret; |
| 1756 | } |
| 1757 | |
| 1758 | // give flashlight a chance to close devices if necessary. |
| 1759 | mFlashlight->prepareDeviceOpen(cameraId); |
| 1760 | |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1761 | int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing, /*out*/&orientation); |
Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 1762 | if (facing == -1) { |
| 1763 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string()); |
| 1764 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1765 | "Unable to get camera device \"%s\" facing", cameraId.string()); |
| 1766 | } |
| 1767 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1768 | sp<BasicClient> tmp = nullptr; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1769 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( |
| 1770 | mPerfClassPrimaryCameraIds, cameraId.string(), targetSdkVersion); |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 1771 | if(!(ret = makeClient(this, cameraCb, clientPackageName, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 1772 | cameraId, api1CameraId, facing, orientation, |
Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 1773 | clientPid, clientUid, getpid(), |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1774 | deviceVersion, effectiveApiLevel, overrideForPerfClass, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1775 | /*out*/&tmp)).isOk()) { |
| 1776 | return ret; |
| 1777 | } |
| 1778 | client = static_cast<CLIENT*>(tmp.get()); |
| 1779 | |
| 1780 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", |
| 1781 | __FUNCTION__); |
| 1782 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 1783 | err = client->initialize(mCameraProviderManager, mMonitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1784 | if (err != OK) { |
| 1785 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1786 | // Errors could be from the HAL module open call or from AppOpsManager |
| 1787 | switch(err) { |
| 1788 | case BAD_VALUE: |
| 1789 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1790 | "Illegal argument to HAL module for camera \"%s\"", cameraId.string()); |
| 1791 | case -EBUSY: |
| 1792 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 1793 | "Camera \"%s\" is already open", cameraId.string()); |
| 1794 | case -EUSERS: |
| 1795 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 1796 | "Too many cameras already open, cannot open camera \"%s\"", |
| 1797 | cameraId.string()); |
| 1798 | case PERMISSION_DENIED: |
| 1799 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 1800 | "No permission to open camera \"%s\"", cameraId.string()); |
| 1801 | case -EACCES: |
| 1802 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1803 | "Camera \"%s\" disabled by policy", cameraId.string()); |
| 1804 | case -ENODEV: |
| 1805 | default: |
| 1806 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 1807 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(), |
| 1808 | strerror(-err), err); |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | // Update shim paremeters for legacy clients |
| 1813 | if (effectiveApiLevel == API_1) { |
| 1814 | // Assume we have always received a Client subclass for API1 |
| 1815 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); |
| 1816 | String8 rawParams = shimClient->getParameters(); |
| 1817 | CameraParameters params(rawParams); |
| 1818 | |
| 1819 | auto cameraState = getCameraState(cameraId); |
| 1820 | if (cameraState != nullptr) { |
| 1821 | cameraState->setShimParams(params); |
| 1822 | } else { |
| 1823 | ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.", |
| 1824 | __FUNCTION__, cameraId.string()); |
| 1825 | } |
| 1826 | } |
| 1827 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1828 | // Set rotate-and-crop override behavior |
| 1829 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { |
| 1830 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); |
Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1831 | } else if (CameraServiceProxyWrapper::isRotateAndCropOverrideNeeded(clientPackageName, |
| 1832 | orientation, facing)) { |
| 1833 | client->setRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_90); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1834 | } |
| 1835 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 1836 | // Set camera muting behavior |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame^] | 1837 | bool isCameraPrivacyEnabled = |
| 1838 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(multiuser_get_user_id(clientUid)); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 1839 | if (client->supportsCameraMute()) { |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame^] | 1840 | client->setCameraMute( |
| 1841 | mOverrideCameraMuteMode || isCameraPrivacyEnabled); |
| 1842 | } else if (isCameraPrivacyEnabled) { |
| 1843 | // no camera mute supported, but privacy is on! => disconnect |
| 1844 | ALOGI("Camera mute not supported for package: %s, camera id: %s", |
| 1845 | String8(client->getPackageName()).string(), cameraId.string()); |
| 1846 | // Do not hold mServiceLock while disconnecting clients, but |
| 1847 | // retain the condition blocking other clients from connecting |
| 1848 | // in mServiceLockWrapper if held. |
| 1849 | mServiceLock.unlock(); |
| 1850 | // Clear caller identity temporarily so client disconnect PID |
| 1851 | // checks work correctly |
| 1852 | int64_t token = CameraThreadState::clearCallingIdentity(); |
| 1853 | // Note AppOp to trigger the "Unblock" dialog |
| 1854 | client->noteAppOp(); |
| 1855 | client->disconnect(); |
| 1856 | CameraThreadState::restoreCallingIdentity(token); |
| 1857 | // Reacquire mServiceLock |
| 1858 | mServiceLock.lock(); |
| 1859 | |
| 1860 | return STATUS_ERROR_FMT(ERROR_DISABLED, |
| 1861 | "Camera \"%s\" disabled due to camera mute", cameraId.string()); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 1862 | } |
| 1863 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1864 | if (shimUpdateOnly) { |
| 1865 | // If only updating legacy shim parameters, immediately disconnect client |
| 1866 | mServiceLock.unlock(); |
| 1867 | client->disconnect(); |
| 1868 | mServiceLock.lock(); |
| 1869 | } else { |
| 1870 | // Otherwise, add client to active clients list |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1871 | finishConnectLocked(client, partial, oomScoreOffset); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1872 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 1873 | |
| 1874 | client->setImageDumpMask(mImageDumpMask); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1875 | } // lock is destroyed, allow further connect calls |
| 1876 | |
| 1877 | // Important: release the mutex here so the client can call back into the service from its |
| 1878 | // destructor (can be at the end of the call) |
| 1879 | device = client; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1880 | |
| 1881 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); |
| 1882 | CameraServiceProxyWrapper::logOpen(cameraId, facing, clientPackageName, |
| 1883 | effectiveApiLevel, isNdk, openLatencyMs); |
| 1884 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1885 | return ret; |
| 1886 | } |
| 1887 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 1888 | status_t CameraService::addOfflineClient(String8 cameraId, sp<BasicClient> offlineClient) { |
| 1889 | if (offlineClient.get() == nullptr) { |
| 1890 | return BAD_VALUE; |
| 1891 | } |
| 1892 | |
| 1893 | { |
| 1894 | // Acquire mServiceLock and prevent other clients from connecting |
| 1895 | std::unique_ptr<AutoConditionLock> lock = |
| 1896 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); |
| 1897 | |
| 1898 | if (lock == nullptr) { |
| 1899 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." |
| 1900 | , __FUNCTION__, offlineClient->getClientPid()); |
| 1901 | return TIMED_OUT; |
| 1902 | } |
| 1903 | |
| 1904 | auto onlineClientDesc = mActiveClientManager.get(cameraId); |
| 1905 | if (onlineClientDesc.get() == nullptr) { |
| 1906 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, |
| 1907 | cameraId.c_str()); |
| 1908 | return BAD_VALUE; |
| 1909 | } |
| 1910 | |
| 1911 | // Offline clients do not evict or conflict with other online devices. Resource sharing |
| 1912 | // conflicts are handled by the camera provider which will either succeed or fail before |
| 1913 | // reaching this method. |
| 1914 | const auto& onlinePriority = onlineClientDesc->getPriority(); |
| 1915 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( |
| 1916 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, |
| 1917 | /*conflictingKeys*/ std::set<String8>(), onlinePriority.getScore(), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1918 | onlineClientDesc->getOwnerId(), onlinePriority.getState(), |
| 1919 | /*ommScoreOffset*/ 0); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 1920 | |
| 1921 | // Allow only one offline device per camera |
| 1922 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); |
| 1923 | if (!incompatibleClients.empty()) { |
| 1924 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); |
| 1925 | return BAD_VALUE; |
| 1926 | } |
| 1927 | |
| 1928 | auto err = offlineClient->initialize(mCameraProviderManager, mMonitorTags); |
| 1929 | if (err != OK) { |
| 1930 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); |
| 1931 | return err; |
| 1932 | } |
| 1933 | |
| 1934 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); |
| 1935 | if (evicted.size() > 0) { |
| 1936 | for (auto& i : evicted) { |
| 1937 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", |
| 1938 | __FUNCTION__, i->getKey().string()); |
| 1939 | } |
| 1940 | |
| 1941 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " |
| 1942 | "properly", __FUNCTION__); |
| 1943 | |
| 1944 | return BAD_VALUE; |
| 1945 | } |
| 1946 | |
| 1947 | logConnectedOffline(offlineClientDesc->getKey(), |
| 1948 | static_cast<int>(offlineClientDesc->getOwnerId()), |
| 1949 | String8(offlineClient->getPackageName())); |
| 1950 | |
| 1951 | sp<IBinder> remoteCallback = offlineClient->getRemote(); |
| 1952 | if (remoteCallback != nullptr) { |
| 1953 | remoteCallback->linkToDeath(this); |
| 1954 | } |
| 1955 | } // lock is destroyed, allow further connect calls |
| 1956 | |
| 1957 | return OK; |
| 1958 | } |
| 1959 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1960 | Status CameraService::setTorchMode(const String16& cameraId, bool enabled, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1961 | const sp<IBinder>& clientBinder) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1962 | Mutex::Autolock lock(mServiceLock); |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1963 | |
| 1964 | ATRACE_CALL(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 1965 | if (enabled && clientBinder == nullptr) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1966 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1967 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, |
| 1968 | "Torch client Binder is null"); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1969 | } |
| 1970 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1971 | String8 id = String8(cameraId.string()); |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1972 | int uid = CameraThreadState::getCallingUid(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1973 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 1974 | if (shouldRejectSystemCameraConnection(id)) { |
| 1975 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" |
| 1976 | " for system only device %s: ", id.string()); |
| 1977 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1978 | // verify id is valid. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1979 | auto state = getCameraState(id); |
| 1980 | if (state == nullptr) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 1981 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1982 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1983 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1984 | } |
| 1985 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1986 | StatusInternal cameraStatus = state->getStatus(); |
| 1987 | if (cameraStatus != StatusInternal::PRESENT && |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 1988 | cameraStatus != StatusInternal::NOT_AVAILABLE) { |
| 1989 | 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] | 1990 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 1991 | "Camera ID \"%s\" is a not valid camera ID", id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | { |
| 1995 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1996 | TorchModeStatus status; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1997 | status_t err = getTorchStatusLocked(id, &status); |
| 1998 | if (err != OK) { |
| 1999 | if (err == NAME_NOT_FOUND) { |
| 2000 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, |
| 2001 | "Camera \"%s\" does not have a flash unit", id.string()); |
| 2002 | } |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2003 | ALOGE("%s: getting current torch status failed for camera %s", |
| 2004 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2005 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, |
| 2006 | "Error updating torch status for camera \"%s\": %s (%d)", id.string(), |
| 2007 | strerror(-err), err); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2008 | } |
| 2009 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2010 | if (status == TorchModeStatus::NOT_AVAILABLE) { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2011 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2012 | ALOGE("%s: torch mode of camera %s is not available because " |
| 2013 | "camera is in use", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2014 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, |
| 2015 | "Torch for camera \"%s\" is not available due to an existing camera user", |
| 2016 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2017 | } else { |
| 2018 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 2019 | "insufficient resources", __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2020 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, |
| 2021 | "Torch for camera \"%s\" is not available due to insufficient resources", |
| 2022 | id.string()); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2023 | } |
| 2024 | } |
| 2025 | } |
| 2026 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2027 | { |
| 2028 | // Update UID map - this is used in the torch status changed callbacks, so must be done |
| 2029 | // before setTorchMode |
Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 2030 | Mutex::Autolock al(mTorchUidMapMutex); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2031 | if (mTorchUidMap.find(id) == mTorchUidMap.end()) { |
| 2032 | mTorchUidMap[id].first = uid; |
| 2033 | mTorchUidMap[id].second = uid; |
| 2034 | } else { |
| 2035 | // Set the pending UID |
| 2036 | mTorchUidMap[id].first = uid; |
| 2037 | } |
| 2038 | } |
| 2039 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2040 | status_t err = mFlashlight->setTorchMode(id, enabled); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2041 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2042 | if (err != OK) { |
| 2043 | int32_t errorCode; |
| 2044 | String8 msg; |
| 2045 | switch (err) { |
| 2046 | case -ENOSYS: |
| 2047 | msg = String8::format("Camera \"%s\" has no flashlight", |
| 2048 | id.string()); |
| 2049 | errorCode = ERROR_ILLEGAL_ARGUMENT; |
| 2050 | break; |
| 2051 | default: |
| 2052 | msg = String8::format( |
| 2053 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", |
| 2054 | id.string(), enabled, strerror(-err), err); |
| 2055 | errorCode = ERROR_INVALID_OPERATION; |
| 2056 | } |
| 2057 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2058 | logServiceError(msg,errorCode); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2059 | return STATUS_ERROR(errorCode, msg.string()); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2060 | } |
| 2061 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2062 | { |
| 2063 | // update the link to client's death |
| 2064 | Mutex::Autolock al(mTorchClientMapMutex); |
| 2065 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 2066 | if (enabled) { |
| 2067 | if (index == NAME_NOT_FOUND) { |
| 2068 | mTorchClientMap.add(id, clientBinder); |
| 2069 | } else { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2070 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2071 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 2072 | } |
| 2073 | clientBinder->linkToDeath(this); |
| 2074 | } else if (index != NAME_NOT_FOUND) { |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2075 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2076 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2077 | } |
| 2078 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 2079 | int clientPid = CameraThreadState::getCallingPid(); |
| 2080 | const char *id_cstr = id.c_str(); |
| 2081 | const char *torchState = enabled ? "on" : "off"; |
| 2082 | ALOGI("Torch for camera id %s turned %s for client PID %d", id_cstr, torchState, clientPid); |
| 2083 | logTorchEvent(id_cstr, torchState , clientPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2084 | return Status::ok(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2085 | } |
| 2086 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2087 | Status CameraService::notifySystemEvent(int32_t eventId, |
| 2088 | const std::vector<int32_t>& args) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2089 | const int pid = CameraThreadState::getCallingPid(); |
| 2090 | const int selfPid = getpid(); |
| 2091 | |
| 2092 | // Permission checks |
| 2093 | if (pid != selfPid) { |
| 2094 | // Ensure we're being called by system_server, or similar process with |
| 2095 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2096 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2097 | const int uid = CameraThreadState::getCallingUid(); |
| 2098 | ALOGE("Permission Denial: cannot send updates to camera service about system" |
| 2099 | " events from pid=%d, uid=%d", pid, uid); |
| 2100 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 2101 | "No permission to send updates to camera service about system events" |
| 2102 | " from pid=%d, uid=%d", pid, uid); |
Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2103 | } |
| 2104 | } |
| 2105 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2106 | ATRACE_CALL(); |
| 2107 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2108 | switch(eventId) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2109 | case ICameraService::EVENT_USER_SWITCHED: { |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2110 | // 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] | 2111 | // from a system server crash |
| 2112 | mUidPolicy->registerSelf(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2113 | mSensorPrivacyPolicy->registerSelf(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2114 | doUserSwitch(/*newUserIds*/ args); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2115 | break; |
| 2116 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2117 | case ICameraService::EVENT_NONE: |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2118 | default: { |
| 2119 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, |
| 2120 | eventId); |
| 2121 | break; |
| 2122 | } |
| 2123 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2124 | return Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2125 | } |
| 2126 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2127 | void CameraService::notifyMonitoredUids() { |
| 2128 | Mutex::Autolock lock(mStatusListenerLock); |
| 2129 | |
| 2130 | for (const auto& it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2131 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2132 | if (!ret.isOk()) { |
| 2133 | ALOGE("%s: Failed to trigger permission callback: %d", __FUNCTION__, |
| 2134 | ret.exceptionCode()); |
| 2135 | } |
| 2136 | } |
| 2137 | } |
| 2138 | |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2139 | Status CameraService::notifyDeviceStateChange(int64_t newState) { |
| 2140 | const int pid = CameraThreadState::getCallingPid(); |
| 2141 | const int selfPid = getpid(); |
| 2142 | |
| 2143 | // Permission checks |
| 2144 | if (pid != selfPid) { |
| 2145 | // Ensure we're being called by system_server, or similar process with |
| 2146 | // permissions to notify the camera service about system events |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2147 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2148 | const int uid = CameraThreadState::getCallingUid(); |
| 2149 | ALOGE("Permission Denial: cannot send updates to camera service about device" |
| 2150 | " state changes from pid=%d, uid=%d", pid, uid); |
| 2151 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2152 | "No permission to send updates to camera service about device state" |
| 2153 | " changes from pid=%d, uid=%d", pid, uid); |
| 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | ATRACE_CALL(); |
| 2158 | |
| 2159 | using hardware::camera::provider::V2_5::DeviceState; |
| 2160 | hardware::hidl_bitfield<DeviceState> newDeviceState{}; |
| 2161 | if (newState & ICameraService::DEVICE_STATE_BACK_COVERED) { |
| 2162 | newDeviceState |= DeviceState::BACK_COVERED; |
| 2163 | } |
| 2164 | if (newState & ICameraService::DEVICE_STATE_FRONT_COVERED) { |
| 2165 | newDeviceState |= DeviceState::FRONT_COVERED; |
| 2166 | } |
| 2167 | if (newState & ICameraService::DEVICE_STATE_FOLDED) { |
| 2168 | newDeviceState |= DeviceState::FOLDED; |
| 2169 | } |
| 2170 | // Only map vendor bits directly |
| 2171 | uint64_t vendorBits = static_cast<uint64_t>(newState) & 0xFFFFFFFF00000000l; |
| 2172 | newDeviceState |= vendorBits; |
| 2173 | |
| 2174 | ALOGV("%s: New device state 0x%" PRIx64, __FUNCTION__, newDeviceState); |
| 2175 | Mutex::Autolock l(mServiceLock); |
| 2176 | mCameraProviderManager->notifyDeviceStateChange(newDeviceState); |
| 2177 | |
| 2178 | return Status::ok(); |
| 2179 | } |
| 2180 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2181 | Status CameraService::notifyDisplayConfigurationChange() { |
| 2182 | ATRACE_CALL(); |
| 2183 | const int callingPid = CameraThreadState::getCallingPid(); |
| 2184 | const int selfPid = getpid(); |
| 2185 | |
| 2186 | // Permission checks |
| 2187 | if (callingPid != selfPid) { |
| 2188 | // Ensure we're being called by system_server, or similar process with |
| 2189 | // permissions to notify the camera service about system events |
| 2190 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { |
| 2191 | const int uid = CameraThreadState::getCallingUid(); |
| 2192 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" |
| 2193 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2194 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 2195 | "No permission to send updates to camera service about orientation" |
| 2196 | " changes from pid=%d, uid=%d", callingPid, uid); |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | Mutex::Autolock lock(mServiceLock); |
| 2201 | |
| 2202 | // Don't do anything if rotate-and-crop override via cmd is active |
| 2203 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); |
| 2204 | |
| 2205 | const auto clients = mActiveClientManager.getAll(); |
| 2206 | for (auto& current : clients) { |
| 2207 | if (current != nullptr) { |
| 2208 | const auto basicClient = current->getValue(); |
| 2209 | if (basicClient.get() != nullptr) { |
| 2210 | if (CameraServiceProxyWrapper::isRotateAndCropOverrideNeeded( |
| 2211 | basicClient->getPackageName(), basicClient->getCameraOrientation(), |
| 2212 | basicClient->getCameraFacing())) { |
| 2213 | basicClient->setRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_90); |
| 2214 | } else { |
| 2215 | basicClient->setRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE); |
| 2216 | } |
| 2217 | } |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | return Status::ok(); |
| 2222 | } |
| 2223 | |
| 2224 | Status CameraService::getConcurrentCameraIds( |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2225 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { |
| 2226 | ATRACE_CALL(); |
| 2227 | if (!concurrentCameraIds) { |
| 2228 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); |
| 2229 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); |
| 2230 | } |
| 2231 | |
| 2232 | if (!mInitialized) { |
| 2233 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2234 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2235 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2236 | "Camera subsystem is not available"); |
| 2237 | } |
| 2238 | // First call into the provider and get the set of concurrent camera |
| 2239 | // combinations |
| 2240 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = |
Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 2241 | mCameraProviderManager->getConcurrentCameraIds(); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2242 | for (auto &combination : concurrentCameraCombinations) { |
| 2243 | std::vector<std::string> validCombination; |
| 2244 | for (auto &cameraId : combination) { |
| 2245 | // if the camera state is not present, skip |
| 2246 | String8 cameraIdStr(cameraId.c_str()); |
| 2247 | auto state = getCameraState(cameraIdStr); |
| 2248 | if (state == nullptr) { |
| 2249 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); |
| 2250 | continue; |
| 2251 | } |
| 2252 | StatusInternal status = state->getStatus(); |
| 2253 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { |
| 2254 | continue; |
| 2255 | } |
| 2256 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { |
| 2257 | continue; |
| 2258 | } |
| 2259 | validCombination.push_back(cameraId); |
| 2260 | } |
| 2261 | if (validCombination.size() != 0) { |
| 2262 | concurrentCameraIds->push_back(std::move(validCombination)); |
| 2263 | } |
| 2264 | } |
| 2265 | return Status::ok(); |
| 2266 | } |
| 2267 | |
| 2268 | Status CameraService::isConcurrentSessionConfigurationSupported( |
| 2269 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2270 | int targetSdkVersion, /*out*/bool* isSupported) { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2271 | if (!isSupported) { |
| 2272 | ALOGE("%s: isSupported is NULL", __FUNCTION__); |
| 2273 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); |
| 2274 | } |
| 2275 | |
| 2276 | if (!mInitialized) { |
| 2277 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); |
| 2278 | return STATUS_ERROR(ERROR_DISCONNECTED, |
| 2279 | "Camera subsystem is not available"); |
| 2280 | } |
| 2281 | |
| 2282 | // Check for camera permissions |
| 2283 | int callingPid = CameraThreadState::getCallingPid(); |
| 2284 | int callingUid = CameraThreadState::getCallingUid(); |
| 2285 | if ((callingPid != getpid()) && !checkPermission(sCameraPermission, callingPid, callingUid)) { |
| 2286 | ALOGE("%s: pid %d doesn't have camera permissions", __FUNCTION__, callingPid); |
| 2287 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 2288 | "android.permission.CAMERA needed to call" |
| 2289 | "isConcurrentSessionConfigurationSupported"); |
| 2290 | } |
| 2291 | |
| 2292 | status_t res = |
| 2293 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2294 | cameraIdsAndSessionConfigurations, mPerfClassPrimaryCameraIds, |
| 2295 | targetSdkVersion, isSupported); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2296 | if (res != OK) { |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2297 | logServiceError(String8::format("Unable to query session configuration support"), |
| 2298 | ERROR_INVALID_OPERATION); |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2299 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " |
| 2300 | "support %s (%d)", strerror(-res), res); |
| 2301 | } |
| 2302 | return Status::ok(); |
| 2303 | } |
| 2304 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2305 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, |
| 2306 | /*out*/ |
| 2307 | std::vector<hardware::CameraStatus> *cameraStatuses) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2308 | return addListenerHelper(listener, cameraStatuses); |
| 2309 | } |
| 2310 | |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2311 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, |
| 2312 | std::vector<hardware::CameraStatus>* cameraStatuses) { |
| 2313 | return addListenerHelper(listener, cameraStatuses, false, true); |
| 2314 | } |
| 2315 | |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2316 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, |
| 2317 | /*out*/ |
| 2318 | std::vector<hardware::CameraStatus> *cameraStatuses, |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2319 | bool isVendorListener, bool isProcessLocalTest) { |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2320 | |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2321 | ATRACE_CALL(); |
| 2322 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2323 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2324 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 2325 | if (listener == nullptr) { |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2326 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2327 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2328 | } |
| 2329 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2330 | auto clientUid = CameraThreadState::getCallingUid(); |
| 2331 | auto clientPid = CameraThreadState::getCallingPid(); |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2332 | bool openCloseCallbackAllowed = checkPermission(sCameraOpenCloseListenerPermission, |
| 2333 | clientPid, clientUid); |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2334 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2335 | Mutex::Autolock lock(mServiceLock); |
| 2336 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2337 | { |
| 2338 | Mutex::Autolock lock(mStatusListenerLock); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2339 | for (const auto &it : mListenerList) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2340 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2341 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 2342 | __FUNCTION__, listener.get()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2343 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2344 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2345 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2346 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2347 | sp<ServiceListener> serviceListener = |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2348 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, |
| 2349 | openCloseCallbackAllowed); |
Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2350 | auto ret = serviceListener->initialize(isProcessLocalTest); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2351 | if (ret != NO_ERROR) { |
| 2352 | String8 msg = String8::format("Failed to initialize service listener: %s (%d)", |
| 2353 | strerror(-ret), ret); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2354 | logServiceError(msg,ERROR_ILLEGAL_ARGUMENT); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2355 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2356 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
| 2357 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2358 | // The listener still needs to be added to the list of listeners, regardless of what |
| 2359 | // permissions the listener process has / whether it is a vendor listener. Since it might be |
| 2360 | // eligible to listen to other camera ids. |
| 2361 | mListenerList.emplace_back(serviceListener); |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2362 | mUidPolicy->registerMonitorUid(clientUid); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2363 | } |
| 2364 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2365 | /* Collect current devices and status */ |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2366 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2367 | Mutex::Autolock lock(mCameraStatesLock); |
| 2368 | for (auto& i : mCameraStates) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 2369 | cameraStatuses->emplace_back(i.first, |
| 2370 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2371 | } |
| 2372 | } |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2373 | // Remove the camera statuses that should be hidden from the client, we do |
| 2374 | // this after collecting the states in order to avoid holding |
| 2375 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at |
| 2376 | // the same time. |
| 2377 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), |
| 2378 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { |
| 2379 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 2380 | if (getSystemCameraKind(s.cameraId, &deviceKind) != OK) { |
| 2381 | ALOGE("%s: Invalid camera id %s, skipping status update", |
| 2382 | __FUNCTION__, s.cameraId.c_str()); |
| 2383 | return true; |
| 2384 | } |
| 2385 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, |
| 2386 | clientUid);}), cameraStatuses->end()); |
| 2387 | |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2388 | //cameraStatuses will have non-eligible camera ids removed. |
| 2389 | std::set<String16> idsChosenForCallback; |
| 2390 | for (const auto &s : *cameraStatuses) { |
| 2391 | idsChosenForCallback.insert(String16(s.cameraId)); |
| 2392 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2393 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2394 | /* |
| 2395 | * Immediately signal current torch status to this listener only |
| 2396 | * This may be a subset of all the devices, so don't include it in the response directly |
| 2397 | */ |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2398 | { |
| 2399 | Mutex::Autolock al(mTorchStatusMutex); |
| 2400 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2401 | String16 id = String16(mTorchStatusMap.keyAt(i).string()); |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2402 | // The camera id is visible to the client. Fine to send torch |
| 2403 | // callback. |
| 2404 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { |
| 2405 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id); |
| 2406 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2407 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2408 | } |
| 2409 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2410 | return Status::ok(); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2411 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2412 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2413 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2414 | ATRACE_CALL(); |
| 2415 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2416 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 2417 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2418 | if (listener == 0) { |
| 2419 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2420 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2421 | } |
| 2422 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2423 | Mutex::Autolock lock(mServiceLock); |
| 2424 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2425 | { |
| 2426 | Mutex::Autolock lock(mStatusListenerLock); |
| 2427 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2428 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { |
| 2429 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid()); |
| 2430 | IInterface::asBinder(listener)->unlinkToDeath(*it); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2431 | mListenerList.erase(it); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2432 | return Status::ok(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2433 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 2438 | __FUNCTION__, listener.get()); |
| 2439 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2440 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2441 | } |
| 2442 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2443 | Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2444 | |
| 2445 | ATRACE_CALL(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2446 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 2447 | |
| 2448 | if (parameters == NULL) { |
| 2449 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2450 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2451 | } |
| 2452 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2453 | Status ret = Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2454 | |
| 2455 | CameraParameters shimParams; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2456 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2457 | // Error logged by caller |
| 2458 | return ret; |
| 2459 | } |
| 2460 | |
| 2461 | String8 shimParamsString8 = shimParams.flatten(); |
| 2462 | String16 shimParamsString16 = String16(shimParamsString8); |
| 2463 | |
| 2464 | *parameters = shimParamsString16; |
| 2465 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2466 | return ret; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2467 | } |
| 2468 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2469 | Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion, |
| 2470 | /*out*/ bool *isSupported) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2471 | ATRACE_CALL(); |
| 2472 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2473 | const String8 id = String8(cameraId); |
| 2474 | |
| 2475 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2476 | |
| 2477 | switch (apiVersion) { |
| 2478 | case API_VERSION_1: |
| 2479 | case API_VERSION_2: |
| 2480 | break; |
| 2481 | default: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2482 | String8 msg = String8::format("Unknown API version %d", apiVersion); |
| 2483 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2484 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2485 | } |
| 2486 | |
Emilian Peev | 28ad2ea | 2017-02-07 16:14:32 +0000 | [diff] [blame] | 2487 | int deviceVersion = getDeviceVersion(id); |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2488 | switch (deviceVersion) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2489 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 2490 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 2491 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 2492 | if (apiVersion == API_VERSION_2) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2493 | ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim", |
| 2494 | __FUNCTION__, id.string(), deviceVersion); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2495 | *isSupported = false; |
| 2496 | } else { // if (apiVersion == API_VERSION_1) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2497 | ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported", |
| 2498 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2499 | *isSupported = true; |
| 2500 | } |
| 2501 | break; |
| 2502 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 2503 | case CAMERA_DEVICE_API_VERSION_3_3: |
Zhijun He | 4afbdec | 2016-05-04 15:42:51 -0700 | [diff] [blame] | 2504 | case CAMERA_DEVICE_API_VERSION_3_4: |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2505 | case CAMERA_DEVICE_API_VERSION_3_5: |
Yin-Chia Yeh | 56d98ba | 2020-01-16 16:57:14 -0800 | [diff] [blame] | 2506 | case CAMERA_DEVICE_API_VERSION_3_6: |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 2507 | case CAMERA_DEVICE_API_VERSION_3_7: |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2508 | ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly", |
| 2509 | __FUNCTION__, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2510 | *isSupported = true; |
| 2511 | break; |
| 2512 | case -1: { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2513 | String8 msg = String8::format("Unknown camera ID %s", id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2514 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2515 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2516 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2517 | default: { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2518 | String8 msg = String8::format("Unknown device version %x for device %s", |
| 2519 | deviceVersion, id.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2520 | ALOGE("%s: %s", __FUNCTION__, msg.string()); |
| 2521 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string()); |
| 2522 | } |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2523 | } |
| 2524 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2525 | return Status::ok(); |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2526 | } |
| 2527 | |
Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2528 | Status CameraService::isHiddenPhysicalCamera(const String16& cameraId, |
| 2529 | /*out*/ bool *isSupported) { |
| 2530 | ATRACE_CALL(); |
| 2531 | |
| 2532 | const String8 id = String8(cameraId); |
| 2533 | |
| 2534 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); |
| 2535 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(id.string()); |
| 2536 | |
| 2537 | return Status::ok(); |
| 2538 | } |
| 2539 | |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2540 | Status CameraService::injectCamera( |
| 2541 | const String16& packageName, const String16& internalCamId, |
| 2542 | const String16& externalCamId, |
| 2543 | const sp<ICameraInjectionCallback>& callback, |
| 2544 | /*out*/ |
| 2545 | sp<hardware::camera2::ICameraInjectionSession>* cameraInjectionSession) { |
| 2546 | ATRACE_CALL(); |
| 2547 | |
| 2548 | if (!checkCallingPermission(sCameraInjectExternalCameraPermission)) { |
| 2549 | const int pid = CameraThreadState::getCallingPid(); |
| 2550 | const int uid = CameraThreadState::getCallingUid(); |
| 2551 | ALOGE("Permission Denial: can't inject camera pid=%d, uid=%d", pid, uid); |
| 2552 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, |
| 2553 | "Permission Denial: no permission to inject camera"); |
| 2554 | } |
| 2555 | |
| 2556 | ALOGV( |
| 2557 | "%s: Package name = %s, Internal camera ID = %s, External camera ID = " |
| 2558 | "%s", |
| 2559 | __FUNCTION__, String8(packageName).string(), |
| 2560 | String8(internalCamId).string(), String8(externalCamId).string()); |
| 2561 | |
| 2562 | binder::Status ret = binder::Status::ok(); |
| 2563 | // TODO: Implement the injection camera function. |
| 2564 | // ret = internalInjectCamera(...); |
| 2565 | // if(!ret.isOk()) { |
| 2566 | // mInjectionStatusListener->notifyInjectionError(...); |
| 2567 | // return ret; |
| 2568 | // } |
| 2569 | |
| 2570 | mInjectionStatusListener->addListener(callback); |
| 2571 | *cameraInjectionSession = new CameraInjectionSession(this); |
| 2572 | |
| 2573 | return ret; |
| 2574 | } |
| 2575 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2576 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2577 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2578 | for (auto& i : mActiveClientManager.getAll()) { |
| 2579 | auto clientSp = i->getValue(); |
| 2580 | if (clientSp.get() == client) { |
| 2581 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2582 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2583 | } |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2584 | updateAudioRestrictionLocked(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2585 | } |
| 2586 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2587 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2588 | bool ret = false; |
| 2589 | { |
| 2590 | // Acquire mServiceLock and prevent other clients from connecting |
| 2591 | std::unique_ptr<AutoConditionLock> lock = |
| 2592 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2593 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2594 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2595 | std::vector<sp<BasicClient>> evicted; |
| 2596 | for (auto& i : mActiveClientManager.getAll()) { |
| 2597 | auto clientSp = i->getValue(); |
| 2598 | if (clientSp.get() == nullptr) { |
| 2599 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 2600 | mActiveClientManager.remove(i); |
| 2601 | continue; |
| 2602 | } |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 2603 | if (remote == clientSp->getRemote()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2604 | mActiveClientManager.remove(i); |
| 2605 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2606 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2607 | // Notify the client of disconnection |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2608 | clientSp->notifyError( |
| 2609 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2610 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2611 | } |
| 2612 | } |
| 2613 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2614 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 2615 | // other clients from connecting in mServiceLockWrapper if held |
| 2616 | mServiceLock.unlock(); |
| 2617 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2618 | // Do not clear caller identity, remote caller should be client proccess |
| 2619 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2620 | for (auto& i : evicted) { |
| 2621 | if (i.get() != nullptr) { |
| 2622 | i->disconnect(); |
| 2623 | ret = true; |
| 2624 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2625 | } |
| 2626 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2627 | // Reacquire mServiceLock |
| 2628 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2629 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2630 | } // lock is destroyed, allow further connect calls |
| 2631 | |
| 2632 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2635 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
| 2636 | const String8& cameraId) const { |
| 2637 | std::shared_ptr<CameraState> state; |
| 2638 | { |
| 2639 | Mutex::Autolock lock(mCameraStatesLock); |
| 2640 | auto iter = mCameraStates.find(cameraId); |
| 2641 | if (iter != mCameraStates.end()) { |
| 2642 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2643 | } |
| 2644 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2645 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2646 | } |
| 2647 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2648 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) { |
| 2649 | // Remove from active clients list |
| 2650 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 2651 | if (clientDescriptorPtr == nullptr) { |
| 2652 | ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__, |
| 2653 | cameraId.string()); |
| 2654 | return sp<BasicClient>{nullptr}; |
| 2655 | } |
| 2656 | |
| 2657 | return clientDescriptorPtr->getValue(); |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 2658 | } |
| 2659 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2660 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2661 | // Acquire mServiceLock and prevent other clients from connecting |
| 2662 | std::unique_ptr<AutoConditionLock> lock = |
| 2663 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
| 2664 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2665 | std::set<userid_t> newAllowedUsers; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2666 | for (size_t i = 0; i < newUserIds.size(); i++) { |
| 2667 | if (newUserIds[i] < 0) { |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2668 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2669 | __FUNCTION__, newUserIds[i]); |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2670 | return; |
| 2671 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2672 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2673 | } |
| 2674 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2675 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2676 | if (newAllowedUsers == mAllowedUsers) { |
| 2677 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); |
| 2678 | return; |
| 2679 | } |
| 2680 | |
| 2681 | logUserSwitch(mAllowedUsers, newAllowedUsers); |
| 2682 | |
| 2683 | mAllowedUsers = std::move(newAllowedUsers); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2684 | |
| 2685 | // Current user has switched, evict all current clients. |
| 2686 | std::vector<sp<BasicClient>> evicted; |
| 2687 | for (auto& i : mActiveClientManager.getAll()) { |
| 2688 | auto clientSp = i->getValue(); |
| 2689 | |
| 2690 | if (clientSp.get() == nullptr) { |
| 2691 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 2692 | continue; |
| 2693 | } |
| 2694 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2695 | // Don't evict clients that are still allowed. |
| 2696 | uid_t clientUid = clientSp->getClientUid(); |
| 2697 | userid_t clientUserId = multiuser_get_user_id(clientUid); |
| 2698 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { |
| 2699 | continue; |
| 2700 | } |
| 2701 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2702 | evicted.push_back(clientSp); |
| 2703 | |
| 2704 | String8 curTime = getFormattedCurrentTime(); |
| 2705 | |
| 2706 | ALOGE("Evicting conflicting client for camera ID %s due to user change", |
| 2707 | i->getKey().string()); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2708 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2709 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2710 | logEvent(String8::format("EVICT device %s client held by package %s (PID %" |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 2711 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due" |
| 2712 | " to user switch.", i->getKey().string(), |
| 2713 | String8{clientSp->getPackageName()}.string(), |
| 2714 | i->getOwnerId(), i->getPriority().getScore(), |
| 2715 | i->getPriority().getState())); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2716 | |
| 2717 | } |
| 2718 | |
| 2719 | // Do not hold mServiceLock while disconnecting clients, but retain the condition |
| 2720 | // blocking other clients from connecting in mServiceLockWrapper if held. |
| 2721 | mServiceLock.unlock(); |
| 2722 | |
| 2723 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2724 | int64_t token = CameraThreadState::clearCallingIdentity(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2725 | |
| 2726 | for (auto& i : evicted) { |
| 2727 | i->disconnect(); |
| 2728 | } |
| 2729 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2730 | CameraThreadState::restoreCallingIdentity(token); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2731 | |
| 2732 | // Reacquire mServiceLock |
| 2733 | mServiceLock.lock(); |
| 2734 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2735 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2736 | void CameraService::logEvent(const char* event) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2737 | String8 curTime = getFormattedCurrentTime(); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2738 | Mutex::Autolock l(mLogLock); |
Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2739 | String8 msg = String8::format("%s : %s", curTime.string(), event); |
| 2740 | // For service error events, print the msg only once. |
| 2741 | if(!msg.contains("SERVICE ERROR")) { |
| 2742 | mEventLog.add(msg); |
| 2743 | } else if(sServiceErrorEventSet.find(msg) == sServiceErrorEventSet.end()) { |
| 2744 | // Error event not added to the dumpsys log before |
| 2745 | mEventLog.add(msg); |
| 2746 | sServiceErrorEventSet.insert(msg); |
| 2747 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2748 | } |
| 2749 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2750 | void CameraService::logDisconnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2751 | const char* clientPackage) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2752 | // Log the clients evicted |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2753 | 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] | 2754 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2755 | } |
| 2756 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2757 | void CameraService::logDisconnectedOffline(const char* cameraId, int clientPid, |
| 2758 | const char* clientPackage) { |
| 2759 | // Log the clients evicted |
| 2760 | logEvent(String8::format("DISCONNECT offline device %s client for package %s (PID %d)", |
| 2761 | cameraId, clientPackage, clientPid)); |
| 2762 | } |
| 2763 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2764 | void CameraService::logConnected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2765 | const char* clientPackage) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2766 | // Log the clients evicted |
| 2767 | 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] | 2768 | clientPackage, clientPid)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2769 | } |
| 2770 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2771 | void CameraService::logConnectedOffline(const char* cameraId, int clientPid, |
| 2772 | const char* clientPackage) { |
| 2773 | // Log the clients evicted |
| 2774 | logEvent(String8::format("CONNECT offline device %s client for package %s (PID %d)", cameraId, |
| 2775 | clientPackage, clientPid)); |
| 2776 | } |
| 2777 | |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2778 | void CameraService::logRejected(const char* cameraId, int clientPid, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2779 | const char* clientPackage, const char* reason) { |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2780 | // Log the client rejected |
| 2781 | 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] | 2782 | cameraId, clientPackage, clientPid, reason)); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2783 | } |
| 2784 | |
Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 2785 | void CameraService::logTorchEvent(const char* cameraId, const char *torchState, int clientPid) { |
| 2786 | // Log torch event |
| 2787 | logEvent(String8::format("Torch for camera id %s turned %s for client PID %d", cameraId, |
| 2788 | torchState, clientPid)); |
| 2789 | } |
| 2790 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 2791 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, |
| 2792 | const std::set<userid_t>& newUserIds) { |
| 2793 | String8 newUsers = toString(newUserIds); |
| 2794 | String8 oldUsers = toString(oldUserIds); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 2795 | if (oldUsers.size() == 0) { |
| 2796 | oldUsers = "<None>"; |
| 2797 | } |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2798 | // Log the new and old users |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 2799 | 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] | 2800 | oldUsers.string(), newUsers.string())); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) { |
| 2804 | // Log the device removal |
| 2805 | logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason)); |
| 2806 | } |
| 2807 | |
| 2808 | void CameraService::logDeviceAdded(const char* cameraId, const char* reason) { |
| 2809 | // Log the device removal |
| 2810 | logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason)); |
| 2811 | } |
| 2812 | |
| 2813 | void CameraService::logClientDied(int clientPid, const char* reason) { |
| 2814 | // Log the device removal |
| 2815 | 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] | 2816 | } |
| 2817 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 2818 | void CameraService::logServiceError(const char* msg, int errorCode) { |
| 2819 | String8 curTime = getFormattedCurrentTime(); |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 2820 | 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] | 2821 | } |
| 2822 | |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2823 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 2824 | uint32_t flags) { |
| 2825 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2826 | // Permission checks |
| 2827 | switch (code) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 2828 | case SHELL_COMMAND_TRANSACTION: { |
| 2829 | int in = data.readFileDescriptor(); |
| 2830 | int out = data.readFileDescriptor(); |
| 2831 | int err = data.readFileDescriptor(); |
| 2832 | int argc = data.readInt32(); |
| 2833 | Vector<String16> args; |
| 2834 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 2835 | args.add(data.readString16()); |
| 2836 | } |
| 2837 | sp<IBinder> unusedCallback; |
| 2838 | sp<IResultReceiver> resultReceiver; |
| 2839 | status_t status; |
| 2840 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 2841 | return status; |
| 2842 | } |
| 2843 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 2844 | return status; |
| 2845 | } |
| 2846 | status = shellCommand(in, out, err, args); |
| 2847 | if (resultReceiver != nullptr) { |
| 2848 | resultReceiver->send(status); |
| 2849 | } |
| 2850 | return NO_ERROR; |
| 2851 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | return BnCameraService::onTransact(code, data, reply, flags); |
| 2855 | } |
| 2856 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2857 | // We share the media players for shutter and recording sound for all clients. |
| 2858 | // A reference count is kept to determine when we will actually release the |
| 2859 | // media players. |
| 2860 | |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 2861 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { |
| 2862 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 2863 | status_t error; |
| 2864 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 2865 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 2866 | error = mp->prepare(); |
| 2867 | } |
| 2868 | if (error != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2869 | ALOGE("Failed to load CameraService sounds: %s", file); |
Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 2870 | mp->disconnect(); |
| 2871 | mp.clear(); |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 2872 | return nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2873 | } |
| 2874 | return mp; |
| 2875 | } |
| 2876 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2877 | void CameraService::increaseSoundRef() { |
| 2878 | Mutex::Autolock lock(mSoundLock); |
| 2879 | mSoundRef++; |
| 2880 | } |
| 2881 | |
| 2882 | void CameraService::loadSoundLocked(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2883 | ATRACE_CALL(); |
| 2884 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2885 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); |
| 2886 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { |
| 2887 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); |
| 2888 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { |
| 2889 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 2890 | } |
| 2891 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) { |
| 2892 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); |
| 2893 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { |
| 2894 | mSoundPlayer[SOUND_RECORDING_START] = |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 2895 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2896 | } |
| 2897 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { |
| 2898 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); |
| 2899 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { |
| 2900 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); |
| 2901 | } |
Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 2902 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2903 | } |
| 2904 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2905 | void CameraService::decreaseSoundRef() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2906 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2907 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2908 | if (--mSoundRef) return; |
| 2909 | |
| 2910 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 2911 | if (mSoundPlayer[i] != 0) { |
| 2912 | mSoundPlayer[i]->disconnect(); |
| 2913 | mSoundPlayer[i].clear(); |
| 2914 | } |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | void CameraService::playSound(sound_kind kind) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2919 | ATRACE_CALL(); |
| 2920 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2921 | LOG1("playSound(%d)", kind); |
Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame] | 2922 | if (kind < 0 || kind >= NUM_SOUNDS) { |
| 2923 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); |
| 2924 | return; |
| 2925 | } |
| 2926 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2927 | Mutex::Autolock lock(mSoundLock); |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2928 | loadSoundLocked(kind); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2929 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 2930 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 2931 | player->seekTo(0); |
| 2932 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | // ---------------------------------------------------------------------------- |
| 2937 | |
| 2938 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 2939 | const sp<ICameraClient>& cameraClient, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 2940 | const String16& clientPackageName, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 2941 | const std::optional<String16>& clientFeatureId, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2942 | const String8& cameraIdStr, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2943 | int api1CameraId, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2944 | int clientPid, uid_t clientUid, |
| 2945 | int servicePid) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 2946 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 2947 | IInterface::asBinder(cameraClient), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 2948 | clientPackageName, clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2949 | cameraIdStr, cameraFacing, sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2950 | clientPid, clientUid, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2951 | servicePid), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 2952 | mCameraId(api1CameraId) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2953 | { |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2954 | int callingPid = CameraThreadState::getCallingPid(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2955 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2956 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 2957 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2958 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2959 | cameraService->increaseSoundRef(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2960 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2961 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2962 | } |
| 2963 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2964 | // tear down the client |
| 2965 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 2966 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2967 | mDestructionStarted = true; |
| 2968 | |
username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 2969 | sCameraService->decreaseSoundRef(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 2970 | // unconditionally disconnect. function is idempotent |
| 2971 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2972 | } |
| 2973 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2974 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; |
| 2975 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2976 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2977 | const sp<IBinder>& remoteCallback, |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 2978 | const String16& clientPackageName, const std::optional<String16>& clientFeatureId, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2979 | const String8& cameraIdStr, int cameraFacing, int sensorOrientation, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 2980 | int clientPid, uid_t clientUid, |
| 2981 | int servicePid): |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 2982 | mDestructionStarted(false), |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2983 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), mOrientation(sensorOrientation), |
Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 2984 | mClientPackageName(clientPackageName), mClientFeatureId(clientFeatureId), |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 2985 | mClientPid(clientPid), mClientUid(clientUid), |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2986 | mServicePid(servicePid), |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 2987 | mDisconnected(false), mUidIsTrusted(false), |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2988 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 2989 | mRemoteBinder(remoteCallback), |
| 2990 | mOpsActive(false), |
| 2991 | mOpsStreaming(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2992 | { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2993 | if (sCameraService == nullptr) { |
| 2994 | sCameraService = cameraService; |
| 2995 | } |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 2996 | |
| 2997 | // In some cases the calling code has no access to the package it runs under. |
| 2998 | // For example, NDK camera API. |
| 2999 | // In this case we will get the packages for the calling UID and pick the first one |
| 3000 | // for attributing the app op. This will work correctly for runtime permissions |
| 3001 | // as for legacy apps we will toggle the app op for all packages in the UID. |
| 3002 | // The caveat is that the operation may be attributed to the wrong package and |
| 3003 | // stats based on app ops may be slightly off. |
| 3004 | if (mClientPackageName.size() <= 0) { |
| 3005 | sp<IServiceManager> sm = defaultServiceManager(); |
| 3006 | sp<IBinder> binder = sm->getService(String16(kPermissionServiceName)); |
| 3007 | if (binder == 0) { |
| 3008 | ALOGE("Cannot get permission service"); |
| 3009 | // Leave mClientPackageName unchanged (empty) and the further interaction |
| 3010 | // with camera will fail in BasicClient::startCameraOps |
| 3011 | return; |
| 3012 | } |
| 3013 | |
| 3014 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 3015 | Vector<String16> packages; |
| 3016 | |
| 3017 | permCtrl->getPackagesForUid(mClientUid, packages); |
| 3018 | |
| 3019 | if (packages.isEmpty()) { |
| 3020 | ALOGE("No packages for calling UID"); |
| 3021 | // Leave mClientPackageName unchanged (empty) and the further interaction |
| 3022 | // with camera will fail in BasicClient::startCameraOps |
| 3023 | return; |
| 3024 | } |
| 3025 | mClientPackageName = packages[0]; |
| 3026 | } |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 3027 | if (getCurrentServingCall() != BinderCallType::HWBINDER) { |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3028 | mAppOpsManager = std::make_unique<AppOpsManager>(); |
| 3029 | } |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3030 | |
| 3031 | mUidIsTrusted = isTrustedCallingUid(mClientUid); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3035 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3036 | mDestructionStarted = true; |
| 3037 | } |
| 3038 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3039 | binder::Status CameraService::BasicClient::disconnect() { |
| 3040 | binder::Status res = Status::ok(); |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3041 | if (mDisconnected) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3042 | return res; |
Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3043 | } |
Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 3044 | mDisconnected = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3045 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3046 | sCameraService->removeByClient(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3047 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, String8(mClientPackageName)); |
Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 3048 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, |
| 3049 | mCameraIdStr.c_str()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3050 | |
| 3051 | sp<IBinder> remote = getRemote(); |
| 3052 | if (remote != nullptr) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3053 | remote->unlinkToDeath(sCameraService); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3054 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3055 | |
| 3056 | finishCameraOps(); |
Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 3057 | // Notify flashlight that a camera device is closed. |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3058 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); |
| 3059 | ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(), |
| 3060 | mClientPid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3061 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3062 | // client shouldn't be able to call into us anymore |
| 3063 | mClientPid = 0; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3064 | |
| 3065 | return res; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3066 | } |
| 3067 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3068 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { |
| 3069 | // No dumping of clients directly over Binder, |
| 3070 | // must go through CameraService::dump |
| 3071 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3072 | CameraThreadState::getCallingUid(), NULL, 0); |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3073 | return OK; |
| 3074 | } |
| 3075 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3076 | String16 CameraService::BasicClient::getPackageName() const { |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3077 | return mClientPackageName; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3078 | } |
| 3079 | |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3080 | int CameraService::BasicClient::getCameraFacing() const { |
| 3081 | return mCameraFacing; |
| 3082 | } |
| 3083 | |
| 3084 | int CameraService::BasicClient::getCameraOrientation() const { |
| 3085 | return mOrientation; |
| 3086 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3087 | |
| 3088 | int CameraService::BasicClient::getClientPid() const { |
| 3089 | return mClientPid; |
| 3090 | } |
| 3091 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3092 | uid_t CameraService::BasicClient::getClientUid() const { |
| 3093 | return mClientUid; |
| 3094 | } |
| 3095 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3096 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { |
| 3097 | // Defaults to API2. |
| 3098 | return level == API_2; |
| 3099 | } |
| 3100 | |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3101 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3102 | { |
| 3103 | Mutex::Autolock l(mAudioRestrictionLock); |
| 3104 | mAudioRestriction = mode; |
| 3105 | } |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3106 | sCameraService->updateAudioRestriction(); |
| 3107 | return OK; |
| 3108 | } |
| 3109 | |
| 3110 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3111 | return sCameraService->updateAudioRestriction(); |
| 3112 | } |
| 3113 | |
| 3114 | int32_t CameraService::BasicClient::getAudioRestriction() const { |
| 3115 | Mutex::Autolock l(mAudioRestrictionLock); |
| 3116 | return mAudioRestriction; |
| 3117 | } |
| 3118 | |
| 3119 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { |
| 3120 | switch (mode) { |
| 3121 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: |
| 3122 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: |
| 3123 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: |
| 3124 | return true; |
| 3125 | default: |
| 3126 | return false; |
| 3127 | } |
| 3128 | } |
| 3129 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3130 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { |
| 3131 | if (mode == AppOpsManager::MODE_ERRORED) { |
| 3132 | ALOGI("Camera %s: Access for \"%s\" has been revoked", |
| 3133 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 3134 | return PERMISSION_DENIED; |
| 3135 | } else if (!mUidIsTrusted && mode == AppOpsManager::MODE_IGNORED) { |
| 3136 | // If the calling Uid is trusted (a native service), the AppOpsManager could |
| 3137 | // return MODE_IGNORED. Do not treat such case as error. |
| 3138 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, |
| 3139 | mClientPackageName); |
| 3140 | bool isCameraPrivacyEnabled = |
| 3141 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled( |
| 3142 | multiuser_get_user_id(mClientUid)); |
| 3143 | if (!isUidActive || !isCameraPrivacyEnabled) { |
| 3144 | ALOGI("Camera %s: Access for \"%s\" has been restricted", |
| 3145 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 3146 | // Return the same error as for device policy manager rejection |
| 3147 | return -EACCES; |
| 3148 | } |
| 3149 | } |
| 3150 | return OK; |
| 3151 | } |
| 3152 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3153 | status_t CameraService::BasicClient::startCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3154 | ATRACE_CALL(); |
| 3155 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3156 | { |
| 3157 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3158 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3159 | } |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3160 | if (mAppOpsManager != nullptr) { |
| 3161 | // Notify app ops that the camera is not available |
| 3162 | mOpsCallback = new OpsCallback(this); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3163 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 3164 | mClientPackageName, mOpsCallback); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3165 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3166 | // Just check for camera acccess here on open - delay startOp until |
| 3167 | // camera frames start streaming in startCameraStreamingOps |
| 3168 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3169 | mClientPackageName); |
| 3170 | status_t res = handleAppOpMode(mode); |
| 3171 | if (res != OK) { |
| 3172 | return res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3173 | } |
Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 3174 | } |
| 3175 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3176 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3177 | |
| 3178 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3179 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3180 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3181 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid); |
| 3182 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3183 | // Notify listeners of camera open/close status |
| 3184 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); |
| 3185 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3186 | return OK; |
| 3187 | } |
| 3188 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3189 | status_t CameraService::BasicClient::startCameraStreamingOps() { |
| 3190 | ATRACE_CALL(); |
| 3191 | |
| 3192 | if (!mOpsActive) { |
| 3193 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3194 | return INVALID_OPERATION; |
| 3195 | } |
| 3196 | if (mOpsStreaming) { |
| 3197 | ALOGV("%s: Streaming already active!", __FUNCTION__); |
| 3198 | return OK; |
| 3199 | } |
| 3200 | |
| 3201 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", |
| 3202 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3203 | |
| 3204 | if (mAppOpsManager != nullptr) { |
| 3205 | int32_t mode = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, |
| 3206 | mClientPackageName, /*startIfModeDefault*/ false, mClientFeatureId, |
| 3207 | String16("start camera ") + String16(mCameraIdStr)); |
| 3208 | status_t res = handleAppOpMode(mode); |
| 3209 | if (res != OK) { |
| 3210 | return res; |
| 3211 | } |
| 3212 | } |
| 3213 | |
| 3214 | mOpsStreaming = true; |
| 3215 | |
| 3216 | return OK; |
| 3217 | } |
| 3218 | |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame^] | 3219 | status_t CameraService::BasicClient::noteAppOp() { |
| 3220 | ATRACE_CALL(); |
| 3221 | |
| 3222 | ALOGV("%s: Start camera noteAppOp, package name = %s, client UID = %d", |
| 3223 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 3224 | |
| 3225 | // noteAppOp is only used for when camera mute is not supported, in order |
| 3226 | // to trigger the sensor privacy "Unblock" dialog |
| 3227 | if (mAppOpsManager != nullptr) { |
| 3228 | int32_t mode = mAppOpsManager->noteOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3229 | mClientPackageName, mClientFeatureId, |
| 3230 | String16("start camera ") + String16(mCameraIdStr)); |
| 3231 | status_t res = handleAppOpMode(mode); |
| 3232 | if (res != OK) { |
| 3233 | return res; |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | return OK; |
| 3238 | } |
| 3239 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3240 | status_t CameraService::BasicClient::finishCameraStreamingOps() { |
| 3241 | ATRACE_CALL(); |
| 3242 | |
| 3243 | if (!mOpsActive) { |
| 3244 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); |
| 3245 | return INVALID_OPERATION; |
| 3246 | } |
| 3247 | if (!mOpsStreaming) { |
| 3248 | ALOGV("%s: Streaming not active!", __FUNCTION__); |
| 3249 | return OK; |
| 3250 | } |
| 3251 | |
| 3252 | if (mAppOpsManager != nullptr) { |
| 3253 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 3254 | mClientPackageName, mClientFeatureId); |
| 3255 | mOpsStreaming = false; |
| 3256 | } |
| 3257 | |
| 3258 | return OK; |
| 3259 | } |
| 3260 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3261 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3262 | ATRACE_CALL(); |
| 3263 | |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3264 | if (mOpsStreaming) { |
| 3265 | // Make sure we've notified everyone about camera stopping |
| 3266 | finishCameraStreamingOps(); |
| 3267 | } |
| 3268 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3269 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3270 | if (mOpsActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3271 | mOpsActive = false; |
| 3272 | |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3273 | // This function is called when a client disconnects. This should |
| 3274 | // release the camera, but actually only if it was in a proper |
| 3275 | // functional state, i.e. with status NOT_AVAILABLE |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3276 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, |
Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3277 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3278 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3279 | // 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] | 3280 | sCameraService->updateStatus(StatusInternal::PRESENT, |
| 3281 | mCameraIdStr, rejected); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3282 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3283 | // Always stop watching, even if no camera op is active |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3284 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 3285 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3286 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3287 | mOpsCallback.clear(); |
| 3288 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3289 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid); |
| 3290 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3291 | // Notify listeners of camera open/close status |
| 3292 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); |
| 3293 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3294 | return OK; |
| 3295 | } |
| 3296 | |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3297 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3298 | ATRACE_CALL(); |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3299 | if (mAppOpsManager == nullptr) { |
| 3300 | return; |
| 3301 | } |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3302 | // TODO : add offline camera session case |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3303 | if (op != AppOpsManager::OP_CAMERA) { |
| 3304 | ALOGW("Unexpected app ops notification received: %d", op); |
| 3305 | return; |
| 3306 | } |
| 3307 | |
| 3308 | int32_t res; |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3309 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3310 | mClientUid, mClientPackageName); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3311 | ALOGV("checkOp returns: %d, %s ", res, |
| 3312 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 3313 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 3314 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 3315 | "UNKNOWN"); |
| 3316 | |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3317 | if (res == AppOpsManager::MODE_ERRORED) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3318 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(), |
Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3319 | String8(mClientPackageName).string()); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3320 | block(); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3321 | } else if (res == AppOpsManager::MODE_IGNORED) { |
| 3322 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3323 | bool isCameraPrivacyEnabled = |
| 3324 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled( |
| 3325 | multiuser_get_user_id(mClientUid)); |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3326 | ALOGI("Camera %s: Access for \"%s\" has been restricted, isUidTrusted %d, isUidActive %d", |
| 3327 | mCameraIdStr.string(), String8(mClientPackageName).string(), |
| 3328 | mUidIsTrusted, isUidActive); |
| 3329 | // If the calling Uid is trusted (a native service), or the client Uid is active (WAR for |
| 3330 | // b/175320666), the AppOpsManager could return MODE_IGNORED. Do not treat such cases as |
| 3331 | // error. |
Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame^] | 3332 | if (!mUidIsTrusted) { |
| 3333 | if (isUidActive && isCameraPrivacyEnabled && supportsCameraMute()) { |
| 3334 | setCameraMute(true); |
| 3335 | } else if (!isUidActive |
| 3336 | || (isCameraPrivacyEnabled && !supportsCameraMute())) { |
| 3337 | block(); |
| 3338 | } |
Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3339 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3340 | } else if (res == AppOpsManager::MODE_ALLOWED) { |
| 3341 | setCameraMute(sCameraService->mOverrideCameraMuteMode); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3342 | } |
| 3343 | } |
| 3344 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3345 | void CameraService::BasicClient::block() { |
| 3346 | ATRACE_CALL(); |
| 3347 | |
| 3348 | // Reset the client PID to allow server-initiated disconnect, |
| 3349 | // and to prevent further calls by client. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3350 | mClientPid = CameraThreadState::getCallingPid(); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3351 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 3352 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); |
| 3353 | disconnect(); |
| 3354 | } |
| 3355 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3356 | // ---------------------------------------------------------------------------- |
| 3357 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3358 | void CameraService::Client::notifyError(int32_t errorCode, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3359 | const CaptureResultExtras& resultExtras) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 3360 | (void) resultExtras; |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3361 | if (mRemoteCallback != NULL) { |
Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 3362 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; |
| 3363 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { |
| 3364 | api1ErrorCode = CAMERA_ERROR_DISABLED; |
| 3365 | } |
| 3366 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); |
Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3367 | } else { |
| 3368 | ALOGE("mRemoteCallback is NULL!!"); |
| 3369 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3370 | } |
| 3371 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3372 | // NOTE: function is idempotent |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3373 | binder::Status CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3374 | ALOGV("Client::disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3375 | return BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 3376 | } |
| 3377 | |
Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3378 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { |
| 3379 | return level == API_1; |
| 3380 | } |
| 3381 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3382 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 3383 | mClient(client) { |
| 3384 | } |
| 3385 | |
| 3386 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 3387 | const String16& packageName) { |
| 3388 | sp<BasicClient> client = mClient.promote(); |
| 3389 | if (client != NULL) { |
| 3390 | client->opChanged(op, packageName); |
| 3391 | } |
| 3392 | } |
| 3393 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3394 | // ---------------------------------------------------------------------------- |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3395 | // UidPolicy |
| 3396 | // ---------------------------------------------------------------------------- |
| 3397 | |
| 3398 | void CameraService::UidPolicy::registerSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3399 | Mutex::Autolock _l(mUidLock); |
| 3400 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3401 | if (mRegistered) return; |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3402 | status_t res = mAm.linkToDeath(this); |
| 3403 | mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3404 | | ActivityManager::UID_OBSERVER_IDLE |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3405 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE, |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3406 | ActivityManager::PROCESS_STATE_UNKNOWN, |
| 3407 | String16("cameraserver")); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3408 | if (res == OK) { |
| 3409 | mRegistered = true; |
| 3410 | ALOGV("UidPolicy: Registered with ActivityManager"); |
| 3411 | } |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | void CameraService::UidPolicy::unregisterSelf() { |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3415 | Mutex::Autolock _l(mUidLock); |
| 3416 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3417 | mAm.unregisterUidObserver(this); |
| 3418 | mAm.unlinkToDeath(this); |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3419 | mRegistered = false; |
| 3420 | mActiveUids.clear(); |
| 3421 | ALOGV("UidPolicy: Unregistered with ActivityManager"); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3422 | } |
| 3423 | |
| 3424 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { |
| 3425 | onUidIdle(uid, disabled); |
| 3426 | } |
| 3427 | |
| 3428 | void CameraService::UidPolicy::onUidActive(uid_t uid) { |
| 3429 | Mutex::Autolock _l(mUidLock); |
| 3430 | mActiveUids.insert(uid); |
| 3431 | } |
| 3432 | |
| 3433 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { |
| 3434 | bool deleted = false; |
| 3435 | { |
| 3436 | Mutex::Autolock _l(mUidLock); |
| 3437 | if (mActiveUids.erase(uid) > 0) { |
| 3438 | deleted = true; |
| 3439 | } |
| 3440 | } |
| 3441 | if (deleted) { |
| 3442 | sp<CameraService> service = mService.promote(); |
| 3443 | if (service != nullptr) { |
| 3444 | service->blockClientsForUid(uid); |
| 3445 | } |
| 3446 | } |
| 3447 | } |
| 3448 | |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3449 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 3450 | int64_t procStateSeq __unused, int32_t capability __unused) { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3451 | bool procStateChange = false; |
| 3452 | { |
| 3453 | Mutex::Autolock _l(mUidLock); |
| 3454 | if ((mMonitoredUids.find(uid) != mMonitoredUids.end()) && |
| 3455 | (mMonitoredUids[uid].first != procState)) { |
| 3456 | mMonitoredUids[uid].first = procState; |
| 3457 | procStateChange = true; |
| 3458 | } |
| 3459 | } |
| 3460 | |
| 3461 | if (procStateChange) { |
| 3462 | sp<CameraService> service = mService.promote(); |
| 3463 | if (service != nullptr) { |
| 3464 | service->notifyMonitoredUids(); |
| 3465 | } |
| 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid) { |
| 3470 | Mutex::Autolock _l(mUidLock); |
| 3471 | auto it = mMonitoredUids.find(uid); |
| 3472 | if (it != mMonitoredUids.end()) { |
| 3473 | it->second.second++; |
| 3474 | } else { |
| 3475 | mMonitoredUids.emplace( |
| 3476 | std::pair<uid_t, std::pair<int32_t, size_t>> (uid, |
| 3477 | std::pair<int32_t, size_t> (ActivityManager::PROCESS_STATE_NONEXISTENT, 1))); |
| 3478 | } |
| 3479 | } |
| 3480 | |
| 3481 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid) { |
| 3482 | Mutex::Autolock _l(mUidLock); |
| 3483 | auto it = mMonitoredUids.find(uid); |
| 3484 | if (it != mMonitoredUids.end()) { |
| 3485 | it->second.second--; |
| 3486 | if (it->second.second == 0) { |
| 3487 | mMonitoredUids.erase(it); |
| 3488 | } |
| 3489 | } else { |
| 3490 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); |
| 3491 | } |
| 3492 | } |
| 3493 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3494 | bool CameraService::UidPolicy::isUidActive(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3495 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3496 | return isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3497 | } |
| 3498 | |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3499 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; |
| 3500 | static const int64_t kPollUidActiveTimeoutMillis = 50; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3501 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3502 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, String16 callingPackage) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3503 | // Non-app UIDs are considered always active |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3504 | // If activity manager is unreachable, assume everything is active |
| 3505 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3506 | return true; |
| 3507 | } |
| 3508 | auto it = mOverrideUids.find(uid); |
| 3509 | if (it != mOverrideUids.end()) { |
| 3510 | return it->second; |
| 3511 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3512 | bool active = mActiveUids.find(uid) != mActiveUids.end(); |
| 3513 | if (!active) { |
| 3514 | // We want active UIDs to always access camera with their first attempt since |
| 3515 | // there is no guarantee the app is robustly written and would retry getting |
| 3516 | // the camera on failure. The inverse case is not a problem as we would take |
| 3517 | // camera away soon once we get the callback that the uid is no longer active. |
| 3518 | ActivityManager am; |
| 3519 | // Okay to access with a lock held as UID changes are dispatched without |
| 3520 | // a lock and we are a higher level component. |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3521 | int64_t startTimeMillis = 0; |
| 3522 | do { |
| 3523 | // TODO: Fix this b/109950150! |
| 3524 | // Okay this is a hack. There is a race between the UID turning active and |
| 3525 | // activity being resumed. The proper fix is very risky, so we temporary add |
| 3526 | // some polling which should happen pretty rarely anyway as the race is hard |
| 3527 | // to hit. |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3528 | active = mActiveUids.find(uid) != mActiveUids.end(); |
Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 3529 | if (!active) active = am.isUidActive(uid, callingPackage); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3530 | if (active) { |
| 3531 | break; |
| 3532 | } |
| 3533 | if (startTimeMillis <= 0) { |
| 3534 | startTimeMillis = uptimeMillis(); |
| 3535 | } |
| 3536 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3537 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3538 | if (remainingTimeMillis <= 0) { |
| 3539 | break; |
| 3540 | } |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3541 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); |
| 3542 | |
| 3543 | mUidLock.unlock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3544 | usleep(remainingTimeMillis * 1000); |
Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3545 | mUidLock.lock(); |
Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3546 | } while (true); |
| 3547 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3548 | if (active) { |
| 3549 | // Now that we found out the UID is actually active, cache that |
| 3550 | mActiveUids.insert(uid); |
| 3551 | } |
| 3552 | } |
| 3553 | return active; |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3554 | } |
| 3555 | |
Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 3556 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { |
| 3557 | Mutex::Autolock _l(mUidLock); |
| 3558 | return getProcStateLocked(uid); |
| 3559 | } |
| 3560 | |
| 3561 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { |
| 3562 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 3563 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { |
| 3564 | procState = mMonitoredUids[uid].first; |
| 3565 | } |
| 3566 | return procState; |
| 3567 | } |
| 3568 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3569 | void CameraService::UidPolicy::UidPolicy::addOverrideUid(uid_t uid, |
| 3570 | String16 callingPackage, bool active) { |
| 3571 | updateOverrideUid(uid, callingPackage, active, true); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3572 | } |
| 3573 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3574 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, String16 callingPackage) { |
| 3575 | updateOverrideUid(uid, callingPackage, false, false); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3576 | } |
| 3577 | |
Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3578 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 3579 | Mutex::Autolock _l(mUidLock); |
| 3580 | ALOGV("UidPolicy: ActivityManager has died"); |
| 3581 | mRegistered = false; |
| 3582 | mActiveUids.clear(); |
| 3583 | } |
| 3584 | |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3585 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, String16 callingPackage, |
| 3586 | bool active, bool insert) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3587 | bool wasActive = false; |
| 3588 | bool isActive = false; |
| 3589 | { |
| 3590 | Mutex::Autolock _l(mUidLock); |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3591 | wasActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3592 | mOverrideUids.erase(uid); |
| 3593 | if (insert) { |
| 3594 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 3595 | } |
Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3596 | isActive = isUidActiveLocked(uid, callingPackage); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3597 | } |
| 3598 | if (wasActive != isActive && !isActive) { |
| 3599 | sp<CameraService> service = mService.promote(); |
| 3600 | if (service != nullptr) { |
| 3601 | service->blockClientsForUid(uid); |
| 3602 | } |
| 3603 | } |
| 3604 | } |
| 3605 | |
| 3606 | // ---------------------------------------------------------------------------- |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3607 | // SensorPrivacyPolicy |
| 3608 | // ---------------------------------------------------------------------------- |
| 3609 | void CameraService::SensorPrivacyPolicy::registerSelf() { |
| 3610 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3611 | if (mRegistered) { |
| 3612 | return; |
| 3613 | } |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3614 | hasCameraPrivacyFeature(); // Called so the result is cached |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 3615 | mSpm.addSensorPrivacyListener(this); |
| 3616 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); |
| 3617 | status_t res = mSpm.linkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3618 | if (res == OK) { |
| 3619 | mRegistered = true; |
| 3620 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); |
| 3621 | } |
| 3622 | } |
| 3623 | |
| 3624 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { |
| 3625 | Mutex::Autolock _l(mSensorPrivacyLock); |
Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 3626 | mSpm.removeSensorPrivacyListener(this); |
| 3627 | mSpm.unlinkToDeath(this); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3628 | mRegistered = false; |
| 3629 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); |
| 3630 | } |
| 3631 | |
| 3632 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
| 3633 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3634 | return mSensorPrivacyEnabled; |
| 3635 | } |
| 3636 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3637 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled(userid_t userId) { |
| 3638 | if (!hasCameraPrivacyFeature()) { |
| 3639 | return false; |
| 3640 | } |
| 3641 | return mSpm.isIndividualSensorPrivacyEnabled(userId, |
| 3642 | SensorPrivacyManager::INDIVIDUAL_SENSOR_CAMERA); |
| 3643 | } |
| 3644 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3645 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) { |
| 3646 | { |
| 3647 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3648 | mSensorPrivacyEnabled = enabled; |
| 3649 | } |
| 3650 | // 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] | 3651 | if (enabled) { |
| 3652 | sp<CameraService> service = mService.promote(); |
| 3653 | if (service != nullptr) { |
| 3654 | service->blockAllClients(); |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3655 | } |
| 3656 | } |
| 3657 | return binder::Status::ok(); |
| 3658 | } |
| 3659 | |
| 3660 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { |
| 3661 | Mutex::Autolock _l(mSensorPrivacyLock); |
| 3662 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); |
| 3663 | mRegistered = false; |
| 3664 | } |
| 3665 | |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3666 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { |
Evan Severson | 671806e | 2021-04-20 16:44:53 -0700 | [diff] [blame] | 3667 | return mSpm.supportsSensorToggle(SensorPrivacyManager::INDIVIDUAL_SENSOR_CAMERA); |
Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3668 | } |
| 3669 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 3670 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3671 | // CameraState |
| 3672 | // ---------------------------------------------------------------------------- |
| 3673 | |
| 3674 | CameraService::CameraState::CameraState(const String8& id, int cost, |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3675 | const std::set<String8>& conflicting, SystemCameraKind systemCameraKind) : mId(id), |
| 3676 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), |
| 3677 | mSystemCameraKind(systemCameraKind) {} |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3678 | |
| 3679 | CameraService::CameraState::~CameraState() {} |
| 3680 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3681 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3682 | Mutex::Autolock lock(mStatusLock); |
| 3683 | return mStatus; |
| 3684 | } |
| 3685 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3686 | std::vector<String8> CameraService::CameraState::getUnavailablePhysicalIds() const { |
| 3687 | Mutex::Autolock lock(mStatusLock); |
| 3688 | std::vector<String8> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); |
| 3689 | return res; |
| 3690 | } |
| 3691 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3692 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 3693 | return mShimParams; |
| 3694 | } |
| 3695 | |
| 3696 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 3697 | mShimParams = params; |
| 3698 | } |
| 3699 | |
| 3700 | int CameraService::CameraState::getCost() const { |
| 3701 | return mCost; |
| 3702 | } |
| 3703 | |
| 3704 | std::set<String8> CameraService::CameraState::getConflicting() const { |
| 3705 | return mConflicting; |
| 3706 | } |
| 3707 | |
| 3708 | String8 CameraService::CameraState::getId() const { |
| 3709 | return mId; |
| 3710 | } |
| 3711 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 3712 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { |
| 3713 | return mSystemCameraKind; |
| 3714 | } |
| 3715 | |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 3716 | bool CameraService::CameraState::addUnavailablePhysicalId(const String8& physicalId) { |
| 3717 | Mutex::Autolock lock(mStatusLock); |
| 3718 | auto result = mUnavailablePhysicalIds.insert(physicalId); |
| 3719 | return result.second; |
| 3720 | } |
| 3721 | |
| 3722 | bool CameraService::CameraState::removeUnavailablePhysicalId(const String8& physicalId) { |
| 3723 | Mutex::Autolock lock(mStatusLock); |
| 3724 | auto count = mUnavailablePhysicalIds.erase(physicalId); |
| 3725 | return count > 0; |
| 3726 | } |
| 3727 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3728 | // ---------------------------------------------------------------------------- |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3729 | // ClientEventListener |
| 3730 | // ---------------------------------------------------------------------------- |
| 3731 | |
| 3732 | void CameraService::ClientEventListener::onClientAdded( |
| 3733 | const resource_policy::ClientDescriptor<String8, |
| 3734 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 3735 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3736 | if (basicClient.get() != nullptr) { |
| 3737 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 3738 | notifier.noteStartCamera(descriptor.getKey(), |
| 3739 | static_cast<int>(basicClient->getClientUid())); |
| 3740 | } |
| 3741 | } |
| 3742 | |
| 3743 | void CameraService::ClientEventListener::onClientRemoved( |
| 3744 | const resource_policy::ClientDescriptor<String8, |
| 3745 | sp<CameraService::BasicClient>>& descriptor) { |
Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 3746 | const auto& basicClient = descriptor.getValue(); |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3747 | if (basicClient.get() != nullptr) { |
| 3748 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); |
| 3749 | notifier.noteStopCamera(descriptor.getKey(), |
| 3750 | static_cast<int>(basicClient->getClientUid())); |
| 3751 | } |
| 3752 | } |
| 3753 | |
| 3754 | |
| 3755 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3756 | // CameraClientManager |
| 3757 | // ---------------------------------------------------------------------------- |
| 3758 | |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 3759 | CameraService::CameraClientManager::CameraClientManager() { |
| 3760 | setListener(std::make_shared<ClientEventListener>()); |
| 3761 | } |
| 3762 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3763 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 3764 | |
| 3765 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
| 3766 | const String8& id) const { |
| 3767 | auto descriptor = get(id); |
| 3768 | if (descriptor == nullptr) { |
| 3769 | return sp<BasicClient>{nullptr}; |
| 3770 | } |
| 3771 | return descriptor->getValue(); |
| 3772 | } |
| 3773 | |
| 3774 | String8 CameraService::CameraClientManager::toString() const { |
| 3775 | auto all = getAll(); |
| 3776 | String8 ret("["); |
| 3777 | bool hasAny = false; |
| 3778 | for (auto& i : all) { |
| 3779 | hasAny = true; |
| 3780 | String8 key = i->getKey(); |
| 3781 | int32_t cost = i->getCost(); |
| 3782 | int32_t pid = i->getOwnerId(); |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3783 | int32_t score = i->getPriority().getScore(); |
| 3784 | int32_t state = i->getPriority().getState(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3785 | auto conflicting = i->getConflicting(); |
| 3786 | auto clientSp = i->getValue(); |
| 3787 | String8 packageName; |
Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 3788 | userid_t clientUserId = 0; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3789 | if (clientSp.get() != nullptr) { |
| 3790 | packageName = String8{clientSp->getPackageName()}; |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3791 | uid_t clientUid = clientSp->getClientUid(); |
| 3792 | clientUserId = multiuser_get_user_id(clientUid); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3793 | } |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3794 | ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" |
| 3795 | PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3796 | |
Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3797 | if (clientSp.get() != nullptr) { |
| 3798 | ret.appendFormat("User Id: %d, ", clientUserId); |
| 3799 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3800 | if (packageName.size() != 0) { |
| 3801 | ret.appendFormat("Client Package Name: %s", packageName.string()); |
| 3802 | } |
| 3803 | |
| 3804 | ret.append(", Conflicting Client Devices: {"); |
| 3805 | for (auto& j : conflicting) { |
| 3806 | ret.appendFormat("%s, ", j.string()); |
| 3807 | } |
| 3808 | ret.append("})"); |
| 3809 | } |
| 3810 | if (hasAny) ret.append("\n"); |
| 3811 | ret.append("]\n"); |
| 3812 | return ret; |
| 3813 | } |
| 3814 | |
| 3815 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
| 3816 | const String8& key, const sp<BasicClient>& value, int32_t cost, |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3817 | const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId, |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 3818 | int32_t state, int32_t oomScoreOffset) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3819 | |
Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 3820 | bool isVendorClient = getCurrentServingCall() == BinderCallType::HWBINDER; |
Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 3821 | int32_t score_adj = isVendorClient ? kVendorClientScore : score; |
| 3822 | int32_t state_adj = isVendorClient ? kVendorClientState: state; |
| 3823 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3824 | return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>( |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 3825 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, isVendorClient, |
| 3826 | oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 3830 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial, |
| 3831 | int32_t oomScoreOffset) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3832 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3833 | partial->getConflicting(), partial->getPriority().getScore(), |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 3834 | partial->getOwnerId(), partial->getPriority().getState(), oomScoreOffset); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | // ---------------------------------------------------------------------------- |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 3838 | // InjectionStatusListener |
| 3839 | // ---------------------------------------------------------------------------- |
| 3840 | |
| 3841 | void CameraService::InjectionStatusListener::addListener( |
| 3842 | const sp<ICameraInjectionCallback>& callback) { |
| 3843 | Mutex::Autolock lock(mListenerLock); |
| 3844 | if (mCameraInjectionCallback) return; |
| 3845 | status_t res = IInterface::asBinder(callback)->linkToDeath(this); |
| 3846 | if (res == OK) { |
| 3847 | mCameraInjectionCallback = callback; |
| 3848 | } |
| 3849 | } |
| 3850 | |
| 3851 | void CameraService::InjectionStatusListener::removeListener() { |
| 3852 | Mutex::Autolock lock(mListenerLock); |
| 3853 | if (mCameraInjectionCallback == nullptr) { |
| 3854 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 3855 | return; |
| 3856 | } |
| 3857 | IInterface::asBinder(mCameraInjectionCallback)->unlinkToDeath(this); |
| 3858 | mCameraInjectionCallback = nullptr; |
| 3859 | } |
| 3860 | |
| 3861 | void CameraService::InjectionStatusListener::notifyInjectionError( |
| 3862 | int errorCode) { |
| 3863 | Mutex::Autolock lock(mListenerLock); |
| 3864 | if (mCameraInjectionCallback == nullptr) { |
| 3865 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); |
| 3866 | return; |
| 3867 | } |
| 3868 | mCameraInjectionCallback->onInjectionError(errorCode); |
| 3869 | } |
| 3870 | |
| 3871 | void CameraService::InjectionStatusListener::binderDied( |
| 3872 | const wp<IBinder>& /*who*/) { |
| 3873 | Mutex::Autolock lock(mListenerLock); |
| 3874 | ALOGV("InjectionStatusListener: ICameraInjectionCallback has died"); |
| 3875 | auto parent = mParent.promote(); |
| 3876 | if (parent != nullptr) { |
| 3877 | parent->stopInjectionImpl(); |
| 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | // ---------------------------------------------------------------------------- |
| 3882 | // CameraInjectionSession |
| 3883 | // ---------------------------------------------------------------------------- |
| 3884 | |
| 3885 | binder::Status CameraService::CameraInjectionSession::stopInjection() { |
| 3886 | Mutex::Autolock lock(mInjectionSessionLock); |
| 3887 | auto parent = mParent.promote(); |
| 3888 | if (parent == nullptr) { |
| 3889 | ALOGE("CameraInjectionSession: Parent is gone"); |
| 3890 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SERVICE, |
| 3891 | "Camera service encountered error"); |
| 3892 | } |
| 3893 | parent->stopInjectionImpl(); |
| 3894 | return binder::Status::ok(); |
| 3895 | } |
| 3896 | |
| 3897 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3898 | |
| 3899 | static const int kDumpLockRetries = 50; |
| 3900 | static const int kDumpLockSleep = 60000; |
| 3901 | |
| 3902 | static bool tryLock(Mutex& mutex) |
| 3903 | { |
| 3904 | bool locked = false; |
| 3905 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 3906 | if (mutex.tryLock() == NO_ERROR) { |
| 3907 | locked = true; |
| 3908 | break; |
| 3909 | } |
| 3910 | usleep(kDumpLockSleep); |
| 3911 | } |
| 3912 | return locked; |
| 3913 | } |
| 3914 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 3915 | void CameraService::cacheDump() { |
| 3916 | if (mMemFd != -1) { |
| 3917 | const Vector<String16> args; |
| 3918 | ATRACE_CALL(); |
| 3919 | // Acquiring service lock here will avoid the deadlock since |
| 3920 | // cacheDump will not be called during the second disconnect. |
| 3921 | Mutex::Autolock lock(mServiceLock); |
| 3922 | |
| 3923 | Mutex::Autolock l(mCameraStatesLock); |
| 3924 | // Start collecting the info for open sessions and store it in temp file. |
| 3925 | for (const auto& state : mCameraStates) { |
| 3926 | String8 cameraId = state.first; |
| 3927 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 3928 | if (clientDescriptor != nullptr) { |
| 3929 | dprintf(mMemFd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
| 3930 | // Log the current open session info before device is disconnected. |
| 3931 | dumpOpenSessionClientLogs(mMemFd, args, cameraId); |
| 3932 | } |
| 3933 | } |
| 3934 | } |
| 3935 | } |
| 3936 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3937 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3938 | ATRACE_CALL(); |
| 3939 | |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 3940 | if (checkCallingPermission(sDumpPermission) == false) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3941 | 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] | 3942 | CameraThreadState::getCallingPid(), |
| 3943 | CameraThreadState::getCallingUid()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3944 | return NO_ERROR; |
| 3945 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3946 | bool locked = tryLock(mServiceLock); |
| 3947 | // failed to lock - CameraService is probably deadlocked |
| 3948 | if (!locked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3949 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3950 | } |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3951 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3952 | if (!mInitialized) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3953 | dprintf(fd, "!! No camera HAL available !!\n"); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 3954 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3955 | // Dump event log for error information |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3956 | dumpEventLog(fd); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3957 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3958 | if (locked) mServiceLock.unlock(); |
| 3959 | return NO_ERROR; |
| 3960 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3961 | dprintf(fd, "\n== Service global info: ==\n\n"); |
| 3962 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3963 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); |
Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 3964 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", |
| 3965 | mNormalDeviceIdsWithoutSystemCamera.size()); |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3966 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { |
| 3967 | dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); |
| 3968 | } |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3969 | String8 activeClientString = mActiveClientManager.toString(); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3970 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string()); |
| 3971 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3972 | |
| 3973 | dumpEventLog(fd); |
| 3974 | |
| 3975 | bool stateLocked = tryLock(mCameraStatesLock); |
| 3976 | if (!stateLocked) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3977 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3978 | } |
| 3979 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 3980 | int argSize = args.size(); |
| 3981 | for (int i = 0; i < argSize; i++) { |
| 3982 | if (args[i] == TagMonitor::kMonitorOption) { |
| 3983 | if (i + 1 < argSize) { |
| 3984 | mMonitorTags = String8(args[i + 1]); |
| 3985 | } |
| 3986 | break; |
| 3987 | } |
| 3988 | } |
| 3989 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3990 | for (auto& state : mCameraStates) { |
| 3991 | String8 cameraId = state.first; |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3992 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3993 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string()); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3994 | |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3995 | CameraParameters p = state.second->getShimParams(); |
| 3996 | if (!p.isEmpty()) { |
| 3997 | dprintf(fd, " Camera1 API shim is using parameters:\n "); |
| 3998 | p.dump(fd, args); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4002 | if (clientDescriptor != nullptr) { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4003 | // log the current open session info |
| 4004 | dumpOpenSessionClientLogs(fd, args, cameraId); |
Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4005 | } else { |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4006 | dumpClosedSessionClientLogs(fd, cameraId); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4007 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4008 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | if (stateLocked) mCameraStatesLock.unlock(); |
| 4012 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4013 | if (locked) mServiceLock.unlock(); |
| 4014 | |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 4015 | mCameraProviderManager->dump(fd, args); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4016 | |
| 4017 | dprintf(fd, "\n== Vendor tags: ==\n\n"); |
| 4018 | |
| 4019 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 4020 | if (desc == NULL) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 4021 | sp<VendorTagDescriptorCache> cache = |
| 4022 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 4023 | if (cache == NULL) { |
| 4024 | dprintf(fd, "No vendor tags.\n"); |
| 4025 | } else { |
| 4026 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 4027 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4028 | } else { |
| 4029 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); |
| 4030 | } |
| 4031 | |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4032 | // Dump camera traces if there were any |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4033 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4034 | camera3::CameraTraces::dump(fd, args); |
| 4035 | |
| 4036 | // Process dump arguments, if any |
| 4037 | int n = args.size(); |
| 4038 | String16 verboseOption("-v"); |
| 4039 | String16 unreachableOption("--unreachable"); |
| 4040 | for (int i = 0; i < n; i++) { |
| 4041 | if (args[i] == verboseOption) { |
| 4042 | // change logging level |
| 4043 | if (i + 1 >= n) continue; |
| 4044 | String8 levelStr(args[i+1]); |
| 4045 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4046 | dprintf(fd, "\nSetting log level to %d.\n", level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4047 | setLogLevel(level); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4048 | } else if (args[i] == unreachableOption) { |
| 4049 | // Dump memory analysis |
| 4050 | // TODO - should limit be an argument parameter? |
| 4051 | UnreachableMemoryInfo info; |
| 4052 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); |
| 4053 | if (!success) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4054 | dprintf(fd, "\n== Unable to dump unreachable memory. " |
| 4055 | "Try disabling SELinux enforcement. ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4056 | } else { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4057 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); |
Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4058 | std::string s = info.ToString(/*log_contents*/ true); |
| 4059 | write(fd, s.c_str(), s.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4060 | } |
| 4061 | } |
| 4062 | } |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4063 | |
| 4064 | bool serviceLocked = tryLock(mServiceLock); |
| 4065 | |
| 4066 | // Dump info from previous open sessions. |
| 4067 | // Reposition the offset to beginning of the file before reading |
| 4068 | |
| 4069 | if ((mMemFd >= 0) && (lseek(mMemFd, 0, SEEK_SET) != -1)) { |
| 4070 | dprintf(fd, "\n**********Dumpsys from previous open session**********\n"); |
| 4071 | ssize_t size_read; |
| 4072 | char buf[4096]; |
| 4073 | while ((size_read = read(mMemFd, buf, (sizeof(buf) - 1))) > 0) { |
| 4074 | // Read data from file to a small buffer and write it to fd. |
| 4075 | write(fd, buf, size_read); |
| 4076 | if (size_read == -1) { |
| 4077 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 4078 | break; |
| 4079 | } |
| 4080 | } |
| 4081 | dprintf(fd, "\n**********End of Dumpsys from previous open session**********\n"); |
| 4082 | } else { |
| 4083 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); |
| 4084 | } |
| 4085 | |
| 4086 | if (serviceLocked) mServiceLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4087 | return NO_ERROR; |
| 4088 | } |
| 4089 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4090 | void CameraService::dumpOpenSessionClientLogs(int fd, |
| 4091 | const Vector<String16>& args, const String8& cameraId) { |
| 4092 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 4093 | dprintf(fd, " Device %s is open. Client instance dump:\n", |
| 4094 | cameraId.string()); |
| 4095 | dprintf(fd, " Client priority score: %d state: %d\n", |
| 4096 | clientDescriptor->getPriority().getScore(), |
| 4097 | clientDescriptor->getPriority().getState()); |
| 4098 | dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 4099 | |
| 4100 | auto client = clientDescriptor->getValue(); |
| 4101 | dprintf(fd, " Client package: %s\n", |
| 4102 | String8(client->getPackageName()).string()); |
| 4103 | |
| 4104 | client->dumpClient(fd, args); |
| 4105 | } |
| 4106 | |
| 4107 | void CameraService::dumpClosedSessionClientLogs(int fd, const String8& cameraId) { |
| 4108 | dprintf(fd, " Device %s is closed, no client instance\n", |
| 4109 | cameraId.string()); |
| 4110 | } |
| 4111 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4112 | void CameraService::dumpEventLog(int fd) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4113 | 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] | 4114 | |
| 4115 | Mutex::Autolock l(mLogLock); |
| 4116 | for (const auto& msg : mEventLog) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4117 | dprintf(fd, " %s\n", msg.string()); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4121 | dprintf(fd, " ...\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4122 | } else if (mEventLog.size() == 0) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4123 | dprintf(fd, " [no events yet]\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4124 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4125 | dprintf(fd, "\n"); |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4126 | } |
| 4127 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4128 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4129 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4130 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 4131 | if (mTorchClientMap[i] == who) { |
| 4132 | // 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] | 4133 | String8 cameraId = mTorchClientMap.keyAt(i); |
| 4134 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 4135 | if (res) { |
| 4136 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 4137 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 4138 | return; |
| 4139 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4140 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4141 | break; |
| 4142 | } |
| 4143 | } |
| 4144 | } |
| 4145 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4146 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 4147 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 4148 | /** |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4149 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the |
| 4150 | * binder driver |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 4151 | */ |
Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 4152 | // PID here is approximate and can be wrong. |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 4153 | logClientDied(CameraThreadState::getCallingPid(), String8("Binder died unexpectedly")); |
Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4154 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4155 | // check torch client |
| 4156 | handleTorchClientBinderDied(who); |
| 4157 | |
| 4158 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4159 | if(!evictClientIdByRemote(who)) { |
| 4160 | 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] | 4161 | return; |
| 4162 | } |
| 4163 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4164 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 4165 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 4166 | } |
| 4167 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4168 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4169 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 4170 | } |
| 4171 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4172 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId, |
| 4173 | std::initializer_list<StatusInternal> rejectSourceStates) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4174 | // Do not lock mServiceLock here or can get into a deadlock from |
| 4175 | // connect() -> disconnect -> updateStatus |
| 4176 | |
| 4177 | auto state = getCameraState(cameraId); |
| 4178 | |
| 4179 | if (state == nullptr) { |
| 4180 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 4181 | cameraId.string()); |
| 4182 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4183 | } |
| 4184 | |
Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4185 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) |
| 4186 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; |
| 4187 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { |
| 4188 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); |
| 4189 | return; |
| 4190 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4191 | |
| 4192 | // Collect the logical cameras without holding mStatusLock in updateStatus |
| 4193 | // as that can lead to a deadlock(b/162192331). |
| 4194 | auto logicalCameraIds = getLogicalCameras(cameraId); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4195 | // 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] | 4196 | // of the listeners with both the mStatusLock and mStatusListenerLock held |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 4197 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4198 | &logicalCameraIds] |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4199 | (const String8& cameraId, StatusInternal status) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4200 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4201 | if (status != StatusInternal::ENUMERATING) { |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4202 | // Update torch status if it has a flash unit. |
| 4203 | Mutex::Autolock al(mTorchStatusMutex); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4204 | TorchModeStatus torchStatus; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4205 | if (getTorchStatusLocked(cameraId, &torchStatus) != |
| 4206 | NAME_NOT_FOUND) { |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4207 | TorchModeStatus newTorchStatus = |
| 4208 | status == StatusInternal::PRESENT ? |
| 4209 | TorchModeStatus::AVAILABLE_OFF : |
| 4210 | TorchModeStatus::NOT_AVAILABLE; |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4211 | if (torchStatus != newTorchStatus) { |
Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 4212 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); |
Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4213 | } |
| 4214 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | Mutex::Autolock lock(mStatusListenerLock); |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4218 | notifyPhysicalCameraStatusLocked(mapToInterface(status), String16(cameraId), |
| 4219 | logicalCameraIds, deviceKind); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4220 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4221 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 4222 | bool isVendorListener = listener->isVendorListener(); |
| 4223 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, |
| 4224 | listener->getListenerPid(), listener->getListenerUid()) || |
Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 4225 | isVendorListener) { |
| 4226 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4227 | cameraId.c_str()); |
Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 4228 | continue; |
| 4229 | } |
Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4230 | listener->getListener()->onStatusChanged(mapToInterface(status), |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4231 | String16(cameraId)); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4232 | } |
| 4233 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4234 | } |
| 4235 | |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4236 | void CameraService::updateOpenCloseStatus(const String8& cameraId, bool open, |
| 4237 | const String16& clientPackageName) { |
| 4238 | Mutex::Autolock lock(mStatusListenerLock); |
| 4239 | |
| 4240 | for (const auto& it : mListenerList) { |
| 4241 | if (!it->isOpenCloseCallbackAllowed()) { |
| 4242 | continue; |
| 4243 | } |
| 4244 | |
| 4245 | binder::Status ret; |
| 4246 | String16 cameraId64(cameraId); |
| 4247 | if (open) { |
| 4248 | ret = it->getListener()->onCameraOpened(cameraId64, clientPackageName); |
| 4249 | } else { |
| 4250 | ret = it->getListener()->onCameraClosed(cameraId64); |
| 4251 | } |
| 4252 | if (!ret.isOk()) { |
| 4253 | ALOGE("%s: Failed to trigger onCameraOpened/onCameraClosed callback: %d", __FUNCTION__, |
| 4254 | ret.exceptionCode()); |
| 4255 | } |
| 4256 | } |
| 4257 | } |
| 4258 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4259 | template<class Func> |
| 4260 | void CameraService::CameraState::updateStatus(StatusInternal status, |
| 4261 | const String8& cameraId, |
| 4262 | std::initializer_list<StatusInternal> rejectSourceStates, |
| 4263 | Func onStatusUpdatedLocked) { |
| 4264 | Mutex::Autolock lock(mStatusLock); |
| 4265 | StatusInternal oldStatus = mStatus; |
| 4266 | mStatus = status; |
| 4267 | |
| 4268 | if (oldStatus == status) { |
| 4269 | return; |
| 4270 | } |
| 4271 | |
| 4272 | ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__, |
| 4273 | cameraId.string(), oldStatus, status); |
| 4274 | |
| 4275 | if (oldStatus == StatusInternal::NOT_PRESENT && |
| 4276 | (status != StatusInternal::PRESENT && |
| 4277 | status != StatusInternal::ENUMERATING)) { |
| 4278 | |
| 4279 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", |
| 4280 | __FUNCTION__); |
| 4281 | mStatus = oldStatus; |
| 4282 | return; |
| 4283 | } |
| 4284 | |
| 4285 | /** |
| 4286 | * Sometimes we want to conditionally do a transition. |
| 4287 | * For example if a client disconnects, we want to go to PRESENT |
| 4288 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 4289 | */ |
| 4290 | for (auto& rejectStatus : rejectSourceStates) { |
| 4291 | if (oldStatus == rejectStatus) { |
| 4292 | ALOGV("%s: Rejecting status transition for Camera ID %s, since the source " |
| 4293 | "state was was in one of the bad states.", __FUNCTION__, cameraId.string()); |
| 4294 | mStatus = oldStatus; |
| 4295 | return; |
| 4296 | } |
| 4297 | } |
| 4298 | |
| 4299 | onStatusUpdatedLocked(cameraId, status); |
| 4300 | } |
| 4301 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4302 | status_t CameraService::getTorchStatusLocked( |
| 4303 | const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4304 | TorchModeStatus *status) const { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4305 | if (!status) { |
| 4306 | return BAD_VALUE; |
| 4307 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4308 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 4309 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4310 | // invalid camera ID or the camera doesn't have a flash unit |
| 4311 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4312 | } |
| 4313 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4314 | *status = mTorchStatusMap.valueAt(index); |
| 4315 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4316 | } |
| 4317 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4318 | status_t CameraService::setTorchStatusLocked(const String8& cameraId, |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4319 | TorchModeStatus status) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4320 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 4321 | if (index == NAME_NOT_FOUND) { |
| 4322 | return BAD_VALUE; |
| 4323 | } |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4324 | mTorchStatusMap.editValueAt(index) = status; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4325 | |
| 4326 | return OK; |
| 4327 | } |
| 4328 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4329 | std::list<String16> CameraService::getLogicalCameras( |
| 4330 | const String8& physicalCameraId) { |
| 4331 | std::list<String16> retList; |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4332 | Mutex::Autolock lock(mCameraStatesLock); |
| 4333 | for (const auto& state : mCameraStates) { |
| 4334 | std::vector<std::string> physicalCameraIds; |
| 4335 | if (!mCameraProviderManager->isLogicalCamera(state.first.c_str(), &physicalCameraIds)) { |
| 4336 | // This is not a logical multi-camera. |
| 4337 | continue; |
| 4338 | } |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4339 | if (std::find(physicalCameraIds.begin(), physicalCameraIds.end(), physicalCameraId.c_str()) |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4340 | == physicalCameraIds.end()) { |
| 4341 | // cameraId is not a physical camera of this logical multi-camera. |
| 4342 | continue; |
| 4343 | } |
| 4344 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4345 | retList.emplace_back(String16(state.first)); |
| 4346 | } |
| 4347 | return retList; |
| 4348 | } |
| 4349 | |
| 4350 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, |
| 4351 | const String16& physicalCameraId, const std::list<String16>& logicalCameraIds, |
| 4352 | SystemCameraKind deviceKind) { |
| 4353 | // mStatusListenerLock is expected to be locked |
| 4354 | for (const auto& logicalCameraId : logicalCameraIds) { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4355 | for (auto& listener : mListenerList) { |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4356 | // Note: we check only the deviceKind of the physical camera id |
| 4357 | // since, logical camera ids and their physical camera ids are |
| 4358 | // guaranteed to have the same system camera kind. |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 4359 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), |
| 4360 | listener->getListenerPid(), listener->getListenerUid())) { |
| 4361 | ALOGV("Skipping discovery callback for system-only camera device %s", |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4362 | String8(physicalCameraId).c_str()); |
Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 4363 | continue; |
| 4364 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4365 | listener->getListener()->onPhysicalCameraStatusChanged(status, |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4366 | logicalCameraId, physicalCameraId); |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4367 | } |
| 4368 | } |
| 4369 | } |
| 4370 | |
Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4371 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4372 | void CameraService::blockClientsForUid(uid_t uid) { |
| 4373 | const auto clients = mActiveClientManager.getAll(); |
| 4374 | for (auto& current : clients) { |
| 4375 | if (current != nullptr) { |
| 4376 | const auto basicClient = current->getValue(); |
| 4377 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { |
| 4378 | basicClient->block(); |
| 4379 | } |
| 4380 | } |
| 4381 | } |
| 4382 | } |
| 4383 | |
Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4384 | void CameraService::blockAllClients() { |
| 4385 | const auto clients = mActiveClientManager.getAll(); |
| 4386 | for (auto& current : clients) { |
| 4387 | if (current != nullptr) { |
| 4388 | const auto basicClient = current->getValue(); |
| 4389 | if (basicClient.get() != nullptr) { |
| 4390 | basicClient->block(); |
| 4391 | } |
| 4392 | } |
| 4393 | } |
| 4394 | } |
| 4395 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4396 | // NOTE: This is a remote API - make sure all args are validated |
| 4397 | status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) { |
| 4398 | if (!checkCallingPermission(sManageCameraPermission, nullptr, nullptr)) { |
| 4399 | return PERMISSION_DENIED; |
| 4400 | } |
| 4401 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 4402 | return BAD_VALUE; |
| 4403 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4404 | if (args.size() >= 3 && args[0] == String16("set-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4405 | return handleSetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4406 | } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4407 | return handleResetUidState(args, err); |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4408 | } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4409 | return handleGetUidState(args, out, err); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4410 | } else if (args.size() >= 2 && args[0] == String16("set-rotate-and-crop")) { |
| 4411 | return handleSetRotateAndCrop(args); |
| 4412 | } else if (args.size() >= 1 && args[0] == String16("get-rotate-and-crop")) { |
| 4413 | return handleGetRotateAndCrop(out); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4414 | } else if (args.size() >= 2 && args[0] == String16("set-image-dump-mask")) { |
| 4415 | return handleSetImageDumpMask(args); |
| 4416 | } else if (args.size() >= 1 && args[0] == String16("get-image-dump-mask")) { |
| 4417 | return handleGetImageDumpMask(out); |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 4418 | } else if (args.size() >= 2 && args[0] == String16("set-camera-mute")) { |
| 4419 | return handleSetCameraMute(args); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4420 | } else if (args.size() == 1 && args[0] == String16("help")) { |
| 4421 | printHelp(out); |
| 4422 | return NO_ERROR; |
| 4423 | } |
| 4424 | printHelp(err); |
| 4425 | return BAD_VALUE; |
| 4426 | } |
| 4427 | |
| 4428 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4429 | String16 packageName = args[1]; |
| 4430 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4431 | bool active = false; |
| 4432 | if (args[2] == String16("active")) { |
| 4433 | active = true; |
| 4434 | } else if ((args[2] != String16("idle"))) { |
| 4435 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string()); |
| 4436 | return BAD_VALUE; |
| 4437 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4438 | |
| 4439 | int userId = 0; |
| 4440 | if (args.size() >= 5 && args[3] == String16("--user")) { |
| 4441 | userId = atoi(String8(args[4])); |
| 4442 | } |
| 4443 | |
| 4444 | uid_t uid; |
| 4445 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
| 4446 | return BAD_VALUE; |
| 4447 | } |
| 4448 | |
| 4449 | mUidPolicy->addOverrideUid(uid, packageName, active); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4450 | return NO_ERROR; |
| 4451 | } |
| 4452 | |
| 4453 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4454 | String16 packageName = args[1]; |
| 4455 | |
| 4456 | int userId = 0; |
| 4457 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 4458 | userId = atoi(String8(args[3])); |
| 4459 | } |
| 4460 | |
| 4461 | uid_t uid; |
| 4462 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4463 | return BAD_VALUE; |
| 4464 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4465 | |
| 4466 | mUidPolicy->removeOverrideUid(uid, packageName); |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4467 | return NO_ERROR; |
| 4468 | } |
| 4469 | |
| 4470 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4471 | String16 packageName = args[1]; |
| 4472 | |
| 4473 | int userId = 0; |
| 4474 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 4475 | userId = atoi(String8(args[3])); |
| 4476 | } |
| 4477 | |
| 4478 | uid_t uid; |
| 4479 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4480 | return BAD_VALUE; |
| 4481 | } |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4482 | |
| 4483 | if (mUidPolicy->isUidActive(uid, packageName)) { |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4484 | return dprintf(out, "active\n"); |
| 4485 | } else { |
| 4486 | return dprintf(out, "idle\n"); |
| 4487 | } |
| 4488 | } |
| 4489 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4490 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { |
| 4491 | int rotateValue = atoi(String8(args[1])); |
| 4492 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || |
| 4493 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; |
| 4494 | Mutex::Autolock lock(mServiceLock); |
| 4495 | |
| 4496 | mOverrideRotateAndCropMode = rotateValue; |
| 4497 | |
| 4498 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; |
| 4499 | |
| 4500 | const auto clients = mActiveClientManager.getAll(); |
| 4501 | for (auto& current : clients) { |
| 4502 | if (current != nullptr) { |
| 4503 | const auto basicClient = current->getValue(); |
| 4504 | if (basicClient.get() != nullptr) { |
| 4505 | basicClient->setRotateAndCropOverride(rotateValue); |
| 4506 | } |
| 4507 | } |
| 4508 | } |
| 4509 | |
| 4510 | return OK; |
| 4511 | } |
| 4512 | |
| 4513 | status_t CameraService::handleGetRotateAndCrop(int out) { |
| 4514 | Mutex::Autolock lock(mServiceLock); |
| 4515 | |
| 4516 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); |
| 4517 | } |
| 4518 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4519 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { |
| 4520 | char *endPtr; |
| 4521 | errno = 0; |
| 4522 | String8 maskString8 = String8(args[1]); |
| 4523 | long maskValue = strtol(maskString8.c_str(), &endPtr, 10); |
| 4524 | |
| 4525 | if (errno != 0) return BAD_VALUE; |
| 4526 | if (endPtr != maskString8.c_str() + maskString8.size()) return BAD_VALUE; |
| 4527 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; |
| 4528 | |
| 4529 | Mutex::Autolock lock(mServiceLock); |
| 4530 | |
| 4531 | mImageDumpMask = maskValue; |
| 4532 | |
| 4533 | return OK; |
| 4534 | } |
| 4535 | |
| 4536 | status_t CameraService::handleGetImageDumpMask(int out) { |
| 4537 | Mutex::Autolock lock(mServiceLock); |
| 4538 | |
| 4539 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); |
| 4540 | } |
| 4541 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 4542 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { |
| 4543 | int muteValue = strtol(String8(args[1]), nullptr, 10); |
| 4544 | if (errno != 0) return BAD_VALUE; |
| 4545 | |
| 4546 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; |
| 4547 | Mutex::Autolock lock(mServiceLock); |
| 4548 | |
| 4549 | mOverrideCameraMuteMode = (muteValue == 1); |
| 4550 | |
| 4551 | const auto clients = mActiveClientManager.getAll(); |
| 4552 | for (auto& current : clients) { |
| 4553 | if (current != nullptr) { |
| 4554 | const auto basicClient = current->getValue(); |
| 4555 | if (basicClient.get() != nullptr) { |
| 4556 | if (basicClient->supportsCameraMute()) { |
| 4557 | basicClient->setCameraMute(mOverrideCameraMuteMode); |
| 4558 | } |
| 4559 | } |
| 4560 | } |
| 4561 | } |
| 4562 | |
| 4563 | return OK; |
| 4564 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4565 | |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4566 | status_t CameraService::printHelp(int out) { |
| 4567 | return dprintf(out, "Camera service commands:\n" |
Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4568 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 4569 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 4570 | " 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] | 4571 | " set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" |
| 4572 | " Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" |
| 4573 | " 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] | 4574 | " set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" |
| 4575 | " Valid values 0=OFF, 1=ON for JPEG\n" |
| 4576 | " 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] | 4577 | " set-camera-mute <0/1> enable or disable camera muting\n" |
Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4578 | " help print this message\n"); |
| 4579 | } |
| 4580 | |
Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 4581 | int32_t CameraService::updateAudioRestriction() { |
| 4582 | Mutex::Autolock lock(mServiceLock); |
| 4583 | return updateAudioRestrictionLocked(); |
| 4584 | } |
| 4585 | |
| 4586 | int32_t CameraService::updateAudioRestrictionLocked() { |
| 4587 | int32_t mode = 0; |
| 4588 | // iterate through all active client |
| 4589 | for (const auto& i : mActiveClientManager.getAll()) { |
| 4590 | const auto clientSp = i->getValue(); |
| 4591 | mode |= clientSp->getAudioRestriction(); |
| 4592 | } |
| 4593 | |
| 4594 | bool modeChanged = (mAudioRestriction != mode); |
| 4595 | mAudioRestriction = mode; |
| 4596 | if (modeChanged) { |
| 4597 | mAppOps.setCameraAudioRestriction(mode); |
| 4598 | } |
| 4599 | return mode; |
| 4600 | } |
| 4601 | |
Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4602 | void CameraService::stopInjectionImpl() { |
| 4603 | mInjectionStatusListener->removeListener(); |
| 4604 | |
| 4605 | // TODO: Implement the stop injection function. |
| 4606 | } |
| 4607 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4608 | }; // namespace android |