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