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