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> |
| 30 | #include <AudioPort.h> |
| 31 | #include <IOProfile.h> |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 32 | #include <AudioIODescriptorInterface.h> |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 33 | #include <policy.h> |
| 34 | #include <utils/String8.h> |
| 35 | #include <utils/Log.h> |
| 36 | |
| 37 | namespace android |
| 38 | { |
| 39 | namespace audio_policy |
| 40 | { |
| 41 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 42 | struct legacy_strategy_map { const char *name; legacy_strategy id; }; |
| 43 | static const std::vector<legacy_strategy_map> gLegacyStrategy = { |
| 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 | }; |
| 56 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 57 | Engine::Engine() |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 58 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 59 | auto result = EngineBase::loadAudioPolicyEngineConfig(); |
| 60 | ALOGE_IF(result.nbSkippedElement != 0, |
| 61 | "Policy Engine configuration is partially invalid, skipped %zu elements", |
| 62 | result.nbSkippedElement); |
| 63 | |
| 64 | for (const auto &strategy : gLegacyStrategy) { |
| 65 | mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 69 | status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) |
| 70 | { |
| 71 | switch(usage) { |
| 72 | case AUDIO_POLICY_FORCE_FOR_COMMUNICATION: |
| 73 | if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO && |
| 74 | config != AUDIO_POLICY_FORCE_NONE) { |
| 75 | ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); |
| 76 | return BAD_VALUE; |
| 77 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 78 | break; |
| 79 | case AUDIO_POLICY_FORCE_FOR_MEDIA: |
| 80 | if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP && |
| 81 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 82 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 83 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE && |
| 84 | config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) { |
| 85 | ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config); |
| 86 | return BAD_VALUE; |
| 87 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 88 | break; |
| 89 | case AUDIO_POLICY_FORCE_FOR_RECORD: |
| 90 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 91 | config != AUDIO_POLICY_FORCE_NONE) { |
| 92 | ALOGW("setForceUse() invalid config %d for FOR_RECORD", config); |
| 93 | return BAD_VALUE; |
| 94 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 95 | break; |
| 96 | case AUDIO_POLICY_FORCE_FOR_DOCK: |
| 97 | if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK && |
| 98 | config != AUDIO_POLICY_FORCE_BT_DESK_DOCK && |
| 99 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 100 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 101 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) { |
| 102 | ALOGW("setForceUse() invalid config %d for FOR_DOCK", config); |
| 103 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 104 | break; |
| 105 | case AUDIO_POLICY_FORCE_FOR_SYSTEM: |
| 106 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 107 | config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 108 | ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); |
| 109 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 110 | break; |
| 111 | case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO: |
| 112 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 113 | config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) { |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 114 | ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config); |
| 115 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 116 | break; |
| 117 | case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND: |
| 118 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 119 | config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER && |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 120 | config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS && |
| 121 | config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 122 | ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config); |
| 123 | return BAD_VALUE; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 124 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 125 | break; |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 126 | case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING: |
| 127 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) { |
| 128 | ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config); |
| 129 | return BAD_VALUE; |
| 130 | } |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 131 | break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 132 | default: |
| 133 | ALOGW("setForceUse() invalid usage %d", usage); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 134 | break; // TODO return BAD_VALUE? |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 135 | } |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 136 | return EngineBase::setForceUse(usage, config); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 137 | } |
| 138 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 139 | DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy, |
| 140 | DeviceVector availableOutputDevices, |
| 141 | DeviceVector availableInputDevices, |
| 142 | const SwAudioOutputCollection &outputs) const |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 143 | { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 144 | DeviceVector devices; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 145 | |
| 146 | switch (strategy) { |
| 147 | |
| 148 | case STRATEGY_TRANSMITTED_THROUGH_SPEAKER: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 149 | devices = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 150 | break; |
| 151 | |
| 152 | case STRATEGY_SONIFICATION_RESPECTFUL: |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 153 | if (isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 154 | devices = getDevicesForStrategyInt( |
| 155 | STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 156 | } else { |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 157 | bool media_active_locally = |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 158 | outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC), |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 159 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY) |
| 160 | || outputs.isActiveLocally( |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 161 | toVolumeSource(AUDIO_STREAM_ACCESSIBILITY), |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 162 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY); |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 163 | // routing is same as media without the "remote" device |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 164 | availableOutputDevices.remove(availableOutputDevices.getDevicesFromTypeMask( |
| 165 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX)); |
| 166 | devices = getDevicesForStrategyInt(STRATEGY_MEDIA, |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 167 | availableOutputDevices, |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 168 | availableInputDevices, outputs); |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 169 | // if no media is playing on the device, check for mandatory use of "safe" speaker |
| 170 | // when media would have played on speaker, and the safe speaker path is available |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 171 | if (!media_active_locally) { |
| 172 | devices.replaceDevicesByType( |
| 173 | AUDIO_DEVICE_OUT_SPEAKER, |
| 174 | availableOutputDevices.getDevicesFromTypeMask( |
| 175 | AUDIO_DEVICE_OUT_SPEAKER_SAFE)); |
Eric Laurent | 9a7d922 | 2015-07-02 15:30:23 -0700 | [diff] [blame] | 176 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 177 | } |
| 178 | break; |
| 179 | |
| 180 | case STRATEGY_DTMF: |
| 181 | if (!isInCall()) { |
| 182 | // when off call, DTMF strategy follows the same rules as MEDIA strategy |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 183 | devices = getDevicesForStrategyInt( |
| 184 | STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 185 | break; |
| 186 | } |
| 187 | // when in call, DTMF and PHONE strategies follow the same rules |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 188 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 189 | |
| 190 | case STRATEGY_PHONE: |
| 191 | // Force use of only devices on primary output if: |
| 192 | // - in call AND |
| 193 | // - cannot route from voice call RX OR |
| 194 | // - audio HAL version is < 3.0 and TX device is on the primary HW module |
| 195 | if (getPhoneState() == AUDIO_MODE_IN_CALL) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 196 | audio_devices_t txDevice = getDeviceForInputSource( |
| 197 | AUDIO_SOURCE_VOICE_COMMUNICATION)->type(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 198 | sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput(); |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 199 | DeviceVector availPrimaryInputDevices = |
| 200 | availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle()); |
Eric Laurent | 58a73fc | 2018-02-21 18:46:13 -0800 | [diff] [blame] | 201 | |
| 202 | // TODO: getPrimaryOutput return only devices from first module in |
| 203 | // audio_policy_configuration.xml, hearing aid is not there, but it's |
| 204 | // a primary device |
| 205 | // FIXME: this is not the right way of solving this problem |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 206 | DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypeMask( |
| 207 | primaryOutput->supportedDevices().types()); |
| 208 | availPrimaryOutputDevices.add( |
| 209 | availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_HEARING_AID)); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 210 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 211 | if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX, |
| 212 | String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) || |
| 213 | ((availPrimaryInputDevices.getDevice( |
| 214 | txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) && |
| 215 | (primaryOutput->getAudioPort()->getModuleVersionMajor() < 3))) { |
| 216 | availableOutputDevices = availPrimaryOutputDevices; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 217 | } |
| 218 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 219 | // for phone strategy, we first consider the forced use and then the available devices by |
| 220 | // order of priority |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 221 | switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 222 | case AUDIO_POLICY_FORCE_BT_SCO: |
| 223 | if (!isInCall() || strategy != STRATEGY_DTMF) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 224 | devices = availableOutputDevices.getDevicesFromTypeMask( |
| 225 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT); |
| 226 | if (!devices.isEmpty()) break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 227 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 228 | devices = availableOutputDevices.getFirstDevicesFromTypes({ |
| 229 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, AUDIO_DEVICE_OUT_BLUETOOTH_SCO}); |
| 230 | if (!devices.isEmpty()) break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 231 | // if SCO device is requested but no SCO device is available, fall back to default case |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 232 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 233 | |
| 234 | default: // FORCE_NONE |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 235 | devices = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_HEARING_AID); |
| 236 | if (!devices.isEmpty()) break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 237 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP |
| 238 | if (!isInCall() && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 239 | (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 240 | outputs.isA2dpSupported()) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 241 | devices = availableOutputDevices.getFirstDevicesFromTypes({ |
| 242 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 243 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES}); |
| 244 | if (!devices.isEmpty()) break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 245 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 246 | devices = availableOutputDevices.getFirstDevicesFromTypes({ |
| 247 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE, AUDIO_DEVICE_OUT_WIRED_HEADSET, |
| 248 | AUDIO_DEVICE_OUT_LINE, AUDIO_DEVICE_OUT_USB_HEADSET, |
| 249 | AUDIO_DEVICE_OUT_USB_DEVICE}); |
| 250 | if (!devices.isEmpty()) break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 251 | if (!isInCall()) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 252 | devices = availableOutputDevices.getFirstDevicesFromTypes({ |
| 253 | AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, |
| 254 | AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET}); |
| 255 | if (!devices.isEmpty()) break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 256 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 257 | devices = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_EARPIECE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 258 | break; |
| 259 | |
| 260 | case AUDIO_POLICY_FORCE_SPEAKER: |
| 261 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to |
| 262 | // A2DP speaker when forcing to speaker output |
| 263 | if (!isInCall() && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 264 | (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 265 | outputs.isA2dpSupported()) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 266 | devices = availableOutputDevices.getDevicesFromTypeMask( |
| 267 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER); |
| 268 | if (!devices.isEmpty()) break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 269 | } |
| 270 | if (!isInCall()) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 271 | devices = availableOutputDevices.getFirstDevicesFromTypes({ |
| 272 | AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_USB_DEVICE, |
| 273 | AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_AUX_DIGITAL, |
| 274 | AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET}); |
| 275 | if (!devices.isEmpty()) break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 276 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 277 | devices = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | break; |
| 281 | |
| 282 | case STRATEGY_SONIFICATION: |
| 283 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 284 | // If incall, just select the STRATEGY_PHONE device |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 285 | if (isInCall() || |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 286 | outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 287 | devices = getDevicesForStrategyInt( |
| 288 | STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 289 | break; |
| 290 | } |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 291 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 292 | |
| 293 | case STRATEGY_ENFORCED_AUDIBLE: |
| 294 | // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION |
| 295 | // except: |
| 296 | // - when in call where it doesn't default to STRATEGY_PHONE behavior |
| 297 | // - in countries where not enforced in which case it follows STRATEGY_MEDIA |
| 298 | |
| 299 | if ((strategy == STRATEGY_SONIFICATION) || |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 300 | (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 301 | devices = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 302 | } |
Eric Laurent | a8e0f02 | 2017-01-27 17:41:53 -0800 | [diff] [blame] | 303 | |
| 304 | // if SCO headset is connected and we are told to use it, play ringtone over |
| 305 | // speaker and BT SCO |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 306 | if (!availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_ALL_SCO).isEmpty()) { |
| 307 | DeviceVector devices2; |
| 308 | devices2 = availableOutputDevices.getFirstDevicesFromTypes({ |
| 309 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, |
| 310 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO}); |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 311 | // Use ONLY Bluetooth SCO output when ringing in vibration mode |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 312 | if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 313 | && (strategy == STRATEGY_ENFORCED_AUDIBLE))) { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 314 | if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING) |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 315 | == AUDIO_POLICY_FORCE_BT_SCO) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 316 | if (!devices2.isEmpty()) { |
| 317 | devices = devices2; |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 318 | break; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | // Use both Bluetooth SCO and phone default output when ringing in normal mode |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 323 | if (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 324 | if (strategy == STRATEGY_SONIFICATION) { |
| 325 | devices.replaceDevicesByType( |
| 326 | AUDIO_DEVICE_OUT_SPEAKER, |
| 327 | availableOutputDevices.getDevicesFromTypeMask( |
| 328 | AUDIO_DEVICE_OUT_SPEAKER_SAFE)); |
juyuchen | 5fa0aed | 2018-05-16 10:58:37 +0800 | [diff] [blame] | 329 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 330 | if (!devices2.isEmpty()) { |
| 331 | devices.add(devices2); |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 332 | break; |
| 333 | } |
Eric Laurent | a8e0f02 | 2017-01-27 17:41:53 -0800 | [diff] [blame] | 334 | } |
| 335 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 336 | // 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] | 337 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 338 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 339 | case STRATEGY_ACCESSIBILITY: |
| 340 | if (strategy == STRATEGY_ACCESSIBILITY) { |
| 341 | // do not route accessibility prompts to a digital output currently configured with a |
| 342 | // compressed format as they would likely not be mixed and dropped. |
| 343 | for (size_t i = 0; i < outputs.size(); i++) { |
| 344 | sp<AudioOutputDescriptor> desc = outputs.valueAt(i); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame^] | 345 | if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 346 | availableOutputDevices.remove(desc->devices().getDevicesFromTypeMask( |
| 347 | AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF |
| 348 | | AUDIO_DEVICE_OUT_HDMI_ARC)); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 349 | } |
| 350 | } |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 351 | if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) || |
| 352 | outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 353 | return getDevicesForStrategyInt( |
| 354 | STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 355 | } |
| 356 | if (isInCall()) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 357 | return getDevicesForStrategyInt( |
| 358 | STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 359 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 360 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 361 | // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 362 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 363 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 364 | // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 365 | case STRATEGY_REROUTING: |
| 366 | case STRATEGY_MEDIA: { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 367 | DeviceVector devices2; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 368 | if (strategy != STRATEGY_SONIFICATION) { |
| 369 | // no sonification on remote submix (e.g. WFD) |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 370 | sp<DeviceDescriptor> remoteSubmix; |
| 371 | if ((remoteSubmix = availableOutputDevices.getDevice( |
| 372 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"), |
| 373 | AUDIO_FORMAT_DEFAULT)) != nullptr) { |
| 374 | devices2.add(remoteSubmix); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 375 | } |
| 376 | } |
Eric Laurent | a20d4fa | 2015-06-04 18:39:28 -0700 | [diff] [blame] | 377 | if (isInCall() && (strategy == STRATEGY_MEDIA)) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 378 | devices = getDevicesForStrategyInt( |
| 379 | STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs); |
Eric Laurent | a20d4fa | 2015-06-04 18:39:28 -0700 | [diff] [blame] | 380 | break; |
| 381 | } |
juyuchen | ebebb5f | 2019-01-11 14:41:04 +0800 | [diff] [blame] | 382 | // FIXME: Find a better solution to prevent routing to BT hearing aid(b/122931261). |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 383 | if ((devices2.isEmpty()) && |
juyuchen | ebebb5f | 2019-01-11 14:41:04 +0800 | [diff] [blame] | 384 | (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 385 | devices2 = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_HEARING_AID); |
Eric Laurent | 58a73fc | 2018-02-21 18:46:13 -0800 | [diff] [blame] | 386 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 387 | if ((devices2.isEmpty()) && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 388 | (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 389 | outputs.isA2dpSupported()) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 390 | devices2 = availableOutputDevices.getFirstDevicesFromTypes({ |
| 391 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, |
| 392 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER}); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 393 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 394 | if ((devices2.isEmpty()) && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 395 | (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 396 | devices2 = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 397 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 398 | if (devices2.isEmpty()) { |
| 399 | devices2 = availableOutputDevices.getFirstDevicesFromTypes({ |
| 400 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE, AUDIO_DEVICE_OUT_LINE, |
| 401 | AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_USB_HEADSET, |
| 402 | AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_USB_DEVICE, |
| 403 | AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET}); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 404 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 405 | if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 406 | // no sonification on aux digital (e.g. HDMI) |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 407 | devices2 = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_AUX_DIGITAL); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 408 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 409 | if ((devices2.isEmpty()) && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 410 | (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 411 | devices2 = availableOutputDevices.getDevicesFromTypeMask( |
| 412 | AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 413 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 414 | if (devices2.isEmpty()) { |
| 415 | devices2 = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 416 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 417 | DeviceVector devices3; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 418 | if (strategy == STRATEGY_MEDIA) { |
| 419 | // ARC, SPDIF and AUX_LINE can co-exist with others. |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 420 | devices3 = availableOutputDevices.getDevicesFromTypeMask( |
| 421 | AUDIO_DEVICE_OUT_HDMI_ARC | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_AUX_LINE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 422 | } |
| 423 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 424 | devices2.add(devices3); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 425 | // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or |
| 426 | // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 427 | devices.add(devices2); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 428 | |
| 429 | // If hdmi system audio mode is on, remove speaker out of output list. |
| 430 | if ((strategy == STRATEGY_MEDIA) && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 431 | (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) == |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 432 | AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 433 | devices.remove(devices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER)); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 434 | } |
Jean-Michel Trivi | 654afa0 | 2017-05-11 14:12:33 -0700 | [diff] [blame] | 435 | |
| 436 | // for STRATEGY_SONIFICATION: |
| 437 | // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 438 | if (strategy == STRATEGY_SONIFICATION) { |
| 439 | devices.replaceDevicesByType( |
| 440 | AUDIO_DEVICE_OUT_SPEAKER, |
| 441 | availableOutputDevices.getDevicesFromTypeMask( |
| 442 | AUDIO_DEVICE_OUT_SPEAKER_SAFE)); |
Jean-Michel Trivi | 654afa0 | 2017-05-11 14:12:33 -0700 | [diff] [blame] | 443 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 444 | } break; |
| 445 | |
| 446 | default: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 447 | ALOGW("getDevicesForStrategy() unknown strategy: %d", strategy); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 448 | break; |
| 449 | } |
| 450 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 451 | if (devices.isEmpty()) { |
| 452 | ALOGV("getDevicesForStrategy() no device found for strategy %d", strategy); |
| 453 | sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice(); |
| 454 | if (defaultOutputDevice != nullptr) { |
| 455 | devices.add(defaultOutputDevice); |
| 456 | } |
| 457 | ALOGE_IF(devices.isEmpty(), |
| 458 | "getDevicesForStrategy() no default device defined"); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 459 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 460 | |
| 461 | ALOGVV("getDevices" |
| 462 | "ForStrategy() strategy %d, device %x", strategy, devices.types()); |
| 463 | return devices; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 467 | sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 468 | { |
Eric Laurent | af37777 | 2019-03-29 14:50:21 -0700 | [diff] [blame] | 469 | const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
| 470 | const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 471 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 472 | DeviceVector availableDevices = availableInputDevices; |
Eric Laurent | 5ee8d12 | 2019-04-19 15:41:52 -0700 | [diff] [blame] | 473 | sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput(); |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 474 | DeviceVector availablePrimaryDevices = availableInputDevices.getDevicesFromHwModule( |
| 475 | primaryOutput->getModuleHandle()); |
| 476 | sp<DeviceDescriptor> device; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 477 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 478 | // when a call is active, force device selection to match source VOICE_COMMUNICATION |
| 479 | // for most other input sources to avoid rerouting call TX audio |
| 480 | if (isInCall()) { |
| 481 | switch (inputSource) { |
| 482 | case AUDIO_SOURCE_DEFAULT: |
| 483 | case AUDIO_SOURCE_MIC: |
| 484 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
| 485 | case AUDIO_SOURCE_UNPROCESSED: |
| 486 | case AUDIO_SOURCE_HOTWORD: |
| 487 | case AUDIO_SOURCE_CAMCORDER: |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 488 | case AUDIO_SOURCE_VOICE_PERFORMANCE: |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 489 | inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 490 | break; |
| 491 | default: |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 496 | switch (inputSource) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 497 | case AUDIO_SOURCE_DEFAULT: |
| 498 | case AUDIO_SOURCE_MIC: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 499 | device = availableDevices.getDevice( |
| 500 | AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT); |
| 501 | if (device != nullptr) break; |
| 502 | if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) { |
| 503 | device = availableDevices.getDevice( |
| 504 | AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT); |
| 505 | if (device != nullptr) break; |
| 506 | } |
| 507 | device = availableDevices.getFirstExistingDevice({ |
| 508 | AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET, |
| 509 | AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC}); |
| 510 | break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 511 | |
| 512 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
| 513 | // Allow only use of devices on primary input if in call and HAL does not support routing |
| 514 | // to voice call path. |
| 515 | if ((getPhoneState() == AUDIO_MODE_IN_CALL) && |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 516 | (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, |
| 517 | String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) { |
| 518 | availableDevices = availablePrimaryDevices; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 519 | } |
| 520 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 521 | switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 522 | case AUDIO_POLICY_FORCE_BT_SCO: |
| 523 | // 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] | 524 | device = availableDevices.getDevice( |
| 525 | AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT); |
| 526 | if (device != nullptr) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 527 | break; |
| 528 | } |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 529 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 530 | |
| 531 | default: // FORCE_NONE |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 532 | device = availableDevices.getFirstExistingDevice({ |
| 533 | AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET, |
| 534 | AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC}); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 535 | break; |
| 536 | |
| 537 | case AUDIO_POLICY_FORCE_SPEAKER: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 538 | device = availableDevices.getFirstExistingDevice({ |
| 539 | AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC}); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 540 | break; |
| 541 | } |
| 542 | break; |
| 543 | |
| 544 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
rago | 8a397d5 | 2015-12-02 11:27:57 -0800 | [diff] [blame] | 545 | case AUDIO_SOURCE_UNPROCESSED: |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 546 | case AUDIO_SOURCE_HOTWORD: |
Eric Laurent | 5ee8d12 | 2019-04-19 15:41:52 -0700 | [diff] [blame] | 547 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 548 | availableDevices = availablePrimaryDevices; |
Eric Laurent | 5ee8d12 | 2019-04-19 15:41:52 -0700 | [diff] [blame] | 549 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 550 | if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) { |
| 551 | device = availableDevices.getDevice( |
| 552 | AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT); |
| 553 | if (device != nullptr) break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 554 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 555 | device = availableDevices.getFirstExistingDevice({ |
| 556 | AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET, |
| 557 | AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC}); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 558 | break; |
| 559 | case AUDIO_SOURCE_CAMCORDER: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 560 | // For a device without built-in mic, adding usb device |
| 561 | device = availableDevices.getFirstExistingDevice({ |
| 562 | AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC, |
| 563 | AUDIO_DEVICE_IN_USB_DEVICE}); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 564 | break; |
| 565 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 566 | case AUDIO_SOURCE_VOICE_CALL: |
Eric Laurent | 5ee8d12 | 2019-04-19 15:41:52 -0700 | [diff] [blame] | 567 | case AUDIO_SOURCE_VOICE_UPLINK: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 568 | device = availableDevices.getDevice( |
| 569 | AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 570 | break; |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 571 | case AUDIO_SOURCE_VOICE_PERFORMANCE: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 572 | device = availableDevices.getFirstExistingDevice({ |
| 573 | AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET, |
| 574 | AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC}); |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 575 | break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 576 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 577 | device = availableDevices.getDevice( |
| 578 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 579 | break; |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 580 | case AUDIO_SOURCE_FM_TUNER: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 581 | device = availableDevices.getDevice( |
| 582 | AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 583 | break; |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 584 | case AUDIO_SOURCE_ECHO_REFERENCE: |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 585 | device = availableDevices.getDevice( |
| 586 | AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT); |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 587 | break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 588 | default: |
| 589 | ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); |
| 590 | break; |
| 591 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 592 | if (device == nullptr) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 593 | ALOGV("getDeviceForInputSource() no device found for source %d", inputSource); |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 594 | device = availableDevices.getDevice( |
| 595 | AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT); |
| 596 | ALOGE_IF(device == nullptr, |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 597 | "getDeviceForInputSource() no default device defined"); |
| 598 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 599 | ALOGV_IF(device != nullptr, |
| 600 | "getDeviceForInputSource()input source %d, device %08x", |
| 601 | inputSource, device->type()); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 602 | return device; |
| 603 | } |
| 604 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 605 | void Engine::updateDeviceSelectionCache() |
| 606 | { |
| 607 | for (const auto &iter : getProductStrategies()) { |
| 608 | const auto &strategy = iter.second; |
| 609 | auto devices = getDevicesForProductStrategy(strategy->getId()); |
| 610 | mDevicesForStrategies[strategy->getId()] = devices; |
| 611 | strategy->setDeviceTypes(devices.types()); |
| 612 | strategy->setDeviceAddress(devices.getFirstValidAddress().c_str()); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const |
| 617 | { |
| 618 | DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
| 619 | DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
| 620 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
| 621 | |
| 622 | auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ? |
| 623 | mLegacyStrategyMap.at(strategy) : STRATEGY_NONE; |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 624 | return getDevicesForStrategyInt(legacyStrategy, |
| 625 | availableOutputDevices, |
| 626 | availableInputDevices, outputs); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes, |
| 630 | const sp<DeviceDescriptor> &preferredDevice, |
| 631 | bool fromCache) const |
| 632 | { |
| 633 | // First check for explict routing device |
| 634 | if (preferredDevice != nullptr) { |
| 635 | ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str()); |
| 636 | return DeviceVector(preferredDevice); |
| 637 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 638 | product_strategy_t strategy = getProductStrategyForAttributes(attributes); |
Eric Laurent | af37777 | 2019-03-29 14:50:21 -0700 | [diff] [blame] | 639 | const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 640 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 641 | // |
| 642 | // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to |
| 643 | // be by APM? |
| 644 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 645 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 646 | // case the last active client route is used |
| 647 | sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices); |
| 648 | if (device != nullptr) { |
| 649 | return DeviceVector(device); |
| 650 | } |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 651 | |
| 652 | return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy); |
| 653 | } |
| 654 | |
| 655 | DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const |
| 656 | { |
| 657 | auto attributes = getAttributesForStreamType(stream); |
| 658 | return getOutputDevicesForAttributes(attributes, nullptr, fromCache); |
| 659 | } |
| 660 | |
| 661 | sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 662 | sp<AudioPolicyMix> *mix) const |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 663 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 664 | const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection(); |
Eric Laurent | af37777 | 2019-03-29 14:50:21 -0700 | [diff] [blame] | 665 | const auto availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 666 | const auto &inputs = getApmObserver()->getInputs(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 667 | std::string address; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 668 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 669 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 670 | // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered |
| 671 | // first as it used to be by APM? |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 672 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 673 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 674 | // case the last active client route is used |
| 675 | sp<DeviceDescriptor> device = |
| 676 | findPreferredDevice(inputs, attr.source, availableInputDevices); |
| 677 | if (device != nullptr) { |
| 678 | return device; |
| 679 | } |
| 680 | |
| 681 | device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix); |
| 682 | if (device != nullptr) { |
| 683 | return device; |
| 684 | } |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 685 | |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 686 | device = getDeviceForInputSource(attr.source); |
| 687 | if (device == nullptr || !audio_is_remote_submix_device(device->type())) { |
| 688 | // Return immediately if the device is null or it is not a remote submix device. |
| 689 | return device; |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 690 | } |
jiabin | f502d15 | 2019-08-07 14:43:33 -0700 | [diff] [blame] | 691 | |
| 692 | // For remote submix device, try to find the device by address. |
| 693 | address = "0"; |
| 694 | std::size_t pos; |
| 695 | std::string tags { attr.tags }; |
| 696 | if ((pos = tags.find("addr=")) != std::string::npos) { |
| 697 | address = tags.substr(pos + std::strlen("addr=")); |
| 698 | } |
| 699 | return availableInputDevices.getDevice(device->type(), |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 700 | String8(address.c_str()), |
| 701 | AUDIO_FORMAT_DEFAULT); |
| 702 | } |
| 703 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 704 | } // namespace audio_policy |
| 705 | } // namespace android |
| 706 | |
| 707 | |