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