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