Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | */ |
| 16 | |
| 17 | #define LOG_TAG "AudioPolicyService" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 20 | #include "Configuration.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 21 | #undef __STRICT_ANSI__ |
| 22 | #define __STDINT_LIMITS |
| 23 | #define __STDC_LIMIT_MACROS |
| 24 | #include <stdint.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 25 | #include <sys/time.h> |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 26 | #include <dlfcn.h> |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 27 | |
| 28 | #include <audio_utils/clock.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 29 | #include <binder/IServiceManager.h> |
| 30 | #include <utils/Log.h> |
| 31 | #include <cutils/properties.h> |
| 32 | #include <binder/IPCThreadState.h> |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 33 | #include <binder/PermissionController.h> |
| 34 | #include <binder/IResultReceiver.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 35 | #include <utils/String16.h> |
| 36 | #include <utils/threads.h> |
Atneya Nair | 5b1ed64 | 2023-06-23 14:43:37 -0700 | [diff] [blame] | 37 | #include "AudioRecordClient.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 38 | #include "AudioPolicyService.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 39 | #include <hardware_legacy/power.h> |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 40 | #include <media/AidlConversion.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 41 | #include <media/AudioEffect.h> |
Chih-Hung Hsieh | c84d9d2 | 2014-11-14 13:33:34 -0800 | [diff] [blame] | 42 | #include <media/AudioParameter.h> |
Andy Hung | c747c53 | 2022-03-07 21:41:14 -0800 | [diff] [blame] | 43 | #include <mediautils/MethodStatistics.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 44 | #include <mediautils/ServiceUtilities.h> |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 45 | #include <mediautils/TimeCheck.h> |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 46 | #include <sensorprivacy/SensorPrivacyManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 47 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 48 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 49 | #include <system/audio_policy.h> |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 50 | #include <AudioPolicyConfig.h> |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 51 | #include <AudioPolicyManager.h> |
Mikhail Naganov | 61a4fac | 2016-10-13 14:44:18 -0700 | [diff] [blame] | 52 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 53 | namespace android { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 54 | using binder::Status; |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 55 | using media::audio::common::Spatialization; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 56 | |
Glenn Kasten | 8dad0e3 | 2012-01-09 08:41:22 -0800 | [diff] [blame] | 57 | static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n"; |
| 58 | static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n"; |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 59 | static const char kAudioPolicyManagerCustomPath[] = "libaudiopolicymanagercustom.so"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 60 | |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 61 | static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 63 | static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 64 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 65 | static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY"); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 66 | |
Andy Hung | c747c53 | 2022-03-07 21:41:14 -0800 | [diff] [blame] | 67 | // Creates an association between Binder code to name for IAudioPolicyService. |
| 68 | #define IAUDIOPOLICYSERVICE_BINDER_METHOD_MACRO_LIST \ |
| 69 | BINDER_METHOD_ENTRY(onNewAudioModulesAvailable) \ |
| 70 | BINDER_METHOD_ENTRY(setDeviceConnectionState) \ |
| 71 | BINDER_METHOD_ENTRY(getDeviceConnectionState) \ |
| 72 | BINDER_METHOD_ENTRY(handleDeviceConfigChange) \ |
| 73 | BINDER_METHOD_ENTRY(setPhoneState) \ |
| 74 | BINDER_METHOD_ENTRY(setForceUse) \ |
| 75 | BINDER_METHOD_ENTRY(getForceUse) \ |
| 76 | BINDER_METHOD_ENTRY(getOutput) \ |
| 77 | BINDER_METHOD_ENTRY(getOutputForAttr) \ |
| 78 | BINDER_METHOD_ENTRY(startOutput) \ |
| 79 | BINDER_METHOD_ENTRY(stopOutput) \ |
| 80 | BINDER_METHOD_ENTRY(releaseOutput) \ |
| 81 | BINDER_METHOD_ENTRY(getInputForAttr) \ |
| 82 | BINDER_METHOD_ENTRY(startInput) \ |
| 83 | BINDER_METHOD_ENTRY(stopInput) \ |
| 84 | BINDER_METHOD_ENTRY(releaseInput) \ |
| 85 | BINDER_METHOD_ENTRY(initStreamVolume) \ |
| 86 | BINDER_METHOD_ENTRY(setStreamVolumeIndex) \ |
| 87 | BINDER_METHOD_ENTRY(getStreamVolumeIndex) \ |
| 88 | BINDER_METHOD_ENTRY(setVolumeIndexForAttributes) \ |
| 89 | BINDER_METHOD_ENTRY(getVolumeIndexForAttributes) \ |
| 90 | BINDER_METHOD_ENTRY(getMaxVolumeIndexForAttributes) \ |
| 91 | BINDER_METHOD_ENTRY(getMinVolumeIndexForAttributes) \ |
| 92 | BINDER_METHOD_ENTRY(getStrategyForStream) \ |
| 93 | BINDER_METHOD_ENTRY(getDevicesForAttributes) \ |
| 94 | BINDER_METHOD_ENTRY(getOutputForEffect) \ |
| 95 | BINDER_METHOD_ENTRY(registerEffect) \ |
| 96 | BINDER_METHOD_ENTRY(unregisterEffect) \ |
| 97 | BINDER_METHOD_ENTRY(setEffectEnabled) \ |
| 98 | BINDER_METHOD_ENTRY(moveEffectsToIo) \ |
| 99 | BINDER_METHOD_ENTRY(isStreamActive) \ |
| 100 | BINDER_METHOD_ENTRY(isStreamActiveRemotely) \ |
| 101 | BINDER_METHOD_ENTRY(isSourceActive) \ |
| 102 | BINDER_METHOD_ENTRY(queryDefaultPreProcessing) \ |
| 103 | BINDER_METHOD_ENTRY(addSourceDefaultEffect) \ |
| 104 | BINDER_METHOD_ENTRY(addStreamDefaultEffect) \ |
| 105 | BINDER_METHOD_ENTRY(removeSourceDefaultEffect) \ |
| 106 | BINDER_METHOD_ENTRY(removeStreamDefaultEffect) \ |
| 107 | BINDER_METHOD_ENTRY(setSupportedSystemUsages) \ |
| 108 | BINDER_METHOD_ENTRY(setAllowedCapturePolicy) \ |
| 109 | BINDER_METHOD_ENTRY(getOffloadSupport) \ |
| 110 | BINDER_METHOD_ENTRY(isDirectOutputSupported) \ |
| 111 | BINDER_METHOD_ENTRY(listAudioPorts) \ |
| 112 | BINDER_METHOD_ENTRY(getAudioPort) \ |
| 113 | BINDER_METHOD_ENTRY(createAudioPatch) \ |
| 114 | BINDER_METHOD_ENTRY(releaseAudioPatch) \ |
| 115 | BINDER_METHOD_ENTRY(listAudioPatches) \ |
| 116 | BINDER_METHOD_ENTRY(setAudioPortConfig) \ |
| 117 | BINDER_METHOD_ENTRY(registerClient) \ |
| 118 | BINDER_METHOD_ENTRY(setAudioPortCallbacksEnabled) \ |
| 119 | BINDER_METHOD_ENTRY(setAudioVolumeGroupCallbacksEnabled) \ |
| 120 | BINDER_METHOD_ENTRY(acquireSoundTriggerSession) \ |
| 121 | BINDER_METHOD_ENTRY(releaseSoundTriggerSession) \ |
| 122 | BINDER_METHOD_ENTRY(getPhoneState) \ |
| 123 | BINDER_METHOD_ENTRY(registerPolicyMixes) \ |
Jan Sebechlebsky | 0af8e87 | 2023-08-11 14:45:08 +0200 | [diff] [blame] | 124 | BINDER_METHOD_ENTRY(updatePolicyMixes) \ |
Andy Hung | c747c53 | 2022-03-07 21:41:14 -0800 | [diff] [blame] | 125 | BINDER_METHOD_ENTRY(setUidDeviceAffinities) \ |
| 126 | BINDER_METHOD_ENTRY(removeUidDeviceAffinities) \ |
| 127 | BINDER_METHOD_ENTRY(setUserIdDeviceAffinities) \ |
| 128 | BINDER_METHOD_ENTRY(removeUserIdDeviceAffinities) \ |
| 129 | BINDER_METHOD_ENTRY(startAudioSource) \ |
| 130 | BINDER_METHOD_ENTRY(stopAudioSource) \ |
| 131 | BINDER_METHOD_ENTRY(setMasterMono) \ |
| 132 | BINDER_METHOD_ENTRY(getMasterMono) \ |
| 133 | BINDER_METHOD_ENTRY(getStreamVolumeDB) \ |
| 134 | BINDER_METHOD_ENTRY(getSurroundFormats) \ |
| 135 | BINDER_METHOD_ENTRY(getReportedSurroundFormats) \ |
| 136 | BINDER_METHOD_ENTRY(getHwOffloadFormatsSupportedForBluetoothMedia) \ |
| 137 | BINDER_METHOD_ENTRY(setSurroundFormatEnabled) \ |
| 138 | BINDER_METHOD_ENTRY(setAssistantServicesUids) \ |
| 139 | BINDER_METHOD_ENTRY(setActiveAssistantServicesUids) \ |
| 140 | BINDER_METHOD_ENTRY(setA11yServicesUids) \ |
| 141 | BINDER_METHOD_ENTRY(setCurrentImeUid) \ |
| 142 | BINDER_METHOD_ENTRY(isHapticPlaybackSupported) \ |
| 143 | BINDER_METHOD_ENTRY(isUltrasoundSupported) \ |
Atneya Nair | 698f5ef | 2022-12-15 16:15:09 -0800 | [diff] [blame] | 144 | BINDER_METHOD_ENTRY(isHotwordStreamSupported) \ |
Andy Hung | c747c53 | 2022-03-07 21:41:14 -0800 | [diff] [blame] | 145 | BINDER_METHOD_ENTRY(listAudioProductStrategies) \ |
| 146 | BINDER_METHOD_ENTRY(getProductStrategyFromAudioAttributes) \ |
| 147 | BINDER_METHOD_ENTRY(listAudioVolumeGroups) \ |
| 148 | BINDER_METHOD_ENTRY(getVolumeGroupFromAudioAttributes) \ |
| 149 | BINDER_METHOD_ENTRY(setRttEnabled) \ |
| 150 | BINDER_METHOD_ENTRY(isCallScreenModeSupported) \ |
| 151 | BINDER_METHOD_ENTRY(setDevicesRoleForStrategy) \ |
| 152 | BINDER_METHOD_ENTRY(removeDevicesRoleForStrategy) \ |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 153 | BINDER_METHOD_ENTRY(clearDevicesRoleForStrategy) \ |
Andy Hung | c747c53 | 2022-03-07 21:41:14 -0800 | [diff] [blame] | 154 | BINDER_METHOD_ENTRY(getDevicesForRoleAndStrategy) \ |
| 155 | BINDER_METHOD_ENTRY(setDevicesRoleForCapturePreset) \ |
| 156 | BINDER_METHOD_ENTRY(addDevicesRoleForCapturePreset) \ |
| 157 | BINDER_METHOD_ENTRY(removeDevicesRoleForCapturePreset) \ |
| 158 | BINDER_METHOD_ENTRY(clearDevicesRoleForCapturePreset) \ |
| 159 | BINDER_METHOD_ENTRY(getDevicesForRoleAndCapturePreset) \ |
| 160 | BINDER_METHOD_ENTRY(registerSoundTriggerCaptureStateListener) \ |
| 161 | BINDER_METHOD_ENTRY(getSpatializer) \ |
| 162 | BINDER_METHOD_ENTRY(canBeSpatialized) \ |
| 163 | BINDER_METHOD_ENTRY(getDirectPlaybackSupport) \ |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 164 | BINDER_METHOD_ENTRY(getDirectProfilesForAttributes) \ |
| 165 | BINDER_METHOD_ENTRY(getSupportedMixerAttributes) \ |
| 166 | BINDER_METHOD_ENTRY(setPreferredMixerAttributes) \ |
| 167 | BINDER_METHOD_ENTRY(getPreferredMixerAttributes) \ |
| 168 | BINDER_METHOD_ENTRY(clearPreferredMixerAttributes) \ |
Marvin Ramin | bdefaf0 | 2023-11-01 09:10:32 +0100 | [diff] [blame] | 169 | BINDER_METHOD_ENTRY(getRegisteredPolicyMixes) \ |
| 170 | \ |
Andy Hung | c747c53 | 2022-03-07 21:41:14 -0800 | [diff] [blame] | 171 | // singleton for Binder Method Statistics for IAudioPolicyService |
| 172 | static auto& getIAudioPolicyServiceStatistics() { |
| 173 | using Code = int; |
| 174 | |
| 175 | #pragma push_macro("BINDER_METHOD_ENTRY") |
| 176 | #undef BINDER_METHOD_ENTRY |
| 177 | #define BINDER_METHOD_ENTRY(ENTRY) \ |
| 178 | {(Code)media::BnAudioPolicyService::TRANSACTION_##ENTRY, #ENTRY}, |
| 179 | |
| 180 | static mediautils::MethodStatistics<Code> methodStatistics{ |
| 181 | IAUDIOPOLICYSERVICE_BINDER_METHOD_MACRO_LIST |
| 182 | METHOD_STATISTICS_BINDER_CODE_NAMES(Code) |
| 183 | }; |
| 184 | #pragma pop_macro("BINDER_METHOD_ENTRY") |
| 185 | |
| 186 | return methodStatistics; |
| 187 | } |
| 188 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 189 | // ---------------------------------------------------------------------------- |
| 190 | |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 191 | static AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 192 | { |
Mikhail Naganov | 9e459d7 | 2023-05-05 17:36:39 -0700 | [diff] [blame] | 193 | AudioPolicyManager *apm = nullptr; |
| 194 | media::AudioPolicyConfig apmConfig; |
| 195 | if (status_t status = clientInterface->getAudioPolicyConfig(&apmConfig); status == OK) { |
| 196 | auto config = AudioPolicyConfig::loadFromApmAidlConfigWithFallback(apmConfig); |
| 197 | LOG_ALWAYS_FATAL_IF(config->getEngineLibraryNameSuffix() != |
| 198 | AudioPolicyConfig::kDefaultEngineLibraryNameSuffix, |
| 199 | "Only default engine is currently supported with the AIDL HAL"); |
| 200 | apm = new AudioPolicyManager(config, |
| 201 | loadApmEngineLibraryAndCreateEngine( |
| 202 | config->getEngineLibraryNameSuffix(), apmConfig.engineConfig), |
| 203 | clientInterface); |
| 204 | } else { |
| 205 | auto config = AudioPolicyConfig::loadFromApmXmlConfigWithFallback(); // This can't fail. |
| 206 | apm = new AudioPolicyManager(config, |
| 207 | loadApmEngineLibraryAndCreateEngine(config->getEngineLibraryNameSuffix()), |
| 208 | clientInterface); |
| 209 | } |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 210 | status_t status = apm->initialize(); |
| 211 | if (status != NO_ERROR) { |
| 212 | delete apm; |
| 213 | apm = nullptr; |
| 214 | } |
| 215 | return apm; |
| 216 | } |
| 217 | |
| 218 | static void destroyAudioPolicyManager(AudioPolicyInterface *interface) |
| 219 | { |
| 220 | delete interface; |
| 221 | } |
| 222 | // ---------------------------------------------------------------------------- |
| 223 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 224 | AudioPolicyService::AudioPolicyService() |
Ytai Ben-Tsvi | 85093d5 | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 225 | : BnAudioPolicyService(), |
Ytai Ben-Tsvi | 85093d5 | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 226 | mAudioPolicyManager(NULL), |
| 227 | mAudioPolicyClient(NULL), |
| 228 | mPhoneState(AUDIO_MODE_INVALID), |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 229 | mCaptureStateNotifier(false), |
| 230 | mCreateAudioPolicyManager(createAudioPolicyManager), |
Eric Laurent | 0d13fea | 2022-11-04 17:12:08 +0100 | [diff] [blame] | 231 | mDestroyAudioPolicyManager(destroyAudioPolicyManager), |
| 232 | mUsecaseValidator(media::createUsecaseValidator()) { |
Andy Hung | 225aef6 | 2022-12-06 16:33:20 -0800 | [diff] [blame] | 233 | setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO); |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void AudioPolicyService::loadAudioPolicyManager() |
| 237 | { |
| 238 | mLibraryHandle = dlopen(kAudioPolicyManagerCustomPath, RTLD_NOW); |
| 239 | if (mLibraryHandle != nullptr) { |
| 240 | ALOGI("%s loading %s", __func__, kAudioPolicyManagerCustomPath); |
| 241 | mCreateAudioPolicyManager = reinterpret_cast<CreateAudioPolicyManagerInstance> |
| 242 | (dlsym(mLibraryHandle, "createAudioPolicyManager")); |
| 243 | const char *lastError = dlerror(); |
| 244 | ALOGW_IF(mCreateAudioPolicyManager == nullptr, "%s createAudioPolicyManager is null %s", |
| 245 | __func__, lastError != nullptr ? lastError : "no error"); |
| 246 | |
| 247 | mDestroyAudioPolicyManager = reinterpret_cast<DestroyAudioPolicyManagerInstance>( |
| 248 | dlsym(mLibraryHandle, "destroyAudioPolicyManager")); |
| 249 | lastError = dlerror(); |
| 250 | ALOGW_IF(mDestroyAudioPolicyManager == nullptr, "%s destroyAudioPolicyManager is null %s", |
| 251 | __func__, lastError != nullptr ? lastError : "no error"); |
| 252 | if (mCreateAudioPolicyManager == nullptr || mDestroyAudioPolicyManager == nullptr){ |
| 253 | unloadAudioPolicyManager(); |
| 254 | LOG_ALWAYS_FATAL("could not find audiopolicymanager interface methods"); |
| 255 | } |
| 256 | } |
Eric Laurent | f5ada6e | 2014-10-09 17:49:00 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void AudioPolicyService::onFirstRef() |
| 260 | { |
Andy Hung | d47aca2 | 2022-03-15 11:50:51 -0700 | [diff] [blame] | 261 | // Log an AudioPolicy "constructor" mediametrics event on first ref. |
| 262 | // This records the time it takes to load the audio modules and devices. |
| 263 | mediametrics::Defer defer([beginNs = systemTime()] { |
| 264 | mediametrics::LogItem(AMEDIAMETRICS_KEY_AUDIO_POLICY) |
| 265 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CTOR) |
| 266 | .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(systemTime() - beginNs)) |
| 267 | .record(); }); |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 268 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 269 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 270 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 271 | // start audio commands thread |
| 272 | mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this); |
| 273 | // start output activity command thread |
| 274 | mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 275 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 276 | mAudioPolicyClient = new AudioPolicyClient(this); |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 277 | |
| 278 | loadAudioPolicyManager(); |
| 279 | mAudioPolicyManager = mCreateAudioPolicyManager(mAudioPolicyClient); |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 280 | } |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 281 | |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 282 | // load audio processing modules |
Shunkai Yao | 8d6489a | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 283 | const sp<EffectsFactoryHalInterface> effectsFactoryHal = EffectsFactoryHalInterface::create(); |
Andy Hung | d3595ed | 2023-12-19 15:34:46 -0800 | [diff] [blame] | 284 | auto audioPolicyEffects = sp<AudioPolicyEffects>::make(effectsFactoryHal); |
| 285 | auto uidPolicy = sp<UidPolicy>::make(this); |
| 286 | auto sensorPrivacyPolicy = sp<SensorPrivacyPolicy>::make(this); |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 287 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 288 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 289 | mAudioPolicyEffects = audioPolicyEffects; |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 290 | mUidPolicy = uidPolicy; |
| 291 | mSensorPrivacyPolicy = sensorPrivacyPolicy; |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 292 | } |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 293 | uidPolicy->registerSelf(); |
| 294 | sensorPrivacyPolicy->registerSelf(); |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 295 | |
Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 296 | // Create spatializer if supported |
Eric Laurent | 52b0bd5 | 2021-09-27 15:25:40 +0200 | [diff] [blame] | 297 | if (mAudioPolicyManager != nullptr) { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 298 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | 52b0bd5 | 2021-09-27 15:25:40 +0200 | [diff] [blame] | 299 | const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA); |
| 300 | AudioDeviceTypeAddrVector devices; |
| 301 | bool hasSpatializer = mAudioPolicyManager->canBeSpatialized(&attr, nullptr, devices); |
| 302 | if (hasSpatializer) { |
Andy Hung | 1040da5 | 2023-12-06 20:59:06 -0800 | [diff] [blame] | 303 | // Unlock as Spatializer::create() will use the callback and acquire the |
| 304 | // AudioPolicyService_Mutex. |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 305 | mMutex.unlock(); |
Shunkai Yao | 8d6489a | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 306 | mSpatializer = Spatializer::create(this, effectsFactoryHal); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 307 | mMutex.lock(); |
Eric Laurent | 52b0bd5 | 2021-09-27 15:25:40 +0200 | [diff] [blame] | 308 | } |
Andy Hung | 8aa43c0 | 2022-09-13 18:53:06 -0700 | [diff] [blame] | 309 | if (mSpatializer == nullptr) { |
| 310 | // No spatializer created, signal the reason: NO_INIT a failure, OK means intended. |
| 311 | const status_t createStatus = hasSpatializer ? NO_INIT : OK; |
| 312 | Spatializer::sendEmptyCreateSpatializerMetricWithStatus(createStatus); |
| 313 | } |
Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 314 | } |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 315 | AudioSystem::audioPolicyReady(); |
Andy Hung | b08b375 | 2024-01-09 19:05:23 -0800 | [diff] [blame^] | 316 | } |
| 317 | |
| 318 | void AudioPolicyService::onAudioSystemReady() { |
| 319 | sp<AudioPolicyEffects> audioPolicyEffects; |
| 320 | { |
| 321 | audio_utils::lock_guard _l(mMutex); |
| 322 | |
| 323 | audioPolicyEffects = mAudioPolicyEffects; |
| 324 | } |
| 325 | audioPolicyEffects->initDefaultDeviceEffects(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 328 | void AudioPolicyService::unloadAudioPolicyManager() |
| 329 | { |
| 330 | ALOGV("%s ", __func__); |
| 331 | if (mLibraryHandle != nullptr) { |
| 332 | dlclose(mLibraryHandle); |
| 333 | } |
| 334 | mLibraryHandle = nullptr; |
| 335 | mCreateAudioPolicyManager = nullptr; |
| 336 | mDestroyAudioPolicyManager = nullptr; |
| 337 | } |
| 338 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 339 | AudioPolicyService::~AudioPolicyService() |
| 340 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 341 | mAudioCommandThread->exit(); |
Eric Laurent | 657ff61 | 2014-05-07 11:58:24 -0700 | [diff] [blame] | 342 | mOutputCommandThread->exit(); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 343 | |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 344 | mDestroyAudioPolicyManager(mAudioPolicyManager); |
| 345 | unloadAudioPolicyManager(); |
| 346 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 347 | delete mAudioPolicyClient; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 348 | |
| 349 | mNotificationClients.clear(); |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 350 | mAudioPolicyEffects.clear(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 351 | |
| 352 | mUidPolicy->unregisterSelf(); |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 353 | mSensorPrivacyPolicy->unregisterSelf(); |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 354 | |
| 355 | mUidPolicy.clear(); |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 356 | mSensorPrivacyPolicy.clear(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // A notification client is always registered by AudioSystem when the client process |
| 360 | // connects to AudioPolicyService. |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 361 | Status AudioPolicyService::registerClient(const sp<media::IAudioPolicyServiceClient>& client) |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 362 | { |
Eric Laurent | 1259025 | 2015-08-21 18:40:20 -0700 | [diff] [blame] | 363 | if (client == 0) { |
| 364 | ALOGW("%s got NULL client", __FUNCTION__); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 365 | return Status::ok(); |
Eric Laurent | 1259025 | 2015-08-21 18:40:20 -0700 | [diff] [blame] | 366 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 367 | audio_utils::lock_guard _l(mNotificationClientsMutex); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 368 | |
| 369 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 370 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 371 | int64_t token = ((int64_t)uid<<32) | pid; |
| 372 | |
| 373 | if (mNotificationClients.indexOfKey(token) < 0) { |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 374 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 375 | client, |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 376 | uid, |
| 377 | pid); |
| 378 | ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 379 | |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 380 | mNotificationClients.add(token, notificationClient); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 381 | |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 382 | sp<IBinder> binder = IInterface::asBinder(client); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 383 | binder->linkToDeath(notificationClient); |
| 384 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 385 | return Status::ok(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 388 | Status AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled) |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 389 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 390 | audio_utils::lock_guard _l(mNotificationClientsMutex); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 391 | |
| 392 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 393 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 394 | int64_t token = ((int64_t)uid<<32) | pid; |
| 395 | |
| 396 | if (mNotificationClients.indexOfKey(token) < 0) { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 397 | return Status::ok(); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 398 | } |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 399 | mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 400 | return Status::ok(); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 403 | Status AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled) |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 404 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 405 | audio_utils::lock_guard _l(mNotificationClientsMutex); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 406 | |
| 407 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 408 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 409 | int64_t token = ((int64_t)uid<<32) | pid; |
| 410 | |
| 411 | if (mNotificationClients.indexOfKey(token) < 0) { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 412 | return Status::ok(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 413 | } |
| 414 | mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 415 | return Status::ok(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 418 | // removeNotificationClient() is called when the client process dies. |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 419 | void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid) |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 420 | { |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 421 | bool hasSameUid = false; |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 422 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 423 | audio_utils::lock_guard _l(mNotificationClientsMutex); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 424 | int64_t token = ((int64_t)uid<<32) | pid; |
| 425 | mNotificationClients.removeItem(token); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 426 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 427 | if (mNotificationClients.valueAt(i)->uid() == uid) { |
| 428 | hasSameUid = true; |
| 429 | break; |
| 430 | } |
| 431 | } |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 432 | } |
| 433 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 434 | audio_utils::lock_guard _l(mMutex); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 435 | if (mAudioPolicyManager && !hasSameUid) { |
Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 436 | // called from binder death notification: no need to clear caller identity |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 437 | mAudioPolicyManager->releaseResourcesForUid(uid); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 438 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 439 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | void AudioPolicyService::onAudioPortListUpdate() |
| 443 | { |
| 444 | mOutputCommandThread->updateAudioPortListCommand(); |
| 445 | } |
| 446 | |
| 447 | void AudioPolicyService::doOnAudioPortListUpdate() |
| 448 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 449 | audio_utils::lock_guard _l(mNotificationClientsMutex); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 450 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 451 | mNotificationClients.valueAt(i)->onAudioPortListUpdate(); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | void AudioPolicyService::onAudioPatchListUpdate() |
| 456 | { |
| 457 | mOutputCommandThread->updateAudioPatchListCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 460 | void AudioPolicyService::doOnAudioPatchListUpdate() |
| 461 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 462 | audio_utils::lock_guard _l(mNotificationClientsMutex); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 463 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 464 | mNotificationClients.valueAt(i)->onAudioPatchListUpdate(); |
| 465 | } |
| 466 | } |
| 467 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 468 | void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags) |
| 469 | { |
| 470 | mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags); |
| 471 | } |
| 472 | |
| 473 | void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags) |
| 474 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 475 | audio_utils::lock_guard _l(mNotificationClientsMutex); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 476 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 477 | mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags); |
| 478 | } |
| 479 | } |
| 480 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 481 | void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state) |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 482 | { |
| 483 | ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 484 | regId.c_str(), state); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 485 | mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state); |
| 486 | } |
| 487 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 488 | void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state) |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 489 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 490 | audio_utils::lock_guard _l(mNotificationClientsMutex); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 491 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 492 | mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state); |
| 493 | } |
| 494 | } |
| 495 | |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 496 | void AudioPolicyService::onRecordingConfigurationUpdate( |
| 497 | int event, |
| 498 | const record_client_info_t *clientInfo, |
| 499 | const audio_config_base_t *clientConfig, |
| 500 | std::vector<effect_descriptor_t> clientEffects, |
| 501 | const audio_config_base_t *deviceConfig, |
| 502 | std::vector<effect_descriptor_t> effects, |
| 503 | audio_patch_handle_t patchHandle, |
| 504 | audio_source_t source) |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 505 | { |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 506 | mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo, |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 507 | clientConfig, clientEffects, deviceConfig, effects, patchHandle, source); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 510 | void AudioPolicyService::doOnRecordingConfigurationUpdate( |
| 511 | int event, |
| 512 | const record_client_info_t *clientInfo, |
| 513 | const audio_config_base_t *clientConfig, |
| 514 | std::vector<effect_descriptor_t> clientEffects, |
| 515 | const audio_config_base_t *deviceConfig, |
| 516 | std::vector<effect_descriptor_t> effects, |
| 517 | audio_patch_handle_t patchHandle, |
| 518 | audio_source_t source) |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 519 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 520 | audio_utils::lock_guard _l(mNotificationClientsMutex); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 521 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 522 | mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo, |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 523 | clientConfig, clientEffects, deviceConfig, effects, patchHandle, source); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 527 | void AudioPolicyService::onRoutingUpdated() |
| 528 | { |
| 529 | mOutputCommandThread->routingChangedCommand(); |
| 530 | } |
| 531 | |
| 532 | void AudioPolicyService::doOnRoutingUpdated() |
| 533 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 534 | audio_utils::lock_guard _l(mNotificationClientsMutex); |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 535 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 536 | mNotificationClients.valueAt(i)->onRoutingUpdated(); |
| 537 | } |
| 538 | } |
| 539 | |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 540 | void AudioPolicyService::onVolumeRangeInitRequest() |
| 541 | { |
| 542 | mOutputCommandThread->volRangeInitReqCommand(); |
| 543 | } |
| 544 | |
| 545 | void AudioPolicyService::doOnVolumeRangeInitRequest() |
| 546 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 547 | audio_utils::lock_guard _l(mNotificationClientsMutex); |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 548 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 549 | mNotificationClients.valueAt(i)->onVolumeRangeInitRequest(); |
| 550 | } |
| 551 | } |
| 552 | |
Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 553 | void AudioPolicyService::onCheckSpatializer() |
| 554 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 555 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 556 | onCheckSpatializer_l(); |
| 557 | } |
| 558 | |
| 559 | void AudioPolicyService::onCheckSpatializer_l() |
| 560 | { |
| 561 | if (mSpatializer != nullptr) { |
| 562 | mOutputCommandThread->checkSpatializerCommand(); |
| 563 | } |
Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | void AudioPolicyService::doOnCheckSpatializer() |
| 567 | { |
Eric Laurent | d6bee3a | 2022-08-31 16:07:50 +0200 | [diff] [blame] | 568 | ALOGV("%s mSpatializer %p level %d", |
| 569 | __func__, mSpatializer.get(), (int)mSpatializer->getLevel()); |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 570 | |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 571 | if (mSpatializer != nullptr) { |
Eric Laurent | 52b0bd5 | 2021-09-27 15:25:40 +0200 | [diff] [blame] | 572 | // Note: mSpatializer != nullptr => mAudioPolicyManager != nullptr |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 573 | if (mSpatializer->getLevel() != Spatialization::Level::NONE) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 574 | audio_io_handle_t currentOutput = mSpatializer->getOutput(); |
| 575 | audio_io_handle_t newOutput; |
| 576 | const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA); |
| 577 | audio_config_base_t config = mSpatializer->getAudioInConfig(); |
Eric Laurent | 21270b6 | 2022-07-04 15:11:29 +0200 | [diff] [blame] | 578 | |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 579 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 580 | status_t status = |
| 581 | mAudioPolicyManager->getSpatializerOutput(&config, &attr, &newOutput); |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 582 | ALOGV("%s currentOutput %d newOutput %d channel_mask %#x", |
| 583 | __func__, currentOutput, newOutput, config.channel_mask); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 584 | if (status == NO_ERROR && currentOutput == newOutput) { |
| 585 | return; |
| 586 | } |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 587 | size_t numActiveTracks = countActiveClientsOnOutput_l(newOutput); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 588 | mMutex.unlock(); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 589 | // It is OK to call detachOutput() is none is already attached. |
| 590 | mSpatializer->detachOutput(); |
Eric Laurent | 21270b6 | 2022-07-04 15:11:29 +0200 | [diff] [blame] | 591 | if (status == NO_ERROR && newOutput != AUDIO_IO_HANDLE_NONE) { |
| 592 | status = mSpatializer->attachOutput(newOutput, numActiveTracks); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 593 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 594 | mMutex.lock(); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 595 | if (status != NO_ERROR) { |
| 596 | mAudioPolicyManager->releaseSpatializerOutput(newOutput); |
| 597 | } |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 598 | } else if (mSpatializer->getLevel() == Spatialization::Level::NONE && |
| 599 | mSpatializer->getOutput() != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 600 | audio_io_handle_t output = mSpatializer->detachOutput(); |
Eric Laurent | 21270b6 | 2022-07-04 15:11:29 +0200 | [diff] [blame] | 601 | |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 602 | if (output != AUDIO_IO_HANDLE_NONE) { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 603 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 604 | mAudioPolicyManager->releaseSpatializerOutput(output); |
Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 610 | size_t AudioPolicyService::countActiveClientsOnOutput_l( |
| 611 | audio_io_handle_t output, bool spatializedOnly) { |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 612 | size_t count = 0; |
| 613 | for (size_t i = 0; i < mAudioPlaybackClients.size(); i++) { |
| 614 | auto client = mAudioPlaybackClients.valueAt(i); |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 615 | if (client->io == output && client->active |
| 616 | && (!spatializedOnly || client->isSpatialized)) { |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 617 | count++; |
| 618 | } |
| 619 | } |
| 620 | return count; |
| 621 | } |
| 622 | |
| 623 | void AudioPolicyService::onUpdateActiveSpatializerTracks_l() { |
| 624 | if (mSpatializer == nullptr) { |
| 625 | return; |
| 626 | } |
| 627 | mOutputCommandThread->updateActiveSpatializerTracksCommand(); |
| 628 | } |
| 629 | |
| 630 | void AudioPolicyService::doOnUpdateActiveSpatializerTracks() |
| 631 | { |
Eric Laurent | 21270b6 | 2022-07-04 15:11:29 +0200 | [diff] [blame] | 632 | if (mSpatializer == nullptr) { |
| 633 | return; |
| 634 | } |
| 635 | audio_io_handle_t output = mSpatializer->getOutput(); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 636 | size_t activeClients; |
| 637 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 638 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | 21270b6 | 2022-07-04 15:11:29 +0200 | [diff] [blame] | 639 | activeClients = countActiveClientsOnOutput_l(output); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 640 | } |
Eric Laurent | 21270b6 | 2022-07-04 15:11:29 +0200 | [diff] [blame] | 641 | mSpatializer->updateActiveTracks(activeClients); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 642 | } |
| 643 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 644 | status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch, |
| 645 | audio_patch_handle_t *handle, |
| 646 | int delayMs) |
| 647 | { |
| 648 | return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs); |
| 649 | } |
| 650 | |
| 651 | status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle, |
| 652 | int delayMs) |
| 653 | { |
| 654 | return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs); |
| 655 | } |
| 656 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 657 | status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config, |
| 658 | int delayMs) |
| 659 | { |
| 660 | return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs); |
| 661 | } |
| 662 | |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 663 | AudioPolicyService::NotificationClient::NotificationClient( |
| 664 | const sp<AudioPolicyService>& service, |
| 665 | const sp<media::IAudioPolicyServiceClient>& client, |
| 666 | uid_t uid, |
| 667 | pid_t pid) |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 668 | : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client), |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 669 | mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false) |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 670 | { |
| 671 | } |
| 672 | |
| 673 | AudioPolicyService::NotificationClient::~NotificationClient() |
| 674 | { |
| 675 | } |
| 676 | |
| 677 | void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused) |
| 678 | { |
| 679 | sp<NotificationClient> keep(this); |
| 680 | sp<AudioPolicyService> service = mService.promote(); |
| 681 | if (service != 0) { |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 682 | service->removeNotificationClient(mUid, mPid); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
| 686 | void AudioPolicyService::NotificationClient::onAudioPortListUpdate() |
| 687 | { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 688 | if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) { |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 689 | mAudioPolicyServiceClient->onAudioPortListUpdate(); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | void AudioPolicyService::NotificationClient::onAudioPatchListUpdate() |
| 694 | { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 695 | if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) { |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 696 | mAudioPolicyServiceClient->onAudioPatchListUpdate(); |
| 697 | } |
| 698 | } |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 699 | |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 700 | void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 701 | int flags) |
| 702 | { |
| 703 | if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) { |
| 704 | mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 709 | void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 710 | const String8& regId, int32_t state) |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 711 | { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 712 | if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) { |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 713 | mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate( |
| 714 | legacy2aidl_String8_string(regId).value(), state); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
| 718 | void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate( |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 719 | int event, |
| 720 | const record_client_info_t *clientInfo, |
| 721 | const audio_config_base_t *clientConfig, |
| 722 | std::vector<effect_descriptor_t> clientEffects, |
| 723 | const audio_config_base_t *deviceConfig, |
| 724 | std::vector<effect_descriptor_t> effects, |
| 725 | audio_patch_handle_t patchHandle, |
| 726 | audio_source_t source) |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 727 | { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 728 | if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) { |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 729 | status_t status = [&]() -> status_t { |
| 730 | int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event)); |
| 731 | media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS( |
| 732 | legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo)); |
Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 733 | AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS( |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 734 | legacy2aidl_audio_config_base_t_AudioConfigBase( |
| 735 | *clientConfig, true /*isInput*/)); |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 736 | std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS( |
| 737 | convertContainer<std::vector<media::EffectDescriptor>>( |
| 738 | clientEffects, |
| 739 | legacy2aidl_effect_descriptor_t_EffectDescriptor)); |
Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 740 | AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS( |
Mikhail Naganov | de3fa18 | 2021-07-30 15:06:42 -0700 | [diff] [blame] | 741 | legacy2aidl_audio_config_base_t_AudioConfigBase( |
| 742 | *deviceConfig, true /*isInput*/)); |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 743 | std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS( |
| 744 | convertContainer<std::vector<media::EffectDescriptor>>( |
| 745 | effects, |
| 746 | legacy2aidl_effect_descriptor_t_EffectDescriptor)); |
| 747 | int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS( |
| 748 | legacy2aidl_audio_patch_handle_t_int32_t(patchHandle)); |
Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 749 | media::audio::common::AudioSource sourceAidl = VALUE_OR_RETURN_STATUS( |
| 750 | legacy2aidl_audio_source_t_AudioSource(source)); |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 751 | return aidl_utils::statusTFromBinderStatus( |
| 752 | mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl, |
| 753 | clientInfoAidl, |
| 754 | clientConfigAidl, |
| 755 | clientEffectsAidl, |
| 756 | deviceConfigAidl, |
| 757 | effectsAidl, |
| 758 | patchHandleAidl, |
| 759 | sourceAidl)); |
| 760 | }(); |
| 761 | ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 765 | void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled) |
| 766 | { |
| 767 | mAudioPortCallbacksEnabled = enabled; |
| 768 | } |
| 769 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 770 | void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled) |
| 771 | { |
| 772 | mAudioVolumeGroupCallbacksEnabled = enabled; |
| 773 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 774 | |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 775 | void AudioPolicyService::NotificationClient::onRoutingUpdated() |
| 776 | { |
| 777 | if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) { |
| 778 | mAudioPolicyServiceClient->onRoutingUpdated(); |
| 779 | } |
| 780 | } |
| 781 | |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 782 | void AudioPolicyService::NotificationClient::onVolumeRangeInitRequest() |
| 783 | { |
| 784 | if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) { |
| 785 | mAudioPolicyServiceClient->onVolumeRangeInitRequest(); |
| 786 | } |
| 787 | } |
| 788 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 789 | void AudioPolicyService::binderDied(const wp<IBinder>& who) { |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 790 | ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(), |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 791 | IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 794 | static void dumpReleaseLock(audio_utils::mutex& mutex, bool locked) |
| 795 | RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 796 | { |
| 797 | if (locked) mutex.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | status_t AudioPolicyService::dumpInternals(int fd) |
| 801 | { |
| 802 | const size_t SIZE = 256; |
| 803 | char buffer[SIZE]; |
| 804 | String8 result; |
| 805 | |
Mikhail Naganov | b3bcb4f | 2022-02-23 23:46:56 +0000 | [diff] [blame] | 806 | snprintf(buffer, SIZE, "Supported System Usages:\n "); |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 807 | result.append(buffer); |
Mikhail Naganov | b3bcb4f | 2022-02-23 23:46:56 +0000 | [diff] [blame] | 808 | std::stringstream msg; |
| 809 | size_t i = 0; |
| 810 | for (auto usage : mSupportedSystemUsages) { |
| 811 | if (i++ != 0) msg << ", "; |
| 812 | if (const char* strUsage = audio_usage_to_string(usage); strUsage) { |
| 813 | msg << strUsage; |
| 814 | } else { |
| 815 | msg << usage << " (unknown)"; |
| 816 | } |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 817 | } |
Mikhail Naganov | b3bcb4f | 2022-02-23 23:46:56 +0000 | [diff] [blame] | 818 | if (i == 0) { |
| 819 | msg << "None"; |
| 820 | } |
| 821 | msg << std::endl; |
| 822 | result.append(msg.str().c_str()); |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 823 | |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 824 | write(fd, result.c_str(), result.size()); |
Oscar Azucena | 829d90d | 2022-01-28 17:17:56 -0800 | [diff] [blame] | 825 | |
| 826 | mUidPolicy->dumpInternals(fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 827 | return NO_ERROR; |
| 828 | } |
| 829 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 830 | void AudioPolicyService::updateUidStates() |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 831 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 832 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 833 | updateUidStates_l(); |
| 834 | } |
| 835 | |
| 836 | void AudioPolicyService::updateUidStates_l() |
| 837 | { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 838 | // Go over all active clients and allow capture (does not force silence) in the |
| 839 | // following cases: |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 840 | // The client is in the active assistant list |
| 841 | // AND is TOP |
| 842 | // AND an accessibility service is TOP |
| 843 | // AND source is either VOICE_RECOGNITION OR HOTWORD |
| 844 | // OR there is no active privacy sensitive capture or call |
| 845 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
| 846 | // AND source is VOICE_RECOGNITION OR HOTWORD |
| 847 | // The client is an assistant AND active assistant is not being used |
Evan Severson | 1f700cd | 2021-02-10 13:10:37 -0800 | [diff] [blame] | 848 | // AND an accessibility service is on TOP or a RTT call is active |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 849 | // AND the source is VOICE_RECOGNITION or HOTWORD |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 850 | // OR there is no active privacy sensitive capture or call |
Evan Severson | 1f700cd | 2021-02-10 13:10:37 -0800 | [diff] [blame] | 851 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 852 | // AND is TOP most recent assistant and uses VOICE_RECOGNITION or HOTWORD |
| 853 | // OR there is no top recent assistant and source is HOTWORD |
Evan Severson | 1f700cd | 2021-02-10 13:10:37 -0800 | [diff] [blame] | 854 | // OR The client is an accessibility service |
| 855 | // AND Is on TOP |
| 856 | // AND the source is VOICE_RECOGNITION or HOTWORD |
| 857 | // OR The assistant is not on TOP |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 858 | // AND there is no active privacy sensitive capture or call |
| 859 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Evan Severson | 1f700cd | 2021-02-10 13:10:37 -0800 | [diff] [blame] | 860 | // AND is on TOP |
| 861 | // AND the source is VOICE_RECOGNITION or HOTWORD |
| 862 | // OR the client source is virtual (remote submix, call audio TX or RX...) |
| 863 | // OR the client source is HOTWORD |
| 864 | // AND is on TOP |
| 865 | // OR all active clients are using HOTWORD source |
| 866 | // AND no call is active |
| 867 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
| 868 | // OR the client is the current InputMethodService |
| 869 | // AND a RTT call is active AND the source is VOICE_RECOGNITION |
| 870 | // OR Any client |
| 871 | // AND The assistant is not on TOP |
| 872 | // AND is on TOP or latest started |
| 873 | // AND there is no active privacy sensitive capture or call |
| 874 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 875 | |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 876 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 877 | sp<AudioRecordClient> topActive; |
| 878 | sp<AudioRecordClient> latestActive; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 879 | sp<AudioRecordClient> topSensitiveActive; |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 880 | sp<AudioRecordClient> latestSensitiveActiveOrComm; |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 881 | sp<AudioRecordClient> latestActiveAssistant; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 882 | |
Eric Laurent | a46bedb | 2018-12-07 18:01:26 -0800 | [diff] [blame] | 883 | nsecs_t topStartNs = 0; |
| 884 | nsecs_t latestStartNs = 0; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 885 | nsecs_t topSensitiveStartNs = 0; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 886 | nsecs_t latestSensitiveStartNs = 0; |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 887 | nsecs_t latestAssistantStartNs = 0; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 888 | bool isA11yOnTop = mUidPolicy->isA11yOnTop(); |
| 889 | bool isAssistantOnTop = false; |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 890 | bool useActiveAssistantList = false; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 891 | bool isSensitiveActive = false; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 892 | bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 893 | bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION; |
| 894 | bool rttCallActive = (isInCall || isInCommunication) |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 895 | && mUidPolicy->isRttEnabled(); |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 896 | bool onlyHotwordActive = true; |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 897 | bool isPhoneStateOwnerActive = false; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 898 | |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 899 | // if Sensor Privacy is enabled then all recordings should be silenced. |
| 900 | if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
| 901 | silenceAllRecordings_l(); |
| 902 | return; |
| 903 | } |
| 904 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 905 | for (size_t i =0; i < mAudioRecordClients.size(); i++) { |
| 906 | sp<AudioRecordClient> current = mAudioRecordClients[i]; |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 907 | uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t( |
| 908 | current->attributionSource.uid)); |
Evan Severson | 1f700cd | 2021-02-10 13:10:37 -0800 | [diff] [blame] | 909 | if (!current->active) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 910 | continue; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 911 | } |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 912 | |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 913 | app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(currentUid)); |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 914 | // clients which app is in IDLE state are not eligible for top active or |
| 915 | // latest active |
| 916 | if (appState == APP_STATE_IDLE) { |
| 917 | continue; |
| 918 | } |
| 919 | |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 920 | bool isAccessibility = mUidPolicy->isA11yUid(currentUid); |
Eric Laurent | 14a8863 | 2020-07-16 12:28:30 -0700 | [diff] [blame] | 921 | // Clients capturing for Accessibility services or virtual sources are not considered |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 922 | // for top or latest active to avoid masking regular clients started before |
Eric Laurent | 14a8863 | 2020-07-16 12:28:30 -0700 | [diff] [blame] | 923 | if (!isAccessibility && !isVirtualSource(current->attributes.source)) { |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 924 | bool isAssistant = mUidPolicy->isAssistantUid(currentUid); |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 925 | bool isActiveAssistant = mUidPolicy->isActiveAssistantUid(currentUid); |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 926 | bool isPrivacySensitive = |
| 927 | (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0; |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 928 | |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 929 | if (appState == APP_STATE_TOP) { |
| 930 | if (isPrivacySensitive) { |
| 931 | if (current->startTimeNs > topSensitiveStartNs) { |
| 932 | topSensitiveActive = current; |
| 933 | topSensitiveStartNs = current->startTimeNs; |
| 934 | } |
| 935 | } else { |
| 936 | if (current->startTimeNs > topStartNs) { |
| 937 | topActive = current; |
| 938 | topStartNs = current->startTimeNs; |
| 939 | } |
| 940 | } |
| 941 | if (isAssistant) { |
| 942 | isAssistantOnTop = true; |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 943 | if (isActiveAssistant) { |
| 944 | useActiveAssistantList = true; |
| 945 | } else if (!useActiveAssistantList) { |
| 946 | if (current->startTimeNs > latestAssistantStartNs) { |
| 947 | latestActiveAssistant = current; |
| 948 | latestAssistantStartNs = current->startTimeNs; |
| 949 | } |
| 950 | } |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 951 | } |
Eric Laurent | a46bedb | 2018-12-07 18:01:26 -0800 | [diff] [blame] | 952 | } |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 953 | // Clients capturing for HOTWORD are not considered |
| 954 | // for latest active to avoid masking regular clients started before |
| 955 | if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD |
| 956 | || ((isA11yOnTop || rttCallActive) && isAssistant))) { |
| 957 | if (isPrivacySensitive) { |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 958 | // if audio mode is IN_COMMUNICATION, make sure the audio mode owner |
| 959 | // is marked latest sensitive active even if another app qualifies. |
| 960 | if (current->startTimeNs > latestSensitiveStartNs |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 961 | || (isInCommunication && currentUid == mPhoneStateOwnerUid)) { |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 962 | if (!isInCommunication || latestSensitiveActiveOrComm == nullptr |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 963 | || VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t( |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 964 | latestSensitiveActiveOrComm->attributionSource.uid)) |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 965 | != mPhoneStateOwnerUid) { |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 966 | latestSensitiveActiveOrComm = current; |
| 967 | latestSensitiveStartNs = current->startTimeNs; |
| 968 | } |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 969 | } |
| 970 | isSensitiveActive = true; |
| 971 | } else { |
| 972 | if (current->startTimeNs > latestStartNs) { |
| 973 | latestActive = current; |
| 974 | latestStartNs = current->startTimeNs; |
| 975 | } |
| 976 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 977 | } |
| 978 | } |
François Gaffie | db1a441 | 2023-10-10 17:32:33 +0000 | [diff] [blame] | 979 | if (current->attributes.source != AUDIO_SOURCE_HOTWORD && |
| 980 | !isVirtualSource(current->attributes.source)) { |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 981 | onlyHotwordActive = false; |
| 982 | } |
Eric Laurent | b0eff0f | 2021-11-09 16:05:49 +0100 | [diff] [blame] | 983 | if (currentUid == mPhoneStateOwnerUid && |
| 984 | !isVirtualSource(current->attributes.source)) { |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 985 | isPhoneStateOwnerActive = true; |
| 986 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 987 | } |
| 988 | |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 989 | // if no active client with UI on Top, consider latest active as top |
| 990 | if (topActive == nullptr) { |
| 991 | topActive = latestActive; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 992 | topStartNs = latestStartNs; |
| 993 | } |
| 994 | if (topSensitiveActive == nullptr) { |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 995 | topSensitiveActive = latestSensitiveActiveOrComm; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 996 | topSensitiveStartNs = latestSensitiveStartNs; |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 997 | } else if (latestSensitiveActiveOrComm != nullptr) { |
| 998 | // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with |
| 999 | // foreground UI in case both are capturing with privacy sensitive flag. |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1000 | uid_t latestActiveUid = VALUE_OR_FATAL( |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1001 | aidl2legacy_int32_t_uid_t(latestSensitiveActiveOrComm->attributionSource.uid)); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1002 | if (isInCommunication && latestActiveUid == mPhoneStateOwnerUid) { |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 1003 | topSensitiveActive = latestSensitiveActiveOrComm; |
| 1004 | topSensitiveStartNs = latestSensitiveStartNs; |
| 1005 | } |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | // If both privacy sensitive and regular capture are active: |
| 1009 | // if the regular capture is privileged |
| 1010 | // allow concurrency |
| 1011 | // else |
| 1012 | // favor the privacy sensitive case |
| 1013 | if (topActive != nullptr && topSensitiveActive != nullptr |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 1014 | && !topActive->canCaptureOutput) { |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 1015 | topActive = nullptr; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | for (size_t i =0; i < mAudioRecordClients.size(); i++) { |
| 1019 | sp<AudioRecordClient> current = mAudioRecordClients[i]; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1020 | uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t( |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1021 | current->attributionSource.uid)); |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1022 | if (!current->active) { |
| 1023 | continue; |
| 1024 | } |
| 1025 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1026 | audio_source_t source = current->attributes.source; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1027 | bool isTopOrLatestActive = topActive == nullptr ? false : |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1028 | current->attributionSource.uid == topActive->attributionSource.uid; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1029 | bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? false : |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1030 | current->attributionSource.uid == topSensitiveActive->attributionSource.uid; |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 1031 | bool isTopOrLatestAssistant = latestActiveAssistant == nullptr ? false : |
| 1032 | current->attributionSource.uid == latestActiveAssistant->attributionSource.uid; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 1033 | |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1034 | auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mMutex) { |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1035 | uid_t recordUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t( |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1036 | recordClient->attributionSource.uid)); |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 1037 | bool canCaptureCall = recordClient->canCaptureOutput; |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 1038 | bool canCaptureCommunication = recordClient->canCaptureOutput |
| 1039 | || !isPhoneStateOwnerActive |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1040 | || recordUid == mPhoneStateOwnerUid; |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 1041 | return !(isInCall && !canCaptureCall) |
| 1042 | && !(isInCommunication && !canCaptureCommunication); |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 1043 | }; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1044 | |
| 1045 | // By default allow capture if: |
| 1046 | // The assistant is not on TOP |
Eric Laurent | a171e35 | 2019-05-07 13:04:45 -0700 | [diff] [blame] | 1047 | // AND is on TOP or latest started |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1048 | // AND there is no active privacy sensitive capture or call |
| 1049 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | 4ab1841 | 2022-11-07 16:25:32 +0100 | [diff] [blame] | 1050 | bool allowSensitiveCapture = |
| 1051 | !isSensitiveActive || isTopOrLatestSensitive || current->canCaptureOutput; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1052 | bool allowCapture = !isAssistantOnTop |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 1053 | && (isTopOrLatestActive || isTopOrLatestSensitive) |
Eric Laurent | 4ab1841 | 2022-11-07 16:25:32 +0100 | [diff] [blame] | 1054 | && allowSensitiveCapture |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 1055 | && canCaptureIfInCallOrCommunication(current); |
Eric Laurent | 2dc962b | 2019-03-01 08:25:25 -0800 | [diff] [blame] | 1056 | |
Eric Laurent | ed726cc | 2021-07-01 14:26:41 +0200 | [diff] [blame] | 1057 | if (!current->hasOp()) { |
| 1058 | // Never allow capture if app op is denied |
| 1059 | allowCapture = false; |
| 1060 | } else if (isVirtualSource(source)) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1061 | // Allow capture for virtual (remote submix, call audio TX or RX...) sources |
| 1062 | allowCapture = true; |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 1063 | } else if (!useActiveAssistantList && mUidPolicy->isAssistantUid(currentUid)) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1064 | // For assistant allow capture if: |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 1065 | // Active assistant list is not being used |
| 1066 | // AND accessibility service is on TOP or a RTT call is active |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1067 | // AND the source is VOICE_RECOGNITION or HOTWORD |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 1068 | // OR there is no active privacy sensitive capture or call |
| 1069 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
| 1070 | // AND is latest TOP assistant AND |
| 1071 | // uses VOICE_RECOGNITION OR uses HOTWORD |
| 1072 | // OR there is no TOP assistant and uses HOTWORD |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 1073 | if (isA11yOnTop || rttCallActive) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1074 | if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1075 | allowCapture = true; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1076 | } |
Eric Laurent | 4ab1841 | 2022-11-07 16:25:32 +0100 | [diff] [blame] | 1077 | } else if (allowSensitiveCapture |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 1078 | && canCaptureIfInCallOrCommunication(current)) { |
| 1079 | if (isTopOrLatestAssistant |
| 1080 | && (source == AUDIO_SOURCE_VOICE_RECOGNITION |
| 1081 | || source == AUDIO_SOURCE_HOTWORD)) { |
| 1082 | allowCapture = true; |
| 1083 | } else if (!isAssistantOnTop && (source == AUDIO_SOURCE_HOTWORD)) { |
| 1084 | allowCapture = true; |
| 1085 | } |
| 1086 | } |
| 1087 | } else if (useActiveAssistantList && mUidPolicy->isActiveAssistantUid(currentUid)) { |
| 1088 | // For assistant on active list and on top allow capture if: |
| 1089 | // An accessibility service is on TOP |
| 1090 | // AND the source is VOICE_RECOGNITION or HOTWORD |
| 1091 | // OR there is no active privacy sensitive capture or call |
| 1092 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
| 1093 | // AND uses VOICE_RECOGNITION OR uses HOTWORD |
| 1094 | if (isA11yOnTop) { |
| 1095 | if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) { |
| 1096 | allowCapture = true; |
| 1097 | } |
Eric Laurent | 4ab1841 | 2022-11-07 16:25:32 +0100 | [diff] [blame] | 1098 | } else if (allowSensitiveCapture |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 1099 | && canCaptureIfInCallOrCommunication(current)) { |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 1100 | if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) || (source == AUDIO_SOURCE_HOTWORD)) |
| 1101 | { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1102 | allowCapture = true; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1103 | } |
| 1104 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1105 | } else if (mUidPolicy->isA11yUid(currentUid)) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1106 | // For accessibility service allow capture if: |
Eric Laurent | 47670c9 | 2019-08-28 16:59:05 -0700 | [diff] [blame] | 1107 | // The assistant is not on TOP |
| 1108 | // AND there is no active privacy sensitive capture or call |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 1109 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | 47670c9 | 2019-08-28 16:59:05 -0700 | [diff] [blame] | 1110 | // OR |
| 1111 | // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD |
| 1112 | if (!isAssistantOnTop |
Eric Laurent | 4ab1841 | 2022-11-07 16:25:32 +0100 | [diff] [blame] | 1113 | && allowSensitiveCapture |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 1114 | && canCaptureIfInCallOrCommunication(current)) { |
Eric Laurent | 47670c9 | 2019-08-28 16:59:05 -0700 | [diff] [blame] | 1115 | allowCapture = true; |
| 1116 | } |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 1117 | if (isA11yOnTop) { |
| 1118 | if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) { |
| 1119 | allowCapture = true; |
| 1120 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1121 | } |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 1122 | } else if (source == AUDIO_SOURCE_HOTWORD) { |
| 1123 | // For HOTWORD source allow capture when not on TOP if: |
| 1124 | // All active clients are using HOTWORD source |
| 1125 | // AND no call is active |
| 1126 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 1127 | if (onlyHotwordActive |
| 1128 | && canCaptureIfInCallOrCommunication(current)) { |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 1129 | allowCapture = true; |
| 1130 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1131 | } else if (mUidPolicy->isCurrentImeUid(currentUid)) { |
Kohsuke Yatoh | a623a13 | 2020-03-24 20:10:26 -0700 | [diff] [blame] | 1132 | // For current InputMethodService allow capture if: |
| 1133 | // A RTT call is active AND the source is VOICE_RECOGNITION |
| 1134 | if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) { |
| 1135 | allowCapture = true; |
| 1136 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1137 | } |
Eric Laurent | 8c7ef89 | 2021-06-10 13:32:16 +0200 | [diff] [blame] | 1138 | setAppState_l(current, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1139 | allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(currentUid)) : |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1140 | APP_STATE_IDLE); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 1144 | void AudioPolicyService::silenceAllRecordings_l() { |
| 1145 | for (size_t i = 0; i < mAudioRecordClients.size(); i++) { |
| 1146 | sp<AudioRecordClient> current = mAudioRecordClients[i]; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1147 | if (!isVirtualSource(current->attributes.source)) { |
Eric Laurent | 8c7ef89 | 2021-06-10 13:32:16 +0200 | [diff] [blame] | 1148 | setAppState_l(current, APP_STATE_IDLE); |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1149 | } |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1153 | /* static */ |
| 1154 | app_state_t AudioPolicyService::apmStatFromAmState(int amState) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1155 | |
| 1156 | if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1157 | return APP_STATE_IDLE; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 1158 | } else if (amState <= ActivityManager::PROCESS_STATE_TOP) { |
| 1159 | // include persistent services |
| 1160 | return APP_STATE_TOP; |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1161 | } |
| 1162 | return APP_STATE_FOREGROUND; |
| 1163 | } |
| 1164 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1165 | /* static */ |
Eric Laurent | 2dc962b | 2019-03-01 08:25:25 -0800 | [diff] [blame] | 1166 | bool AudioPolicyService::isVirtualSource(audio_source_t source) |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1167 | { |
| 1168 | switch (source) { |
| 1169 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 1170 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 1171 | case AUDIO_SOURCE_VOICE_CALL: |
Eric Laurent | 2dc962b | 2019-03-01 08:25:25 -0800 | [diff] [blame] | 1172 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
| 1173 | case AUDIO_SOURCE_FM_TUNER: |
Eric Laurent | 68eb212 | 2020-04-30 17:40:57 -0700 | [diff] [blame] | 1174 | case AUDIO_SOURCE_ECHO_REFERENCE: |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1175 | return true; |
| 1176 | default: |
| 1177 | break; |
| 1178 | } |
| 1179 | return false; |
| 1180 | } |
| 1181 | |
Eric Laurent | 8c7ef89 | 2021-06-10 13:32:16 +0200 | [diff] [blame] | 1182 | void AudioPolicyService::setAppState_l(sp<AudioRecordClient> client, app_state_t state) |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1183 | { |
| 1184 | AutoCallerClear acc; |
| 1185 | |
| 1186 | if (mAudioPolicyManager) { |
Eric Laurent | 8c7ef89 | 2021-06-10 13:32:16 +0200 | [diff] [blame] | 1187 | mAudioPolicyManager->setAppState(client->portId, state); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1188 | } |
| 1189 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1190 | if (af) { |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 1191 | bool silenced = state == APP_STATE_IDLE; |
Eric Laurent | 8c7ef89 | 2021-06-10 13:32:16 +0200 | [diff] [blame] | 1192 | if (client->silenced != silenced) { |
| 1193 | if (client->active) { |
| 1194 | if (silenced) { |
| 1195 | finishRecording(client->attributionSource, client->attributes.source); |
| 1196 | } else { |
| 1197 | std::stringstream msg; |
| 1198 | msg << "Audio recording un-silenced on session " << client->session; |
| 1199 | if (!startRecording(client->attributionSource, String16(msg.str().c_str()), |
| 1200 | client->attributes.source)) { |
| 1201 | silenced = true; |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | af->setRecordSilenced(client->portId, silenced); |
| 1206 | client->silenced = silenced; |
| 1207 | } |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1208 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1211 | status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused) |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1212 | NO_THREAD_SAFETY_ANALYSIS // update for trylock. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1213 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 1214 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1215 | dumpPermissionDenial(fd); |
| 1216 | } else { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1217 | const bool locked = mMutex.try_lock(kDumpLockTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1218 | if (!locked) { |
| 1219 | String8 result(kDeadlockedString); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1220 | write(fd, result.c_str(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | dumpInternals(fd); |
Mikhail Naganov | 1b22e54 | 2022-02-25 04:24:49 +0000 | [diff] [blame] | 1224 | |
| 1225 | String8 actPtr = String8::format("AudioCommandThread: %p\n", mAudioCommandThread.get()); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1226 | write(fd, actPtr.c_str(), actPtr.size()); |
Glenn Kasten | 9d1f02d | 2012-02-08 17:47:58 -0800 | [diff] [blame] | 1227 | if (mAudioCommandThread != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1228 | mAudioCommandThread->dump(fd); |
| 1229 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1230 | |
Mikhail Naganov | 1b22e54 | 2022-02-25 04:24:49 +0000 | [diff] [blame] | 1231 | String8 octPtr = String8::format("OutputCommandThread: %p\n", mOutputCommandThread.get()); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1232 | write(fd, octPtr.c_str(), octPtr.size()); |
Mikhail Naganov | 1b22e54 | 2022-02-25 04:24:49 +0000 | [diff] [blame] | 1233 | if (mOutputCommandThread != 0) { |
| 1234 | mOutputCommandThread->dump(fd); |
| 1235 | } |
| 1236 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 1237 | if (mAudioPolicyManager) { |
| 1238 | mAudioPolicyManager->dump(fd); |
Mikhail Naganov | 1b22e54 | 2022-02-25 04:24:49 +0000 | [diff] [blame] | 1239 | } else { |
| 1240 | String8 apmPtr = String8::format("AudioPolicyManager: %p\n", mAudioPolicyManager); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1241 | write(fd, apmPtr.c_str(), apmPtr.size()); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 1242 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1243 | |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 1244 | mPackageManager.dump(fd); |
| 1245 | |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1246 | dumpReleaseLock(mMutex, locked); |
Andy Hung | c747c53 | 2022-03-07 21:41:14 -0800 | [diff] [blame] | 1247 | |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1248 | if (mSpatializer != nullptr) { |
| 1249 | std::string dumpString = mSpatializer->toString(1 /* level */); |
| 1250 | write(fd, dumpString.c_str(), dumpString.size()); |
| 1251 | } else { |
| 1252 | String8 spatializerPtr = String8::format("Spatializer no supportted on this device\n"); |
| 1253 | write(fd, spatializerPtr.c_str(), spatializerPtr.size()); |
| 1254 | } |
| 1255 | |
Andy Hung | c747c53 | 2022-03-07 21:41:14 -0800 | [diff] [blame] | 1256 | { |
| 1257 | std::string timeCheckStats = getIAudioPolicyServiceStatistics().dump(); |
| 1258 | dprintf(fd, "\nIAudioPolicyService binder call profile\n"); |
| 1259 | write(fd, timeCheckStats.c_str(), timeCheckStats.size()); |
| 1260 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1261 | } |
| 1262 | return NO_ERROR; |
| 1263 | } |
| 1264 | |
| 1265 | status_t AudioPolicyService::dumpPermissionDenial(int fd) |
| 1266 | { |
| 1267 | const size_t SIZE = 256; |
| 1268 | char buffer[SIZE]; |
| 1269 | String8 result; |
| 1270 | snprintf(buffer, SIZE, "Permission Denial: " |
| 1271 | "can't dump AudioPolicyService from pid=%d, uid=%d\n", |
| 1272 | IPCThreadState::self()->getCallingPid(), |
| 1273 | IPCThreadState::self()->getCallingUid()); |
| 1274 | result.append(buffer); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1275 | write(fd, result.c_str(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1276 | return NO_ERROR; |
| 1277 | } |
| 1278 | |
| 1279 | status_t AudioPolicyService::onTransact( |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1280 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1281 | // make sure transactions reserved to AudioFlinger do not come from other processes |
| 1282 | switch (code) { |
| 1283 | case TRANSACTION_startOutput: |
| 1284 | case TRANSACTION_stopOutput: |
| 1285 | case TRANSACTION_releaseOutput: |
| 1286 | case TRANSACTION_getInputForAttr: |
| 1287 | case TRANSACTION_startInput: |
| 1288 | case TRANSACTION_stopInput: |
| 1289 | case TRANSACTION_releaseInput: |
| 1290 | case TRANSACTION_getOutputForEffect: |
| 1291 | case TRANSACTION_registerEffect: |
| 1292 | case TRANSACTION_unregisterEffect: |
| 1293 | case TRANSACTION_setEffectEnabled: |
| 1294 | case TRANSACTION_getStrategyForStream: |
| 1295 | case TRANSACTION_getOutputForAttr: |
| 1296 | case TRANSACTION_moveEffectsToIo: |
| 1297 | ALOGW("%s: transaction %d received from PID %d", |
| 1298 | __func__, code, IPCThreadState::self()->getCallingPid()); |
| 1299 | return INVALID_OPERATION; |
| 1300 | default: |
| 1301 | break; |
| 1302 | } |
| 1303 | |
| 1304 | // make sure the following transactions come from system components |
| 1305 | switch (code) { |
| 1306 | case TRANSACTION_setDeviceConnectionState: |
| 1307 | case TRANSACTION_handleDeviceConfigChange: |
| 1308 | case TRANSACTION_setPhoneState: |
| 1309 | //FIXME: Allow setForceUse calls from system apps until a better use case routing API is available |
| 1310 | // case TRANSACTION_setForceUse: |
| 1311 | case TRANSACTION_initStreamVolume: |
| 1312 | case TRANSACTION_setStreamVolumeIndex: |
| 1313 | case TRANSACTION_setVolumeIndexForAttributes: |
| 1314 | case TRANSACTION_getStreamVolumeIndex: |
| 1315 | case TRANSACTION_getVolumeIndexForAttributes: |
| 1316 | case TRANSACTION_getMinVolumeIndexForAttributes: |
| 1317 | case TRANSACTION_getMaxVolumeIndexForAttributes: |
| 1318 | case TRANSACTION_isStreamActive: |
| 1319 | case TRANSACTION_isStreamActiveRemotely: |
| 1320 | case TRANSACTION_isSourceActive: |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1321 | case TRANSACTION_registerPolicyMixes: |
Jan Sebechlebsky | 0af8e87 | 2023-08-11 14:45:08 +0200 | [diff] [blame] | 1322 | case TRANSACTION_updatePolicyMixes: |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1323 | case TRANSACTION_setMasterMono: |
| 1324 | case TRANSACTION_getSurroundFormats: |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1325 | case TRANSACTION_getReportedSurroundFormats: |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1326 | case TRANSACTION_setSurroundFormatEnabled: |
Oscar Azucena | 829d90d | 2022-01-28 17:17:56 -0800 | [diff] [blame] | 1327 | case TRANSACTION_setAssistantServicesUids: |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 1328 | case TRANSACTION_setActiveAssistantServicesUids: |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1329 | case TRANSACTION_setA11yServicesUids: |
| 1330 | case TRANSACTION_setUidDeviceAffinities: |
| 1331 | case TRANSACTION_removeUidDeviceAffinities: |
| 1332 | case TRANSACTION_setUserIdDeviceAffinities: |
| 1333 | case TRANSACTION_removeUserIdDeviceAffinities: |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 1334 | case TRANSACTION_getHwOffloadFormatsSupportedForBluetoothMedia: |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1335 | case TRANSACTION_listAudioVolumeGroups: |
| 1336 | case TRANSACTION_getVolumeGroupFromAudioAttributes: |
| 1337 | case TRANSACTION_acquireSoundTriggerSession: |
| 1338 | case TRANSACTION_releaseSoundTriggerSession: |
Atneya Nair | 698f5ef | 2022-12-15 16:15:09 -0800 | [diff] [blame] | 1339 | case TRANSACTION_isHotwordStreamSupported: |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1340 | case TRANSACTION_setRttEnabled: |
| 1341 | case TRANSACTION_isCallScreenModeSupported: |
| 1342 | case TRANSACTION_setDevicesRoleForStrategy: |
| 1343 | case TRANSACTION_setSupportedSystemUsages: |
| 1344 | case TRANSACTION_removeDevicesRoleForStrategy: |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 1345 | case TRANSACTION_clearDevicesRoleForStrategy: |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1346 | case TRANSACTION_getDevicesForRoleAndStrategy: |
| 1347 | case TRANSACTION_getDevicesForAttributes: |
| 1348 | case TRANSACTION_setAllowedCapturePolicy: |
| 1349 | case TRANSACTION_onNewAudioModulesAvailable: |
| 1350 | case TRANSACTION_setCurrentImeUid: |
| 1351 | case TRANSACTION_registerSoundTriggerCaptureStateListener: |
| 1352 | case TRANSACTION_setDevicesRoleForCapturePreset: |
| 1353 | case TRANSACTION_addDevicesRoleForCapturePreset: |
| 1354 | case TRANSACTION_removeDevicesRoleForCapturePreset: |
| 1355 | case TRANSACTION_clearDevicesRoleForCapturePreset: |
Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1356 | case TRANSACTION_getDevicesForRoleAndCapturePreset: |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1357 | case TRANSACTION_getSpatializer: |
| 1358 | case TRANSACTION_setPreferredMixerAttributes: |
Marvin Ramin | bdefaf0 | 2023-11-01 09:10:32 +0100 | [diff] [blame] | 1359 | case TRANSACTION_clearPreferredMixerAttributes: |
| 1360 | case TRANSACTION_getRegisteredPolicyMixes: { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1361 | if (!isServiceUid(IPCThreadState::self()->getCallingUid())) { |
| 1362 | ALOGW("%s: transaction %d received from PID %d unauthorized UID %d", |
| 1363 | __func__, code, IPCThreadState::self()->getCallingPid(), |
| 1364 | IPCThreadState::self()->getCallingUid()); |
| 1365 | return INVALID_OPERATION; |
| 1366 | } |
| 1367 | } break; |
| 1368 | default: |
| 1369 | break; |
| 1370 | } |
| 1371 | |
Andy Hung | c747c53 | 2022-03-07 21:41:14 -0800 | [diff] [blame] | 1372 | const std::string methodName = getIAudioPolicyServiceStatistics().getMethodForCode(code); |
| 1373 | mediautils::TimeCheck check( |
| 1374 | std::string("IAudioPolicyService::").append(methodName), |
| 1375 | [code, methodName](bool timeout, float elapsedMs) { // don't move methodName. |
| 1376 | if (timeout) { |
| 1377 | mediametrics::LogItem(AMEDIAMETRICS_KEY_AUDIO_POLICY) |
| 1378 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_TIMEOUT) |
| 1379 | .set(AMEDIAMETRICS_PROP_METHODCODE, int64_t(code)) |
| 1380 | .set(AMEDIAMETRICS_PROP_METHODNAME, methodName.c_str()) |
| 1381 | .record(); |
| 1382 | } else { |
| 1383 | getIAudioPolicyServiceStatistics().event(code, elapsedMs); |
| 1384 | } |
Andy Hung | 741b3dd | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 1385 | }, mediautils::TimeCheck::kDefaultTimeoutDuration, |
| 1386 | mediautils::TimeCheck::kDefaultSecondChanceDuration, |
| 1387 | true /* crashOnTimeout */); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1388 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1389 | switch (code) { |
| 1390 | case SHELL_COMMAND_TRANSACTION: { |
| 1391 | int in = data.readFileDescriptor(); |
| 1392 | int out = data.readFileDescriptor(); |
| 1393 | int err = data.readFileDescriptor(); |
| 1394 | int argc = data.readInt32(); |
| 1395 | Vector<String16> args; |
| 1396 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 1397 | args.add(data.readString16()); |
| 1398 | } |
| 1399 | sp<IBinder> unusedCallback; |
| 1400 | sp<IResultReceiver> resultReceiver; |
| 1401 | status_t status; |
| 1402 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 1403 | return status; |
| 1404 | } |
| 1405 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 1406 | return status; |
| 1407 | } |
| 1408 | status = shellCommand(in, out, err, args); |
| 1409 | if (resultReceiver != nullptr) { |
| 1410 | resultReceiver->send(status); |
| 1411 | } |
| 1412 | return NO_ERROR; |
| 1413 | } |
| 1414 | } |
| 1415 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1416 | return BnAudioPolicyService::onTransact(code, data, reply, flags); |
| 1417 | } |
| 1418 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1419 | // ------------------- Shell command implementation ------------------- |
| 1420 | |
| 1421 | // NOTE: This is a remote API - make sure all args are validated |
| 1422 | status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) { |
| 1423 | if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) { |
| 1424 | return PERMISSION_DENIED; |
| 1425 | } |
| 1426 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 1427 | return BAD_VALUE; |
| 1428 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1429 | if (args.size() >= 3 && args[0] == String16("set-uid-state")) { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1430 | return handleSetUidState(args, err); |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1431 | } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1432 | return handleResetUidState(args, err); |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1433 | } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1434 | return handleGetUidState(args, out, err); |
Eric Laurent | 269acb4 | 2021-04-23 16:53:22 +0200 | [diff] [blame] | 1435 | } else if (args.size() >= 1 && args[0] == String16("purge_permission-cache")) { |
| 1436 | purgePermissionCache(); |
| 1437 | return NO_ERROR; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1438 | } else if (args.size() == 1 && args[0] == String16("help")) { |
| 1439 | printHelp(out); |
| 1440 | return NO_ERROR; |
| 1441 | } |
| 1442 | printHelp(err); |
| 1443 | return BAD_VALUE; |
| 1444 | } |
| 1445 | |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1446 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { |
| 1447 | if (userId < 0) { |
| 1448 | ALOGE("Invalid user: %d", userId); |
| 1449 | dprintf(err, "Invalid user: %d\n", userId); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1450 | return BAD_VALUE; |
| 1451 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1452 | |
| 1453 | PermissionController pc; |
| 1454 | uid = pc.getPackageUid(packageName, 0); |
| 1455 | if (uid <= 0) { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1456 | ALOGE("Unknown package: '%s'", String8(packageName).c_str()); |
| 1457 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).c_str()); |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1458 | return BAD_VALUE; |
| 1459 | } |
| 1460 | |
| 1461 | uid = multiuser_get_uid(userId, uid); |
| 1462 | return NO_ERROR; |
| 1463 | } |
| 1464 | |
| 1465 | status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) { |
| 1466 | // Valid arg.size() is 3 or 5, args.size() is 5 with --user option. |
| 1467 | if (!(args.size() == 3 || args.size() == 5)) { |
| 1468 | printHelp(err); |
| 1469 | return BAD_VALUE; |
| 1470 | } |
| 1471 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1472 | bool active = false; |
| 1473 | if (args[2] == String16("active")) { |
| 1474 | active = true; |
| 1475 | } else if ((args[2] != String16("idle"))) { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1476 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).c_str()); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1477 | return BAD_VALUE; |
| 1478 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1479 | |
| 1480 | int userId = 0; |
| 1481 | if (args.size() >= 5 && args[3] == String16("--user")) { |
| 1482 | userId = atoi(String8(args[4])); |
| 1483 | } |
| 1484 | |
| 1485 | uid_t uid; |
| 1486 | if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) { |
| 1487 | return BAD_VALUE; |
| 1488 | } |
| 1489 | |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1490 | sp<UidPolicy> uidPolicy; |
| 1491 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1492 | audio_utils::lock_guard _l(mMutex); |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1493 | uidPolicy = mUidPolicy; |
| 1494 | } |
| 1495 | if (uidPolicy) { |
| 1496 | uidPolicy->addOverrideUid(uid, active); |
| 1497 | return NO_ERROR; |
| 1498 | } |
| 1499 | return NO_INIT; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) { |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1503 | // Valid arg.size() is 2 or 4, args.size() is 4 with --user option. |
| 1504 | if (!(args.size() == 2 || args.size() == 4)) { |
| 1505 | printHelp(err); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1506 | return BAD_VALUE; |
| 1507 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1508 | |
| 1509 | int userId = 0; |
| 1510 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 1511 | userId = atoi(String8(args[3])); |
| 1512 | } |
| 1513 | |
| 1514 | uid_t uid; |
| 1515 | if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) { |
| 1516 | return BAD_VALUE; |
| 1517 | } |
| 1518 | |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1519 | sp<UidPolicy> uidPolicy; |
| 1520 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1521 | audio_utils::lock_guard _l(mMutex); |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1522 | uidPolicy = mUidPolicy; |
| 1523 | } |
| 1524 | if (uidPolicy) { |
| 1525 | uidPolicy->removeOverrideUid(uid); |
| 1526 | return NO_ERROR; |
| 1527 | } |
| 1528 | return NO_INIT; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) { |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1532 | // Valid arg.size() is 2 or 4, args.size() is 4 with --user option. |
| 1533 | if (!(args.size() == 2 || args.size() == 4)) { |
| 1534 | printHelp(err); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1535 | return BAD_VALUE; |
| 1536 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1537 | |
| 1538 | int userId = 0; |
| 1539 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 1540 | userId = atoi(String8(args[3])); |
| 1541 | } |
| 1542 | |
| 1543 | uid_t uid; |
| 1544 | if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) { |
| 1545 | return BAD_VALUE; |
| 1546 | } |
| 1547 | |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1548 | sp<UidPolicy> uidPolicy; |
| 1549 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1550 | audio_utils::lock_guard _l(mMutex); |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1551 | uidPolicy = mUidPolicy; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1552 | } |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1553 | if (uidPolicy) { |
| 1554 | return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n"); |
| 1555 | } |
| 1556 | return NO_INIT; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | status_t AudioPolicyService::printHelp(int out) { |
| 1560 | return dprintf(out, "Audio policy service commands:\n" |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1561 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 1562 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 1563 | " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n" |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1564 | " help print this message\n"); |
| 1565 | } |
| 1566 | |
Eric Laurent | 0d13fea | 2022-11-04 17:12:08 +0100 | [diff] [blame] | 1567 | status_t AudioPolicyService::registerOutput(audio_io_handle_t output, |
| 1568 | const audio_config_base_t& config, |
| 1569 | const audio_output_flags_t flags) { |
| 1570 | return mUsecaseValidator->registerStream(output, config, flags); |
| 1571 | } |
| 1572 | |
| 1573 | status_t AudioPolicyService::unregisterOutput(audio_io_handle_t output) { |
| 1574 | return mUsecaseValidator->unregisterStream(output); |
| 1575 | } |
| 1576 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1577 | // ----------- AudioPolicyService::UidPolicy implementation ---------- |
| 1578 | |
| 1579 | void AudioPolicyService::UidPolicy::registerSelf() { |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 1580 | status_t res = mAm.linkToDeath(this); |
| 1581 | mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1582 | | ActivityManager::UID_OBSERVER_IDLE |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1583 | | ActivityManager::UID_OBSERVER_ACTIVE |
| 1584 | | ActivityManager::UID_OBSERVER_PROCSTATE, |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1585 | ActivityManager::PROCESS_STATE_UNKNOWN, |
| 1586 | String16("audioserver")); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1587 | if (!res) { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1588 | audio_utils::lock_guard _l(mMutex); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1589 | mObserverRegistered = true; |
| 1590 | } else { |
| 1591 | ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1592 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 1593 | mAm.unregisterUidObserver(this); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1594 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | void AudioPolicyService::UidPolicy::unregisterSelf() { |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 1598 | mAm.unlinkToDeath(this); |
| 1599 | mAm.unregisterUidObserver(this); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1600 | audio_utils::lock_guard _l(mMutex); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1601 | mObserverRegistered = false; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1602 | } |
| 1603 | |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1604 | void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1605 | audio_utils::lock_guard _l(mMutex); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1606 | mCachedUids.clear(); |
| 1607 | mObserverRegistered = false; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1610 | void AudioPolicyService::UidPolicy::checkRegistered() { |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1611 | bool needToReregister = false; |
| 1612 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1613 | audio_utils::lock_guard _l(mMutex); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1614 | needToReregister = !mObserverRegistered; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1615 | } |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1616 | if (needToReregister) { |
| 1617 | // Looks like ActivityManager has died previously, attempt to re-register. |
| 1618 | registerSelf(); |
| 1619 | } |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) { |
| 1623 | if (isServiceUid(uid)) return true; |
| 1624 | checkRegistered(); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1625 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1626 | audio_utils::lock_guard _l(mMutex); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1627 | auto overrideIter = mOverrideUids.find(uid); |
| 1628 | if (overrideIter != mOverrideUids.end()) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1629 | return overrideIter->second.first; |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1630 | } |
| 1631 | // In an absense of the ActivityManager, assume everything to be active. |
| 1632 | if (!mObserverRegistered) return true; |
| 1633 | auto cacheIter = mCachedUids.find(uid); |
Mikhail Naganov | eba668a | 2018-04-05 08:13:15 -0700 | [diff] [blame] | 1634 | if (cacheIter != mCachedUids.end()) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1635 | return cacheIter->second.first; |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1636 | } |
| 1637 | } |
| 1638 | ActivityManager am; |
Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 1639 | bool active = am.isUidActive(uid, String16("audioserver")); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1640 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1641 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1642 | mCachedUids.insert(std::pair<uid_t, |
| 1643 | std::pair<bool, int>>(uid, std::pair<bool, int>(active, |
| 1644 | ActivityManager::PROCESS_STATE_UNKNOWN))); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1645 | } |
| 1646 | return active; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1647 | } |
| 1648 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1649 | int AudioPolicyService::UidPolicy::getUidState(uid_t uid) { |
| 1650 | if (isServiceUid(uid)) { |
| 1651 | return ActivityManager::PROCESS_STATE_TOP; |
| 1652 | } |
| 1653 | checkRegistered(); |
| 1654 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1655 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1656 | auto overrideIter = mOverrideUids.find(uid); |
| 1657 | if (overrideIter != mOverrideUids.end()) { |
| 1658 | if (overrideIter->second.first) { |
| 1659 | if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) { |
| 1660 | return overrideIter->second.second; |
| 1661 | } else { |
| 1662 | auto cacheIter = mCachedUids.find(uid); |
| 1663 | if (cacheIter != mCachedUids.end()) { |
| 1664 | return cacheIter->second.second; |
| 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | return ActivityManager::PROCESS_STATE_UNKNOWN; |
| 1669 | } |
| 1670 | // In an absense of the ActivityManager, assume everything to be active. |
| 1671 | if (!mObserverRegistered) { |
| 1672 | return ActivityManager::PROCESS_STATE_TOP; |
| 1673 | } |
| 1674 | auto cacheIter = mCachedUids.find(uid); |
| 1675 | if (cacheIter != mCachedUids.end()) { |
| 1676 | if (cacheIter->second.first) { |
| 1677 | return cacheIter->second.second; |
| 1678 | } else { |
| 1679 | return ActivityManager::PROCESS_STATE_UNKNOWN; |
| 1680 | } |
| 1681 | } |
| 1682 | } |
| 1683 | ActivityManager am; |
Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 1684 | bool active = am.isUidActive(uid, String16("audioserver")); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1685 | int state = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 1686 | if (active) { |
| 1687 | state = am.getUidProcessState(uid, String16("audioserver")); |
| 1688 | } |
| 1689 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1690 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1691 | mCachedUids.insert(std::pair<uid_t, |
| 1692 | std::pair<bool, int>>(uid, std::pair<bool, int>(active, state))); |
| 1693 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1694 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1695 | return state; |
| 1696 | } |
| 1697 | |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1698 | void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1699 | updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1703 | updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1707 | updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1708 | } |
| 1709 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1710 | void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid, |
| 1711 | int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 1712 | int64_t procStateSeq __unused, |
| 1713 | int32_t capability __unused) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1714 | if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) { |
| 1715 | updateUid(&mCachedUids, uid, true, procState, true); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1716 | } |
| 1717 | } |
| 1718 | |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame] | 1719 | void AudioPolicyService::UidPolicy::onUidProcAdjChanged(uid_t uid __unused, int32_t adj __unused) { |
Austin Borger | 6557768 | 2022-02-17 00:25:43 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1722 | void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1723 | updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert); |
| 1724 | } |
| 1725 | |
| 1726 | void AudioPolicyService::UidPolicy::notifyService() { |
| 1727 | sp<AudioPolicyService> service = mService.promote(); |
| 1728 | if (service != nullptr) { |
| 1729 | service->updateUidStates(); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1730 | } |
| 1731 | } |
| 1732 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1733 | void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t, |
| 1734 | std::pair<bool, int>> *uids, |
| 1735 | uid_t uid, |
| 1736 | bool active, |
| 1737 | int state, |
| 1738 | bool insert) { |
| 1739 | if (isServiceUid(uid)) { |
| 1740 | return; |
| 1741 | } |
| 1742 | bool wasActive = isUidActive(uid); |
| 1743 | int previousState = getUidState(uid); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1744 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1745 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1746 | updateUidLocked(uids, uid, active, state, insert); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1747 | } |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1748 | if (wasActive != isUidActive(uid) || state != previousState) { |
| 1749 | notifyService(); |
| 1750 | } |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1753 | void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t, |
| 1754 | std::pair<bool, int>> *uids, |
| 1755 | uid_t uid, |
| 1756 | bool active, |
| 1757 | int state, |
| 1758 | bool insert) { |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1759 | auto it = uids->find(uid); |
| 1760 | if (it != uids->end()) { |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1761 | if (insert) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1762 | if (state == ActivityManager::PROCESS_STATE_UNKNOWN) { |
| 1763 | it->second.first = active; |
| 1764 | } |
| 1765 | if (it->second.first) { |
| 1766 | it->second.second = state; |
| 1767 | } else { |
| 1768 | it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 1769 | } |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1770 | } else { |
| 1771 | uids->erase(it); |
| 1772 | } |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1773 | } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) { |
| 1774 | uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid, |
| 1775 | std::pair<bool, int>(active, state))); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1776 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1777 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1778 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1779 | bool AudioPolicyService::UidPolicy::isA11yOnTop() { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1780 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1781 | for (const auto &uid : mCachedUids) { |
Eric Laurent | 47670c9 | 2019-08-28 16:59:05 -0700 | [diff] [blame] | 1782 | if (!isA11yUid(uid.first)) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1783 | continue; |
| 1784 | } |
Amith Yamasani | bcbb300 | 2019-01-23 13:53:33 -0800 | [diff] [blame] | 1785 | if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP |
| 1786 | && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1787 | return true; |
| 1788 | } |
| 1789 | } |
| 1790 | return false; |
| 1791 | } |
| 1792 | |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1793 | bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid) |
| 1794 | { |
| 1795 | std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid); |
| 1796 | return it != mA11yUids.end(); |
| 1797 | } |
| 1798 | |
Oscar Azucena | 829d90d | 2022-01-28 17:17:56 -0800 | [diff] [blame] | 1799 | void AudioPolicyService::UidPolicy::setAssistantUids(const std::vector<uid_t>& uids) { |
| 1800 | mAssistantUids.clear(); |
| 1801 | mAssistantUids = uids; |
| 1802 | } |
| 1803 | |
| 1804 | bool AudioPolicyService::UidPolicy::isAssistantUid(uid_t uid) |
| 1805 | { |
| 1806 | std::vector<uid_t>::iterator it = find(mAssistantUids.begin(), mAssistantUids.end(), uid); |
| 1807 | return it != mAssistantUids.end(); |
| 1808 | } |
| 1809 | |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 1810 | void AudioPolicyService::UidPolicy::setActiveAssistantUids(const std::vector<uid_t>& activeUids) { |
| 1811 | mActiveAssistantUids = activeUids; |
| 1812 | } |
| 1813 | |
| 1814 | bool AudioPolicyService::UidPolicy::isActiveAssistantUid(uid_t uid) |
| 1815 | { |
| 1816 | std::vector<uid_t>::iterator it = find(mActiveAssistantUids.begin(), |
| 1817 | mActiveAssistantUids.end(), uid); |
| 1818 | return it != mActiveAssistantUids.end(); |
| 1819 | } |
| 1820 | |
Oscar Azucena | 829d90d | 2022-01-28 17:17:56 -0800 | [diff] [blame] | 1821 | void AudioPolicyService::UidPolicy::dumpInternals(int fd) { |
| 1822 | const size_t SIZE = 256; |
| 1823 | char buffer[SIZE]; |
| 1824 | String8 result; |
| 1825 | auto appendUidsToResult = [&](const char* title, const std::vector<uid_t> &uids) { |
| 1826 | snprintf(buffer, SIZE, "\t%s: \n", title); |
| 1827 | result.append(buffer); |
| 1828 | int counter = 0; |
| 1829 | if (uids.empty()) { |
| 1830 | snprintf(buffer, SIZE, "\t\tNo UIDs present.\n"); |
| 1831 | result.append(buffer); |
| 1832 | return; |
| 1833 | } |
| 1834 | for (const auto &uid : uids) { |
| 1835 | snprintf(buffer, SIZE, "\t\tUID[%d]=%d\n", counter++, uid); |
| 1836 | result.append(buffer); |
| 1837 | } |
| 1838 | }; |
| 1839 | |
| 1840 | snprintf(buffer, SIZE, "UID Policy:\n"); |
| 1841 | result.append(buffer); |
| 1842 | snprintf(buffer, SIZE, "\tmObserverRegistered=%s\n",(mObserverRegistered ? "True":"False")); |
| 1843 | result.append(buffer); |
| 1844 | |
| 1845 | appendUidsToResult("Assistants UIDs", mAssistantUids); |
Oscar Azucena | c2cdda3 | 2022-01-31 19:10:39 -0800 | [diff] [blame] | 1846 | appendUidsToResult("Active Assistants UIDs", mActiveAssistantUids); |
Oscar Azucena | 829d90d | 2022-01-28 17:17:56 -0800 | [diff] [blame] | 1847 | |
| 1848 | appendUidsToResult("Accessibility UIDs", mA11yUids); |
| 1849 | |
| 1850 | snprintf(buffer, SIZE, "\tInput Method Service UID=%d\n", mCurrentImeUid); |
| 1851 | result.append(buffer); |
| 1852 | |
| 1853 | snprintf(buffer, SIZE, "\tIs RTT Enabled: %s\n", (mRttEnabled ? "True":"False")); |
| 1854 | result.append(buffer); |
| 1855 | |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1856 | write(fd, result.c_str(), result.size()); |
Oscar Azucena | 829d90d | 2022-01-28 17:17:56 -0800 | [diff] [blame] | 1857 | } |
| 1858 | |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 1859 | // ----------- AudioPolicyService::SensorPrivacyService implementation ---------- |
| 1860 | void AudioPolicyService::SensorPrivacyPolicy::registerSelf() { |
| 1861 | SensorPrivacyManager spm; |
| 1862 | mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled(); |
| 1863 | spm.addSensorPrivacyListener(this); |
| 1864 | } |
| 1865 | |
| 1866 | void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() { |
| 1867 | SensorPrivacyManager spm; |
| 1868 | spm.removeSensorPrivacyListener(this); |
| 1869 | } |
| 1870 | |
| 1871 | bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
| 1872 | return mSensorPrivacyEnabled; |
| 1873 | } |
| 1874 | |
Evan Severson | d8dc683 | 2022-01-27 10:47:03 -0800 | [diff] [blame] | 1875 | binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged( |
| 1876 | int toggleType __unused, int sensor __unused, bool enabled) { |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 1877 | mSensorPrivacyEnabled = enabled; |
| 1878 | sp<AudioPolicyService> service = mService.promote(); |
| 1879 | if (service != nullptr) { |
| 1880 | service->updateUidStates(); |
| 1881 | } |
| 1882 | return binder::Status::ok(); |
| 1883 | } |
| 1884 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1885 | // ----------- AudioPolicyService::AudioCommandThread implementation ---------- |
| 1886 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1887 | AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name, |
| 1888 | const wp<AudioPolicyService>& service) |
| 1889 | : Thread(false), mName(name), mService(service) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1890 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | |
| 1894 | AudioPolicyService::AudioCommandThread::~AudioCommandThread() |
| 1895 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1896 | if (!mAudioCommands.isEmpty()) { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1897 | release_wake_lock(mName.c_str()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1898 | } |
| 1899 | mAudioCommands.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | void AudioPolicyService::AudioCommandThread::onFirstRef() |
| 1903 | { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1904 | run(mName.c_str(), ANDROID_PRIORITY_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | bool AudioPolicyService::AudioCommandThread::threadLoop() |
| 1908 | { |
Eric Laurent | d7eda8d | 2016-02-02 17:18:39 -0800 | [diff] [blame] | 1909 | nsecs_t waitTime = -1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1910 | |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1911 | audio_utils::unique_lock ul(mMutex); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1912 | while (!exitPending()) |
| 1913 | { |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1914 | sp<AudioPolicyService> svc; |
Ytai Ben-Tsvi | 6958b02 | 2022-04-26 15:45:53 -0700 | [diff] [blame] | 1915 | int numTimesBecameEmpty = 0; |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1916 | while (!mAudioCommands.isEmpty() && !exitPending()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1917 | nsecs_t curTime = systemTime(); |
| 1918 | // commands are sorted by increasing time stamp: execute them from index 0 and up |
| 1919 | if (mAudioCommands[0]->mTime <= curTime) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1920 | sp<AudioCommand> command = mAudioCommands[0]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1921 | mAudioCommands.removeAt(0); |
Ytai Ben-Tsvi | 6958b02 | 2022-04-26 15:45:53 -0700 | [diff] [blame] | 1922 | if (mAudioCommands.isEmpty()) { |
| 1923 | ++numTimesBecameEmpty; |
| 1924 | } |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1925 | mLastCommand = command; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1926 | |
| 1927 | switch (command->mCommand) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1928 | case SET_VOLUME: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1929 | VolumeData *data = (VolumeData *)command->mParam.get(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1930 | ALOGV("AudioCommandThread() processing set volume stream %d, \ |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1931 | volume %f, output %d", data->mStream, data->mVolume, data->mIO); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1932 | ul.unlock(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1933 | command->mStatus = AudioSystem::setStreamVolume(data->mStream, |
| 1934 | data->mVolume, |
| 1935 | data->mIO); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1936 | ul.lock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1937 | }break; |
| 1938 | case SET_PARAMETERS: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1939 | ParametersData *data = (ParametersData *)command->mParam.get(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1940 | ALOGV("AudioCommandThread() processing set parameters string %s, io %d", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1941 | data->mKeyValuePairs.c_str(), data->mIO); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1942 | ul.unlock(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1943 | command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1944 | ul.lock(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1945 | }break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1946 | case SET_VOICE_VOLUME: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1947 | VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1948 | ALOGV("AudioCommandThread() processing set voice volume volume %f", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1949 | data->mVolume); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1950 | ul.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1951 | command->mStatus = AudioSystem::setVoiceVolume(data->mVolume); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1952 | ul.lock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1953 | }break; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1954 | case STOP_OUTPUT: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1955 | StopOutputData *data = (StopOutputData *)command->mParam.get(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1956 | ALOGV("AudioCommandThread() processing stop output portId %d", |
| 1957 | data->mPortId); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1958 | svc = mService.promote(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1959 | if (svc == 0) { |
| 1960 | break; |
| 1961 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1962 | ul.unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1963 | svc->doStopOutput(data->mPortId); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1964 | ul.lock(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1965 | }break; |
| 1966 | case RELEASE_OUTPUT: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1967 | ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1968 | ALOGV("AudioCommandThread() processing release output portId %d", |
| 1969 | data->mPortId); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1970 | svc = mService.promote(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1971 | if (svc == 0) { |
| 1972 | break; |
| 1973 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1974 | ul.unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1975 | svc->doReleaseOutput(data->mPortId); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1976 | ul.lock(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1977 | }break; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1978 | case CREATE_AUDIO_PATCH: { |
| 1979 | CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get(); |
| 1980 | ALOGV("AudioCommandThread() processing create audio patch"); |
| 1981 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1982 | if (af == 0) { |
| 1983 | command->mStatus = PERMISSION_DENIED; |
| 1984 | } else { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1985 | ul.unlock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1986 | command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1987 | ul.lock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1988 | } |
| 1989 | } break; |
| 1990 | case RELEASE_AUDIO_PATCH: { |
| 1991 | ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get(); |
| 1992 | ALOGV("AudioCommandThread() processing release audio patch"); |
| 1993 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1994 | if (af == 0) { |
| 1995 | command->mStatus = PERMISSION_DENIED; |
| 1996 | } else { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1997 | ul.unlock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1998 | command->mStatus = af->releaseAudioPatch(data->mHandle); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1999 | ul.lock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2000 | } |
| 2001 | } break; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2002 | case UPDATE_AUDIOPORT_LIST: { |
| 2003 | ALOGV("AudioCommandThread() processing update audio port list"); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 2004 | svc = mService.promote(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2005 | if (svc == 0) { |
| 2006 | break; |
| 2007 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2008 | ul.unlock(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2009 | svc->doOnAudioPortListUpdate(); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2010 | ul.lock(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2011 | }break; |
| 2012 | case UPDATE_AUDIOPATCH_LIST: { |
| 2013 | ALOGV("AudioCommandThread() processing update audio patch list"); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 2014 | svc = mService.promote(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2015 | if (svc == 0) { |
| 2016 | break; |
| 2017 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2018 | ul.unlock(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2019 | svc->doOnAudioPatchListUpdate(); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2020 | ul.lock(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2021 | }break; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2022 | case CHANGED_AUDIOVOLUMEGROUP: { |
| 2023 | AudioVolumeGroupData *data = |
| 2024 | static_cast<AudioVolumeGroupData *>(command->mParam.get()); |
| 2025 | ALOGV("AudioCommandThread() processing update audio volume group"); |
| 2026 | svc = mService.promote(); |
| 2027 | if (svc == 0) { |
| 2028 | break; |
| 2029 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2030 | ul.unlock(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2031 | svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2032 | ul.lock(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2033 | }break; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2034 | case SET_AUDIOPORT_CONFIG: { |
| 2035 | SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get(); |
| 2036 | ALOGV("AudioCommandThread() processing set port config"); |
| 2037 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 2038 | if (af == 0) { |
| 2039 | command->mStatus = PERMISSION_DENIED; |
| 2040 | } else { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2041 | ul.unlock(); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2042 | command->mStatus = af->setAudioPortConfig(&data->mConfig); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2043 | ul.lock(); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2044 | } |
| 2045 | } break; |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2046 | case DYN_POLICY_MIX_STATE_UPDATE: { |
| 2047 | DynPolicyMixStateUpdateData *data = |
| 2048 | (DynPolicyMixStateUpdateData *)command->mParam.get(); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2049 | ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2050 | data->mRegId.c_str(), data->mState); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2051 | svc = mService.promote(); |
| 2052 | if (svc == 0) { |
| 2053 | break; |
| 2054 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2055 | ul.unlock(); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2056 | svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2057 | ul.lock(); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2058 | } break; |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2059 | case RECORDING_CONFIGURATION_UPDATE: { |
| 2060 | RecordingConfigurationUpdateData *data = |
| 2061 | (RecordingConfigurationUpdateData *)command->mParam.get(); |
| 2062 | ALOGV("AudioCommandThread() processing recording configuration update"); |
| 2063 | svc = mService.promote(); |
| 2064 | if (svc == 0) { |
| 2065 | break; |
| 2066 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2067 | ul.unlock(); |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 2068 | svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo, |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 2069 | &data->mClientConfig, data->mClientEffects, |
| 2070 | &data->mDeviceConfig, data->mEffects, |
| 2071 | data->mPatchHandle, data->mSource); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2072 | ul.lock(); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2073 | } break; |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 2074 | case SET_EFFECT_SUSPENDED: { |
| 2075 | SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get(); |
| 2076 | ALOGV("AudioCommandThread() processing set effect suspended"); |
| 2077 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 2078 | if (af != 0) { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2079 | ul.unlock(); |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 2080 | af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2081 | ul.lock(); |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 2082 | } |
| 2083 | } break; |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 2084 | case AUDIO_MODULES_UPDATE: { |
| 2085 | ALOGV("AudioCommandThread() processing audio modules update"); |
| 2086 | svc = mService.promote(); |
| 2087 | if (svc == 0) { |
| 2088 | break; |
| 2089 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2090 | ul.unlock(); |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 2091 | svc->doOnNewAudioModulesAvailable(); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2092 | ul.lock(); |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 2093 | } break; |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2094 | case ROUTING_UPDATED: { |
| 2095 | ALOGV("AudioCommandThread() processing routing update"); |
| 2096 | svc = mService.promote(); |
| 2097 | if (svc == 0) { |
| 2098 | break; |
| 2099 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2100 | ul.unlock(); |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2101 | svc->doOnRoutingUpdated(); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2102 | ul.lock(); |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2103 | } break; |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 2104 | |
Eric Laurent | ed726cc | 2021-07-01 14:26:41 +0200 | [diff] [blame] | 2105 | case UPDATE_UID_STATES: { |
| 2106 | ALOGV("AudioCommandThread() processing updateUID states"); |
| 2107 | svc = mService.promote(); |
| 2108 | if (svc == 0) { |
| 2109 | break; |
| 2110 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2111 | ul.unlock(); |
Eric Laurent | ed726cc | 2021-07-01 14:26:41 +0200 | [diff] [blame] | 2112 | svc->updateUidStates(); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2113 | ul.lock(); |
Eric Laurent | ed726cc | 2021-07-01 14:26:41 +0200 | [diff] [blame] | 2114 | } break; |
| 2115 | |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 2116 | case CHECK_SPATIALIZER_OUTPUT: { |
| 2117 | ALOGV("AudioCommandThread() processing check spatializer"); |
Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 2118 | svc = mService.promote(); |
| 2119 | if (svc == 0) { |
| 2120 | break; |
| 2121 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2122 | ul.unlock(); |
Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 2123 | svc->doOnCheckSpatializer(); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2124 | ul.lock(); |
Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 2125 | } break; |
| 2126 | |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 2127 | case UPDATE_ACTIVE_SPATIALIZER_TRACKS: { |
| 2128 | ALOGV("AudioCommandThread() processing update spatializer tracks"); |
| 2129 | svc = mService.promote(); |
| 2130 | if (svc == 0) { |
| 2131 | break; |
| 2132 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2133 | ul.unlock(); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 2134 | svc->doOnUpdateActiveSpatializerTracks(); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2135 | ul.lock(); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 2136 | } break; |
| 2137 | |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2138 | case VOL_RANGE_INIT_REQUEST: { |
| 2139 | ALOGV("AudioCommandThread() processing volume range init request"); |
| 2140 | svc = mService.promote(); |
| 2141 | if (svc == 0) { |
| 2142 | break; |
| 2143 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2144 | ul.unlock(); |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2145 | svc->doOnVolumeRangeInitRequest(); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2146 | ul.lock(); |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2147 | } break; |
| 2148 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2149 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2150 | ALOGW("AudioCommandThread() unknown command %d", command->mCommand); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2151 | } |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2152 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2153 | audio_utils::lock_guard _l(command->mMutex); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2154 | if (command->mWaitStatus) { |
| 2155 | command->mWaitStatus = false; |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2156 | command->mCond.notify_one(); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2157 | } |
| 2158 | } |
Eric Laurent | d7eda8d | 2016-02-02 17:18:39 -0800 | [diff] [blame] | 2159 | waitTime = -1; |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2160 | // release ul before releasing strong reference on the service as |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 2161 | // AudioPolicyService destructor calls AudioCommandThread::exit() which |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2162 | // acquires ul. |
| 2163 | ul.unlock(); |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 2164 | svc.clear(); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2165 | ul.lock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2166 | } else { |
| 2167 | waitTime = mAudioCommands[0]->mTime - curTime; |
| 2168 | break; |
| 2169 | } |
| 2170 | } |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 2171 | |
Ytai Ben-Tsvi | 6958b02 | 2022-04-26 15:45:53 -0700 | [diff] [blame] | 2172 | // release delayed commands wake lock as many times as we made the queue is |
| 2173 | // empty during popping. |
| 2174 | while (numTimesBecameEmpty--) { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2175 | release_wake_lock(mName.c_str()); |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | // At this stage we have either an empty command queue or the first command in the queue |
| 2179 | // has a finite delay. So unless we are exiting it is safe to wait. |
| 2180 | if (!exitPending()) { |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 2181 | ALOGV("AudioCommandThread() going to sleep"); |
Eric Laurent | d7eda8d | 2016-02-02 17:18:39 -0800 | [diff] [blame] | 2182 | if (waitTime == -1) { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2183 | mWaitWorkCV.wait(ul); |
Eric Laurent | d7eda8d | 2016-02-02 17:18:39 -0800 | [diff] [blame] | 2184 | } else { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2185 | // discard return value. |
| 2186 | mWaitWorkCV.wait_for(ul, std::chrono::nanoseconds(waitTime)); |
Eric Laurent | d7eda8d | 2016-02-02 17:18:39 -0800 | [diff] [blame] | 2187 | } |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 2188 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2189 | } |
Ricardo Garcia | 05f2fdc | 2014-07-24 15:48:24 -0700 | [diff] [blame] | 2190 | // release delayed commands wake lock before quitting |
| 2191 | if (!mAudioCommands.isEmpty()) { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2192 | release_wake_lock(mName.c_str()); |
Ricardo Garcia | 05f2fdc | 2014-07-24 15:48:24 -0700 | [diff] [blame] | 2193 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2194 | return false; |
| 2195 | } |
| 2196 | |
| 2197 | status_t AudioPolicyService::AudioCommandThread::dump(int fd) |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2198 | NO_THREAD_SAFETY_ANALYSIS // trylock |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2199 | { |
| 2200 | const size_t SIZE = 256; |
| 2201 | char buffer[SIZE]; |
| 2202 | String8 result; |
| 2203 | |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2204 | const bool locked = mMutex.try_lock(kDumpLockTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2205 | if (!locked) { |
| 2206 | String8 result2(kCmdDeadlockedString); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2207 | write(fd, result2.c_str(), result2.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | snprintf(buffer, SIZE, "- Commands:\n"); |
| 2211 | result = String8(buffer); |
| 2212 | result.append(" Command Time Wait pParam\n"); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 2213 | for (size_t i = 0; i < mAudioCommands.size(); i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2214 | mAudioCommands[i]->dump(buffer, SIZE); |
| 2215 | result.append(buffer); |
| 2216 | } |
| 2217 | result.append(" Last Command\n"); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2218 | if (mLastCommand != 0) { |
| 2219 | mLastCommand->dump(buffer, SIZE); |
| 2220 | result.append(buffer); |
| 2221 | } else { |
| 2222 | result.append(" none\n"); |
| 2223 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2224 | |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2225 | write(fd, result.c_str(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2226 | |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2227 | dumpReleaseLock(mMutex, locked); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2228 | |
| 2229 | return NO_ERROR; |
| 2230 | } |
| 2231 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 2232 | status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2233 | float volume, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2234 | audio_io_handle_t output, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2235 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2236 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2237 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2238 | command->mCommand = SET_VOLUME; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2239 | sp<VolumeData> data = new VolumeData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2240 | data->mStream = stream; |
| 2241 | data->mVolume = volume; |
| 2242 | data->mIO = output; |
| 2243 | command->mParam = data; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2244 | command->mWaitStatus = true; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2245 | ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2246 | stream, volume, output); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2247 | return sendCommand(command, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2250 | status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2251 | const char *keyValuePairs, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2252 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2253 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2254 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2255 | command->mCommand = SET_PARAMETERS; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2256 | sp<ParametersData> data = new ParametersData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2257 | data->mIO = ioHandle; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2258 | data->mKeyValuePairs = String8(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2259 | command->mParam = data; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2260 | command->mWaitStatus = true; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2261 | ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2262 | keyValuePairs, ioHandle, delayMs); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2263 | return sendCommand(command, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs) |
| 2267 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2268 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2269 | command->mCommand = SET_VOICE_VOLUME; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2270 | sp<VoiceVolumeData> data = new VoiceVolumeData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2271 | data->mVolume = volume; |
| 2272 | command->mParam = data; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2273 | command->mWaitStatus = true; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2274 | ALOGV("AudioCommandThread() adding set voice volume volume %f", volume); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2275 | return sendCommand(command, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 2278 | void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId, |
| 2279 | audio_session_t sessionId, |
| 2280 | bool suspended) |
| 2281 | { |
| 2282 | sp<AudioCommand> command = new AudioCommand(); |
| 2283 | command->mCommand = SET_EFFECT_SUSPENDED; |
| 2284 | sp<SetEffectSuspendedData> data = new SetEffectSuspendedData(); |
| 2285 | data->mEffectId = effectId; |
| 2286 | data->mSessionId = sessionId; |
| 2287 | data->mSuspended = suspended; |
| 2288 | command->mParam = data; |
| 2289 | ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d", |
| 2290 | effectId, sessionId, suspended); |
| 2291 | sendCommand(command); |
| 2292 | } |
| 2293 | |
| 2294 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2295 | void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2296 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2297 | sp<AudioCommand> command = new AudioCommand(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2298 | command->mCommand = STOP_OUTPUT; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2299 | sp<StopOutputData> data = new StopOutputData(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2300 | data->mPortId = portId; |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 2301 | command->mParam = data; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2302 | ALOGV("AudioCommandThread() adding stop output portId %d", portId); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2303 | sendCommand(command); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2304 | } |
| 2305 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2306 | void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2307 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2308 | sp<AudioCommand> command = new AudioCommand(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2309 | command->mCommand = RELEASE_OUTPUT; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2310 | sp<ReleaseOutputData> data = new ReleaseOutputData(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2311 | data->mPortId = portId; |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 2312 | command->mParam = data; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2313 | ALOGV("AudioCommandThread() adding release output portId %d", portId); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2314 | sendCommand(command); |
| 2315 | } |
| 2316 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2317 | status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand( |
| 2318 | const struct audio_patch *patch, |
| 2319 | audio_patch_handle_t *handle, |
| 2320 | int delayMs) |
| 2321 | { |
| 2322 | status_t status = NO_ERROR; |
| 2323 | |
| 2324 | sp<AudioCommand> command = new AudioCommand(); |
| 2325 | command->mCommand = CREATE_AUDIO_PATCH; |
| 2326 | CreateAudioPatchData *data = new CreateAudioPatchData(); |
| 2327 | data->mPatch = *patch; |
| 2328 | data->mHandle = *handle; |
| 2329 | command->mParam = data; |
| 2330 | command->mWaitStatus = true; |
| 2331 | ALOGV("AudioCommandThread() adding create patch delay %d", delayMs); |
| 2332 | status = sendCommand(command, delayMs); |
| 2333 | if (status == NO_ERROR) { |
| 2334 | *handle = data->mHandle; |
| 2335 | } |
| 2336 | return status; |
| 2337 | } |
| 2338 | |
| 2339 | status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle, |
| 2340 | int delayMs) |
| 2341 | { |
| 2342 | sp<AudioCommand> command = new AudioCommand(); |
| 2343 | command->mCommand = RELEASE_AUDIO_PATCH; |
| 2344 | ReleaseAudioPatchData *data = new ReleaseAudioPatchData(); |
| 2345 | data->mHandle = handle; |
| 2346 | command->mParam = data; |
| 2347 | command->mWaitStatus = true; |
| 2348 | ALOGV("AudioCommandThread() adding release patch delay %d", delayMs); |
| 2349 | return sendCommand(command, delayMs); |
| 2350 | } |
| 2351 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2352 | void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand() |
| 2353 | { |
| 2354 | sp<AudioCommand> command = new AudioCommand(); |
| 2355 | command->mCommand = UPDATE_AUDIOPORT_LIST; |
| 2356 | ALOGV("AudioCommandThread() adding update audio port list"); |
| 2357 | sendCommand(command); |
| 2358 | } |
| 2359 | |
Eric Laurent | ed726cc | 2021-07-01 14:26:41 +0200 | [diff] [blame] | 2360 | void AudioPolicyService::AudioCommandThread::updateUidStatesCommand() |
| 2361 | { |
| 2362 | sp<AudioCommand> command = new AudioCommand(); |
| 2363 | command->mCommand = UPDATE_UID_STATES; |
| 2364 | ALOGV("AudioCommandThread() adding update UID states"); |
| 2365 | sendCommand(command); |
| 2366 | } |
| 2367 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2368 | void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand() |
| 2369 | { |
| 2370 | sp<AudioCommand>command = new AudioCommand(); |
| 2371 | command->mCommand = UPDATE_AUDIOPATCH_LIST; |
| 2372 | ALOGV("AudioCommandThread() adding update audio patch list"); |
| 2373 | sendCommand(command); |
| 2374 | } |
| 2375 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2376 | void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group, |
| 2377 | int flags) |
| 2378 | { |
| 2379 | sp<AudioCommand>command = new AudioCommand(); |
| 2380 | command->mCommand = CHANGED_AUDIOVOLUMEGROUP; |
| 2381 | AudioVolumeGroupData *data= new AudioVolumeGroupData(); |
| 2382 | data->mGroup = group; |
| 2383 | data->mFlags = flags; |
| 2384 | command->mParam = data; |
| 2385 | ALOGV("AudioCommandThread() adding audio volume group changed"); |
| 2386 | sendCommand(command); |
| 2387 | } |
| 2388 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2389 | status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand( |
| 2390 | const struct audio_port_config *config, int delayMs) |
| 2391 | { |
| 2392 | sp<AudioCommand> command = new AudioCommand(); |
| 2393 | command->mCommand = SET_AUDIOPORT_CONFIG; |
| 2394 | SetAudioPortConfigData *data = new SetAudioPortConfigData(); |
| 2395 | data->mConfig = *config; |
| 2396 | command->mParam = data; |
| 2397 | command->mWaitStatus = true; |
| 2398 | ALOGV("AudioCommandThread() adding set port config delay %d", delayMs); |
| 2399 | return sendCommand(command, delayMs); |
| 2400 | } |
| 2401 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2402 | void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2403 | const String8& regId, int32_t state) |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2404 | { |
| 2405 | sp<AudioCommand> command = new AudioCommand(); |
| 2406 | command->mCommand = DYN_POLICY_MIX_STATE_UPDATE; |
| 2407 | DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData(); |
| 2408 | data->mRegId = regId; |
| 2409 | data->mState = state; |
| 2410 | command->mParam = data; |
| 2411 | ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2412 | regId.c_str(), state); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2413 | sendCommand(command); |
| 2414 | } |
| 2415 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2416 | void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand( |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 2417 | int event, |
| 2418 | const record_client_info_t *clientInfo, |
| 2419 | const audio_config_base_t *clientConfig, |
| 2420 | std::vector<effect_descriptor_t> clientEffects, |
| 2421 | const audio_config_base_t *deviceConfig, |
| 2422 | std::vector<effect_descriptor_t> effects, |
| 2423 | audio_patch_handle_t patchHandle, |
| 2424 | audio_source_t source) |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2425 | { |
| 2426 | sp<AudioCommand>command = new AudioCommand(); |
| 2427 | command->mCommand = RECORDING_CONFIGURATION_UPDATE; |
| 2428 | RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData(); |
| 2429 | data->mEvent = event; |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 2430 | data->mClientInfo = *clientInfo; |
Jean-Michel Trivi | 7281aa9 | 2016-02-17 15:33:40 -0800 | [diff] [blame] | 2431 | data->mClientConfig = *clientConfig; |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 2432 | data->mClientEffects = clientEffects; |
Jean-Michel Trivi | 7281aa9 | 2016-02-17 15:33:40 -0800 | [diff] [blame] | 2433 | data->mDeviceConfig = *deviceConfig; |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 2434 | data->mEffects = effects; |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2435 | data->mPatchHandle = patchHandle; |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 2436 | data->mSource = source; |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2437 | command->mParam = data; |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 2438 | ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u", |
| 2439 | event, clientInfo->source, clientInfo->uid); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2440 | sendCommand(command); |
| 2441 | } |
| 2442 | |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 2443 | void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand() |
| 2444 | { |
| 2445 | sp<AudioCommand> command = new AudioCommand(); |
| 2446 | command->mCommand = AUDIO_MODULES_UPDATE; |
| 2447 | sendCommand(command); |
| 2448 | } |
| 2449 | |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2450 | void AudioPolicyService::AudioCommandThread::routingChangedCommand() |
| 2451 | { |
| 2452 | sp<AudioCommand>command = new AudioCommand(); |
| 2453 | command->mCommand = ROUTING_UPDATED; |
| 2454 | ALOGV("AudioCommandThread() adding routing update"); |
| 2455 | sendCommand(command); |
| 2456 | } |
| 2457 | |
Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 2458 | void AudioPolicyService::AudioCommandThread::checkSpatializerCommand() |
| 2459 | { |
| 2460 | sp<AudioCommand>command = new AudioCommand(); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 2461 | command->mCommand = CHECK_SPATIALIZER_OUTPUT; |
Eric Laurent | 81dd0f5 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 2462 | ALOGV("AudioCommandThread() adding check spatializer"); |
| 2463 | sendCommand(command); |
| 2464 | } |
| 2465 | |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 2466 | void AudioPolicyService::AudioCommandThread::updateActiveSpatializerTracksCommand() |
| 2467 | { |
| 2468 | sp<AudioCommand>command = new AudioCommand(); |
| 2469 | command->mCommand = UPDATE_ACTIVE_SPATIALIZER_TRACKS; |
| 2470 | ALOGV("AudioCommandThread() adding update active spatializer tracks"); |
| 2471 | sendCommand(command); |
| 2472 | } |
| 2473 | |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2474 | void AudioPolicyService::AudioCommandThread::volRangeInitReqCommand() |
| 2475 | { |
| 2476 | sp<AudioCommand>command = new AudioCommand(); |
| 2477 | command->mCommand = VOL_RANGE_INIT_REQUEST; |
| 2478 | ALOGV("AudioCommandThread() adding volume range init request"); |
| 2479 | sendCommand(command); |
| 2480 | } |
| 2481 | |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2482 | status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs) |
| 2483 | { |
| 2484 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2485 | audio_utils::lock_guard _l(mMutex); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2486 | insertCommand_l(command, delayMs); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2487 | mWaitWorkCV.notify_one(); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2488 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2489 | audio_utils::unique_lock ul(command->mMutex); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2490 | while (command->mWaitStatus) { |
| 2491 | nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2492 | if (command->mCond.wait_for( |
Andy Hung | 02ea2a0 | 2024-01-25 17:02:30 -0800 | [diff] [blame] | 2493 | ul, std::chrono::nanoseconds(timeOutNs), getTid()) == std::cv_status::timeout) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2494 | command->mStatus = TIMED_OUT; |
| 2495 | command->mWaitStatus = false; |
| 2496 | } |
| 2497 | } |
| 2498 | return command->mStatus; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2499 | } |
| 2500 | |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2501 | // insertCommand_l() must be called with mMutex held |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2502 | void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2503 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 2504 | ssize_t i; // not size_t because i will count down to -1 |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2505 | Vector < sp<AudioCommand> > removedCommands; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2506 | command->mTime = systemTime() + milliseconds(delayMs); |
| 2507 | |
| 2508 | // acquire wake lock to make sure delayed commands are processed |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2509 | if (mAudioCommands.isEmpty()) { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2510 | acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.c_str()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | // check same pending commands with later time stamps and eliminate them |
Ivan Lozano | 5ff158f | 2017-10-30 09:06:24 -0700 | [diff] [blame] | 2514 | for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2515 | sp<AudioCommand> command2 = mAudioCommands[i]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2516 | // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands |
| 2517 | if (command2->mTime <= command->mTime) break; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2518 | |
| 2519 | // create audio patch or release audio patch commands are equivalent |
| 2520 | // with regard to filtering |
| 2521 | if ((command->mCommand == CREATE_AUDIO_PATCH) || |
| 2522 | (command->mCommand == RELEASE_AUDIO_PATCH)) { |
| 2523 | if ((command2->mCommand != CREATE_AUDIO_PATCH) && |
| 2524 | (command2->mCommand != RELEASE_AUDIO_PATCH)) { |
| 2525 | continue; |
| 2526 | } |
| 2527 | } else if (command2->mCommand != command->mCommand) continue; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2528 | |
| 2529 | switch (command->mCommand) { |
| 2530 | case SET_PARAMETERS: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2531 | ParametersData *data = (ParametersData *)command->mParam.get(); |
| 2532 | ParametersData *data2 = (ParametersData *)command2->mParam.get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2533 | if (data->mIO != data2->mIO) break; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2534 | ALOGV("Comparing parameter command %s to new command %s", |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2535 | data2->mKeyValuePairs.c_str(), data->mKeyValuePairs.c_str()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2536 | AudioParameter param = AudioParameter(data->mKeyValuePairs); |
| 2537 | AudioParameter param2 = AudioParameter(data2->mKeyValuePairs); |
| 2538 | for (size_t j = 0; j < param.size(); j++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2539 | String8 key; |
| 2540 | String8 value; |
| 2541 | param.getAt(j, key, value); |
| 2542 | for (size_t k = 0; k < param2.size(); k++) { |
| 2543 | String8 key2; |
| 2544 | String8 value2; |
| 2545 | param2.getAt(k, key2, value2); |
| 2546 | if (key2 == key) { |
| 2547 | param2.remove(key2); |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2548 | ALOGV("Filtering out parameter %s", key2.c_str()); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2549 | break; |
| 2550 | } |
| 2551 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2552 | } |
| 2553 | // if all keys have been filtered out, remove the command. |
| 2554 | // otherwise, update the key value pairs |
| 2555 | if (param2.size() == 0) { |
| 2556 | removedCommands.add(command2); |
| 2557 | } else { |
| 2558 | data2->mKeyValuePairs = param2.toString(); |
| 2559 | } |
Eric Laurent | 21e5456 | 2013-09-23 12:08:05 -0700 | [diff] [blame] | 2560 | command->mTime = command2->mTime; |
| 2561 | // force delayMs to non 0 so that code below does not request to wait for |
| 2562 | // command status as the command is now delayed |
| 2563 | delayMs = 1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2564 | } break; |
| 2565 | |
| 2566 | case SET_VOLUME: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2567 | VolumeData *data = (VolumeData *)command->mParam.get(); |
| 2568 | VolumeData *data2 = (VolumeData *)command2->mParam.get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2569 | if (data->mIO != data2->mIO) break; |
| 2570 | if (data->mStream != data2->mStream) break; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2571 | ALOGV("Filtering out volume command on output %d for stream %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2572 | data->mIO, data->mStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2573 | removedCommands.add(command2); |
Eric Laurent | 21e5456 | 2013-09-23 12:08:05 -0700 | [diff] [blame] | 2574 | command->mTime = command2->mTime; |
| 2575 | // force delayMs to non 0 so that code below does not request to wait for |
| 2576 | // command status as the command is now delayed |
| 2577 | delayMs = 1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2578 | } break; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2579 | |
Eric Laurent | baf35fe | 2016-07-27 15:36:53 -0700 | [diff] [blame] | 2580 | case SET_VOICE_VOLUME: { |
| 2581 | VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get(); |
| 2582 | VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get(); |
| 2583 | ALOGV("Filtering out voice volume command value %f replaced by %f", |
| 2584 | data2->mVolume, data->mVolume); |
| 2585 | removedCommands.add(command2); |
| 2586 | command->mTime = command2->mTime; |
| 2587 | // force delayMs to non 0 so that code below does not request to wait for |
| 2588 | // command status as the command is now delayed |
| 2589 | delayMs = 1; |
| 2590 | } break; |
| 2591 | |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2592 | case CREATE_AUDIO_PATCH: |
| 2593 | case RELEASE_AUDIO_PATCH: { |
| 2594 | audio_patch_handle_t handle; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 2595 | struct audio_patch patch; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2596 | if (command->mCommand == CREATE_AUDIO_PATCH) { |
| 2597 | handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 2598 | patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2599 | } else { |
| 2600 | handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle; |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 2601 | memset(&patch, 0, sizeof(patch)); |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2602 | } |
| 2603 | audio_patch_handle_t handle2; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 2604 | struct audio_patch patch2; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2605 | if (command2->mCommand == CREATE_AUDIO_PATCH) { |
| 2606 | handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 2607 | patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2608 | } else { |
| 2609 | handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle; |
Glenn Kasten | f60b6b6 | 2015-07-06 10:53:26 -0700 | [diff] [blame] | 2610 | memset(&patch2, 0, sizeof(patch2)); |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2611 | } |
| 2612 | if (handle != handle2) break; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 2613 | /* Filter CREATE_AUDIO_PATCH commands only when they are issued for |
| 2614 | same output. */ |
| 2615 | if( (command->mCommand == CREATE_AUDIO_PATCH) && |
| 2616 | (command2->mCommand == CREATE_AUDIO_PATCH) ) { |
| 2617 | bool isOutputDiff = false; |
| 2618 | if (patch.num_sources == patch2.num_sources) { |
| 2619 | for (unsigned count = 0; count < patch.num_sources; count++) { |
| 2620 | if (patch.sources[count].id != patch2.sources[count].id) { |
| 2621 | isOutputDiff = true; |
| 2622 | break; |
| 2623 | } |
| 2624 | } |
| 2625 | if (isOutputDiff) |
| 2626 | break; |
| 2627 | } |
| 2628 | } |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2629 | ALOGV("Filtering out %s audio patch command for handle %d", |
| 2630 | (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle); |
| 2631 | removedCommands.add(command2); |
| 2632 | command->mTime = command2->mTime; |
| 2633 | // force delayMs to non 0 so that code below does not request to wait for |
| 2634 | // command status as the command is now delayed |
| 2635 | delayMs = 1; |
| 2636 | } break; |
| 2637 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2638 | case DYN_POLICY_MIX_STATE_UPDATE: { |
| 2639 | |
| 2640 | } break; |
| 2641 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2642 | case RECORDING_CONFIGURATION_UPDATE: { |
| 2643 | |
| 2644 | } break; |
| 2645 | |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2646 | case ROUTING_UPDATED: { |
| 2647 | |
| 2648 | } break; |
| 2649 | |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2650 | case VOL_RANGE_INIT_REQUEST: { |
| 2651 | // command may come from different requests, do not filter |
| 2652 | } break; |
| 2653 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2654 | default: |
| 2655 | break; |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | // remove filtered commands |
| 2660 | for (size_t j = 0; j < removedCommands.size(); j++) { |
| 2661 | // removed commands always have time stamps greater than current command |
| 2662 | for (size_t k = i + 1; k < mAudioCommands.size(); k++) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2663 | if (mAudioCommands[k].get() == removedCommands[j].get()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2664 | ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2665 | mAudioCommands.removeAt(k); |
| 2666 | break; |
| 2667 | } |
| 2668 | } |
| 2669 | } |
| 2670 | removedCommands.clear(); |
| 2671 | |
Eric Laurent | aa79bef | 2015-01-15 14:33:51 -0800 | [diff] [blame] | 2672 | // Disable wait for status if delay is not 0. |
| 2673 | // Except for create audio patch command because the returned patch handle |
| 2674 | // is needed by audio policy manager |
| 2675 | if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) { |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 2676 | command->mWaitStatus = false; |
| 2677 | } |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 2678 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2679 | // insert command at the right place according to its time stamp |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 2680 | ALOGV("inserting command: %d at index %zd, num commands %zu", |
| 2681 | command->mCommand, i+1, mAudioCommands.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2682 | mAudioCommands.insertAt(command, i + 1); |
| 2683 | } |
| 2684 | |
| 2685 | void AudioPolicyService::AudioCommandThread::exit() |
| 2686 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2687 | ALOGV("AudioCommandThread::exit"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2688 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2689 | audio_utils::lock_guard _l(mMutex); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2690 | requestExit(); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 2691 | mWaitWorkCV.notify_one(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2692 | } |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 2693 | // Note that we can call it from the thread loop if all other references have been released |
| 2694 | // but it will safely return WOULD_BLOCK in this case |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2695 | requestExitAndWait(); |
| 2696 | } |
| 2697 | |
| 2698 | void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size) |
| 2699 | { |
| 2700 | snprintf(buffer, size, " %02d %06d.%03d %01u %p\n", |
| 2701 | mCommand, |
| 2702 | (int)ns2s(mTime), |
| 2703 | (int)ns2ms(mTime)%1000, |
| 2704 | mWaitStatus, |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2705 | mParam.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2706 | } |
| 2707 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2708 | /******* helpers for the service_ops callbacks defined below *********/ |
| 2709 | void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, |
| 2710 | const char *keyValuePairs, |
| 2711 | int delayMs) |
| 2712 | { |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2713 | mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2714 | delayMs); |
| 2715 | } |
| 2716 | |
| 2717 | int AudioPolicyService::setStreamVolume(audio_stream_type_t stream, |
| 2718 | float volume, |
| 2719 | audio_io_handle_t output, |
| 2720 | int delayMs) |
| 2721 | { |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 2722 | return (int)mAudioCommandThread->volumeCommand(stream, volume, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2723 | output, delayMs); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2726 | int AudioPolicyService::setVoiceVolume(float volume, int delayMs) |
| 2727 | { |
| 2728 | return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs); |
| 2729 | } |
| 2730 | |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 2731 | void AudioPolicyService::setEffectSuspended(int effectId, |
| 2732 | audio_session_t sessionId, |
| 2733 | bool suspended) |
| 2734 | { |
| 2735 | mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended); |
| 2736 | } |
| 2737 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2738 | Status AudioPolicyService::onNewAudioModulesAvailable() |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 2739 | { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 2740 | mOutputCommandThread->audioModulesUpdateCommand(); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2741 | return Status::ok(); |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 2744 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2745 | extern "C" { |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2746 | audio_module_handle_t aps_load_hw_module(void *service __unused, |
| 2747 | const char *name); |
| 2748 | audio_io_handle_t aps_open_output(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2749 | audio_devices_t *pDevices, |
| 2750 | uint32_t *pSamplingRate, |
| 2751 | audio_format_t *pFormat, |
| 2752 | audio_channel_mask_t *pChannelMask, |
| 2753 | uint32_t *pLatencyMs, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2754 | audio_output_flags_t flags); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2755 | |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2756 | audio_io_handle_t aps_open_output_on_module(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2757 | audio_module_handle_t module, |
| 2758 | audio_devices_t *pDevices, |
| 2759 | uint32_t *pSamplingRate, |
| 2760 | audio_format_t *pFormat, |
| 2761 | audio_channel_mask_t *pChannelMask, |
| 2762 | uint32_t *pLatencyMs, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 2763 | audio_output_flags_t flags, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2764 | const audio_offload_info_t *offloadInfo); |
| 2765 | audio_io_handle_t aps_open_dup_output(void *service __unused, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2766 | audio_io_handle_t output1, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2767 | audio_io_handle_t output2); |
| 2768 | int aps_close_output(void *service __unused, audio_io_handle_t output); |
| 2769 | int aps_suspend_output(void *service __unused, audio_io_handle_t output); |
| 2770 | int aps_restore_output(void *service __unused, audio_io_handle_t output); |
| 2771 | audio_io_handle_t aps_open_input(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2772 | audio_devices_t *pDevices, |
| 2773 | uint32_t *pSamplingRate, |
| 2774 | audio_format_t *pFormat, |
| 2775 | audio_channel_mask_t *pChannelMask, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2776 | audio_in_acoustics_t acoustics __unused); |
| 2777 | audio_io_handle_t aps_open_input_on_module(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2778 | audio_module_handle_t module, |
| 2779 | audio_devices_t *pDevices, |
| 2780 | uint32_t *pSamplingRate, |
| 2781 | audio_format_t *pFormat, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2782 | audio_channel_mask_t *pChannelMask); |
| 2783 | int aps_close_input(void *service __unused, audio_io_handle_t input); |
| 2784 | int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream); |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2785 | int aps_move_effects(void *service __unused, audio_session_t session, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2786 | audio_io_handle_t src_output, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2787 | audio_io_handle_t dst_output); |
| 2788 | char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle, |
| 2789 | const char *keys); |
| 2790 | void aps_set_parameters(void *service, audio_io_handle_t io_handle, |
| 2791 | const char *kv_pairs, int delay_ms); |
| 2792 | int aps_set_stream_volume(void *service, audio_stream_type_t stream, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2793 | float volume, audio_io_handle_t output, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2794 | int delay_ms); |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2795 | int aps_set_voice_volume(void *service, float volume, int delay_ms); |
| 2796 | }; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2797 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 2798 | } // namespace android |