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