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::setPhoneState(audio_mode_t state) |
| 70 | { |
| 71 | ALOGV("setPhoneState() state %d", state); |
| 72 | |
| 73 | if (state < 0 || state >= AUDIO_MODE_CNT) { |
| 74 | ALOGW("setPhoneState() invalid state %d", state); |
| 75 | return BAD_VALUE; |
| 76 | } |
| 77 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 78 | if (state == getPhoneState()) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 79 | ALOGW("setPhoneState() setting same state %d", state); |
| 80 | return BAD_VALUE; |
| 81 | } |
| 82 | |
| 83 | // store previous phone state for management of sonification strategy below |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 84 | int oldState = getPhoneState(); |
| 85 | EngineBase::setPhoneState(state); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 86 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 87 | if (!is_state_in_call(oldState) && is_state_in_call(state)) { |
| 88 | ALOGV(" Entering call in setPhoneState()"); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 89 | getApmObserver()->getVolumeCurves().switchVolumeCurve(AUDIO_STREAM_VOICE_CALL, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 90 | AUDIO_STREAM_DTMF); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 91 | } else if (is_state_in_call(oldState) && !is_state_in_call(state)) { |
| 92 | ALOGV(" Exiting call in setPhoneState()"); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 93 | getApmObserver()->getVolumeCurves().restoreOriginVolumeCurve(AUDIO_STREAM_DTMF); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 94 | } |
| 95 | return NO_ERROR; |
| 96 | } |
| 97 | |
| 98 | status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) |
| 99 | { |
| 100 | switch(usage) { |
| 101 | case AUDIO_POLICY_FORCE_FOR_COMMUNICATION: |
| 102 | if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO && |
| 103 | config != AUDIO_POLICY_FORCE_NONE) { |
| 104 | ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); |
| 105 | return BAD_VALUE; |
| 106 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 107 | break; |
| 108 | case AUDIO_POLICY_FORCE_FOR_MEDIA: |
| 109 | if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP && |
| 110 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 111 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 112 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE && |
| 113 | config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) { |
| 114 | ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config); |
| 115 | return BAD_VALUE; |
| 116 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 117 | break; |
| 118 | case AUDIO_POLICY_FORCE_FOR_RECORD: |
| 119 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 120 | config != AUDIO_POLICY_FORCE_NONE) { |
| 121 | ALOGW("setForceUse() invalid config %d for FOR_RECORD", config); |
| 122 | return BAD_VALUE; |
| 123 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 124 | break; |
| 125 | case AUDIO_POLICY_FORCE_FOR_DOCK: |
| 126 | if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK && |
| 127 | config != AUDIO_POLICY_FORCE_BT_DESK_DOCK && |
| 128 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 129 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 130 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) { |
| 131 | ALOGW("setForceUse() invalid config %d for FOR_DOCK", config); |
| 132 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 133 | break; |
| 134 | case AUDIO_POLICY_FORCE_FOR_SYSTEM: |
| 135 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 136 | config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 137 | ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); |
| 138 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 139 | break; |
| 140 | case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO: |
| 141 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 142 | config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) { |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 143 | ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config); |
| 144 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 145 | break; |
| 146 | case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND: |
| 147 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 148 | config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER && |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 149 | config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS && |
| 150 | config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 151 | ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config); |
| 152 | return BAD_VALUE; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 153 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 154 | break; |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 155 | case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING: |
| 156 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) { |
| 157 | ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config); |
| 158 | return BAD_VALUE; |
| 159 | } |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 160 | break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 161 | default: |
| 162 | ALOGW("setForceUse() invalid usage %d", usage); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 163 | break; // TODO return BAD_VALUE? |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 164 | } |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 165 | return EngineBase::setForceUse(usage, config); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | routing_strategy Engine::getStrategyForStream(audio_stream_type_t stream) |
| 169 | { |
| 170 | // stream to strategy mapping |
| 171 | switch (stream) { |
| 172 | case AUDIO_STREAM_VOICE_CALL: |
| 173 | case AUDIO_STREAM_BLUETOOTH_SCO: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 174 | return android::STRATEGY_PHONE; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 175 | case AUDIO_STREAM_RING: |
| 176 | case AUDIO_STREAM_ALARM: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 177 | return android::STRATEGY_SONIFICATION; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 178 | case AUDIO_STREAM_NOTIFICATION: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 179 | return android::STRATEGY_SONIFICATION_RESPECTFUL; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 180 | case AUDIO_STREAM_DTMF: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 181 | return android::STRATEGY_DTMF; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 182 | default: |
| 183 | ALOGE("unknown stream type %d", stream); |
Andy Hung | 320fd85 | 2018-10-09 14:06:37 -0700 | [diff] [blame] | 184 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 185 | case AUDIO_STREAM_SYSTEM: |
| 186 | // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs |
| 187 | // while key clicks are played produces a poor result |
| 188 | case AUDIO_STREAM_MUSIC: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 189 | return android::STRATEGY_MEDIA; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 190 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 191 | return android::STRATEGY_ENFORCED_AUDIBLE; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 192 | case AUDIO_STREAM_TTS: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 193 | return android::STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 194 | case AUDIO_STREAM_ACCESSIBILITY: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 195 | return android::STRATEGY_ACCESSIBILITY; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 196 | case AUDIO_STREAM_REROUTING: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 197 | return android::STRATEGY_REROUTING; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | routing_strategy Engine::getStrategyForUsage(audio_usage_t usage) |
| 202 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 203 | // usage to strategy mapping |
| 204 | switch (usage) { |
| 205 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 206 | return android::STRATEGY_ACCESSIBILITY; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 207 | |
| 208 | case AUDIO_USAGE_MEDIA: |
| 209 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 210 | case AUDIO_USAGE_ASSISTANT: |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 211 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 212 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 213 | return android::STRATEGY_MEDIA; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 214 | |
| 215 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 216 | return android::STRATEGY_PHONE; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 217 | |
| 218 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 219 | return android::STRATEGY_DTMF; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 220 | |
| 221 | case AUDIO_USAGE_ALARM: |
| 222 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 223 | return android::STRATEGY_SONIFICATION; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 224 | |
| 225 | case AUDIO_USAGE_NOTIFICATION: |
| 226 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 227 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 228 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 229 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 230 | return android::STRATEGY_SONIFICATION_RESPECTFUL; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 231 | |
| 232 | case AUDIO_USAGE_UNKNOWN: |
| 233 | default: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 234 | return android::STRATEGY_MEDIA; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | audio_devices_t Engine::getDeviceForStrategy(routing_strategy strategy) const |
| 239 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 240 | DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
| 241 | DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 242 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 243 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 244 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame^] | 245 | return getDeviceForStrategyInt(static_cast<legacy_strategy>(strategy), |
| 246 | availableOutputDevices, |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 247 | availableInputDevices, outputs, (uint32_t)AUDIO_DEVICE_NONE); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 248 | } |
| 249 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 250 | audio_devices_t Engine::getDeviceForStrategyInt(legacy_strategy strategy, |
| 251 | DeviceVector availableOutputDevices, |
| 252 | DeviceVector availableInputDevices, |
| 253 | const SwAudioOutputCollection &outputs, |
| 254 | uint32_t outputDeviceTypesToIgnore) const |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 255 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 256 | uint32_t device = AUDIO_DEVICE_NONE; |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 257 | uint32_t availableOutputDevicesType = |
| 258 | availableOutputDevices.types() & ~outputDeviceTypesToIgnore; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 259 | |
| 260 | switch (strategy) { |
| 261 | |
| 262 | case STRATEGY_TRANSMITTED_THROUGH_SPEAKER: |
| 263 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 264 | break; |
| 265 | |
| 266 | case STRATEGY_SONIFICATION_RESPECTFUL: |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 267 | if (isInCall() || outputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 268 | device = getDeviceForStrategyInt( |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 269 | STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs, |
| 270 | outputDeviceTypesToIgnore); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 271 | } else { |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 272 | bool media_active_locally = |
| 273 | outputs.isStreamActiveLocally( |
| 274 | AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY) |
| 275 | || outputs.isStreamActiveLocally( |
| 276 | AUDIO_STREAM_ACCESSIBILITY, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY); |
| 277 | // routing is same as media without the "remote" device |
| 278 | device = getDeviceForStrategyInt(STRATEGY_MEDIA, |
| 279 | availableOutputDevices, |
| 280 | availableInputDevices, outputs, |
| 281 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX | outputDeviceTypesToIgnore); |
| 282 | // if no media is playing on the device, check for mandatory use of "safe" speaker |
| 283 | // when media would have played on speaker, and the safe speaker path is available |
| 284 | if (!media_active_locally |
| 285 | && (device & AUDIO_DEVICE_OUT_SPEAKER) |
| 286 | && (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
Eric Laurent | 9a7d922 | 2015-07-02 15:30:23 -0700 | [diff] [blame] | 287 | device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 288 | device &= ~AUDIO_DEVICE_OUT_SPEAKER; |
| 289 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 290 | } |
| 291 | break; |
| 292 | |
| 293 | case STRATEGY_DTMF: |
| 294 | if (!isInCall()) { |
| 295 | // when off call, DTMF strategy follows the same rules as MEDIA strategy |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 296 | device = getDeviceForStrategyInt( |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 297 | STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs, |
| 298 | outputDeviceTypesToIgnore); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 299 | break; |
| 300 | } |
| 301 | // when in call, DTMF and PHONE strategies follow the same rules |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 302 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 303 | |
| 304 | case STRATEGY_PHONE: |
| 305 | // Force use of only devices on primary output if: |
| 306 | // - in call AND |
| 307 | // - cannot route from voice call RX OR |
| 308 | // - audio HAL version is < 3.0 and TX device is on the primary HW module |
| 309 | if (getPhoneState() == AUDIO_MODE_IN_CALL) { |
| 310 | audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 311 | sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput(); |
| 312 | audio_devices_t availPrimaryInputDevices = |
Mikhail Naganov | 9366193 | 2018-07-26 14:37:41 -0700 | [diff] [blame] | 313 | availableInputDevices.getDeviceTypesFromHwModule(primaryOutput->getModuleHandle()); |
Eric Laurent | 58a73fc | 2018-02-21 18:46:13 -0800 | [diff] [blame] | 314 | |
| 315 | // TODO: getPrimaryOutput return only devices from first module in |
| 316 | // audio_policy_configuration.xml, hearing aid is not there, but it's |
| 317 | // a primary device |
| 318 | // FIXME: this is not the right way of solving this problem |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 319 | audio_devices_t availPrimaryOutputDevices = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 320 | (primaryOutput->supportedDevices().types() | AUDIO_DEVICE_OUT_HEARING_AID) & |
Eric Laurent | 58a73fc | 2018-02-21 18:46:13 -0800 | [diff] [blame] | 321 | availableOutputDevices.types(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 322 | |
| 323 | if (((availableInputDevices.types() & |
| 324 | AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) || |
| 325 | (((txDevice & availPrimaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 326 | (primaryOutput->getAudioPort()->getModuleVersionMajor() < 3))) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 327 | availableOutputDevicesType = availPrimaryOutputDevices; |
| 328 | } |
| 329 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 330 | // for phone strategy, we first consider the forced use and then the available devices by |
| 331 | // order of priority |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 332 | switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 333 | case AUDIO_POLICY_FORCE_BT_SCO: |
| 334 | if (!isInCall() || strategy != STRATEGY_DTMF) { |
| 335 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
| 336 | if (device) break; |
| 337 | } |
| 338 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
| 339 | if (device) break; |
| 340 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; |
| 341 | if (device) break; |
| 342 | // 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] | 343 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 344 | |
| 345 | default: // FORCE_NONE |
Eric Laurent | 58a73fc | 2018-02-21 18:46:13 -0800 | [diff] [blame] | 346 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_HEARING_AID; |
| 347 | if (device) break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 348 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP |
| 349 | if (!isInCall() && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 350 | (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] | 351 | outputs.isA2dpSupported()) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 352 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; |
| 353 | if (device) break; |
| 354 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
| 355 | if (device) break; |
| 356 | } |
| 357 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
| 358 | if (device) break; |
| 359 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET; |
| 360 | if (device) break; |
Eric Laurent | a0b18ce | 2016-03-08 11:05:00 -0800 | [diff] [blame] | 361 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE; |
| 362 | if (device) break; |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 363 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET; |
| 364 | if (device) break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 365 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 366 | if (device) break; |
| 367 | if (!isInCall()) { |
| 368 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
| 369 | if (device) break; |
| 370 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
| 371 | if (device) break; |
| 372 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
| 373 | if (device) break; |
| 374 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 375 | if (device) break; |
| 376 | } |
| 377 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_EARPIECE; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 378 | break; |
| 379 | |
| 380 | case AUDIO_POLICY_FORCE_SPEAKER: |
| 381 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to |
| 382 | // A2DP speaker when forcing to speaker output |
| 383 | if (!isInCall() && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 384 | (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] | 385 | outputs.isA2dpSupported()) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 386 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
| 387 | if (device) break; |
| 388 | } |
| 389 | if (!isInCall()) { |
| 390 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
| 391 | if (device) break; |
| 392 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 393 | if (device) break; |
| 394 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
| 395 | if (device) break; |
| 396 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
| 397 | if (device) break; |
| 398 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 399 | if (device) break; |
| 400 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 401 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 402 | break; |
| 403 | } |
| 404 | break; |
| 405 | |
| 406 | case STRATEGY_SONIFICATION: |
| 407 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 408 | // If incall, just select the STRATEGY_PHONE device |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 409 | if (isInCall() || outputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 410 | device = getDeviceForStrategyInt( |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 411 | STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs, |
| 412 | outputDeviceTypesToIgnore); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 413 | break; |
| 414 | } |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 415 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 416 | |
| 417 | case STRATEGY_ENFORCED_AUDIBLE: |
| 418 | // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION |
| 419 | // except: |
| 420 | // - when in call where it doesn't default to STRATEGY_PHONE behavior |
| 421 | // - in countries where not enforced in which case it follows STRATEGY_MEDIA |
| 422 | |
| 423 | if ((strategy == STRATEGY_SONIFICATION) || |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 424 | (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 425 | device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 426 | } |
Eric Laurent | a8e0f02 | 2017-01-27 17:41:53 -0800 | [diff] [blame] | 427 | |
| 428 | // if SCO headset is connected and we are told to use it, play ringtone over |
| 429 | // speaker and BT SCO |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 430 | if ((availableOutputDevicesType & AUDIO_DEVICE_OUT_ALL_SCO) != 0) { |
Eric Laurent | a8e0f02 | 2017-01-27 17:41:53 -0800 | [diff] [blame] | 431 | uint32_t device2 = AUDIO_DEVICE_NONE; |
| 432 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
| 433 | if (device2 == AUDIO_DEVICE_NONE) { |
| 434 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
| 435 | } |
| 436 | if (device2 == AUDIO_DEVICE_NONE) { |
| 437 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; |
| 438 | } |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 439 | // Use ONLY Bluetooth SCO output when ringing in vibration mode |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 440 | if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 441 | && (strategy == STRATEGY_ENFORCED_AUDIBLE))) { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 442 | if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING) |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 443 | == AUDIO_POLICY_FORCE_BT_SCO) { |
| 444 | if (device2 != AUDIO_DEVICE_NONE) { |
| 445 | device = device2; |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | // 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] | 451 | if (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) { |
juyuchen | 5fa0aed | 2018-05-16 10:58:37 +0800 | [diff] [blame] | 452 | if ((strategy == STRATEGY_SONIFICATION) && |
| 453 | (device & AUDIO_DEVICE_OUT_SPEAKER) && |
| 454 | (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
| 455 | device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 456 | device &= ~AUDIO_DEVICE_OUT_SPEAKER; |
| 457 | } |
Jack He | 96117ae | 2018-02-12 20:52:53 -0800 | [diff] [blame] | 458 | if (device2 != AUDIO_DEVICE_NONE) { |
| 459 | device |= device2; |
| 460 | break; |
| 461 | } |
Eric Laurent | a8e0f02 | 2017-01-27 17:41:53 -0800 | [diff] [blame] | 462 | } |
| 463 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 464 | // 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] | 465 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 466 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 467 | case STRATEGY_ACCESSIBILITY: |
| 468 | if (strategy == STRATEGY_ACCESSIBILITY) { |
| 469 | // do not route accessibility prompts to a digital output currently configured with a |
| 470 | // compressed format as they would likely not be mixed and dropped. |
| 471 | for (size_t i = 0; i < outputs.size(); i++) { |
| 472 | sp<AudioOutputDescriptor> desc = outputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 473 | audio_devices_t devices = desc->devices().types() & |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 474 | (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC); |
| 475 | if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) && |
| 476 | devices != AUDIO_DEVICE_NONE) { |
| 477 | availableOutputDevicesType = availableOutputDevices.types() & ~devices; |
| 478 | } |
| 479 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 480 | availableOutputDevices = |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 481 | availableOutputDevices.getDevicesFromTypeMask(availableOutputDevicesType); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 482 | if (outputs.isStreamActive(AUDIO_STREAM_RING) || |
| 483 | outputs.isStreamActive(AUDIO_STREAM_ALARM)) { |
| 484 | return getDeviceForStrategyInt( |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 485 | STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs, |
| 486 | outputDeviceTypesToIgnore); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 487 | } |
| 488 | if (isInCall()) { |
| 489 | return getDeviceForStrategyInt( |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 490 | STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs, |
| 491 | outputDeviceTypesToIgnore); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 492 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 493 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 494 | // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 495 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 496 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 497 | // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 498 | case STRATEGY_REROUTING: |
| 499 | case STRATEGY_MEDIA: { |
| 500 | uint32_t device2 = AUDIO_DEVICE_NONE; |
| 501 | if (strategy != STRATEGY_SONIFICATION) { |
| 502 | // no sonification on remote submix (e.g. WFD) |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 503 | if (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 504 | String8("0"), AUDIO_FORMAT_DEFAULT) != 0) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 505 | device2 = availableOutputDevices.types() & AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 506 | } |
| 507 | } |
Eric Laurent | a20d4fa | 2015-06-04 18:39:28 -0700 | [diff] [blame] | 508 | if (isInCall() && (strategy == STRATEGY_MEDIA)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 509 | device = getDeviceForStrategyInt( |
Jean-Michel Trivi | 6130952 | 2018-01-23 09:58:17 -0800 | [diff] [blame] | 510 | STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs, |
| 511 | outputDeviceTypesToIgnore); |
Eric Laurent | a20d4fa | 2015-06-04 18:39:28 -0700 | [diff] [blame] | 512 | break; |
| 513 | } |
Eric Laurent | 58a73fc | 2018-02-21 18:46:13 -0800 | [diff] [blame] | 514 | if (device2 == AUDIO_DEVICE_NONE) { |
| 515 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HEARING_AID; |
| 516 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 517 | if ((device2 == AUDIO_DEVICE_NONE) && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 518 | (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] | 519 | outputs.isA2dpSupported()) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 520 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; |
| 521 | if (device2 == AUDIO_DEVICE_NONE) { |
| 522 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
| 523 | } |
| 524 | if (device2 == AUDIO_DEVICE_NONE) { |
| 525 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
| 526 | } |
| 527 | } |
| 528 | if ((device2 == AUDIO_DEVICE_NONE) && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 529 | (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 530 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; |
| 531 | } |
| 532 | if (device2 == AUDIO_DEVICE_NONE) { |
| 533 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
| 534 | } |
Jean-Michel Trivi | 5c233f8 | 2015-04-03 09:21:24 -0700 | [diff] [blame] | 535 | if (device2 == AUDIO_DEVICE_NONE) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 536 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE; |
| 537 | } |
| 538 | if (device2 == AUDIO_DEVICE_NONE) { |
| 539 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET; |
| 540 | } |
| 541 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 542 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET; |
| 543 | } |
| 544 | if (device2 == AUDIO_DEVICE_NONE) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 545 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
| 546 | } |
| 547 | if (device2 == AUDIO_DEVICE_NONE) { |
| 548 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 549 | } |
| 550 | if (device2 == AUDIO_DEVICE_NONE) { |
| 551 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
| 552 | } |
| 553 | if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) { |
| 554 | // no sonification on aux digital (e.g. HDMI) |
| 555 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
| 556 | } |
| 557 | if ((device2 == AUDIO_DEVICE_NONE) && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 558 | (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 559 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 560 | } |
| 561 | if (device2 == AUDIO_DEVICE_NONE) { |
| 562 | device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; |
| 563 | } |
| 564 | int device3 = AUDIO_DEVICE_NONE; |
| 565 | if (strategy == STRATEGY_MEDIA) { |
| 566 | // ARC, SPDIF and AUX_LINE can co-exist with others. |
| 567 | device3 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HDMI_ARC; |
| 568 | device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPDIF); |
| 569 | device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_LINE); |
| 570 | } |
| 571 | |
| 572 | device2 |= device3; |
| 573 | // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or |
| 574 | // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise |
| 575 | device |= device2; |
| 576 | |
| 577 | // If hdmi system audio mode is on, remove speaker out of output list. |
| 578 | if ((strategy == STRATEGY_MEDIA) && |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 579 | (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) == |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 580 | AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) { |
| 581 | device &= ~AUDIO_DEVICE_OUT_SPEAKER; |
| 582 | } |
Jean-Michel Trivi | 654afa0 | 2017-05-11 14:12:33 -0700 | [diff] [blame] | 583 | |
| 584 | // for STRATEGY_SONIFICATION: |
| 585 | // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead |
| 586 | if ((strategy == STRATEGY_SONIFICATION) && |
| 587 | (device & AUDIO_DEVICE_OUT_SPEAKER) && |
| 588 | (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
| 589 | device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 590 | device &= ~AUDIO_DEVICE_OUT_SPEAKER; |
| 591 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 592 | } break; |
| 593 | |
| 594 | default: |
| 595 | ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy); |
| 596 | break; |
| 597 | } |
| 598 | |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 599 | if (device == AUDIO_DEVICE_NONE) { |
| 600 | ALOGV("getDeviceForStrategy() no device found for strategy %d", strategy); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 601 | device = getApmObserver()->getDefaultOutputDevice()->type(); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 602 | ALOGE_IF(device == AUDIO_DEVICE_NONE, |
| 603 | "getDeviceForStrategy() no default device defined"); |
| 604 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 605 | ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device); |
| 606 | return device; |
| 607 | } |
| 608 | |
| 609 | |
| 610 | audio_devices_t Engine::getDeviceForInputSource(audio_source_t inputSource) const |
| 611 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 612 | const DeviceVector &availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
| 613 | const DeviceVector &availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
| 614 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 615 | audio_devices_t availableDeviceTypes = availableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 616 | |
| 617 | uint32_t device = AUDIO_DEVICE_NONE; |
| 618 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 619 | // when a call is active, force device selection to match source VOICE_COMMUNICATION |
| 620 | // for most other input sources to avoid rerouting call TX audio |
| 621 | if (isInCall()) { |
| 622 | switch (inputSource) { |
| 623 | case AUDIO_SOURCE_DEFAULT: |
| 624 | case AUDIO_SOURCE_MIC: |
| 625 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
| 626 | case AUDIO_SOURCE_UNPROCESSED: |
| 627 | case AUDIO_SOURCE_HOTWORD: |
| 628 | case AUDIO_SOURCE_CAMCORDER: |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 629 | case AUDIO_SOURCE_VOICE_PERFORMANCE: |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 630 | inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 631 | break; |
| 632 | default: |
| 633 | break; |
| 634 | } |
| 635 | } |
| 636 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 637 | switch (inputSource) { |
| 638 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 639 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { |
| 640 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
| 641 | break; |
| 642 | } |
| 643 | break; |
| 644 | |
| 645 | case AUDIO_SOURCE_DEFAULT: |
| 646 | case AUDIO_SOURCE_MIC: |
| 647 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) { |
| 648 | device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP; |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 649 | } else if ((getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 650 | (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) { |
| 651 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 652 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 653 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 654 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) { |
| 655 | device = AUDIO_DEVICE_IN_USB_HEADSET; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 656 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 657 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 658 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 659 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 660 | } |
| 661 | break; |
| 662 | |
| 663 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
| 664 | // Allow only use of devices on primary input if in call and HAL does not support routing |
| 665 | // to voice call path. |
| 666 | if ((getPhoneState() == AUDIO_MODE_IN_CALL) && |
| 667 | (availableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) { |
| 668 | sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput(); |
Mikhail Naganov | 9366193 | 2018-07-26 14:37:41 -0700 | [diff] [blame] | 669 | availableDeviceTypes = availableInputDevices.getDeviceTypesFromHwModule( |
| 670 | primaryOutput->getModuleHandle()) & ~AUDIO_DEVICE_BIT_IN; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 671 | } |
| 672 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 673 | switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 674 | case AUDIO_POLICY_FORCE_BT_SCO: |
| 675 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 676 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
| 677 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 678 | break; |
| 679 | } |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 680 | FALLTHROUGH_INTENDED; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 681 | |
| 682 | default: // FORCE_NONE |
| 683 | if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 684 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 685 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) { |
| 686 | device = AUDIO_DEVICE_IN_USB_HEADSET; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 687 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 688 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 689 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 690 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 691 | } |
| 692 | break; |
| 693 | |
| 694 | case AUDIO_POLICY_FORCE_SPEAKER: |
| 695 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { |
| 696 | device = AUDIO_DEVICE_IN_BACK_MIC; |
| 697 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 698 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 699 | } |
| 700 | break; |
| 701 | } |
| 702 | break; |
| 703 | |
| 704 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
rago | 8a397d5 | 2015-12-02 11:27:57 -0800 | [diff] [blame] | 705 | case AUDIO_SOURCE_UNPROCESSED: |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 706 | case AUDIO_SOURCE_HOTWORD: |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 707 | if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 708 | availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
| 709 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 710 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 711 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 712 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) { |
| 713 | device = AUDIO_DEVICE_IN_USB_HEADSET; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 714 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 715 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 716 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 717 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 718 | } |
| 719 | break; |
| 720 | case AUDIO_SOURCE_CAMCORDER: |
| 721 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { |
| 722 | device = AUDIO_DEVICE_IN_BACK_MIC; |
| 723 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 724 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
Takeshi Oishi | 86ad3d1 | 2018-07-20 16:13:06 +0900 | [diff] [blame] | 725 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 726 | // This is specifically for a device without built-in mic |
| 727 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 728 | } |
| 729 | break; |
| 730 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 731 | case AUDIO_SOURCE_VOICE_CALL: |
| 732 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { |
| 733 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
| 734 | } |
| 735 | break; |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 736 | case AUDIO_SOURCE_VOICE_PERFORMANCE: |
| 737 | if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 738 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 739 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) { |
| 740 | device = AUDIO_DEVICE_IN_USB_HEADSET; |
| 741 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 742 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 743 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 744 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 745 | } |
| 746 | break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 747 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
| 748 | if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
| 749 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 750 | } |
| 751 | break; |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 752 | case AUDIO_SOURCE_FM_TUNER: |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 753 | if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) { |
| 754 | device = AUDIO_DEVICE_IN_FM_TUNER; |
| 755 | } |
| 756 | break; |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 757 | case AUDIO_SOURCE_ECHO_REFERENCE: |
| 758 | if (availableDeviceTypes & AUDIO_DEVICE_IN_ECHO_REFERENCE) { |
| 759 | device = AUDIO_DEVICE_IN_ECHO_REFERENCE; |
| 760 | } |
| 761 | break; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 762 | default: |
| 763 | ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); |
| 764 | break; |
| 765 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 766 | if (device == AUDIO_DEVICE_NONE) { |
| 767 | ALOGV("getDeviceForInputSource() no device found for source %d", inputSource); |
| 768 | if (availableDeviceTypes & AUDIO_DEVICE_IN_STUB) { |
| 769 | device = AUDIO_DEVICE_IN_STUB; |
| 770 | } |
| 771 | ALOGE_IF(device == AUDIO_DEVICE_NONE, |
| 772 | "getDeviceForInputSource() no default device defined"); |
| 773 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 774 | ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); |
| 775 | return device; |
| 776 | } |
| 777 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 778 | void Engine::updateDeviceSelectionCache() |
| 779 | { |
| 780 | for (const auto &iter : getProductStrategies()) { |
| 781 | const auto &strategy = iter.second; |
| 782 | auto devices = getDevicesForProductStrategy(strategy->getId()); |
| 783 | mDevicesForStrategies[strategy->getId()] = devices; |
| 784 | strategy->setDeviceTypes(devices.types()); |
| 785 | strategy->setDeviceAddress(devices.getFirstValidAddress().c_str()); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const |
| 790 | { |
| 791 | DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
| 792 | DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
| 793 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
| 794 | |
| 795 | auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ? |
| 796 | mLegacyStrategyMap.at(strategy) : STRATEGY_NONE; |
| 797 | audio_devices_t devices = getDeviceForStrategyInt(legacyStrategy, |
| 798 | availableOutputDevices, |
| 799 | availableInputDevices, outputs, |
| 800 | (uint32_t)AUDIO_DEVICE_NONE); |
| 801 | return availableOutputDevices.getDevicesFromTypeMask(devices); |
| 802 | } |
| 803 | |
| 804 | DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes, |
| 805 | const sp<DeviceDescriptor> &preferredDevice, |
| 806 | bool fromCache) const |
| 807 | { |
| 808 | // First check for explict routing device |
| 809 | if (preferredDevice != nullptr) { |
| 810 | ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str()); |
| 811 | return DeviceVector(preferredDevice); |
| 812 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame^] | 813 | product_strategy_t strategy = getProductStrategyForAttributes(attributes); |
| 814 | const DeviceVector &availableOutputDevices = getApmObserver()->getAvailableOutputDevices(); |
| 815 | const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 816 | // |
| 817 | // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to |
| 818 | // be by APM? |
| 819 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame^] | 820 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 821 | // case the last active client route is used |
| 822 | sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices); |
| 823 | if (device != nullptr) { |
| 824 | return DeviceVector(device); |
| 825 | } |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 826 | |
| 827 | return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy); |
| 828 | } |
| 829 | |
| 830 | DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const |
| 831 | { |
| 832 | auto attributes = getAttributesForStreamType(stream); |
| 833 | return getOutputDevicesForAttributes(attributes, nullptr, fromCache); |
| 834 | } |
| 835 | |
| 836 | sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame^] | 837 | AudioMix **mix) const |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 838 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame^] | 839 | const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 840 | const auto &availableInputDevices = getApmObserver()->getAvailableInputDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame^] | 841 | const auto &inputs = getApmObserver()->getInputs(); |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 842 | std::string address; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame^] | 843 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 844 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame^] | 845 | // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered |
| 846 | // first as it used to be by APM? |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 847 | // |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame^] | 848 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 849 | // case the last active client route is used |
| 850 | sp<DeviceDescriptor> device = |
| 851 | findPreferredDevice(inputs, attr.source, availableInputDevices); |
| 852 | if (device != nullptr) { |
| 853 | return device; |
| 854 | } |
| 855 | |
| 856 | device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix); |
| 857 | if (device != nullptr) { |
| 858 | return device; |
| 859 | } |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 860 | audio_devices_t deviceType = getDeviceForInputSource(attr.source); |
| 861 | |
| 862 | if (audio_is_remote_submix_device(deviceType)) { |
| 863 | address = "0"; |
| 864 | std::size_t pos; |
| 865 | std::string tags { attr.tags }; |
| 866 | if ((pos = tags.find("addr=")) != std::string::npos) { |
| 867 | address = tags.substr(pos + std::strlen("addr=")); |
| 868 | } |
| 869 | } |
| 870 | return availableInputDevices.getDevice(deviceType, |
| 871 | String8(address.c_str()), |
| 872 | AUDIO_FORMAT_DEFAULT); |
| 873 | } |
| 874 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 875 | template <> |
| 876 | AudioPolicyManagerInterface *Engine::queryInterface() |
| 877 | { |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 878 | return this; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | } // namespace audio_policy |
| 882 | } // namespace android |
| 883 | |
| 884 | |