François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "APM::AudioPolicyEngine" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | //#define VERY_VERBOSE_LOGGING |
| 21 | #ifdef VERY_VERBOSE_LOGGING |
| 22 | #define ALOGVV ALOGV |
| 23 | #else |
| 24 | #define ALOGVV(a...) do { } while(0) |
| 25 | #endif |
| 26 | |
| 27 | #include "Engine.h" |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 28 | #include <android-base/macros.h> |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 29 | #include <AudioPolicyManagerObserver.h> |
jiabin | e128485 | 2019-09-11 10:15:46 -0700 | [diff] [blame] | 30 | #include <PolicyAudioPort.h> |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 31 | #include <IOProfile.h> |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 32 | #include <AudioIODescriptorInterface.h> |
Eric Laurent | ffff1f7 | 2024-06-12 19:08:15 +0000 | [diff] [blame] | 33 | #include <com_android_media_audioserver.h> |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 34 | #include <policy.h> |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 35 | #include <media/AudioContainers.h> |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 36 | #include <utils/String8.h> |
| 37 | #include <utils/Log.h> |
| 38 | |
Mikhail Naganov | 9e459d7 | 2023-05-05 17:36:39 -0700 | [diff] [blame] | 39 | namespace android::audio_policy { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 40 | |
Mikhail Naganov | f900362 | 2020-03-10 13:15:08 -0700 | [diff] [blame] | 41 | static const std::vector<legacy_strategy_map>& getLegacyStrategy() { |
Mikhail Naganov | bada1f5 | 2024-12-03 16:20:10 -0800 | [diff] [blame^] | 42 | static const std::vector<legacy_strategy_map> legacyStrategy = getLegacyStrategyMap(); |
Mikhail Naganov | f900362 | 2020-03-10 13:15:08 -0700 | [diff] [blame] | 43 | return legacyStrategy; |
| 44 | } |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 45 | |
Mikhail Naganov | 9e459d7 | 2023-05-05 17:36:39 -0700 | [diff] [blame] | 46 | status_t Engine::loadFromHalConfigWithFallback( |
| 47 | const media::audio::common::AudioHalEngineConfig& aidlConfig) { |
| 48 | return loadWithFallback(aidlConfig); |
| 49 | } |
| 50 | |
Mikhail Naganov | abb0478 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 51 | status_t Engine::loadFromXmlConfigWithFallback(const std::string& xmlFilePath) { |
Mikhail Naganov | 9e459d7 | 2023-05-05 17:36:39 -0700 | [diff] [blame] | 52 | return loadWithFallback(xmlFilePath); |
| 53 | } |
| 54 | |
| 55 | template<typename T> |
| 56 | status_t Engine::loadWithFallback(const T& configSource) { |
Mikhail Naganov | bada1f5 | 2024-12-03 16:20:10 -0800 | [diff] [blame^] | 57 | auto result = EngineBase::loadAudioPolicyEngineConfig(configSource, false /*isConfigurable*/); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 58 | ALOGE_IF(result.nbSkippedElement != 0, |
| 59 | "Policy Engine configuration is partially invalid, skipped %zu elements", |
| 60 | result.nbSkippedElement); |
| 61 | |
Mikhail Naganov | f900362 | 2020-03-10 13:15:08 -0700 | [diff] [blame] | 62 | auto legacyStrategy = getLegacyStrategy(); |
| 63 | for (const auto &strategy : legacyStrategy) { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 64 | mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 65 | } |
Mikhail Naganov | abb0478 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 66 | |
| 67 | return OK; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Mikhail Naganov | 9e459d7 | 2023-05-05 17:36:39 -0700 | [diff] [blame] | 70 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 71 | status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) |
| 72 | { |
| 73 | switch(usage) { |
| 74 | case AUDIO_POLICY_FORCE_FOR_COMMUNICATION: |
| 75 | if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO && |
| 76 | config != AUDIO_POLICY_FORCE_NONE) { |
Lorena Torres-Huerta | 922da31 | 2022-07-21 08:07:32 +0000 | [diff] [blame] | 77 | ALOGW("setForceUse() invalid config %d for COMMUNICATION", config); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 78 | return BAD_VALUE; |
| 79 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 80 | break; |
| 81 | case AUDIO_POLICY_FORCE_FOR_MEDIA: |
| 82 | if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP && |
| 83 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 84 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 85 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE && |
| 86 | config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) { |
Lorena Torres-Huerta | 922da31 | 2022-07-21 08:07:32 +0000 | [diff] [blame] | 87 | ALOGW("setForceUse() invalid config %d for MEDIA", config); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 88 | return BAD_VALUE; |
| 89 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 90 | break; |
| 91 | case AUDIO_POLICY_FORCE_FOR_RECORD: |
| 92 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 93 | config != AUDIO_POLICY_FORCE_NONE) { |
Lorena Torres-Huerta | 922da31 | 2022-07-21 08:07:32 +0000 | [diff] [blame] | 94 | ALOGW("setForceUse() invalid config %d for RECORD", config); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 95 | return BAD_VALUE; |
| 96 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 97 | break; |
| 98 | case AUDIO_POLICY_FORCE_FOR_DOCK: |
| 99 | if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK && |
| 100 | config != AUDIO_POLICY_FORCE_BT_DESK_DOCK && |
| 101 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 102 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 103 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) { |
Lorena Torres-Huerta | 922da31 | 2022-07-21 08:07:32 +0000 | [diff] [blame] | 104 | ALOGW("setForceUse() invalid config %d for DOCK", config); |
| 105 | return BAD_VALUE; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 106 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 107 | break; |
| 108 | case AUDIO_POLICY_FORCE_FOR_SYSTEM: |
| 109 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 110 | config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Lorena Torres-Huerta | 922da31 | 2022-07-21 08:07:32 +0000 | [diff] [blame] | 111 | ALOGW("setForceUse() invalid config %d for SYSTEM", config); |
| 112 | return BAD_VALUE; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 113 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 114 | break; |
| 115 | case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO: |
| 116 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 117 | config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) { |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 118 | ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config); |
Lorena Torres-Huerta | 922da31 | 2022-07-21 08:07:32 +0000 | [diff] [blame] | 119 | return BAD_VALUE; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 120 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 121 | break; |
| 122 | case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND: |
| 123 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 124 | config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER && |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 125 | config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS && |
| 126 | config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 127 | ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config); |
| 128 | return BAD_VALUE; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 129 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 130 | break; |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 131 | case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING: |
chenxin20 | e9f2d9f | 2024-06-26 10:51:31 +0800 | [diff] [blame] | 132 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_BT_BLE |
| 133 | && config != AUDIO_POLICY_FORCE_NONE) { |
Lorena Torres-Huerta | 922da31 | 2022-07-21 08:07:32 +0000 | [diff] [blame] | 134 | ALOGW("setForceUse() invalid config %d for VIBRATE_RINGING", config); |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 135 | return BAD_VALUE; |
| 136 | } |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 137 | break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 138 | default: |
| 139 | ALOGW("setForceUse() invalid usage %d", usage); |
Lorena Torres-Huerta | 922da31 | 2022-07-21 08:07:32 +0000 | [diff] [blame] | 140 | return BAD_VALUE; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 141 | } |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 142 | return EngineBase::setForceUse(usage, config); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Eric Laurent | ffff1f7 | 2024-06-12 19:08:15 +0000 | [diff] [blame] | 145 | bool Engine::isBtScoActive(DeviceVector& availableOutputDevices, |
| 146 | const SwAudioOutputCollection &outputs) const { |
| 147 | if (availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) { |
| 148 | return false; |
| 149 | } |
| 150 | // SCO is active if: |
| 151 | // 1) we are in a call and SCO is the preferred device for PHONE strategy |
| 152 | if (isInCall() && audio_is_bluetooth_out_sco_device( |
| 153 | getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE))) { |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | // 2) A strategy for which the preferred device is SCO is active |
| 158 | for (const auto &ps : getOrderedProductStrategies()) { |
| 159 | if (outputs.isStrategyActive(ps) && |
| 160 | !getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, ps) |
| 161 | .getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) { |
| 162 | return true; |
| 163 | } |
| 164 | } |
| 165 | // 3) a ringtone is active and SCO is used for ringing |
| 166 | if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING)) |
| 167 | && (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING) |
| 168 | == AUDIO_POLICY_FORCE_BT_SCO)) { |
| 169 | return true; |
| 170 | } |
| 171 | // 4) an active input is routed from SCO |
| 172 | DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
| 173 | const auto &inputs = getApmObserver()->getInputs(); |
| 174 | if (inputs.activeInputsCountOnDevices(availableInputDevices.getDevicesFromType( |
| 175 | AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) > 0) { |
| 176 | return true; |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
Eric Laurent | 2802b86 | 2021-03-01 09:36:16 +0100 | [diff] [blame] | 181 | void Engine::filterOutputDevicesForStrategy(legacy_strategy strategy, |
| 182 | DeviceVector& availableOutputDevices, |
Eric Laurent | 2802b86 | 2021-03-01 09:36:16 +0100 | [diff] [blame] | 183 | const SwAudioOutputCollection &outputs) const |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 184 | { |
Eric Laurent | 6cd5f90 | 2021-03-23 18:29:58 +0100 | [diff] [blame] | 185 | DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
| 186 | |
Eric Laurent | ffff1f7 | 2024-06-12 19:08:15 +0000 | [diff] [blame] | 187 | if (com::android::media::audioserver::use_bt_sco_for_media()) { |
| 188 | // remove A2DP and LE Audio devices whenever BT SCO is in use |
| 189 | if (isBtScoActive(availableOutputDevices, outputs)) { |
| 190 | availableOutputDevices.remove( |
| 191 | availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllA2dpSet())); |
| 192 | availableOutputDevices.remove( |
| 193 | availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllBleSet())); |
| 194 | } |
| 195 | } |
| 196 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 197 | switch (strategy) { |
Eric Laurent | 2802b86 | 2021-03-01 09:36:16 +0100 | [diff] [blame] | 198 | case STRATEGY_SONIFICATION_RESPECTFUL: { |
| 199 | if (!(isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL)))) { |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 200 | // routing is same as media without the "remote" device |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 201 | availableOutputDevices.remove(availableOutputDevices.getDevicesFromType( |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 202 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX)); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 203 | } |
Eric Laurent | 2802b86 | 2021-03-01 09:36:16 +0100 | [diff] [blame] | 204 | } break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 205 | case STRATEGY_DTMF: |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 206 | case STRATEGY_PHONE: { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 207 | // Force use of only devices on primary output if: |
| 208 | // - in call AND |
| 209 | // - cannot route from voice call RX OR |
| 210 | // - audio HAL version is < 3.0 and TX device is on the primary HW module |
| 211 | if (getPhoneState() == AUDIO_MODE_IN_CALL) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 212 | sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput(); |
Eric Laurent | 5569bf4 | 2023-08-30 15:53:55 +0200 | [diff] [blame] | 213 | if (primaryOutput != nullptr) { |
| 214 | audio_devices_t txDevice = AUDIO_DEVICE_NONE; |
| 215 | sp<DeviceDescriptor> txDeviceDesc = |
| 216 | getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 217 | if (txDeviceDesc != nullptr) { |
| 218 | txDevice = txDeviceDesc->type(); |
| 219 | } |
| 220 | DeviceVector availPrimaryInputDevices = |
| 221 | availableInputDevices.getDevicesFromHwModule( |
| 222 | primaryOutput->getModuleHandle()); |
Eric Laurent | 58a73fc | 2018-02-21 18:46:13 -0800 | [diff] [blame] | 223 | |
Eric Laurent | 5569bf4 | 2023-08-30 15:53:55 +0200 | [diff] [blame] | 224 | // TODO: getPrimaryOutput return only devices from first module in |
| 225 | // audio_policy_configuration.xml, hearing aid is not there, but it's |
| 226 | // a primary device |
| 227 | // FIXME: this is not the right way of solving this problem |
| 228 | DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes( |
| 229 | primaryOutput->supportedDevices().types()); |
| 230 | availPrimaryOutputDevices.add( |
| 231 | availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID)); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 232 | |
Eric Laurent | 5569bf4 | 2023-08-30 15:53:55 +0200 | [diff] [blame] | 233 | if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX, |
| 234 | String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) |
| 235 | || ((availPrimaryInputDevices.getDevice( |
| 236 | txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) && |
| 237 | (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) { |
| 238 | availableOutputDevices = availPrimaryOutputDevices; |
| 239 | } |
| 240 | } else { |
| 241 | ALOGE("%s, STRATEGY_PHONE: Primary output not found", __func__); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 242 | } |
Eric Laurent | 97cec6f | 2021-05-20 13:52:47 +0200 | [diff] [blame] | 243 | } |
| 244 | // Do not use A2DP devices when in call but use them when not in call |
| 245 | // (e.g for voice mail playback) |
| 246 | if (isInCall()) { |
Eric Laurent | 8f2f4e9 | 2021-05-17 14:42:27 +0200 | [diff] [blame] | 247 | availableOutputDevices.remove(availableOutputDevices.getDevicesFromTypes({ |
| 248 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, |
| 249 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, })); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 250 | } |
Eric Laurent | 802ad1d | 2022-07-01 16:54:43 +0200 | [diff] [blame] | 251 | // If connected to a dock, never use the device speaker for calls |
| 252 | if (!availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET}) |
| 253 | .isEmpty()) { |
| 254 | availableOutputDevices.remove( |
| 255 | availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_SPEAKER})); |
| 256 | } |
Eric Laurent | 2802b86 | 2021-03-01 09:36:16 +0100 | [diff] [blame] | 257 | } break; |
| 258 | case STRATEGY_ACCESSIBILITY: { |
| 259 | // do not route accessibility prompts to a digital output currently configured with a |
| 260 | // compressed format as they would likely not be mixed and dropped. |
| 261 | for (size_t i = 0; i < outputs.size(); i++) { |
| 262 | sp<AudioOutputDescriptor> desc = outputs.valueAt(i); |
| 263 | if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) { |
| 264 | availableOutputDevices.remove(desc->devices().getDevicesFromTypes({ |
| 265 | AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF, |
Kuowei Li | 01a686b | 2020-10-27 16:54:39 +0800 | [diff] [blame] | 266 | AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC})); |
Eric Laurent | 2802b86 | 2021-03-01 09:36:16 +0100 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | } break; |
| 270 | default: |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | |
Eric Laurent | 6cd5f90 | 2021-03-23 18:29:58 +0100 | [diff] [blame] | 275 | product_strategy_t Engine::remapStrategyFromContext(product_strategy_t strategy, |
| 276 | const SwAudioOutputCollection &outputs) const { |
| 277 | auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ? |
| 278 | mLegacyStrategyMap.at(strategy) : STRATEGY_NONE; |
| 279 | |
| 280 | if (isInCall()) { |
| 281 | switch (legacyStrategy) { |
| 282 | case STRATEGY_ACCESSIBILITY: |
| 283 | case STRATEGY_DTMF: |
| 284 | case STRATEGY_MEDIA: |
| 285 | case STRATEGY_SONIFICATION: |
| 286 | case STRATEGY_SONIFICATION_RESPECTFUL: |
| 287 | legacyStrategy = STRATEGY_PHONE; |
| 288 | break; |
| 289 | |
| 290 | default: |
| 291 | return strategy; |
| 292 | } |
| 293 | } else { |
| 294 | switch (legacyStrategy) { |
| 295 | case STRATEGY_SONIFICATION_RESPECTFUL: |
| 296 | case STRATEGY_SONIFICATION: |
| 297 | if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) { |
| 298 | legacyStrategy = STRATEGY_PHONE; |
| 299 | } |
| 300 | break; |
| 301 | |
| 302 | case STRATEGY_ACCESSIBILITY: |
| 303 | if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) || |
| 304 | outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) { |
| 305 | legacyStrategy = STRATEGY_SONIFICATION; |
| 306 | } |
| 307 | break; |
| 308 | |
| 309 | default: |
| 310 | return strategy; |
| 311 | } |
| 312 | } |
| 313 | return getProductStrategyFromLegacy(legacyStrategy); |
| 314 | } |
| 315 | |
Eric Laurent | 2802b86 | 2021-03-01 09:36:16 +0100 | [diff] [blame] | 316 | DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy, |
| 317 | DeviceVector availableOutputDevices, |
Eric Laurent | 2802b86 | 2021-03-01 09:36:16 +0100 | [diff] [blame] | 318 | const SwAudioOutputCollection &outputs) const |
| 319 | { |
| 320 | DeviceVector devices; |
| 321 | |
| 322 | switch (strategy) { |
| 323 | |
| 324 | case STRATEGY_TRANSMITTED_THROUGH_SPEAKER: |
| 325 | devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER); |
| 326 | break; |
| 327 | |
Eric Laurent | 2802b86 | 2021-03-01 09:36:16 +0100 | [diff] [blame] | 328 | case STRATEGY_PHONE: { |
Lorena Torres-Huerta | 07fd48a | 2022-08-24 19:18:53 +0000 | [diff] [blame] | 329 | // TODO(b/243670205): remove this logic that gives preference to last removable devices |
| 330 | // once a UX decision has been made |
Eric Laurent | c069759 | 2021-05-10 11:45:22 +0200 | [diff] [blame] | 331 | devices = availableOutputDevices.getFirstDevicesFromTypes( |
Lorena Torres-Huerta | 07fd48a | 2022-08-24 19:18:53 +0000 | [diff] [blame] | 332 | getLastRemovableMediaDevices(GROUP_NONE, { |
| 333 | // excluding HEARING_AID and BLE_HEADSET because Dialer uses |
| 334 | // setCommunicationDevice to select them explicitly |
| 335 | AUDIO_DEVICE_OUT_HEARING_AID, |
Jaideep Sharma | fd79c29 | 2023-10-10 00:11:22 +0530 | [diff] [blame] | 336 | AUDIO_DEVICE_OUT_BLE_HEADSET, |
| 337 | AUDIO_DEVICE_OUT_AUX_DIGITAL |
Lorena Torres-Huerta | 07fd48a | 2022-08-24 19:18:53 +0000 | [diff] [blame] | 338 | })); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 339 | if (!devices.isEmpty()) break; |
Eric Laurent | 454a2cc | 2022-01-26 11:56:13 +0100 | [diff] [blame] | 340 | devices = availableOutputDevices.getFirstDevicesFromTypes({ |
jiabin | 2923018 | 2023-04-04 21:02:36 +0000 | [diff] [blame] | 341 | AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_EARPIECE, |
| 342 | AUDIO_DEVICE_OUT_SPEAKER}); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 343 | } break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 344 | |
| 345 | case STRATEGY_SONIFICATION: |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 346 | case STRATEGY_ENFORCED_AUDIBLE: |
| 347 | // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION |
| 348 | // except: |
| 349 | // - when in call where it doesn't default to STRATEGY_PHONE behavior |
| 350 | // - in countries where not enforced in which case it follows STRATEGY_MEDIA |
| 351 | |
| 352 | if ((strategy == STRATEGY_SONIFICATION) || |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 353 | (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) { |
Jean-Michel Trivi | 00afde1 | 2023-02-01 20:46:32 +0000 | [diff] [blame] | 354 | // favor dock over speaker when available |
| 355 | devices = availableOutputDevices.getFirstDevicesFromTypes({ |
| 356 | AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER}); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 357 | } |
Eric Laurent | a8e0f02 | 2017-01-27 17:41:53 -0800 | [diff] [blame] | 358 | |
| 359 | // if SCO headset is connected and we are told to use it, play ringtone over |
| 360 | // speaker and BT SCO |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 361 | if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 362 | DeviceVector devices2; |
| 363 | devices2 = availableOutputDevices.getFirstDevicesFromTypes({ |
| 364 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, |
| 365 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO}); |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 366 | // Use ONLY Bluetooth SCO output when ringing in vibration mode |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 367 | if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 368 | && (strategy == STRATEGY_ENFORCED_AUDIBLE))) { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 369 | if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING) |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 370 | == AUDIO_POLICY_FORCE_BT_SCO) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 371 | if (!devices2.isEmpty()) { |
| 372 | devices = devices2; |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 373 | break; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | // Use both Bluetooth SCO and phone default output when ringing in normal mode |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 378 | if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy( |
| 379 | availableOutputDevices, STRATEGY_PHONE))) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 380 | if (strategy == STRATEGY_SONIFICATION) { |
| 381 | devices.replaceDevicesByType( |
| 382 | AUDIO_DEVICE_OUT_SPEAKER, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 383 | availableOutputDevices.getDevicesFromType( |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 384 | AUDIO_DEVICE_OUT_SPEAKER_SAFE)); |
juyuchen | 5fa0aed | 2018-05-16 10:58:37 +0800 | [diff] [blame] | 385 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 386 | if (!devices2.isEmpty()) { |
| 387 | devices.add(devices2); |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 388 | break; |
| 389 | } |
Eric Laurent | a8e0f02 | 2017-01-27 17:41:53 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
chenxin20 | e9f2d9f | 2024-06-26 10:51:31 +0800 | [diff] [blame] | 392 | |
| 393 | // if LEA headset is connected and we are told to use it, play ringtone over |
| 394 | // speaker and BT LEA |
| 395 | if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllBleSet()).isEmpty()) { |
| 396 | DeviceVector devices2; |
| 397 | devices2 = availableOutputDevices.getFirstDevicesFromTypes({ |
| 398 | AUDIO_DEVICE_OUT_BLE_HEADSET, AUDIO_DEVICE_OUT_BLE_SPEAKER}); |
| 399 | // Use ONLY Bluetooth LEA output when ringing in vibration mode |
| 400 | if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
| 401 | && (strategy == STRATEGY_ENFORCED_AUDIBLE))) { |
| 402 | if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING) |
| 403 | == AUDIO_POLICY_FORCE_BT_BLE) { |
| 404 | if (!devices2.isEmpty()) { |
| 405 | devices = devices2; |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | // Use both Bluetooth LEA and phone default output when ringing in normal mode |
| 411 | if (audio_is_ble_out_device(getPreferredDeviceTypeForLegacyStrategy( |
| 412 | availableOutputDevices, STRATEGY_PHONE))) { |
| 413 | if (strategy == STRATEGY_SONIFICATION) { |
| 414 | devices.replaceDevicesByType( |
| 415 | AUDIO_DEVICE_OUT_SPEAKER, |
| 416 | availableOutputDevices.getDevicesFromType( |
| 417 | AUDIO_DEVICE_OUT_SPEAKER_SAFE)); |
| 418 | } |
| 419 | if (!devices2.isEmpty()) { |
| 420 | devices.add(devices2); |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 426 | // The second device used for sonification is the same as the device used by media strategy |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 427 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 428 | |
Eric Laurent | 6cd5f90 | 2021-03-23 18:29:58 +0100 | [diff] [blame] | 429 | case STRATEGY_DTMF: |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 430 | case STRATEGY_ACCESSIBILITY: |
Eric Laurent | 6cd5f90 | 2021-03-23 18:29:58 +0100 | [diff] [blame] | 431 | case STRATEGY_SONIFICATION_RESPECTFUL: |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 432 | case STRATEGY_REROUTING: |
| 433 | case STRATEGY_MEDIA: { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 434 | DeviceVector devices2; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 435 | if (strategy != STRATEGY_SONIFICATION) { |
| 436 | // no sonification on remote submix (e.g. WFD) |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 437 | sp<DeviceDescriptor> remoteSubmix; |
| 438 | if ((remoteSubmix = availableOutputDevices.getDevice( |
| 439 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"), |
| 440 | AUDIO_FORMAT_DEFAULT)) != nullptr) { |
| 441 | devices2.add(remoteSubmix); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 442 | } |
| 443 | } |
Eric Laurent | a998686 | 2020-10-07 08:42:28 -0700 | [diff] [blame] | 444 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 445 | if ((devices2.isEmpty()) && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 446 | (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 447 | devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 448 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 449 | |
| 450 | // LE audio broadcast device is only used if: |
| 451 | // - No call is active |
| 452 | // - either MEDIA or SONIFICATION_RESPECTFUL is the highest priority active strategy |
| 453 | // OR the LE audio unicast device is not active |
| 454 | if (devices2.isEmpty() && !isInCall() |
| 455 | && (strategy == STRATEGY_MEDIA || strategy == STRATEGY_SONIFICATION_RESPECTFUL)) { |
| 456 | legacy_strategy topActiveStrategy = STRATEGY_NONE; |
| 457 | for (const auto &ps : getOrderedProductStrategies()) { |
| 458 | if (outputs.isStrategyActive(ps)) { |
| 459 | topActiveStrategy = mLegacyStrategyMap.find(ps) != end(mLegacyStrategyMap) ? |
| 460 | mLegacyStrategyMap.at(ps) : STRATEGY_NONE; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if (topActiveStrategy == STRATEGY_NONE || topActiveStrategy == STRATEGY_MEDIA |
| 466 | || topActiveStrategy == STRATEGY_SONIFICATION_RESPECTFUL |
| 467 | || !outputs.isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet())) { |
| 468 | devices2 = |
| 469 | availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST); |
| 470 | } |
| 471 | } |
| 472 | |
Baekgyeong Kim | 0845152 | 2019-11-01 20:40:02 +0900 | [diff] [blame] | 473 | if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) { |
Jaideep Sharma | fd79c29 | 2023-10-10 00:11:22 +0530 | [diff] [blame] | 474 | std::vector<audio_devices_t> excludedDevices; |
| 475 | // no sonification on aux digital (e.g. HDMI) |
| 476 | if (strategy == STRATEGY_SONIFICATION) { |
| 477 | excludedDevices.push_back(AUDIO_DEVICE_OUT_AUX_DIGITAL); |
| 478 | } |
Eric Laurent | a998686 | 2020-10-07 08:42:28 -0700 | [diff] [blame] | 479 | if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) { |
Baekgyeong Kim | 0845152 | 2019-11-01 20:40:02 +0900 | [diff] [blame] | 480 | // Get the last connected device of wired and bluetooth a2dp |
| 481 | devices2 = availableOutputDevices.getFirstDevicesFromTypes( |
Jaideep Sharma | fd79c29 | 2023-10-10 00:11:22 +0530 | [diff] [blame] | 482 | getLastRemovableMediaDevices(GROUP_NONE, excludedDevices)); |
Baekgyeong Kim | 0845152 | 2019-11-01 20:40:02 +0900 | [diff] [blame] | 483 | } else { |
| 484 | // Get the last connected device of wired except bluetooth a2dp |
| 485 | devices2 = availableOutputDevices.getFirstDevicesFromTypes( |
Jaideep Sharma | fd79c29 | 2023-10-10 00:11:22 +0530 | [diff] [blame] | 486 | getLastRemovableMediaDevices(GROUP_WIRED, excludedDevices)); |
Baekgyeong Kim | 0845152 | 2019-11-01 20:40:02 +0900 | [diff] [blame] | 487 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 488 | } |
Eric Laurent | ffff1f7 | 2024-06-12 19:08:15 +0000 | [diff] [blame] | 489 | |
| 490 | if (com::android::media::audioserver::use_bt_sco_for_media()) { |
| 491 | if (devices2.isEmpty() && isBtScoActive(availableOutputDevices, outputs)) { |
| 492 | devices2 = availableOutputDevices.getFirstDevicesFromTypes( |
| 493 | { AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, |
| 494 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, |
| 495 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO}); |
| 496 | } |
| 497 | } |
| 498 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 499 | if ((devices2.isEmpty()) && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 500 | (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 501 | devices2 = availableOutputDevices.getDevicesFromType( |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 502 | AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 503 | } |
Eric Laurent | ffff1f7 | 2024-06-12 19:08:15 +0000 | [diff] [blame] | 504 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 505 | if (devices2.isEmpty()) { |
Eric Laurent | 454a2cc | 2022-01-26 11:56:13 +0100 | [diff] [blame] | 506 | devices2 = availableOutputDevices.getFirstDevicesFromTypes({ |
| 507 | AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER}); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 508 | } |
Eric Laurent | ffff1f7 | 2024-06-12 19:08:15 +0000 | [diff] [blame] | 509 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 510 | DeviceVector devices3; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 511 | if (strategy == STRATEGY_MEDIA) { |
| 512 | // ARC, SPDIF and AUX_LINE can co-exist with others. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 513 | devices3 = availableOutputDevices.getDevicesFromTypes({ |
Kuowei Li | 01a686b | 2020-10-27 16:54:39 +0800 | [diff] [blame] | 514 | AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC, |
| 515 | AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE, |
| 516 | }); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 517 | } |
| 518 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 519 | devices2.add(devices3); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 520 | // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or |
| 521 | // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 522 | devices.add(devices2); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 523 | |
| 524 | // If hdmi system audio mode is on, remove speaker out of output list. |
| 525 | if ((strategy == STRATEGY_MEDIA) && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 526 | (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) == |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 527 | AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 528 | devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER)); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 529 | } |
Jean-Michel Trivi | 654afa0 | 2017-05-11 14:12:33 -0700 | [diff] [blame] | 530 | |
Eric Laurent | 6cd5f90 | 2021-03-23 18:29:58 +0100 | [diff] [blame] | 531 | bool mediaActiveLocally = |
| 532 | outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC), |
| 533 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY) |
| 534 | || outputs.isActiveLocally( |
| 535 | toVolumeSource(AUDIO_STREAM_ACCESSIBILITY), |
| 536 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY); |
Carter Hsu | 524ee46 | 2021-07-20 08:47:31 +0800 | [diff] [blame] | 537 | |
| 538 | bool ringActiveLocally = outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING), 0); |
| 539 | // - for STRATEGY_SONIFICATION and ringtone active: |
Jean-Michel Trivi | 654afa0 | 2017-05-11 14:12:33 -0700 | [diff] [blame] | 540 | // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead |
Eric Laurent | 6cd5f90 | 2021-03-23 18:29:58 +0100 | [diff] [blame] | 541 | // - for STRATEGY_SONIFICATION_RESPECTFUL: |
| 542 | // if no media is playing on the device, check for mandatory use of "safe" speaker |
| 543 | // when media would have played on speaker, and the safe speaker path is available |
Carter Hsu | 524ee46 | 2021-07-20 08:47:31 +0800 | [diff] [blame] | 544 | if (strategy == STRATEGY_SONIFICATION || ringActiveLocally |
Eric Laurent | 6cd5f90 | 2021-03-23 18:29:58 +0100 | [diff] [blame] | 545 | || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 546 | devices.replaceDevicesByType( |
| 547 | AUDIO_DEVICE_OUT_SPEAKER, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 548 | availableOutputDevices.getDevicesFromType( |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 549 | AUDIO_DEVICE_OUT_SPEAKER_SAFE)); |
Jean-Michel Trivi | 654afa0 | 2017-05-11 14:12:33 -0700 | [diff] [blame] | 550 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 551 | } break; |
| 552 | |
Eric Laurent | 21777f8 | 2019-12-06 18:12:06 -0800 | [diff] [blame] | 553 | case STRATEGY_CALL_ASSISTANT: |
| 554 | devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 555 | break; |
| 556 | |
Eric Laurent | d976693 | 2020-08-07 14:01:22 -0700 | [diff] [blame] | 557 | case STRATEGY_NONE: |
| 558 | // Happens when internal strategies are processed ("rerouting", "patch"...) |
| 559 | break; |
| 560 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 561 | default: |
Eric Laurent | d976693 | 2020-08-07 14:01:22 -0700 | [diff] [blame] | 562 | ALOGW("%s unknown strategy: %d", __func__, strategy); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 563 | break; |
| 564 | } |
| 565 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 566 | if (devices.isEmpty()) { |
jiabin | d0a8d6f | 2022-09-19 20:32:01 +0000 | [diff] [blame] | 567 | ALOGI("%s no device found for strategy %d", __func__, strategy); |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 568 | sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice(); |
| 569 | if (defaultOutputDevice != nullptr) { |
| 570 | devices.add(defaultOutputDevice); |
| 571 | } |
| 572 | ALOGE_IF(devices.isEmpty(), |
Eric Laurent | d976693 | 2020-08-07 14:01:22 -0700 | [diff] [blame] | 573 | "%s no default device defined", __func__); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 574 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 575 | |
Eric Laurent | d976693 | 2020-08-07 14:01:22 -0700 | [diff] [blame] | 576 | ALOGVV("%s strategy %d, device %s", __func__, |
jiabin | cd51052 | 2020-01-22 09:40:55 -0800 | [diff] [blame] | 577 | strategy, dumpDeviceTypes(devices.types()).c_str()); |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 578 | return devices; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 579 | } |
| 580 | |
jiabin | 14662b5 | 2023-03-06 21:35:29 +0000 | [diff] [blame] | 581 | DeviceVector Engine::getPreferredAvailableDevicesForInputSource( |
| 582 | const DeviceVector& availableInputDevices, audio_source_t inputSource) const { |
| 583 | DeviceVector preferredAvailableDevVec = {}; |
| 584 | AudioDeviceTypeAddrVector preferredDevices; |
| 585 | const status_t status = getDevicesForRoleAndCapturePreset( |
| 586 | inputSource, DEVICE_ROLE_PREFERRED, preferredDevices); |
| 587 | if (status == NO_ERROR) { |
| 588 | // Only use preferred devices when they are all available. |
| 589 | preferredAvailableDevVec = |
| 590 | availableInputDevices.getDevicesFromDeviceTypeAddrVec(preferredDevices); |
| 591 | if (preferredAvailableDevVec.size() == preferredDevices.size()) { |
| 592 | ALOGVV("%s using pref device %s for source %u", |
| 593 | __func__, preferredAvailableDevVec.toString().c_str(), inputSource); |
| 594 | return preferredAvailableDevVec; |
| 595 | } |
| 596 | } |
| 597 | return preferredAvailableDevVec; |
| 598 | } |
| 599 | |
| 600 | DeviceVector Engine::getDisabledDevicesForInputSource( |
| 601 | const DeviceVector& availableInputDevices, audio_source_t inputSource) const { |
| 602 | DeviceVector disabledDevices = {}; |
| 603 | AudioDeviceTypeAddrVector disabledDevicesTypeAddr; |
| 604 | const status_t status = getDevicesForRoleAndCapturePreset( |
| 605 | inputSource, DEVICE_ROLE_DISABLED, disabledDevicesTypeAddr); |
| 606 | if (status == NO_ERROR) { |
| 607 | disabledDevices = |
| 608 | availableInputDevices.getDevicesFromDeviceTypeAddrVec(disabledDevicesTypeAddr); |
| 609 | } |
| 610 | return disabledDevices; |
| 611 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 612 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 613 | sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 614 | { |
Eric Laurent | af37777 | 2019-03-29 14:50:21 -0700 | [diff] [blame] | 615 | const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
| 616 | const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 617 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 618 | DeviceVector availableDevices = availableInputDevices; |
Eric Laurent | 5ee8d12 | 2019-04-19 15:41:52 -0700 | [diff] [blame] | 619 | sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput(); |
jiabin | b6b0748 | 2019-09-26 18:05:18 -0700 | [diff] [blame] | 620 | DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector() |
| 621 | : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle()); |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 622 | sp<DeviceDescriptor> device; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 623 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 624 | // when a call is active, force device selection to match source VOICE_COMMUNICATION |
| 625 | // for most other input sources to avoid rerouting call TX audio |
| 626 | if (isInCall()) { |
| 627 | switch (inputSource) { |
| 628 | case AUDIO_SOURCE_DEFAULT: |
| 629 | case AUDIO_SOURCE_MIC: |
| 630 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
| 631 | case AUDIO_SOURCE_UNPROCESSED: |
| 632 | case AUDIO_SOURCE_HOTWORD: |
| 633 | case AUDIO_SOURCE_CAMCORDER: |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 634 | case AUDIO_SOURCE_VOICE_PERFORMANCE: |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 635 | case AUDIO_SOURCE_ULTRASOUND: |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 636 | inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 637 | break; |
| 638 | default: |
| 639 | break; |
| 640 | } |
| 641 | } |
| 642 | |
jiabin | 14662b5 | 2023-03-06 21:35:29 +0000 | [diff] [blame] | 643 | // Use the preferred device for the input source if it is available. |
| 644 | DeviceVector preferredInputDevices = getPreferredAvailableDevicesForInputSource( |
| 645 | availableDevices, inputSource); |
| 646 | if (!preferredInputDevices.isEmpty()) { |
| 647 | // Currently, only support single device for input. The public JAVA API also only |
| 648 | // support setting single device as preferred device. In that case, returning the |
| 649 | // first device is OK here. |
| 650 | return preferredInputDevices[0]; |
| 651 | } |
| 652 | // Remove the disabled device for the input source from the available input device list. |
| 653 | DeviceVector disabledInputDevices = getDisabledDevicesForInputSource( |
| 654 | availableDevices, inputSource); |
| 655 | availableDevices.remove(disabledInputDevices); |
| 656 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 657 | audio_devices_t commDeviceType = |
| 658 | getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE); |
| 659 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 660 | switch (inputSource) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 661 | case AUDIO_SOURCE_DEFAULT: |
| 662 | case AUDIO_SOURCE_MIC: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 663 | device = availableDevices.getDevice( |
| 664 | AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT); |
| 665 | if (device != nullptr) break; |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 666 | if (audio_is_bluetooth_out_sco_device(commDeviceType)) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 667 | device = availableDevices.getDevice( |
| 668 | AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT); |
| 669 | if (device != nullptr) break; |
| 670 | } |
| 671 | device = availableDevices.getFirstExistingDevice({ |
Eric Laurent | 93666b2 | 2022-05-13 14:42:13 +0200 | [diff] [blame] | 672 | AUDIO_DEVICE_IN_WIRED_HEADSET, |
Eric Laurent | a998686 | 2020-10-07 08:42:28 -0700 | [diff] [blame] | 673 | AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE, |
wei wang | a702052 | 2020-12-10 21:36:11 -0500 | [diff] [blame] | 674 | AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC}); |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 675 | break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 676 | |
| 677 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
| 678 | // Allow only use of devices on primary input if in call and HAL does not support routing |
| 679 | // to voice call path. |
| 680 | if ((getPhoneState() == AUDIO_MODE_IN_CALL) && |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 681 | (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, |
| 682 | String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) { |
Eric Laurent | 5569bf4 | 2023-08-30 15:53:55 +0200 | [diff] [blame] | 683 | if (!availablePrimaryDevices.isEmpty()) { |
| 684 | availableDevices = availablePrimaryDevices; |
| 685 | } else { |
| 686 | ALOGE("%s, AUDIO_SOURCE_VOICE_COMMUNICATION: Primary devices not found", __func__); |
| 687 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 690 | if (audio_is_bluetooth_out_sco_device(commDeviceType)) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 691 | // if SCO device is requested but no SCO device is available, fall back to default case |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 692 | device = availableDevices.getDevice( |
| 693 | AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT); |
| 694 | if (device != nullptr) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 695 | break; |
| 696 | } |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 697 | } |
| 698 | switch (commDeviceType) { |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 699 | case AUDIO_DEVICE_OUT_SPEAKER: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 700 | device = availableDevices.getFirstExistingDevice({ |
Eric Laurent | 6435dd7 | 2020-12-02 14:42:42 +0100 | [diff] [blame] | 701 | AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC, |
| 702 | AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET}); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 703 | break; |
Eric Laurent | cedd5b5 | 2023-03-22 00:03:31 +0000 | [diff] [blame] | 704 | case AUDIO_DEVICE_OUT_BLE_HEADSET: |
| 705 | device = availableDevices.getDevice( |
| 706 | AUDIO_DEVICE_IN_BLE_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT); |
| 707 | if (device != nullptr) { |
| 708 | break; |
| 709 | } |
| 710 | ALOGE("%s LE Audio selected for communication but input device not available", |
| 711 | __func__); |
| 712 | FALLTHROUGH_INTENDED; |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 713 | default: // FORCE_NONE |
| 714 | device = availableDevices.getFirstExistingDevice({ |
| 715 | AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET, |
wei wang | a702052 | 2020-12-10 21:36:11 -0500 | [diff] [blame] | 716 | AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE, |
| 717 | AUDIO_DEVICE_IN_BUILTIN_MIC}); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 718 | break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 719 | } |
| 720 | break; |
| 721 | |
| 722 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
rago | 8a397d5 | 2015-12-02 11:27:57 -0800 | [diff] [blame] | 723 | case AUDIO_SOURCE_UNPROCESSED: |
wei wang | a702052 | 2020-12-10 21:36:11 -0500 | [diff] [blame] | 724 | if (audio_is_bluetooth_out_sco_device(commDeviceType)) { |
| 725 | device = availableDevices.getDevice( |
| 726 | AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT); |
| 727 | if (device != nullptr) break; |
Eric Laurent | 5ee8d12 | 2019-04-19 15:41:52 -0700 | [diff] [blame] | 728 | } |
wei wang | a702052 | 2020-12-10 21:36:11 -0500 | [diff] [blame] | 729 | // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC, |
| 730 | // because sometimes user want to do voice search by bt remote |
| 731 | // even if BUILDIN_MIC is available. |
| 732 | device = availableDevices.getFirstExistingDevice({ |
Eric Laurent | 93666b2 | 2022-05-13 14:42:13 +0200 | [diff] [blame] | 733 | AUDIO_DEVICE_IN_WIRED_HEADSET, |
wei wang | a702052 | 2020-12-10 21:36:11 -0500 | [diff] [blame] | 734 | AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE, |
| 735 | AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC}); |
| 736 | |
| 737 | break; |
| 738 | case AUDIO_SOURCE_HOTWORD: |
| 739 | // We should not use primary output criteria for Hotword but rather limit |
| 740 | // to devices attached to the same HW module as the build in mic |
Eric Laurent | 5569bf4 | 2023-08-30 15:53:55 +0200 | [diff] [blame] | 741 | if (!availablePrimaryDevices.isEmpty()) { |
| 742 | availableDevices = availablePrimaryDevices; |
| 743 | } else { |
| 744 | ALOGE("%s, AUDIO_SOURCE_HOTWORD: Primary devices not found", __func__); |
| 745 | } |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 746 | if (audio_is_bluetooth_out_sco_device(commDeviceType)) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 747 | device = availableDevices.getDevice( |
| 748 | AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT); |
| 749 | if (device != nullptr) break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 750 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 751 | device = availableDevices.getFirstExistingDevice({ |
Eric Laurent | 93666b2 | 2022-05-13 14:42:13 +0200 | [diff] [blame] | 752 | AUDIO_DEVICE_IN_WIRED_HEADSET, |
Eric Laurent | a998686 | 2020-10-07 08:42:28 -0700 | [diff] [blame] | 753 | AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE, |
| 754 | AUDIO_DEVICE_IN_BUILTIN_MIC}); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 755 | break; |
| 756 | case AUDIO_SOURCE_CAMCORDER: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 757 | // For a device without built-in mic, adding usb device |
| 758 | device = availableDevices.getFirstExistingDevice({ |
| 759 | AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC, |
| 760 | AUDIO_DEVICE_IN_USB_DEVICE}); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 761 | break; |
| 762 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 763 | case AUDIO_SOURCE_VOICE_CALL: |
Eric Laurent | 5ee8d12 | 2019-04-19 15:41:52 -0700 | [diff] [blame] | 764 | case AUDIO_SOURCE_VOICE_UPLINK: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 765 | device = availableDevices.getDevice( |
| 766 | AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 767 | break; |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 768 | case AUDIO_SOURCE_VOICE_PERFORMANCE: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 769 | device = availableDevices.getFirstExistingDevice({ |
| 770 | AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET, |
wei wang | a702052 | 2020-12-10 21:36:11 -0500 | [diff] [blame] | 771 | AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE, |
| 772 | AUDIO_DEVICE_IN_BUILTIN_MIC}); |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 773 | break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 774 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 775 | device = availableDevices.getDevice( |
| 776 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 777 | break; |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 778 | case AUDIO_SOURCE_FM_TUNER: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 779 | device = availableDevices.getDevice( |
| 780 | AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 781 | break; |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 782 | case AUDIO_SOURCE_ECHO_REFERENCE: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 783 | device = availableDevices.getDevice( |
| 784 | AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT); |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 785 | break; |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 786 | case AUDIO_SOURCE_ULTRASOUND: |
| 787 | device = availableDevices.getFirstExistingDevice({ |
| 788 | AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC}); |
| 789 | break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 790 | default: |
| 791 | ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); |
| 792 | break; |
| 793 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 794 | if (device == nullptr) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 795 | ALOGV("getDeviceForInputSource() no device found for source %d", inputSource); |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 796 | device = availableDevices.getDevice( |
| 797 | AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT); |
| 798 | ALOGE_IF(device == nullptr, |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 799 | "getDeviceForInputSource() no default device defined"); |
| 800 | } |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 801 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 802 | ALOGV_IF(device != nullptr, |
| 803 | "getDeviceForInputSource()input source %d, device %08x", |
| 804 | inputSource, device->type()); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 805 | return device; |
| 806 | } |
| 807 | |
jiabin | 2923018 | 2023-04-04 21:02:36 +0000 | [diff] [blame] | 808 | void Engine::setStrategyDevices(const sp<ProductStrategy>& strategy, const DeviceVector &devices) { |
| 809 | strategy->setDeviceTypes(devices.types()); |
| 810 | strategy->setDeviceAddress(devices.getFirstValidAddress().c_str()); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 811 | } |
| 812 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 813 | product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const { |
| 814 | for (const auto& strategyMap : mLegacyStrategyMap) { |
| 815 | if (strategyMap.second == legacyStrategy) { |
| 816 | return strategyMap.first; |
| 817 | } |
| 818 | } |
| 819 | return PRODUCT_STRATEGY_NONE; |
| 820 | } |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 821 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 822 | audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy( |
| 823 | const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const { |
| 824 | product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy); |
| 825 | DeviceVector devices = getPreferredAvailableDevicesForProductStrategy( |
| 826 | availableOutputDevices, strategy); |
| 827 | if (devices.size() > 0) { |
| 828 | return devices[0]->type(); |
| 829 | } |
| 830 | return AUDIO_DEVICE_NONE; |
| 831 | } |
| 832 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 833 | DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const { |
Eric Laurent | 6cd5f90 | 2021-03-23 18:29:58 +0100 | [diff] [blame] | 834 | const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs(); |
| 835 | |
| 836 | // Take context into account to remap product strategy before |
| 837 | // checking preferred device for strategy and applying default routing rules |
| 838 | strategy = remapStrategyFromContext(strategy, outputs); |
| 839 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 840 | auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ? |
| 841 | mLegacyStrategyMap.at(strategy) : STRATEGY_NONE; |
| 842 | |
Eric Laurent | 6cd5f90 | 2021-03-23 18:29:58 +0100 | [diff] [blame] | 843 | DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
Eric Laurent | 2802b86 | 2021-03-01 09:36:16 +0100 | [diff] [blame] | 844 | |
Eric Laurent | 6cd5f90 | 2021-03-23 18:29:58 +0100 | [diff] [blame] | 845 | filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs); |
Eric Laurent | 2802b86 | 2021-03-01 09:36:16 +0100 | [diff] [blame] | 846 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 847 | // check if this strategy has a preferred device that is available, |
| 848 | // if yes, give priority to it. |
| 849 | DeviceVector preferredAvailableDevVec = |
| 850 | getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy); |
| 851 | if (!preferredAvailableDevVec.isEmpty()) { |
| 852 | return preferredAvailableDevVec; |
| 853 | } |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 854 | |
jiabin | d0a8d6f | 2022-09-19 20:32:01 +0000 | [diff] [blame] | 855 | // Remove all disabled devices from the available device list. |
| 856 | DeviceVector disabledDevVec = |
| 857 | getDisabledDevicesForProductStrategy(availableOutputDevices, strategy); |
| 858 | availableOutputDevices.remove(disabledDevVec); |
| 859 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 860 | return getDevicesForStrategyInt(legacyStrategy, |
| 861 | availableOutputDevices, |
Eric Laurent | 6cd5f90 | 2021-03-23 18:29:58 +0100 | [diff] [blame] | 862 | outputs); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes, |
| 866 | const sp<DeviceDescriptor> &preferredDevice, |
| 867 | bool fromCache) const |
| 868 | { |
| 869 | // First check for explict routing device |
| 870 | if (preferredDevice != nullptr) { |
| 871 | ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str()); |
| 872 | return DeviceVector(preferredDevice); |
| 873 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 874 | product_strategy_t strategy = getProductStrategyForAttributes(attributes); |
Eric Laurent | af37777 | 2019-03-29 14:50:21 -0700 | [diff] [blame] | 875 | const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 876 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 877 | // |
| 878 | // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to |
| 879 | // be by APM? |
| 880 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 881 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 882 | // case the last active client route is used |
| 883 | sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices); |
| 884 | if (device != nullptr) { |
| 885 | return DeviceVector(device); |
| 886 | } |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 887 | |
| 888 | return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy); |
| 889 | } |
| 890 | |
| 891 | DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const |
| 892 | { |
| 893 | auto attributes = getAttributesForStreamType(stream); |
| 894 | return getOutputDevicesForAttributes(attributes, nullptr, fromCache); |
| 895 | } |
| 896 | |
| 897 | sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr, |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 898 | uid_t uid, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 899 | audio_session_t session, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 900 | sp<AudioPolicyMix> *mix) const |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 901 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 902 | const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection(); |
Eric Laurent | af37777 | 2019-03-29 14:50:21 -0700 | [diff] [blame] | 903 | const auto availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 904 | const auto &inputs = getApmObserver()->getInputs(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 905 | std::string address; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 906 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 907 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 908 | // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered |
| 909 | // first as it used to be by APM? |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 910 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 911 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 912 | // case the last active client route is used |
| 913 | sp<DeviceDescriptor> device = |
| 914 | findPreferredDevice(inputs, attr.source, availableInputDevices); |
| 915 | if (device != nullptr) { |
| 916 | return device; |
| 917 | } |
| 918 | |
Jan Sebechlebsky | 28ed945 | 2022-09-15 17:57:42 +0200 | [diff] [blame] | 919 | device = policyMixes.getDeviceAndMixForInputSource(attr, |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 920 | availableInputDevices, |
| 921 | uid, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 922 | session, |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 923 | mix); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 924 | if (device != nullptr) { |
| 925 | return device; |
| 926 | } |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 927 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 928 | device = getDeviceForInputSource(attr.source); |
| 929 | if (device == nullptr || !audio_is_remote_submix_device(device->type())) { |
| 930 | // Return immediately if the device is null or it is not a remote submix device. |
| 931 | return device; |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 932 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 933 | |
| 934 | // For remote submix device, try to find the device by address. |
| 935 | address = "0"; |
| 936 | std::size_t pos; |
| 937 | std::string tags { attr.tags }; |
| 938 | if ((pos = tags.find("addr=")) != std::string::npos) { |
| 939 | address = tags.substr(pos + std::strlen("addr=")); |
| 940 | } |
| 941 | return availableInputDevices.getDevice(device->type(), |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 942 | String8(address.c_str()), |
| 943 | AUDIO_FORMAT_DEFAULT); |
| 944 | } |
| 945 | |
Mikhail Naganov | 9e459d7 | 2023-05-05 17:36:39 -0700 | [diff] [blame] | 946 | } // namespace android::audio_policy |