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