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