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