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