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