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