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