| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* | 
| Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 2 | * Copyright (C) 2008 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 16 |  | 
|  | 17 | #define LOG_TAG "CameraService" | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA | 
| Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 20 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 21 | #include <algorithm> | 
|  | 22 | #include <climits> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 23 | #include <stdio.h> | 
| Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 24 | #include <cstdlib> | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 25 | #include <cstring> | 
|  | 26 | #include <ctime> | 
|  | 27 | #include <string> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 28 | #include <sys/types.h> | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 29 | #include <inttypes.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 30 | #include <pthread.h> | 
| Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 31 | #include <poll.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 32 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 33 | #include <android/hardware/ICamera.h> | 
|  | 34 | #include <android/hardware/ICameraClient.h> | 
|  | 35 |  | 
| Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 36 | #include <aidl/AidlCameraService.h> | 
| Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 37 | #include <android-base/macros.h> | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 38 | #include <android-base/parseint.h> | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 39 | #include <android-base/stringprintf.h> | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 40 | #include <binder/ActivityManager.h> | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 41 | #include <binder/AppOpsManager.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | #include <binder/IPCThreadState.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 43 | #include <binder/MemoryBase.h> | 
|  | 44 | #include <binder/MemoryHeapBase.h> | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 45 | #include <binder/PermissionController.h> | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 46 | #include <binder/IResultReceiver.h> | 
| Steven Moreland | 89a2c5c | 2020-01-31 15:02:25 -0800 | [diff] [blame] | 47 | #include <binderthreadstate/CallerUtils.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 48 | #include <cutils/atomic.h> | 
| Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 49 | #include <cutils/properties.h> | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 50 | #include <cutils/misc.h> | 
| Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 51 | #include <gui/Surface.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | #include <hardware/hardware.h> | 
| Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 53 | #include "hidl/HidlCameraService.h" | 
|  | 54 | #include <hidl/HidlTransportSupport.h> | 
| Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 55 | #include <hwbinder/IPCThreadState.h> | 
| Eino-Ville Talvala | d89821e | 2016-04-20 11:23:50 -0700 | [diff] [blame] | 56 | #include <memunreachable/memunreachable.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 57 | #include <media/AudioSystem.h> | 
| Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 58 | #include <media/IMediaHTTPService.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 59 | #include <media/mediaplayer.h> | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 60 | #include <mediautils/BatteryNotifier.h> | 
| Steven Moreland | 886d732 | 2021-04-02 04:19:45 +0000 | [diff] [blame] | 61 | #include <processinfo/ProcessInfoService.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | #include <utils/Errors.h> | 
|  | 63 | #include <utils/Log.h> | 
|  | 64 | #include <utils/String16.h> | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 65 | #include <utils/SystemClock.h> | 
| Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 66 | #include <utils/Trace.h> | 
| Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 67 | #include <utils/CallStack.h> | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 68 | #include <private/android_filesystem_config.h> | 
| Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 69 | #include <system/camera_vendor_tags.h> | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 70 | #include <system/camera_metadata.h> | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 71 |  | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 72 | #include <system/camera.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 73 |  | 
|  | 74 | #include "CameraService.h" | 
| Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 75 | #include "api1/Camera2Client.h" | 
| Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 76 | #include "api2/CameraDeviceClient.h" | 
| Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 77 | #include "utils/CameraTraces.h" | 
| Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 78 | #include "utils/TagMonitor.h" | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 79 | #include "utils/CameraThreadState.h" | 
| Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 80 | #include "utils/CameraServiceProxyWrapper.h" | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 81 |  | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 82 | namespace { | 
|  | 83 | const char* kPermissionServiceName = "permission"; | 
|  | 84 | }; // namespace anonymous | 
|  | 85 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 86 | namespace android { | 
|  | 87 |  | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 88 | using base::StringPrintf; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 89 | using binder::Status; | 
| Austin Borger | ea93124 | 2021-12-13 23:10:41 +0000 | [diff] [blame] | 90 | using namespace camera3; | 
| Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 91 | using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService; | 
| Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 92 | using frameworks::cameraservice::service::implementation::AidlCameraService; | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 93 | using hardware::ICamera; | 
|  | 94 | using hardware::ICameraClient; | 
|  | 95 | using hardware::ICameraServiceListener; | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 96 | using hardware::camera2::ICameraInjectionCallback; | 
|  | 97 | using hardware::camera2::ICameraInjectionSession; | 
| Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 98 | using hardware::camera2::utils::CameraIdAndSessionConfiguration; | 
|  | 99 | using hardware::camera2::utils::ConcurrentCameraIdCombination; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 100 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 101 | // ---------------------------------------------------------------------------- | 
|  | 102 | // Logging support -- this is for debugging only | 
|  | 103 | // Use "adb shell dumpsys media.camera -v 1" to change it. | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 104 | volatile int32_t gLogLevel = 0; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 105 |  | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 106 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); | 
|  | 107 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 108 |  | 
|  | 109 | static void setLogLevel(int level) { | 
|  | 110 | android_atomic_write(level, &gLogLevel); | 
|  | 111 | } | 
|  | 112 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 113 | // Convenience methods for constructing binder::Status objects for error returns | 
|  | 114 |  | 
|  | 115 | #define STATUS_ERROR(errorCode, errorString) \ | 
|  | 116 | binder::Status::fromServiceSpecificError(errorCode, \ | 
|  | 117 | String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString)) | 
|  | 118 |  | 
|  | 119 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ | 
|  | 120 | binder::Status::fromServiceSpecificError(errorCode, \ | 
|  | 121 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \ | 
|  | 122 | __VA_ARGS__)) | 
|  | 123 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 124 | // ---------------------------------------------------------------------------- | 
|  | 125 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 126 | static const String16 sDumpPermission("android.permission.DUMP"); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 127 | static const String16 sManageCameraPermission("android.permission.MANAGE_CAMERA"); | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 128 | static const String16 sCameraPermission("android.permission.CAMERA"); | 
|  | 129 | static const String16 sSystemCameraPermission("android.permission.SYSTEM_CAMERA"); | 
|  | 130 | static const String16 | 
|  | 131 | sCameraSendSystemEventsPermission("android.permission.CAMERA_SEND_SYSTEM_EVENTS"); | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 132 | static const String16 sCameraOpenCloseListenerPermission( | 
|  | 133 | "android.permission.CAMERA_OPEN_CLOSE_LISTENER"); | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 134 | static const String16 | 
|  | 135 | sCameraInjectExternalCameraPermission("android.permission.CAMERA_INJECT_EXTERNAL_CAMERA"); | 
| Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 136 | const char *sFileName = "lastOpenSessionDumpFile"; | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 137 | static constexpr int32_t kSystemNativeClientScore = resource_policy::PERCEPTIBLE_APP_ADJ; | 
|  | 138 | static constexpr int32_t kSystemNativeClientState = | 
|  | 139 | ActivityManager::PROCESS_STATE_PERSISTENT_UI; | 
| Eino-Ville Talvala | 7c602c3 | 2021-03-20 17:00:18 -0700 | [diff] [blame] | 140 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 141 | const String8 CameraService::kOfflineDevice("offline-"); | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 142 | const String16 CameraService::kWatchAllClientsFlag("all"); | 
| Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 143 |  | 
| Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 144 | // Set to keep track of logged service error events. | 
|  | 145 | static std::set<String8> sServiceErrorEventSet; | 
|  | 146 |  | 
| Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 147 | CameraService::CameraService( | 
|  | 148 | std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper) : | 
|  | 149 | mCameraServiceProxyWrapper(cameraServiceProxyWrapper == nullptr ? | 
|  | 150 | std::make_shared<CameraServiceProxyWrapper>() : cameraServiceProxyWrapper), | 
| Eino-Ville Talvala | 49c9705 | 2016-01-12 14:29:40 -0800 | [diff] [blame] | 151 | mEventLog(DEFAULT_EVENT_LOG_LENGTH), | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 152 | mNumberOfCameras(0), | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 153 | mNumberOfCamerasWithoutSystemCamera(0), | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 154 | mSoundRef(0), mInitialized(false), | 
|  | 155 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) { | 
| Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 156 | ALOGI("CameraService started (pid=%d)", getpid()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 157 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); | 
| Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 158 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); | 
|  | 159 | if (mMemFd == -1) { | 
|  | 160 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); | 
|  | 161 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 162 | } | 
|  | 163 |  | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 164 | // The word 'System' here does not refer to clients only on the system | 
|  | 165 | // partition. They just need to have a android system uid. | 
|  | 166 | static bool doesClientHaveSystemUid() { | 
|  | 167 | return (CameraThreadState::getCallingUid() < AID_APP_START); | 
|  | 168 | } | 
|  | 169 |  | 
| Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 170 | void CameraService::onFirstRef() | 
|  | 171 | { | 
| Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 172 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 173 | ALOGI("CameraService process starting"); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 174 |  | 
| Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 175 | BnCameraService::onFirstRef(); | 
|  | 176 |  | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 177 | // Update battery life tracking if service is restarting | 
|  | 178 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); | 
|  | 179 | notifier.noteResetCamera(); | 
|  | 180 | notifier.noteResetFlashlight(); | 
|  | 181 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 182 | status_t res = INVALID_OPERATION; | 
| Eino-Ville Talvala | 9cbbc83 | 2017-01-23 15:39:53 -0800 | [diff] [blame] | 183 |  | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 184 | res = enumerateProviders(); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 185 | if (res == OK) { | 
|  | 186 | mInitialized = true; | 
|  | 187 | } | 
|  | 188 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 189 | mUidPolicy = new UidPolicy(this); | 
|  | 190 | mUidPolicy->registerSelf(); | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 191 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this); | 
|  | 192 | mSensorPrivacyPolicy->registerSelf(); | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 193 | mInjectionStatusListener = new InjectionStatusListener(this); | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 194 | mAppOps.setCameraAudioRestriction(mAudioRestriction); | 
| Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame] | 195 | sp<HidlCameraService> hcs = HidlCameraService::getInstance(this); | 
|  | 196 | if (hcs->registerAsService() != android::OK) { | 
|  | 197 | ALOGE("%s: Failed to register default android.frameworks.cameraservice.service@1.0", | 
|  | 198 | __FUNCTION__); | 
|  | 199 | } | 
| Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 200 |  | 
| Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 201 | if (!AidlCameraService::registerService(this)) { | 
|  | 202 | ALOGE("%s: Failed to register default AIDL VNDK CameraService", __FUNCTION__); | 
|  | 203 | } | 
|  | 204 |  | 
| Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 205 | // This needs to be last call in this function, so that it's as close to | 
|  | 206 | // ServiceManager::addService() as possible. | 
| Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 207 | mCameraServiceProxyWrapper->pingCameraServiceProxy(); | 
| Shuzhen Wang | 24b4415 | 2019-09-20 10:38:11 -0700 | [diff] [blame] | 208 | ALOGI("CameraService pinged cameraservice proxy"); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 209 | } | 
|  | 210 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 211 | status_t CameraService::enumerateProviders() { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 212 | status_t res; | 
| Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 213 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 214 | std::vector<std::string> deviceIds; | 
| Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 215 | std::unordered_map<std::string, std::set<std::string>> unavailPhysicalIds; | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 216 | { | 
|  | 217 | Mutex::Autolock l(mServiceLock); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 218 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 219 | if (nullptr == mCameraProviderManager.get()) { | 
|  | 220 | mCameraProviderManager = new CameraProviderManager(); | 
|  | 221 | res = mCameraProviderManager->initialize(this); | 
|  | 222 | if (res != OK) { | 
|  | 223 | ALOGE("%s: Unable to initialize camera provider manager: %s (%d)", | 
|  | 224 | __FUNCTION__, strerror(-res), res); | 
| Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 225 | logServiceError(String8::format("Unable to initialize camera provider manager"), | 
|  | 226 | ERROR_DISCONNECTED); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 227 | return res; | 
| Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 228 | } | 
|  | 229 | } | 
|  | 230 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 231 |  | 
|  | 232 | // Setup vendor tags before we call get_camera_info the first time | 
|  | 233 | // because HAL might need to setup static vendor keys in get_camera_info | 
|  | 234 | // TODO: maybe put this into CameraProviderManager::initialize()? | 
|  | 235 | mCameraProviderManager->setUpVendorTags(); | 
|  | 236 |  | 
|  | 237 | if (nullptr == mFlashlight.get()) { | 
|  | 238 | mFlashlight = new CameraFlashlight(mCameraProviderManager, this); | 
| Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 239 | } | 
|  | 240 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 241 | res = mFlashlight->findFlashUnits(); | 
|  | 242 | if (res != OK) { | 
|  | 243 | ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res); | 
|  | 244 | } | 
|  | 245 |  | 
| Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 246 | deviceIds = mCameraProviderManager->getCameraDeviceIds(&unavailPhysicalIds); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
|  | 249 |  | 
|  | 250 | for (auto& cameraId : deviceIds) { | 
|  | 251 | String8 id8 = String8(cameraId.c_str()); | 
| Shuzhen Wang | 6ba3f5e | 2018-11-20 10:04:08 -0800 | [diff] [blame] | 252 | if (getCameraState(id8) == nullptr) { | 
|  | 253 | onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT); | 
|  | 254 | } | 
| Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 255 | if (unavailPhysicalIds.count(cameraId) > 0) { | 
|  | 256 | for (const auto& physicalId : unavailPhysicalIds[cameraId]) { | 
|  | 257 | String8 physicalId8 = String8(physicalId.c_str()); | 
|  | 258 | onDeviceStatusChanged(id8, physicalId8, CameraDeviceStatus::NOT_PRESENT); | 
|  | 259 | } | 
|  | 260 | } | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
| Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 263 | // Derive primary rear/front cameras, and filter their charactierstics. | 
|  | 264 | // This needs to be done after all cameras are enumerated and camera ids are sorted. | 
| Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 265 | if (SessionConfigurationUtils::IS_PERF_CLASS) { | 
| Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 266 | // Assume internal cameras are advertised from the same | 
|  | 267 | // provider. If multiple providers are registered at different time, | 
|  | 268 | // and each provider contains multiple internal color cameras, the current | 
|  | 269 | // logic may filter the characteristics of more than one front/rear color | 
|  | 270 | // cameras. | 
|  | 271 | Mutex::Autolock l(mServiceLock); | 
|  | 272 | filterSPerfClassCharacteristicsLocked(); | 
| Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 273 | } | 
| Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 274 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 275 | return OK; | 
|  | 276 | } | 
|  | 277 |  | 
| Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 278 | void CameraService::broadcastTorchModeStatus(const String8& cameraId, TorchModeStatus status, | 
|  | 279 | SystemCameraKind systemCameraKind) { | 
| Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 280 | Mutex::Autolock lock(mStatusListenerLock); | 
| Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 281 | for (auto& i : mListenerList) { | 
| Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 282 | if (shouldSkipStatusUpdates(systemCameraKind, i->isVendorListener(), i->getListenerPid(), | 
|  | 283 | i->getListenerUid())) { | 
|  | 284 | ALOGV("Skipping torch callback for system-only camera device %s", | 
|  | 285 | cameraId.c_str()); | 
|  | 286 | continue; | 
|  | 287 | } | 
| Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 288 | auto ret = i->getListener()->onTorchStatusChanged(mapToInterface(status), | 
|  | 289 | String16{cameraId}); | 
|  | 290 | i->handleBinderStatus(ret, "%s: Failed to trigger onTorchStatusChanged for %d:%d: %d", | 
|  | 291 | __FUNCTION__, i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); | 
| Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 292 | } | 
|  | 293 | } | 
|  | 294 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 295 | CameraService::~CameraService() { | 
| Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 296 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 297 | mUidPolicy->unregisterSelf(); | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 298 | mSensorPrivacyPolicy->unregisterSelf(); | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 299 | mInjectionStatusListener->removeListener(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 300 | } | 
|  | 301 |  | 
| Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 302 | void CameraService::onNewProviderRegistered() { | 
|  | 303 | enumerateProviders(); | 
|  | 304 | } | 
|  | 305 |  | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 306 | void CameraService::filterAPI1SystemCameraLocked( | 
|  | 307 | const std::vector<std::string> &normalDeviceIds) { | 
|  | 308 | mNormalDeviceIdsWithoutSystemCamera.clear(); | 
|  | 309 | for (auto &deviceId : normalDeviceIds) { | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 310 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
|  | 311 | if (getSystemCameraKind(String8(deviceId.c_str()), &deviceKind) != OK) { | 
|  | 312 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, deviceId.c_str()); | 
|  | 313 | continue; | 
|  | 314 | } | 
|  | 315 | if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) { | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 316 | // All system camera ids will necessarily come after public camera | 
|  | 317 | // device ids as per the HAL interface contract. | 
|  | 318 | break; | 
|  | 319 | } | 
|  | 320 | mNormalDeviceIdsWithoutSystemCamera.push_back(deviceId); | 
|  | 321 | } | 
|  | 322 | ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__, | 
|  | 323 | mNormalDeviceIdsWithoutSystemCamera.size()); | 
|  | 324 | } | 
|  | 325 |  | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 326 | status_t CameraService::getSystemCameraKind(const String8& cameraId, SystemCameraKind *kind) const { | 
|  | 327 | auto state = getCameraState(cameraId); | 
|  | 328 | if (state != nullptr) { | 
|  | 329 | *kind = state->getSystemCameraKind(); | 
|  | 330 | return OK; | 
|  | 331 | } | 
|  | 332 | // Hidden physical camera ids won't have CameraState | 
|  | 333 | return mCameraProviderManager->getSystemCameraKind(cameraId.c_str(), kind); | 
|  | 334 | } | 
|  | 335 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 336 | void CameraService::updateCameraNumAndIds() { | 
|  | 337 | Mutex::Autolock l(mServiceLock); | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 338 | std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount(); | 
|  | 339 | // Excludes hidden secure cameras | 
|  | 340 | mNumberOfCameras = | 
|  | 341 | systemAndNonSystemCameras.first + systemAndNonSystemCameras.second; | 
|  | 342 | mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second; | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 343 | mNormalDeviceIds = | 
|  | 344 | mCameraProviderManager->getAPI1CompatibleCameraDeviceIds(); | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 345 | filterAPI1SystemCameraLocked(mNormalDeviceIds); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 346 | } | 
|  | 347 |  | 
| Shuzhen Wang | b38b53f | 2021-07-15 12:46:09 -0700 | [diff] [blame] | 348 | void CameraService::filterSPerfClassCharacteristicsLocked() { | 
| Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 349 | // To claim to be S Performance primary cameras, the cameras must be | 
|  | 350 | // backward compatible. So performance class primary camera Ids must be API1 | 
|  | 351 | // compatible. | 
|  | 352 | bool firstRearCameraSeen = false, firstFrontCameraSeen = false; | 
|  | 353 | for (const auto& cameraId : mNormalDeviceIdsWithoutSystemCamera) { | 
|  | 354 | int facing = -1; | 
|  | 355 | int orientation = 0; | 
|  | 356 | String8 cameraId8(cameraId.c_str()); | 
| Shuzhen Wang | f221e8d | 2022-12-15 13:26:29 -0800 | [diff] [blame] | 357 | int portraitRotation; | 
|  | 358 | getDeviceVersion(cameraId8, /*overrideToPortrait*/false, /*out*/&portraitRotation, | 
|  | 359 | /*out*/&facing, /*out*/&orientation); | 
| Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 360 | if (facing == -1) { | 
|  | 361 | ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.c_str()); | 
|  | 362 | return; | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | if ((facing == hardware::CAMERA_FACING_BACK && !firstRearCameraSeen) || | 
|  | 366 | (facing == hardware::CAMERA_FACING_FRONT && !firstFrontCameraSeen)) { | 
| Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 367 | status_t res = mCameraProviderManager->filterSmallJpegSizes(cameraId); | 
|  | 368 | if (res == OK) { | 
|  | 369 | mPerfClassPrimaryCameraIds.insert(cameraId); | 
|  | 370 | } else { | 
|  | 371 | ALOGE("%s: Failed to filter small JPEG sizes for performance class primary " | 
|  | 372 | "camera %s: %s(%d)", __FUNCTION__, cameraId.c_str(), strerror(-res), res); | 
|  | 373 | break; | 
|  | 374 | } | 
| Shuzhen Wang | 89db299 | 2021-05-20 13:09:48 -0700 | [diff] [blame] | 375 |  | 
|  | 376 | if (facing == hardware::CAMERA_FACING_BACK) { | 
|  | 377 | firstRearCameraSeen = true; | 
|  | 378 | } | 
|  | 379 | if (facing == hardware::CAMERA_FACING_FRONT) { | 
|  | 380 | firstFrontCameraSeen = true; | 
|  | 381 | } | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | if (firstRearCameraSeen && firstFrontCameraSeen) { | 
|  | 385 | break; | 
|  | 386 | } | 
|  | 387 | } | 
|  | 388 | } | 
|  | 389 |  | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 390 | void CameraService::addStates(const String8 id) { | 
|  | 391 | std::string cameraId(id.c_str()); | 
| Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 392 | CameraResourceCost cost; | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 393 | status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost); | 
|  | 394 | if (res != OK) { | 
|  | 395 | ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res); | 
|  | 396 | return; | 
|  | 397 | } | 
| Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 398 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 399 | res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind); | 
|  | 400 | if (res != OK) { | 
|  | 401 | ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res); | 
|  | 402 | return; | 
|  | 403 | } | 
| Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 404 | std::vector<std::string> physicalCameraIds; | 
|  | 405 | mCameraProviderManager->isLogicalCamera(cameraId, &physicalCameraIds); | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 406 | std::set<String8> conflicting; | 
|  | 407 | for (size_t i = 0; i < cost.conflictingDevices.size(); i++) { | 
|  | 408 | conflicting.emplace(String8(cost.conflictingDevices[i].c_str())); | 
|  | 409 | } | 
|  | 410 |  | 
|  | 411 | { | 
|  | 412 | Mutex::Autolock lock(mCameraStatesLock); | 
|  | 413 | mCameraStates.emplace(id, std::make_shared<CameraState>(id, cost.resourceCost, | 
| Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 414 | conflicting, deviceKind, physicalCameraIds)); | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 415 | } | 
|  | 416 |  | 
|  | 417 | if (mFlashlight->hasFlashUnit(id)) { | 
| Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 418 | Mutex::Autolock al(mTorchStatusMutex); | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 419 | mTorchStatusMap.add(id, TorchModeStatus::AVAILABLE_OFF); | 
| Shuzhen Wang | 7d859d4 | 2018-11-06 15:33:23 -0800 | [diff] [blame] | 420 |  | 
| Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 421 | broadcastTorchModeStatus(id, TorchModeStatus::AVAILABLE_OFF, deviceKind); | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 422 | } | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 423 |  | 
|  | 424 | updateCameraNumAndIds(); | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 425 | logDeviceAdded(id, "Device added"); | 
|  | 426 | } | 
|  | 427 |  | 
| Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 428 | void CameraService::removeStates(const String8 id) { | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 429 | updateCameraNumAndIds(); | 
| Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 430 | if (mFlashlight->hasFlashUnit(id)) { | 
| Emilian Peev | 7f25e5f | 2018-04-11 16:50:34 +0100 | [diff] [blame] | 431 | Mutex::Autolock al(mTorchStatusMutex); | 
| Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 432 | mTorchStatusMap.removeItem(id); | 
|  | 433 | } | 
|  | 434 |  | 
|  | 435 | { | 
|  | 436 | Mutex::Autolock lock(mCameraStatesLock); | 
|  | 437 | mCameraStates.erase(id); | 
|  | 438 | } | 
|  | 439 | } | 
|  | 440 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 441 | void CameraService::onDeviceStatusChanged(const String8& id, | 
|  | 442 | CameraDeviceStatus newHalStatus) { | 
|  | 443 | ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__, | 
|  | 444 | id.string(), newHalStatus); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 445 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 446 | StatusInternal newStatus = mapToInternal(newHalStatus); | 
|  | 447 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 448 | std::shared_ptr<CameraState> state = getCameraState(id); | 
|  | 449 |  | 
|  | 450 | if (state == nullptr) { | 
| Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 451 | if (newStatus == StatusInternal::PRESENT) { | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 452 | ALOGI("%s: Unknown camera ID %s, a new camera is added", | 
| Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 453 | __FUNCTION__, id.string()); | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 454 |  | 
|  | 455 | // First add as absent to make sure clients are notified below | 
|  | 456 | addStates(id); | 
|  | 457 |  | 
|  | 458 | updateStatus(newStatus, id); | 
| Yin-Chia Yeh | 92e3321 | 2017-05-24 15:54:15 -0700 | [diff] [blame] | 459 | } else { | 
|  | 460 | ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string()); | 
|  | 461 | } | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 462 | return; | 
|  | 463 | } | 
|  | 464 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 465 | StatusInternal oldStatus = state->getStatus(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 466 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 467 | if (oldStatus == newStatus) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 468 | 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] | 469 | return; | 
|  | 470 | } | 
|  | 471 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 472 | if (newStatus == StatusInternal::NOT_PRESENT) { | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 473 | logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus, | 
|  | 474 | newStatus)); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 475 |  | 
|  | 476 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect | 
|  | 477 | // to this device until the status changes | 
|  | 478 | updateStatus(StatusInternal::NOT_PRESENT, id); | 
|  | 479 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 480 | sp<BasicClient> clientToDisconnectOnline, clientToDisconnectOffline; | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 481 | { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 482 | // Don't do this in updateStatus to avoid deadlock over mServiceLock | 
|  | 483 | Mutex::Autolock lock(mServiceLock); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 484 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 485 | // Remove cached shim parameters | 
|  | 486 | state->setShimParams(CameraParameters()); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 487 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 488 | // Remove online as well as offline client from the list of active clients, | 
|  | 489 | // if they are present | 
|  | 490 | clientToDisconnectOnline = removeClientLocked(id); | 
|  | 491 | clientToDisconnectOffline = removeClientLocked(kOfflineDevice + id); | 
| Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 492 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 493 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 494 | disconnectClient(id, clientToDisconnectOnline); | 
|  | 495 | disconnectClient(kOfflineDevice + id, clientToDisconnectOffline); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 496 |  | 
| Guennadi Liakhovetski | 6034bf5 | 2017-12-07 10:28:29 +0100 | [diff] [blame] | 497 | removeStates(id); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 498 | } else { | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 499 | if (oldStatus == StatusInternal::NOT_PRESENT) { | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 500 | logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus, | 
|  | 501 | newStatus)); | 
|  | 502 | } | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 503 | updateStatus(newStatus, id); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 504 | } | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 505 | } | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 506 |  | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 507 | void CameraService::onDeviceStatusChanged(const String8& id, | 
|  | 508 | const String8& physicalId, | 
|  | 509 | CameraDeviceStatus newHalStatus) { | 
|  | 510 | ALOGI("%s: Status changed for cameraId=%s, physicalCameraId=%s, newStatus=%d", | 
|  | 511 | __FUNCTION__, id.string(), physicalId.string(), newHalStatus); | 
|  | 512 |  | 
|  | 513 | StatusInternal newStatus = mapToInternal(newHalStatus); | 
|  | 514 |  | 
|  | 515 | std::shared_ptr<CameraState> state = getCameraState(id); | 
|  | 516 |  | 
|  | 517 | if (state == nullptr) { | 
|  | 518 | ALOGE("%s: Physical camera id %s status change on a non-present ID %s", | 
| Shuzhen Wang | 3d316f3 | 2022-10-25 20:33:34 +0000 | [diff] [blame] | 519 | __FUNCTION__, physicalId.string(), id.string()); | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 520 | return; | 
|  | 521 | } | 
|  | 522 |  | 
|  | 523 | StatusInternal logicalCameraStatus = state->getStatus(); | 
|  | 524 | if (logicalCameraStatus != StatusInternal::PRESENT && | 
|  | 525 | logicalCameraStatus != StatusInternal::NOT_AVAILABLE) { | 
|  | 526 | ALOGE("%s: Physical camera id %s status %d change for an invalid logical camera state %d", | 
|  | 527 | __FUNCTION__, physicalId.string(), newHalStatus, logicalCameraStatus); | 
|  | 528 | return; | 
|  | 529 | } | 
|  | 530 |  | 
|  | 531 | bool updated = false; | 
|  | 532 | if (newStatus == StatusInternal::PRESENT) { | 
|  | 533 | updated = state->removeUnavailablePhysicalId(physicalId); | 
|  | 534 | } else { | 
|  | 535 | updated = state->addUnavailablePhysicalId(physicalId); | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | if (updated) { | 
| Shuzhen Wang | 4fa28d2 | 2020-01-23 15:57:25 -0800 | [diff] [blame] | 539 | String8 idCombo = id + " : " + physicalId; | 
|  | 540 | if (newStatus == StatusInternal::PRESENT) { | 
|  | 541 | logDeviceAdded(idCombo, | 
|  | 542 | String8::format("Device status changed to %d", newStatus)); | 
|  | 543 | } else { | 
|  | 544 | logDeviceRemoved(idCombo, | 
|  | 545 | String8::format("Device status changed to %d", newStatus)); | 
|  | 546 | } | 
| Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 547 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) | 
|  | 548 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
|  | 549 | if (getSystemCameraKind(id, &deviceKind) != OK) { | 
|  | 550 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.string()); | 
|  | 551 | return; | 
|  | 552 | } | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 553 | String16 id16(id), physicalId16(physicalId); | 
|  | 554 | Mutex::Autolock lock(mStatusListenerLock); | 
|  | 555 | for (auto& listener : mListenerList) { | 
| Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 556 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), | 
|  | 557 | listener->getListenerPid(), listener->getListenerUid())) { | 
|  | 558 | ALOGV("Skipping discovery callback for system-only camera device %s", | 
|  | 559 | id.c_str()); | 
|  | 560 | continue; | 
|  | 561 | } | 
| Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 562 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged( | 
|  | 563 | mapToInterface(newStatus), id16, physicalId16); | 
|  | 564 | listener->handleBinderStatus(ret, | 
|  | 565 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", | 
|  | 566 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), | 
|  | 567 | ret.exceptionCode()); | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 568 | } | 
|  | 569 | } | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 570 | } | 
|  | 571 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 572 | void CameraService::disconnectClient(const String8& id, sp<BasicClient> clientToDisconnect) { | 
|  | 573 | if (clientToDisconnect.get() != nullptr) { | 
|  | 574 | ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL", | 
|  | 575 | __FUNCTION__, id.string()); | 
|  | 576 | // Notify the client of disconnection | 
|  | 577 | clientToDisconnect->notifyError( | 
|  | 578 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, | 
|  | 579 | CaptureResultExtras{}); | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 580 | clientToDisconnect->disconnect(); | 
|  | 581 | } | 
|  | 582 | } | 
|  | 583 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 584 | void CameraService::onTorchStatusChanged(const String8& cameraId, | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 585 | TorchModeStatus newStatus) { | 
| Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 586 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; | 
|  | 587 | status_t res = getSystemCameraKind(cameraId, &systemCameraKind); | 
|  | 588 | if (res != OK) { | 
|  | 589 | ALOGE("%s: Could not get system camera kind for camera id %s", __FUNCTION__, | 
|  | 590 | cameraId.string()); | 
|  | 591 | return; | 
|  | 592 | } | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 593 | Mutex::Autolock al(mTorchStatusMutex); | 
| Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 594 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 595 | } | 
|  | 596 |  | 
| Jayant Chowdhary | 46ef0f5 | 2021-10-05 14:36:13 -0700 | [diff] [blame] | 597 |  | 
|  | 598 | void CameraService::onTorchStatusChanged(const String8& cameraId, | 
|  | 599 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { | 
|  | 600 | Mutex::Autolock al(mTorchStatusMutex); | 
|  | 601 | onTorchStatusChangedLocked(cameraId, newStatus, systemCameraKind); | 
|  | 602 | } | 
|  | 603 |  | 
| Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 604 | void CameraService::broadcastTorchStrengthLevel(const String8& cameraId, | 
|  | 605 | int32_t newStrengthLevel) { | 
|  | 606 | Mutex::Autolock lock(mStatusListenerLock); | 
|  | 607 | for (auto& i : mListenerList) { | 
| Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 608 | auto ret = i->getListener()->onTorchStrengthLevelChanged(String16{cameraId}, | 
| Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 609 | newStrengthLevel); | 
| Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 610 | i->handleBinderStatus(ret, | 
|  | 611 | "%s: Failed to trigger onTorchStrengthLevelChanged for %d:%d: %d", __FUNCTION__, | 
|  | 612 | i->getListenerUid(), i->getListenerPid(), ret.exceptionCode()); | 
| Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 613 | } | 
|  | 614 | } | 
|  | 615 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 616 | void CameraService::onTorchStatusChangedLocked(const String8& cameraId, | 
| Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 617 | TorchModeStatus newStatus, SystemCameraKind systemCameraKind) { | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 618 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", | 
|  | 619 | __FUNCTION__, cameraId.string(), newStatus); | 
|  | 620 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 621 | TorchModeStatus status; | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 622 | status_t res = getTorchStatusLocked(cameraId, &status); | 
|  | 623 | if (res) { | 
| Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 624 | ALOGE("%s: cannot get torch status of camera %s: %s (%d)", | 
|  | 625 | __FUNCTION__, cameraId.string(), strerror(-res), res); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 626 | return; | 
|  | 627 | } | 
|  | 628 | if (status == newStatus) { | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 629 | return; | 
|  | 630 | } | 
|  | 631 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 632 | res = setTorchStatusLocked(cameraId, newStatus); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 633 | if (res) { | 
| Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 634 | ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__, | 
|  | 635 | (uint32_t)newStatus, strerror(-res), res); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 636 | return; | 
|  | 637 | } | 
|  | 638 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 639 | { | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 640 | // Update battery life logging for flashlight | 
| Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 641 | Mutex::Autolock al(mTorchUidMapMutex); | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 642 | auto iter = mTorchUidMap.find(cameraId); | 
|  | 643 | if (iter != mTorchUidMap.end()) { | 
|  | 644 | int oldUid = iter->second.second; | 
|  | 645 | int newUid = iter->second.first; | 
|  | 646 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); | 
|  | 647 | if (oldUid != newUid) { | 
|  | 648 | // 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] | 649 | if (status == TorchModeStatus::AVAILABLE_ON) { | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 650 | notifier.noteFlashlightOff(cameraId, oldUid); | 
|  | 651 | } | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 652 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 653 | notifier.noteFlashlightOn(cameraId, newUid); | 
|  | 654 | } | 
|  | 655 | iter->second.second = newUid; | 
|  | 656 | } else { | 
|  | 657 | // If the UID has not changed, log the status | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 658 | if (newStatus == TorchModeStatus::AVAILABLE_ON) { | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 659 | notifier.noteFlashlightOn(cameraId, oldUid); | 
|  | 660 | } else { | 
|  | 661 | notifier.noteFlashlightOff(cameraId, oldUid); | 
|  | 662 | } | 
|  | 663 | } | 
|  | 664 | } | 
|  | 665 | } | 
| Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 666 | broadcastTorchModeStatus(cameraId, newStatus, systemCameraKind); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 667 | } | 
|  | 668 |  | 
| Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 669 | static bool hasPermissionsForSystemCamera(int callingPid, int callingUid, | 
|  | 670 | bool logPermissionFailure = false) { | 
|  | 671 | return checkPermission(sSystemCameraPermission, callingPid, callingUid, | 
|  | 672 | logPermissionFailure) && | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 673 | checkPermission(sCameraPermission, callingPid, callingUid); | 
|  | 674 | } | 
|  | 675 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 676 | Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 677 | ATRACE_CALL(); | 
| Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 678 | Mutex::Autolock l(mServiceLock); | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 679 | bool hasSystemCameraPermissions = | 
|  | 680 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), | 
|  | 681 | CameraThreadState::getCallingUid()); | 
| Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 682 | switch (type) { | 
|  | 683 | case CAMERA_TYPE_BACKWARD_COMPATIBLE: | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 684 | if (hasSystemCameraPermissions) { | 
|  | 685 | *numCameras = static_cast<int>(mNormalDeviceIds.size()); | 
|  | 686 | } else { | 
|  | 687 | *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size()); | 
|  | 688 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 689 | break; | 
| Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 690 | case CAMERA_TYPE_ALL: | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 691 | if (hasSystemCameraPermissions) { | 
|  | 692 | *numCameras = mNumberOfCameras; | 
|  | 693 | } else { | 
|  | 694 | *numCameras = mNumberOfCamerasWithoutSystemCamera; | 
|  | 695 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 696 | break; | 
| Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 697 | default: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 698 | ALOGW("%s: Unknown camera type %d", | 
| Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 699 | __FUNCTION__, type); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 700 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 701 | "Unknown camera type %d", type); | 
| Eino-Ville Talvala | bad4358 | 2015-08-14 13:12:32 -0700 | [diff] [blame] | 702 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 703 | return Status::ok(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 704 | } | 
|  | 705 |  | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 706 | Status CameraService::getCameraInfo(int cameraId, bool overrideToPortrait, | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 707 | CameraInfo* cameraInfo) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 708 | ATRACE_CALL(); | 
| Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 709 | Mutex::Autolock l(mServiceLock); | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 710 | std::string cameraIdStr = cameraIdIntToStrLocked(cameraId); | 
|  | 711 | if (shouldRejectSystemCameraConnection(String8(cameraIdStr.c_str()))) { | 
|  | 712 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" | 
|  | 713 | "characteristics for system only device %s: ", cameraIdStr.c_str()); | 
|  | 714 | } | 
| Emilian Peev | aee727d | 2017-05-04 16:35:48 +0100 | [diff] [blame] | 715 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 716 | if (!mInitialized) { | 
| 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"); | 
| Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 720 | } | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 721 | bool hasSystemCameraPermissions = | 
|  | 722 | hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(), | 
|  | 723 | CameraThreadState::getCallingUid()); | 
|  | 724 | int cameraIdBound = mNumberOfCamerasWithoutSystemCamera; | 
|  | 725 | if (hasSystemCameraPermissions) { | 
|  | 726 | cameraIdBound = mNumberOfCameras; | 
|  | 727 | } | 
|  | 728 | if (cameraId < 0 || cameraId >= cameraIdBound) { | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 729 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, | 
|  | 730 | "CameraId is not valid"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 731 | } | 
|  | 732 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 733 | Status ret = Status::ok(); | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 734 | int portraitRotation; | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 735 | status_t err = mCameraProviderManager->getCameraInfo( | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 736 | cameraIdStr.c_str(), overrideToPortrait, &portraitRotation, cameraInfo); | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 737 | if (err != OK) { | 
|  | 738 | ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 739 | "Error retrieving camera info from device %d: %s (%d)", cameraId, | 
|  | 740 | strerror(-err), err); | 
| Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 741 | logServiceError(String8::format("Error retrieving camera info from device %d",cameraId), | 
|  | 742 | ERROR_INVALID_OPERATION); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 743 | } | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 744 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 745 | return ret; | 
|  | 746 | } | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 747 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 748 | std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt) { | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 749 | const std::vector<std::string> *deviceIds = &mNormalDeviceIdsWithoutSystemCamera; | 
|  | 750 | auto callingPid = CameraThreadState::getCallingPid(); | 
|  | 751 | auto callingUid = CameraThreadState::getCallingUid(); | 
| Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 752 | if (checkPermission(sSystemCameraPermission, callingPid, callingUid, | 
|  | 753 | /*logPermissionFailure*/false) || getpid() == callingPid) { | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 754 | deviceIds = &mNormalDeviceIds; | 
|  | 755 | } | 
|  | 756 | if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(deviceIds->size())) { | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 757 | ALOGE("%s: input id %d invalid: valid range  (0, %zu)", | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 758 | __FUNCTION__, cameraIdInt, deviceIds->size()); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 759 | return std::string{}; | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 760 | } | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 761 |  | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 762 | return (*deviceIds)[cameraIdInt]; | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 763 | } | 
|  | 764 |  | 
|  | 765 | String8 CameraService::cameraIdIntToStr(int cameraIdInt) { | 
|  | 766 | Mutex::Autolock lock(mServiceLock); | 
|  | 767 | return String8(cameraIdIntToStrLocked(cameraIdInt).c_str()); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 768 | } | 
|  | 769 |  | 
|  | 770 | Status CameraService::getCameraCharacteristics(const String16& cameraId, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 771 | int targetSdkVersion, bool overrideToPortrait, CameraMetadata* cameraInfo) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 772 | ATRACE_CALL(); | 
| Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 773 | if (!cameraInfo) { | 
|  | 774 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 775 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL"); | 
| Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 776 | } | 
|  | 777 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 778 | if (!mInitialized) { | 
|  | 779 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); | 
| Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 780 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 781 | return STATUS_ERROR(ERROR_DISCONNECTED, | 
|  | 782 | "Camera subsystem is not available");; | 
| Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 783 | } | 
|  | 784 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 785 | if (shouldRejectSystemCameraConnection(String8(cameraId))) { | 
|  | 786 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera" | 
|  | 787 | "characteristics for system only device %s: ", String8(cameraId).string()); | 
|  | 788 | } | 
|  | 789 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 790 | Status ret{}; | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 791 |  | 
| Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 792 |  | 
|  | 793 | std::string cameraIdStr = String8(cameraId).string(); | 
|  | 794 | bool overrideForPerfClass = | 
|  | 795 | SessionConfigurationUtils::targetPerfClassPrimaryCamera(mPerfClassPrimaryCameraIds, | 
|  | 796 | cameraIdStr, targetSdkVersion); | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 797 | status_t res = mCameraProviderManager->getCameraCharacteristics( | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 798 | cameraIdStr, overrideForPerfClass, cameraInfo, overrideToPortrait); | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 799 | if (res != OK) { | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 800 | if (res == NAME_NOT_FOUND) { | 
|  | 801 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to retrieve camera " | 
|  | 802 | "characteristics for unknown device %s: %s (%d)", String8(cameraId).string(), | 
|  | 803 | strerror(-res), res); | 
|  | 804 | } else { | 
| Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 805 | logServiceError(String8::format("Unable to retrieve camera characteristics for " | 
|  | 806 | "device %s.", String8(cameraId).string()),ERROR_INVALID_OPERATION); | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 807 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera " | 
|  | 808 | "characteristics for device %s: %s (%d)", String8(cameraId).string(), | 
|  | 809 | strerror(-res), res); | 
|  | 810 | } | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 811 | } | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 812 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
|  | 813 | if (getSystemCameraKind(String8(cameraId), &deviceKind) != OK) { | 
|  | 814 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, String8(cameraId).string()); | 
|  | 815 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera kind " | 
|  | 816 | "for device %s", String8(cameraId).string()); | 
|  | 817 | } | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 818 | int callingPid = CameraThreadState::getCallingPid(); | 
|  | 819 | int callingUid = CameraThreadState::getCallingUid(); | 
| Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 820 | std::vector<int32_t> tagsRemoved; | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 821 | // If it's not calling from cameraserver, check the permission only if | 
|  | 822 | // android.permission.CAMERA is required. If android.permission.SYSTEM_CAMERA was needed, | 
|  | 823 | // it would've already been checked in shouldRejectSystemCameraConnection. | 
| Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 824 | if ((callingPid != getpid()) && | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 825 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 826 | !checkPermission(sCameraPermission, callingPid, callingUid)) { | 
| Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 827 | res = cameraInfo->removePermissionEntries( | 
|  | 828 | mCameraProviderManager->getProviderTagIdLocked(String8(cameraId).string()), | 
|  | 829 | &tagsRemoved); | 
|  | 830 | if (res != OK) { | 
|  | 831 | cameraInfo->clear(); | 
|  | 832 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to remove camera" | 
|  | 833 | " characteristics needing camera permission for device %s: %s (%d)", | 
|  | 834 | String8(cameraId).string(), strerror(-res), res); | 
|  | 835 | } | 
|  | 836 | } | 
|  | 837 |  | 
|  | 838 | if (!tagsRemoved.empty()) { | 
|  | 839 | res = cameraInfo->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION, | 
|  | 840 | tagsRemoved.data(), tagsRemoved.size()); | 
|  | 841 | if (res != OK) { | 
|  | 842 | cameraInfo->clear(); | 
|  | 843 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to insert camera " | 
|  | 844 | "keys needing permission for device %s: %s (%d)", String8(cameraId).string(), | 
|  | 845 | strerror(-res), res); | 
|  | 846 | } | 
|  | 847 | } | 
|  | 848 |  | 
| Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 849 | return ret; | 
|  | 850 | } | 
|  | 851 |  | 
| Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 852 | Status CameraService::getTorchStrengthLevel(const String16& cameraId, | 
|  | 853 | int32_t* torchStrength) { | 
|  | 854 | ATRACE_CALL(); | 
|  | 855 | Mutex::Autolock l(mServiceLock); | 
|  | 856 | if (!mInitialized) { | 
|  | 857 | ALOGE("%s: Camera HAL couldn't be initialized.", __FUNCTION__); | 
|  | 858 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera HAL couldn't be initialized."); | 
|  | 859 | } | 
|  | 860 |  | 
|  | 861 | if(torchStrength == NULL) { | 
|  | 862 | ALOGE("%s: strength level must not be null.", __FUNCTION__); | 
|  | 863 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Strength level should not be null."); | 
|  | 864 | } | 
|  | 865 |  | 
|  | 866 | status_t res = mCameraProviderManager->getTorchStrengthLevel(String8(cameraId).string(), | 
|  | 867 | torchStrength); | 
|  | 868 | if (res != OK) { | 
|  | 869 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve torch " | 
|  | 870 | "strength level for device %s: %s (%d)", String8(cameraId).string(), | 
|  | 871 | strerror(-res), res); | 
|  | 872 | } | 
|  | 873 | ALOGI("%s: Torch strength level is: %d", __FUNCTION__, *torchStrength); | 
|  | 874 | return Status::ok(); | 
|  | 875 | } | 
|  | 876 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 877 | String8 CameraService::getFormattedCurrentTime() { | 
|  | 878 | time_t now = time(nullptr); | 
|  | 879 | char formattedTime[64]; | 
|  | 880 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); | 
|  | 881 | return String8(formattedTime); | 
|  | 882 | } | 
|  | 883 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 884 | Status CameraService::getCameraVendorTagDescriptor( | 
|  | 885 | /*out*/ | 
|  | 886 | hardware::camera2::params::VendorTagDescriptor* desc) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 887 | ATRACE_CALL(); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 888 | if (!mInitialized) { | 
|  | 889 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 890 | return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available"); | 
| Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 891 | } | 
| Eino-Ville Talvala | 1e74e24 | 2016-03-03 11:24:28 -0800 | [diff] [blame] | 892 | sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor(); | 
|  | 893 | if (globalDescriptor != nullptr) { | 
|  | 894 | *desc = *(globalDescriptor.get()); | 
|  | 895 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 896 | return Status::ok(); | 
| Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 897 | } | 
|  | 898 |  | 
| Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 899 | Status CameraService::getCameraVendorTagCache( | 
|  | 900 | /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) { | 
|  | 901 | ATRACE_CALL(); | 
|  | 902 | if (!mInitialized) { | 
|  | 903 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); | 
|  | 904 | return STATUS_ERROR(ERROR_DISCONNECTED, | 
|  | 905 | "Camera subsystem not available"); | 
|  | 906 | } | 
|  | 907 | sp<VendorTagDescriptorCache> globalCache = | 
|  | 908 | VendorTagDescriptorCache::getGlobalVendorTagCache(); | 
|  | 909 | if (globalCache != nullptr) { | 
|  | 910 | *cache = *(globalCache.get()); | 
|  | 911 | } | 
|  | 912 | return Status::ok(); | 
|  | 913 | } | 
|  | 914 |  | 
| Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 915 | void CameraService::clearCachedVariables() { | 
|  | 916 | BasicClient::BasicClient::sCameraService = nullptr; | 
|  | 917 | } | 
|  | 918 |  | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 919 | std::pair<int, IPCTransport> CameraService::getDeviceVersion(const String8& cameraId, | 
|  | 920 | bool overrideToPortrait, int* portraitRotation, int* facing, int* orientation) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 921 | ATRACE_CALL(); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 922 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 923 | int deviceVersion = 0; | 
|  | 924 |  | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 925 | status_t res; | 
|  | 926 | hardware::hidl_version maxVersion{0,0}; | 
| Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 927 | IPCTransport transport = IPCTransport::INVALID; | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 928 | res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(), | 
| Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 929 | &maxVersion, &transport); | 
|  | 930 | if (res != OK || transport == IPCTransport::INVALID) { | 
|  | 931 | ALOGE("%s: Unable to get highest supported version for camera id %s", __FUNCTION__, | 
|  | 932 | cameraId.string()); | 
|  | 933 | return std::make_pair(-1, IPCTransport::INVALID) ; | 
|  | 934 | } | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 935 | deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor()); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 936 |  | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 937 | hardware::CameraInfo info; | 
|  | 938 | if (facing) { | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 939 | res = mCameraProviderManager->getCameraInfo(cameraId.string(), overrideToPortrait, | 
|  | 940 | portraitRotation, &info); | 
| Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 941 | if (res != OK) { | 
|  | 942 | return std::make_pair(-1, IPCTransport::INVALID); | 
|  | 943 | } | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 944 | *facing = info.facing; | 
| Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 945 | if (orientation) { | 
|  | 946 | *orientation = info.orientation; | 
|  | 947 | } | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 948 | } | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 949 |  | 
| Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 950 | return std::make_pair(deviceVersion, transport); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 951 | } | 
|  | 952 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 953 | Status CameraService::filterGetInfoErrorCode(status_t err) { | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 954 | switch(err) { | 
|  | 955 | case NO_ERROR: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 956 | return Status::ok(); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 957 | case BAD_VALUE: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 958 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, | 
|  | 959 | "CameraId is not valid for HAL module"); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 960 | case NO_INIT: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 961 | return STATUS_ERROR(ERROR_DISCONNECTED, | 
|  | 962 | "Camera device not available"); | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 963 | default: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 964 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 965 | "Camera HAL encountered error %d: %s", | 
|  | 966 | err, strerror(-err)); | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 967 | } | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 968 | } | 
|  | 969 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 970 | Status CameraService::makeClient(const sp<CameraService>& cameraService, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 971 | const sp<IInterface>& cameraCb, const String16& packageName, bool systemNativeClient, | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 972 | const std::optional<String16>& featureId,  const String8& cameraId, | 
| Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 973 | int api1CameraId, int facing, int sensorOrientation, int clientPid, uid_t clientUid, | 
| Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 974 | int servicePid, std::pair<int, IPCTransport> deviceVersionAndTransport, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 975 | apiLevel effectiveApiLevel, bool overrideForPerfClass, bool overrideToPortrait, | 
|  | 976 | /*out*/sp<BasicClient>* client) { | 
| Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 977 | // For HIDL devices | 
|  | 978 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { | 
|  | 979 | // Create CameraClient based on device version reported by the HAL. | 
|  | 980 | int deviceVersion = deviceVersionAndTransport.first; | 
|  | 981 | switch(deviceVersion) { | 
|  | 982 | case CAMERA_DEVICE_API_VERSION_1_0: | 
|  | 983 | ALOGE("Camera using old HAL version: %d", deviceVersion); | 
|  | 984 | return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL, | 
|  | 985 | "Camera device \"%s\" HAL version %d no longer supported", | 
|  | 986 | cameraId.string(), deviceVersion); | 
|  | 987 | break; | 
|  | 988 | case CAMERA_DEVICE_API_VERSION_3_0: | 
|  | 989 | case CAMERA_DEVICE_API_VERSION_3_1: | 
|  | 990 | case CAMERA_DEVICE_API_VERSION_3_2: | 
|  | 991 | case CAMERA_DEVICE_API_VERSION_3_3: | 
|  | 992 | case CAMERA_DEVICE_API_VERSION_3_4: | 
|  | 993 | case CAMERA_DEVICE_API_VERSION_3_5: | 
|  | 994 | case CAMERA_DEVICE_API_VERSION_3_6: | 
|  | 995 | case CAMERA_DEVICE_API_VERSION_3_7: | 
|  | 996 | break; | 
|  | 997 | default: | 
|  | 998 | // Should not be reachable | 
|  | 999 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); | 
|  | 1000 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 1001 | "Camera device \"%s\" has unknown HAL version %d", | 
|  | 1002 | cameraId.string(), deviceVersion); | 
|  | 1003 | } | 
|  | 1004 | } | 
|  | 1005 | if (effectiveApiLevel == API_1) { // Camera1 API route | 
|  | 1006 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); | 
| Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1007 | *client = new Camera2Client(cameraService, tmp, cameraService->mCameraServiceProxyWrapper, | 
|  | 1008 | packageName, featureId, cameraId, api1CameraId, facing, sensorOrientation, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1009 | clientPid, clientUid, servicePid, overrideForPerfClass, overrideToPortrait); | 
|  | 1010 | ALOGI("%s: Camera1 API (legacy), override to portrait %d", __FUNCTION__, | 
|  | 1011 | overrideToPortrait); | 
| Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1012 | } else { // Camera2 API route | 
|  | 1013 | sp<hardware::camera2::ICameraDeviceCallbacks> tmp = | 
|  | 1014 | static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get()); | 
| Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 1015 | *client = new CameraDeviceClient(cameraService, tmp, | 
|  | 1016 | cameraService->mCameraServiceProxyWrapper, packageName, systemNativeClient, | 
|  | 1017 | featureId, cameraId, facing, sensorOrientation, clientPid, clientUid, servicePid, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1018 | overrideForPerfClass, overrideToPortrait); | 
|  | 1019 | ALOGI("%s: Camera2 API, override to portrait %d", __FUNCTION__, overrideToPortrait); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1020 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1021 | return Status::ok(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1022 | } | 
|  | 1023 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1024 | String8 CameraService::toString(std::set<userid_t> intSet) { | 
|  | 1025 | String8 s(""); | 
|  | 1026 | bool first = true; | 
|  | 1027 | for (userid_t i : intSet) { | 
|  | 1028 | if (first) { | 
|  | 1029 | s.appendFormat("%d", i); | 
|  | 1030 | first = false; | 
|  | 1031 | } else { | 
|  | 1032 | s.appendFormat(", %d", i); | 
|  | 1033 | } | 
|  | 1034 | } | 
|  | 1035 | return s; | 
|  | 1036 | } | 
|  | 1037 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1038 | int32_t CameraService::mapToInterface(TorchModeStatus status) { | 
|  | 1039 | int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; | 
|  | 1040 | switch (status) { | 
|  | 1041 | case TorchModeStatus::NOT_AVAILABLE: | 
|  | 1042 | serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; | 
|  | 1043 | break; | 
|  | 1044 | case TorchModeStatus::AVAILABLE_OFF: | 
|  | 1045 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; | 
|  | 1046 | break; | 
|  | 1047 | case TorchModeStatus::AVAILABLE_ON: | 
|  | 1048 | serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; | 
|  | 1049 | break; | 
|  | 1050 | default: | 
|  | 1051 | ALOGW("Unknown new flash status: %d", status); | 
|  | 1052 | } | 
|  | 1053 | return serviceStatus; | 
|  | 1054 | } | 
|  | 1055 |  | 
|  | 1056 | CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) { | 
|  | 1057 | StatusInternal serviceStatus = StatusInternal::NOT_PRESENT; | 
|  | 1058 | switch (status) { | 
|  | 1059 | case CameraDeviceStatus::NOT_PRESENT: | 
|  | 1060 | serviceStatus = StatusInternal::NOT_PRESENT; | 
|  | 1061 | break; | 
|  | 1062 | case CameraDeviceStatus::PRESENT: | 
|  | 1063 | serviceStatus = StatusInternal::PRESENT; | 
|  | 1064 | break; | 
|  | 1065 | case CameraDeviceStatus::ENUMERATING: | 
|  | 1066 | serviceStatus = StatusInternal::ENUMERATING; | 
|  | 1067 | break; | 
|  | 1068 | default: | 
|  | 1069 | ALOGW("Unknown new HAL device status: %d", status); | 
|  | 1070 | } | 
|  | 1071 | return serviceStatus; | 
|  | 1072 | } | 
|  | 1073 |  | 
|  | 1074 | int32_t CameraService::mapToInterface(StatusInternal status) { | 
|  | 1075 | int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; | 
|  | 1076 | switch (status) { | 
|  | 1077 | case StatusInternal::NOT_PRESENT: | 
|  | 1078 | serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT; | 
|  | 1079 | break; | 
|  | 1080 | case StatusInternal::PRESENT: | 
|  | 1081 | serviceStatus = ICameraServiceListener::STATUS_PRESENT; | 
|  | 1082 | break; | 
|  | 1083 | case StatusInternal::ENUMERATING: | 
|  | 1084 | serviceStatus = ICameraServiceListener::STATUS_ENUMERATING; | 
|  | 1085 | break; | 
|  | 1086 | case StatusInternal::NOT_AVAILABLE: | 
|  | 1087 | serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE; | 
|  | 1088 | break; | 
|  | 1089 | case StatusInternal::UNKNOWN: | 
|  | 1090 | serviceStatus = ICameraServiceListener::STATUS_UNKNOWN; | 
|  | 1091 | break; | 
|  | 1092 | default: | 
|  | 1093 | ALOGW("Unknown new internal device status: %d", status); | 
|  | 1094 | } | 
|  | 1095 | return serviceStatus; | 
|  | 1096 | } | 
|  | 1097 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1098 | Status CameraService::initializeShimMetadata(int cameraId) { | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1099 | int uid = CameraThreadState::getCallingUid(); | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1100 |  | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1101 | String16 internalPackageName("cameraserver"); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1102 | String8 id = String8::format("%d", cameraId); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1103 | Status ret = Status::ok(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1104 | sp<Client> tmp = nullptr; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1105 | if (!(ret = connectHelper<ICameraClient,Client>( | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1106 | sp<ICameraClient>{nullptr}, id, cameraId, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1107 | internalPackageName, /*systemNativeClient*/ false, {}, uid, USE_CALLING_PID, | 
| Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1108 | API_1, /*shimUpdateOnly*/ true, /*oomScoreOffset*/ 0, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1109 | /*targetSdkVersion*/ __ANDROID_API_FUTURE__, /*overrideToPortrait*/ true, /*out*/ tmp) | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1110 | ).isOk()) { | 
|  | 1111 | ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string()); | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1112 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1113 | return ret; | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1114 | } | 
|  | 1115 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1116 | Status CameraService::getLegacyParametersLazy(int cameraId, | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1117 | /*out*/ | 
|  | 1118 | CameraParameters* parameters) { | 
|  | 1119 |  | 
|  | 1120 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); | 
|  | 1121 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1122 | Status ret = Status::ok(); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1123 |  | 
|  | 1124 | if (parameters == NULL) { | 
|  | 1125 | ALOGE("%s: parameters must not be null", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1126 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1127 | } | 
|  | 1128 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1129 | String8 id = String8::format("%d", cameraId); | 
|  | 1130 |  | 
|  | 1131 | // Check if we already have parameters | 
|  | 1132 | { | 
|  | 1133 | // Scope for service lock | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1134 | Mutex::Autolock lock(mServiceLock); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1135 | auto cameraState = getCameraState(id); | 
|  | 1136 | if (cameraState == nullptr) { | 
|  | 1137 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1138 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 1139 | "Invalid camera ID: %s", id.string()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1140 | } | 
|  | 1141 | CameraParameters p = cameraState->getShimParams(); | 
|  | 1142 | if (!p.isEmpty()) { | 
|  | 1143 | *parameters = p; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1144 | return ret; | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1145 | } | 
|  | 1146 | } | 
|  | 1147 |  | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1148 | int64_t token = CameraThreadState::clearCallingIdentity(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1149 | ret = initializeShimMetadata(cameraId); | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1150 | CameraThreadState::restoreCallingIdentity(token); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1151 | if (!ret.isOk()) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1152 | // Error already logged by callee | 
|  | 1153 | return ret; | 
|  | 1154 | } | 
|  | 1155 |  | 
|  | 1156 | // Check for parameters again | 
|  | 1157 | { | 
|  | 1158 | // Scope for service lock | 
|  | 1159 | Mutex::Autolock lock(mServiceLock); | 
|  | 1160 | auto cameraState = getCameraState(id); | 
|  | 1161 | if (cameraState == nullptr) { | 
|  | 1162 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1163 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 1164 | "Invalid camera ID: %s", id.string()); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1165 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1166 | CameraParameters p = cameraState->getShimParams(); | 
|  | 1167 | if (!p.isEmpty()) { | 
|  | 1168 | *parameters = p; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1169 | return ret; | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1170 | } | 
|  | 1171 | } | 
|  | 1172 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1173 | ALOGE("%s: Parameters were not initialized, or were empty.  Device may not be present.", | 
|  | 1174 | __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1175 | return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters"); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1176 | } | 
|  | 1177 |  | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1178 | // Can camera service trust the caller based on the calling UID? | 
|  | 1179 | static bool isTrustedCallingUid(uid_t uid) { | 
|  | 1180 | switch (uid) { | 
| Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1181 | case AID_MEDIA:        // mediaserver | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1182 | case AID_CAMERASERVER: // cameraserver | 
| Eino-Ville Talvala | af9d030 | 2016-06-29 14:40:10 -0700 | [diff] [blame] | 1183 | case AID_RADIO:        // telephony | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1184 | return true; | 
|  | 1185 | default: | 
|  | 1186 | return false; | 
|  | 1187 | } | 
|  | 1188 | } | 
|  | 1189 |  | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 1190 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { | 
|  | 1191 | PermissionController pc; | 
|  | 1192 | uid = pc.getPackageUid(packageName, 0); | 
|  | 1193 | if (uid <= 0) { | 
|  | 1194 | ALOGE("Unknown package: '%s'", String8(packageName).string()); | 
|  | 1195 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).string()); | 
|  | 1196 | return BAD_VALUE; | 
|  | 1197 | } | 
|  | 1198 |  | 
|  | 1199 | if (userId < 0) { | 
|  | 1200 | ALOGE("Invalid user: %d", userId); | 
|  | 1201 | dprintf(err, "Invalid user: %d\n", userId); | 
|  | 1202 | return BAD_VALUE; | 
|  | 1203 | } | 
|  | 1204 |  | 
|  | 1205 | uid = multiuser_get_uid(userId, uid); | 
|  | 1206 | return NO_ERROR; | 
|  | 1207 | } | 
|  | 1208 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1209 | Status CameraService::validateConnectLocked(const String8& cameraId, | 
| Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1210 | const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid, | 
|  | 1211 | /*out*/int& originalClientPid) const { | 
| Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 1212 |  | 
| Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1213 | #ifdef __BRILLO__ | 
|  | 1214 | UNUSED(clientName8); | 
|  | 1215 | UNUSED(clientUid); | 
|  | 1216 | UNUSED(clientPid); | 
|  | 1217 | UNUSED(originalClientPid); | 
|  | 1218 | #else | 
| Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1219 | Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid, | 
|  | 1220 | originalClientPid); | 
| Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1221 | if (!allowed.isOk()) { | 
| Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1222 | return allowed; | 
|  | 1223 | } | 
| Alex Deymo | 9c2a2c2 | 2016-08-25 11:59:14 -0700 | [diff] [blame] | 1224 | #endif  // __BRILLO__ | 
| Christopher Wiley | ce761d1 | 2016-02-16 10:15:00 -0800 | [diff] [blame] | 1225 |  | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1226 | int callingPid = CameraThreadState::getCallingPid(); | 
| Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1227 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1228 | if (!mInitialized) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1229 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", | 
|  | 1230 | callingPid); | 
| Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1231 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, | 
|  | 1232 | "No camera HAL module available to open camera device \"%s\"", cameraId.string()); | 
| Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1233 | } | 
|  | 1234 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1235 | if (getCameraState(cameraId) == nullptr) { | 
|  | 1236 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, | 
|  | 1237 | cameraId.string()); | 
| Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1238 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, | 
|  | 1239 | "No camera device with ID \"%s\" available", cameraId.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1240 | } | 
|  | 1241 |  | 
| Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1242 | status_t err = checkIfDeviceIsUsable(cameraId); | 
|  | 1243 | if (err != NO_ERROR) { | 
|  | 1244 | switch(err) { | 
|  | 1245 | case -ENODEV: | 
|  | 1246 | case -EBUSY: | 
|  | 1247 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, | 
|  | 1248 | "No camera device with ID \"%s\" currently available", cameraId.string()); | 
|  | 1249 | default: | 
|  | 1250 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 1251 | "Unknown error connecting to ID \"%s\"", cameraId.string()); | 
|  | 1252 | } | 
|  | 1253 | } | 
|  | 1254 | return Status::ok(); | 
| Christopher Wiley | 0039bcf | 2016-02-05 10:29:50 -0800 | [diff] [blame] | 1255 | } | 
|  | 1256 |  | 
| Eino-Ville Talvala | 0492686 | 2016-03-02 15:42:53 -0800 | [diff] [blame] | 1257 | Status CameraService::validateClientPermissionsLocked(const String8& cameraId, | 
| Chien-Yu Chen | 7939aee | 2016-03-21 18:19:33 -0700 | [diff] [blame] | 1258 | const String8& clientName8, int& clientUid, int& clientPid, | 
|  | 1259 | /*out*/int& originalClientPid) const { | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1260 | int callingPid = CameraThreadState::getCallingPid(); | 
|  | 1261 | int callingUid = CameraThreadState::getCallingUid(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1262 |  | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1263 | // Check if we can trust clientUid | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1264 | if (clientUid == USE_CALLING_UID) { | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1265 | clientUid = callingUid; | 
|  | 1266 | } else if (!isTrustedCallingUid(callingUid)) { | 
|  | 1267 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " | 
|  | 1268 | "(don't trust clientUid %d)", callingPid, callingUid, clientUid); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1269 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 1270 | "Untrusted caller (calling PID %d, UID %d) trying to " | 
|  | 1271 | "forward camera access to camera %s for client %s (PID %d, UID %d)", | 
|  | 1272 | callingPid, callingUid, cameraId.string(), | 
|  | 1273 | clientName8.string(), clientUid, clientPid); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1274 | } | 
|  | 1275 |  | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1276 | // Check if we can trust clientPid | 
|  | 1277 | if (clientPid == USE_CALLING_PID) { | 
|  | 1278 | clientPid = callingPid; | 
|  | 1279 | } else if (!isTrustedCallingUid(callingUid)) { | 
|  | 1280 | ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected " | 
|  | 1281 | "(don't trust clientPid %d)", callingPid, callingUid, clientPid); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1282 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 1283 | "Untrusted caller (calling PID %d, UID %d) trying to " | 
|  | 1284 | "forward camera access to camera %s for client %s (PID %d, UID %d)", | 
|  | 1285 | callingPid, callingUid, cameraId.string(), | 
|  | 1286 | clientName8.string(), clientUid, clientPid); | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1287 | } | 
|  | 1288 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1289 | if (shouldRejectSystemCameraConnection(cameraId)) { | 
|  | 1290 | ALOGW("Attempting to connect to system-only camera id %s, connection rejected", | 
|  | 1291 | cameraId.c_str()); | 
|  | 1292 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, "No camera device with ID \"%s\" is" | 
|  | 1293 | "available", cameraId.string()); | 
|  | 1294 | } | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1295 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
|  | 1296 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { | 
|  | 1297 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); | 
|  | 1298 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "No camera device with ID \"%s\"" | 
|  | 1299 | "found while trying to query device kind", cameraId.string()); | 
|  | 1300 |  | 
|  | 1301 | } | 
|  | 1302 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1303 | // If it's not calling from cameraserver, check the permission if the | 
|  | 1304 | // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for | 
|  | 1305 | // android.permission.SYSTEM_CAMERA for system only camera devices). | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1306 | if (callingPid != getpid() && | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1307 | (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) && | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1308 | !checkPermission(sCameraPermission, clientPid, clientUid)) { | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1309 | 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] | 1310 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 1311 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission", | 
|  | 1312 | clientName8.string(), clientUid, clientPid, cameraId.string()); | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1313 | } | 
|  | 1314 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1315 | // 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] | 1316 | if (!mUidPolicy->isUidActive(callingUid, String16(clientName8))) { | 
| Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1317 | int32_t procState = mUidPolicy->getProcState(callingUid); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1318 | ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d", | 
|  | 1319 | clientPid, clientUid); | 
|  | 1320 | return STATUS_ERROR_FMT(ERROR_DISABLED, | 
| Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 1321 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background (" | 
|  | 1322 | "calling UID %d proc state %" PRId32 ")", | 
|  | 1323 | clientName8.string(), clientUid, clientPid, cameraId.string(), | 
|  | 1324 | callingUid, procState); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 1325 | } | 
|  | 1326 |  | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 1327 | // If sensor privacy is enabled then prevent access to the camera | 
|  | 1328 | if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { | 
|  | 1329 | ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled"); | 
|  | 1330 | return STATUS_ERROR_FMT(ERROR_DISABLED, | 
|  | 1331 | "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" when sensor privacy " | 
|  | 1332 | "is enabled", clientName8.string(), clientUid, clientPid, cameraId.string()); | 
|  | 1333 | } | 
|  | 1334 |  | 
| Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1335 | // Only use passed in clientPid to check permission. Use calling PID as the client PID that's | 
|  | 1336 | // connected to camera service directly. | 
| Chien-Yu Chen | 18df60e | 2016-03-18 18:18:09 -0700 | [diff] [blame] | 1337 | originalClientPid = clientPid; | 
| Chien-Yu Chen | 4f3d620 | 2016-03-22 10:50:23 -0700 | [diff] [blame] | 1338 | clientPid = callingPid; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1339 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1340 | userid_t clientUserId = multiuser_get_user_id(clientUid); | 
| Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 1341 |  | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1342 | // For non-system clients : Only allow clients who are being used by the current foreground | 
|  | 1343 | // device user, unless calling from our own process. | 
|  | 1344 | if (!doesClientHaveSystemUid() && callingPid != getpid() && | 
| Jayant Chowdhary | 8ec41c1 | 2019-02-21 20:17:22 -0800 | [diff] [blame] | 1345 | (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) { | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 1346 | ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from " | 
|  | 1347 | "device user %d, currently allowed device users: %s)", callingPid, clientUserId, | 
|  | 1348 | toString(mAllowedUsers).string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1349 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 1350 | "Callers from device user %d are not currently allowed to connect to camera \"%s\"", | 
|  | 1351 | clientUserId, cameraId.string()); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 1352 | } | 
|  | 1353 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1354 | return Status::ok(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1355 | } | 
|  | 1356 |  | 
|  | 1357 | status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const { | 
|  | 1358 | auto cameraState = getCameraState(cameraId); | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1359 | int callingPid = CameraThreadState::getCallingPid(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1360 | if (cameraState == nullptr) { | 
|  | 1361 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, | 
|  | 1362 | cameraId.string()); | 
|  | 1363 | return -ENODEV; | 
|  | 1364 | } | 
|  | 1365 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1366 | StatusInternal currentStatus = cameraState->getStatus(); | 
|  | 1367 | if (currentStatus == StatusInternal::NOT_PRESENT) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1368 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", | 
|  | 1369 | callingPid, cameraId.string()); | 
| Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1370 | return -ENODEV; | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1371 | } else if (currentStatus == StatusInternal::ENUMERATING) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1372 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", | 
|  | 1373 | callingPid, cameraId.string()); | 
| Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1374 | return -EBUSY; | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1375 | } | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1376 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1377 | return NO_ERROR; | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1378 | } | 
|  | 1379 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1380 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1381 | const CameraService::DescriptorPtr& desc, int oomScoreOffset, bool systemNativeClient) { | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1382 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1383 | // Make a descriptor for the incoming client | 
| Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1384 | auto clientDescriptor = | 
|  | 1385 | CameraService::CameraClientManager::makeClientDescriptor(client, desc, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1386 | oomScoreOffset, systemNativeClient); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1387 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); | 
|  | 1388 |  | 
|  | 1389 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), | 
|  | 1390 | String8(client->getPackageName())); | 
|  | 1391 |  | 
|  | 1392 | if (evicted.size() > 0) { | 
|  | 1393 | // This should never happen - clients should already have been removed in disconnect | 
|  | 1394 | for (auto& i : evicted) { | 
|  | 1395 | ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect", | 
|  | 1396 | __FUNCTION__, i->getKey().string()); | 
|  | 1397 | } | 
|  | 1398 |  | 
|  | 1399 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", | 
|  | 1400 | __FUNCTION__); | 
|  | 1401 | } | 
| Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 1402 |  | 
|  | 1403 | // And register a death notification for the client callback. Do | 
|  | 1404 | // this last to avoid Binder policy where a nested Binder | 
|  | 1405 | // transaction might be pre-empted to service the client death | 
|  | 1406 | // notification if the client process dies before linkToDeath is | 
|  | 1407 | // invoked. | 
|  | 1408 | sp<IBinder> remoteCallback = client->getRemote(); | 
|  | 1409 | if (remoteCallback != nullptr) { | 
|  | 1410 | remoteCallback->linkToDeath(this); | 
|  | 1411 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1412 | } | 
|  | 1413 |  | 
|  | 1414 | status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid, | 
|  | 1415 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1416 | int oomScoreOffset, bool systemNativeClient, | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1417 | /*out*/ | 
|  | 1418 | sp<BasicClient>* client, | 
|  | 1419 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1420 | ATRACE_CALL(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1421 | status_t ret = NO_ERROR; | 
| Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1422 | std::vector<DescriptorPtr> evictedClients; | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1423 | DescriptorPtr clientDescriptor; | 
|  | 1424 | { | 
|  | 1425 | if (effectiveApiLevel == API_1) { | 
|  | 1426 | // If we are using API1, any existing client for this camera ID with the same remote | 
|  | 1427 | // should be returned rather than evicted to allow MediaRecorder to work properly. | 
|  | 1428 |  | 
|  | 1429 | auto current = mActiveClientManager.get(cameraId); | 
|  | 1430 | if (current != nullptr) { | 
|  | 1431 | auto clientSp = current->getValue(); | 
|  | 1432 | if (clientSp.get() != nullptr) { // should never be needed | 
| Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1433 | if (!clientSp->canCastToApiClient(effectiveApiLevel)) { | 
| Shuzhen Wang | b2d43f6 | 2021-08-25 14:01:11 -0700 | [diff] [blame] | 1434 | ALOGW("CameraService connect called with a different" | 
| Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1435 | " API level, evicting prior client..."); | 
|  | 1436 | } else if (clientSp->getRemote() == remoteCallback) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1437 | ALOGI("CameraService::connect X (PID %d) (second call from same" | 
| Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 1438 | " app binder, returning the same client)", clientPid); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1439 | *client = clientSp; | 
|  | 1440 | return NO_ERROR; | 
|  | 1441 | } | 
|  | 1442 | } | 
| Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1443 | } | 
| Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1444 | } | 
| Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 1445 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1446 | // Get current active client PIDs | 
|  | 1447 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); | 
|  | 1448 | ownerPids.push_back(clientPid); | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1449 |  | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1450 | std::vector<int> priorityScores(ownerPids.size()); | 
|  | 1451 | std::vector<int> states(ownerPids.size()); | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1452 |  | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1453 | // Get priority scores of all active PIDs | 
|  | 1454 | status_t err = ProcessInfoService::getProcessStatesScoresFromPids( | 
|  | 1455 | ownerPids.size(), &ownerPids[0], /*out*/&states[0], | 
|  | 1456 | /*out*/&priorityScores[0]); | 
|  | 1457 | if (err != OK) { | 
|  | 1458 | ALOGE("%s: Priority score query failed: %d", | 
|  | 1459 | __FUNCTION__, err); | 
|  | 1460 | return err; | 
|  | 1461 | } | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1462 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1463 | // Update all active clients' priorities | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1464 | std::map<int,resource_policy::ClientPriority> pidToPriorityMap; | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1465 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1466 | pidToPriorityMap.emplace(ownerPids[i], | 
| Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 1467 | resource_policy::ClientPriority(priorityScores[i], states[i], | 
| Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1468 | /* isVendorClient won't get copied over*/ false, | 
|  | 1469 | /* oomScoreOffset won't get copied over*/ 0)); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1470 | } | 
|  | 1471 | mActiveClientManager.updatePriorities(pidToPriorityMap); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1472 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1473 | // Get state for the given cameraId | 
|  | 1474 | auto state = getCameraState(cameraId); | 
|  | 1475 | if (state == nullptr) { | 
|  | 1476 | ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", | 
|  | 1477 | clientPid, cameraId.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1478 | // Should never get here because validateConnectLocked should have errored out | 
| Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1479 | return BAD_VALUE; | 
| Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1480 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1481 |  | 
| Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1482 | int32_t actualScore = priorityScores[priorityScores.size() - 1]; | 
|  | 1483 | int32_t actualState = states[states.size() - 1]; | 
|  | 1484 |  | 
|  | 1485 | // Make descriptor for incoming client. We store the oomScoreOffset | 
|  | 1486 | // since we might need it later on new handleEvictionsLocked and | 
|  | 1487 | // ProcessInfoService would not take that into account. | 
| Shuzhen Wang | 2db86ff | 2018-04-25 01:11:17 +0000 | [diff] [blame] | 1488 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1489 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), | 
| Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1490 | state->getConflicting(), actualScore, clientPid, actualState, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1491 | oomScoreOffset, systemNativeClient); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1492 |  | 
| Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1493 | resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); | 
|  | 1494 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1495 | // Find clients that would be evicted | 
|  | 1496 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); | 
|  | 1497 |  | 
|  | 1498 | // If the incoming client was 'evicted,' higher priority clients have the camera in the | 
|  | 1499 | // background, so we cannot do evictions | 
|  | 1500 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { | 
|  | 1501 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" | 
|  | 1502 | " priority).", clientPid); | 
|  | 1503 |  | 
|  | 1504 | sp<BasicClient> clientSp = clientDescriptor->getValue(); | 
|  | 1505 | String8 curTime = getFormattedCurrentTime(); | 
|  | 1506 | auto incompatibleClients = | 
|  | 1507 | mActiveClientManager.getIncompatibleClients(clientDescriptor); | 
|  | 1508 |  | 
|  | 1509 | 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] | 1510 | "(PID %d, score %d state %d) due to eviction policy", curTime.string(), | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1511 | cameraId.string(), packageName.string(), clientPid, | 
| Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1512 | clientPriority.getScore(), clientPriority.getState()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1513 |  | 
|  | 1514 | for (auto& i : incompatibleClients) { | 
|  | 1515 | msg.appendFormat("\n   - Blocked by existing device %s client for package %s" | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1516 | "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")", | 
|  | 1517 | i->getKey().string(), | 
|  | 1518 | String8{i->getValue()->getPackageName()}.string(), | 
|  | 1519 | i->getOwnerId(), i->getPriority().getScore(), | 
|  | 1520 | i->getPriority().getState()); | 
| Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1521 | ALOGE("   Conflicts with: Device %s, client package %s (PID %" | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1522 | PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(), | 
| Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 1523 | String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(), | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1524 | i->getPriority().getScore(), i->getPriority().getState()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1525 | } | 
|  | 1526 |  | 
|  | 1527 | // Log the client's attempt | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1528 | Mutex::Autolock l(mLogLock); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1529 | mEventLog.add(msg); | 
|  | 1530 |  | 
| Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1531 | auto current = mActiveClientManager.get(cameraId); | 
|  | 1532 | if (current != nullptr) { | 
|  | 1533 | return -EBUSY; // CAMERA_IN_USE | 
|  | 1534 | } else { | 
|  | 1535 | return -EUSERS; // MAX_CAMERAS_IN_USE | 
|  | 1536 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1537 | } | 
|  | 1538 |  | 
|  | 1539 | for (auto& i : evicted) { | 
|  | 1540 | sp<BasicClient> clientSp = i->getValue(); | 
|  | 1541 | if (clientSp.get() == nullptr) { | 
|  | 1542 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); | 
|  | 1543 |  | 
|  | 1544 | // TODO: Remove this | 
|  | 1545 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", | 
|  | 1546 | __FUNCTION__); | 
|  | 1547 | mActiveClientManager.remove(i); | 
|  | 1548 | continue; | 
|  | 1549 | } | 
|  | 1550 |  | 
|  | 1551 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", | 
|  | 1552 | i->getKey().string()); | 
| Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1553 | evictedClients.push_back(i); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1554 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1555 | // Log the clients evicted | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 1556 | logEvent(String8::format("EVICT device %s client held by package %s (PID" | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1557 | " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for" | 
|  | 1558 | " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")", | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1559 | i->getKey().string(), String8{clientSp->getPackageName()}.string(), | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 1560 | i->getOwnerId(), i->getPriority().getScore(), | 
|  | 1561 | i->getPriority().getState(), cameraId.string(), | 
| Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1562 | packageName.string(), clientPid, clientPriority.getScore(), | 
|  | 1563 | clientPriority.getState())); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1564 |  | 
|  | 1565 | // Notify the client of disconnection | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1566 | clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1567 | CaptureResultExtras()); | 
| Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1568 | } | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1569 | } | 
|  | 1570 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1571 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking | 
|  | 1572 | // other clients from connecting in mServiceLockWrapper if held | 
|  | 1573 | mServiceLock.unlock(); | 
|  | 1574 |  | 
|  | 1575 | // Clear caller identity temporarily so client disconnect PID checks work correctly | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1576 | int64_t token = CameraThreadState::clearCallingIdentity(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1577 |  | 
|  | 1578 | // Destroy evicted clients | 
|  | 1579 | for (auto& i : evictedClients) { | 
|  | 1580 | // 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] | 1581 | i->getValue()->disconnect(); // Clients will remove themselves from the active client list | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1582 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1583 |  | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1584 | CameraThreadState::restoreCallingIdentity(token); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1585 |  | 
| Ruben Brunk | 4f9576b | 2015-04-10 17:26:56 -0700 | [diff] [blame] | 1586 | for (const auto& i : evictedClients) { | 
|  | 1587 | ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")", | 
|  | 1588 | __FUNCTION__, i->getKey().string(), i->getOwnerId()); | 
|  | 1589 | ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS); | 
|  | 1590 | if (ret == TIMED_OUT) { | 
|  | 1591 | ALOGE("%s: Timed out waiting for client for device %s to disconnect, " | 
|  | 1592 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), | 
|  | 1593 | mActiveClientManager.toString().string()); | 
|  | 1594 | return -EBUSY; | 
|  | 1595 | } | 
|  | 1596 | if (ret != NO_ERROR) { | 
|  | 1597 | ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), " | 
|  | 1598 | "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret), | 
|  | 1599 | ret, mActiveClientManager.toString().string()); | 
|  | 1600 | return ret; | 
|  | 1601 | } | 
|  | 1602 | } | 
|  | 1603 |  | 
|  | 1604 | evictedClients.clear(); | 
|  | 1605 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1606 | // Once clients have been disconnected, relock | 
|  | 1607 | mServiceLock.lock(); | 
|  | 1608 |  | 
|  | 1609 | // Check again if the device was unplugged or something while we weren't holding mServiceLock | 
|  | 1610 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { | 
|  | 1611 | return ret; | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1612 | } | 
|  | 1613 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1614 | *partial = clientDescriptor; | 
|  | 1615 | return NO_ERROR; | 
| Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1616 | } | 
|  | 1617 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1618 | Status CameraService::connect( | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1619 | const sp<ICameraClient>& cameraClient, | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1620 | int api1CameraId, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1621 | const String16& clientPackageName, | 
| Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1622 | int clientUid, | 
| Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1623 | int clientPid, | 
| Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1624 | int targetSdkVersion, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1625 | bool overrideToPortrait, | 
| Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1626 | /*out*/ | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1627 | sp<ICamera>* device) { | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1628 |  | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1629 | ATRACE_CALL(); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1630 | Status ret = Status::ok(); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1631 |  | 
|  | 1632 | String8 id = cameraIdIntToStr(api1CameraId); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1633 | sp<Client> client = nullptr; | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 1634 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1635 | clientPackageName,/*systemNativeClient*/ false, {}, clientUid, clientPid, API_1, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1636 | /*shimUpdateOnly*/ false, /*oomScoreOffset*/ 0, targetSdkVersion, | 
|  | 1637 | overrideToPortrait, /*out*/client); | 
| Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1638 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1639 | if(!ret.isOk()) { | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 1640 | logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageName), | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1641 | ret.toString8()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1642 | return ret; | 
| Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1643 | } | 
|  | 1644 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1645 | *device = client; | 
|  | 1646 | return ret; | 
| Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1647 | } | 
|  | 1648 |  | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1649 | bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind, | 
|  | 1650 | bool isVendorListener, int clientPid, int clientUid) { | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1651 | // If the client is not a vendor client, don't add listener if | 
|  | 1652 | //   a) the camera is a publicly hidden secure camera OR | 
|  | 1653 | //   b) the camera is a system only camera and the client doesn't | 
|  | 1654 | //      have android.permission.SYSTEM_CAMERA permissions. | 
|  | 1655 | if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA || | 
|  | 1656 | (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && | 
|  | 1657 | !hasPermissionsForSystemCamera(clientPid, clientUid)))) { | 
| Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 1658 | return true; | 
|  | 1659 | } | 
|  | 1660 | return false; | 
|  | 1661 | } | 
|  | 1662 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1663 | bool CameraService::shouldRejectSystemCameraConnection(const String8& cameraId) const { | 
|  | 1664 | // Rules for rejection: | 
|  | 1665 | // 1) If cameraserver tries to access this camera device, accept the | 
|  | 1666 | //    connection. | 
|  | 1667 | // 2) The camera device is a publicly hidden secure camera device AND some | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1668 | //    non system component is trying to access it. | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1669 | // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY | 
|  | 1670 | //    and the serving thread is a non hwbinder thread, the client must have | 
|  | 1671 | //    android.permission.SYSTEM_CAMERA permissions to connect. | 
|  | 1672 |  | 
|  | 1673 | int cPid = CameraThreadState::getCallingPid(); | 
|  | 1674 | int cUid = CameraThreadState::getCallingUid(); | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1675 | bool systemClient = doesClientHaveSystemUid(); | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1676 | SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC; | 
|  | 1677 | if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) { | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1678 | // This isn't a known camera ID, so it's not a system camera | 
|  | 1679 | ALOGV("%s: Unknown camera id %s, ", __FUNCTION__, cameraId.c_str()); | 
|  | 1680 | return false; | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 1681 | } | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1682 |  | 
|  | 1683 | // (1) Cameraserver trying to connect, accept. | 
|  | 1684 | if (CameraThreadState::getCallingPid() == getpid()) { | 
|  | 1685 | return false; | 
|  | 1686 | } | 
|  | 1687 | // (2) | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1688 | if (!systemClient && systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) { | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1689 | ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str()); | 
|  | 1690 | return true; | 
|  | 1691 | } | 
|  | 1692 | // (3) Here we only check for permissions if it is a system only camera device. This is since | 
|  | 1693 | //     getCameraCharacteristics() allows for calls to succeed (albeit after hiding some | 
|  | 1694 | //     characteristics) even if clients don't have android.permission.CAMERA. We do not want the | 
|  | 1695 | //     same behavior for system camera devices. | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1696 | if (!systemClient && systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA && | 
| Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 1697 | !hasPermissionsForSystemCamera(cPid, cUid, /*logPermissionFailure*/true)) { | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 1698 | ALOGW("Rejecting access to system only camera %s, inadequete permissions", | 
|  | 1699 | cameraId.c_str()); | 
|  | 1700 | return true; | 
|  | 1701 | } | 
|  | 1702 |  | 
|  | 1703 | return false; | 
|  | 1704 | } | 
|  | 1705 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1706 | Status CameraService::connectDevice( | 
|  | 1707 | const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1708 | const String16& cameraId, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 1709 | const String16& clientPackageName, | 
| Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1710 | const std::optional<String16>& clientFeatureId, | 
| Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1711 | int clientUid, int oomScoreOffset, int targetSdkVersion, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1712 | bool overrideToPortrait, | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1713 | /*out*/ | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1714 | sp<hardware::camera2::ICameraDeviceUser>* device) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1715 |  | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 1716 | ATRACE_CALL(); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1717 | Status ret = Status::ok(); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1718 | String8 id = String8(cameraId); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1719 | sp<CameraDeviceClient> client = nullptr; | 
| Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1720 | String16 clientPackageNameAdj = clientPackageName; | 
| Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1721 | int callingPid = CameraThreadState::getCallingPid(); | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1722 | bool systemNativeClient = false; | 
|  | 1723 | if (doesClientHaveSystemUid() && (clientPackageNameAdj.size() == 0)) { | 
|  | 1724 | std::string systemClient = | 
|  | 1725 | StringPrintf("client.pid<%d>", CameraThreadState::getCallingPid()); | 
|  | 1726 | clientPackageNameAdj = String16(systemClient.c_str()); | 
|  | 1727 | systemNativeClient = true; | 
| Jayant Chowdhary | 5bf11bf | 2019-06-24 19:42:56 -0700 | [diff] [blame] | 1728 | } | 
| Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1729 |  | 
|  | 1730 | if (oomScoreOffset < 0) { | 
|  | 1731 | String8 msg = | 
|  | 1732 | String8::format("Cannot increase the priority of a client %s pid %d for " | 
|  | 1733 | "camera id %s", String8(clientPackageNameAdj).string(), callingPid, | 
|  | 1734 | id.string()); | 
|  | 1735 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 1736 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); | 
|  | 1737 | } | 
|  | 1738 |  | 
| Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 1739 | userid_t clientUserId = multiuser_get_user_id(clientUid); | 
|  | 1740 | int callingUid = CameraThreadState::getCallingUid(); | 
|  | 1741 | if (clientUid == USE_CALLING_UID) { | 
|  | 1742 | clientUserId = multiuser_get_user_id(callingUid); | 
|  | 1743 | } | 
|  | 1744 |  | 
|  | 1745 | if (mCameraServiceProxyWrapper->isCameraDisabled(clientUserId)) { | 
| Austin Borger | 5f7abe2 | 2022-04-26 15:55:10 -0700 | [diff] [blame] | 1746 | String8 msg = | 
|  | 1747 | String8::format("Camera disabled by device policy"); | 
|  | 1748 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 1749 | return STATUS_ERROR(ERROR_DISABLED, msg.string()); | 
|  | 1750 | } | 
|  | 1751 |  | 
| Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1752 | // enforce system camera permissions | 
|  | 1753 | if (oomScoreOffset > 0 && | 
| Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 1754 | !hasPermissionsForSystemCamera(callingPid, CameraThreadState::getCallingUid()) && | 
|  | 1755 | !isTrustedCallingUid(CameraThreadState::getCallingUid())) { | 
| Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1756 | String8 msg = | 
|  | 1757 | String8::format("Cannot change the priority of a client %s pid %d for " | 
|  | 1758 | "camera id %s without SYSTEM_CAMERA permissions", | 
|  | 1759 | String8(clientPackageNameAdj).string(), callingPid, id.string()); | 
|  | 1760 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 1761 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, msg.string()); | 
|  | 1762 | } | 
|  | 1763 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1764 | ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1765 | /*api1CameraId*/-1, clientPackageNameAdj, systemNativeClient,clientFeatureId, | 
| Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1766 | clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, oomScoreOffset, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1767 | targetSdkVersion, overrideToPortrait, /*out*/client); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1768 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1769 | if(!ret.isOk()) { | 
| Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 1770 | logRejected(id, callingPid, String8(clientPackageNameAdj), ret.toString8()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1771 | return ret; | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1772 | } | 
|  | 1773 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1774 | *device = client; | 
| Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 1775 | Mutex::Autolock lock(mServiceLock); | 
|  | 1776 |  | 
|  | 1777 | // Clear the previous cached logs and reposition the | 
|  | 1778 | // file offset to beginning of the file to log new data. | 
|  | 1779 | // If either truncate or lseek fails, close the previous file and create a new one. | 
|  | 1780 | if ((ftruncate(mMemFd, 0) == -1) || (lseek(mMemFd, 0, SEEK_SET) == -1)) { | 
|  | 1781 | ALOGE("%s: Error while truncating the file: %s", __FUNCTION__, sFileName); | 
|  | 1782 | // Close the previous memfd. | 
|  | 1783 | close(mMemFd); | 
|  | 1784 | // If failure to wipe the data, then create a new file and | 
|  | 1785 | // assign the new value to mMemFd. | 
|  | 1786 | mMemFd = memfd_create(sFileName, MFD_ALLOW_SEALING); | 
|  | 1787 | if (mMemFd == -1) { | 
|  | 1788 | ALOGE("%s: Error while creating the file: %s", __FUNCTION__, sFileName); | 
|  | 1789 | } | 
|  | 1790 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1791 | return ret; | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1792 | } | 
|  | 1793 |  | 
| Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1794 | String16 CameraService::getPackageNameFromUid(int clientUid) { | 
|  | 1795 | String16 packageName(""); | 
|  | 1796 |  | 
|  | 1797 | sp<IServiceManager> sm = defaultServiceManager(); | 
|  | 1798 | sp<IBinder> binder = sm->getService(String16(kPermissionServiceName)); | 
|  | 1799 | if (binder == 0) { | 
|  | 1800 | ALOGE("Cannot get permission service"); | 
|  | 1801 | // Return empty package name and the further interaction | 
|  | 1802 | // with camera will likely fail | 
|  | 1803 | return packageName; | 
|  | 1804 | } | 
|  | 1805 |  | 
|  | 1806 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); | 
|  | 1807 | Vector<String16> packages; | 
|  | 1808 |  | 
|  | 1809 | permCtrl->getPackagesForUid(clientUid, packages); | 
|  | 1810 |  | 
|  | 1811 | if (packages.isEmpty()) { | 
|  | 1812 | ALOGE("No packages for calling UID %d", clientUid); | 
|  | 1813 | // Return empty package name and the further interaction | 
|  | 1814 | // with camera will likely fail | 
|  | 1815 | return packageName; | 
|  | 1816 | } | 
|  | 1817 |  | 
|  | 1818 | // Arbitrarily pick the first name in the list | 
|  | 1819 | packageName = packages[0]; | 
|  | 1820 |  | 
|  | 1821 | return packageName; | 
|  | 1822 | } | 
|  | 1823 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1824 | template<class CALLBACK, class CLIENT> | 
|  | 1825 | Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, | 
| Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1826 | int api1CameraId, const String16& clientPackageNameMaybe, bool systemNativeClient, | 
| Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 1827 | const std::optional<String16>& clientFeatureId, int clientUid, int clientPid, | 
| Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1828 | apiLevel effectiveApiLevel, bool shimUpdateOnly, int oomScoreOffset, int targetSdkVersion, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1829 | bool overrideToPortrait, /*out*/sp<CLIENT>& device) { | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1830 | binder::Status ret = binder::Status::ok(); | 
|  | 1831 |  | 
| Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1832 | bool isNonSystemNdk = false; | 
|  | 1833 | String16 clientPackageName; | 
|  | 1834 | if (clientPackageNameMaybe.size() <= 0) { | 
|  | 1835 | // NDK calls don't come with package names, but we need one for various cases. | 
|  | 1836 | // Generally, there's a 1:1 mapping between UID and package name, but shared UIDs | 
|  | 1837 | // do exist. For all authentication cases, all packages under the same UID get the | 
|  | 1838 | // same permissions, so picking any associated package name is sufficient. For some | 
|  | 1839 | // other cases, this may give inaccurate names for clients in logs. | 
|  | 1840 | isNonSystemNdk = true; | 
|  | 1841 | int packageUid = (clientUid == USE_CALLING_UID) ? | 
|  | 1842 | CameraThreadState::getCallingUid() : clientUid; | 
|  | 1843 | clientPackageName = getPackageNameFromUid(packageUid); | 
|  | 1844 | } else { | 
|  | 1845 | clientPackageName = clientPackageNameMaybe; | 
|  | 1846 | } | 
|  | 1847 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1848 | String8 clientName8(clientPackageName); | 
|  | 1849 |  | 
|  | 1850 | int originalClientPid = 0; | 
|  | 1851 |  | 
| Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1852 | int packagePid = (clientPid == USE_CALLING_PID) ? | 
|  | 1853 | CameraThreadState::getCallingPid() : clientPid; | 
| Eino-Ville Talvala | a976df8 | 2019-06-13 18:01:58 -0700 | [diff] [blame] | 1854 | ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) and " | 
| Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1855 | "Camera API version %d", packagePid, clientName8.string(), cameraId.string(), | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1856 | static_cast<int>(effectiveApiLevel)); | 
|  | 1857 |  | 
| Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1858 | nsecs_t openTimeNs = systemTime(); | 
|  | 1859 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1860 | sp<CLIENT> client = nullptr; | 
| Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1861 | int facing = -1; | 
| Emilian Peev | b91f180 | 2021-03-23 14:50:28 -0700 | [diff] [blame] | 1862 | int orientation = 0; | 
| Eino-Ville Talvala | 58106af | 2022-09-23 16:51:06 -0700 | [diff] [blame] | 1863 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1864 | { | 
|  | 1865 | // Acquire mServiceLock and prevent other clients from connecting | 
|  | 1866 | std::unique_ptr<AutoConditionLock> lock = | 
|  | 1867 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); | 
|  | 1868 |  | 
|  | 1869 | if (lock == nullptr) { | 
|  | 1870 | ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)." | 
|  | 1871 | , clientPid); | 
|  | 1872 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, | 
|  | 1873 | "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting", | 
|  | 1874 | cameraId.string(), clientName8.string(), clientPid); | 
|  | 1875 | } | 
|  | 1876 |  | 
| Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 1877 | // Enforce client permissions and do basic validity checks | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1878 | if(!(ret = validateConnectLocked(cameraId, clientName8, | 
|  | 1879 | /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) { | 
|  | 1880 | return ret; | 
|  | 1881 | } | 
|  | 1882 |  | 
|  | 1883 | // Check the shim parameters after acquiring lock, if they have already been updated and | 
|  | 1884 | // we were doing a shim update, return immediately | 
|  | 1885 | if (shimUpdateOnly) { | 
|  | 1886 | auto cameraState = getCameraState(cameraId); | 
|  | 1887 | if (cameraState != nullptr) { | 
|  | 1888 | if (!cameraState->getShimParams().isEmpty()) return ret; | 
|  | 1889 | } | 
|  | 1890 | } | 
|  | 1891 |  | 
|  | 1892 | status_t err; | 
|  | 1893 |  | 
|  | 1894 | sp<BasicClient> clientTmp = nullptr; | 
|  | 1895 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial; | 
|  | 1896 | if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1897 | IInterface::asBinder(cameraCb), clientName8, oomScoreOffset, systemNativeClient, | 
|  | 1898 | /*out*/&clientTmp, /*out*/&partial)) != NO_ERROR) { | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1899 | switch (err) { | 
|  | 1900 | case -ENODEV: | 
|  | 1901 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, | 
|  | 1902 | "No camera device with ID \"%s\" currently available", | 
|  | 1903 | cameraId.string()); | 
|  | 1904 | case -EBUSY: | 
|  | 1905 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, | 
|  | 1906 | "Higher-priority client using camera, ID \"%s\" currently unavailable", | 
|  | 1907 | cameraId.string()); | 
| Yin-Chia Yeh | 8dfe464 | 2020-06-01 11:57:45 -0700 | [diff] [blame] | 1908 | case -EUSERS: | 
|  | 1909 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, | 
|  | 1910 | "Too many cameras already open, cannot open camera \"%s\"", | 
|  | 1911 | cameraId.string()); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1912 | default: | 
|  | 1913 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 1914 | "Unexpected error %s (%d) opening camera \"%s\"", | 
|  | 1915 | strerror(-err), err, cameraId.string()); | 
|  | 1916 | } | 
|  | 1917 | } | 
|  | 1918 |  | 
|  | 1919 | if (clientTmp.get() != nullptr) { | 
|  | 1920 | // Handle special case for API1 MediaRecorder where the existing client is returned | 
|  | 1921 | device = static_cast<CLIENT*>(clientTmp.get()); | 
|  | 1922 | return ret; | 
|  | 1923 | } | 
|  | 1924 |  | 
|  | 1925 | // give flashlight a chance to close devices if necessary. | 
|  | 1926 | mFlashlight->prepareDeviceOpen(cameraId); | 
|  | 1927 |  | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1928 | int portraitRotation; | 
| Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1929 | auto deviceVersionAndTransport = | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1930 | getDeviceVersion(cameraId, overrideToPortrait, /*out*/&portraitRotation, | 
|  | 1931 | /*out*/&facing, /*out*/&orientation); | 
| Eino-Ville Talvala | 6963d0a | 2017-01-31 13:00:34 -0800 | [diff] [blame] | 1932 | if (facing == -1) { | 
|  | 1933 | ALOGE("%s: Unable to get camera device \"%s\"  facing", __FUNCTION__, cameraId.string()); | 
|  | 1934 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 1935 | "Unable to get camera device \"%s\" facing", cameraId.string()); | 
|  | 1936 | } | 
|  | 1937 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1938 | sp<BasicClient> tmp = nullptr; | 
| Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 1939 | bool overrideForPerfClass = SessionConfigurationUtils::targetPerfClassPrimaryCamera( | 
|  | 1940 | mPerfClassPrimaryCameraIds, cameraId.string(), targetSdkVersion); | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 1941 | if(!(ret = makeClient(this, cameraCb, clientPackageName, systemNativeClient, | 
|  | 1942 | clientFeatureId, cameraId, api1CameraId, facing, orientation, | 
| Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 1943 | clientPid, clientUid, getpid(), | 
| Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 1944 | deviceVersionAndTransport, effectiveApiLevel, overrideForPerfClass, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 1945 | overrideToPortrait, /*out*/&tmp)).isOk()) { | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1946 | return ret; | 
|  | 1947 | } | 
|  | 1948 | client = static_cast<CLIENT*>(tmp.get()); | 
|  | 1949 |  | 
|  | 1950 | LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state", | 
|  | 1951 | __FUNCTION__); | 
|  | 1952 |  | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 1953 | String8 monitorTags = isClientWatched(client.get()) ? mMonitorTags : String8(""); | 
|  | 1954 | err = client->initialize(mCameraProviderManager, monitorTags); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1955 | if (err != OK) { | 
|  | 1956 | ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 1957 | // Errors could be from the HAL module open call or from AppOpsManager | 
|  | 1958 | switch(err) { | 
|  | 1959 | case BAD_VALUE: | 
|  | 1960 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 1961 | "Illegal argument to HAL module for camera \"%s\"", cameraId.string()); | 
|  | 1962 | case -EBUSY: | 
|  | 1963 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, | 
|  | 1964 | "Camera \"%s\" is already open", cameraId.string()); | 
|  | 1965 | case -EUSERS: | 
|  | 1966 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, | 
|  | 1967 | "Too many cameras already open, cannot open camera \"%s\"", | 
|  | 1968 | cameraId.string()); | 
|  | 1969 | case PERMISSION_DENIED: | 
|  | 1970 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 1971 | "No permission to open camera \"%s\"", cameraId.string()); | 
|  | 1972 | case -EACCES: | 
|  | 1973 | return STATUS_ERROR_FMT(ERROR_DISABLED, | 
|  | 1974 | "Camera \"%s\" disabled by policy", cameraId.string()); | 
|  | 1975 | case -ENODEV: | 
|  | 1976 | default: | 
|  | 1977 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 1978 | "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(), | 
|  | 1979 | strerror(-err), err); | 
|  | 1980 | } | 
|  | 1981 | } | 
|  | 1982 |  | 
|  | 1983 | // Update shim paremeters for legacy clients | 
|  | 1984 | if (effectiveApiLevel == API_1) { | 
|  | 1985 | // Assume we have always received a Client subclass for API1 | 
|  | 1986 | sp<Client> shimClient = reinterpret_cast<Client*>(client.get()); | 
|  | 1987 | String8 rawParams = shimClient->getParameters(); | 
|  | 1988 | CameraParameters params(rawParams); | 
|  | 1989 |  | 
|  | 1990 | auto cameraState = getCameraState(cameraId); | 
|  | 1991 | if (cameraState != nullptr) { | 
|  | 1992 | cameraState->setShimParams(params); | 
|  | 1993 | } else { | 
|  | 1994 | ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.", | 
|  | 1995 | __FUNCTION__, cameraId.string()); | 
|  | 1996 | } | 
|  | 1997 | } | 
|  | 1998 |  | 
| Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 1999 | // Enable/disable camera service watchdog | 
|  | 2000 | client->setCameraServiceWatchdog(mCameraServiceWatchdogEnabled); | 
|  | 2001 |  | 
| Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 2002 | // Set rotate-and-crop override behavior | 
|  | 2003 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) { | 
|  | 2004 | client->setRotateAndCropOverride(mOverrideRotateAndCropMode); | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2005 | } else if (overrideToPortrait && portraitRotation != 0) { | 
|  | 2006 | uint8_t rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_AUTO; | 
|  | 2007 | switch (portraitRotation) { | 
|  | 2008 | case 90: | 
|  | 2009 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_90; | 
|  | 2010 | break; | 
|  | 2011 | case 180: | 
|  | 2012 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_180; | 
|  | 2013 | break; | 
|  | 2014 | case 270: | 
|  | 2015 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_270; | 
|  | 2016 | break; | 
|  | 2017 | default: | 
|  | 2018 | ALOGE("Unexpected portrait rotation: %d", portraitRotation); | 
|  | 2019 | break; | 
|  | 2020 | } | 
|  | 2021 | client->setRotateAndCropOverride(rotateAndCropMode); | 
| Emilian Peev | 13f35ad | 2022-04-27 11:28:48 -0700 | [diff] [blame] | 2022 | } else { | 
| Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 2023 | client->setRotateAndCropOverride( | 
|  | 2024 | mCameraServiceProxyWrapper->getRotateAndCropOverride( | 
|  | 2025 | clientPackageName, facing, multiuser_get_user_id(clientUid))); | 
|  | 2026 | } | 
|  | 2027 |  | 
|  | 2028 | // Set autoframing override behaviour | 
|  | 2029 | if (mOverrideAutoframingMode != ANDROID_CONTROL_AUTOFRAMING_AUTO) { | 
|  | 2030 | client->setAutoframingOverride(mOverrideAutoframingMode); | 
|  | 2031 | } else { | 
|  | 2032 | client->setAutoframingOverride( | 
|  | 2033 | mCameraServiceProxyWrapper->getAutoframingOverride( | 
|  | 2034 | clientPackageName)); | 
| Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 2035 | } | 
|  | 2036 |  | 
| Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2037 | // Set camera muting behavior | 
| Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 2038 | bool isCameraPrivacyEnabled = | 
| Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 2039 | mSensorPrivacyPolicy->isCameraPrivacyEnabled(); | 
| Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2040 | if (client->supportsCameraMute()) { | 
| Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 2041 | client->setCameraMute( | 
|  | 2042 | mOverrideCameraMuteMode || isCameraPrivacyEnabled); | 
|  | 2043 | } else if (isCameraPrivacyEnabled) { | 
|  | 2044 | // no camera mute supported, but privacy is on! => disconnect | 
|  | 2045 | ALOGI("Camera mute not supported for package: %s, camera id: %s", | 
|  | 2046 | String8(client->getPackageName()).string(), cameraId.string()); | 
|  | 2047 | // Do not hold mServiceLock while disconnecting clients, but | 
|  | 2048 | // retain the condition blocking other clients from connecting | 
|  | 2049 | // in mServiceLockWrapper if held. | 
|  | 2050 | mServiceLock.unlock(); | 
|  | 2051 | // Clear caller identity temporarily so client disconnect PID | 
|  | 2052 | // checks work correctly | 
|  | 2053 | int64_t token = CameraThreadState::clearCallingIdentity(); | 
|  | 2054 | // Note AppOp to trigger the "Unblock" dialog | 
|  | 2055 | client->noteAppOp(); | 
|  | 2056 | client->disconnect(); | 
|  | 2057 | CameraThreadState::restoreCallingIdentity(token); | 
|  | 2058 | // Reacquire mServiceLock | 
|  | 2059 | mServiceLock.lock(); | 
|  | 2060 |  | 
|  | 2061 | return STATUS_ERROR_FMT(ERROR_DISABLED, | 
|  | 2062 | "Camera \"%s\" disabled due to camera mute", cameraId.string()); | 
| Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 2063 | } | 
|  | 2064 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2065 | if (shimUpdateOnly) { | 
|  | 2066 | // If only updating legacy shim parameters, immediately disconnect client | 
|  | 2067 | mServiceLock.unlock(); | 
|  | 2068 | client->disconnect(); | 
|  | 2069 | mServiceLock.lock(); | 
|  | 2070 | } else { | 
|  | 2071 | // Otherwise, add client to active clients list | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2072 | finishConnectLocked(client, partial, oomScoreOffset, systemNativeClient); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2073 | } | 
| Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 2074 |  | 
|  | 2075 | client->setImageDumpMask(mImageDumpMask); | 
| Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 2076 | client->setStreamUseCaseOverrides(mStreamUseCaseOverrides); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2077 | } // lock is destroyed, allow further connect calls | 
|  | 2078 |  | 
|  | 2079 | // Important: release the mutex here so the client can call back into the service from its | 
|  | 2080 | // destructor (can be at the end of the call) | 
|  | 2081 | device = client; | 
| Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2082 |  | 
|  | 2083 | int32_t openLatencyMs = ns2ms(systemTime() - openTimeNs); | 
| Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 2084 | mCameraServiceProxyWrapper->logOpen(cameraId, facing, clientPackageName, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2085 | effectiveApiLevel, isNonSystemNdk, openLatencyMs); | 
| Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 2086 |  | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2087 | { | 
|  | 2088 | Mutex::Autolock lock(mInjectionParametersLock); | 
|  | 2089 | if (cameraId == mInjectionInternalCamId && mInjectionInitPending) { | 
|  | 2090 | mInjectionInitPending = false; | 
|  | 2091 | status_t res = NO_ERROR; | 
|  | 2092 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); | 
|  | 2093 | if (clientDescriptor != nullptr) { | 
| Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2094 | sp<BasicClient> clientSp = clientDescriptor->getValue(); | 
|  | 2095 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); | 
|  | 2096 | if(res != OK) { | 
|  | 2097 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, | 
|  | 2098 | "No camera device with ID \"%s\" currently available", | 
|  | 2099 | mInjectionExternalCamId.string()); | 
|  | 2100 | } | 
|  | 2101 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2102 | if (res != OK) { | 
|  | 2103 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); | 
|  | 2104 | } | 
|  | 2105 | } else { | 
|  | 2106 | ALOGE("%s: Internal camera ID = %s 's client does not exist!", | 
|  | 2107 | __FUNCTION__, mInjectionInternalCamId.string()); | 
|  | 2108 | res = NO_INIT; | 
|  | 2109 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); | 
|  | 2110 | } | 
|  | 2111 | } | 
|  | 2112 | } | 
|  | 2113 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2114 | return ret; | 
|  | 2115 | } | 
|  | 2116 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2117 | status_t CameraService::addOfflineClient(String8 cameraId, sp<BasicClient> offlineClient) { | 
|  | 2118 | if (offlineClient.get() == nullptr) { | 
|  | 2119 | return BAD_VALUE; | 
|  | 2120 | } | 
|  | 2121 |  | 
|  | 2122 | { | 
|  | 2123 | // Acquire mServiceLock and prevent other clients from connecting | 
|  | 2124 | std::unique_ptr<AutoConditionLock> lock = | 
|  | 2125 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS); | 
|  | 2126 |  | 
|  | 2127 | if (lock == nullptr) { | 
|  | 2128 | ALOGE("%s: (PID %d) rejected (too many other clients connecting)." | 
|  | 2129 | , __FUNCTION__, offlineClient->getClientPid()); | 
|  | 2130 | return TIMED_OUT; | 
|  | 2131 | } | 
|  | 2132 |  | 
|  | 2133 | auto onlineClientDesc = mActiveClientManager.get(cameraId); | 
|  | 2134 | if (onlineClientDesc.get() == nullptr) { | 
|  | 2135 | ALOGE("%s: No active online client using camera id: %s", __FUNCTION__, | 
|  | 2136 | cameraId.c_str()); | 
|  | 2137 | return BAD_VALUE; | 
|  | 2138 | } | 
|  | 2139 |  | 
|  | 2140 | // Offline clients do not evict or conflict with other online devices. Resource sharing | 
|  | 2141 | // conflicts are handled by the camera provider which will either succeed or fail before | 
|  | 2142 | // reaching this method. | 
|  | 2143 | const auto& onlinePriority = onlineClientDesc->getPriority(); | 
|  | 2144 | auto offlineClientDesc = CameraClientManager::makeClientDescriptor( | 
|  | 2145 | kOfflineDevice + onlineClientDesc->getKey(), offlineClient, /*cost*/ 0, | 
|  | 2146 | /*conflictingKeys*/ std::set<String8>(), onlinePriority.getScore(), | 
| Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 2147 | onlineClientDesc->getOwnerId(), onlinePriority.getState(), | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 2148 | // native clients don't have offline processing support. | 
|  | 2149 | /*ommScoreOffset*/ 0, /*systemNativeClient*/false); | 
| Guillaume Bailey | 417e43d | 2022-11-02 15:30:24 +0100 | [diff] [blame] | 2150 | if (offlineClientDesc == nullptr) { | 
|  | 2151 | ALOGE("%s: Offline client descriptor was NULL", __FUNCTION__); | 
|  | 2152 | return BAD_VALUE; | 
|  | 2153 | } | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2154 |  | 
|  | 2155 | // Allow only one offline device per camera | 
|  | 2156 | auto incompatibleClients = mActiveClientManager.getIncompatibleClients(offlineClientDesc); | 
|  | 2157 | if (!incompatibleClients.empty()) { | 
|  | 2158 | ALOGE("%s: Incompatible offline clients present!", __FUNCTION__); | 
|  | 2159 | return BAD_VALUE; | 
|  | 2160 | } | 
|  | 2161 |  | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2162 | String8 monitorTags = isClientWatched(offlineClient.get()) ? mMonitorTags : String8(""); | 
|  | 2163 | auto err = offlineClient->initialize(mCameraProviderManager, monitorTags); | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 2164 | if (err != OK) { | 
|  | 2165 | ALOGE("%s: Could not initialize offline client.", __FUNCTION__); | 
|  | 2166 | return err; | 
|  | 2167 | } | 
|  | 2168 |  | 
|  | 2169 | auto evicted = mActiveClientManager.addAndEvict(offlineClientDesc); | 
|  | 2170 | if (evicted.size() > 0) { | 
|  | 2171 | for (auto& i : evicted) { | 
|  | 2172 | ALOGE("%s: Invalid state: Offline client for camera %s was not removed ", | 
|  | 2173 | __FUNCTION__, i->getKey().string()); | 
|  | 2174 | } | 
|  | 2175 |  | 
|  | 2176 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, offline clients not evicted " | 
|  | 2177 | "properly", __FUNCTION__); | 
|  | 2178 |  | 
|  | 2179 | return BAD_VALUE; | 
|  | 2180 | } | 
|  | 2181 |  | 
|  | 2182 | logConnectedOffline(offlineClientDesc->getKey(), | 
|  | 2183 | static_cast<int>(offlineClientDesc->getOwnerId()), | 
|  | 2184 | String8(offlineClient->getPackageName())); | 
|  | 2185 |  | 
|  | 2186 | sp<IBinder> remoteCallback = offlineClient->getRemote(); | 
|  | 2187 | if (remoteCallback != nullptr) { | 
|  | 2188 | remoteCallback->linkToDeath(this); | 
|  | 2189 | } | 
|  | 2190 | } // lock is destroyed, allow further connect calls | 
|  | 2191 |  | 
|  | 2192 | return OK; | 
|  | 2193 | } | 
|  | 2194 |  | 
| Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2195 | Status CameraService::turnOnTorchWithStrengthLevel(const String16& cameraId, int32_t torchStrength, | 
|  | 2196 | const sp<IBinder>& clientBinder) { | 
|  | 2197 | Mutex::Autolock lock(mServiceLock); | 
|  | 2198 |  | 
|  | 2199 | ATRACE_CALL(); | 
|  | 2200 | if (clientBinder == nullptr) { | 
|  | 2201 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); | 
|  | 2202 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, | 
|  | 2203 | "Torch client binder in null."); | 
|  | 2204 | } | 
|  | 2205 |  | 
|  | 2206 | String8 id = String8(cameraId.string()); | 
|  | 2207 | int uid = CameraThreadState::getCallingUid(); | 
|  | 2208 |  | 
|  | 2209 | if (shouldRejectSystemCameraConnection(id)) { | 
|  | 2210 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to change the strength level" | 
|  | 2211 | "for system only device %s: ", id.string()); | 
|  | 2212 | } | 
|  | 2213 |  | 
|  | 2214 | // verify id is valid | 
|  | 2215 | auto state = getCameraState(id); | 
|  | 2216 | if (state == nullptr) { | 
|  | 2217 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); | 
|  | 2218 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 2219 | "Camera ID \"%s\" is a not valid camera ID", id.string()); | 
|  | 2220 | } | 
|  | 2221 |  | 
|  | 2222 | StatusInternal cameraStatus = state->getStatus(); | 
|  | 2223 | if (cameraStatus != StatusInternal::NOT_AVAILABLE && | 
|  | 2224 | cameraStatus != StatusInternal::PRESENT) { | 
|  | 2225 | ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), | 
|  | 2226 | (int)cameraStatus); | 
|  | 2227 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 2228 | "Camera ID \"%s\" is a not valid camera ID", id.string()); | 
|  | 2229 | } | 
|  | 2230 |  | 
|  | 2231 | { | 
|  | 2232 | Mutex::Autolock al(mTorchStatusMutex); | 
|  | 2233 | TorchModeStatus status; | 
|  | 2234 | status_t err = getTorchStatusLocked(id, &status); | 
|  | 2235 | if (err != OK) { | 
|  | 2236 | if (err == NAME_NOT_FOUND) { | 
|  | 2237 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 2238 | "Camera \"%s\" does not have a flash unit", id.string()); | 
|  | 2239 | } | 
|  | 2240 | ALOGE("%s: getting current torch status failed for camera %s", | 
|  | 2241 | __FUNCTION__, id.string()); | 
|  | 2242 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 2243 | "Error changing torch strength level for camera \"%s\": %s (%d)", | 
|  | 2244 | id.string(), strerror(-err), err); | 
|  | 2245 | } | 
|  | 2246 |  | 
|  | 2247 | if (status == TorchModeStatus::NOT_AVAILABLE) { | 
|  | 2248 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { | 
|  | 2249 | ALOGE("%s: torch mode of camera %s is not available because " | 
|  | 2250 | "camera is in use.", __FUNCTION__, id.string()); | 
|  | 2251 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, | 
|  | 2252 | "Torch for camera \"%s\" is not available due to an existing camera user", | 
|  | 2253 | id.string()); | 
|  | 2254 | } else { | 
|  | 2255 | ALOGE("%s: torch mode of camera %s is not available due to " | 
|  | 2256 | "insufficient resources", __FUNCTION__, id.string()); | 
|  | 2257 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, | 
|  | 2258 | "Torch for camera \"%s\" is not available due to insufficient resources", | 
|  | 2259 | id.string()); | 
|  | 2260 | } | 
|  | 2261 | } | 
|  | 2262 | } | 
|  | 2263 |  | 
|  | 2264 | { | 
|  | 2265 | Mutex::Autolock al(mTorchUidMapMutex); | 
|  | 2266 | updateTorchUidMapLocked(cameraId, uid); | 
|  | 2267 | } | 
|  | 2268 | // Check if the current torch strength level is same as the new one. | 
|  | 2269 | bool shouldSkipTorchStrengthUpdates = mCameraProviderManager->shouldSkipTorchStrengthUpdate( | 
|  | 2270 | id.string(), torchStrength); | 
|  | 2271 |  | 
|  | 2272 | status_t err = mFlashlight->turnOnTorchWithStrengthLevel(id, torchStrength); | 
|  | 2273 |  | 
|  | 2274 | if (err != OK) { | 
|  | 2275 | int32_t errorCode; | 
|  | 2276 | String8 msg; | 
|  | 2277 | switch (err) { | 
|  | 2278 | case -ENOSYS: | 
|  | 2279 | msg = String8::format("Camera \"%s\" has no flashlight.", | 
|  | 2280 | id.string()); | 
|  | 2281 | errorCode = ERROR_ILLEGAL_ARGUMENT; | 
|  | 2282 | break; | 
|  | 2283 | case -EBUSY: | 
|  | 2284 | msg = String8::format("Camera \"%s\" is in use", | 
|  | 2285 | id.string()); | 
|  | 2286 | errorCode = ERROR_CAMERA_IN_USE; | 
|  | 2287 | break; | 
| Rucha Katakwar | 922fa9c | 2022-02-25 17:46:49 -0800 | [diff] [blame] | 2288 | case -EINVAL: | 
|  | 2289 | msg = String8::format("Torch strength level %d is not within the " | 
|  | 2290 | "valid range.", torchStrength); | 
|  | 2291 | errorCode = ERROR_ILLEGAL_ARGUMENT; | 
|  | 2292 | break; | 
| Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2293 | default: | 
|  | 2294 | msg = String8::format("Changing torch strength level failed."); | 
|  | 2295 | errorCode = ERROR_INVALID_OPERATION; | 
| Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2296 | } | 
|  | 2297 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 2298 | return STATUS_ERROR(errorCode, msg.string()); | 
|  | 2299 | } | 
|  | 2300 |  | 
|  | 2301 | { | 
|  | 2302 | // update the link to client's death | 
|  | 2303 | // Store the last client that turns on each camera's torch mode. | 
|  | 2304 | Mutex::Autolock al(mTorchClientMapMutex); | 
|  | 2305 | ssize_t index = mTorchClientMap.indexOfKey(id); | 
|  | 2306 | if (index == NAME_NOT_FOUND) { | 
|  | 2307 | mTorchClientMap.add(id, clientBinder); | 
|  | 2308 | } else { | 
|  | 2309 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); | 
|  | 2310 | mTorchClientMap.replaceValueAt(index, clientBinder); | 
|  | 2311 | } | 
|  | 2312 | clientBinder->linkToDeath(this); | 
|  | 2313 | } | 
|  | 2314 |  | 
|  | 2315 | int clientPid = CameraThreadState::getCallingPid(); | 
|  | 2316 | const char *id_cstr = id.c_str(); | 
|  | 2317 | ALOGI("%s: Torch strength for camera id %s changed to %d for client PID %d", | 
|  | 2318 | __FUNCTION__, id_cstr, torchStrength, clientPid); | 
|  | 2319 | if (!shouldSkipTorchStrengthUpdates) { | 
|  | 2320 | broadcastTorchStrengthLevel(id, torchStrength); | 
|  | 2321 | } | 
|  | 2322 | return Status::ok(); | 
|  | 2323 | } | 
|  | 2324 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2325 | Status CameraService::setTorchMode(const String16& cameraId, bool enabled, | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2326 | const sp<IBinder>& clientBinder) { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2327 | Mutex::Autolock lock(mServiceLock); | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2328 |  | 
|  | 2329 | ATRACE_CALL(); | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2330 | if (enabled && clientBinder == nullptr) { | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2331 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2332 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, | 
|  | 2333 | "Torch client Binder is null"); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2334 | } | 
|  | 2335 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2336 | String8 id = String8(cameraId.string()); | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 2337 | int uid = CameraThreadState::getCallingUid(); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2338 |  | 
| Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2339 | if (shouldRejectSystemCameraConnection(id)) { | 
|  | 2340 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Unable to set torch mode" | 
|  | 2341 | " for system only device %s: ", id.string()); | 
|  | 2342 | } | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2343 | // verify id is valid. | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2344 | auto state = getCameraState(id); | 
|  | 2345 | if (state == nullptr) { | 
| Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 2346 | ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2347 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 2348 | "Camera ID \"%s\" is a not valid camera ID", id.string()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2349 | } | 
|  | 2350 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2351 | StatusInternal cameraStatus = state->getStatus(); | 
|  | 2352 | if (cameraStatus != StatusInternal::PRESENT && | 
| Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2353 | cameraStatus != StatusInternal::NOT_AVAILABLE) { | 
|  | 2354 | 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] | 2355 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 2356 | "Camera ID \"%s\" is a not valid camera ID", id.string()); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2357 | } | 
|  | 2358 |  | 
|  | 2359 | { | 
|  | 2360 | Mutex::Autolock al(mTorchStatusMutex); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2361 | TorchModeStatus status; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2362 | status_t err = getTorchStatusLocked(id, &status); | 
|  | 2363 | if (err != OK) { | 
|  | 2364 | if (err == NAME_NOT_FOUND) { | 
|  | 2365 | return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, | 
|  | 2366 | "Camera \"%s\" does not have a flash unit", id.string()); | 
|  | 2367 | } | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2368 | ALOGE("%s: getting current torch status failed for camera %s", | 
|  | 2369 | __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2370 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, | 
|  | 2371 | "Error updating torch status for camera \"%s\": %s (%d)", id.string(), | 
|  | 2372 | strerror(-err), err); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2373 | } | 
|  | 2374 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2375 | if (status == TorchModeStatus::NOT_AVAILABLE) { | 
| Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 2376 | if (cameraStatus == StatusInternal::NOT_AVAILABLE) { | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2377 | ALOGE("%s: torch mode of camera %s is not available because " | 
|  | 2378 | "camera is in use", __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2379 | return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE, | 
|  | 2380 | "Torch for camera \"%s\" is not available due to an existing camera user", | 
|  | 2381 | id.string()); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2382 | } else { | 
|  | 2383 | ALOGE("%s: torch mode of camera %s is not available due to " | 
|  | 2384 | "insufficient resources", __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2385 | return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE, | 
|  | 2386 | "Torch for camera \"%s\" is not available due to insufficient resources", | 
|  | 2387 | id.string()); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2388 | } | 
|  | 2389 | } | 
|  | 2390 | } | 
|  | 2391 |  | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2392 | { | 
|  | 2393 | // Update UID map - this is used in the torch status changed callbacks, so must be done | 
|  | 2394 | // before setTorchMode | 
| Chien-Yu Chen | fe751be | 2015-09-01 14:16:44 -0700 | [diff] [blame] | 2395 | Mutex::Autolock al(mTorchUidMapMutex); | 
| Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2396 | updateTorchUidMapLocked(cameraId, uid); | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2397 | } | 
|  | 2398 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2399 | status_t err = mFlashlight->setTorchMode(id, enabled); | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2400 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2401 | if (err != OK) { | 
|  | 2402 | int32_t errorCode; | 
|  | 2403 | String8 msg; | 
|  | 2404 | switch (err) { | 
|  | 2405 | case -ENOSYS: | 
|  | 2406 | msg = String8::format("Camera \"%s\" has no flashlight", | 
|  | 2407 | id.string()); | 
|  | 2408 | errorCode = ERROR_ILLEGAL_ARGUMENT; | 
|  | 2409 | break; | 
| Dijack Dong | c8a6f25 | 2021-09-17 16:21:16 +0800 | [diff] [blame] | 2410 | case -EBUSY: | 
|  | 2411 | msg = String8::format("Camera \"%s\" is in use", | 
|  | 2412 | id.string()); | 
|  | 2413 | errorCode = ERROR_CAMERA_IN_USE; | 
|  | 2414 | break; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2415 | default: | 
|  | 2416 | msg = String8::format( | 
|  | 2417 | "Setting torch mode of camera \"%s\" to %d failed: %s (%d)", | 
|  | 2418 | id.string(), enabled, strerror(-err), err); | 
|  | 2419 | errorCode = ERROR_INVALID_OPERATION; | 
|  | 2420 | } | 
|  | 2421 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
| Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2422 | logServiceError(msg,errorCode); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2423 | return STATUS_ERROR(errorCode, msg.string()); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2424 | } | 
|  | 2425 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2426 | { | 
|  | 2427 | // update the link to client's death | 
|  | 2428 | Mutex::Autolock al(mTorchClientMapMutex); | 
|  | 2429 | ssize_t index = mTorchClientMap.indexOfKey(id); | 
|  | 2430 | if (enabled) { | 
|  | 2431 | if (index == NAME_NOT_FOUND) { | 
|  | 2432 | mTorchClientMap.add(id, clientBinder); | 
|  | 2433 | } else { | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2434 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2435 | mTorchClientMap.replaceValueAt(index, clientBinder); | 
|  | 2436 | } | 
|  | 2437 | clientBinder->linkToDeath(this); | 
|  | 2438 | } else if (index != NAME_NOT_FOUND) { | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 2439 | mTorchClientMap.valueAt(index)->unlinkToDeath(this); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2440 | } | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2441 | } | 
|  | 2442 |  | 
| Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 2443 | int clientPid = CameraThreadState::getCallingPid(); | 
|  | 2444 | const char *id_cstr = id.c_str(); | 
|  | 2445 | const char *torchState = enabled ? "on" : "off"; | 
|  | 2446 | ALOGI("Torch for camera id %s turned %s for client PID %d", id_cstr, torchState, clientPid); | 
|  | 2447 | logTorchEvent(id_cstr, torchState , clientPid); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2448 | return Status::ok(); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2449 | } | 
|  | 2450 |  | 
| Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 2451 | void CameraService::updateTorchUidMapLocked(const String16& cameraId, int uid) { | 
|  | 2452 | String8 id = String8(cameraId.string()); | 
|  | 2453 | if (mTorchUidMap.find(id) == mTorchUidMap.end()) { | 
|  | 2454 | mTorchUidMap[id].first = uid; | 
|  | 2455 | mTorchUidMap[id].second = uid; | 
|  | 2456 | } else { | 
|  | 2457 | // Set the pending UID | 
|  | 2458 | mTorchUidMap[id].first = uid; | 
|  | 2459 | } | 
|  | 2460 | } | 
|  | 2461 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2462 | Status CameraService::notifySystemEvent(int32_t eventId, | 
|  | 2463 | const std::vector<int32_t>& args) { | 
| Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2464 | const int pid = CameraThreadState::getCallingPid(); | 
|  | 2465 | const int selfPid = getpid(); | 
|  | 2466 |  | 
|  | 2467 | // Permission checks | 
|  | 2468 | if (pid != selfPid) { | 
|  | 2469 | // Ensure we're being called by system_server, or similar process with | 
|  | 2470 | // permissions to notify the camera service about system events | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2471 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { | 
| Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2472 | const int uid = CameraThreadState::getCallingUid(); | 
|  | 2473 | ALOGE("Permission Denial: cannot send updates to camera service about system" | 
|  | 2474 | " events from pid=%d, uid=%d", pid, uid); | 
|  | 2475 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
| Jeongik Cha | aa1b815 | 2018-11-21 10:02:25 +0900 | [diff] [blame] | 2476 | "No permission to send updates to camera service about system events" | 
|  | 2477 | " from pid=%d, uid=%d", pid, uid); | 
| Jeongik Cha | aa5e64c | 2018-11-17 05:08:04 +0900 | [diff] [blame] | 2478 | } | 
|  | 2479 | } | 
|  | 2480 |  | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2481 | ATRACE_CALL(); | 
|  | 2482 |  | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2483 | switch(eventId) { | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2484 | case ICameraService::EVENT_USER_SWITCHED: { | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2485 | // 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] | 2486 | // from a system server crash | 
|  | 2487 | mUidPolicy->registerSelf(); | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 2488 | mSensorPrivacyPolicy->registerSelf(); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2489 | doUserSwitch(/*newUserIds*/ args); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2490 | break; | 
|  | 2491 | } | 
| Valentin Iftime | 29e2e15 | 2021-08-13 15:17:33 +0200 | [diff] [blame] | 2492 | case ICameraService::EVENT_USB_DEVICE_ATTACHED: | 
|  | 2493 | case ICameraService::EVENT_USB_DEVICE_DETACHED: { | 
|  | 2494 | // Notify CameraProviderManager for lazy HALs | 
|  | 2495 | mCameraProviderManager->notifyUsbDeviceEvent(eventId, | 
|  | 2496 | std::to_string(args[0])); | 
|  | 2497 | break; | 
|  | 2498 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2499 | case ICameraService::EVENT_NONE: | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2500 | default: { | 
|  | 2501 | ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__, | 
|  | 2502 | eventId); | 
|  | 2503 | break; | 
|  | 2504 | } | 
|  | 2505 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2506 | return Status::ok(); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 2507 | } | 
|  | 2508 |  | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2509 | void CameraService::notifyMonitoredUids() { | 
|  | 2510 | Mutex::Autolock lock(mStatusListenerLock); | 
|  | 2511 |  | 
|  | 2512 | for (const auto& it : mListenerList) { | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2513 | auto ret = it->getListener()->onCameraAccessPrioritiesChanged(); | 
| Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 2514 | it->handleBinderStatus(ret, "%s: Failed to trigger permission callback for %d:%d: %d", | 
|  | 2515 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2516 | } | 
|  | 2517 | } | 
|  | 2518 |  | 
| Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2519 | Status CameraService::notifyDeviceStateChange(int64_t newState) { | 
|  | 2520 | const int pid = CameraThreadState::getCallingPid(); | 
|  | 2521 | const int selfPid = getpid(); | 
|  | 2522 |  | 
|  | 2523 | // Permission checks | 
|  | 2524 | if (pid != selfPid) { | 
|  | 2525 | // Ensure we're being called by system_server, or similar process with | 
|  | 2526 | // permissions to notify the camera service about system events | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2527 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { | 
| Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2528 | const int uid = CameraThreadState::getCallingUid(); | 
|  | 2529 | ALOGE("Permission Denial: cannot send updates to camera service about device" | 
|  | 2530 | " state changes from pid=%d, uid=%d", pid, uid); | 
|  | 2531 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 2532 | "No permission to send updates to camera service about device state" | 
|  | 2533 | " changes from pid=%d, uid=%d", pid, uid); | 
|  | 2534 | } | 
|  | 2535 | } | 
|  | 2536 |  | 
|  | 2537 | ATRACE_CALL(); | 
|  | 2538 |  | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2539 | { | 
|  | 2540 | Mutex::Autolock lock(mServiceLock); | 
|  | 2541 | mDeviceState = newState; | 
|  | 2542 | } | 
|  | 2543 |  | 
| Jayant Chowdhary | 0bd3852 | 2021-11-05 17:49:27 -0700 | [diff] [blame] | 2544 | mCameraProviderManager->notifyDeviceStateChange(newState); | 
| Eino-Ville Talvala | 63f3611 | 2018-12-06 14:57:03 -0800 | [diff] [blame] | 2545 |  | 
|  | 2546 | return Status::ok(); | 
|  | 2547 | } | 
|  | 2548 |  | 
| Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2549 | Status CameraService::notifyDisplayConfigurationChange() { | 
|  | 2550 | ATRACE_CALL(); | 
|  | 2551 | const int callingPid = CameraThreadState::getCallingPid(); | 
|  | 2552 | const int selfPid = getpid(); | 
|  | 2553 |  | 
|  | 2554 | // Permission checks | 
|  | 2555 | if (callingPid != selfPid) { | 
|  | 2556 | // Ensure we're being called by system_server, or similar process with | 
|  | 2557 | // permissions to notify the camera service about system events | 
|  | 2558 | if (!checkCallingPermission(sCameraSendSystemEventsPermission)) { | 
|  | 2559 | const int uid = CameraThreadState::getCallingUid(); | 
|  | 2560 | ALOGE("Permission Denial: cannot send updates to camera service about orientation" | 
|  | 2561 | " changes from pid=%d, uid=%d", callingPid, uid); | 
|  | 2562 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, | 
|  | 2563 | "No permission to send updates to camera service about orientation" | 
|  | 2564 | " changes from pid=%d, uid=%d", callingPid, uid); | 
|  | 2565 | } | 
|  | 2566 | } | 
|  | 2567 |  | 
|  | 2568 | Mutex::Autolock lock(mServiceLock); | 
|  | 2569 |  | 
|  | 2570 | // Don't do anything if rotate-and-crop override via cmd is active | 
|  | 2571 | if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return Status::ok(); | 
|  | 2572 |  | 
|  | 2573 | const auto clients = mActiveClientManager.getAll(); | 
|  | 2574 | for (auto& current : clients) { | 
|  | 2575 | if (current != nullptr) { | 
|  | 2576 | const auto basicClient = current->getValue(); | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2577 | if (basicClient.get() != nullptr && !basicClient->getOverrideToPortrait()) { | 
|  | 2578 | basicClient->setRotateAndCropOverride( | 
|  | 2579 | mCameraServiceProxyWrapper->getRotateAndCropOverride( | 
|  | 2580 | basicClient->getPackageName(), | 
|  | 2581 | basicClient->getCameraFacing(), | 
|  | 2582 | multiuser_get_user_id(basicClient->getClientUid()))); | 
| Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 2583 | } | 
|  | 2584 | } | 
|  | 2585 | } | 
|  | 2586 |  | 
|  | 2587 | return Status::ok(); | 
|  | 2588 | } | 
|  | 2589 |  | 
|  | 2590 | Status CameraService::getConcurrentCameraIds( | 
| Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2591 | std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) { | 
|  | 2592 | ATRACE_CALL(); | 
|  | 2593 | if (!concurrentCameraIds) { | 
|  | 2594 | ALOGE("%s: concurrentCameraIds is NULL", __FUNCTION__); | 
|  | 2595 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "concurrentCameraIds is NULL"); | 
|  | 2596 | } | 
|  | 2597 |  | 
|  | 2598 | if (!mInitialized) { | 
|  | 2599 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); | 
| Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2600 | logServiceError(String8::format("Camera subsystem is not available"),ERROR_DISCONNECTED); | 
| Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2601 | return STATUS_ERROR(ERROR_DISCONNECTED, | 
|  | 2602 | "Camera subsystem is not available"); | 
|  | 2603 | } | 
|  | 2604 | // First call into the provider and get the set of concurrent camera | 
|  | 2605 | // combinations | 
|  | 2606 | std::vector<std::unordered_set<std::string>> concurrentCameraCombinations = | 
| Jayant Chowdhary | cad23c2 | 2020-03-10 15:04:59 -0700 | [diff] [blame] | 2607 | mCameraProviderManager->getConcurrentCameraIds(); | 
| Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2608 | for (auto &combination : concurrentCameraCombinations) { | 
|  | 2609 | std::vector<std::string> validCombination; | 
|  | 2610 | for (auto &cameraId : combination) { | 
|  | 2611 | // if the camera state is not present, skip | 
|  | 2612 | String8 cameraIdStr(cameraId.c_str()); | 
|  | 2613 | auto state = getCameraState(cameraIdStr); | 
|  | 2614 | if (state == nullptr) { | 
|  | 2615 | ALOGW("%s: camera id %s does not exist", __FUNCTION__, cameraId.c_str()); | 
|  | 2616 | continue; | 
|  | 2617 | } | 
|  | 2618 | StatusInternal status = state->getStatus(); | 
|  | 2619 | if (status == StatusInternal::NOT_PRESENT || status == StatusInternal::ENUMERATING) { | 
|  | 2620 | continue; | 
|  | 2621 | } | 
|  | 2622 | if (shouldRejectSystemCameraConnection(cameraIdStr)) { | 
|  | 2623 | continue; | 
|  | 2624 | } | 
|  | 2625 | validCombination.push_back(cameraId); | 
|  | 2626 | } | 
|  | 2627 | if (validCombination.size() != 0) { | 
|  | 2628 | concurrentCameraIds->push_back(std::move(validCombination)); | 
|  | 2629 | } | 
|  | 2630 | } | 
|  | 2631 | return Status::ok(); | 
|  | 2632 | } | 
|  | 2633 |  | 
|  | 2634 | Status CameraService::isConcurrentSessionConfigurationSupported( | 
|  | 2635 | const std::vector<CameraIdAndSessionConfiguration>& cameraIdsAndSessionConfigurations, | 
| Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2636 | int targetSdkVersion, /*out*/bool* isSupported) { | 
| Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2637 | if (!isSupported) { | 
|  | 2638 | ALOGE("%s: isSupported is NULL", __FUNCTION__); | 
|  | 2639 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "isSupported is NULL"); | 
|  | 2640 | } | 
|  | 2641 |  | 
|  | 2642 | if (!mInitialized) { | 
|  | 2643 | ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__); | 
|  | 2644 | return STATUS_ERROR(ERROR_DISCONNECTED, | 
|  | 2645 | "Camera subsystem is not available"); | 
|  | 2646 | } | 
|  | 2647 |  | 
|  | 2648 | // Check for camera permissions | 
|  | 2649 | int callingPid = CameraThreadState::getCallingPid(); | 
|  | 2650 | int callingUid = CameraThreadState::getCallingUid(); | 
|  | 2651 | if ((callingPid != getpid()) && !checkPermission(sCameraPermission, callingPid, callingUid)) { | 
|  | 2652 | ALOGE("%s: pid %d doesn't have camera permissions", __FUNCTION__, callingPid); | 
|  | 2653 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, | 
|  | 2654 | "android.permission.CAMERA needed to call" | 
|  | 2655 | "isConcurrentSessionConfigurationSupported"); | 
|  | 2656 | } | 
|  | 2657 |  | 
|  | 2658 | status_t res = | 
|  | 2659 | mCameraProviderManager->isConcurrentSessionConfigurationSupported( | 
| Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 2660 | cameraIdsAndSessionConfigurations, mPerfClassPrimaryCameraIds, | 
|  | 2661 | targetSdkVersion, isSupported); | 
| Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2662 | if (res != OK) { | 
| Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2663 | logServiceError(String8::format("Unable to query session configuration support"), | 
|  | 2664 | ERROR_INVALID_OPERATION); | 
| Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 2665 | return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to query session configuration " | 
|  | 2666 | "support %s (%d)", strerror(-res), res); | 
|  | 2667 | } | 
|  | 2668 | return Status::ok(); | 
|  | 2669 | } | 
|  | 2670 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2671 | Status CameraService::addListener(const sp<ICameraServiceListener>& listener, | 
|  | 2672 | /*out*/ | 
|  | 2673 | std::vector<hardware::CameraStatus> *cameraStatuses) { | 
| Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2674 | return addListenerHelper(listener, cameraStatuses); | 
|  | 2675 | } | 
|  | 2676 |  | 
| Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2677 | binder::Status CameraService::addListenerTest(const sp<hardware::ICameraServiceListener>& listener, | 
|  | 2678 | std::vector<hardware::CameraStatus>* cameraStatuses) { | 
|  | 2679 | return addListenerHelper(listener, cameraStatuses, false, true); | 
|  | 2680 | } | 
|  | 2681 |  | 
| Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2682 | Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener, | 
|  | 2683 | /*out*/ | 
|  | 2684 | std::vector<hardware::CameraStatus> *cameraStatuses, | 
| Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2685 | bool isVendorListener, bool isProcessLocalTest) { | 
| Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 2686 |  | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2687 | ATRACE_CALL(); | 
|  | 2688 |  | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2689 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2690 |  | 
| Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 2691 | if (listener == nullptr) { | 
| Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2692 | ALOGE("%s: Listener must not be null", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2693 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener"); | 
| Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2694 | } | 
|  | 2695 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2696 | auto clientUid = CameraThreadState::getCallingUid(); | 
|  | 2697 | auto clientPid = CameraThreadState::getCallingPid(); | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2698 | bool openCloseCallbackAllowed = checkPermission(sCameraOpenCloseListenerPermission, | 
| Jayant Chowdhary | c3198c3 | 2021-07-28 20:59:14 +0000 | [diff] [blame] | 2699 | clientPid, clientUid, /*logPermissionFailure*/false); | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2700 |  | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2701 | Mutex::Autolock lock(mServiceLock); | 
|  | 2702 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2703 | { | 
|  | 2704 | Mutex::Autolock lock(mStatusListenerLock); | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2705 | for (const auto &it : mListenerList) { | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2706 | if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2707 | ALOGW("%s: Tried to add listener %p which was already subscribed", | 
|  | 2708 | __FUNCTION__, listener.get()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2709 | return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered"); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2710 | } | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2711 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2712 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2713 | sp<ServiceListener> serviceListener = | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 2714 | new ServiceListener(this, listener, clientUid, clientPid, isVendorListener, | 
|  | 2715 | openCloseCallbackAllowed); | 
| Jayant Chowdhary | 32ced0e | 2021-04-09 14:00:22 -0700 | [diff] [blame] | 2716 | auto ret = serviceListener->initialize(isProcessLocalTest); | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2717 | if (ret != NO_ERROR) { | 
|  | 2718 | String8 msg = String8::format("Failed to initialize service listener: %s (%d)", | 
|  | 2719 | strerror(-ret), ret); | 
| Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 2720 | logServiceError(msg,ERROR_ILLEGAL_ARGUMENT); | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2721 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 2722 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); | 
|  | 2723 | } | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2724 | // The listener still needs to be added to the list of listeners, regardless of what | 
|  | 2725 | // permissions the listener process has / whether it is a vendor listener. Since it might be | 
|  | 2726 | // eligible to listen to other camera ids. | 
|  | 2727 | mListenerList.emplace_back(serviceListener); | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 2728 | mUidPolicy->registerMonitorUid(clientUid); | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2729 | } | 
|  | 2730 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2731 | /* Collect current devices and status */ | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2732 | { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2733 | Mutex::Autolock lock(mCameraStatesLock); | 
|  | 2734 | for (auto& i : mCameraStates) { | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 2735 | cameraStatuses->emplace_back(i.first, | 
| Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 2736 | mapToInterface(i.second->getStatus()), i.second->getUnavailablePhysicalIds(), | 
|  | 2737 | openCloseCallbackAllowed ? i.second->getClientPackage() : String8::empty()); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2738 | } | 
|  | 2739 | } | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 2740 | // Remove the camera statuses that should be hidden from the client, we do | 
|  | 2741 | // this after collecting the states in order to avoid holding | 
|  | 2742 | // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at | 
|  | 2743 | // the same time. | 
|  | 2744 | cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(), | 
|  | 2745 | [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) { | 
|  | 2746 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
|  | 2747 | if (getSystemCameraKind(s.cameraId, &deviceKind) != OK) { | 
|  | 2748 | ALOGE("%s: Invalid camera id %s, skipping status update", | 
|  | 2749 | __FUNCTION__, s.cameraId.c_str()); | 
|  | 2750 | return true; | 
|  | 2751 | } | 
|  | 2752 | return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid, | 
|  | 2753 | clientUid);}), cameraStatuses->end()); | 
|  | 2754 |  | 
| Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2755 | //cameraStatuses will have non-eligible camera ids removed. | 
|  | 2756 | std::set<String16> idsChosenForCallback; | 
|  | 2757 | for (const auto &s : *cameraStatuses) { | 
|  | 2758 | idsChosenForCallback.insert(String16(s.cameraId)); | 
|  | 2759 | } | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2760 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2761 | /* | 
|  | 2762 | * Immediately signal current torch status to this listener only | 
|  | 2763 | * This may be a subset of all the devices, so don't include it in the response directly | 
|  | 2764 | */ | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2765 | { | 
|  | 2766 | Mutex::Autolock al(mTorchStatusMutex); | 
|  | 2767 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2768 | String16 id = String16(mTorchStatusMap.keyAt(i).string()); | 
| Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 2769 | // The camera id is visible to the client. Fine to send torch | 
|  | 2770 | // callback. | 
|  | 2771 | if (idsChosenForCallback.find(id) != idsChosenForCallback.end()) { | 
|  | 2772 | listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id); | 
|  | 2773 | } | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2774 | } | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2775 | } | 
|  | 2776 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2777 | return Status::ok(); | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2778 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2779 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2780 | Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2781 | ATRACE_CALL(); | 
|  | 2782 |  | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2783 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); | 
|  | 2784 |  | 
| Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2785 | if (listener == 0) { | 
|  | 2786 | ALOGE("%s: Listener must not be null", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2787 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener"); | 
| Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 2788 | } | 
|  | 2789 |  | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2790 | Mutex::Autolock lock(mServiceLock); | 
|  | 2791 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2792 | { | 
|  | 2793 | Mutex::Autolock lock(mStatusListenerLock); | 
|  | 2794 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 2795 | if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) { | 
|  | 2796 | mUidPolicy->unregisterMonitorUid((*it)->getListenerUid()); | 
|  | 2797 | IInterface::asBinder(listener)->unlinkToDeath(*it); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2798 | mListenerList.erase(it); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2799 | return Status::ok(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2800 | } | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2801 | } | 
|  | 2802 | } | 
|  | 2803 |  | 
|  | 2804 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", | 
|  | 2805 | __FUNCTION__, listener.get()); | 
|  | 2806 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2807 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener"); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2808 | } | 
|  | 2809 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2810 | Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2811 |  | 
|  | 2812 | ATRACE_CALL(); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2813 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); | 
|  | 2814 |  | 
|  | 2815 | if (parameters == NULL) { | 
|  | 2816 | ALOGE("%s: parameters must not be null", __FUNCTION__); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2817 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null"); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2818 | } | 
|  | 2819 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2820 | Status ret = Status::ok(); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2821 |  | 
|  | 2822 | CameraParameters shimParams; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2823 | if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) { | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2824 | // Error logged by caller | 
|  | 2825 | return ret; | 
|  | 2826 | } | 
|  | 2827 |  | 
|  | 2828 | String8 shimParamsString8 = shimParams.flatten(); | 
|  | 2829 | String16 shimParamsString16 = String16(shimParamsString8); | 
|  | 2830 |  | 
|  | 2831 | *parameters = shimParamsString16; | 
|  | 2832 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2833 | return ret; | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2834 | } | 
|  | 2835 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 2836 | Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion, | 
|  | 2837 | /*out*/ bool *isSupported) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 2838 | ATRACE_CALL(); | 
|  | 2839 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2840 | const String8 id = String8(cameraId); | 
|  | 2841 |  | 
|  | 2842 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2843 |  | 
|  | 2844 | switch (apiVersion) { | 
|  | 2845 | case API_VERSION_1: | 
|  | 2846 | case API_VERSION_2: | 
|  | 2847 | break; | 
|  | 2848 | default: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2849 | String8 msg = String8::format("Unknown API version %d", apiVersion); | 
|  | 2850 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 2851 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2852 | } | 
|  | 2853 |  | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 2854 | int portraitRotation; | 
|  | 2855 | auto deviceVersionAndTransport = getDeviceVersion(id, false, &portraitRotation); | 
| Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2856 | if (deviceVersionAndTransport.first == -1) { | 
|  | 2857 | String8 msg = String8::format("Unknown camera ID %s", id.string()); | 
|  | 2858 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 2859 | return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string()); | 
|  | 2860 | } | 
|  | 2861 | if (deviceVersionAndTransport.second == IPCTransport::HIDL) { | 
|  | 2862 | int deviceVersion = deviceVersionAndTransport.first; | 
|  | 2863 | switch (deviceVersion) { | 
|  | 2864 | case CAMERA_DEVICE_API_VERSION_1_0: | 
|  | 2865 | case CAMERA_DEVICE_API_VERSION_3_0: | 
|  | 2866 | case CAMERA_DEVICE_API_VERSION_3_1: | 
|  | 2867 | if (apiVersion == API_VERSION_2) { | 
|  | 2868 | ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without " | 
|  | 2869 | "shim", __FUNCTION__, id.string(), deviceVersion); | 
|  | 2870 | *isSupported = false; | 
|  | 2871 | } else { // if (apiVersion == API_VERSION_1) { | 
|  | 2872 | ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always " | 
|  | 2873 | "supported", __FUNCTION__, id.string()); | 
|  | 2874 | *isSupported = true; | 
|  | 2875 | } | 
|  | 2876 | break; | 
|  | 2877 | case CAMERA_DEVICE_API_VERSION_3_2: | 
|  | 2878 | case CAMERA_DEVICE_API_VERSION_3_3: | 
|  | 2879 | case CAMERA_DEVICE_API_VERSION_3_4: | 
|  | 2880 | case CAMERA_DEVICE_API_VERSION_3_5: | 
|  | 2881 | case CAMERA_DEVICE_API_VERSION_3_6: | 
|  | 2882 | case CAMERA_DEVICE_API_VERSION_3_7: | 
|  | 2883 | ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly", | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 2884 | __FUNCTION__, id.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2885 | *isSupported = true; | 
| Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2886 | break; | 
|  | 2887 | default: { | 
|  | 2888 | String8 msg = String8::format("Unknown device version %x for device %s", | 
|  | 2889 | deviceVersion, id.string()); | 
|  | 2890 | ALOGE("%s: %s", __FUNCTION__, msg.string()); | 
|  | 2891 | return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string()); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2892 | } | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2893 | } | 
| Jayant Chowdhary | ffc5d68 | 2022-05-12 18:34:34 +0000 | [diff] [blame] | 2894 | } else { | 
|  | 2895 | *isSupported = true; | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2896 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2897 | return Status::ok(); | 
| Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 2898 | } | 
|  | 2899 |  | 
| Shuzhen Wang | f9d2c02 | 2018-08-21 12:07:35 -0700 | [diff] [blame] | 2900 | Status CameraService::isHiddenPhysicalCamera(const String16& cameraId, | 
|  | 2901 | /*out*/ bool *isSupported) { | 
|  | 2902 | ATRACE_CALL(); | 
|  | 2903 |  | 
|  | 2904 | const String8 id = String8(cameraId); | 
|  | 2905 |  | 
|  | 2906 | ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string()); | 
|  | 2907 | *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(id.string()); | 
|  | 2908 |  | 
|  | 2909 | return Status::ok(); | 
|  | 2910 | } | 
|  | 2911 |  | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2912 | Status CameraService::injectCamera( | 
|  | 2913 | const String16& packageName, const String16& internalCamId, | 
|  | 2914 | const String16& externalCamId, | 
|  | 2915 | const sp<ICameraInjectionCallback>& callback, | 
|  | 2916 | /*out*/ | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2917 | sp<ICameraInjectionSession>* cameraInjectionSession) { | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2918 | ATRACE_CALL(); | 
|  | 2919 |  | 
|  | 2920 | if (!checkCallingPermission(sCameraInjectExternalCameraPermission)) { | 
|  | 2921 | const int pid = CameraThreadState::getCallingPid(); | 
|  | 2922 | const int uid = CameraThreadState::getCallingUid(); | 
|  | 2923 | ALOGE("Permission Denial: can't inject camera pid=%d, uid=%d", pid, uid); | 
|  | 2924 | return STATUS_ERROR(ERROR_PERMISSION_DENIED, | 
|  | 2925 | "Permission Denial: no permission to inject camera"); | 
|  | 2926 | } | 
|  | 2927 |  | 
|  | 2928 | ALOGV( | 
|  | 2929 | "%s: Package name = %s, Internal camera ID = %s, External camera ID = " | 
|  | 2930 | "%s", | 
|  | 2931 | __FUNCTION__, String8(packageName).string(), | 
|  | 2932 | String8(internalCamId).string(), String8(externalCamId).string()); | 
|  | 2933 |  | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2934 | { | 
|  | 2935 | Mutex::Autolock lock(mInjectionParametersLock); | 
|  | 2936 | mInjectionInternalCamId = String8(internalCamId); | 
|  | 2937 | mInjectionExternalCamId = String8(externalCamId); | 
| Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2938 | mInjectionStatusListener->addListener(callback); | 
|  | 2939 | *cameraInjectionSession = new CameraInjectionSession(this); | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2940 | status_t res = NO_ERROR; | 
|  | 2941 | auto clientDescriptor = mActiveClientManager.get(mInjectionInternalCamId); | 
|  | 2942 | // If the client already exists, we can directly connect to the camera device through the | 
|  | 2943 | // client's injectCamera(), otherwise we need to wait until the client is established | 
|  | 2944 | // (execute connectHelper()) before injecting the camera to the camera device. | 
|  | 2945 | if (clientDescriptor != nullptr) { | 
|  | 2946 | mInjectionInitPending = false; | 
| Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 2947 | sp<BasicClient> clientSp = clientDescriptor->getValue(); | 
|  | 2948 | res = checkIfInjectionCameraIsPresent(mInjectionExternalCamId, clientSp); | 
|  | 2949 | if(res != OK) { | 
|  | 2950 | return STATUS_ERROR_FMT(ERROR_DISCONNECTED, | 
|  | 2951 | "No camera device with ID \"%s\" currently available", | 
|  | 2952 | mInjectionExternalCamId.string()); | 
|  | 2953 | } | 
|  | 2954 | res = clientSp->injectCamera(mInjectionExternalCamId, mCameraProviderManager); | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2955 | if(res != OK) { | 
|  | 2956 | mInjectionStatusListener->notifyInjectionError(mInjectionExternalCamId, res); | 
|  | 2957 | } | 
|  | 2958 | } else { | 
|  | 2959 | mInjectionInitPending = true; | 
|  | 2960 | } | 
|  | 2961 | } | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2962 |  | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 2963 | return binder::Status::ok(); | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 2964 | } | 
|  | 2965 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2966 | void CameraService::removeByClient(const BasicClient* client) { | 
| Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2967 | Mutex::Autolock lock(mServiceLock); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2968 | for (auto& i : mActiveClientManager.getAll()) { | 
|  | 2969 | auto clientSp = i->getValue(); | 
|  | 2970 | if (clientSp.get() == client) { | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 2971 | cacheClientTagDumpIfNeeded(client->mCameraIdStr, clientSp.get()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2972 | mActiveClientManager.remove(i); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2973 | } | 
| Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2974 | } | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 2975 | updateAudioRestrictionLocked(); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2976 | } | 
|  | 2977 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2978 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2979 | bool ret = false; | 
|  | 2980 | { | 
|  | 2981 | // Acquire mServiceLock and prevent other clients from connecting | 
|  | 2982 | std::unique_ptr<AutoConditionLock> lock = | 
|  | 2983 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2984 |  | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2985 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2986 | std::vector<sp<BasicClient>> evicted; | 
|  | 2987 | for (auto& i : mActiveClientManager.getAll()) { | 
|  | 2988 | auto clientSp = i->getValue(); | 
|  | 2989 | if (clientSp.get() == nullptr) { | 
|  | 2990 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); | 
|  | 2991 | mActiveClientManager.remove(i); | 
|  | 2992 | continue; | 
|  | 2993 | } | 
| Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 2994 | if (remote == clientSp->getRemote()) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2995 | mActiveClientManager.remove(i); | 
|  | 2996 | evicted.push_back(clientSp); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 2997 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2998 | // Notify the client of disconnection | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2999 | clientSp->notifyError( | 
|  | 3000 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3001 | CaptureResultExtras()); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3002 | } | 
|  | 3003 | } | 
|  | 3004 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3005 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking | 
|  | 3006 | // other clients from connecting in mServiceLockWrapper if held | 
|  | 3007 | mServiceLock.unlock(); | 
|  | 3008 |  | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3009 | // Do not clear caller identity, remote caller should be client proccess | 
|  | 3010 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3011 | for (auto& i : evicted) { | 
|  | 3012 | if (i.get() != nullptr) { | 
|  | 3013 | i->disconnect(); | 
|  | 3014 | ret = true; | 
|  | 3015 | } | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3016 | } | 
|  | 3017 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3018 | // Reacquire mServiceLock | 
|  | 3019 | mServiceLock.lock(); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3020 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3021 | } // lock is destroyed, allow further connect calls | 
|  | 3022 |  | 
|  | 3023 | return ret; | 
| Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 3024 | } | 
|  | 3025 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3026 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( | 
|  | 3027 | const String8& cameraId) const { | 
|  | 3028 | std::shared_ptr<CameraState> state; | 
|  | 3029 | { | 
|  | 3030 | Mutex::Autolock lock(mCameraStatesLock); | 
|  | 3031 | auto iter = mCameraStates.find(cameraId); | 
|  | 3032 | if (iter != mCameraStates.end()) { | 
|  | 3033 | state = iter->second; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3034 | } | 
|  | 3035 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3036 | return state; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3037 | } | 
|  | 3038 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3039 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) { | 
|  | 3040 | // Remove from active clients list | 
|  | 3041 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); | 
|  | 3042 | if (clientDescriptorPtr == nullptr) { | 
|  | 3043 | ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__, | 
|  | 3044 | cameraId.string()); | 
|  | 3045 | return sp<BasicClient>{nullptr}; | 
|  | 3046 | } | 
|  | 3047 |  | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 3048 | sp<BasicClient> client = clientDescriptorPtr->getValue(); | 
|  | 3049 | if (client.get() != nullptr) { | 
|  | 3050 | cacheClientTagDumpIfNeeded(clientDescriptorPtr->getKey(), client.get()); | 
|  | 3051 | } | 
|  | 3052 | return client; | 
| Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 3053 | } | 
|  | 3054 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3055 | void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) { | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3056 | // Acquire mServiceLock and prevent other clients from connecting | 
|  | 3057 | std::unique_ptr<AutoConditionLock> lock = | 
|  | 3058 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); | 
|  | 3059 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3060 | std::set<userid_t> newAllowedUsers; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3061 | for (size_t i = 0; i < newUserIds.size(); i++) { | 
|  | 3062 | if (newUserIds[i] < 0) { | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3063 | ALOGE("%s: Bad user ID %d given during user switch, ignoring.", | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3064 | __FUNCTION__, newUserIds[i]); | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3065 | return; | 
|  | 3066 | } | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3067 | newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i])); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3068 | } | 
|  | 3069 |  | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3070 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3071 | if (newAllowedUsers == mAllowedUsers) { | 
|  | 3072 | ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__); | 
|  | 3073 | return; | 
|  | 3074 | } | 
|  | 3075 |  | 
|  | 3076 | logUserSwitch(mAllowedUsers, newAllowedUsers); | 
|  | 3077 |  | 
|  | 3078 | mAllowedUsers = std::move(newAllowedUsers); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3079 |  | 
|  | 3080 | // Current user has switched, evict all current clients. | 
|  | 3081 | std::vector<sp<BasicClient>> evicted; | 
|  | 3082 | for (auto& i : mActiveClientManager.getAll()) { | 
|  | 3083 | auto clientSp = i->getValue(); | 
|  | 3084 |  | 
|  | 3085 | if (clientSp.get() == nullptr) { | 
|  | 3086 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); | 
|  | 3087 | continue; | 
|  | 3088 | } | 
|  | 3089 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3090 | // Don't evict clients that are still allowed. | 
|  | 3091 | uid_t clientUid = clientSp->getClientUid(); | 
|  | 3092 | userid_t clientUserId = multiuser_get_user_id(clientUid); | 
|  | 3093 | if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) { | 
|  | 3094 | continue; | 
|  | 3095 | } | 
|  | 3096 |  | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3097 | evicted.push_back(clientSp); | 
|  | 3098 |  | 
|  | 3099 | String8 curTime = getFormattedCurrentTime(); | 
|  | 3100 |  | 
|  | 3101 | ALOGE("Evicting conflicting client for camera ID %s due to user change", | 
|  | 3102 | i->getKey().string()); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3103 |  | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3104 | // Log the clients evicted | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3105 | logEvent(String8::format("EVICT device %s client held by package %s (PID %" | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 3106 | PRId32 ", score %" PRId32 ", state %" PRId32 ")\n   - Evicted due" | 
|  | 3107 | " to user switch.", i->getKey().string(), | 
|  | 3108 | String8{clientSp->getPackageName()}.string(), | 
|  | 3109 | i->getOwnerId(), i->getPriority().getScore(), | 
|  | 3110 | i->getPriority().getState())); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3111 |  | 
|  | 3112 | } | 
|  | 3113 |  | 
|  | 3114 | // Do not hold mServiceLock while disconnecting clients, but retain the condition | 
|  | 3115 | // blocking other clients from connecting in mServiceLockWrapper if held. | 
|  | 3116 | mServiceLock.unlock(); | 
|  | 3117 |  | 
|  | 3118 | // Clear caller identity temporarily so client disconnect PID checks work correctly | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3119 | int64_t token = CameraThreadState::clearCallingIdentity(); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3120 |  | 
|  | 3121 | for (auto& i : evicted) { | 
|  | 3122 | i->disconnect(); | 
|  | 3123 | } | 
|  | 3124 |  | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3125 | CameraThreadState::restoreCallingIdentity(token); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3126 |  | 
|  | 3127 | // Reacquire mServiceLock | 
|  | 3128 | mServiceLock.lock(); | 
|  | 3129 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3130 |  | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3131 | void CameraService::logEvent(const char* event) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3132 | String8 curTime = getFormattedCurrentTime(); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3133 | Mutex::Autolock l(mLogLock); | 
| Rucha Katakwar | d9ea645 | 2021-05-06 11:57:16 -0700 | [diff] [blame] | 3134 | String8 msg = String8::format("%s : %s", curTime.string(), event); | 
|  | 3135 | // For service error events, print the msg only once. | 
|  | 3136 | if(!msg.contains("SERVICE ERROR")) { | 
|  | 3137 | mEventLog.add(msg); | 
|  | 3138 | } else if(sServiceErrorEventSet.find(msg) == sServiceErrorEventSet.end()) { | 
|  | 3139 | // Error event not added to the dumpsys log before | 
|  | 3140 | mEventLog.add(msg); | 
|  | 3141 | sServiceErrorEventSet.insert(msg); | 
|  | 3142 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3143 | } | 
|  | 3144 |  | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3145 | void CameraService::logDisconnected(const char* cameraId, int clientPid, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3146 | const char* clientPackage) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3147 | // Log the clients evicted | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3148 | 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] | 3149 | clientPackage, clientPid)); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3150 | } | 
|  | 3151 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3152 | void CameraService::logDisconnectedOffline(const char* cameraId, int clientPid, | 
|  | 3153 | const char* clientPackage) { | 
|  | 3154 | // Log the clients evicted | 
|  | 3155 | logEvent(String8::format("DISCONNECT offline device %s client for package %s (PID %d)", | 
|  | 3156 | cameraId, clientPackage, clientPid)); | 
|  | 3157 | } | 
|  | 3158 |  | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3159 | void CameraService::logConnected(const char* cameraId, int clientPid, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3160 | const char* clientPackage) { | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3161 | // Log the clients evicted | 
|  | 3162 | 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] | 3163 | clientPackage, clientPid)); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3164 | } | 
|  | 3165 |  | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3166 | void CameraService::logConnectedOffline(const char* cameraId, int clientPid, | 
|  | 3167 | const char* clientPackage) { | 
|  | 3168 | // Log the clients evicted | 
|  | 3169 | logEvent(String8::format("CONNECT offline device %s client for package %s (PID %d)", cameraId, | 
|  | 3170 | clientPackage, clientPid)); | 
|  | 3171 | } | 
|  | 3172 |  | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3173 | void CameraService::logRejected(const char* cameraId, int clientPid, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3174 | const char* clientPackage, const char* reason) { | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3175 | // Log the client rejected | 
|  | 3176 | 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] | 3177 | cameraId, clientPackage, clientPid, reason)); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3178 | } | 
|  | 3179 |  | 
| Jayant Chowdhary | 0e2eefd | 2019-04-18 14:05:43 -0700 | [diff] [blame] | 3180 | void CameraService::logTorchEvent(const char* cameraId, const char *torchState, int clientPid) { | 
|  | 3181 | // Log torch event | 
|  | 3182 | logEvent(String8::format("Torch for camera id %s turned %s for client PID %d", cameraId, | 
|  | 3183 | torchState, clientPid)); | 
|  | 3184 | } | 
|  | 3185 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3186 | void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds, | 
|  | 3187 | const std::set<userid_t>& newUserIds) { | 
|  | 3188 | String8 newUsers = toString(newUserIds); | 
|  | 3189 | String8 oldUsers = toString(oldUserIds); | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3190 | if (oldUsers.size() == 0) { | 
|  | 3191 | oldUsers = "<None>"; | 
|  | 3192 | } | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3193 | // Log the new and old users | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 3194 | 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] | 3195 | oldUsers.string(), newUsers.string())); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 3196 | } | 
|  | 3197 |  | 
|  | 3198 | void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) { | 
|  | 3199 | // Log the device removal | 
|  | 3200 | logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason)); | 
|  | 3201 | } | 
|  | 3202 |  | 
|  | 3203 | void CameraService::logDeviceAdded(const char* cameraId, const char* reason) { | 
|  | 3204 | // Log the device removal | 
|  | 3205 | logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason)); | 
|  | 3206 | } | 
|  | 3207 |  | 
|  | 3208 | void CameraService::logClientDied(int clientPid, const char* reason) { | 
|  | 3209 | // Log the device removal | 
|  | 3210 | 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] | 3211 | } | 
|  | 3212 |  | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 3213 | void CameraService::logServiceError(const char* msg, int errorCode) { | 
|  | 3214 | String8 curTime = getFormattedCurrentTime(); | 
| Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 3215 | 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] | 3216 | } | 
|  | 3217 |  | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3218 | status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, | 
|  | 3219 | uint32_t flags) { | 
|  | 3220 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3221 | // Permission checks | 
|  | 3222 | switch (code) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3223 | case SHELL_COMMAND_TRANSACTION: { | 
|  | 3224 | int in = data.readFileDescriptor(); | 
|  | 3225 | int out = data.readFileDescriptor(); | 
|  | 3226 | int err = data.readFileDescriptor(); | 
|  | 3227 | int argc = data.readInt32(); | 
|  | 3228 | Vector<String16> args; | 
|  | 3229 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { | 
|  | 3230 | args.add(data.readString16()); | 
|  | 3231 | } | 
|  | 3232 | sp<IBinder> unusedCallback; | 
|  | 3233 | sp<IResultReceiver> resultReceiver; | 
|  | 3234 | status_t status; | 
|  | 3235 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { | 
|  | 3236 | return status; | 
|  | 3237 | } | 
|  | 3238 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { | 
|  | 3239 | return status; | 
|  | 3240 | } | 
|  | 3241 | status = shellCommand(in, out, err, args); | 
|  | 3242 | if (resultReceiver != nullptr) { | 
|  | 3243 | resultReceiver->send(status); | 
|  | 3244 | } | 
|  | 3245 | return NO_ERROR; | 
|  | 3246 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3247 | } | 
|  | 3248 |  | 
|  | 3249 | return BnCameraService::onTransact(code, data, reply, flags); | 
|  | 3250 | } | 
|  | 3251 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3252 | // We share the media players for shutter and recording sound for all clients. | 
|  | 3253 | // A reference count is kept to determine when we will actually release the | 
|  | 3254 | // media players. | 
|  | 3255 |  | 
| Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3256 | sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) { | 
|  | 3257 | sp<MediaPlayer> mp = new MediaPlayer(); | 
|  | 3258 | status_t error; | 
|  | 3259 | if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) { | 
| Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 3260 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); | 
| Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3261 | error = mp->prepare(); | 
|  | 3262 | } | 
|  | 3263 | if (error != NO_ERROR) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3264 | ALOGE("Failed to load CameraService sounds: %s", file); | 
| Jaekyun Seok | ef49805 | 2018-03-23 13:09:44 +0900 | [diff] [blame] | 3265 | mp->disconnect(); | 
|  | 3266 | mp.clear(); | 
| Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3267 | return nullptr; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3268 | } | 
|  | 3269 | return mp; | 
|  | 3270 | } | 
|  | 3271 |  | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3272 | void CameraService::increaseSoundRef() { | 
|  | 3273 | Mutex::Autolock lock(mSoundLock); | 
|  | 3274 | mSoundRef++; | 
|  | 3275 | } | 
|  | 3276 |  | 
|  | 3277 | void CameraService::loadSoundLocked(sound_kind kind) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3278 | ATRACE_CALL(); | 
|  | 3279 |  | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3280 | LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef); | 
|  | 3281 | if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) { | 
|  | 3282 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg"); | 
|  | 3283 | if (mSoundPlayer[SOUND_SHUTTER] == nullptr) { | 
|  | 3284 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); | 
|  | 3285 | } | 
|  | 3286 | } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] ==  NULL) { | 
|  | 3287 | mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg"); | 
|  | 3288 | if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) { | 
|  | 3289 | mSoundPlayer[SOUND_RECORDING_START] = | 
| Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3290 | newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3291 | } | 
|  | 3292 | } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) { | 
|  | 3293 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg"); | 
|  | 3294 | if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) { | 
|  | 3295 | mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg"); | 
|  | 3296 | } | 
| Jaekyun Seok | 59a8ef0 | 2018-01-15 14:49:05 +0900 | [diff] [blame] | 3297 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3298 | } | 
|  | 3299 |  | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3300 | void CameraService::decreaseSoundRef() { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3301 | Mutex::Autolock lock(mSoundLock); | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3302 | LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3303 | if (--mSoundRef) return; | 
|  | 3304 |  | 
|  | 3305 | for (int i = 0; i < NUM_SOUNDS; i++) { | 
|  | 3306 | if (mSoundPlayer[i] != 0) { | 
|  | 3307 | mSoundPlayer[i]->disconnect(); | 
|  | 3308 | mSoundPlayer[i].clear(); | 
|  | 3309 | } | 
|  | 3310 | } | 
|  | 3311 | } | 
|  | 3312 |  | 
|  | 3313 | void CameraService::playSound(sound_kind kind) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3314 | ATRACE_CALL(); | 
|  | 3315 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3316 | LOG1("playSound(%d)", kind); | 
| Eino-Ville Talvala | 139ca75 | 2021-04-23 15:40:34 -0700 | [diff] [blame] | 3317 | if (kind < 0 || kind >= NUM_SOUNDS) { | 
|  | 3318 | ALOGE("%s: Invalid sound id requested: %d", __FUNCTION__, kind); | 
|  | 3319 | return; | 
|  | 3320 | } | 
|  | 3321 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3322 | Mutex::Autolock lock(mSoundLock); | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3323 | loadSoundLocked(kind); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3324 | sp<MediaPlayer> player = mSoundPlayer[kind]; | 
|  | 3325 | if (player != 0) { | 
| Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 3326 | player->seekTo(0); | 
|  | 3327 | player->start(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3328 | } | 
|  | 3329 | } | 
|  | 3330 |  | 
|  | 3331 | // ---------------------------------------------------------------------------- | 
|  | 3332 |  | 
|  | 3333 | CameraService::Client::Client(const sp<CameraService>& cameraService, | 
| Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 3334 | const sp<ICameraClient>& cameraClient, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3335 | const String16& clientPackageName, bool systemNativeClient, | 
| Jooyung Han | b3f7cd2 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 3336 | const std::optional<String16>& clientFeatureId, | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3337 | const String8& cameraIdStr, | 
| Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3338 | int api1CameraId, int cameraFacing, int sensorOrientation, | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3339 | int clientPid, uid_t clientUid, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3340 | int servicePid, bool overrideToPortrait) : | 
| Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3341 | CameraService::BasicClient(cameraService, | 
| Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 3342 | IInterface::asBinder(cameraClient), | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3343 | clientPackageName, systemNativeClient, clientFeatureId, | 
| Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3344 | cameraIdStr, cameraFacing, sensorOrientation, | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3345 | clientPid, clientUid, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3346 | servicePid, overrideToPortrait), | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 3347 | mCameraId(api1CameraId) | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3348 | { | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3349 | int callingPid = CameraThreadState::getCallingPid(); | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3350 | LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3351 |  | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 3352 | mRemoteCallback = cameraClient; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3353 |  | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3354 | cameraService->increaseSoundRef(); | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3355 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3356 | LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3357 | } | 
|  | 3358 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3359 | // tear down the client | 
|  | 3360 | CameraService::Client::~Client() { | 
| Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3361 | ALOGV("~Client"); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3362 | mDestructionStarted = true; | 
|  | 3363 |  | 
| username | 5755fea | 2018-12-27 09:48:08 +0800 | [diff] [blame] | 3364 | sCameraService->decreaseSoundRef(); | 
| Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3365 | // unconditionally disconnect. function is idempotent | 
|  | 3366 | Client::disconnect(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3367 | } | 
|  | 3368 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3369 | sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService; | 
|  | 3370 |  | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3371 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3372 | const sp<IBinder>& remoteCallback, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3373 | const String16& clientPackageName, bool nativeClient, | 
|  | 3374 | const std::optional<String16>& clientFeatureId, const String8& cameraIdStr, | 
|  | 3375 | int cameraFacing, int sensorOrientation, int clientPid, uid_t clientUid, | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3376 | int servicePid, bool overrideToPortrait): | 
| Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3377 | mDestructionStarted(false), | 
| Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3378 | mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing), mOrientation(sensorOrientation), | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3379 | mClientPackageName(clientPackageName), mSystemNativeClient(nativeClient), | 
|  | 3380 | mClientFeatureId(clientFeatureId), | 
| Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 3381 | mClientPid(clientPid), mClientUid(clientUid), | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3382 | mServicePid(servicePid), | 
| Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3383 | mDisconnected(false), mUidIsTrusted(false), | 
| Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 3384 | mOverrideToPortrait(overrideToPortrait), | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3385 | mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE), | 
| Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3386 | mRemoteBinder(remoteCallback), | 
|  | 3387 | mOpsActive(false), | 
|  | 3388 | mOpsStreaming(false) | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3389 | { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3390 | if (sCameraService == nullptr) { | 
|  | 3391 | sCameraService = cameraService; | 
|  | 3392 | } | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 3393 |  | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 3394 | // There are 2 scenarios in which a client won't have AppOps operations | 
|  | 3395 | // (both scenarios : native clients) | 
|  | 3396 | //    1) It's an system native client*, the package name will be empty | 
|  | 3397 | //       and it will return from this function in the previous if condition | 
|  | 3398 | //       (This is the same as the previously existing behavior). | 
|  | 3399 | //    2) It is a system native client, but its package name has been | 
|  | 3400 | //       modified for debugging, however it still must not use AppOps since | 
|  | 3401 | //       the package name is not a real one. | 
|  | 3402 | // | 
|  | 3403 | //       * system native client - native client with UID < AID_APP_START. It | 
|  | 3404 | //         doesn't exclude clients not on the system partition. | 
|  | 3405 | if (!mSystemNativeClient) { | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3406 | mAppOpsManager = std::make_unique<AppOpsManager>(); | 
|  | 3407 | } | 
| Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 3408 |  | 
|  | 3409 | mUidIsTrusted = isTrustedCallingUid(mClientUid); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3410 | } | 
|  | 3411 |  | 
|  | 3412 | CameraService::BasicClient::~BasicClient() { | 
| Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3413 | ALOGV("~BasicClient"); | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3414 | mDestructionStarted = true; | 
|  | 3415 | } | 
|  | 3416 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3417 | binder::Status CameraService::BasicClient::disconnect() { | 
|  | 3418 | binder::Status res = Status::ok(); | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3419 | if (mDisconnected) { | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3420 | return res; | 
| Ruben Brunk | 36597b2 | 2015-03-20 22:15:57 -0700 | [diff] [blame] | 3421 | } | 
| Eino-Ville Talvala | 24901c8 | 2015-09-04 14:15:58 -0700 | [diff] [blame] | 3422 | mDisconnected = true; | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3423 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3424 | sCameraService->removeByClient(this); | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3425 | sCameraService->logDisconnected(mCameraIdStr, mClientPid, String8(mClientPackageName)); | 
| Peter Kalauskas | a29c135 | 2018-10-10 12:05:42 -0700 | [diff] [blame] | 3426 | sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA, | 
|  | 3427 | mCameraIdStr.c_str()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3428 |  | 
|  | 3429 | sp<IBinder> remote = getRemote(); | 
|  | 3430 | if (remote != nullptr) { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3431 | remote->unlinkToDeath(sCameraService); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3432 | } | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3433 |  | 
|  | 3434 | finishCameraOps(); | 
| Chien-Yu Chen | e4fe21b | 2016-08-04 12:42:40 -0700 | [diff] [blame] | 3435 | // Notify flashlight that a camera device is closed. | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3436 | sCameraService->mFlashlight->deviceClosed(mCameraIdStr); | 
|  | 3437 | ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(), | 
|  | 3438 | mClientPid); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3439 |  | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 3440 | // client shouldn't be able to call into us anymore | 
|  | 3441 | mClientPid = 0; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3442 |  | 
|  | 3443 | return res; | 
| Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 3444 | } | 
|  | 3445 |  | 
| Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3446 | status_t CameraService::BasicClient::dump(int, const Vector<String16>&) { | 
|  | 3447 | // No dumping of clients directly over Binder, | 
|  | 3448 | // must go through CameraService::dump | 
|  | 3449 | android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403", | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3450 | CameraThreadState::getCallingUid(), NULL, 0); | 
| Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 3451 | return OK; | 
|  | 3452 | } | 
|  | 3453 |  | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 3454 | status_t CameraService::BasicClient::startWatchingTags(const String8&, int) { | 
|  | 3455 | // Can't watch tags directly, must go through CameraService::startWatchingTags | 
|  | 3456 | return OK; | 
|  | 3457 | } | 
|  | 3458 |  | 
|  | 3459 | status_t CameraService::BasicClient::stopWatchingTags(int) { | 
|  | 3460 | // Can't watch tags directly, must go through CameraService::stopWatchingTags | 
|  | 3461 | return OK; | 
|  | 3462 | } | 
|  | 3463 |  | 
|  | 3464 | status_t CameraService::BasicClient::dumpWatchedEventsToVector(std::vector<std::string> &) { | 
|  | 3465 | // Can't watch tags directly, must go through CameraService::dumpWatchedEventsToVector | 
|  | 3466 | return OK; | 
|  | 3467 | } | 
|  | 3468 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3469 | String16 CameraService::BasicClient::getPackageName() const { | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3470 | return mClientPackageName; | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3471 | } | 
|  | 3472 |  | 
| Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 3473 | int CameraService::BasicClient::getCameraFacing() const { | 
|  | 3474 | return mCameraFacing; | 
|  | 3475 | } | 
|  | 3476 |  | 
|  | 3477 | int CameraService::BasicClient::getCameraOrientation() const { | 
|  | 3478 | return mOrientation; | 
|  | 3479 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3480 |  | 
|  | 3481 | int CameraService::BasicClient::getClientPid() const { | 
|  | 3482 | return mClientPid; | 
|  | 3483 | } | 
|  | 3484 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 3485 | uid_t CameraService::BasicClient::getClientUid() const { | 
|  | 3486 | return mClientUid; | 
|  | 3487 | } | 
|  | 3488 |  | 
| Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3489 | bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const { | 
|  | 3490 | // Defaults to API2. | 
|  | 3491 | return level == API_2; | 
|  | 3492 | } | 
|  | 3493 |  | 
| Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3494 | status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) { | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3495 | { | 
|  | 3496 | Mutex::Autolock l(mAudioRestrictionLock); | 
|  | 3497 | mAudioRestriction = mode; | 
|  | 3498 | } | 
| Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 3499 | sCameraService->updateAudioRestriction(); | 
|  | 3500 | return OK; | 
|  | 3501 | } | 
|  | 3502 |  | 
|  | 3503 | int32_t CameraService::BasicClient::getServiceAudioRestriction() const { | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 3504 | return sCameraService->updateAudioRestriction(); | 
|  | 3505 | } | 
|  | 3506 |  | 
|  | 3507 | int32_t CameraService::BasicClient::getAudioRestriction() const { | 
|  | 3508 | Mutex::Autolock l(mAudioRestrictionLock); | 
|  | 3509 | return mAudioRestriction; | 
|  | 3510 | } | 
|  | 3511 |  | 
|  | 3512 | bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) { | 
|  | 3513 | switch (mode) { | 
|  | 3514 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE: | 
|  | 3515 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION: | 
|  | 3516 | case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND: | 
|  | 3517 | return true; | 
|  | 3518 | default: | 
|  | 3519 | return false; | 
|  | 3520 | } | 
|  | 3521 | } | 
|  | 3522 |  | 
| Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3523 | status_t CameraService::BasicClient::handleAppOpMode(int32_t mode) { | 
|  | 3524 | if (mode == AppOpsManager::MODE_ERRORED) { | 
|  | 3525 | ALOGI("Camera %s: Access for \"%s\" has been revoked", | 
|  | 3526 | mCameraIdStr.string(), String8(mClientPackageName).string()); | 
|  | 3527 | return PERMISSION_DENIED; | 
|  | 3528 | } else if (!mUidIsTrusted && mode == AppOpsManager::MODE_IGNORED) { | 
|  | 3529 | // If the calling Uid is trusted (a native service), the AppOpsManager could | 
|  | 3530 | // return MODE_IGNORED. Do not treat such case as error. | 
|  | 3531 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, | 
|  | 3532 | mClientPackageName); | 
|  | 3533 | bool isCameraPrivacyEnabled = | 
| Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3534 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); | 
| Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3535 | if (!isUidActive || !isCameraPrivacyEnabled) { | 
|  | 3536 | ALOGI("Camera %s: Access for \"%s\" has been restricted", | 
|  | 3537 | mCameraIdStr.string(), String8(mClientPackageName).string()); | 
|  | 3538 | // Return the same error as for device policy manager rejection | 
|  | 3539 | return -EACCES; | 
|  | 3540 | } | 
|  | 3541 | } | 
|  | 3542 | return OK; | 
|  | 3543 | } | 
|  | 3544 |  | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3545 | status_t CameraService::BasicClient::startCameraOps() { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3546 | ATRACE_CALL(); | 
|  | 3547 |  | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3548 | { | 
|  | 3549 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3550 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3551 | } | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3552 | if (mAppOpsManager != nullptr) { | 
|  | 3553 | // Notify app ops that the camera is not available | 
|  | 3554 | mOpsCallback = new OpsCallback(this); | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3555 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, | 
|  | 3556 | mClientPackageName, mOpsCallback); | 
| Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 3557 |  | 
| Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3558 | // Just check for camera acccess here on open - delay startOp until | 
|  | 3559 | // camera frames start streaming in startCameraStreamingOps | 
|  | 3560 | int32_t mode = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, mClientUid, | 
|  | 3561 | mClientPackageName); | 
|  | 3562 | status_t res = handleAppOpMode(mode); | 
|  | 3563 | if (res != OK) { | 
|  | 3564 | return res; | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3565 | } | 
| Svetoslav | 28e8ef7 | 2015-05-11 19:21:31 -0700 | [diff] [blame] | 3566 | } | 
|  | 3567 |  | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3568 | mOpsActive = true; | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3569 |  | 
|  | 3570 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3571 | sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr); | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3572 |  | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3573 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid); | 
|  | 3574 |  | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3575 | // Notify listeners of camera open/close status | 
|  | 3576 | sCameraService->updateOpenCloseStatus(mCameraIdStr, true/*open*/, mClientPackageName); | 
|  | 3577 |  | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3578 | return OK; | 
|  | 3579 | } | 
|  | 3580 |  | 
| Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3581 | status_t CameraService::BasicClient::startCameraStreamingOps() { | 
|  | 3582 | ATRACE_CALL(); | 
|  | 3583 |  | 
|  | 3584 | if (!mOpsActive) { | 
|  | 3585 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); | 
|  | 3586 | return INVALID_OPERATION; | 
|  | 3587 | } | 
|  | 3588 | if (mOpsStreaming) { | 
|  | 3589 | ALOGV("%s: Streaming already active!", __FUNCTION__); | 
|  | 3590 | return OK; | 
|  | 3591 | } | 
|  | 3592 |  | 
|  | 3593 | ALOGV("%s: Start camera streaming ops, package name = %s, client UID = %d", | 
|  | 3594 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); | 
|  | 3595 |  | 
|  | 3596 | if (mAppOpsManager != nullptr) { | 
|  | 3597 | int32_t mode = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid, | 
|  | 3598 | mClientPackageName, /*startIfModeDefault*/ false, mClientFeatureId, | 
|  | 3599 | String16("start camera ") + String16(mCameraIdStr)); | 
|  | 3600 | status_t res = handleAppOpMode(mode); | 
|  | 3601 | if (res != OK) { | 
|  | 3602 | return res; | 
|  | 3603 | } | 
|  | 3604 | } | 
|  | 3605 |  | 
|  | 3606 | mOpsStreaming = true; | 
|  | 3607 |  | 
|  | 3608 | return OK; | 
|  | 3609 | } | 
|  | 3610 |  | 
| Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 3611 | status_t CameraService::BasicClient::noteAppOp() { | 
|  | 3612 | ATRACE_CALL(); | 
|  | 3613 |  | 
|  | 3614 | ALOGV("%s: Start camera noteAppOp, package name = %s, client UID = %d", | 
|  | 3615 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); | 
|  | 3616 |  | 
|  | 3617 | // noteAppOp is only used for when camera mute is not supported, in order | 
|  | 3618 | // to trigger the sensor privacy "Unblock" dialog | 
|  | 3619 | if (mAppOpsManager != nullptr) { | 
|  | 3620 | int32_t mode = mAppOpsManager->noteOp(AppOpsManager::OP_CAMERA, mClientUid, | 
|  | 3621 | mClientPackageName, mClientFeatureId, | 
|  | 3622 | String16("start camera ") + String16(mCameraIdStr)); | 
|  | 3623 | status_t res = handleAppOpMode(mode); | 
|  | 3624 | if (res != OK) { | 
|  | 3625 | return res; | 
|  | 3626 | } | 
|  | 3627 | } | 
|  | 3628 |  | 
|  | 3629 | return OK; | 
|  | 3630 | } | 
|  | 3631 |  | 
| Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3632 | status_t CameraService::BasicClient::finishCameraStreamingOps() { | 
|  | 3633 | ATRACE_CALL(); | 
|  | 3634 |  | 
|  | 3635 | if (!mOpsActive) { | 
|  | 3636 | ALOGE("%s: Calling streaming start when not yet active", __FUNCTION__); | 
|  | 3637 | return INVALID_OPERATION; | 
|  | 3638 | } | 
|  | 3639 | if (!mOpsStreaming) { | 
|  | 3640 | ALOGV("%s: Streaming not active!", __FUNCTION__); | 
|  | 3641 | return OK; | 
|  | 3642 | } | 
|  | 3643 |  | 
|  | 3644 | if (mAppOpsManager != nullptr) { | 
|  | 3645 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, | 
|  | 3646 | mClientPackageName, mClientFeatureId); | 
|  | 3647 | mOpsStreaming = false; | 
|  | 3648 | } | 
|  | 3649 |  | 
|  | 3650 | return OK; | 
|  | 3651 | } | 
|  | 3652 |  | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3653 | status_t CameraService::BasicClient::finishCameraOps() { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3654 | ATRACE_CALL(); | 
|  | 3655 |  | 
| Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3656 | if (mOpsStreaming) { | 
|  | 3657 | // Make sure we've notified everyone about camera stopping | 
|  | 3658 | finishCameraStreamingOps(); | 
|  | 3659 | } | 
|  | 3660 |  | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3661 | // Check if startCameraOps succeeded, and if so, finish the camera op | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3662 | if (mOpsActive) { | 
| Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 3663 | mOpsActive = false; | 
|  | 3664 |  | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3665 | // This function is called when a client disconnects. This should | 
|  | 3666 | // release the camera, but actually only if it was in a proper | 
|  | 3667 | // functional state, i.e. with status NOT_AVAILABLE | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 3668 | std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT, | 
| Guennadi Liakhovetski | 151e3be | 2017-11-28 16:34:18 +0100 | [diff] [blame] | 3669 | StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT}; | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3670 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 3671 | // 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] | 3672 | sCameraService->updateStatus(StatusInternal::PRESENT, | 
|  | 3673 | mCameraIdStr, rejected); | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3674 | } | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 3675 | // Always stop watching, even if no camera op is active | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3676 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { | 
|  | 3677 | mAppOpsManager->stopWatchingMode(mOpsCallback); | 
| Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 3678 | } | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3679 | mOpsCallback.clear(); | 
|  | 3680 |  | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3681 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid); | 
|  | 3682 |  | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 3683 | // Notify listeners of camera open/close status | 
|  | 3684 | sCameraService->updateOpenCloseStatus(mCameraIdStr, false/*open*/, mClientPackageName); | 
|  | 3685 |  | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3686 | return OK; | 
|  | 3687 | } | 
|  | 3688 |  | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3689 | void CameraService::BasicClient::opChanged(int32_t op, const String16&) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 3690 | ATRACE_CALL(); | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3691 | if (mAppOpsManager == nullptr) { | 
|  | 3692 | return; | 
|  | 3693 | } | 
| Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 3694 | // TODO : add offline camera session case | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3695 | if (op != AppOpsManager::OP_CAMERA) { | 
|  | 3696 | ALOGW("Unexpected app ops notification received: %d", op); | 
|  | 3697 | return; | 
|  | 3698 | } | 
|  | 3699 |  | 
|  | 3700 | int32_t res; | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3701 | res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA, | 
| Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 3702 | mClientUid, mClientPackageName); | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3703 | ALOGV("checkOp returns: %d, %s ", res, | 
|  | 3704 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : | 
|  | 3705 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : | 
|  | 3706 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : | 
|  | 3707 | "UNKNOWN"); | 
|  | 3708 |  | 
| Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3709 | if (res == AppOpsManager::MODE_ERRORED) { | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 3710 | ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(), | 
| Jayant Chowdhary | b61526c | 2019-05-13 19:37:42 -0700 | [diff] [blame] | 3711 | String8(mClientPackageName).string()); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3712 | block(); | 
| Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3713 | } else if (res == AppOpsManager::MODE_IGNORED) { | 
|  | 3714 | bool isUidActive = sCameraService->mUidPolicy->isUidActive(mClientUid, mClientPackageName); | 
| Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3715 | bool isCameraPrivacyEnabled = | 
| Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 3716 | sCameraService->mSensorPrivacyPolicy->isCameraPrivacyEnabled(); | 
| Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3717 | ALOGI("Camera %s: Access for \"%s\" has been restricted, isUidTrusted %d, isUidActive %d", | 
|  | 3718 | mCameraIdStr.string(), String8(mClientPackageName).string(), | 
|  | 3719 | mUidIsTrusted, isUidActive); | 
|  | 3720 | // If the calling Uid is trusted (a native service), or the client Uid is active (WAR for | 
|  | 3721 | // b/175320666), the AppOpsManager could return MODE_IGNORED. Do not treat such cases as | 
|  | 3722 | // error. | 
| Valentin Iftime | c0b8d47 | 2021-07-23 20:21:06 +0200 | [diff] [blame] | 3723 | if (!mUidIsTrusted) { | 
|  | 3724 | if (isUidActive && isCameraPrivacyEnabled && supportsCameraMute()) { | 
|  | 3725 | setCameraMute(true); | 
|  | 3726 | } else if (!isUidActive | 
|  | 3727 | || (isCameraPrivacyEnabled && !supportsCameraMute())) { | 
|  | 3728 | block(); | 
|  | 3729 | } | 
| Shuzhen Wang | 6490085 | 2021-02-05 09:03:29 -0800 | [diff] [blame] | 3730 | } | 
| Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 3731 | } else if (res == AppOpsManager::MODE_ALLOWED) { | 
|  | 3732 | setCameraMute(sCameraService->mOverrideCameraMuteMode); | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3733 | } | 
|  | 3734 | } | 
|  | 3735 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3736 | void CameraService::BasicClient::block() { | 
|  | 3737 | ATRACE_CALL(); | 
|  | 3738 |  | 
|  | 3739 | // Reset the client PID to allow server-initiated disconnect, | 
|  | 3740 | // and to prevent further calls by client. | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 3741 | mClientPid = CameraThreadState::getCallingPid(); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3742 | CaptureResultExtras resultExtras; // a dummy result (invalid) | 
|  | 3743 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras); | 
|  | 3744 | disconnect(); | 
|  | 3745 | } | 
|  | 3746 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3747 | // ---------------------------------------------------------------------------- | 
|  | 3748 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3749 | void CameraService::Client::notifyError(int32_t errorCode, | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3750 | const CaptureResultExtras& resultExtras) { | 
| Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 3751 | (void) resultExtras; | 
| Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3752 | if (mRemoteCallback != NULL) { | 
| Yin-Chia Yeh | f13bda5 | 2018-05-31 12:12:59 -0700 | [diff] [blame] | 3753 | int32_t api1ErrorCode = CAMERA_ERROR_RELEASED; | 
|  | 3754 | if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) { | 
|  | 3755 | api1ErrorCode = CAMERA_ERROR_DISABLED; | 
|  | 3756 | } | 
|  | 3757 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0); | 
| Ranjith Kagathi Ananda | 3e60089 | 2015-10-08 16:00:33 -0700 | [diff] [blame] | 3758 | } else { | 
|  | 3759 | ALOGE("mRemoteCallback is NULL!!"); | 
|  | 3760 | } | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3761 | } | 
|  | 3762 |  | 
| Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 3763 | // NOTE: function is idempotent | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3764 | binder::Status CameraService::Client::disconnect() { | 
| Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 3765 | ALOGV("Client::disconnect"); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3766 | return BasicClient::disconnect(); | 
| Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 3767 | } | 
|  | 3768 |  | 
| Ruben Brunk | 0bbf8b2 | 2015-04-30 14:35:42 -0700 | [diff] [blame] | 3769 | bool CameraService::Client::canCastToApiClient(apiLevel level) const { | 
|  | 3770 | return level == API_1; | 
|  | 3771 | } | 
|  | 3772 |  | 
| Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 3773 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): | 
|  | 3774 | mClient(client) { | 
|  | 3775 | } | 
|  | 3776 |  | 
|  | 3777 | void CameraService::Client::OpsCallback::opChanged(int32_t op, | 
|  | 3778 | const String16& packageName) { | 
|  | 3779 | sp<BasicClient> client = mClient.promote(); | 
|  | 3780 | if (client != NULL) { | 
|  | 3781 | client->opChanged(op, packageName); | 
|  | 3782 | } | 
|  | 3783 | } | 
|  | 3784 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3785 | // ---------------------------------------------------------------------------- | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3786 | //                  UidPolicy | 
|  | 3787 | // ---------------------------------------------------------------------------- | 
|  | 3788 |  | 
|  | 3789 | void CameraService::UidPolicy::registerSelf() { | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3790 | Mutex::Autolock _l(mUidLock); | 
|  | 3791 |  | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3792 | if (mRegistered) return; | 
| Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3793 | status_t res = mAm.linkToDeath(this); | 
|  | 3794 | mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3795 | | ActivityManager::UID_OBSERVER_IDLE | 
| Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3796 | | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE | 
|  | 3797 | | ActivityManager::UID_OBSERVER_PROC_OOM_ADJ, | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3798 | ActivityManager::PROCESS_STATE_UNKNOWN, | 
|  | 3799 | String16("cameraserver")); | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3800 | if (res == OK) { | 
|  | 3801 | mRegistered = true; | 
|  | 3802 | ALOGV("UidPolicy: Registered with ActivityManager"); | 
|  | 3803 | } | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3804 | } | 
|  | 3805 |  | 
|  | 3806 | void CameraService::UidPolicy::unregisterSelf() { | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3807 | Mutex::Autolock _l(mUidLock); | 
|  | 3808 |  | 
| Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 3809 | mAm.unregisterUidObserver(this); | 
|  | 3810 | mAm.unlinkToDeath(this); | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3811 | mRegistered = false; | 
|  | 3812 | mActiveUids.clear(); | 
|  | 3813 | ALOGV("UidPolicy: Unregistered with ActivityManager"); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3814 | } | 
|  | 3815 |  | 
|  | 3816 | void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) { | 
|  | 3817 | onUidIdle(uid, disabled); | 
|  | 3818 | } | 
|  | 3819 |  | 
|  | 3820 | void CameraService::UidPolicy::onUidActive(uid_t uid) { | 
|  | 3821 | Mutex::Autolock _l(mUidLock); | 
|  | 3822 | mActiveUids.insert(uid); | 
|  | 3823 | } | 
|  | 3824 |  | 
|  | 3825 | void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) { | 
|  | 3826 | bool deleted = false; | 
|  | 3827 | { | 
|  | 3828 | Mutex::Autolock _l(mUidLock); | 
|  | 3829 | if (mActiveUids.erase(uid) > 0) { | 
|  | 3830 | deleted = true; | 
|  | 3831 | } | 
|  | 3832 | } | 
|  | 3833 | if (deleted) { | 
|  | 3834 | sp<CameraService> service = mService.promote(); | 
|  | 3835 | if (service != nullptr) { | 
|  | 3836 | service->blockClientsForUid(uid); | 
|  | 3837 | } | 
|  | 3838 | } | 
|  | 3839 | } | 
|  | 3840 |  | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3841 | void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState, | 
| Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 3842 | int64_t procStateSeq __unused, int32_t capability __unused) { | 
| Austin Borger | c5585dc | 2022-05-12 00:48:17 +0000 | [diff] [blame] | 3843 | bool procStateChange = false; | 
|  | 3844 | { | 
|  | 3845 | Mutex::Autolock _l(mUidLock); | 
|  | 3846 | if (mMonitoredUids.find(uid) != mMonitoredUids.end() && | 
|  | 3847 | mMonitoredUids[uid].procState != procState) { | 
|  | 3848 | mMonitoredUids[uid].procState = procState; | 
|  | 3849 | procStateChange = true; | 
|  | 3850 | } | 
|  | 3851 | } | 
|  | 3852 |  | 
|  | 3853 | if (procStateChange) { | 
|  | 3854 | sp<CameraService> service = mService.promote(); | 
|  | 3855 | if (service != nullptr) { | 
|  | 3856 | service->notifyMonitoredUids(); | 
|  | 3857 | } | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3858 | } | 
|  | 3859 | } | 
|  | 3860 |  | 
| Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3861 | void CameraService::UidPolicy::onUidProcAdjChanged(uid_t uid) { | 
|  | 3862 | bool procAdjChange = false; | 
|  | 3863 | { | 
|  | 3864 | Mutex::Autolock _l(mUidLock); | 
|  | 3865 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { | 
|  | 3866 | procAdjChange = true; | 
|  | 3867 | } | 
|  | 3868 | } | 
|  | 3869 |  | 
|  | 3870 | if (procAdjChange) { | 
|  | 3871 | sp<CameraService> service = mService.promote(); | 
|  | 3872 | if (service != nullptr) { | 
|  | 3873 | service->notifyMonitoredUids(); | 
|  | 3874 | } | 
|  | 3875 | } | 
|  | 3876 | } | 
|  | 3877 |  | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3878 | void CameraService::UidPolicy::registerMonitorUid(uid_t uid) { | 
|  | 3879 | Mutex::Autolock _l(mUidLock); | 
|  | 3880 | auto it = mMonitoredUids.find(uid); | 
|  | 3881 | if (it != mMonitoredUids.end()) { | 
| Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3882 | it->second.refCount++; | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3883 | } else { | 
| Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3884 | MonitoredUid monitoredUid; | 
|  | 3885 | monitoredUid.procState = ActivityManager::PROCESS_STATE_NONEXISTENT; | 
|  | 3886 | monitoredUid.refCount = 1; | 
|  | 3887 | mMonitoredUids.emplace(std::pair<uid_t, MonitoredUid>(uid, monitoredUid)); | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3888 | } | 
|  | 3889 | } | 
|  | 3890 |  | 
|  | 3891 | void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid) { | 
|  | 3892 | Mutex::Autolock _l(mUidLock); | 
|  | 3893 | auto it = mMonitoredUids.find(uid); | 
|  | 3894 | if (it != mMonitoredUids.end()) { | 
| Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3895 | it->second.refCount--; | 
|  | 3896 | if (it->second.refCount == 0) { | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 3897 | mMonitoredUids.erase(it); | 
|  | 3898 | } | 
|  | 3899 | } else { | 
|  | 3900 | ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid); | 
|  | 3901 | } | 
|  | 3902 | } | 
|  | 3903 |  | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3904 | bool CameraService::UidPolicy::isUidActive(uid_t uid, String16 callingPackage) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3905 | Mutex::Autolock _l(mUidLock); | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3906 | return isUidActiveLocked(uid, callingPackage); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3907 | } | 
|  | 3908 |  | 
| Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3909 | static const int64_t kPollUidActiveTimeoutTotalMillis = 300; | 
|  | 3910 | static const int64_t kPollUidActiveTimeoutMillis = 50; | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3911 |  | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3912 | bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, String16 callingPackage) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3913 | // Non-app UIDs are considered always active | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3914 | // If activity manager is unreachable, assume everything is active | 
|  | 3915 | if (uid < FIRST_APPLICATION_UID || !mRegistered) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3916 | return true; | 
|  | 3917 | } | 
|  | 3918 | auto it = mOverrideUids.find(uid); | 
|  | 3919 | if (it != mOverrideUids.end()) { | 
|  | 3920 | return it->second; | 
|  | 3921 | } | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3922 | bool active = mActiveUids.find(uid) != mActiveUids.end(); | 
|  | 3923 | if (!active) { | 
|  | 3924 | // We want active UIDs to always access camera with their first attempt since | 
|  | 3925 | // there is no guarantee the app is robustly written and would retry getting | 
|  | 3926 | // the camera on failure. The inverse case is not a problem as we would take | 
|  | 3927 | // camera away soon once we get the callback that the uid is no longer active. | 
|  | 3928 | ActivityManager am; | 
|  | 3929 | // Okay to access with a lock held as UID changes are dispatched without | 
|  | 3930 | // a lock and we are a higher level component. | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3931 | int64_t startTimeMillis = 0; | 
|  | 3932 | do { | 
|  | 3933 | // TODO: Fix this b/109950150! | 
|  | 3934 | // Okay this is a hack. There is a race between the UID turning active and | 
|  | 3935 | // activity being resumed. The proper fix is very risky, so we temporary add | 
|  | 3936 | // some polling which should happen pretty rarely anyway as the race is hard | 
|  | 3937 | // to hit. | 
| Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3938 | active = mActiveUids.find(uid) != mActiveUids.end(); | 
| Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 3939 | if (!active) active = am.isUidActive(uid, callingPackage); | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3940 | if (active) { | 
|  | 3941 | break; | 
|  | 3942 | } | 
|  | 3943 | if (startTimeMillis <= 0) { | 
|  | 3944 | startTimeMillis = uptimeMillis(); | 
|  | 3945 | } | 
|  | 3946 | int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; | 
| Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3947 | int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis; | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3948 | if (remainingTimeMillis <= 0) { | 
|  | 3949 | break; | 
|  | 3950 | } | 
| Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3951 | remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis); | 
|  | 3952 |  | 
|  | 3953 | mUidLock.unlock(); | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3954 | usleep(remainingTimeMillis * 1000); | 
| Eino-Ville Talvala | 32b8c20 | 2018-08-20 10:27:58 -0700 | [diff] [blame] | 3955 | mUidLock.lock(); | 
| Svet Ganov | 94ec46f | 2018-06-08 15:03:46 -0700 | [diff] [blame] | 3956 | } while (true); | 
|  | 3957 |  | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3958 | if (active) { | 
|  | 3959 | // Now that we found out the UID is actually active, cache that | 
|  | 3960 | mActiveUids.insert(uid); | 
|  | 3961 | } | 
|  | 3962 | } | 
|  | 3963 | return active; | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3964 | } | 
|  | 3965 |  | 
| Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 3966 | int32_t CameraService::UidPolicy::getProcState(uid_t uid) { | 
|  | 3967 | Mutex::Autolock _l(mUidLock); | 
|  | 3968 | return getProcStateLocked(uid); | 
|  | 3969 | } | 
|  | 3970 |  | 
|  | 3971 | int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) { | 
|  | 3972 | int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN; | 
|  | 3973 | if (mMonitoredUids.find(uid) != mMonitoredUids.end()) { | 
| Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 3974 | procState = mMonitoredUids[uid].procState; | 
| Varun Shah | b42f1eb | 2019-04-16 14:45:13 -0700 | [diff] [blame] | 3975 | } | 
|  | 3976 | return procState; | 
|  | 3977 | } | 
|  | 3978 |  | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3979 | void CameraService::UidPolicy::UidPolicy::addOverrideUid(uid_t uid, | 
|  | 3980 | String16 callingPackage, bool active) { | 
|  | 3981 | updateOverrideUid(uid, callingPackage, active, true); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3982 | } | 
|  | 3983 |  | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3984 | void CameraService::UidPolicy::removeOverrideUid(uid_t uid, String16 callingPackage) { | 
|  | 3985 | updateOverrideUid(uid, callingPackage, false, false); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3986 | } | 
|  | 3987 |  | 
| Eino-Ville Talvala | 8abec3f | 2018-03-20 11:07:00 -0700 | [diff] [blame] | 3988 | void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) { | 
|  | 3989 | Mutex::Autolock _l(mUidLock); | 
|  | 3990 | ALOGV("UidPolicy: ActivityManager has died"); | 
|  | 3991 | mRegistered = false; | 
|  | 3992 | mActiveUids.clear(); | 
|  | 3993 | } | 
|  | 3994 |  | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 3995 | void CameraService::UidPolicy::updateOverrideUid(uid_t uid, String16 callingPackage, | 
|  | 3996 | bool active, bool insert) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 3997 | bool wasActive = false; | 
|  | 3998 | bool isActive = false; | 
|  | 3999 | { | 
|  | 4000 | Mutex::Autolock _l(mUidLock); | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4001 | wasActive = isUidActiveLocked(uid, callingPackage); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4002 | mOverrideUids.erase(uid); | 
|  | 4003 | if (insert) { | 
|  | 4004 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); | 
|  | 4005 | } | 
| Svet Ganov | 7b4ab78 | 2018-03-25 12:48:10 -0700 | [diff] [blame] | 4006 | isActive = isUidActiveLocked(uid, callingPackage); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4007 | } | 
|  | 4008 | if (wasActive != isActive && !isActive) { | 
|  | 4009 | sp<CameraService> service = mService.promote(); | 
|  | 4010 | if (service != nullptr) { | 
|  | 4011 | service->blockClientsForUid(uid); | 
|  | 4012 | } | 
|  | 4013 | } | 
|  | 4014 | } | 
|  | 4015 |  | 
|  | 4016 | // ---------------------------------------------------------------------------- | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4017 | //                  SensorPrivacyPolicy | 
|  | 4018 | // ---------------------------------------------------------------------------- | 
|  | 4019 | void CameraService::SensorPrivacyPolicy::registerSelf() { | 
|  | 4020 | Mutex::Autolock _l(mSensorPrivacyLock); | 
|  | 4021 | if (mRegistered) { | 
|  | 4022 | return; | 
|  | 4023 | } | 
| Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4024 | hasCameraPrivacyFeature(); // Called so the result is cached | 
| Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4025 | mSpm.addSensorPrivacyListener(this); | 
|  | 4026 | mSensorPrivacyEnabled = mSpm.isSensorPrivacyEnabled(); | 
|  | 4027 | status_t res = mSpm.linkToDeath(this); | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4028 | if (res == OK) { | 
|  | 4029 | mRegistered = true; | 
|  | 4030 | ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager"); | 
|  | 4031 | } | 
|  | 4032 | } | 
|  | 4033 |  | 
|  | 4034 | void CameraService::SensorPrivacyPolicy::unregisterSelf() { | 
|  | 4035 | Mutex::Autolock _l(mSensorPrivacyLock); | 
| Steven Moreland | 3cf6717 | 2020-01-29 11:44:22 -0800 | [diff] [blame] | 4036 | mSpm.removeSensorPrivacyListener(this); | 
|  | 4037 | mSpm.unlinkToDeath(this); | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4038 | mRegistered = false; | 
|  | 4039 | ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager"); | 
|  | 4040 | } | 
|  | 4041 |  | 
|  | 4042 | bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { | 
|  | 4043 | Mutex::Autolock _l(mSensorPrivacyLock); | 
|  | 4044 | return mSensorPrivacyEnabled; | 
|  | 4045 | } | 
|  | 4046 |  | 
| Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4047 | bool CameraService::SensorPrivacyPolicy::isCameraPrivacyEnabled() { | 
| Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4048 | if (!hasCameraPrivacyFeature()) { | 
|  | 4049 | return false; | 
|  | 4050 | } | 
| Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4051 | return mSpm.isToggleSensorPrivacyEnabled(SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); | 
| Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4052 | } | 
|  | 4053 |  | 
| Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4054 | binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged( | 
|  | 4055 | int toggleType __unused, int sensor __unused, bool enabled) { | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4056 | { | 
|  | 4057 | Mutex::Autolock _l(mSensorPrivacyLock); | 
| Kwangkyu Park | e3af299 | 2022-11-05 02:46:52 +0900 | [diff] [blame] | 4058 | mSensorPrivacyEnabled = enabled; | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4059 | } | 
|  | 4060 | // 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] | 4061 | if (enabled) { | 
|  | 4062 | sp<CameraService> service = mService.promote(); | 
|  | 4063 | if (service != nullptr) { | 
|  | 4064 | service->blockAllClients(); | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4065 | } | 
|  | 4066 | } | 
|  | 4067 | return binder::Status::ok(); | 
|  | 4068 | } | 
|  | 4069 |  | 
|  | 4070 | void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) { | 
|  | 4071 | Mutex::Autolock _l(mSensorPrivacyLock); | 
|  | 4072 | ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died"); | 
|  | 4073 | mRegistered = false; | 
|  | 4074 | } | 
|  | 4075 |  | 
| Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4076 | bool CameraService::SensorPrivacyPolicy::hasCameraPrivacyFeature() { | 
| Evan Severson | d0b6992 | 2022-01-27 10:47:34 -0800 | [diff] [blame] | 4077 | bool supportsSoftwareToggle = mSpm.supportsSensorToggle( | 
|  | 4078 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); | 
|  | 4079 | bool supportsHardwareToggle = mSpm.supportsSensorToggle( | 
|  | 4080 | SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, SensorPrivacyManager::TOGGLE_SENSOR_CAMERA); | 
|  | 4081 | return supportsSoftwareToggle || supportsHardwareToggle; | 
| Evan Severson | 09ab400 | 2021-02-10 14:15:19 -0800 | [diff] [blame] | 4082 | } | 
|  | 4083 |  | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4084 | // ---------------------------------------------------------------------------- | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4085 | //                  CameraState | 
|  | 4086 | // ---------------------------------------------------------------------------- | 
|  | 4087 |  | 
|  | 4088 | CameraService::CameraState::CameraState(const String8& id, int cost, | 
| Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4089 | const std::set<String8>& conflicting, SystemCameraKind systemCameraKind, | 
|  | 4090 | const std::vector<std::string>& physicalCameras) : mId(id), | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4091 | mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting), | 
| Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4092 | mSystemCameraKind(systemCameraKind), mPhysicalCameras(physicalCameras) {} | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4093 |  | 
|  | 4094 | CameraService::CameraState::~CameraState() {} | 
|  | 4095 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4096 | CameraService::StatusInternal CameraService::CameraState::getStatus() const { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4097 | Mutex::Autolock lock(mStatusLock); | 
|  | 4098 | return mStatus; | 
|  | 4099 | } | 
|  | 4100 |  | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4101 | std::vector<String8> CameraService::CameraState::getUnavailablePhysicalIds() const { | 
|  | 4102 | Mutex::Autolock lock(mStatusLock); | 
|  | 4103 | std::vector<String8> res(mUnavailablePhysicalIds.begin(), mUnavailablePhysicalIds.end()); | 
|  | 4104 | return res; | 
|  | 4105 | } | 
|  | 4106 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4107 | CameraParameters CameraService::CameraState::getShimParams() const { | 
|  | 4108 | return mShimParams; | 
|  | 4109 | } | 
|  | 4110 |  | 
|  | 4111 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { | 
|  | 4112 | mShimParams = params; | 
|  | 4113 | } | 
|  | 4114 |  | 
|  | 4115 | int CameraService::CameraState::getCost() const { | 
|  | 4116 | return mCost; | 
|  | 4117 | } | 
|  | 4118 |  | 
|  | 4119 | std::set<String8> CameraService::CameraState::getConflicting() const { | 
|  | 4120 | return mConflicting; | 
|  | 4121 | } | 
|  | 4122 |  | 
|  | 4123 | String8 CameraService::CameraState::getId() const { | 
|  | 4124 | return mId; | 
|  | 4125 | } | 
|  | 4126 |  | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4127 | SystemCameraKind CameraService::CameraState::getSystemCameraKind() const { | 
|  | 4128 | return mSystemCameraKind; | 
|  | 4129 | } | 
|  | 4130 |  | 
| Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4131 | bool CameraService::CameraState::containsPhysicalCamera(const std::string& physicalCameraId) const { | 
|  | 4132 | return std::find(mPhysicalCameras.begin(), mPhysicalCameras.end(), physicalCameraId) | 
|  | 4133 | != mPhysicalCameras.end(); | 
|  | 4134 | } | 
|  | 4135 |  | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4136 | bool CameraService::CameraState::addUnavailablePhysicalId(const String8& physicalId) { | 
|  | 4137 | Mutex::Autolock lock(mStatusLock); | 
|  | 4138 | auto result = mUnavailablePhysicalIds.insert(physicalId); | 
|  | 4139 | return result.second; | 
|  | 4140 | } | 
|  | 4141 |  | 
|  | 4142 | bool CameraService::CameraState::removeUnavailablePhysicalId(const String8& physicalId) { | 
|  | 4143 | Mutex::Autolock lock(mStatusLock); | 
|  | 4144 | auto count = mUnavailablePhysicalIds.erase(physicalId); | 
|  | 4145 | return count > 0; | 
|  | 4146 | } | 
|  | 4147 |  | 
| Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 4148 | void CameraService::CameraState::setClientPackage(const String8& clientPackage) { | 
|  | 4149 | Mutex::Autolock lock(mStatusLock); | 
|  | 4150 | mClientPackage = clientPackage; | 
|  | 4151 | } | 
|  | 4152 |  | 
|  | 4153 | String8 CameraService::CameraState::getClientPackage() const { | 
|  | 4154 | Mutex::Autolock lock(mStatusLock); | 
|  | 4155 | return mClientPackage; | 
|  | 4156 | } | 
|  | 4157 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4158 | // ---------------------------------------------------------------------------- | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4159 | //                  ClientEventListener | 
|  | 4160 | // ---------------------------------------------------------------------------- | 
|  | 4161 |  | 
|  | 4162 | void CameraService::ClientEventListener::onClientAdded( | 
|  | 4163 | const resource_policy::ClientDescriptor<String8, | 
|  | 4164 | sp<CameraService::BasicClient>>& descriptor) { | 
| Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 4165 | const auto& basicClient = descriptor.getValue(); | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4166 | if (basicClient.get() != nullptr) { | 
|  | 4167 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); | 
|  | 4168 | notifier.noteStartCamera(descriptor.getKey(), | 
|  | 4169 | static_cast<int>(basicClient->getClientUid())); | 
|  | 4170 | } | 
|  | 4171 | } | 
|  | 4172 |  | 
|  | 4173 | void CameraService::ClientEventListener::onClientRemoved( | 
|  | 4174 | const resource_policy::ClientDescriptor<String8, | 
|  | 4175 | sp<CameraService::BasicClient>>& descriptor) { | 
| Chih-Hung Hsieh | 5404ee1 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 4176 | const auto& basicClient = descriptor.getValue(); | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4177 | if (basicClient.get() != nullptr) { | 
|  | 4178 | BatteryNotifier& notifier(BatteryNotifier::getInstance()); | 
|  | 4179 | notifier.noteStopCamera(descriptor.getKey(), | 
|  | 4180 | static_cast<int>(basicClient->getClientUid())); | 
|  | 4181 | } | 
|  | 4182 | } | 
|  | 4183 |  | 
|  | 4184 |  | 
|  | 4185 | // ---------------------------------------------------------------------------- | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4186 | //                  CameraClientManager | 
|  | 4187 | // ---------------------------------------------------------------------------- | 
|  | 4188 |  | 
| Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 4189 | CameraService::CameraClientManager::CameraClientManager() { | 
|  | 4190 | setListener(std::make_shared<ClientEventListener>()); | 
|  | 4191 | } | 
|  | 4192 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4193 | CameraService::CameraClientManager::~CameraClientManager() {} | 
|  | 4194 |  | 
|  | 4195 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( | 
|  | 4196 | const String8& id) const { | 
|  | 4197 | auto descriptor = get(id); | 
|  | 4198 | if (descriptor == nullptr) { | 
|  | 4199 | return sp<BasicClient>{nullptr}; | 
|  | 4200 | } | 
|  | 4201 | return descriptor->getValue(); | 
|  | 4202 | } | 
|  | 4203 |  | 
|  | 4204 | String8 CameraService::CameraClientManager::toString() const { | 
|  | 4205 | auto all = getAll(); | 
|  | 4206 | String8 ret("["); | 
|  | 4207 | bool hasAny = false; | 
|  | 4208 | for (auto& i : all) { | 
|  | 4209 | hasAny = true; | 
|  | 4210 | String8 key = i->getKey(); | 
|  | 4211 | int32_t cost = i->getCost(); | 
|  | 4212 | int32_t pid = i->getOwnerId(); | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4213 | int32_t score = i->getPriority().getScore(); | 
|  | 4214 | int32_t state = i->getPriority().getState(); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4215 | auto conflicting = i->getConflicting(); | 
|  | 4216 | auto clientSp = i->getValue(); | 
|  | 4217 | String8 packageName; | 
| Eino-Ville Talvala | 022f0cb | 2015-05-19 16:31:16 -0700 | [diff] [blame] | 4218 | userid_t clientUserId = 0; | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4219 | if (clientSp.get() != nullptr) { | 
|  | 4220 | packageName = String8{clientSp->getPackageName()}; | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4221 | uid_t clientUid = clientSp->getClientUid(); | 
|  | 4222 | clientUserId = multiuser_get_user_id(clientUid); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4223 | } | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4224 | ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %" | 
|  | 4225 | PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4226 |  | 
| Ruben Brunk | 6267b53 | 2015-04-30 17:44:07 -0700 | [diff] [blame] | 4227 | if (clientSp.get() != nullptr) { | 
|  | 4228 | ret.appendFormat("User Id: %d, ", clientUserId); | 
|  | 4229 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4230 | if (packageName.size() != 0) { | 
|  | 4231 | ret.appendFormat("Client Package Name: %s", packageName.string()); | 
|  | 4232 | } | 
|  | 4233 |  | 
|  | 4234 | ret.append(", Conflicting Client Devices: {"); | 
|  | 4235 | for (auto& j : conflicting) { | 
|  | 4236 | ret.appendFormat("%s, ", j.string()); | 
|  | 4237 | } | 
|  | 4238 | ret.append("})"); | 
|  | 4239 | } | 
|  | 4240 | if (hasAny) ret.append("\n"); | 
|  | 4241 | ret.append("]\n"); | 
|  | 4242 | return ret; | 
|  | 4243 | } | 
|  | 4244 |  | 
|  | 4245 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( | 
|  | 4246 | const String8& key, const sp<BasicClient>& value, int32_t cost, | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4247 | const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4248 | int32_t state, int32_t oomScoreOffset, bool systemNativeClient) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4249 |  | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4250 | int32_t score_adj = systemNativeClient ? kSystemNativeClientScore : score; | 
|  | 4251 | int32_t state_adj = systemNativeClient ? kSystemNativeClientState: state; | 
| Jayant Chowdhary | c578a50 | 2019-05-08 10:57:54 -0700 | [diff] [blame] | 4252 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4253 | return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>( | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4254 | key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, | 
|  | 4255 | systemNativeClient, oomScoreOffset); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4256 | } | 
|  | 4257 |  | 
|  | 4258 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( | 
| Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 4259 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4260 | int32_t oomScoreOffset, bool systemNativeClient) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4261 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), | 
| Emilian Peev | 8131a26 | 2017-02-01 12:33:43 +0000 | [diff] [blame] | 4262 | partial->getConflicting(), partial->getPriority().getScore(), | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 4263 | partial->getOwnerId(), partial->getPriority().getState(), oomScoreOffset, | 
|  | 4264 | systemNativeClient); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4265 | } | 
|  | 4266 |  | 
|  | 4267 | // ---------------------------------------------------------------------------- | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4268 | //                  InjectionStatusListener | 
|  | 4269 | // ---------------------------------------------------------------------------- | 
|  | 4270 |  | 
|  | 4271 | void CameraService::InjectionStatusListener::addListener( | 
|  | 4272 | const sp<ICameraInjectionCallback>& callback) { | 
|  | 4273 | Mutex::Autolock lock(mListenerLock); | 
|  | 4274 | if (mCameraInjectionCallback) return; | 
|  | 4275 | status_t res = IInterface::asBinder(callback)->linkToDeath(this); | 
|  | 4276 | if (res == OK) { | 
|  | 4277 | mCameraInjectionCallback = callback; | 
|  | 4278 | } | 
|  | 4279 | } | 
|  | 4280 |  | 
|  | 4281 | void CameraService::InjectionStatusListener::removeListener() { | 
|  | 4282 | Mutex::Autolock lock(mListenerLock); | 
|  | 4283 | if (mCameraInjectionCallback == nullptr) { | 
|  | 4284 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); | 
|  | 4285 | return; | 
|  | 4286 | } | 
|  | 4287 | IInterface::asBinder(mCameraInjectionCallback)->unlinkToDeath(this); | 
|  | 4288 | mCameraInjectionCallback = nullptr; | 
|  | 4289 | } | 
|  | 4290 |  | 
|  | 4291 | void CameraService::InjectionStatusListener::notifyInjectionError( | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4292 | String8 injectedCamId, status_t err) { | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4293 | if (mCameraInjectionCallback == nullptr) { | 
|  | 4294 | ALOGW("InjectionStatusListener: mCameraInjectionCallback == nullptr"); | 
|  | 4295 | return; | 
|  | 4296 | } | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4297 |  | 
|  | 4298 | switch (err) { | 
|  | 4299 | case -ENODEV: | 
|  | 4300 | mCameraInjectionCallback->onInjectionError( | 
|  | 4301 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); | 
|  | 4302 | ALOGE("No camera device with ID \"%s\" currently available!", | 
|  | 4303 | injectedCamId.string()); | 
|  | 4304 | break; | 
|  | 4305 | case -EBUSY: | 
|  | 4306 | mCameraInjectionCallback->onInjectionError( | 
|  | 4307 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); | 
|  | 4308 | ALOGE("Higher-priority client using camera, ID \"%s\" currently unavailable!", | 
|  | 4309 | injectedCamId.string()); | 
|  | 4310 | break; | 
|  | 4311 | case DEAD_OBJECT: | 
|  | 4312 | mCameraInjectionCallback->onInjectionError( | 
|  | 4313 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); | 
|  | 4314 | ALOGE("Camera ID \"%s\" object is dead!", | 
|  | 4315 | injectedCamId.string()); | 
|  | 4316 | break; | 
|  | 4317 | case INVALID_OPERATION: | 
|  | 4318 | mCameraInjectionCallback->onInjectionError( | 
|  | 4319 | ICameraInjectionCallback::ERROR_INJECTION_SESSION); | 
|  | 4320 | ALOGE("Camera ID \"%s\" encountered an operating or internal error!", | 
|  | 4321 | injectedCamId.string()); | 
|  | 4322 | break; | 
|  | 4323 | case UNKNOWN_TRANSACTION: | 
|  | 4324 | mCameraInjectionCallback->onInjectionError( | 
|  | 4325 | ICameraInjectionCallback::ERROR_INJECTION_UNSUPPORTED); | 
|  | 4326 | ALOGE("Camera ID \"%s\" method doesn't support!", | 
|  | 4327 | injectedCamId.string()); | 
|  | 4328 | break; | 
|  | 4329 | default: | 
|  | 4330 | mCameraInjectionCallback->onInjectionError( | 
|  | 4331 | ICameraInjectionCallback::ERROR_INJECTION_INVALID_ERROR); | 
|  | 4332 | ALOGE("Unexpected error %s (%d) opening camera \"%s\"!", | 
|  | 4333 | strerror(-err), err, injectedCamId.string()); | 
|  | 4334 | } | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4335 | } | 
|  | 4336 |  | 
|  | 4337 | void CameraService::InjectionStatusListener::binderDied( | 
|  | 4338 | const wp<IBinder>& /*who*/) { | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4339 | ALOGV("InjectionStatusListener: ICameraInjectionCallback has died"); | 
|  | 4340 | auto parent = mParent.promote(); | 
|  | 4341 | if (parent != nullptr) { | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4342 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); | 
|  | 4343 | if (clientDescriptor != nullptr) { | 
|  | 4344 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); | 
|  | 4345 | baseClientPtr->stopInjection(); | 
|  | 4346 | } | 
| Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 4347 | parent->clearInjectionParameters(); | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4348 | } | 
|  | 4349 | } | 
|  | 4350 |  | 
|  | 4351 | // ---------------------------------------------------------------------------- | 
|  | 4352 | //                  CameraInjectionSession | 
|  | 4353 | // ---------------------------------------------------------------------------- | 
|  | 4354 |  | 
|  | 4355 | binder::Status CameraService::CameraInjectionSession::stopInjection() { | 
|  | 4356 | Mutex::Autolock lock(mInjectionSessionLock); | 
|  | 4357 | auto parent = mParent.promote(); | 
|  | 4358 | if (parent == nullptr) { | 
|  | 4359 | ALOGE("CameraInjectionSession: Parent is gone"); | 
|  | 4360 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SERVICE, | 
|  | 4361 | "Camera service encountered error"); | 
|  | 4362 | } | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4363 |  | 
|  | 4364 | status_t res = NO_ERROR; | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 4365 | auto clientDescriptor = parent->mActiveClientManager.get(parent->mInjectionInternalCamId); | 
|  | 4366 | if (clientDescriptor != nullptr) { | 
|  | 4367 | BasicClient* baseClientPtr = clientDescriptor->getValue().get(); | 
|  | 4368 | res = baseClientPtr->stopInjection(); | 
|  | 4369 | if (res != OK) { | 
|  | 4370 | ALOGE("CameraInjectionSession: Failed to stop the injection camera!" | 
|  | 4371 | " ret != NO_ERROR: %d", res); | 
|  | 4372 | return STATUS_ERROR(ICameraInjectionCallback::ERROR_INJECTION_SESSION, | 
|  | 4373 | "Camera session encountered error"); | 
|  | 4374 | } | 
|  | 4375 | } | 
| Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 4376 | parent->clearInjectionParameters(); | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 4377 | return binder::Status::ok(); | 
|  | 4378 | } | 
|  | 4379 |  | 
|  | 4380 | // ---------------------------------------------------------------------------- | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4381 |  | 
|  | 4382 | static const int kDumpLockRetries = 50; | 
|  | 4383 | static const int kDumpLockSleep = 60000; | 
|  | 4384 |  | 
|  | 4385 | static bool tryLock(Mutex& mutex) | 
|  | 4386 | { | 
|  | 4387 | bool locked = false; | 
|  | 4388 | for (int i = 0; i < kDumpLockRetries; ++i) { | 
|  | 4389 | if (mutex.tryLock() == NO_ERROR) { | 
|  | 4390 | locked = true; | 
|  | 4391 | break; | 
|  | 4392 | } | 
|  | 4393 | usleep(kDumpLockSleep); | 
|  | 4394 | } | 
|  | 4395 | return locked; | 
|  | 4396 | } | 
|  | 4397 |  | 
| Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4398 | void CameraService::cacheDump() { | 
|  | 4399 | if (mMemFd != -1) { | 
|  | 4400 | const Vector<String16> args; | 
|  | 4401 | ATRACE_CALL(); | 
|  | 4402 | // Acquiring service lock here will avoid the deadlock since | 
|  | 4403 | // cacheDump will not be called during the second disconnect. | 
|  | 4404 | Mutex::Autolock lock(mServiceLock); | 
|  | 4405 |  | 
|  | 4406 | Mutex::Autolock l(mCameraStatesLock); | 
|  | 4407 | // Start collecting the info for open sessions and store it in temp file. | 
|  | 4408 | for (const auto& state : mCameraStates) { | 
|  | 4409 | String8 cameraId = state.first; | 
|  | 4410 | auto clientDescriptor = mActiveClientManager.get(cameraId); | 
|  | 4411 | if (clientDescriptor != nullptr) { | 
|  | 4412 | dprintf(mMemFd, "== Camera device %s dynamic info: ==\n", cameraId.string()); | 
|  | 4413 | // Log the current open session info before device is disconnected. | 
|  | 4414 | dumpOpenSessionClientLogs(mMemFd, args, cameraId); | 
|  | 4415 | } | 
|  | 4416 | } | 
|  | 4417 | } | 
|  | 4418 | } | 
|  | 4419 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4420 | status_t CameraService::dump(int fd, const Vector<String16>& args) { | 
| Eino-Ville Talvala | a84bbe6 | 2015-09-08 17:59:17 -0700 | [diff] [blame] | 4421 | ATRACE_CALL(); | 
|  | 4422 |  | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4423 | if (checkCallingPermission(sDumpPermission) == false) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4424 | 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] | 4425 | CameraThreadState::getCallingPid(), | 
|  | 4426 | CameraThreadState::getCallingUid()); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4427 | return NO_ERROR; | 
|  | 4428 | } | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4429 | bool locked = tryLock(mServiceLock); | 
|  | 4430 | // failed to lock - CameraService is probably deadlocked | 
|  | 4431 | if (!locked) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4432 | dprintf(fd, "!! CameraService may be deadlocked !!\n"); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4433 | } | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4434 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4435 | if (!mInitialized) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4436 | dprintf(fd, "!! No camera HAL available !!\n"); | 
| Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 4437 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4438 | // Dump event log for error information | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4439 | dumpEventLog(fd); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4440 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4441 | if (locked) mServiceLock.unlock(); | 
|  | 4442 | return NO_ERROR; | 
|  | 4443 | } | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4444 | dprintf(fd, "\n== Service global info: ==\n\n"); | 
|  | 4445 | dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4446 | dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size()); | 
| Jayant Chowdhary | 847947d | 2019-08-30 18:02:59 -0700 | [diff] [blame] | 4447 | dprintf(fd, "Number of public camera devices visible to API1: %zu\n", | 
|  | 4448 | mNormalDeviceIdsWithoutSystemCamera.size()); | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 4449 | for (size_t i = 0; i < mNormalDeviceIds.size(); i++) { | 
|  | 4450 | dprintf(fd, "    Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str()); | 
|  | 4451 | } | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4452 | String8 activeClientString = mActiveClientManager.toString(); | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4453 | dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string()); | 
|  | 4454 | dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string()); | 
| Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 4455 | if (mStreamUseCaseOverrides.size() > 0) { | 
|  | 4456 | dprintf(fd, "Active stream use case overrides:"); | 
|  | 4457 | for (int64_t useCaseOverride : mStreamUseCaseOverrides) { | 
|  | 4458 | dprintf(fd, " %" PRId64, useCaseOverride); | 
|  | 4459 | } | 
|  | 4460 | dprintf(fd, "\n"); | 
|  | 4461 | } | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4462 |  | 
|  | 4463 | dumpEventLog(fd); | 
|  | 4464 |  | 
|  | 4465 | bool stateLocked = tryLock(mCameraStatesLock); | 
|  | 4466 | if (!stateLocked) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4467 | dprintf(fd, "CameraStates in use, may be deadlocked\n"); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4468 | } | 
|  | 4469 |  | 
| Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 4470 | int argSize = args.size(); | 
|  | 4471 | for (int i = 0; i < argSize; i++) { | 
|  | 4472 | if (args[i] == TagMonitor::kMonitorOption) { | 
|  | 4473 | if (i + 1 < argSize) { | 
|  | 4474 | mMonitorTags = String8(args[i + 1]); | 
|  | 4475 | } | 
|  | 4476 | break; | 
|  | 4477 | } | 
|  | 4478 | } | 
|  | 4479 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4480 | for (auto& state : mCameraStates) { | 
|  | 4481 | String8 cameraId = state.first; | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4482 |  | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4483 | dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string()); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4484 |  | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4485 | CameraParameters p = state.second->getShimParams(); | 
|  | 4486 | if (!p.isEmpty()) { | 
|  | 4487 | dprintf(fd, "  Camera1 API shim is using parameters:\n        "); | 
|  | 4488 | p.dump(fd, args); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4489 | } | 
|  | 4490 |  | 
|  | 4491 | auto clientDescriptor = mActiveClientManager.get(cameraId); | 
| Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4492 | if (clientDescriptor != nullptr) { | 
| Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4493 | // log the current open session info | 
|  | 4494 | dumpOpenSessionClientLogs(fd, args, cameraId); | 
| Zhijun He | 2f140ed | 2017-02-08 09:57:23 -0800 | [diff] [blame] | 4495 | } else { | 
| Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4496 | dumpClosedSessionClientLogs(fd, cameraId); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4497 | } | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4498 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4499 | } | 
|  | 4500 |  | 
|  | 4501 | if (stateLocked) mCameraStatesLock.unlock(); | 
|  | 4502 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4503 | if (locked) mServiceLock.unlock(); | 
|  | 4504 |  | 
| Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 4505 | mCameraProviderManager->dump(fd, args); | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4506 |  | 
|  | 4507 | dprintf(fd, "\n== Vendor tags: ==\n\n"); | 
|  | 4508 |  | 
|  | 4509 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); | 
|  | 4510 | if (desc == NULL) { | 
| Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 4511 | sp<VendorTagDescriptorCache> cache = | 
|  | 4512 | VendorTagDescriptorCache::getGlobalVendorTagCache(); | 
|  | 4513 | if (cache == NULL) { | 
|  | 4514 | dprintf(fd, "No vendor tags.\n"); | 
|  | 4515 | } else { | 
|  | 4516 | cache->dump(fd, /*verbosity*/2, /*indentation*/2); | 
|  | 4517 | } | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4518 | } else { | 
|  | 4519 | desc->dump(fd, /*verbosity*/2, /*indentation*/2); | 
|  | 4520 | } | 
|  | 4521 |  | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4522 | // Dump camera traces if there were any | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4523 | dprintf(fd, "\n"); | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4524 | camera3::CameraTraces::dump(fd); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4525 |  | 
|  | 4526 | // Process dump arguments, if any | 
|  | 4527 | int n = args.size(); | 
|  | 4528 | String16 verboseOption("-v"); | 
|  | 4529 | String16 unreachableOption("--unreachable"); | 
|  | 4530 | for (int i = 0; i < n; i++) { | 
|  | 4531 | if (args[i] == verboseOption) { | 
|  | 4532 | // change logging level | 
|  | 4533 | if (i + 1 >= n) continue; | 
|  | 4534 | String8 levelStr(args[i+1]); | 
|  | 4535 | int level = atoi(levelStr.string()); | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4536 | dprintf(fd, "\nSetting log level to %d.\n", level); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4537 | setLogLevel(level); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4538 | } else if (args[i] == unreachableOption) { | 
|  | 4539 | // Dump memory analysis | 
|  | 4540 | // TODO - should limit be an argument parameter? | 
|  | 4541 | UnreachableMemoryInfo info; | 
|  | 4542 | bool success = GetUnreachableMemory(info, /*limit*/ 10000); | 
|  | 4543 | if (!success) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4544 | dprintf(fd, "\n== Unable to dump unreachable memory. " | 
|  | 4545 | "Try disabling SELinux enforcement. ==\n"); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4546 | } else { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4547 | dprintf(fd, "\n== Dumping unreachable memory: ==\n"); | 
| Eino-Ville Talvala | 8131418 | 2017-01-30 16:13:45 -0800 | [diff] [blame] | 4548 | std::string s = info.ToString(/*log_contents*/ true); | 
|  | 4549 | write(fd, s.c_str(), s.size()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4550 | } | 
|  | 4551 | } | 
|  | 4552 | } | 
| Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4553 |  | 
|  | 4554 | bool serviceLocked = tryLock(mServiceLock); | 
|  | 4555 |  | 
|  | 4556 | // Dump info from previous open sessions. | 
|  | 4557 | // Reposition the offset to beginning of the file before reading | 
|  | 4558 |  | 
|  | 4559 | if ((mMemFd >= 0) && (lseek(mMemFd, 0, SEEK_SET) != -1)) { | 
|  | 4560 | dprintf(fd, "\n**********Dumpsys from previous open session**********\n"); | 
|  | 4561 | ssize_t size_read; | 
|  | 4562 | char buf[4096]; | 
|  | 4563 | while ((size_read = read(mMemFd, buf, (sizeof(buf) - 1))) > 0) { | 
|  | 4564 | // Read data from file to a small buffer and write it to fd. | 
|  | 4565 | write(fd, buf, size_read); | 
|  | 4566 | if (size_read == -1) { | 
|  | 4567 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); | 
|  | 4568 | break; | 
|  | 4569 | } | 
|  | 4570 | } | 
|  | 4571 | dprintf(fd, "\n**********End of Dumpsys from previous open session**********\n"); | 
|  | 4572 | } else { | 
|  | 4573 | ALOGE("%s: Error during reading the file: %s", __FUNCTION__, sFileName); | 
|  | 4574 | } | 
|  | 4575 |  | 
|  | 4576 | if (serviceLocked) mServiceLock.unlock(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4577 | return NO_ERROR; | 
|  | 4578 | } | 
|  | 4579 |  | 
| Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4580 | void CameraService::dumpOpenSessionClientLogs(int fd, | 
|  | 4581 | const Vector<String16>& args, const String8& cameraId) { | 
|  | 4582 | auto clientDescriptor = mActiveClientManager.get(cameraId); | 
| Rucha Katakwar | 71a0e05 | 2022-04-07 15:44:18 -0700 | [diff] [blame] | 4583 | dprintf(fd, "  %s : Device %s is open. Client instance dump:\n", | 
|  | 4584 | getFormattedCurrentTime().string(), | 
|  | 4585 | cameraId.string()); | 
| Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 4586 | dprintf(fd, "    Client priority score: %d state: %d\n", | 
|  | 4587 | clientDescriptor->getPriority().getScore(), | 
|  | 4588 | clientDescriptor->getPriority().getState()); | 
|  | 4589 | dprintf(fd, "    Client PID: %d\n", clientDescriptor->getOwnerId()); | 
|  | 4590 |  | 
|  | 4591 | auto client = clientDescriptor->getValue(); | 
|  | 4592 | dprintf(fd, "    Client package: %s\n", | 
|  | 4593 | String8(client->getPackageName()).string()); | 
|  | 4594 |  | 
|  | 4595 | client->dumpClient(fd, args); | 
|  | 4596 | } | 
|  | 4597 |  | 
|  | 4598 | void CameraService::dumpClosedSessionClientLogs(int fd, const String8& cameraId) { | 
|  | 4599 | dprintf(fd, "  Device %s is closed, no client instance\n", | 
|  | 4600 | cameraId.string()); | 
|  | 4601 | } | 
|  | 4602 |  | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4603 | void CameraService::dumpEventLog(int fd) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4604 | 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] | 4605 |  | 
|  | 4606 | Mutex::Autolock l(mLogLock); | 
|  | 4607 | for (const auto& msg : mEventLog) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4608 | dprintf(fd, "  %s\n", msg.string()); | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4609 | } | 
|  | 4610 |  | 
|  | 4611 | if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4612 | dprintf(fd, "  ...\n"); | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4613 | } else if (mEventLog.size() == 0) { | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4614 | dprintf(fd, "  [no events yet]\n"); | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4615 | } | 
| Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 4616 | dprintf(fd, "\n"); | 
| Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 4617 | } | 
|  | 4618 |  | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4619 | void CameraService::cacheClientTagDumpIfNeeded(const char *cameraId, BasicClient* client) { | 
|  | 4620 | Mutex::Autolock lock(mLogLock); | 
|  | 4621 | if (!isClientWatchedLocked(client)) { return; } | 
|  | 4622 |  | 
|  | 4623 | std::vector<std::string> dumpVector; | 
|  | 4624 | client->dumpWatchedEventsToVector(dumpVector); | 
|  | 4625 |  | 
|  | 4626 | if (dumpVector.empty()) { return; } | 
|  | 4627 |  | 
| Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 4628 | std::string dumpString; | 
|  | 4629 |  | 
|  | 4630 | String8 currentTime = getFormattedCurrentTime(); | 
|  | 4631 | dumpString += "Cached @ "; | 
|  | 4632 | dumpString += currentTime.string(); | 
|  | 4633 | dumpString += "\n"; // First line is the timestamp of when client is cached. | 
|  | 4634 |  | 
|  | 4635 |  | 
| Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4636 | const String16 &packageName = client->getPackageName(); | 
|  | 4637 |  | 
|  | 4638 | String8 packageName8 = String8(packageName); | 
|  | 4639 | const char *printablePackageName = packageName8.lockBuffer(packageName.size()); | 
|  | 4640 |  | 
| Avichal Rakesh | 882c08b | 2021-12-09 17:47:07 -0800 | [diff] [blame] | 4641 |  | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4642 | size_t i = dumpVector.size(); | 
|  | 4643 |  | 
|  | 4644 | // Store the string in reverse order (latest last) | 
|  | 4645 | while (i > 0) { | 
|  | 4646 | i--; | 
|  | 4647 | dumpString += cameraId; | 
|  | 4648 | dumpString += ":"; | 
| Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4649 | dumpString += printablePackageName; | 
|  | 4650 | dumpString += "  "; | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4651 | dumpString += dumpVector[i]; // implicitly ends with '\n' | 
|  | 4652 | } | 
|  | 4653 |  | 
| Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 4654 | packageName8.unlockBuffer(); | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 4655 | mWatchedClientsDumpCache[packageName] = dumpString; | 
|  | 4656 | } | 
|  | 4657 |  | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4658 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4659 | Mutex::Autolock al(mTorchClientMapMutex); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4660 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { | 
|  | 4661 | if (mTorchClientMap[i] == who) { | 
|  | 4662 | // 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] | 4663 | String8 cameraId = mTorchClientMap.keyAt(i); | 
|  | 4664 | status_t res = mFlashlight->setTorchMode(cameraId, false); | 
|  | 4665 | if (res) { | 
|  | 4666 | ALOGE("%s: torch client died but couldn't turn off torch: " | 
|  | 4667 | "%s (%d)", __FUNCTION__, strerror(-res), res); | 
|  | 4668 | return; | 
|  | 4669 | } | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4670 | mTorchClientMap.removeItemsAt(i); | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4671 | break; | 
|  | 4672 | } | 
|  | 4673 | } | 
|  | 4674 | } | 
|  | 4675 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4676 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { | 
| Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 4677 |  | 
| Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 4678 | /** | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4679 | * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the | 
|  | 4680 | * binder driver | 
| Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 4681 | */ | 
| Yin-Chia Yeh | dbfcb38 | 2018-02-16 11:17:36 -0800 | [diff] [blame] | 4682 | // PID here is approximate and can be wrong. | 
| Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 4683 | logClientDied(CameraThreadState::getCallingPid(), String8("Binder died unexpectedly")); | 
| Ruben Brunk | a8ca915 | 2015-04-07 14:23:40 -0700 | [diff] [blame] | 4684 |  | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4685 | // check torch client | 
|  | 4686 | handleTorchClientBinderDied(who); | 
|  | 4687 |  | 
|  | 4688 | // check camera device client | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4689 | if(!evictClientIdByRemote(who)) { | 
|  | 4690 | 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] | 4691 | return; | 
|  | 4692 | } | 
|  | 4693 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4694 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", | 
|  | 4695 | __FUNCTION__); | 
| Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 4696 | } | 
|  | 4697 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4698 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4699 | updateStatus(status, cameraId, {}); | 
| Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 4700 | } | 
|  | 4701 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4702 | void CameraService::updateStatus(StatusInternal status, const String8& cameraId, | 
|  | 4703 | std::initializer_list<StatusInternal> rejectSourceStates) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4704 | // Do not lock mServiceLock here or can get into a deadlock from | 
|  | 4705 | // connect() -> disconnect -> updateStatus | 
|  | 4706 |  | 
|  | 4707 | auto state = getCameraState(cameraId); | 
|  | 4708 |  | 
|  | 4709 | if (state == nullptr) { | 
|  | 4710 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, | 
|  | 4711 | cameraId.string()); | 
|  | 4712 | return; | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4713 | } | 
|  | 4714 |  | 
| Jayant Chowdhary | 33e8ef8 | 2019-09-27 09:20:42 -0700 | [diff] [blame] | 4715 | // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275) | 
|  | 4716 | SystemCameraKind deviceKind = SystemCameraKind::PUBLIC; | 
|  | 4717 | if (getSystemCameraKind(cameraId, &deviceKind) != OK) { | 
|  | 4718 | ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string()); | 
|  | 4719 | return; | 
|  | 4720 | } | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4721 |  | 
|  | 4722 | // Collect the logical cameras without holding mStatusLock in updateStatus | 
|  | 4723 | // as that can lead to a deadlock(b/162192331). | 
|  | 4724 | auto logicalCameraIds = getLogicalCameras(cameraId); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4725 | // 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] | 4726 | // of the listeners with both the mStatusLock and mStatusListenerLock held | 
| Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 4727 | state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4728 | &logicalCameraIds] | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4729 | (const String8& cameraId, StatusInternal status) { | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4730 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4731 | if (status != StatusInternal::ENUMERATING) { | 
| Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4732 | // Update torch status if it has a flash unit. | 
|  | 4733 | Mutex::Autolock al(mTorchStatusMutex); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4734 | TorchModeStatus torchStatus; | 
| Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4735 | if (getTorchStatusLocked(cameraId, &torchStatus) != | 
|  | 4736 | NAME_NOT_FOUND) { | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4737 | TorchModeStatus newTorchStatus = | 
|  | 4738 | status == StatusInternal::PRESENT ? | 
|  | 4739 | TorchModeStatus::AVAILABLE_OFF : | 
|  | 4740 | TorchModeStatus::NOT_AVAILABLE; | 
| Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4741 | if (torchStatus != newTorchStatus) { | 
| Jayant Chowdhary | 8c62d89 | 2021-03-31 02:13:46 -0700 | [diff] [blame] | 4742 | onTorchStatusChangedLocked(cameraId, newTorchStatus, deviceKind); | 
| Chien-Yu Chen | f6463fc | 2015-04-07 15:11:31 -0700 | [diff] [blame] | 4743 | } | 
|  | 4744 | } | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4745 | } | 
|  | 4746 |  | 
|  | 4747 | Mutex::Autolock lock(mStatusListenerLock); | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4748 | notifyPhysicalCameraStatusLocked(mapToInterface(status), String16(cameraId), | 
|  | 4749 | logicalCameraIds, deviceKind); | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4750 |  | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4751 | for (auto& listener : mListenerList) { | 
| Jayant Chowdhary | 90e6369 | 2019-10-25 14:13:01 -0700 | [diff] [blame] | 4752 | bool isVendorListener = listener->isVendorListener(); | 
|  | 4753 | if (shouldSkipStatusUpdates(deviceKind, isVendorListener, | 
|  | 4754 | listener->getListenerPid(), listener->getListenerUid()) || | 
| Shuzhen Wang | b825979 | 2021-05-27 09:27:06 -0700 | [diff] [blame] | 4755 | isVendorListener) { | 
|  | 4756 | ALOGV("Skipping discovery callback for system-only camera device %s", | 
| Jayant Chowdhary | 5216b21 | 2019-07-17 09:26:23 -0700 | [diff] [blame] | 4757 | cameraId.c_str()); | 
| Jayant Chowdhary | f949ddd | 2019-01-29 14:34:11 -0800 | [diff] [blame] | 4758 | continue; | 
|  | 4759 | } | 
| Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4760 | auto ret = listener->getListener()->onStatusChanged(mapToInterface(status), | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 4761 | String16(cameraId)); | 
| Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4762 | listener->handleBinderStatus(ret, | 
|  | 4763 | "%s: Failed to trigger onStatusChanged callback for %d:%d: %d", | 
|  | 4764 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), | 
|  | 4765 | ret.exceptionCode()); | 
| Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 4766 | } | 
|  | 4767 | }); | 
| Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 4768 | } | 
|  | 4769 |  | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4770 | void CameraService::updateOpenCloseStatus(const String8& cameraId, bool open, | 
|  | 4771 | const String16& clientPackageName) { | 
| Shuzhen Wang | e7aa034 | 2021-08-03 11:29:47 -0700 | [diff] [blame] | 4772 | auto state = getCameraState(cameraId); | 
|  | 4773 | if (state == nullptr) { | 
|  | 4774 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, | 
|  | 4775 | cameraId.string()); | 
|  | 4776 | return; | 
|  | 4777 | } | 
|  | 4778 | if (open) { | 
|  | 4779 | state->setClientPackage(String8(clientPackageName)); | 
|  | 4780 | } else { | 
|  | 4781 | state->setClientPackage(String8::empty()); | 
|  | 4782 | } | 
|  | 4783 |  | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4784 | Mutex::Autolock lock(mStatusListenerLock); | 
|  | 4785 |  | 
|  | 4786 | for (const auto& it : mListenerList) { | 
|  | 4787 | if (!it->isOpenCloseCallbackAllowed()) { | 
|  | 4788 | continue; | 
|  | 4789 | } | 
|  | 4790 |  | 
|  | 4791 | binder::Status ret; | 
|  | 4792 | String16 cameraId64(cameraId); | 
|  | 4793 | if (open) { | 
|  | 4794 | ret = it->getListener()->onCameraOpened(cameraId64, clientPackageName); | 
|  | 4795 | } else { | 
|  | 4796 | ret = it->getListener()->onCameraClosed(cameraId64); | 
|  | 4797 | } | 
| Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4798 |  | 
|  | 4799 | it->handleBinderStatus(ret, | 
|  | 4800 | "%s: Failed to trigger onCameraOpened/onCameraClosed callback for %d:%d: %d", | 
|  | 4801 | __FUNCTION__, it->getListenerUid(), it->getListenerPid(), ret.exceptionCode()); | 
| Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 4802 | } | 
|  | 4803 | } | 
|  | 4804 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4805 | template<class Func> | 
|  | 4806 | void CameraService::CameraState::updateStatus(StatusInternal status, | 
|  | 4807 | const String8& cameraId, | 
|  | 4808 | std::initializer_list<StatusInternal> rejectSourceStates, | 
|  | 4809 | Func onStatusUpdatedLocked) { | 
|  | 4810 | Mutex::Autolock lock(mStatusLock); | 
|  | 4811 | StatusInternal oldStatus = mStatus; | 
|  | 4812 | mStatus = status; | 
|  | 4813 |  | 
|  | 4814 | if (oldStatus == status) { | 
|  | 4815 | return; | 
|  | 4816 | } | 
|  | 4817 |  | 
|  | 4818 | ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__, | 
|  | 4819 | cameraId.string(), oldStatus, status); | 
|  | 4820 |  | 
|  | 4821 | if (oldStatus == StatusInternal::NOT_PRESENT && | 
|  | 4822 | (status != StatusInternal::PRESENT && | 
|  | 4823 | status != StatusInternal::ENUMERATING)) { | 
|  | 4824 |  | 
|  | 4825 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING", | 
|  | 4826 | __FUNCTION__); | 
|  | 4827 | mStatus = oldStatus; | 
|  | 4828 | return; | 
|  | 4829 | } | 
|  | 4830 |  | 
|  | 4831 | /** | 
|  | 4832 | * Sometimes we want to conditionally do a transition. | 
|  | 4833 | * For example if a client disconnects, we want to go to PRESENT | 
|  | 4834 | * only if we weren't already in NOT_PRESENT or ENUMERATING. | 
|  | 4835 | */ | 
|  | 4836 | for (auto& rejectStatus : rejectSourceStates) { | 
|  | 4837 | if (oldStatus == rejectStatus) { | 
|  | 4838 | ALOGV("%s: Rejecting status transition for Camera ID %s,  since the source " | 
|  | 4839 | "state was was in one of the bad states.", __FUNCTION__, cameraId.string()); | 
|  | 4840 | mStatus = oldStatus; | 
|  | 4841 | return; | 
|  | 4842 | } | 
|  | 4843 | } | 
|  | 4844 |  | 
|  | 4845 | onStatusUpdatedLocked(cameraId, status); | 
|  | 4846 | } | 
|  | 4847 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4848 | status_t CameraService::getTorchStatusLocked( | 
|  | 4849 | const String8& cameraId, | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4850 | TorchModeStatus *status) const { | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4851 | if (!status) { | 
|  | 4852 | return BAD_VALUE; | 
|  | 4853 | } | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4854 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); | 
|  | 4855 | if (index == NAME_NOT_FOUND) { | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4856 | // invalid camera ID or the camera doesn't have a flash unit | 
|  | 4857 | return NAME_NOT_FOUND; | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4858 | } | 
|  | 4859 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4860 | *status = mTorchStatusMap.valueAt(index); | 
|  | 4861 | return OK; | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4862 | } | 
|  | 4863 |  | 
| Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 4864 | status_t CameraService::setTorchStatusLocked(const String8& cameraId, | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4865 | TorchModeStatus status) { | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4866 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); | 
|  | 4867 | if (index == NAME_NOT_FOUND) { | 
|  | 4868 | return BAD_VALUE; | 
|  | 4869 | } | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 4870 | mTorchStatusMap.editValueAt(index) = status; | 
| Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 4871 |  | 
|  | 4872 | return OK; | 
|  | 4873 | } | 
|  | 4874 |  | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4875 | std::list<String16> CameraService::getLogicalCameras( | 
|  | 4876 | const String8& physicalCameraId) { | 
|  | 4877 | std::list<String16> retList; | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4878 | Mutex::Autolock lock(mCameraStatesLock); | 
|  | 4879 | for (const auto& state : mCameraStates) { | 
| Shuzhen Wang | 403af6d | 2021-12-21 00:08:43 +0000 | [diff] [blame] | 4880 | if (state.second->containsPhysicalCamera(physicalCameraId.c_str())) { | 
|  | 4881 | retList.emplace_back(String16(state.first)); | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4882 | } | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4883 | } | 
|  | 4884 | return retList; | 
|  | 4885 | } | 
|  | 4886 |  | 
|  | 4887 | void CameraService::notifyPhysicalCameraStatusLocked(int32_t status, | 
|  | 4888 | const String16& physicalCameraId, const std::list<String16>& logicalCameraIds, | 
|  | 4889 | SystemCameraKind deviceKind) { | 
|  | 4890 | // mStatusListenerLock is expected to be locked | 
|  | 4891 | for (const auto& logicalCameraId : logicalCameraIds) { | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4892 | for (auto& listener : mListenerList) { | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4893 | // Note: we check only the deviceKind of the physical camera id | 
|  | 4894 | // since, logical camera ids and their physical camera ids are | 
|  | 4895 | // guaranteed to have the same system camera kind. | 
| Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 4896 | if (shouldSkipStatusUpdates(deviceKind, listener->isVendorListener(), | 
|  | 4897 | listener->getListenerPid(), listener->getListenerUid())) { | 
|  | 4898 | ALOGV("Skipping discovery callback for system-only camera device %s", | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4899 | String8(physicalCameraId).c_str()); | 
| Jayant Chowdhary | d1478ce | 2020-05-07 17:35:23 -0700 | [diff] [blame] | 4900 | continue; | 
|  | 4901 | } | 
| Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4902 | auto ret = listener->getListener()->onPhysicalCameraStatusChanged(status, | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4903 | logicalCameraId, physicalCameraId); | 
| Austin Borger | e8e2c42 | 2022-05-12 13:45:24 -0700 | [diff] [blame] | 4904 | listener->handleBinderStatus(ret, | 
|  | 4905 | "%s: Failed to trigger onPhysicalCameraStatusChanged for %d:%d: %d", | 
|  | 4906 | __FUNCTION__, listener->getListenerUid(), listener->getListenerPid(), | 
|  | 4907 | ret.exceptionCode()); | 
| Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 4908 | } | 
|  | 4909 | } | 
|  | 4910 | } | 
|  | 4911 |  | 
| Jayant Chowdhary | 5e2cd30 | 2020-08-14 02:48:34 +0000 | [diff] [blame] | 4912 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4913 | void CameraService::blockClientsForUid(uid_t uid) { | 
|  | 4914 | const auto clients = mActiveClientManager.getAll(); | 
|  | 4915 | for (auto& current : clients) { | 
|  | 4916 | if (current != nullptr) { | 
|  | 4917 | const auto basicClient = current->getValue(); | 
|  | 4918 | if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) { | 
|  | 4919 | basicClient->block(); | 
|  | 4920 | } | 
|  | 4921 | } | 
|  | 4922 | } | 
|  | 4923 | } | 
|  | 4924 |  | 
| Michael Groover | d1d435a | 2018-12-18 17:39:42 -0800 | [diff] [blame] | 4925 | void CameraService::blockAllClients() { | 
|  | 4926 | const auto clients = mActiveClientManager.getAll(); | 
|  | 4927 | for (auto& current : clients) { | 
|  | 4928 | if (current != nullptr) { | 
|  | 4929 | const auto basicClient = current->getValue(); | 
|  | 4930 | if (basicClient.get() != nullptr) { | 
|  | 4931 | basicClient->block(); | 
|  | 4932 | } | 
|  | 4933 | } | 
|  | 4934 | } | 
|  | 4935 | } | 
|  | 4936 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4937 | // NOTE: This is a remote API - make sure all args are validated | 
|  | 4938 | status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) { | 
|  | 4939 | if (!checkCallingPermission(sManageCameraPermission, nullptr, nullptr)) { | 
|  | 4940 | return PERMISSION_DENIED; | 
|  | 4941 | } | 
|  | 4942 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { | 
|  | 4943 | return BAD_VALUE; | 
|  | 4944 | } | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4945 | if (args.size() >= 3 && args[0] == String16("set-uid-state")) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4946 | return handleSetUidState(args, err); | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4947 | } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4948 | return handleResetUidState(args, err); | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4949 | } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4950 | return handleGetUidState(args, out, err); | 
| Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 4951 | } else if (args.size() >= 2 && args[0] == String16("set-rotate-and-crop")) { | 
|  | 4952 | return handleSetRotateAndCrop(args); | 
|  | 4953 | } else if (args.size() >= 1 && args[0] == String16("get-rotate-and-crop")) { | 
|  | 4954 | return handleGetRotateAndCrop(out); | 
| Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 4955 | } else if (args.size() >= 2 && args[0] == String16("set-autoframing")) { | 
|  | 4956 | return handleSetAutoframing(args); | 
|  | 4957 | } else if (args.size() >= 1 && args[0] == String16("get-autoframing")) { | 
|  | 4958 | return handleGetAutoframing(out); | 
| Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 4959 | } else if (args.size() >= 2 && args[0] == String16("set-image-dump-mask")) { | 
|  | 4960 | return handleSetImageDumpMask(args); | 
|  | 4961 | } else if (args.size() >= 1 && args[0] == String16("get-image-dump-mask")) { | 
|  | 4962 | return handleGetImageDumpMask(out); | 
| Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 4963 | } else if (args.size() >= 2 && args[0] == String16("set-camera-mute")) { | 
|  | 4964 | return handleSetCameraMute(args); | 
| Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 4965 | } else if (args.size() >= 2 && args[0] == String16("set-stream-use-case-override")) { | 
|  | 4966 | return handleSetStreamUseCaseOverrides(args); | 
|  | 4967 | } else if (args.size() >= 1 && args[0] == String16("clear-stream-use-case-override")) { | 
|  | 4968 | handleClearStreamUseCaseOverrides(); | 
|  | 4969 | return OK; | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4970 | } else if (args.size() >= 2 && args[0] == String16("watch")) { | 
| Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 4971 | return handleWatchCommand(args, in, out); | 
| Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 4972 | } else if (args.size() >= 2 && args[0] == String16("set-watchdog")) { | 
|  | 4973 | return handleSetCameraServiceWatchdog(args); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4974 | } else if (args.size() == 1 && args[0] == String16("help")) { | 
|  | 4975 | printHelp(out); | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 4976 | return OK; | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4977 | } | 
|  | 4978 | printHelp(err); | 
|  | 4979 | return BAD_VALUE; | 
|  | 4980 | } | 
|  | 4981 |  | 
|  | 4982 | status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) { | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4983 | String16 packageName = args[1]; | 
|  | 4984 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 4985 | bool active = false; | 
|  | 4986 | if (args[2] == String16("active")) { | 
|  | 4987 | active = true; | 
|  | 4988 | } else if ((args[2] != String16("idle"))) { | 
|  | 4989 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string()); | 
|  | 4990 | return BAD_VALUE; | 
|  | 4991 | } | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 4992 |  | 
|  | 4993 | int userId = 0; | 
|  | 4994 | if (args.size() >= 5 && args[3] == String16("--user")) { | 
|  | 4995 | userId = atoi(String8(args[4])); | 
|  | 4996 | } | 
|  | 4997 |  | 
|  | 4998 | uid_t uid; | 
|  | 4999 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { | 
|  | 5000 | return BAD_VALUE; | 
|  | 5001 | } | 
|  | 5002 |  | 
|  | 5003 | mUidPolicy->addOverrideUid(uid, packageName, active); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5004 | return NO_ERROR; | 
|  | 5005 | } | 
|  | 5006 |  | 
|  | 5007 | status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) { | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5008 | String16 packageName = args[1]; | 
|  | 5009 |  | 
|  | 5010 | int userId = 0; | 
|  | 5011 | if (args.size() >= 4 && args[2] == String16("--user")) { | 
|  | 5012 | userId = atoi(String8(args[3])); | 
|  | 5013 | } | 
|  | 5014 |  | 
|  | 5015 | uid_t uid; | 
|  | 5016 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5017 | return BAD_VALUE; | 
|  | 5018 | } | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5019 |  | 
|  | 5020 | mUidPolicy->removeOverrideUid(uid, packageName); | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5021 | return NO_ERROR; | 
|  | 5022 | } | 
|  | 5023 |  | 
|  | 5024 | status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) { | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5025 | String16 packageName = args[1]; | 
|  | 5026 |  | 
|  | 5027 | int userId = 0; | 
|  | 5028 | if (args.size() >= 4 && args[2] == String16("--user")) { | 
|  | 5029 | userId = atoi(String8(args[3])); | 
|  | 5030 | } | 
|  | 5031 |  | 
|  | 5032 | uid_t uid; | 
|  | 5033 | if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5034 | return BAD_VALUE; | 
|  | 5035 | } | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5036 |  | 
|  | 5037 | if (mUidPolicy->isUidActive(uid, packageName)) { | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5038 | return dprintf(out, "active\n"); | 
|  | 5039 | } else { | 
|  | 5040 | return dprintf(out, "idle\n"); | 
|  | 5041 | } | 
|  | 5042 | } | 
|  | 5043 |  | 
| Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5044 | status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) { | 
|  | 5045 | int rotateValue = atoi(String8(args[1])); | 
|  | 5046 | if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE || | 
|  | 5047 | rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE; | 
|  | 5048 | Mutex::Autolock lock(mServiceLock); | 
|  | 5049 |  | 
|  | 5050 | mOverrideRotateAndCropMode = rotateValue; | 
|  | 5051 |  | 
|  | 5052 | if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK; | 
|  | 5053 |  | 
|  | 5054 | const auto clients = mActiveClientManager.getAll(); | 
|  | 5055 | for (auto& current : clients) { | 
|  | 5056 | if (current != nullptr) { | 
|  | 5057 | const auto basicClient = current->getValue(); | 
|  | 5058 | if (basicClient.get() != nullptr) { | 
|  | 5059 | basicClient->setRotateAndCropOverride(rotateValue); | 
|  | 5060 | } | 
|  | 5061 | } | 
|  | 5062 | } | 
|  | 5063 |  | 
|  | 5064 | return OK; | 
|  | 5065 | } | 
|  | 5066 |  | 
| Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5067 | status_t CameraService::handleSetAutoframing(const Vector<String16>& args) { | 
|  | 5068 | char* end; | 
|  | 5069 | int autoframingValue = (int) strtol(String8(args[1]), &end, /*base=*/10); | 
|  | 5070 | if ((*end != '\0') || | 
|  | 5071 | (autoframingValue != ANDROID_CONTROL_AUTOFRAMING_OFF && | 
|  | 5072 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_ON && | 
|  | 5073 | autoframingValue != ANDROID_CONTROL_AUTOFRAMING_AUTO)) { | 
|  | 5074 | return BAD_VALUE; | 
|  | 5075 | } | 
|  | 5076 |  | 
|  | 5077 | Mutex::Autolock lock(mServiceLock); | 
|  | 5078 | mOverrideAutoframingMode = autoframingValue; | 
|  | 5079 |  | 
|  | 5080 | if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) return OK; | 
|  | 5081 |  | 
|  | 5082 | const auto clients = mActiveClientManager.getAll(); | 
|  | 5083 | for (auto& current : clients) { | 
|  | 5084 | if (current != nullptr) { | 
|  | 5085 | const auto basicClient = current->getValue(); | 
|  | 5086 | if (basicClient.get() != nullptr) { | 
|  | 5087 | basicClient->setAutoframingOverride(autoframingValue); | 
|  | 5088 | } | 
|  | 5089 | } | 
|  | 5090 | } | 
|  | 5091 |  | 
|  | 5092 | return OK; | 
|  | 5093 | } | 
|  | 5094 |  | 
| Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 5095 | status_t CameraService::handleSetCameraServiceWatchdog(const Vector<String16>& args) { | 
|  | 5096 | int enableWatchdog = atoi(String8(args[1])); | 
|  | 5097 |  | 
|  | 5098 | if (enableWatchdog < 0 || enableWatchdog > 1) return BAD_VALUE; | 
|  | 5099 |  | 
|  | 5100 | Mutex::Autolock lock(mServiceLock); | 
|  | 5101 |  | 
|  | 5102 | mCameraServiceWatchdogEnabled = enableWatchdog; | 
|  | 5103 |  | 
|  | 5104 | const auto clients = mActiveClientManager.getAll(); | 
|  | 5105 | for (auto& current : clients) { | 
|  | 5106 | if (current != nullptr) { | 
|  | 5107 | const auto basicClient = current->getValue(); | 
|  | 5108 | if (basicClient.get() != nullptr) { | 
|  | 5109 | basicClient->setCameraServiceWatchdog(enableWatchdog); | 
|  | 5110 | } | 
|  | 5111 | } | 
|  | 5112 | } | 
|  | 5113 |  | 
|  | 5114 | return OK; | 
|  | 5115 | } | 
|  | 5116 |  | 
| Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 5117 | status_t CameraService::handleGetRotateAndCrop(int out) { | 
|  | 5118 | Mutex::Autolock lock(mServiceLock); | 
|  | 5119 |  | 
|  | 5120 | return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode); | 
|  | 5121 | } | 
|  | 5122 |  | 
| Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5123 | status_t CameraService::handleGetAutoframing(int out) { | 
|  | 5124 | Mutex::Autolock lock(mServiceLock); | 
|  | 5125 |  | 
|  | 5126 | return dprintf(out, "autoframing override: %d\n", mOverrideAutoframingMode); | 
|  | 5127 | } | 
|  | 5128 |  | 
| Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5129 | status_t CameraService::handleSetImageDumpMask(const Vector<String16>& args) { | 
|  | 5130 | char *endPtr; | 
|  | 5131 | errno = 0; | 
|  | 5132 | String8 maskString8 = String8(args[1]); | 
|  | 5133 | long maskValue = strtol(maskString8.c_str(), &endPtr, 10); | 
|  | 5134 |  | 
|  | 5135 | if (errno != 0) return BAD_VALUE; | 
|  | 5136 | if (endPtr != maskString8.c_str() + maskString8.size()) return BAD_VALUE; | 
|  | 5137 | if (maskValue < 0 || maskValue > 1) return BAD_VALUE; | 
|  | 5138 |  | 
|  | 5139 | Mutex::Autolock lock(mServiceLock); | 
|  | 5140 |  | 
|  | 5141 | mImageDumpMask = maskValue; | 
|  | 5142 |  | 
|  | 5143 | return OK; | 
|  | 5144 | } | 
|  | 5145 |  | 
|  | 5146 | status_t CameraService::handleGetImageDumpMask(int out) { | 
|  | 5147 | Mutex::Autolock lock(mServiceLock); | 
|  | 5148 |  | 
|  | 5149 | return dprintf(out, "Image dump mask: %d\n", mImageDumpMask); | 
|  | 5150 | } | 
|  | 5151 |  | 
| Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 5152 | status_t CameraService::handleSetCameraMute(const Vector<String16>& args) { | 
|  | 5153 | int muteValue = strtol(String8(args[1]), nullptr, 10); | 
|  | 5154 | if (errno != 0) return BAD_VALUE; | 
|  | 5155 |  | 
|  | 5156 | if (muteValue < 0 || muteValue > 1) return BAD_VALUE; | 
|  | 5157 | Mutex::Autolock lock(mServiceLock); | 
|  | 5158 |  | 
|  | 5159 | mOverrideCameraMuteMode = (muteValue == 1); | 
|  | 5160 |  | 
|  | 5161 | const auto clients = mActiveClientManager.getAll(); | 
|  | 5162 | for (auto& current : clients) { | 
|  | 5163 | if (current != nullptr) { | 
|  | 5164 | const auto basicClient = current->getValue(); | 
|  | 5165 | if (basicClient.get() != nullptr) { | 
|  | 5166 | if (basicClient->supportsCameraMute()) { | 
|  | 5167 | basicClient->setCameraMute(mOverrideCameraMuteMode); | 
|  | 5168 | } | 
|  | 5169 | } | 
|  | 5170 | } | 
|  | 5171 | } | 
|  | 5172 |  | 
|  | 5173 | return OK; | 
|  | 5174 | } | 
| Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5175 |  | 
| Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5176 | status_t CameraService::handleSetStreamUseCaseOverrides(const Vector<String16>& args) { | 
|  | 5177 | std::vector<int64_t> useCasesOverride; | 
|  | 5178 | for (size_t i = 1; i < args.size(); i++) { | 
|  | 5179 | int64_t useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; | 
|  | 5180 | String8 arg8 = String8(args[i]); | 
|  | 5181 | if (arg8 == "DEFAULT") { | 
|  | 5182 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT; | 
|  | 5183 | } else if (arg8 == "PREVIEW") { | 
|  | 5184 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW; | 
|  | 5185 | } else if (arg8 == "STILL_CAPTURE") { | 
|  | 5186 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_STILL_CAPTURE; | 
|  | 5187 | } else if (arg8 == "VIDEO_RECORD") { | 
|  | 5188 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_RECORD; | 
|  | 5189 | } else if (arg8 == "PREVIEW_VIDEO_STILL") { | 
|  | 5190 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW_VIDEO_STILL; | 
|  | 5191 | } else if (arg8 == "VIDEO_CALL") { | 
|  | 5192 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL; | 
|  | 5193 | } else if (arg8 == "CROPPED_RAW") { | 
|  | 5194 | useCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_CROPPED_RAW; | 
|  | 5195 | } else { | 
|  | 5196 | ALOGE("%s: Invalid stream use case %s", __FUNCTION__, arg8.c_str()); | 
|  | 5197 | return BAD_VALUE; | 
|  | 5198 | } | 
|  | 5199 | useCasesOverride.push_back(useCase); | 
|  | 5200 | } | 
|  | 5201 |  | 
|  | 5202 | Mutex::Autolock lock(mServiceLock); | 
|  | 5203 | mStreamUseCaseOverrides = std::move(useCasesOverride); | 
|  | 5204 |  | 
|  | 5205 | return OK; | 
|  | 5206 | } | 
|  | 5207 |  | 
|  | 5208 | void CameraService::handleClearStreamUseCaseOverrides() { | 
|  | 5209 | Mutex::Autolock lock(mServiceLock); | 
|  | 5210 | mStreamUseCaseOverrides.clear(); | 
|  | 5211 | } | 
|  | 5212 |  | 
| Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5213 | status_t CameraService::handleWatchCommand(const Vector<String16>& args, int inFd, int outFd) { | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5214 | if (args.size() >= 3 && args[1] == String16("start")) { | 
|  | 5215 | return startWatchingTags(args, outFd); | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5216 | } else if (args.size() == 2 && args[1] == String16("stop")) { | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5217 | return stopWatchingTags(outFd); | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5218 | } else if (args.size() == 2 && args[1] == String16("dump")) { | 
|  | 5219 | return printWatchedTags(outFd); | 
|  | 5220 | } else if (args.size() >= 2 && args[1] == String16("live")) { | 
|  | 5221 | return printWatchedTagsUntilInterrupt(args, inFd, outFd); | 
| Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5222 | } else if (args.size() == 2 && args[1] == String16("clear")) { | 
|  | 5223 | return clearCachedMonitoredTagDumps(outFd); | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5224 | } | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5225 | dprintf(outFd, "Camera service watch commands:\n" | 
|  | 5226 | "  start -m <comma_separated_tag_list> [-c <comma_separated_client_list>]\n" | 
|  | 5227 | "        starts watching the provided tags for clients with provided package\n" | 
|  | 5228 | "        recognizes tag shorthands like '3a'\n" | 
|  | 5229 | "        watches all clients if no client is passed, or if 'all' is listed\n" | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5230 | "  dump dumps the monitoring information and exits\n" | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5231 | "  stop stops watching all tags\n" | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5232 | "  live [-n <refresh_interval_ms>]\n" | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5233 | "        prints the monitored information in real time\n" | 
| Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5234 | "        Hit return to exit\n" | 
| Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5235 | "  clear clears all buffers storing information for watch command"); | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5236 | return BAD_VALUE; | 
|  | 5237 | } | 
|  | 5238 |  | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5239 | status_t CameraService::startWatchingTags(const Vector<String16> &args, int outFd) { | 
|  | 5240 | Mutex::Autolock lock(mLogLock); | 
|  | 5241 | size_t tagsIdx; // index of '-m' | 
|  | 5242 | String16 tags(""); | 
|  | 5243 | for (tagsIdx = 2; tagsIdx < args.size() && args[tagsIdx] != String16("-m"); tagsIdx++); | 
|  | 5244 | if (tagsIdx < args.size() - 1) { | 
|  | 5245 | tags = args[tagsIdx + 1]; | 
|  | 5246 | } else { | 
|  | 5247 | dprintf(outFd, "No tags provided.\n"); | 
|  | 5248 | return BAD_VALUE; | 
|  | 5249 | } | 
|  | 5250 |  | 
|  | 5251 | size_t clientsIdx; // index of '-c' | 
|  | 5252 | String16 clients = kWatchAllClientsFlag; // watch all clients if no clients are provided | 
|  | 5253 | for (clientsIdx = 2; clientsIdx < args.size() && args[clientsIdx] != String16("-c"); | 
|  | 5254 | clientsIdx++); | 
|  | 5255 | if (clientsIdx < args.size() - 1) { | 
|  | 5256 | clients = args[clientsIdx + 1]; | 
|  | 5257 | } | 
|  | 5258 | parseClientsToWatchLocked(String8(clients)); | 
|  | 5259 |  | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5260 | // track tags to initialize future clients with the monitoring information | 
|  | 5261 | mMonitorTags = String8(tags); | 
|  | 5262 |  | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5263 | bool serviceLock = tryLock(mServiceLock); | 
|  | 5264 | int numWatchedClients = 0; | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5265 | auto cameraClients = mActiveClientManager.getAll(); | 
|  | 5266 | for (const auto &clientDescriptor: cameraClients) { | 
|  | 5267 | if (clientDescriptor == nullptr) { continue; } | 
|  | 5268 | sp<BasicClient> client = clientDescriptor->getValue(); | 
|  | 5269 | if (client.get() == nullptr) { continue; } | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5270 |  | 
|  | 5271 | if (isClientWatchedLocked(client.get())) { | 
|  | 5272 | client->startWatchingTags(mMonitorTags, outFd); | 
|  | 5273 | numWatchedClients++; | 
|  | 5274 | } | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5275 | } | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5276 | dprintf(outFd, "Started watching %d active clients\n", numWatchedClients); | 
|  | 5277 |  | 
|  | 5278 | if (serviceLock) { mServiceLock.unlock(); } | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5279 | return OK; | 
|  | 5280 | } | 
|  | 5281 |  | 
|  | 5282 | status_t CameraService::stopWatchingTags(int outFd) { | 
|  | 5283 | // clear mMonitorTags to prevent new clients from monitoring tags at initialization | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5284 | Mutex::Autolock lock(mLogLock); | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5285 | mMonitorTags = String8::empty(); | 
|  | 5286 |  | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5287 | mWatchedClientPackages.clear(); | 
|  | 5288 | mWatchedClientsDumpCache.clear(); | 
|  | 5289 |  | 
|  | 5290 | bool serviceLock = tryLock(mServiceLock); | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5291 | auto cameraClients = mActiveClientManager.getAll(); | 
|  | 5292 | for (const auto &clientDescriptor : cameraClients) { | 
|  | 5293 | if (clientDescriptor == nullptr) { continue; } | 
|  | 5294 | sp<BasicClient> client = clientDescriptor->getValue(); | 
|  | 5295 | if (client.get() == nullptr) { continue; } | 
|  | 5296 | client->stopWatchingTags(outFd); | 
|  | 5297 | } | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5298 | dprintf(outFd, "Stopped watching all clients.\n"); | 
|  | 5299 | if (serviceLock) { mServiceLock.unlock(); } | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5300 | return OK; | 
|  | 5301 | } | 
|  | 5302 |  | 
| Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5303 | status_t CameraService::clearCachedMonitoredTagDumps(int outFd) { | 
|  | 5304 | Mutex::Autolock lock(mLogLock); | 
|  | 5305 | size_t clearedSize = mWatchedClientsDumpCache.size(); | 
|  | 5306 | mWatchedClientsDumpCache.clear(); | 
|  | 5307 | dprintf(outFd, "Cleared tag information of %zu cached clients.\n", clearedSize); | 
|  | 5308 | return OK; | 
|  | 5309 | } | 
|  | 5310 |  | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5311 | status_t CameraService::printWatchedTags(int outFd) { | 
|  | 5312 | Mutex::Autolock logLock(mLogLock); | 
| Avichal Rakesh | 1cda16b | 2021-11-09 16:51:02 -0800 | [diff] [blame] | 5313 | std::set<String16> connectedMonitoredClients; | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5314 |  | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5315 | bool printedSomething = false; // tracks if any monitoring information was printed | 
|  | 5316 | // (from either cached or active clients) | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5317 |  | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5318 | bool serviceLock = tryLock(mServiceLock); | 
|  | 5319 | // get all watched clients that are currently connected | 
|  | 5320 | for (const auto &clientDescriptor: mActiveClientManager.getAll()) { | 
|  | 5321 | if (clientDescriptor == nullptr) { continue; } | 
|  | 5322 |  | 
|  | 5323 | sp<BasicClient> client = clientDescriptor->getValue(); | 
|  | 5324 | if (client.get() == nullptr) { continue; } | 
|  | 5325 | if (!isClientWatchedLocked(client.get())) { continue; } | 
|  | 5326 |  | 
|  | 5327 | std::vector<std::string> dumpVector; | 
|  | 5328 | client->dumpWatchedEventsToVector(dumpVector); | 
|  | 5329 |  | 
|  | 5330 | size_t printIdx = dumpVector.size(); | 
|  | 5331 | if (printIdx == 0) { | 
|  | 5332 | continue; | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5333 | } | 
|  | 5334 |  | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5335 | // Print tag dumps for active client | 
|  | 5336 | const String8 &cameraId = clientDescriptor->getKey(); | 
|  | 5337 | String8 packageName8 = String8(client->getPackageName()); | 
|  | 5338 | const char *printablePackageName = packageName8.lockBuffer(packageName8.size()); | 
|  | 5339 | dprintf(outFd, "Client: %s (active)\n", printablePackageName); | 
|  | 5340 | while(printIdx > 0) { | 
|  | 5341 | printIdx--; | 
|  | 5342 | dprintf(outFd, "%s:%s  %s", cameraId.string(), printablePackageName, | 
|  | 5343 | dumpVector[printIdx].c_str()); | 
| Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5344 | } | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5345 | dprintf(outFd, "\n"); | 
|  | 5346 | packageName8.unlockBuffer(); | 
|  | 5347 | printedSomething = true; | 
|  | 5348 |  | 
|  | 5349 | connectedMonitoredClients.emplace(client->getPackageName()); | 
|  | 5350 | } | 
|  | 5351 | if (serviceLock) { mServiceLock.unlock(); } | 
|  | 5352 |  | 
|  | 5353 | // Print entries in mWatchedClientsDumpCache for clients that are not connected | 
|  | 5354 | for (const auto &kv: mWatchedClientsDumpCache) { | 
|  | 5355 | const String16 &package = kv.first; | 
|  | 5356 | if (connectedMonitoredClients.find(package) != connectedMonitoredClients.end()) { | 
|  | 5357 | continue; | 
|  | 5358 | } | 
|  | 5359 |  | 
|  | 5360 | dprintf(outFd, "Client: %s (cached)\n", String8(package).string()); | 
|  | 5361 | dprintf(outFd, "%s\n", kv.second.c_str()); | 
|  | 5362 | printedSomething = true; | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5363 | } | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5364 |  | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5365 | if (!printedSomething) { | 
|  | 5366 | dprintf(outFd, "No monitoring information to print.\n"); | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5367 | } | 
|  | 5368 |  | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5369 | return OK; | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5370 | } | 
|  | 5371 |  | 
| Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5372 | // Print all events in vector `events' that came after lastPrintedEvent | 
|  | 5373 | void printNewWatchedEvents(int outFd, | 
|  | 5374 | const char *cameraId, | 
|  | 5375 | const String16 &packageName, | 
|  | 5376 | const std::vector<std::string> &events, | 
|  | 5377 | const std::string &lastPrintedEvent) { | 
|  | 5378 | if (events.empty()) { return; } | 
|  | 5379 |  | 
|  | 5380 | // index of lastPrintedEvent in events. | 
|  | 5381 | // lastPrintedIdx = events.size() if lastPrintedEvent is not in events | 
|  | 5382 | size_t lastPrintedIdx; | 
|  | 5383 | for (lastPrintedIdx = 0; | 
|  | 5384 | lastPrintedIdx < events.size() && lastPrintedEvent != events[lastPrintedIdx]; | 
|  | 5385 | lastPrintedIdx++); | 
|  | 5386 |  | 
|  | 5387 | if (lastPrintedIdx == 0) { return; } // early exit if no new event in `events` | 
|  | 5388 |  | 
|  | 5389 | String8 packageName8(packageName); | 
|  | 5390 | const char *printablePackageName = packageName8.lockBuffer(packageName8.size()); | 
|  | 5391 |  | 
|  | 5392 | // print events in chronological order (latest event last) | 
|  | 5393 | size_t idxToPrint = lastPrintedIdx; | 
|  | 5394 | do { | 
|  | 5395 | idxToPrint--; | 
|  | 5396 | dprintf(outFd, "%s:%s  %s", cameraId, printablePackageName, events[idxToPrint].c_str()); | 
|  | 5397 | } while (idxToPrint != 0); | 
|  | 5398 |  | 
|  | 5399 | packageName8.unlockBuffer(); | 
|  | 5400 | } | 
|  | 5401 |  | 
|  | 5402 | // Returns true if adb shell cmd watch should be interrupted based on data in inFd. The watch | 
|  | 5403 | // command should be interrupted if the user presses the return key, or if user loses any way to | 
|  | 5404 | // signal interrupt. | 
|  | 5405 | // If timeoutMs == 0, this function will always return false | 
|  | 5406 | bool shouldInterruptWatchCommand(int inFd, int outFd, long timeoutMs) { | 
|  | 5407 | struct timeval startTime; | 
|  | 5408 | int startTimeError = gettimeofday(&startTime, nullptr); | 
|  | 5409 | if (startTimeError) { | 
|  | 5410 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); | 
|  | 5411 | return true; | 
|  | 5412 | } | 
|  | 5413 |  | 
|  | 5414 | const nfds_t numFds = 1; | 
|  | 5415 | struct pollfd pollFd = { .fd = inFd, .events = POLLIN, .revents = 0 }; | 
|  | 5416 |  | 
|  | 5417 | struct timeval currTime; | 
|  | 5418 | char buffer[2]; | 
|  | 5419 | while(true) { | 
|  | 5420 | int currTimeError = gettimeofday(&currTime, nullptr); | 
|  | 5421 | if (currTimeError) { | 
|  | 5422 | dprintf(outFd, "Failed waiting for interrupt, aborting.\n"); | 
|  | 5423 | return true; | 
|  | 5424 | } | 
|  | 5425 |  | 
|  | 5426 | long elapsedTimeMs = ((currTime.tv_sec - startTime.tv_sec) * 1000L) | 
|  | 5427 | + ((currTime.tv_usec - startTime.tv_usec) / 1000L); | 
|  | 5428 | int remainingTimeMs = (int) (timeoutMs - elapsedTimeMs); | 
|  | 5429 |  | 
|  | 5430 | if (remainingTimeMs <= 0) { | 
|  | 5431 | // No user interrupt within timeoutMs, don't interrupt watch command | 
|  | 5432 | return false; | 
|  | 5433 | } | 
|  | 5434 |  | 
|  | 5435 | int numFdsUpdated = poll(&pollFd, numFds, remainingTimeMs); | 
|  | 5436 | if (numFdsUpdated < 0) { | 
|  | 5437 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); | 
|  | 5438 | return true; | 
|  | 5439 | } | 
|  | 5440 |  | 
|  | 5441 | if (numFdsUpdated == 0) { | 
|  | 5442 | // No user input within timeoutMs, don't interrupt watch command | 
|  | 5443 | return false; | 
|  | 5444 | } | 
|  | 5445 |  | 
|  | 5446 | if (!(pollFd.revents & POLLIN)) { | 
|  | 5447 | dprintf(outFd, "Failed while waiting for user input. Exiting.\n"); | 
|  | 5448 | return true; | 
|  | 5449 | } | 
|  | 5450 |  | 
|  | 5451 | ssize_t sizeRead = read(inFd, buffer, sizeof(buffer) - 1); | 
|  | 5452 | if (sizeRead < 0) { | 
|  | 5453 | dprintf(outFd, "Error reading user input. Exiting.\n"); | 
|  | 5454 | return true; | 
|  | 5455 | } | 
|  | 5456 |  | 
|  | 5457 | if (sizeRead == 0) { | 
|  | 5458 | // Reached end of input fd (can happen if input is piped) | 
|  | 5459 | // User has no way to signal an interrupt, so interrupt here | 
|  | 5460 | return true; | 
|  | 5461 | } | 
|  | 5462 |  | 
|  | 5463 | if (buffer[0] == '\n') { | 
|  | 5464 | // User pressed return, interrupt watch command. | 
|  | 5465 | return true; | 
|  | 5466 | } | 
|  | 5467 | } | 
|  | 5468 | } | 
|  | 5469 |  | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5470 | status_t CameraService::printWatchedTagsUntilInterrupt(const Vector<String16> &args, | 
|  | 5471 | int inFd, int outFd) { | 
|  | 5472 | // Figure out refresh interval, if present in args | 
|  | 5473 | long refreshTimeoutMs = 1000L; // refresh every 1s by default | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5474 | if (args.size() > 2) { | 
|  | 5475 | size_t intervalIdx; // index of '-n' | 
|  | 5476 | for (intervalIdx = 2; intervalIdx < args.size() && String16("-n") != args[intervalIdx]; | 
|  | 5477 | intervalIdx++); | 
|  | 5478 |  | 
|  | 5479 | size_t intervalValIdx = intervalIdx + 1; | 
|  | 5480 | if (intervalValIdx < args.size()) { | 
|  | 5481 | refreshTimeoutMs = strtol(String8(args[intervalValIdx].string()), nullptr, 10); | 
|  | 5482 | if (errno) { return BAD_VALUE; } | 
|  | 5483 | } | 
|  | 5484 | } | 
|  | 5485 |  | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5486 | // Set min timeout of 10ms. This prevents edge cases in polling when timeout of 0 is passed. | 
|  | 5487 | refreshTimeoutMs = refreshTimeoutMs < 10 ? 10 : refreshTimeoutMs; | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5488 |  | 
| Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5489 | dprintf(outFd, "Press return to exit...\n\n"); | 
| Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5490 | std::map<String16, std::string> packageNameToLastEvent; | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5491 |  | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5492 | while (true) { | 
| Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5493 | bool serviceLock = tryLock(mServiceLock); | 
|  | 5494 | auto cameraClients = mActiveClientManager.getAll(); | 
|  | 5495 | if (serviceLock) { mServiceLock.unlock(); } | 
|  | 5496 |  | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5497 | for (const auto& clientDescriptor : cameraClients) { | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5498 | Mutex::Autolock lock(mLogLock); | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5499 | if (clientDescriptor == nullptr) { continue; } | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5500 |  | 
|  | 5501 | sp<BasicClient> client = clientDescriptor->getValue(); | 
|  | 5502 | if (client.get() == nullptr) { continue; } | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5503 | if (!isClientWatchedLocked(client.get())) { continue; } | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5504 |  | 
| Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5505 | const String16 &packageName = client->getPackageName(); | 
|  | 5506 | // This also initializes the map entries with an empty string | 
|  | 5507 | const std::string& lastPrintedEvent = packageNameToLastEvent[packageName]; | 
|  | 5508 |  | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5509 | std::vector<std::string> latestEvents; | 
|  | 5510 | client->dumpWatchedEventsToVector(latestEvents); | 
|  | 5511 |  | 
|  | 5512 | if (!latestEvents.empty()) { | 
| Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5513 | String8 cameraId = clientDescriptor->getKey(); | 
|  | 5514 | const char *printableCameraId = cameraId.lockBuffer(cameraId.size()); | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5515 | printNewWatchedEvents(outFd, | 
| Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5516 | printableCameraId, | 
|  | 5517 | packageName, | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5518 | latestEvents, | 
|  | 5519 | lastPrintedEvent); | 
| Avichal Rakesh | a14d983 | 2021-11-11 01:41:55 -0800 | [diff] [blame] | 5520 | packageNameToLastEvent[packageName] = latestEvents[0]; | 
|  | 5521 | cameraId.unlockBuffer(); | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5522 | } | 
|  | 5523 | } | 
| Avichal Rakesh | 9e5a1e4 | 2021-11-15 12:11:21 -0800 | [diff] [blame] | 5524 | if (shouldInterruptWatchCommand(inFd, outFd, refreshTimeoutMs)) { | 
| Avichal Rakesh | 8414713 | 2021-11-11 17:47:11 -0800 | [diff] [blame] | 5525 | break; | 
|  | 5526 | } | 
| Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 5527 | } | 
|  | 5528 | return OK; | 
|  | 5529 | } | 
|  | 5530 |  | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5531 | void CameraService::parseClientsToWatchLocked(String8 clients) { | 
|  | 5532 | mWatchedClientPackages.clear(); | 
|  | 5533 |  | 
|  | 5534 | const char *allSentinel = String8(kWatchAllClientsFlag).string(); | 
|  | 5535 |  | 
|  | 5536 | char *tokenized = clients.lockBuffer(clients.size()); | 
|  | 5537 | char *savePtr; | 
|  | 5538 | char *nextClient = strtok_r(tokenized, ",", &savePtr); | 
|  | 5539 |  | 
|  | 5540 | while (nextClient != nullptr) { | 
|  | 5541 | if (strcmp(nextClient, allSentinel) == 0) { | 
|  | 5542 | // Don't need to track any other package if 'all' is present | 
|  | 5543 | mWatchedClientPackages.clear(); | 
|  | 5544 | mWatchedClientPackages.emplace(kWatchAllClientsFlag); | 
|  | 5545 | break; | 
|  | 5546 | } | 
|  | 5547 |  | 
|  | 5548 | // track package names | 
|  | 5549 | mWatchedClientPackages.emplace(nextClient); | 
|  | 5550 | nextClient = strtok_r(nullptr, ",", &savePtr); | 
|  | 5551 | } | 
|  | 5552 | clients.unlockBuffer(); | 
|  | 5553 | } | 
|  | 5554 |  | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5555 | status_t CameraService::printHelp(int out) { | 
|  | 5556 | return dprintf(out, "Camera service commands:\n" | 
| Nicholas Sauer | a362033 | 2019-04-03 14:05:17 -0700 | [diff] [blame] | 5557 | "  get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" | 
|  | 5558 | "  set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" | 
|  | 5559 | "  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] | 5560 | "  set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n" | 
|  | 5561 | "      Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n" | 
|  | 5562 | "  get-rotate-and-crop returns the current override rotate-and-crop value\n" | 
| Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 5563 | "  set-autoframing <VALUE> overrides the autoframing value for AUTO\n" | 
|  | 5564 | "      Valid values 0=false, 1=true, 2=auto\n" | 
|  | 5565 | "  get-autoframing returns the current override autoframing value\n" | 
| Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 5566 | "  set-image-dump-mask <MASK> specifies the formats to be saved to disk\n" | 
|  | 5567 | "      Valid values 0=OFF, 1=ON for JPEG\n" | 
|  | 5568 | "  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] | 5569 | "  set-camera-mute <0/1> enable or disable camera muting\n" | 
| Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 5570 | "  set-stream-use-case-override <usecase1> <usecase2> ... override stream use cases\n" | 
|  | 5571 | "      Use cases applied in descending resolutions. So usecase1 is assigned to the\n" | 
|  | 5572 | "      largest resolution, usecase2 is assigned to the 2nd largest resolution, and so\n" | 
|  | 5573 | "      on. In case the number of usecases is smaller than the number of streams, the\n" | 
|  | 5574 | "      last use case is assigned to all the remaining streams. In case of multiple\n" | 
|  | 5575 | "      streams with the same resolution, the tie-breaker is (JPEG, RAW, YUV, and PRIV)\n" | 
|  | 5576 | "      Valid values are (case sensitive): DEFAULT, PREVIEW, STILL_CAPTURE, VIDEO_RECORD,\n" | 
|  | 5577 | "      PREVIEW_VIDEO_STILL, VIDEO_CALL, CROPPED_RAW\n" | 
|  | 5578 | "  clear-stream-use-case-override clear the stream use case override\n" | 
| Avichal Rakesh | 3a85d2d | 2021-11-10 16:21:33 -0800 | [diff] [blame] | 5579 | "  watch <start|stop|dump|print|clear> manages tag monitoring in connected clients\n" | 
| Svet Ganov | a453d0d | 2018-01-11 15:37:58 -0800 | [diff] [blame] | 5580 | "  help print this message\n"); | 
|  | 5581 | } | 
|  | 5582 |  | 
| Avichal Rakesh | 7fbc398 | 2021-10-20 04:35:03 -0700 | [diff] [blame] | 5583 | bool CameraService::isClientWatched(const BasicClient *client) { | 
|  | 5584 | Mutex::Autolock lock(mLogLock); | 
|  | 5585 | return isClientWatchedLocked(client); | 
|  | 5586 | } | 
|  | 5587 |  | 
|  | 5588 | bool CameraService::isClientWatchedLocked(const BasicClient *client) { | 
|  | 5589 | return mWatchedClientPackages.find(kWatchAllClientsFlag) != mWatchedClientPackages.end() || | 
|  | 5590 | mWatchedClientPackages.find(client->getPackageName()) != mWatchedClientPackages.end(); | 
|  | 5591 | } | 
|  | 5592 |  | 
| Yin-Chia Yeh | dba0323 | 2019-08-19 15:54:28 -0700 | [diff] [blame] | 5593 | int32_t CameraService::updateAudioRestriction() { | 
|  | 5594 | Mutex::Autolock lock(mServiceLock); | 
|  | 5595 | return updateAudioRestrictionLocked(); | 
|  | 5596 | } | 
|  | 5597 |  | 
|  | 5598 | int32_t CameraService::updateAudioRestrictionLocked() { | 
|  | 5599 | int32_t mode = 0; | 
|  | 5600 | // iterate through all active client | 
|  | 5601 | for (const auto& i : mActiveClientManager.getAll()) { | 
|  | 5602 | const auto clientSp = i->getValue(); | 
|  | 5603 | mode |= clientSp->getAudioRestriction(); | 
|  | 5604 | } | 
|  | 5605 |  | 
|  | 5606 | bool modeChanged = (mAudioRestriction != mode); | 
|  | 5607 | mAudioRestriction = mode; | 
|  | 5608 | if (modeChanged) { | 
|  | 5609 | mAppOps.setCameraAudioRestriction(mode); | 
|  | 5610 | } | 
|  | 5611 | return mode; | 
|  | 5612 | } | 
|  | 5613 |  | 
| Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 5614 | status_t CameraService::checkIfInjectionCameraIsPresent(const String8& externalCamId, | 
|  | 5615 | sp<BasicClient> clientSp) { | 
|  | 5616 | std::unique_ptr<AutoConditionLock> lock = | 
|  | 5617 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); | 
|  | 5618 | status_t res = NO_ERROR; | 
|  | 5619 | if ((res = checkIfDeviceIsUsable(externalCamId)) != NO_ERROR) { | 
|  | 5620 | ALOGW("Device %s is not usable!", externalCamId.string()); | 
|  | 5621 | mInjectionStatusListener->notifyInjectionError( | 
|  | 5622 | externalCamId, UNKNOWN_TRANSACTION); | 
|  | 5623 | clientSp->notifyError( | 
|  | 5624 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, | 
|  | 5625 | CaptureResultExtras()); | 
|  | 5626 |  | 
|  | 5627 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking | 
|  | 5628 | // other clients from connecting in mServiceLockWrapper if held | 
|  | 5629 | mServiceLock.unlock(); | 
|  | 5630 |  | 
|  | 5631 | // Clear caller identity temporarily so client disconnect PID checks work correctly | 
|  | 5632 | int64_t token = CameraThreadState::clearCallingIdentity(); | 
|  | 5633 | clientSp->disconnect(); | 
|  | 5634 | CameraThreadState::restoreCallingIdentity(token); | 
|  | 5635 |  | 
|  | 5636 | // Reacquire mServiceLock | 
|  | 5637 | mServiceLock.lock(); | 
|  | 5638 | } | 
|  | 5639 |  | 
|  | 5640 | return res; | 
|  | 5641 | } | 
|  | 5642 |  | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5643 | void CameraService::clearInjectionParameters() { | 
|  | 5644 | { | 
|  | 5645 | Mutex::Autolock lock(mInjectionParametersLock); | 
| Cliff Wu | 646bd61 | 2021-11-23 23:21:29 +0800 | [diff] [blame] | 5646 | mInjectionInitPending = false; | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 5647 | mInjectionInternalCamId = ""; | 
|  | 5648 | } | 
|  | 5649 | mInjectionExternalCamId = ""; | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5650 | mInjectionStatusListener->removeListener(); | 
| Cliff Wu | d8cae10 | 2021-03-11 01:37:42 +0800 | [diff] [blame] | 5651 | } | 
|  | 5652 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5653 | }; // namespace android |