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